5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 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* silccrypt/SILC PKCS Interface
24 * SILC PKCS API provides generic interface for performing various
25 * public key cryptography related operations with different types of
26 * public and private keys. Support for loading and saving of different
27 * types of public key and private keys are also provided.
34 /* Forward declarations */
35 typedef struct SilcPKCSObjectStruct SilcPKCSObject;
37 /****d* silccrypt/SilcPKCSAPI/SilcPKCSType
41 * typedef enum { ... } SilcPKCSType;
45 * Public key cryptosystem types. These are defined by the SILC
46 * Key Exchange protocol.
51 SILC_PKCS_SILC = 1, /* SILC PKCS */
52 SILC_PKCS_SSH2 = 2, /* SSH2 PKCS (not supported) */
53 SILC_PKCS_X509V3 = 3, /* X.509v3 PKCS (not supported) */
54 SILC_PKCS_OPENPGP = 4, /* OpenPGP PKCS (not supported) */
55 SILC_PKCS_SPKI = 5, /* SPKI PKCS (not supported) */
59 /****s* silccrypt/SilcPKCSAPI/SilcPublicKey
63 * typedef struct { ... } *SilcPublicKey;
67 * This context represents any kind of PKCS public key. It can be
68 * allocated by silc_pkcs_public_key_alloc and is freed by the
69 * silc_pkcs_public_key_free. The PKCS specific public key context
70 * can be retrieved by calling silc_pkcs_get_context.
75 const SilcPKCSObject *pkcs; /* PKCS */
76 void *public_key; /* PKCS specific public key */
80 /****s* silccrypt/SilcPKCSAPI/SilcPrivateKey
84 * typedef struct { ... } *SilcPrivateKey;
88 * This context represents any kind of PKCS private key.
93 const SilcPKCSObject *pkcs; /* PKCS */
94 void *private_key; /* PKCS specific private key */
98 /****d* silccrypt/SilcPKCSAPI/SilcPKCSFileEncoding
102 * typedef enum { ... } SilcPKCSType
106 * Public and private key file encoding types.
111 SILC_PKCS_FILE_BIN, /* Binary encoding */
112 SILC_PKCS_FILE_BASE64 /* Base64 encoding */
113 } SilcPKCSFileEncoding;
116 /* The PKCS Algorithm object to represent any PKCS algorithm. */
118 /* Algorithm name and scheme */
122 /* Supported hash functions, comma separated list */
125 /* Generate new key pair. Returns PKCS algorithm specific public key
126 and private key contexts. */
127 SilcBool (*generate_key)(SilcUInt32 keylen,
129 void **ret_public_key,
130 void **ret_private_key);
132 /* Public key routines */
133 SilcBool (*import_public_key)(unsigned char *key,
135 void **ret_public_key);
136 unsigned char *(*export_public_key)(void *public_key,
137 SilcUInt32 *ret_len);
138 SilcUInt32 (*public_key_bitlen)(void *public_key);
139 void *(*public_key_copy)(void *public_key);
140 SilcBool (*public_key_compare)(void *key1, void *key2);
141 void (*public_key_free)(void *public_key);
143 /* Private key routines */
144 SilcBool (*import_private_key)(unsigned char *key,
146 void **ret_private_key);
147 unsigned char *(*export_private_key)(void *private_key,
148 SilcUInt32 *ret_len);
149 SilcUInt32 (*private_key_bitlen)(void *public_key);
150 void (*private_key_free)(void *private_key);
152 /* Encrypt and decrypt operations */
153 SilcBool (*encrypt)(void *public_key,
158 SilcUInt32 *ret_dst_len);
159 SilcBool (*decrypt)(void *private_key,
164 SilcUInt32 *ret_dst_len);
166 /* Signature and verification operations */
167 SilcBool (*sign)(void *private_key,
170 unsigned char *signature,
171 SilcUInt32 signature_size,
172 SilcUInt32 *ret_signature_len,
174 SilcBool (*verify)(void *public_key,
175 unsigned char *signature,
176 SilcUInt32 signature_len,
182 /* The PKCS (Public Key Cryptosystem) object to represent any PKCS. */
183 struct SilcPKCSObjectStruct {
187 /* Public key routines */
189 /* Returns PKCS algorithm context from public key */
190 const SilcPKCSAlgorithm *(*get_algorithm)(void *public_key);
192 /* Imports from public key file */
193 SilcBool (*import_public_key_file)(unsigned char *filedata,
194 SilcUInt32 filedata_len,
195 SilcPKCSFileEncoding encoding,
196 void **ret_public_key);
198 /* Imports from public key binary data */
199 SilcBool (*import_public_key)(unsigned char *key,
201 void **ret_public_key);
203 /* Exports public key to file */
204 unsigned char *(*export_public_key_file)(void *public_key,
205 SilcPKCSFileEncoding encoding,
206 SilcUInt32 *ret_len);
208 /* Export public key as binary data */
209 unsigned char *(*export_public_key)(void *public_key,
210 SilcUInt32 *ret_len);
212 /* Returns key length in bits */
213 SilcUInt32 (*public_key_bitlen)(void *public_key);
215 /* Copy public key */
216 void *(*public_key_copy)(void *public_key);
218 /* Compares public keys */
219 SilcBool (*public_key_compare)(void *key1, void *key2);
221 /* Free public key */
222 void (*public_key_free)(void *public_key);
224 /* Private key routines */
226 /* Imports from private key file */
227 SilcBool (*import_private_key_file)(unsigned char *filedata,
228 SilcUInt32 filedata_len,
229 const char *passphrase,
230 SilcUInt32 passphrase_len,
231 SilcPKCSFileEncoding encoding,
232 void **ret_private_key);
234 /* Imports from private key binary data */
235 SilcBool (*import_private_key)(unsigned char *key,
237 void **ret_private_key);
239 /* Exports private key to file */
240 unsigned char *(*export_private_key_file)(void *private_key,
241 const char *passphrase,
242 SilcUInt32 passphrase_len,
243 SilcPKCSFileEncoding encoding,
245 SilcUInt32 *ret_len);
247 /* Export private key as binary data */
248 unsigned char *(*export_private_key)(void *private_key,
249 SilcUInt32 *ret_len);
251 /* Returns key length in bits */
252 SilcUInt32 (*private_key_bitlen)(void *private_key);
254 /* Free private key */
255 void (*private_key_free)(void *private_key);
257 /* Encrypt and decrypt operations */
258 SilcBool (*encrypt)(void *public_key,
263 SilcUInt32 *ret_dst_len);
264 SilcBool (*decrypt)(void *private_key,
269 SilcUInt32 *ret_dst_len);
271 /* Signature and verification operations */
272 SilcBool (*sign)(void *private_key,
275 unsigned char *signature,
276 SilcUInt32 signature_size,
277 SilcUInt32 *ret_signature_len,
279 SilcBool (*verify)(void *public_key,
280 unsigned char *signature,
281 SilcUInt32 signature_len,
287 /* Marks for all PKCS in silc. This can be used in silc_pkcs_unregister
288 to unregister all PKCS at once. */
289 #define SILC_ALL_PKCS ((SilcPKCSObject *)1)
290 #define SILC_ALL_PKCS_ALG ((SilcPKCSAlgorithm *)1)
292 /* Static lists of PKCS and PKCS algorithms. */
293 extern DLLAPI const SilcPKCSObject silc_default_pkcs[];
294 extern DLLAPI const SilcPKCSAlgorithm silc_default_pkcs_alg[];
298 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register
302 * SilcBool silc_pkcs_register(const SilcPKCSObject *pkcs);
306 * Registers a new PKCS into the SILC. This function is used
307 * at the initialization of the SILC. All registered PKCSs
308 * should be unregistered with silc_pkcs_unregister. The `pkcs' includes
309 * the name of the PKCS and member functions for the algorithm. Usually
310 * this function is not called directly. Instead, application can call
311 * the silc_pkcs_register_default to register all PKCSs that are
312 * builtin the sources. Returns FALSE on error.
315 SilcBool silc_pkcs_register(const SilcPKCSObject *pkcs);
317 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister
321 * SilcBool silc_pkcs_unregister(SilcPKCSObject *pkcs);
325 * Unregister a PKCS from the SILC. Returns FALSE on error.
328 SilcBool silc_pkcs_unregister(SilcPKCSObject *pkcs);
330 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_algorithm_register
334 * SilcBool silc_pkcs_algorithm_register(const SilcPKCSAlgorithm *pkcs);
338 * Registers a new PKCS Algorithm into the SILC. This function is used
339 * at the initialization of the SILC. All registered PKCS algorithms
340 * should be unregistered with silc_pkcs_unregister.
343 SilcBool silc_pkcs_algorithm_register(const SilcPKCSAlgorithm *pkcs);
345 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_algorithm_unregister
349 * SilcBool silc_pkcs_algorithm_unregister(SilcPKCSAlgorithm *pkcs);
353 * Unregister a PKCS from the SILC. Returns FALSE on error.
356 SilcBool silc_pkcs_algorithm_unregister(SilcPKCSAlgorithm *pkcs);
358 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register_default
362 * SilcBool silc_pkcs_register_default(void);
366 * Registers all the default PKCS (all builtin PKCS) and PKCS algorithms.
367 * The application may use this to register the default PKCS if specific
368 * PKCS in any specific order is not wanted. Returns FALSE on error.
371 SilcBool silc_pkcs_register_default(void);
373 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister_all
377 * SilcBool silc_pkcs_unregister_all(void);
381 * Unregister all PKCS and PKCS algorithms. Returns FALSE on error.
384 SilcBool silc_pkcs_unregister_all(void);
386 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_supported
390 * char *silc_pkcs_get_supported(void);
394 * Returns comma separated list of supported PKCS algorithms.
397 char *silc_pkcs_get_supported(void);
399 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_find_pkcs
403 * const SilcPKCSObject *silc_pkcs_get_pkcs(SilcPKCSType type);
407 * Finds PKCS context by the PKCS type.
410 const SilcPKCSObject *silc_pkcs_find_pkcs(SilcPKCSType type);
412 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_find_algorithm
416 * const SilcPKCSAlgorithm *silc_pkcs_find_algorithm(const char *algorithm,
417 * const char *scheme);
421 * Finds PKCS algorithm context by the algorithm name `algorithm' and
422 * the algorithm scheme `scheme'. The `scheme' may be NULL.
425 const SilcPKCSAlgorithm *silc_pkcs_find_algorithm(const char *algorithm,
428 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_pkcs
432 * const SilcPKCSObject *silc_pkcs_get_pkcs(void *key);
436 * Returns the PKCS object from `key', which may be SilcPublicKey or
437 * SilcPrivateKey pointer.
440 const SilcPKCSObject *silc_pkcs_get_pkcs(void *key);
442 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_algorithm
446 * const SilcPKCSAlgorithm *silc_pkcs_get_algorithm(void *key);
450 * Returns the PKCS algorithm object from `key', which may be SilcPublicKey
451 * or SilcPrivateKey pointer.
454 const SilcPKCSAlgorithm *silc_pkcs_get_algorithm(void *key);
456 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_name
460 * const char *silc_pkcs_get_name(void *key);
464 * Returns PKCS algorithm name from the `key', which may be SilcPublicKey
465 * or SilcPrivateKey pointer.
468 const char *silc_pkcs_get_name(void *key);
470 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_type
474 * SilcPKCSType silc_pkcs_get_type(void *key);
478 * Returns PKCS type from the `key', which may be SilcPublicKey or
479 * SilcPrivateKey pointer.
482 SilcPKCSType silc_pkcs_get_type(void *key);
484 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_context
488 * void *silc_pkcs_get_context(SilcPKCSType type, SilcPublicKey public_key);
492 * Returns the internal PKCS `type' specific public key context from the
493 * `public_key'. The caller needs to explicitly type cast it to correct
494 * type. Returns NULL on error.
496 * For SILC_PKCS_SILC the returned context is SilcSILCPublicKey.
499 void *silc_pkcs_get_context(SilcPKCSType type, SilcPublicKey public_key);
501 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_alloc
505 * SilcBool silc_pkcs_public_key_alloc(SilcPKCSType type,
506 * unsigned char *key,
508 * SilcPublicKey *ret_public_key);
512 * Allocates SilcPublicKey of the type of `type' from the key data
513 * `key' of length of `key_len' bytes. Returns FALSE if the `key'
514 * is malformed or unsupported public key type. This function can be
515 * used to create public key from any kind of PKCS public keys that
516 * the implementation supports.
519 SilcBool silc_pkcs_public_key_alloc(SilcPKCSType type,
522 SilcPublicKey *ret_public_key);
524 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_free
528 * void silc_pkcs_public_key_free(SilcPublicKey public_key);
532 * Frees the public key.
535 void silc_pkcs_public_key_free(SilcPublicKey public_key);
537 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_export
541 * unsigned char *silc_pkcs_public_key_encode(SilcPublicKey public_key,
542 * SilcUInt32 *ret_len);
546 * Encodes the `public_key' into a binary format and returns it. Returns
547 * NULL on error. Caller must free the returned buffer.
550 unsigned char *silc_pkcs_public_key_encode(SilcPublicKey public_key,
551 SilcUInt32 *ret_len);
553 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_get_len
557 * SilcUInt32 silc_pkcs_public_key_get_len(SilcPublicKey public_key);
561 * Returns the key length in bits from the public key.
564 SilcUInt32 silc_pkcs_public_key_get_len(SilcPublicKey public_key);
566 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_compare
570 * SilcBool silc_pkcs_public_key_compare(SilcPublicKey key1,
571 * SilcPublicKey key2);
575 * Compares two public keys and returns TRUE if they are same key, and
576 * FALSE if they are not same.
579 SilcBool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2);
581 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_copy
585 * SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
589 * Copies the public key indicated by `public_key' and returns new
590 * allocated public key which is indentical to the `public_key'.
593 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
595 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_alloc
599 * SilcBool silc_pkcs_private_key_alloc(SilcPKCSType type,
600 * unsigned char *key,
601 * SilcUInt32 key_len,
602 * SilcPrivateKey *ret_private_key);
606 * Allocates SilcPrivateKey of the type of `type' from the key data
607 * `key' of length of `key_len' bytes. Returns FALSE if the `key'
608 * is malformed or unsupported private key type.
611 SilcBool silc_pkcs_private_key_alloc(SilcPKCSType type,
614 SilcPrivateKey *ret_private_key);
616 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_get_len
620 * SilcUInt32 silc_pkcs_private_key_get_len(SilcPrivateKey private_key);
624 * Returns the key length in bits from the private key.
627 SilcUInt32 silc_pkcs_private_key_get_len(SilcPrivateKey private_key);
629 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_free
633 * void silc_pkcs_private_key_free(SilcPrivateKey private_key;
637 * Frees the private key.
640 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
642 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_encrypt
646 * SilcBool silc_pkcs_encrypt(SilcPublicKey public_key,
647 * unsigned char *src, SilcUInt32 src_len,
648 * unsigned char *dst, SilcUInt32 dst_size,
649 * SilcUInt32 *dst_len);
653 * Encrypts with the public key. Returns FALSE on error.
656 SilcBool silc_pkcs_encrypt(SilcPublicKey public_key,
657 unsigned char *src, SilcUInt32 src_len,
658 unsigned char *dst, SilcUInt32 dst_size,
659 SilcUInt32 *dst_len);
661 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_decrypt
665 * SilcBool silc_pkcs_decrypt(SilcPrivateKey private_key,
666 * unsigned char *src, SilcUInt32 src_len,
667 * unsigned char *dst, SilcUInt32 dst_size,
668 * SilcUInt32 *dst_len);
672 * Decrypts with the private key. Returns FALSE on error.
675 SilcBool silc_pkcs_decrypt(SilcPrivateKey private_key,
676 unsigned char *src, SilcUInt32 src_len,
677 unsigned char *dst, SilcUInt32 dst_size,
678 SilcUInt32 *dst_len);
680 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_sign
684 * SilcBool silc_pkcs_sign(SilcPrivateKey private_key,
685 * unsigned char *src, SilcUInt32 src_len,
686 * unsigned char *dst, SilcUInt32 dst_size,
687 * SilcUInt32 *dst_len, SilcHash hash);
691 * Generates signature with the private key. Returns FALSE on error.
692 * If `hash' is non-NULL the `src' will be hashed before signing.
695 SilcBool silc_pkcs_sign(SilcPrivateKey private_key,
696 unsigned char *src, SilcUInt32 src_len,
697 unsigned char *dst, SilcUInt32 dst_size,
698 SilcUInt32 *dst_len, SilcHash hash);
700 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_verify
704 * SilcBool silc_pkcs_verify(SilcPublicKey public_key,
705 * unsigned char *signature,
706 * SilcUInt32 signature_len,
707 * unsigned char *data,
708 * SilcUInt32 data_len, SilcHash hash);
712 * Verifies signature. Returns FALSE on error. The 'signature' is
713 * verified against the 'data'. If the `hash' is non-NULL then the `data'
714 * will hashed before verification. If the `hash' is NULL, then the
715 * hash algorithm to be used is retrieved from the signature. If it
716 * isn't present in the signature the verification is done as is without
720 SilcBool silc_pkcs_verify(SilcPublicKey public_key,
721 unsigned char *signature,
722 SilcUInt32 signature_len,
724 SilcUInt32 data_len, SilcHash hash);
726 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_public_key
730 * SilcBool silc_pkcs_load_public_key(const char *filename,
731 * SilcPublicKey *ret_public_key);
735 * Loads public key from file and allocates new public key. Returns TRUE
736 * if loading was successful.
739 SilcBool silc_pkcs_load_public_key(const char *filename,
740 SilcPublicKey *ret_public_key);
742 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_public_key
746 * SilcBool silc_pkcs_save_public_key(const char *filename,
747 * SilcPublicKey public_key,
748 * SilcPKCSFileEncoding encoding);
752 * Saves public key into file with specified encoding. Returns FALSE
756 SilcBool silc_pkcs_save_public_key(const char *filename,
757 SilcPublicKey public_key,
758 SilcPKCSFileEncoding encoding);
760 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_private_key
764 * SilcBool silc_pkcs_load_private_key(const char *filename,
765 * const unsigned char *passphrase,
766 * SilcUInt32 passphrase_len,
767 * SilcPrivateKey *ret_private_key);
771 * Loads private key from file and allocates new private key. Returns TRUE
772 * if loading was successful. The `passphrase' is used as decryption
773 * key of the private key file, in case it is encrypted.
776 SilcBool silc_pkcs_load_private_key(const char *filename,
777 const unsigned char *passphrase,
778 SilcUInt32 passphrase_len,
779 SilcPrivateKey *ret_private_key);
781 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_private_key
785 * SilcBool silc_pkcs_save_private_key(const char *filename,
786 * SilcPrivateKey private_key,
787 * const unsigned char *passphrase,
788 * SilcUInt32 passphrase_len,
789 * SilcPKCSFileEncoding encoding,
794 * Saves private key into file. The private key is encrypted into
795 * the file with the `passphrase' as a key, if PKCS supports encrypted
796 * private keys. Returns FALSE on error.
799 SilcBool silc_pkcs_save_private_key(const char *filename,
800 SilcPrivateKey private_key,
801 const unsigned char *passphrase,
802 SilcUInt32 passphrase_len,
803 SilcPKCSFileEncoding encoding,
806 #endif /* !SILCPKCS_H */