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 * SilcSKEPKType pk_type,
232 * SilcPublicKey public_key,
234 * SilcSKEVerifyCbCompletion completion,
235 * void *completion_context);
239 * Callback function used to verify the received public key or certificate.
240 * The verification process is most likely asynchronous. That's why the
241 * application must call the `completion' callback when the verification
242 * process has been completed. The `context' is the context given as
243 * arugment to silc_ske_set_callbacks. See silc_ske_set_callbacks for
247 typedef void (*SilcSKEVerifyCb)(SilcSKE ske,
248 SilcSKEPKType pk_type,
249 SilcPublicKey public_key,
251 SilcSKEVerifyCbCompletion completion,
252 void *completion_context);
254 /****f* silcske/SilcSKEAPI/SilcSKECompletionCb
258 * typedef void (*SilcSKECompletionCb)(SilcSKE ske,
259 * SilcSKEStatus status,
260 * SilcSKESecurityProperties prop,
261 * SilcSKEKeyMaterial keymat,
262 * SilcSKERekeyMaterial rekey,
267 * Completion callback. This is called after the key exchange protocol
268 * has been completed. It delivers the status of the protocol, and if
269 * successful the security properties `prop' that was negotiated in the
270 * protocol and the key material `keymat' that can be set into use by
271 * calling silc_ske_set_keys, and the rekey key material `rekey' which
272 * can be used later to start rekey protocol. The `prop' will remain
273 * valid as long as `ske' is valid. After `ske' is freed `prop' will
277 typedef void (*SilcSKECompletionCb)(SilcSKE ske,
278 SilcSKEStatus status,
279 SilcSKESecurityProperties prop,
280 SilcSKEKeyMaterial keymat,
281 SilcSKERekeyMaterial rekey,
286 /****f* silcske/SilcSKEAPI/silc_ske_alloc
290 * SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
291 * SilcPublicKey public_key,
292 * SilcPrivateKey private_key, void *context);
296 * Allocates the SKE session context and returns it. The `rng' is
297 * the random number generator the SKE is going to use when it needs
298 * random number generation during the SKE session. The `context' is
299 * user context that the libary will not touch. Application can get the
300 * context by calling the fuction silc_ske_get_context function. The
301 * application is responsible of freeing the `context'. After the
302 * SKE session context is allocated application must call the
303 * silc_ske_set_callbacks.
307 * // Initiator example
308 * ske = silc_ske_alloc(rng, scheduler, app);
309 * silc_ske_set_callbacks(ske, verify_public_key, completion, app);
311 * silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_PFS |
312 * SILC_SKE_SP_FLAG_MUTUAL,
314 * silc_ske_initiator_start(ske, stream, start_payload);
317 SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
318 SilcPublicKey public_key, SilcPrivateKey private_key,
321 /****f* silcske/SilcSKEAPI/silc_ske_free
325 * void silc_ske_free(SilcSKE ske);
329 * Frees the SKE session context and all allocated resources.
332 void silc_ske_free(SilcSKE ske);
334 /****f* silcske/SilcSKEAPI/silc_ske_get_context
338 * void *silc_ske_get_context(SilcSKE ske);
342 * Returns the context that was given as argument to silc_ske_alloc.
345 void *silc_ske_get_context(SilcSKE ske);
347 /****f* silcske/SilcSKEAPI/silc_ske_set_callbacks
351 * void silc_ske_set_callbacks(SilcSKE ske,
352 * SilcSKEVerifyCb verify_key,
353 * SilcSKECompletion completed,
358 * Sets the callback functions for the SKE session.
360 * The `verify_key' callback is called to verify the received public key
361 * or certificate. The verification process is most likely asynchronous.
362 * That is why the application must call the completion callback when the
363 * verification process has been completed. If this SKE session context
364 * is used to perform rekey, this callback usually is not provided as
365 * argument since sending public key in rekey is not mandatory. Setting
366 * this callback implies that remote end MUST send its public key.
368 * The `completed' callback will be called once the protocol has completed,
369 * either successfully or with an error. The status of the protocol is
370 * delivered to application with the callback.
372 * The `context' is passed as argument to all of the above callback
376 void silc_ske_set_callbacks(SilcSKE ske,
377 SilcSKEVerifyCb verify_key,
378 SilcSKECompletionCb completed,
381 /****f* silcske/SilcSKEAPI/silc_ske_initiator_start
386 * silc_ske_initiator_start(SilcSKE ske,
387 * SilcPacketStream stream,
388 * SilcSKEStartPayload start_payload);
392 * Starts the SILC Key Exchange protocol as initiator. The completion
393 * callback that was set in silc_ske_set_callbacks will be called once
394 * the protocol has completed. The `stream' is the network connection
395 * to the remote host. The SKE library will handle all key exchange
396 * packets sent and received in the `stream' connection.
398 * The `start_payload' includes all configured security properties that
399 * will be sent to the responder. The `start_payload' must be provided.
400 * It can be created by calling silc_ske_assemble_security_properties
401 * function. The caller must not free the payload once it has been
402 * given as argument to this function.
404 * This function returns SilcAsyncOperation operation context which can
405 * be used to control the protocol from the application. Application may
406 * for example safely abort the protocol at any point, if needed. Returns
411 silc_ske_initiator(SilcSKE ske,
412 SilcPacketStream stream,
413 SilcSKEStartPayload start_payload);
415 /****f* silcske/SilcSKEAPI/silc_ske_responder_start
420 * silc_ske_responder_start(SilcSKE ske,
421 * SilcPacketStream stream,
422 * const char *version,
423 * SilcBuffer start_payload,
424 * SilcSKESecurityPropertyFlag flags);
428 * Starts SILC Key Exchange protocol as responder. The completion
429 * callback that was set in silc_ske_set_callbacks will be called once
430 * the protocol has completed. The `stream' is the network connection
431 * to the remote host. The SKE library will handle all key exchange
432 * packets sent and received in the `stream' connection.
434 * The `version' is the responder's SILC protocol version that will be
435 * sent in reply to the initiator. The `flags' indicates the
436 * SilcSKESecurityPropertyFlag flags that responder supports and enforces
437 * for the initiator. Responder may, for example, enforce that the PFS
438 * will be performed in rekey.
440 * This function returns SilcAsyncOperation operation context which can
441 * be used to control the protocol from the application. Application may
442 * for example safely abort the protocol at any point, if needed. Returns
447 silc_ske_responder(SilcSKE ske,
448 SilcPacketStream stream,
450 SilcSKESecurityPropertyFlag flags);
453 silc_ske_rekey_initiator(SilcSKE ske,
454 SilcPacketStream stream,
455 SilcSKERekeyMaterial rekey);
458 silc_ske_rekey_responder(SilcSKE ske,
459 SilcPacketStream stream,
460 SilcBuffer ke_payload,
461 SilcSKERekeyMaterial rekey);
463 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
467 * SilcSKEStartPayload
468 * silc_ske_assemble_security_properties(SilcSKE ske,
469 * SilcSKESecurityPropertyFlag flags,
470 * const char *version);
474 * Assembles security properties to Key Exchange Start Payload to be
475 * sent to the remote end. This checks system wide (SILC system, that is)
476 * settings and chooses from those. However, if other properties
477 * should be used this function is easy to replace by another function.
478 * Returns NULL on error. This is an utility function. This is used
479 * by the initiator of the protocol. The `version' is the local SILC
480 * protocol version string.
484 silc_ske_assemble_security_properties(SilcSKE ske,
485 SilcSKESecurityPropertyFlag flags,
486 const char *version);
488 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
492 * SilcBool silc_ske_set_keys(SilcSKE ske,
493 * SilcSKEKeyMaterial keymat,
494 * SilcSKESecurityProperties prop,
495 * SilcCipher *ret_send_key,
496 * SilcCipher *ret_receive_key,
497 * SilcHmac *ret_hmac_send,
498 * SilcHmac *ret_hmac_receive,
499 * SilcHash *ret_hash);
503 * This function can be used after successful key exchange to take the
504 * key material `keymat' with security properties `prop' into use.
505 * This will allocate send and receive ciphers, HMACs and hash for the
506 * application. Caller must free the returned contexts. This is an
510 SilcBool silc_ske_set_keys(SilcSKE ske,
511 SilcSKEKeyMaterial keymat,
512 SilcSKESecurityProperties prop,
513 SilcCipher *ret_send_key,
514 SilcCipher *ret_receive_key,
515 SilcHmac *ret_hmac_send,
516 SilcHmac *ret_hmac_receive,
519 /****f* silcske/SilcSKEAPI/silc_ske_parse_version
523 * SilcBool silc_ske_parse_version(SilcSKE ske,
524 * SilcUInt32 *protocol_version,
525 * char **protocol_version_string,
526 * SilcUInt32 *software_version,
527 * char **software_version_string,
528 * char **vendor_version);
532 * Utility function to parse the remote host's version string.
535 SilcBool silc_ske_parse_version(SilcSKE ske,
536 SilcUInt32 *protocol_version,
537 char **protocol_version_string,
538 SilcUInt32 *software_version,
539 char **software_version_string,
540 char **vendor_version);
542 /****f* silcske/SilcSKEAPI/silc_ske_map_status
546 * const char *silc_ske_map_status(SilcSKEStatus status);
550 * Utility function to map the `status' into human readable message.
553 const char *silc_ske_map_status(SilcSKEStatus status);
555 #include "silcske_i.h"
557 #endif /* !SILCSKE_H */