42d9bc635648738561b30b3b36991b4171005e7f
[silc.git] / lib / silcske / silcske.h
1 /*
2
3   silcske.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2000 - 2006 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 /****h* silcske/SILC SKE Interface
21  *
22  * DESCRIPTION
23  *
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.
32  *
33  ***/
34
35 #ifndef SILCSKE_H
36 #define SILCSKE_H
37
38 /* Forward declarations */
39 typedef struct SilcSKECallbacksStruct *SilcSKECallbacks;
40 typedef struct SilcSKEStruct *SilcSKE;
41
42 /****d* silcske/SilcSKEAPI/SilcSKEStatus
43  *
44  * NAME
45  *
46  *    typedef enum { ... } SilcSKEStatus;
47  *
48  * DESCRIPTION
49  *
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.
53  *
54  * SOURCE
55  */
56 typedef enum {
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 */
70
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 */
77 } SilcSKEStatus;
78 /***/
79
80 #include "silcske_groups.h"
81 #include "silcske_payload.h"
82
83 /****d* silcske/SilcSKEAPI/SilcSKESecurityPropertyFlag
84  *
85  * NAME
86  *
87  *    typedef enum { ... } SilcSKESecurityPropertyFlag
88  *
89  * DESCRIPTION
90  *
91  *    SKE security property flags as defined by the SK protocol.
92  *
93  * SOURCE
94  */
95 typedef enum {
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;
101 /***/
102
103 /****s* silcske/SilcSKEAPI/SilcSKESecurityProperties
104  *
105  * NAME
106  *
107  *    typedef struct { ... } *SilcSKESecurityProperties;
108  *
109  * DESCRIPTION
110  *
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
114  *    communication.
115  *
116  * SOURCE
117  */
118 typedef struct {
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;
126 /***/
127
128 /****s* silcske/SilcSKEAPI/SilcSKEKeyMaterial
129  *
130  * NAME
131  *
132  *    typedef struct { ... } *SilcSKEKeyMaterial;
133  *
134  * DESCRIPTION
135  *
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.
139  *
140  * SOURCE
141  */
142 typedef struct {
143   unsigned char *send_iv;
144   unsigned char *receive_iv;
145   SilcUInt32 iv_len;
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;
153 /***/
154
155 /****s* silcske/SilcSKEAPI/SilcSKERekeyMaterial
156  *
157  * NAME
158  *
159  *    typedef struct { ... } *SilcSKERekeyMaterial;
160  *
161  * DESCRIPTION
162  *
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.
168  *
169  *    Application may save application specific data to `user_context'.
170  *
171  * SOURCE
172  */
173 typedef struct {
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;
180 /***/
181
182 /****s* silcske/SilcSKEAPI/SilcSKEParams
183  *
184  * NAME
185  *
186  *    typedef struct { ... } *SilcSKEParams, SilcSKEParamsStruct;
187  *
188  * DESCRIPTION
189  *
190  *    SKE parameters structure.  This structure is given as argument to
191  *    silc_ske_initiator and silc_ske_responder functions.
192  *
193  * SOURCE
194  */
195 typedef struct {
196   /* The SKE version string that is sent to the remote end.  This field
197      must be set.  Caller must free the pointer. */
198   char *version;
199
200   /* Security property flags.  When initiator sets these it requests them
201      from the responder.  Responder may set here the flags it supports
202      and wants to enforce for the initiator. */
203   SilcSKESecurityPropertyFlag flags;
204
205   /* SILC Session port when using UDP/IP and SILC_SKE_SP_FLAG_IV_INCLUDED
206      flag.  It is the port the remote will use as SILC session port after
207      the key exchange protocol.  Ignored without SILC_SKE_SP_FLAG_IV_INCLUDED
208      flag. */
209   SilcUInt16 session_port;
210 } *SilcSKEParams, SilcSKEParamsStruct;
211 /***/
212
213 /****d* silcske/SilcSKEAPI/SilcSKEPKType
214  *
215  * NAME
216  *
217  *    typedef enum { ... } SilcSKEPKType;
218  *
219  * DESCRIPTION
220  *
221  *    Public key and certificate types defined by the SKE protocol.
222  *
223  * SOURCE
224  */
225 typedef enum {
226   SILC_SKE_PK_TYPE_SILC    = 1, /* SILC Public Key, mandatory */
227   SILC_SKE_PK_TYPE_SSH2    = 2, /* SSH2 Public key, not supported */
228   SILC_SKE_PK_TYPE_X509V3  = 3, /* X.509v3 certificate, not supported */
229   SILC_SKE_PK_TYPE_OPENPGP = 4, /* OpenPGP certificate, not supported */
230   SILC_SKE_PK_TYPE_SPKI    = 5  /* SPKI certificate, not supported */
231 } SilcSKEPKType;
232 /***/
233
234 /****f* silcske/SilcSKEAPI/SilcSKEVerifyCbCompletion
235  *
236  * SYNOPSIS
237  *
238  *    typedef void (*SilcSKEVerifyCbCompletion)(SilcSKE ske,
239  *                                              SilcSKEStatus status,
240  *                                              void *context);
241  *
242  * DESCRIPTION
243  *
244  *    Completion callback that will be called when the public key
245  *    has been verified.  The `status' will indicate whether the public
246  *    key were trusted or not. If the `status' is PENDING then the status
247  *    is not considered to be available at this moment. In this case the
248  *    SKE libary will assume that the caller will call this callback again
249  *    when the status is available. See silc_ske_set_callbacks for more
250  *    information.
251  *
252  ***/
253 typedef void (*SilcSKEVerifyCbCompletion)(SilcSKE ske,
254                                           SilcSKEStatus status,
255                                           void *context);
256
257 /****f* silcske/SilcSKEAPI/SilcSKEVerifyCb
258  *
259  * SYNOPSIS
260  *
261  *    typedef void (*SilcSKEVerifyCb)(SilcSKE ske,
262  *                                    SilcPublicKey public_key,
263  *                                    void *context,
264  *                                    SilcSKEVerifyCbCompletion completion,
265  *                                    void *completion_context);
266  *
267  * DESCRIPTION
268  *
269  *    Callback function used to verify the received public key or certificate.
270  *    The verification process is most likely asynchronous.  That's why the
271  *    application must call the `completion' callback when the verification
272  *    process has been completed.  The `context' is the context given as
273  *    arugment to silc_ske_set_callbacks.  See silc_ske_set_callbacks for
274  *    more information.
275  *
276  *    If the key repository was provided in silc_ske_alloc this callback
277  *    is called only if the public key was not found from the repository.
278  *
279  ***/
280 typedef void (*SilcSKEVerifyCb)(SilcSKE ske,
281                                 SilcPublicKey public_key,
282                                 void *context,
283                                 SilcSKEVerifyCbCompletion completion,
284                                 void *completion_context);
285
286 /****f* silcske/SilcSKEAPI/SilcSKECompletionCb
287  *
288  * SYNOPSIS
289  *
290  *    typedef void (*SilcSKECompletionCb)(SilcSKE ske,
291  *                                        SilcSKEStatus status,
292  *                                        SilcSKESecurityProperties prop,
293  *                                        SilcSKEKeyMaterial keymat,
294  *                                        SilcSKERekeyMaterial rekey,
295  *                                        void *context);
296  *
297  * DESCRIPTION
298  *
299  *    Completion callback.  This is called after the key exchange protocol
300  *    has been completed.  It delivers the status of the protocol, and if
301  *    successful the security properties `prop' that was negotiated in the
302  *    protocol and the key material `keymat' that can be set into use by
303  *    calling silc_ske_set_keys, and the rekey key material `rekey' which
304  *    can be used later to start rekey protocol.  The `prop' will remain
305  *    valid as long as `ske' is valid.  After `ske' is freed `prop' will
306  *    become invalid.
307  *
308  ***/
309 typedef void (*SilcSKECompletionCb)(SilcSKE ske,
310                                     SilcSKEStatus status,
311                                     SilcSKESecurityProperties prop,
312                                     SilcSKEKeyMaterial keymat,
313                                     SilcSKERekeyMaterial rekey,
314                                     void *context);
315
316 /* Prototypes */
317
318 /****f* silcske/SilcSKEAPI/silc_ske_alloc
319  *
320  * SYNOPSIS
321  *
322  *    SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
323  *                           SilcSKR repository, SilcPublicKey public_key,
324  *                           SilcPrivateKey private_key, void *context);
325  *
326  * DESCRIPTION
327  *
328  *    Allocates the SKE session context and returns it.  The `rng' is
329  *    the random number generator the SKE is going to use when it needs
330  *    random number generation during the SKE session.  The `context' is
331  *    user context that the libary will not touch.  Application can get the
332  *    context by calling the fuction silc_ske_get_context function.  The
333  *    application is responsible of freeing the `context'.  After the
334  *    SKE session context is allocated application must call the
335  *    silc_ske_set_callbacks.
336  *
337  *    If the `repository' is non-NULL then the remote's public key will be
338  *    verified from the repository.  If it is not provided then the
339  *    SilcSKEVerifyCb callback must be set, and it will be called to
340  *    verify the key.  If both `repository' and the callback is provided the
341  *    callback is called only if the key is not found from the repository.
342  *
343  *    The `public_key' and `private_key' is the caller's identity used
344  *    during the key exchange.
345  *
346  * EXMPALE
347  *
348  *    // Initiator example
349  *    params.version = version;
350  *    params.flags = SILC_SKE_SP_FLAG_PFS | SILC_SKE_SP_FLAG_MUTUAL;
351  *    ske = silc_ske_alloc(rng, scheduler, NULL, pk, prv, app);
352  *    silc_ske_set_callbacks(ske, verify_public_key, completion, app);
353  *    silc_ske_initiator_start(ske, stream, &params, NULL);
354  *
355  ***/
356 SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
357                        SilcSKR repository, SilcPublicKey public_key,
358                        SilcPrivateKey private_key, void *context);
359
360 /****f* silcske/SilcSKEAPI/silc_ske_free
361  *
362  * SYNOPSIS
363  *
364  *    void silc_ske_free(SilcSKE ske);
365  *
366  * DESCRIPTION
367  *
368  *    Frees the SKE session context and all allocated resources.
369  *
370  ***/
371 void silc_ske_free(SilcSKE ske);
372
373 /****f* silcske/SilcSKEAPI/silc_ske_get_context
374  *
375  * SYNOPSIS
376  *
377  *    void *silc_ske_get_context(SilcSKE ske);
378  *
379  * DESCRIPTION
380  *
381  *    Returns the context that was given as argument to silc_ske_alloc.
382  *
383  ***/
384 void *silc_ske_get_context(SilcSKE ske);
385
386 /****f* silcske/SilcSKEAPI/silc_ske_set_callbacks
387  *
388  * SYNOPSIS
389  *
390  *    void silc_ske_set_callbacks(SilcSKE ske,
391  *                                SilcSKEVerifyCb verify_key,
392  *                                SilcSKECompletion completed,
393  *                                void *context);
394  *
395  * DESCRIPTION
396  *
397  *    Sets the callback functions for the SKE session.
398  *
399  *    The `verify_key' callback is called to verify the received public key
400  *    or certificate.  The verification process is most likely asynchronous.
401  *    That is why the application must call the completion callback when the
402  *    verification process has been completed.  If this SKE session context
403  *    is used to perform  rekey, this callback usually is not provided as
404  *    argument since sending public key in rekey is not mandatory.  Setting
405  *    this callback implies that remote end MUST send its public key.
406  *
407  *    The `completed' callback will be called once the protocol has completed,
408  *    either successfully or with an error.  The status of the protocol is
409  *    delivered to application with the callback.
410  *
411  *    The `context' is passed as argument to all of the above callback
412  *    functions.
413  *
414  ***/
415 void silc_ske_set_callbacks(SilcSKE ske,
416                             SilcSKEVerifyCb verify_key,
417                             SilcSKECompletionCb completed,
418                             void *context);
419
420 /****f* silcske/SilcSKEAPI/silc_ske_initiator_start
421  *
422  * SYNOPSIS
423  *
424  *    SilcAsyncOperation
425  *    silc_ske_initiator_start(SilcSKE ske,
426  *                             SilcPacketStream stream,
427  *                             SilcSKEParams params,
428  *                             SilcSKEStartPayload start_payload);
429  *
430  * DESCRIPTION
431  *
432  *    Starts the SILC Key Exchange protocol as initiator.  The completion
433  *    callback that was set in silc_ske_set_callbacks will be called once
434  *    the protocol has completed.  The `stream' is the network connection
435  *    to the remote host.  The SKE library will handle all key exchange
436  *    packets sent and received in the `stream' connection.  The `params'
437  *    include SKE parameters, and it must be provided.
438  *
439  *    If the `start_payload' is NULL the library will generate it
440  *    automatically.  Caller may provide it if it wants to send its own
441  *    security properties instead of using the default ones library
442  *    generates.  If caller provides it, it must not free it once it has
443  *    been given as argument to this function.
444  *
445  *    This function returns SilcAsyncOperation operation context which can
446  *    be used to control the protocol from the application.  Application may
447  *    for example safely abort the protocol at any point, if needed.  Returns
448  *    NULL on error.
449  *
450  ***/
451 SilcAsyncOperation
452 silc_ske_initiator(SilcSKE ske,
453                    SilcPacketStream stream,
454                    SilcSKEParams params,
455                    SilcSKEStartPayload start_payload);
456
457 /****f* silcske/SilcSKEAPI/silc_ske_responder_start
458  *
459  * SYNOPSIS
460  *
461  *    SilcAsyncOperation
462  *    silc_ske_responder_start(SilcSKE ske,
463  *                             SilcPacketStream stream,
464  *                             SilcSKEParams params);
465  *
466  * DESCRIPTION
467  *
468  *    Starts SILC Key Exchange protocol as responder.  The completion
469  *    callback that was set in silc_ske_set_callbacks will be called once
470  *    the protocol has completed.  The `stream' is the network connection
471  *    to the remote host.  The SKE library will handle all key exchange
472  *    packets sent and received in the `stream' connection.  The `params'
473  *    include SKE parameters, and must be provided.
474  *
475  *    This function returns SilcAsyncOperation operation context which can
476  *    be used to control the protocol from the application.  Application may
477  *    for example safely abort the protocol at any point, if needed.  Returns
478  *    NULL on error.
479  *
480  ***/
481 SilcAsyncOperation
482 silc_ske_responder(SilcSKE ske,
483                    SilcPacketStream stream,
484                    SilcSKEParams params);
485
486 SilcAsyncOperation
487 silc_ske_rekey_initiator(SilcSKE ske,
488                          SilcPacketStream stream,
489                          SilcSKERekeyMaterial rekey);
490
491 SilcAsyncOperation
492 silc_ske_rekey_responder(SilcSKE ske,
493                          SilcPacketStream stream,
494                          SilcBuffer ke_payload,
495                          SilcSKERekeyMaterial rekey);
496
497 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
498  *
499  * SYNOPSIS
500  *
501  *    SilcBool silc_ske_set_keys(SilcSKE ske,
502  *                               SilcSKEKeyMaterial keymat,
503  *                               SilcSKESecurityProperties prop,
504  *                               SilcCipher *ret_send_key,
505  *                               SilcCipher *ret_receive_key,
506  *                               SilcHmac *ret_hmac_send,
507  *                               SilcHmac *ret_hmac_receive,
508  *                               SilcHash *ret_hash);
509  *
510  * DESCRIPTION
511  *
512  *    This function can be used after successful key exchange to take the
513  *    key material `keymat' with security properties `prop' into use.
514  *    This will allocate send and receive ciphers, HMACs and hash for the
515  *    application.  Caller must free the returned contexts.  This is an
516  *    utility function.
517  *
518  ***/
519 SilcBool silc_ske_set_keys(SilcSKE ske,
520                            SilcSKEKeyMaterial keymat,
521                            SilcSKESecurityProperties prop,
522                            SilcCipher *ret_send_key,
523                            SilcCipher *ret_receive_key,
524                            SilcHmac *ret_hmac_send,
525                            SilcHmac *ret_hmac_receive,
526                            SilcHash *ret_hash);
527
528 /****f* silcske/SilcSKEAPI/silc_ske_parse_version
529  *
530  * SYNOPSIS
531  *
532  *    SilcBool silc_ske_parse_version(SilcSKE ske,
533  *                                    SilcUInt32 *protocol_version,
534  *                                    char **protocol_version_string,
535  *                                    SilcUInt32 *software_version,
536  *                                    char **software_version_string,
537  *                                    char **vendor_version);
538  *
539  * DESCRIPTION
540  *
541  *    Utility function to parse the remote host's version string.
542  *
543  ***/
544 SilcBool silc_ske_parse_version(SilcSKE ske,
545                                 SilcUInt32 *protocol_version,
546                                 char **protocol_version_string,
547                                 SilcUInt32 *software_version,
548                                 char **software_version_string,
549                                 char **vendor_version);
550
551 /****f* silcske/SilcSKEAPI/silc_ske_map_status
552  *
553  * SYNOPSIS
554  *
555  *    const char *silc_ske_map_status(SilcSKEStatus status);
556  *
557  * DESCRIPTION
558  *
559  *    Utility function to map the `status' into human readable message.
560  *
561  ***/
562 const char *silc_ske_map_status(SilcSKEStatus status);
563
564 #include "silcske_i.h"
565
566 #endif  /* !SILCSKE_H */