5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2000 - 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silcske/SILC SKE Interface
24 * The SILC Key Exchange (SKE) protocol interface. The SKE protocol
25 * is used to negotiate secret key material between two parties, to be used
26 * as session key or some other key. For example, when client connects to
27 * server SKE is performed to exchange public keys, and to generate the key
28 * that is then used as session key. Two clients can execute SKE as well
29 * two create secret key material for securing for example file transfer
30 * stream. This SKE implementation provides easy interface for application
31 * that wants to use SKE.
38 /* Forward declarations */
39 typedef struct SilcSKECallbacksStruct *SilcSKECallbacks;
40 typedef struct SilcSKEStruct *SilcSKE;
42 /****d* silcske/SilcSKEAPI/SilcSKEStatus
46 * typedef enum { ... } SilcSKEStatus;
50 * Status types returned in SKE callbacks. This tell the status of
51 * the SKE session, and if an error occurred. Application can map the
52 * status to human readable string with silc_ske_map_status function.
57 /* These are defined by the protocol */
58 SILC_SKE_STATUS_OK = 0, /* No error */
59 SILC_SKE_STATUS_ERROR = 1, /* Unknown error */
60 SILC_SKE_STATUS_BAD_PAYLOAD = 2, /* Malformed payload */
61 SILC_SKE_STATUS_UNKNOWN_GROUP = 3, /* Unsupported DH group */
62 SILC_SKE_STATUS_UNKNOWN_CIPHER = 4, /* Unsupported cipher */
63 SILC_SKE_STATUS_UNKNOWN_PKCS = 5, /* Unsupported PKCS algorithm */
64 SILC_SKE_STATUS_UNKNOWN_HASH_FUNCTION = 6, /* Unsupported hash function */
65 SILC_SKE_STATUS_UNKNOWN_HMAC = 7, /* Unsupported HMAC */
66 SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY = 8, /* Unsupported/not trusted PK */
67 SILC_SKE_STATUS_INCORRECT_SIGNATURE = 9, /* Incorrect signature */
68 SILC_SKE_STATUS_BAD_VERSION = 10, /* Unsupported version */
69 SILC_SKE_STATUS_INVALID_COOKIE = 11, /* Cookie was modified */
71 /* Implementation specific status types */
72 SILC_SKE_STATUS_PUBLIC_KEY_NOT_PROVIDED, /* Remote did not send PK */
73 SILC_SKE_STATUS_BAD_RESERVED_FIELD, /* Reserved field was not 0 */
74 SILC_SKE_STATUS_BAD_PAYLOAD_LENGTH, /* Payload includes garbage */
75 SILC_SKE_STATUS_SIGNATURE_ERROR, /* Error computing signature */
76 SILC_SKE_STATUS_OUT_OF_MEMORY, /* System out of memory */
80 #include "silcske_groups.h"
81 #include "silcske_payload.h"
83 /****d* silcske/SilcSKEAPI/SilcSKESecurityPropertyFlag
87 * typedef enum { ... } SilcSKESecurityPropertyFlag
91 * SKE security property flags as defined by the SK protocol.
96 SILC_SKE_SP_FLAG_NONE = 0x00, /* No flags */
97 SILC_SKE_SP_FLAG_IV_INCLUDED = 0x01, /* IV included in packet */
98 SILC_SKE_SP_FLAG_PFS = 0x02, /* Perfect Forward Secrecy */
99 SILC_SKE_SP_FLAG_MUTUAL = 0x04, /* Mutual authentication */
100 } SilcSKESecurityPropertyFlag;
103 /****s* silcske/SilcSKEAPI/SilcSKESecurityProperties
107 * typedef struct { ... } *SilcSKESecurityProperties;
111 * Security Properties negotiated between key exchange parties. This
112 * structure is filled from the Key Exchange Start Payload which is used
113 * to negotiate what security properties must be used in the
119 SilcSKESecurityPropertyFlag flags; /* Flags */
120 SilcSKEDiffieHellmanGroup group; /* Selected Diffie Hellman group */
121 SilcCipher cipher; /* Selected cipher */
122 SilcHmac hmac; /* Selected HMAC */
123 SilcHash hash; /* Selected hash algorithm */
124 SilcPublicKey public_key; /* Remote public key */
125 } *SilcSKESecurityProperties;
128 /****s* silcske/SilcSKEAPI/SilcSKEKeyMaterial
132 * typedef struct { ... } *SilcSKEKeyMaterial;
136 * This is the key material structure, and is passed as argument by the
137 * application to silc_ske_process_key_material* functions. It includes
138 * the processed key material which can be used as SILC session keys.
143 unsigned char *send_iv;
144 unsigned char *receive_iv;
146 unsigned char *send_enc_key;
147 unsigned char *receive_enc_key;
148 SilcUInt32 enc_key_len;
149 unsigned char *send_hmac_key;
150 unsigned char *receive_hmac_key;
151 SilcUInt32 hmac_key_len;
152 } *SilcSKEKeyMaterial;
155 /****s* silcske/SilcSKEAPI/SilcSKERekeyMaterial
159 * typedef struct { ... } *SilcSKERekeyMaterial;
163 * This context is returned after key exchange protocol to application
164 * in the completion callback. Application may save it and use it later
165 * to perform the rekey with silc_ske_rekey_initiator_start and/or
166 * silc_ske_rekey_responder_start functions. If application does not
167 * need the context, it may free it with silc_free function.
169 * Application may save application specific data to `user_context'.
174 void *user_context; /* Application specific data */
175 unsigned char *send_enc_key;
176 unsigned int enc_key_len : 23;
177 unsigned int ske_group : 8;
178 unsigned int pfs : 1;
179 } *SilcSKERekeyMaterial;
182 /****d* silcske/SilcSKEAPI/SilcSKEPKType
186 * typedef enum { ... } SilcSKEPKType;
190 * Public key and certificate types defined by the SKE protocol.
195 SILC_SKE_PK_TYPE_SILC = 1, /* SILC Public Key, mandatory */
196 SILC_SKE_PK_TYPE_SSH2 = 2, /* SSH2 Public key, not supported */
197 SILC_SKE_PK_TYPE_X509V3 = 3, /* X.509v3 certificate, not supported */
198 SILC_SKE_PK_TYPE_OPENPGP = 4, /* OpenPGP certificate, not supported */
199 SILC_SKE_PK_TYPE_SPKI = 5 /* SPKI certificate, not supported */
203 /****f* silcske/SilcSKEAPI/SilcSKEVerifyCbCompletion
207 * typedef void (*SilcSKEVerifyCbCompletion)(SilcSKE ske,
208 * SilcSKEStatus status,
213 * Completion callback that will be called when the public key
214 * has been verified. The `status' will indicate whether the public
215 * key were trusted or not. If the `status' is PENDING then the status
216 * is not considered to be available at this moment. In this case the
217 * SKE libary will assume that the caller will call this callback again
218 * when the status is available. See silc_ske_set_callbacks for more
222 typedef void (*SilcSKEVerifyCbCompletion)(SilcSKE ske,
223 SilcSKEStatus status,
226 /****f* silcske/SilcSKEAPI/SilcSKEVerifyCb
230 * typedef void (*SilcSKEVerifyCb)(SilcSKE ske,
231 * SilcPublicKey public_key,
233 * SilcSKEVerifyCbCompletion completion,
234 * void *completion_context);
238 * Callback function used to verify the received public key or certificate.
239 * The verification process is most likely asynchronous. That's why the
240 * application must call the `completion' callback when the verification
241 * process has been completed. The `context' is the context given as
242 * arugment to silc_ske_set_callbacks. See silc_ske_set_callbacks for
245 * If the key repository was provided in silc_ske_alloc this callback
246 * is called only if the public key was not found from the repository.
249 typedef void (*SilcSKEVerifyCb)(SilcSKE ske,
250 SilcPublicKey public_key,
252 SilcSKEVerifyCbCompletion completion,
253 void *completion_context);
255 /****f* silcske/SilcSKEAPI/SilcSKECompletionCb
259 * typedef void (*SilcSKECompletionCb)(SilcSKE ske,
260 * SilcSKEStatus status,
261 * SilcSKESecurityProperties prop,
262 * SilcSKEKeyMaterial keymat,
263 * SilcSKERekeyMaterial rekey,
268 * Completion callback. This is called after the key exchange protocol
269 * has been completed. It delivers the status of the protocol, and if
270 * successful the security properties `prop' that was negotiated in the
271 * protocol and the key material `keymat' that can be set into use by
272 * calling silc_ske_set_keys, and the rekey key material `rekey' which
273 * can be used later to start rekey protocol. The `prop' will remain
274 * valid as long as `ske' is valid. After `ske' is freed `prop' will
278 typedef void (*SilcSKECompletionCb)(SilcSKE ske,
279 SilcSKEStatus status,
280 SilcSKESecurityProperties prop,
281 SilcSKEKeyMaterial keymat,
282 SilcSKERekeyMaterial rekey,
287 /****f* silcske/SilcSKEAPI/silc_ske_alloc
291 * SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
292 * SilcSKR repository, SilcPublicKey public_key,
293 * SilcPrivateKey private_key, void *context);
297 * Allocates the SKE session context and returns it. The `rng' is
298 * the random number generator the SKE is going to use when it needs
299 * random number generation during the SKE session. The `context' is
300 * user context that the libary will not touch. Application can get the
301 * context by calling the fuction silc_ske_get_context function. The
302 * application is responsible of freeing the `context'. After the
303 * SKE session context is allocated application must call the
304 * silc_ske_set_callbacks.
306 * If the `repository' is non-NULL then the remote's public key will be
307 * verified from the repository. If it is not provided then the
308 * SilcSKEVerifyCb callback must be set, and it will be called to
309 * verify the key. If both `repository' and the callback is provided the
310 * callback is called only if the key is not found from the repository.
312 * The `public_key' and `private_key' is the caller's identity used
313 * during the key exchange.
317 * // Initiator example
318 * ske = silc_ske_alloc(rng, scheduler, NULL, pk, prv, app);
319 * silc_ske_set_callbacks(ske, verify_public_key, completion, app);
321 * silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_PFS |
322 * SILC_SKE_SP_FLAG_MUTUAL,
324 * silc_ske_initiator_start(ske, stream, start_payload);
327 SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
328 SilcSKR repository, SilcPublicKey public_key,
329 SilcPrivateKey private_key, void *context);
331 /****f* silcske/SilcSKEAPI/silc_ske_free
335 * void silc_ske_free(SilcSKE ske);
339 * Frees the SKE session context and all allocated resources.
342 void silc_ske_free(SilcSKE ske);
344 /****f* silcske/SilcSKEAPI/silc_ske_get_context
348 * void *silc_ske_get_context(SilcSKE ske);
352 * Returns the context that was given as argument to silc_ske_alloc.
355 void *silc_ske_get_context(SilcSKE ske);
357 /****f* silcske/SilcSKEAPI/silc_ske_set_callbacks
361 * void silc_ske_set_callbacks(SilcSKE ske,
362 * SilcSKEVerifyCb verify_key,
363 * SilcSKECompletion completed,
368 * Sets the callback functions for the SKE session.
370 * The `verify_key' callback is called to verify the received public key
371 * or certificate. The verification process is most likely asynchronous.
372 * That is why the application must call the completion callback when the
373 * verification process has been completed. If this SKE session context
374 * is used to perform rekey, this callback usually is not provided as
375 * argument since sending public key in rekey is not mandatory. Setting
376 * this callback implies that remote end MUST send its public key.
378 * The `completed' callback will be called once the protocol has completed,
379 * either successfully or with an error. The status of the protocol is
380 * delivered to application with the callback.
382 * The `context' is passed as argument to all of the above callback
386 void silc_ske_set_callbacks(SilcSKE ske,
387 SilcSKEVerifyCb verify_key,
388 SilcSKECompletionCb completed,
391 /****f* silcske/SilcSKEAPI/silc_ske_initiator_start
396 * silc_ske_initiator_start(SilcSKE ske,
397 * SilcPacketStream stream,
398 * SilcSKEStartPayload start_payload);
402 * Starts the SILC Key Exchange protocol as initiator. The completion
403 * callback that was set in silc_ske_set_callbacks will be called once
404 * the protocol has completed. The `stream' is the network connection
405 * to the remote host. The SKE library will handle all key exchange
406 * packets sent and received in the `stream' connection.
408 * The `start_payload' includes all configured security properties that
409 * will be sent to the responder. The `start_payload' must be provided.
410 * It can be created by calling silc_ske_assemble_security_properties
411 * function. The caller must not free the payload once it has been
412 * given as argument to this function.
414 * This function returns SilcAsyncOperation operation context which can
415 * be used to control the protocol from the application. Application may
416 * for example safely abort the protocol at any point, if needed. Returns
421 silc_ske_initiator(SilcSKE ske,
422 SilcPacketStream stream,
423 SilcSKEStartPayload start_payload);
425 /****f* silcske/SilcSKEAPI/silc_ske_responder_start
430 * silc_ske_responder_start(SilcSKE ske,
431 * SilcPacketStream stream,
432 * const char *version,
433 * SilcBuffer start_payload,
434 * SilcSKESecurityPropertyFlag flags);
438 * Starts SILC Key Exchange protocol as responder. The completion
439 * callback that was set in silc_ske_set_callbacks will be called once
440 * the protocol has completed. The `stream' is the network connection
441 * to the remote host. The SKE library will handle all key exchange
442 * packets sent and received in the `stream' connection.
444 * The `version' is the responder's SILC protocol version that will be
445 * sent in reply to the initiator. The `flags' indicates the
446 * SilcSKESecurityPropertyFlag flags that responder supports and enforces
447 * for the initiator. Responder may, for example, enforce that the PFS
448 * will be performed in rekey.
450 * This function returns SilcAsyncOperation operation context which can
451 * be used to control the protocol from the application. Application may
452 * for example safely abort the protocol at any point, if needed. Returns
457 silc_ske_responder(SilcSKE ske,
458 SilcPacketStream stream,
460 SilcSKESecurityPropertyFlag flags);
463 silc_ske_rekey_initiator(SilcSKE ske,
464 SilcPacketStream stream,
465 SilcSKERekeyMaterial rekey);
468 silc_ske_rekey_responder(SilcSKE ske,
469 SilcPacketStream stream,
470 SilcBuffer ke_payload,
471 SilcSKERekeyMaterial rekey);
473 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
477 * SilcSKEStartPayload
478 * silc_ske_assemble_security_properties(SilcSKE ske,
479 * SilcSKESecurityPropertyFlag flags,
480 * const char *version);
484 * Assembles security properties to Key Exchange Start Payload to be
485 * sent to the remote end. This checks system wide (SILC system, that is)
486 * settings and chooses from those. However, if other properties
487 * should be used this function is easy to replace by another function.
488 * Returns NULL on error. This is an utility function. This is used
489 * by the initiator of the protocol. The `version' is the local SILC
490 * protocol version string.
494 silc_ske_assemble_security_properties(SilcSKE ske,
495 SilcSKESecurityPropertyFlag flags,
496 const char *version);
498 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
502 * SilcBool silc_ske_set_keys(SilcSKE ske,
503 * SilcSKEKeyMaterial keymat,
504 * SilcSKESecurityProperties prop,
505 * SilcCipher *ret_send_key,
506 * SilcCipher *ret_receive_key,
507 * SilcHmac *ret_hmac_send,
508 * SilcHmac *ret_hmac_receive,
509 * SilcHash *ret_hash);
513 * This function can be used after successful key exchange to take the
514 * key material `keymat' with security properties `prop' into use.
515 * This will allocate send and receive ciphers, HMACs and hash for the
516 * application. Caller must free the returned contexts. This is an
520 SilcBool silc_ske_set_keys(SilcSKE ske,
521 SilcSKEKeyMaterial keymat,
522 SilcSKESecurityProperties prop,
523 SilcCipher *ret_send_key,
524 SilcCipher *ret_receive_key,
525 SilcHmac *ret_hmac_send,
526 SilcHmac *ret_hmac_receive,
529 /****f* silcske/SilcSKEAPI/silc_ske_parse_version
533 * SilcBool silc_ske_parse_version(SilcSKE ske,
534 * SilcUInt32 *protocol_version,
535 * char **protocol_version_string,
536 * SilcUInt32 *software_version,
537 * char **software_version_string,
538 * char **vendor_version);
542 * Utility function to parse the remote host's version string.
545 SilcBool silc_ske_parse_version(SilcSKE ske,
546 SilcUInt32 *protocol_version,
547 char **protocol_version_string,
548 SilcUInt32 *software_version,
549 char **software_version_string,
550 char **vendor_version);
552 /****f* silcske/SilcSKEAPI/silc_ske_map_status
556 * const char *silc_ske_map_status(SilcSKEStatus status);
560 * Utility function to map the `status' into human readable message.
563 const char *silc_ske_map_status(SilcSKEStatus status);
565 #include "silcske_i.h"
567 #endif /* !SILCSKE_H */