Added support for SilcSKR in SKE for finding public keys.
[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 /****d* silcske/SilcSKEAPI/SilcSKEPKType
183  *
184  * NAME
185  *
186  *    typedef enum { ... } SilcSKEPKType;
187  *
188  * DESCRIPTION
189  *
190  *    Public key and certificate types defined by the SKE protocol.
191  *
192  * SOURCE
193  */
194 typedef enum {
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 */
200 } SilcSKEPKType;
201 /***/
202
203 /****f* silcske/SilcSKEAPI/SilcSKEVerifyCbCompletion
204  *
205  * SYNOPSIS
206  *
207  *    typedef void (*SilcSKEVerifyCbCompletion)(SilcSKE ske,
208  *                                              SilcSKEStatus status,
209  *                                              void *context);
210  *
211  * DESCRIPTION
212  *
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
219  *    information.
220  *
221  ***/
222 typedef void (*SilcSKEVerifyCbCompletion)(SilcSKE ske,
223                                           SilcSKEStatus status,
224                                           void *context);
225
226 /****f* silcske/SilcSKEAPI/SilcSKEVerifyCb
227  *
228  * SYNOPSIS
229  *
230  *    typedef void (*SilcSKEVerifyCb)(SilcSKE ske,
231  *                                    SilcPublicKey public_key,
232  *                                    void *context,
233  *                                    SilcSKEVerifyCbCompletion completion,
234  *                                    void *completion_context);
235  *
236  * DESCRIPTION
237  *
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
243  *    more information.
244  *
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.
247  *
248  ***/
249 typedef void (*SilcSKEVerifyCb)(SilcSKE ske,
250                                 SilcPublicKey public_key,
251                                 void *context,
252                                 SilcSKEVerifyCbCompletion completion,
253                                 void *completion_context);
254
255 /****f* silcske/SilcSKEAPI/SilcSKECompletionCb
256  *
257  * SYNOPSIS
258  *
259  *    typedef void (*SilcSKECompletionCb)(SilcSKE ske,
260  *                                        SilcSKEStatus status,
261  *                                        SilcSKESecurityProperties prop,
262  *                                        SilcSKEKeyMaterial keymat,
263  *                                        SilcSKERekeyMaterial rekey,
264  *                                        void *context);
265  *
266  * DESCRIPTION
267  *
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
275  *    become invalid.
276  *
277  ***/
278 typedef void (*SilcSKECompletionCb)(SilcSKE ske,
279                                     SilcSKEStatus status,
280                                     SilcSKESecurityProperties prop,
281                                     SilcSKEKeyMaterial keymat,
282                                     SilcSKERekeyMaterial rekey,
283                                     void *context);
284
285 /* Prototypes */
286
287 /****f* silcske/SilcSKEAPI/silc_ske_alloc
288  *
289  * SYNOPSIS
290  *
291  *    SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
292  *                           SilcSKR repository, SilcPublicKey public_key,
293  *                           SilcPrivateKey private_key, void *context);
294  *
295  * DESCRIPTION
296  *
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.
305  *
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.
311  *
312  *    The `public_key' and `private_key' is the caller's identity used
313  *    during the key exchange.
314  *
315  * EXMPALE
316  *
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);
320  *    start_payload =
321  *      silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_PFS |
322  *                                            SILC_SKE_SP_FLAG_MUTUAL,
323  *                                            version);
324  *    silc_ske_initiator_start(ske, stream, start_payload);
325  *
326  ***/
327 SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
328                        SilcSKR repository, SilcPublicKey public_key,
329                        SilcPrivateKey private_key, void *context);
330
331 /****f* silcske/SilcSKEAPI/silc_ske_free
332  *
333  * SYNOPSIS
334  *
335  *    void silc_ske_free(SilcSKE ske);
336  *
337  * DESCRIPTION
338  *
339  *    Frees the SKE session context and all allocated resources.
340  *
341  ***/
342 void silc_ske_free(SilcSKE ske);
343
344 /****f* silcske/SilcSKEAPI/silc_ske_get_context
345  *
346  * SYNOPSIS
347  *
348  *    void *silc_ske_get_context(SilcSKE ske);
349  *
350  * DESCRIPTION
351  *
352  *    Returns the context that was given as argument to silc_ske_alloc.
353  *
354  ***/
355 void *silc_ske_get_context(SilcSKE ske);
356
357 /****f* silcske/SilcSKEAPI/silc_ske_set_callbacks
358  *
359  * SYNOPSIS
360  *
361  *    void silc_ske_set_callbacks(SilcSKE ske,
362  *                                SilcSKEVerifyCb verify_key,
363  *                                SilcSKECompletion completed,
364  *                                void *context);
365  *
366  * DESCRIPTION
367  *
368  *    Sets the callback functions for the SKE session.
369  *
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.
377  *
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.
381  *
382  *    The `context' is passed as argument to all of the above callback
383  *    functions.
384  *
385  ***/
386 void silc_ske_set_callbacks(SilcSKE ske,
387                             SilcSKEVerifyCb verify_key,
388                             SilcSKECompletionCb completed,
389                             void *context);
390
391 /****f* silcske/SilcSKEAPI/silc_ske_initiator_start
392  *
393  * SYNOPSIS
394  *
395  *    SilcAsyncOperation
396  *    silc_ske_initiator_start(SilcSKE ske,
397  *                             SilcPacketStream stream,
398  *                             SilcSKEStartPayload start_payload);
399  *
400  * DESCRIPTION
401  *
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.
407  *
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.
413  *
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
417  *    NULL on error.
418  *
419  ***/
420 SilcAsyncOperation
421 silc_ske_initiator(SilcSKE ske,
422                    SilcPacketStream stream,
423                    SilcSKEStartPayload start_payload);
424
425 /****f* silcske/SilcSKEAPI/silc_ske_responder_start
426  *
427  * SYNOPSIS
428  *
429  *    SilcAsyncOperation
430  *    silc_ske_responder_start(SilcSKE ske,
431  *                             SilcPacketStream stream,
432  *                             const char *version,
433  *                             SilcBuffer start_payload,
434  *                             SilcSKESecurityPropertyFlag flags);
435  *
436  * DESCRIPTION
437  *
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.
443  *
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.
449  *
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
453  *    NULL on error.
454  *
455  ***/
456 SilcAsyncOperation
457 silc_ske_responder(SilcSKE ske,
458                    SilcPacketStream stream,
459                    const char *version,
460                    SilcSKESecurityPropertyFlag flags);
461
462 SilcAsyncOperation
463 silc_ske_rekey_initiator(SilcSKE ske,
464                          SilcPacketStream stream,
465                          SilcSKERekeyMaterial rekey);
466
467 SilcAsyncOperation
468 silc_ske_rekey_responder(SilcSKE ske,
469                          SilcPacketStream stream,
470                          SilcBuffer ke_payload,
471                          SilcSKERekeyMaterial rekey);
472
473 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
474  *
475  * SYNOPSIS
476  *
477  *    SilcSKEStartPayload
478  *    silc_ske_assemble_security_properties(SilcSKE ske,
479  *                                          SilcSKESecurityPropertyFlag flags,
480  *                                          const char *version);
481  *
482  * DESCRIPTION
483  *
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.
491  *
492  ***/
493 SilcSKEStartPayload
494 silc_ske_assemble_security_properties(SilcSKE ske,
495                                       SilcSKESecurityPropertyFlag flags,
496                                       const char *version);
497
498 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
499  *
500  * SYNOPSIS
501  *
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);
510  *
511  * DESCRIPTION
512  *
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
517  *    utility function.
518  *
519  ***/
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,
527                            SilcHash *ret_hash);
528
529 /****f* silcske/SilcSKEAPI/silc_ske_parse_version
530  *
531  * SYNOPSIS
532  *
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);
539  *
540  * DESCRIPTION
541  *
542  *    Utility function to parse the remote host's version string.
543  *
544  ***/
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);
551
552 /****f* silcske/SilcSKEAPI/silc_ske_map_status
553  *
554  * SYNOPSIS
555  *
556  *    const char *silc_ske_map_status(SilcSKEStatus status);
557  *
558  * DESCRIPTION
559  *
560  *    Utility function to map the `status' into human readable message.
561  *
562  ***/
563 const char *silc_ske_map_status(SilcSKEStatus status);
564
565 #include "silcske_i.h"
566
567 #endif  /* !SILCSKE_H */