summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/x_pubkey.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/x_pubkey.c')
-rw-r--r--src/lib/libcrypto/asn1/x_pubkey.c154
1 files changed, 89 insertions, 65 deletions
diff --git a/src/lib/libcrypto/asn1/x_pubkey.c b/src/lib/libcrypto/asn1/x_pubkey.c
index 1d6ab6580a..df915e2dfd 100644
--- a/src/lib/libcrypto/asn1/x_pubkey.c
+++ b/src/lib/libcrypto/asn1/x_pubkey.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -69,8 +69,8 @@
69#endif 69#endif
70 70
71/* Minor tweak to operation: free up EVP_PKEY */ 71/* Minor tweak to operation: free up EVP_PKEY */
72static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 72static int
73 void *exarg) 73pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
74{ 74{
75 if (operation == ASN1_OP_FREE_POST) { 75 if (operation == ASN1_OP_FREE_POST) {
76 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; 76 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
@@ -86,54 +86,61 @@ ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
86 86
87IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY) 87IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
88 88
89int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) 89int
90X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
90{ 91{
91 X509_PUBKEY *pk=NULL; 92 X509_PUBKEY *pk = NULL;
92 93
93 if (x == NULL) return(0); 94 if (x == NULL)
94 95 return (0);
95 if ((pk=X509_PUBKEY_new()) == NULL) goto error; 96 if ((pk = X509_PUBKEY_new()) == NULL)
97 goto error;
96 98
97 if (pkey->ameth) { 99 if (pkey->ameth) {
98 if (pkey->ameth->pub_encode) { 100 if (pkey->ameth->pub_encode) {
99 if (!pkey->ameth->pub_encode(pk, pkey)) { 101 if (!pkey->ameth->pub_encode(pk, pkey)) {
100 X509err(X509_F_X509_PUBKEY_SET, 102 X509err(X509_F_X509_PUBKEY_SET,
101 X509_R_PUBLIC_KEY_ENCODE_ERROR); 103 X509_R_PUBLIC_KEY_ENCODE_ERROR);
102 goto error; 104 goto error;
103 } 105 }
104 } else { 106 } else {
105 X509err(X509_F_X509_PUBKEY_SET, 107 X509err(X509_F_X509_PUBKEY_SET,
106 X509_R_METHOD_NOT_SUPPORTED); 108 X509_R_METHOD_NOT_SUPPORTED);
107 goto error; 109 goto error;
108 } 110 }
109 } else { 111 } else {
110 X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM); 112 X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
111 goto error; 113 goto error;
112 } 114 }
113 115
114 if (*x != NULL) 116 if (*x != NULL)
115 X509_PUBKEY_free(*x); 117 X509_PUBKEY_free(*x);
116 118
117 *x=pk; 119 *x = pk;
118 120
119 return 1; 121 return 1;
122
120error: 123error:
121 if (pk != NULL) X509_PUBKEY_free(pk); 124 if (pk != NULL)
125 X509_PUBKEY_free(pk);
122 return 0; 126 return 0;
123} 127}
124 128
125EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key) 129EVP_PKEY *
130X509_PUBKEY_get(X509_PUBKEY *key)
126{ 131{
127 EVP_PKEY *ret=NULL; 132 EVP_PKEY *ret = NULL;
128 133
129 if (key == NULL) goto error; 134 if (key == NULL)
135 goto error;
130 136
131 if (key->pkey != NULL) { 137 if (key->pkey != NULL) {
132 CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); 138 CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
133 return key->pkey; 139 return key->pkey;
134 } 140 }
135 141
136 if (key->public_key == NULL) goto error; 142 if (key->public_key == NULL)
143 goto error;
137 144
138 if ((ret = EVP_PKEY_new()) == NULL) { 145 if ((ret = EVP_PKEY_new()) == NULL) {
139 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE); 146 X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
@@ -141,14 +148,14 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
141 } 148 }
142 149
143 if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) { 150 if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) {
144 X509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM); 151 X509err(X509_F_X509_PUBKEY_GET, X509_R_UNSUPPORTED_ALGORITHM);
145 goto error; 152 goto error;
146 } 153 }
147 154
148 if (ret->ameth->pub_decode) { 155 if (ret->ameth->pub_decode) {
149 if (!ret->ameth->pub_decode(ret, key)) { 156 if (!ret->ameth->pub_decode(ret, key)) {
150 X509err(X509_F_X509_PUBKEY_GET, 157 X509err(X509_F_X509_PUBKEY_GET,
151 X509_R_PUBLIC_KEY_DECODE_ERROR); 158 X509_R_PUBLIC_KEY_DECODE_ERROR);
152 goto error; 159 goto error;
153 } 160 }
154 } else { 161 } else {
@@ -170,39 +177,44 @@ EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
170 177
171 return ret; 178 return ret;
172 179
173 error: 180error:
174 if (ret != NULL) 181 if (ret != NULL)
175 EVP_PKEY_free(ret); 182 EVP_PKEY_free(ret);
176 return(NULL); 183 return (NULL);
177} 184}
178 185
179/* Now two pseudo ASN1 routines that take an EVP_PKEY structure 186/* Now two pseudo ASN1 routines that take an EVP_PKEY structure
180 * and encode or decode as X509_PUBKEY 187 * and encode or decode as X509_PUBKEY
181 */ 188 */
182 189
183EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, 190EVP_PKEY *
184 long length) 191d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
185{ 192{
186 X509_PUBKEY *xpk; 193 X509_PUBKEY *xpk;
187 EVP_PKEY *pktmp; 194 EVP_PKEY *pktmp;
188 xpk = d2i_X509_PUBKEY(NULL, pp, length); 195 xpk = d2i_X509_PUBKEY(NULL, pp, length);
189 if(!xpk) return NULL; 196 if (!xpk)
197 return NULL;
190 pktmp = X509_PUBKEY_get(xpk); 198 pktmp = X509_PUBKEY_get(xpk);
191 X509_PUBKEY_free(xpk); 199 X509_PUBKEY_free(xpk);
192 if(!pktmp) return NULL; 200 if (!pktmp)
193 if(a) { 201 return NULL;
202 if (a) {
194 EVP_PKEY_free(*a); 203 EVP_PKEY_free(*a);
195 *a = pktmp; 204 *a = pktmp;
196 } 205 }
197 return pktmp; 206 return pktmp;
198} 207}
199 208
200int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp) 209int
210i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
201{ 211{
202 X509_PUBKEY *xpk=NULL; 212 X509_PUBKEY *xpk = NULL;
203 int ret; 213 int ret;
204 if(!a) return 0; 214 if (!a)
205 if(!X509_PUBKEY_set(&xpk, a)) return 0; 215 return 0;
216 if (!X509_PUBKEY_set(&xpk, a))
217 return 0;
206 ret = i2d_X509_PUBKEY(xpk, pp); 218 ret = i2d_X509_PUBKEY(xpk, pp);
207 X509_PUBKEY_free(xpk); 219 X509_PUBKEY_free(xpk);
208 return ret; 220 return ret;
@@ -212,18 +224,20 @@ int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
212 * keys 224 * keys
213 */ 225 */
214#ifndef OPENSSL_NO_RSA 226#ifndef OPENSSL_NO_RSA
215RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, 227RSA *
216 long length) 228d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
217{ 229{
218 EVP_PKEY *pkey; 230 EVP_PKEY *pkey;
219 RSA *key; 231 RSA *key;
220 const unsigned char *q; 232 const unsigned char *q;
221 q = *pp; 233 q = *pp;
222 pkey = d2i_PUBKEY(NULL, &q, length); 234 pkey = d2i_PUBKEY(NULL, &q, length);
223 if (!pkey) return NULL; 235 if (!pkey)
236 return NULL;
224 key = EVP_PKEY_get1_RSA(pkey); 237 key = EVP_PKEY_get1_RSA(pkey);
225 EVP_PKEY_free(pkey); 238 EVP_PKEY_free(pkey);
226 if (!key) return NULL; 239 if (!key)
240 return NULL;
227 *pp = q; 241 *pp = q;
228 if (a) { 242 if (a) {
229 RSA_free(*a); 243 RSA_free(*a);
@@ -232,11 +246,13 @@ RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp,
232 return key; 246 return key;
233} 247}
234 248
235int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp) 249int
250i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
236{ 251{
237 EVP_PKEY *pktmp; 252 EVP_PKEY *pktmp;
238 int ret; 253 int ret;
239 if (!a) return 0; 254 if (!a)
255 return 0;
240 pktmp = EVP_PKEY_new(); 256 pktmp = EVP_PKEY_new();
241 if (!pktmp) { 257 if (!pktmp) {
242 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE); 258 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
@@ -250,18 +266,20 @@ int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
250#endif 266#endif
251 267
252#ifndef OPENSSL_NO_DSA 268#ifndef OPENSSL_NO_DSA
253DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, 269DSA *
254 long length) 270d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
255{ 271{
256 EVP_PKEY *pkey; 272 EVP_PKEY *pkey;
257 DSA *key; 273 DSA *key;
258 const unsigned char *q; 274 const unsigned char *q;
259 q = *pp; 275 q = *pp;
260 pkey = d2i_PUBKEY(NULL, &q, length); 276 pkey = d2i_PUBKEY(NULL, &q, length);
261 if (!pkey) return NULL; 277 if (!pkey)
278 return NULL;
262 key = EVP_PKEY_get1_DSA(pkey); 279 key = EVP_PKEY_get1_DSA(pkey);
263 EVP_PKEY_free(pkey); 280 EVP_PKEY_free(pkey);
264 if (!key) return NULL; 281 if (!key)
282 return NULL;
265 *pp = q; 283 *pp = q;
266 if (a) { 284 if (a) {
267 DSA_free(*a); 285 DSA_free(*a);
@@ -270,13 +288,15 @@ DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp,
270 return key; 288 return key;
271} 289}
272 290
273int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp) 291int
292i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
274{ 293{
275 EVP_PKEY *pktmp; 294 EVP_PKEY *pktmp;
276 int ret; 295 int ret;
277 if(!a) return 0; 296 if (!a)
297 return 0;
278 pktmp = EVP_PKEY_new(); 298 pktmp = EVP_PKEY_new();
279 if(!pktmp) { 299 if (!pktmp) {
280 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE); 300 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
281 return 0; 301 return 0;
282 } 302 }
@@ -288,44 +308,49 @@ int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
288#endif 308#endif
289 309
290#ifndef OPENSSL_NO_EC 310#ifndef OPENSSL_NO_EC
291EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length) 311EC_KEY *
312d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
292{ 313{
293 EVP_PKEY *pkey; 314 EVP_PKEY *pkey;
294 EC_KEY *key; 315 EC_KEY *key;
295 const unsigned char *q; 316 const unsigned char *q;
296 q = *pp; 317 q = *pp;
297 pkey = d2i_PUBKEY(NULL, &q, length); 318 pkey = d2i_PUBKEY(NULL, &q, length);
298 if (!pkey) return(NULL); 319 if (!pkey)
320 return (NULL);
299 key = EVP_PKEY_get1_EC_KEY(pkey); 321 key = EVP_PKEY_get1_EC_KEY(pkey);
300 EVP_PKEY_free(pkey); 322 EVP_PKEY_free(pkey);
301 if (!key) return(NULL); 323 if (!key)
324 return (NULL);
302 *pp = q; 325 *pp = q;
303 if (a) { 326 if (a) {
304 EC_KEY_free(*a); 327 EC_KEY_free(*a);
305 *a = key; 328 *a = key;
306 } 329 }
307 return(key); 330 return (key);
308} 331}
309 332
310int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp) 333int
334i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
311{ 335{
312 EVP_PKEY *pktmp; 336 EVP_PKEY *pktmp;
313 int ret; 337 int ret;
314 if (!a) return(0); 338 if (!a)
339 return (0);
315 if ((pktmp = EVP_PKEY_new()) == NULL) { 340 if ((pktmp = EVP_PKEY_new()) == NULL) {
316 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE); 341 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
317 return(0); 342 return (0);
318 } 343 }
319 EVP_PKEY_set1_EC_KEY(pktmp, a); 344 EVP_PKEY_set1_EC_KEY(pktmp, a);
320 ret = i2d_PUBKEY(pktmp, pp); 345 ret = i2d_PUBKEY(pktmp, pp);
321 EVP_PKEY_free(pktmp); 346 EVP_PKEY_free(pktmp);
322 return(ret); 347 return (ret);
323} 348}
324#endif 349#endif
325 350
326int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, 351int
327 int ptype, void *pval, 352X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, int ptype,
328 unsigned char *penc, int penclen) 353 void *pval, unsigned char *penc, int penclen)
329{ 354{
330 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval)) 355 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
331 return 0; 356 return 0;
@@ -334,17 +359,16 @@ int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
334 free(pub->public_key->data); 359 free(pub->public_key->data);
335 pub->public_key->data = penc; 360 pub->public_key->data = penc;
336 pub->public_key->length = penclen; 361 pub->public_key->length = penclen;
337 /* Set number of unused bits to zero */ 362 /* Set number of unused bits to zero */
338 pub->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); 363 pub->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
339 pub->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT; 364 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
340 } 365 }
341 return 1; 366 return 1;
342} 367}
343 368
344int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, 369int
345 const unsigned char **pk, int *ppklen, 370X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, const unsigned char **pk,
346 X509_ALGOR **pa, 371 int *ppklen, X509_ALGOR **pa, X509_PUBKEY *pub)
347 X509_PUBKEY *pub)
348{ 372{
349 if (ppkalg) 373 if (ppkalg)
350 *ppkalg = pub->algor->algorithm; 374 *ppkalg = pub->algor->algorithm;