New SILC PKCS API, enabling support for other public keys/certs.
[silc.git] / lib / silcske / silcske.h
1 /*
2
3   silcske.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2000 - 2005 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  *                                    SilcSKEPKType pk_type,
232  *                                    SilcPublicKey public_key,
233  *                                    void *context,
234  *                                    SilcSKEVerifyCbCompletion completion,
235  *                                    void *completion_context);
236  *
237  * DESCRIPTION
238  *
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
244  *    more information.
245  *
246  ***/
247 typedef void (*SilcSKEVerifyCb)(SilcSKE ske,
248                                 SilcSKEPKType pk_type,
249                                 SilcPublicKey public_key,
250                                 void *context,
251                                 SilcSKEVerifyCbCompletion completion,
252                                 void *completion_context);
253
254 /****f* silcske/SilcSKEAPI/SilcSKECompletionCb
255  *
256  * SYNOPSIS
257  *
258  *    typedef void (*SilcSKECompletionCb)(SilcSKE ske,
259  *                                        SilcSKEStatus status,
260  *                                        SilcSKESecurityProperties prop,
261  *                                        SilcSKEKeyMaterial keymat,
262  *                                        SilcSKERekeyMaterial rekey,
263  *                                        void *context);
264  *
265  * DESCRIPTION
266  *
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
274  *    become invalid.
275  *
276  ***/
277 typedef void (*SilcSKECompletionCb)(SilcSKE ske,
278                                     SilcSKEStatus status,
279                                     SilcSKESecurityProperties prop,
280                                     SilcSKEKeyMaterial keymat,
281                                     SilcSKERekeyMaterial rekey,
282                                     void *context);
283
284 /* Prototypes */
285
286 /****f* silcske/SilcSKEAPI/silc_ske_alloc
287  *
288  * SYNOPSIS
289  *
290  *    SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
291  *                           SilcPublicKey public_key,
292  *                           SilcPrivateKey private_key, void *context);
293  *
294  * DESCRIPTION
295  *
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.
304  *
305  * EXMPALE
306  *
307  *    // Initiator example
308  *    ske = silc_ske_alloc(rng, scheduler, app);
309  *    silc_ske_set_callbacks(ske, verify_public_key, completion, app);
310  *    start_payload =
311  *      silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_PFS |
312  *                                            SILC_SKE_SP_FLAG_MUTUAL,
313  *                                            version);
314  *    silc_ske_initiator_start(ske);
315  *
316  ***/
317 SilcSKE silc_ske_alloc(SilcRng rng, SilcSchedule schedule,
318                        SilcPublicKey public_key, SilcPrivateKey private_key,
319                        void *context);
320
321 /****f* silcske/SilcSKEAPI/silc_ske_free
322  *
323  * SYNOPSIS
324  *
325  *    void silc_ske_free(SilcSKE ske);
326  *
327  * DESCRIPTION
328  *
329  *    Frees the SKE session context and all allocated resources.
330  *
331  ***/
332 void silc_ske_free(SilcSKE ske);
333
334 /****f* silcske/SilcSKEAPI/silc_ske_get_context
335  *
336  * SYNOPSIS
337  *
338  *    void *silc_ske_get_context(SilcSKE ske);
339  *
340  * DESCRIPTION
341  *
342  *    Returns the context that was given as argument to silc_ske_alloc.
343  *
344  ***/
345 void *silc_ske_get_context(SilcSKE ske);
346
347 /****f* silcske/SilcSKEAPI/silc_ske_set_callbacks
348  *
349  * SYNOPSIS
350  *
351  *    void silc_ske_set_callbacks(SilcSKE ske,
352  *                                SilcSKEVerifyCb verify_key,
353  *                                SilcSKECompletion completed,
354  *                                void *context);
355  *
356  * DESCRIPTION
357  *
358  *    Sets the callback functions for the SKE session.
359  *
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.
367  *
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.
371  *
372  *    The `context' is passed as argument to all of the above callback
373  *    functions.
374  *
375  ***/
376 void silc_ske_set_callbacks(SilcSKE ske,
377                             SilcSKEVerifyCb verify_key,
378                             SilcSKECompletionCb completed,
379                             void *context);
380
381 /****f* silcske/SilcSKEAPI/silc_ske_initiator_start
382  *
383  * SYNOPSIS
384  *
385  *    SilcAsyncOperation
386  *    silc_ske_initiator_start(SilcSKE ske,
387  *                             SilcPacketStream stream,
388  *                             SilcSKEStartPayload start_payload);
389  *
390  * DESCRIPTION
391  *
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.
395  *
396  *    The `stream' is the network connection to the remote host.  Note that
397  *    SKE library will take over the packet stream `stream' while the
398  *    protocol is in process.  The application will not receive any packets
399  *    for `stream' after this function is called.  The `stream' is turned
400  *    over to application once the completion callback is called.
401  *
402  *    The `start_payload' includes all configured security properties that
403  *    will be sent to the responder.  The `start_payload' must be provided.
404  *    It can be created by calling silc_ske_assemble_security_properties
405  *    function.  The caller must not free the payload once it has been
406  *    given as argument to this function.
407  *
408  *    This function returns SilcAsyncOperation operation context which can
409  *    be used to control the protocol from the application.  Application may
410  *    for example safely abort the protocol at any point, if needed.  Returns
411  *    NULL on error.
412  *
413  ***/
414 SilcAsyncOperation
415 silc_ske_initiator(SilcSKE ske,
416                    SilcPacketStream stream,
417                    SilcSKEStartPayload start_payload);
418
419 /****f* silcske/SilcSKEAPI/silc_ske_responder_start
420  *
421  * SYNOPSIS
422  *
423  *    SilcAsyncOperation
424  *    silc_ske_responder_start(SilcSKE ske,
425  *                             SilcPacketStream stream,
426  *                             const char *version,
427  *                             SilcBuffer start_payload,
428  *                             SilcSKESecurityPropertyFlag flags);
429  *
430  * DESCRIPTION
431  *
432  *    Starts SILC Key Exchange protocol as responder.  The completion
433  *    callback that was set in silc_ske_set_callbacks will be called once
434  *    the protocol has completed.
435  *
436  *    The `stream' is the network connection to the remote host.  Note that
437  *    SKE library will take over the packet stream `stream' while the
438  *    protocol is in process.  The application will not receive any packets
439  *    for `stream' after this function is called.  The `stream' is turned
440  *    over to application once the completion callback is called.
441  *
442  *    The `version' is the responder's SILC protocol version that will be
443  *    sent in reply to the initiator.  The `flags' indicates the
444  *    SilcSKESecurityPropertyFlag flags that responder supports and enforces
445  *    for the initiator.  Responder may, for example, enforce that the PFS
446  *    will be performed in rekey.
447  *
448  *    This function returns SilcAsyncOperation operation context which can
449  *    be used to control the protocol from the application.  Application may
450  *    for example safely abort the protocol at any point, if needed.  Returns
451  *    NULL on error.
452  *
453  ***/
454 SilcAsyncOperation
455 silc_ske_responder(SilcSKE ske,
456                    SilcPacketStream stream,
457                    const char *version,
458                    SilcSKESecurityPropertyFlag flags);
459
460 SilcAsyncOperation
461 silc_ske_rekey_initiator(SilcSKE ske,
462                          SilcPacketStream stream,
463                          SilcSKERekeyMaterial rekey);
464
465 SilcAsyncOperation
466 silc_ske_rekey_responder(SilcSKE ske,
467                          SilcPacketStream stream,
468                          SilcBuffer ke_payload,
469                          SilcSKERekeyMaterial rekey);
470
471 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
472  *
473  * SYNOPSIS
474  *
475  *    SilcSKEStartPayload
476  *    silc_ske_assemble_security_properties(SilcSKE ske,
477  *                                          SilcSKESecurityPropertyFlag flags,
478  *                                          const char *version);
479  *
480  * DESCRIPTION
481  *
482  *    Assembles security properties to Key Exchange Start Payload to be
483  *    sent to the remote end.  This checks system wide (SILC system, that is)
484  *    settings and chooses from those.  However, if other properties
485  *    should be used this function is easy to replace by another function.
486  *    Returns NULL on error.  This is an utility function.  This is used
487  *    by the initiator of the protocol.  The `version' is the local SILC
488  *    protocol version string.
489  *
490  ***/
491 SilcSKEStartPayload
492 silc_ske_assemble_security_properties(SilcSKE ske,
493                                       SilcSKESecurityPropertyFlag flags,
494                                       const char *version);
495
496 /****f* silcske/SilcSKEAPI/silc_ske_assemble_security_properties
497  *
498  * SYNOPSIS
499  *
500  *    SilcBool silc_ske_set_keys(SilcSKE ske,
501  *                               SilcSKEKeyMaterial keymat,
502  *                               SilcSKESecurityProperties prop,
503  *                               SilcCipher *ret_send_key,
504  *                               SilcCipher *ret_receive_key,
505  *                               SilcHmac *ret_hmac_send,
506  *                               SilcHmac *ret_hmac_receive,
507  *                               SilcHash *ret_hash);
508  *
509  * DESCRIPTION
510  *
511  *    This function can be used after successful key exchange to take the
512  *    key material `keymat' with security properties `prop' into use.
513  *    This will allocate send and receive ciphers, HMACs and hash for the
514  *    application.  Caller must free the returned contexts.  This is an
515  *    utility function.
516  *
517  ***/
518 SilcBool silc_ske_set_keys(SilcSKE ske,
519                            SilcSKEKeyMaterial keymat,
520                            SilcSKESecurityProperties prop,
521                            SilcCipher *ret_send_key,
522                            SilcCipher *ret_receive_key,
523                            SilcHmac *ret_hmac_send,
524                            SilcHmac *ret_hmac_receive,
525                            SilcHash *ret_hash);
526
527 /****f* silcske/SilcSKEAPI/silc_ske_parse_version
528  *
529  * SYNOPSIS
530  *
531  *    SilcBool silc_ske_parse_version(SilcSKE ske,
532  *                                    SilcUInt32 *protocol_version,
533  *                                    char **protocol_version_string,
534  *                                    SilcUInt32 *software_version,
535  *                                    char **software_version_string,
536  *                                    char **vendor_version);
537  *
538  * DESCRIPTION
539  *
540  *    Utility function to parse the remote host's version string.
541  *
542  ***/
543 SilcBool silc_ske_parse_version(SilcSKE ske,
544                                 SilcUInt32 *protocol_version,
545                                 char **protocol_version_string,
546                                 SilcUInt32 *software_version,
547                                 char **software_version_string,
548                                 char **vendor_version);
549
550 /****f* silcske/SilcSKEAPI/silc_ske_map_status
551  *
552  * SYNOPSIS
553  *
554  *    const char *silc_ske_map_status(SilcSKEStatus status);
555  *
556  * DESCRIPTION
557  *
558  *    Utility function to map the `status' into human readable message.
559  *
560  ***/
561 const char *silc_ske_map_status(SilcSKEStatus status);
562
563 #include "silcske_i.h"
564
565 #endif  /* !SILCSKE_H */