updated openssl plugin to new private/public key API, use encoder framework
[strongswan.git] / src / libstrongswan / credentials / keys / key_encoding.h
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 /**
17 * @defgroup key_encoding key_encoding
18 * @{ @ingroup keys
19 */
20
21 #ifndef KEY_ENCODING_H_
22 #define KEY_ENCODING_H_
23
24 typedef struct key_encoding_t key_encoding_t;
25 typedef enum key_encoding_type_t key_encoding_type_t;
26 typedef enum key_encoding_part_t key_encoding_part_t;
27
28 #include <library.h>
29
30 /**
31 * Key encoder function implementing encoding/fingerprinting.
32 *
33 * The variable argument list takes key_encoding_part_t, followed by part
34 * specific arguments, terminated by KEY_PART_END.
35 *
36 * @param type format to encode the key to
37 * @param args list of (key_encoding_part_t, data)
38 * @param encoding encoding result, allocated
39 * @return TRUE if encoding successful
40 */
41 typedef bool (*key_encoder_t)(key_encoding_type_t type, chunk_t *encoding,
42 va_list args);
43
44 /**
45 * Helper function for key_encoder_t implementations to parse argument list.
46 *
47 * Key encoder functions get a variable argument list to parse. To simplify
48 * the job, this function reads the arguments and returns chunks for each
49 * part.
50 * The argument list of this function takes a key_encoding_part_t, followed
51 * by a data pointer receiving the value, terminated by KEY_PART_END.
52 *
53 * @param args argument list passed to key encoder function
54 * @param ... list of (key_encoding_part_t, data*)
55 * @return TRUE if all parts found, FALSE otherwise
56 */
57 bool key_encoding_args(va_list args, ...);
58
59 /**
60 * Encoding type of a fingerprint/private-/public-key.
61 *
62 * Fingerprints have have the KEY_ID_*, public keys the KEY_PUB_* and
63 * private keys the KEY_PRIV_* prefix.
64 */
65 enum key_encoding_type_t {
66 /** SHA1 fingerprint over subjectPublicKeyInfo */
67 KEY_ID_PUBKEY_INFO_SHA1 = 0,
68 /** SHA1 fingerprint over subjectPublicKey */
69 KEY_ID_PUBKEY_SHA1,
70 /** PGPv3 fingerprint */
71 KEY_ID_PGPV3,
72 /** PGPv4 fingerprint */
73 KEY_ID_PGPV4,
74
75 /** PKCS#1/ASN.1 key encoding */
76 KEY_PUB_ASN1_DER,
77 KEY_PRIV_ASN1_DER,
78 /** PGP key encoding */
79 KEY_PUB_PGP,
80 KEY_PRIV_PGP,
81
82 KEY_ENCODING_MAX,
83 };
84
85 /**
86 * Parts of a key to encode.
87 */
88 enum key_encoding_part_t {
89 /** modulus of a RSA key, n */
90 KEY_PART_RSA_MODULUS,
91 /** public exponent of a RSA key, e */
92 KEY_PART_RSA_PUB_EXP,
93 /** private exponent of a RSA key, d */
94 KEY_PART_RSA_PRIV_EXP,
95 /** prime1 a RSA key, p */
96 KEY_PART_RSA_PRIME1,
97 /** prime2 a RSA key, q */
98 KEY_PART_RSA_PRIME2,
99 /** exponent1 a RSA key, exp1 */
100 KEY_PART_RSA_EXP1,
101 /** exponent1 a RSA key, exp2 */
102 KEY_PART_RSA_EXP2,
103 /** coefficient of RSA key, coeff */
104 KEY_PART_RSA_COEFF,
105 /** a DER encoded RSA public key */
106 KEY_PART_RSA_PUB_ASN1_DER,
107 /** a DER encoded RSA private key */
108 KEY_PART_RSA_PRIV_ASN1_DER,
109 /** a DER encoded ECDSA public key */
110 KEY_PART_ECDSA_PUB_ASN1_DER,
111 /** a DER encoded ECDSA private key */
112 KEY_PART_ECDSA_PRIV_ASN1_DER,
113
114 KEY_PART_END,
115 };
116
117 /**
118 * Private/Public key encoding and fingerprinting facility.
119 */
120 struct key_encoding_t {
121
122 /**
123 * Encode a key into a format using several key parts, optional caching.
124 *
125 * The variable argument list takes key_encoding_part_t, followed by part
126 * specific arguments, terminated by KEY_PART_END.
127 * If a cache key is given, the returned encoding points to internal data:
128 * do not free or modify. If no cache key is given, the encoding is
129 * allocated and must be freed by the caller.
130 *
131 * @param type format the key should be encoded to
132 * @param cache key to use for caching, NULL to not cache
133 * @param encoding encoding result, allocated if caching disabled
134 * @param ... list of (key_encoding_part_t, data)
135 * @return TRUE if encoding successful
136 */
137 bool (*encode)(key_encoding_t *this, key_encoding_type_t type, void *cache,
138 chunk_t *encoding, ...);
139
140 /**
141 * Clear all cached encodings of a given cache key.
142 *
143 * @param cache key used in encode() for caching
144 */
145 void (*clear_cache)(key_encoding_t *this, void *cache);
146
147 /**
148 * Check for a cached encoding.
149 *
150 * @param type format of the key encoding
151 * @param cache key to use for caching, as given to encode()
152 * @encoding encoding result, internal data
153 * @return TRUE if cache entry found
154 */
155 bool (*get_cache)(key_encoding_t *this, key_encoding_type_t type,
156 void *cache, chunk_t *encoding);
157
158 /**
159 * Register a key encoder function.
160 *
161 * @param encoder key encoder function to add
162 */
163 void (*add_encoder)(key_encoding_t *this, key_encoder_t encoder);
164
165 /**
166 * Unregister a previously registered key encoder function.
167 *
168 * @param encoder key encoder function to remove
169 */
170 void (*remove_encoder)(key_encoding_t *this, key_encoder_t encoder);
171
172 /**
173 * Destroy a key_encoding_t.
174 */
175 void (*destroy)(key_encoding_t *this);
176 };
177
178 /**
179 * Create a key_encoding instance.
180 */
181 key_encoding_t *key_encoding_create();
182
183 #endif /* KEY_ENCODING_ @}*/