/src/libressl/crypto/asn1/x_pubkey.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: x_pubkey.c,v 1.32 2022/05/24 19:59:14 tb Exp $ */ |
2 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | | * All rights reserved. |
4 | | * |
5 | | * This package is an SSL implementation written |
6 | | * by Eric Young (eay@cryptsoft.com). |
7 | | * The implementation was written so as to conform with Netscapes SSL. |
8 | | * |
9 | | * This library is free for commercial and non-commercial use as long as |
10 | | * the following conditions are aheared to. The following conditions |
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 |
13 | | * included with this distribution is covered by the same copyright terms |
14 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | | * |
16 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | | * the code are not to be removed. |
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. |
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. |
22 | | * |
23 | | * Redistribution and use in source and binary forms, with or without |
24 | | * modification, are permitted provided that the following conditions |
25 | | * are met: |
26 | | * 1. Redistributions of source code must retain the copyright |
27 | | * notice, this list of conditions and the following disclaimer. |
28 | | * 2. Redistributions in binary form must reproduce the above copyright |
29 | | * notice, this list of conditions and the following disclaimer in the |
30 | | * documentation and/or other materials provided with the distribution. |
31 | | * 3. All advertising materials mentioning features or use of this software |
32 | | * must display the following acknowledgement: |
33 | | * "This product includes cryptographic software written by |
34 | | * Eric Young (eay@cryptsoft.com)" |
35 | | * The word 'cryptographic' can be left out if the rouines from the library |
36 | | * being used are not cryptographic related :-). |
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: |
39 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | | * |
41 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
45 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
46 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
47 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
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 |
51 | | * SUCH DAMAGE. |
52 | | * |
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 |
55 | | * copied and put under another distribution licence |
56 | | * [including the GNU Public Licence.] |
57 | | */ |
58 | | |
59 | | #include <stdio.h> |
60 | | |
61 | | #include <openssl/opensslconf.h> |
62 | | |
63 | | #include <openssl/asn1t.h> |
64 | | #include <openssl/err.h> |
65 | | #include <openssl/x509.h> |
66 | | |
67 | | #ifndef OPENSSL_NO_DSA |
68 | | #include <openssl/dsa.h> |
69 | | #endif |
70 | | #ifndef OPENSSL_NO_RSA |
71 | | #include <openssl/rsa.h> |
72 | | #endif |
73 | | |
74 | | #include "asn1_locl.h" |
75 | | #include "evp_locl.h" |
76 | | #include "x509_lcl.h" |
77 | | |
78 | | /* Minor tweak to operation: free up EVP_PKEY */ |
79 | | static int |
80 | | pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg) |
81 | 0 | { |
82 | 0 | if (operation == ASN1_OP_FREE_POST) { |
83 | 0 | X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; |
84 | 0 | EVP_PKEY_free(pubkey->pkey); |
85 | 0 | } |
86 | 0 | return 1; |
87 | 0 | } |
88 | | |
89 | | static const ASN1_AUX X509_PUBKEY_aux = { |
90 | | .asn1_cb = pubkey_cb, |
91 | | }; |
92 | | static const ASN1_TEMPLATE X509_PUBKEY_seq_tt[] = { |
93 | | { |
94 | | .offset = offsetof(X509_PUBKEY, algor), |
95 | | .field_name = "algor", |
96 | | .item = &X509_ALGOR_it, |
97 | | }, |
98 | | { |
99 | | .offset = offsetof(X509_PUBKEY, public_key), |
100 | | .field_name = "public_key", |
101 | | .item = &ASN1_BIT_STRING_it, |
102 | | }, |
103 | | }; |
104 | | |
105 | | const ASN1_ITEM X509_PUBKEY_it = { |
106 | | .itype = ASN1_ITYPE_SEQUENCE, |
107 | | .utype = V_ASN1_SEQUENCE, |
108 | | .templates = X509_PUBKEY_seq_tt, |
109 | | .tcount = sizeof(X509_PUBKEY_seq_tt) / sizeof(ASN1_TEMPLATE), |
110 | | .funcs = &X509_PUBKEY_aux, |
111 | | .size = sizeof(X509_PUBKEY), |
112 | | .sname = "X509_PUBKEY", |
113 | | }; |
114 | | |
115 | | X509_PUBKEY * |
116 | | d2i_X509_PUBKEY(X509_PUBKEY **a, const unsigned char **in, long len) |
117 | 0 | { |
118 | 0 | return (X509_PUBKEY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
119 | 0 | &X509_PUBKEY_it); |
120 | 0 | } |
121 | | |
122 | | int |
123 | | i2d_X509_PUBKEY(X509_PUBKEY *a, unsigned char **out) |
124 | 0 | { |
125 | 0 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_PUBKEY_it); |
126 | 0 | } |
127 | | |
128 | | X509_PUBKEY * |
129 | | X509_PUBKEY_new(void) |
130 | 0 | { |
131 | 0 | return (X509_PUBKEY *)ASN1_item_new(&X509_PUBKEY_it); |
132 | 0 | } |
133 | | |
134 | | void |
135 | | X509_PUBKEY_free(X509_PUBKEY *a) |
136 | 0 | { |
137 | 0 | ASN1_item_free((ASN1_VALUE *)a, &X509_PUBKEY_it); |
138 | 0 | } |
139 | | |
140 | | int |
141 | | X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) |
142 | 0 | { |
143 | 0 | X509_PUBKEY *pk = NULL; |
144 | |
|
145 | 0 | if (x == NULL) |
146 | 0 | return (0); |
147 | 0 | if ((pk = X509_PUBKEY_new()) == NULL) |
148 | 0 | goto error; |
149 | | |
150 | 0 | if (pkey->ameth) { |
151 | 0 | if (pkey->ameth->pub_encode) { |
152 | 0 | if (!pkey->ameth->pub_encode(pk, pkey)) { |
153 | 0 | X509error(X509_R_PUBLIC_KEY_ENCODE_ERROR); |
154 | 0 | goto error; |
155 | 0 | } |
156 | 0 | } else { |
157 | 0 | X509error(X509_R_METHOD_NOT_SUPPORTED); |
158 | 0 | goto error; |
159 | 0 | } |
160 | 0 | } else { |
161 | 0 | X509error(X509_R_UNSUPPORTED_ALGORITHM); |
162 | 0 | goto error; |
163 | 0 | } |
164 | | |
165 | 0 | if (*x != NULL) |
166 | 0 | X509_PUBKEY_free(*x); |
167 | |
|
168 | 0 | *x = pk; |
169 | |
|
170 | 0 | return 1; |
171 | | |
172 | 0 | error: |
173 | 0 | if (pk != NULL) |
174 | 0 | X509_PUBKEY_free(pk); |
175 | 0 | return 0; |
176 | 0 | } |
177 | | |
178 | | EVP_PKEY * |
179 | | X509_PUBKEY_get0(X509_PUBKEY *key) |
180 | 0 | { |
181 | 0 | EVP_PKEY *ret = NULL; |
182 | |
|
183 | 0 | if (key == NULL) |
184 | 0 | goto error; |
185 | | |
186 | 0 | if (key->pkey != NULL) |
187 | 0 | return key->pkey; |
188 | | |
189 | 0 | if (key->public_key == NULL) |
190 | 0 | goto error; |
191 | | |
192 | 0 | if ((ret = EVP_PKEY_new()) == NULL) { |
193 | 0 | X509error(ERR_R_MALLOC_FAILURE); |
194 | 0 | goto error; |
195 | 0 | } |
196 | | |
197 | 0 | if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) { |
198 | 0 | X509error(X509_R_UNSUPPORTED_ALGORITHM); |
199 | 0 | goto error; |
200 | 0 | } |
201 | | |
202 | 0 | if (ret->ameth->pub_decode) { |
203 | 0 | if (!ret->ameth->pub_decode(ret, key)) { |
204 | 0 | X509error(X509_R_PUBLIC_KEY_DECODE_ERROR); |
205 | 0 | goto error; |
206 | 0 | } |
207 | 0 | } else { |
208 | 0 | X509error(X509_R_METHOD_NOT_SUPPORTED); |
209 | 0 | goto error; |
210 | 0 | } |
211 | | |
212 | | /* Check to see if another thread set key->pkey first */ |
213 | 0 | CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY); |
214 | 0 | if (key->pkey) { |
215 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); |
216 | 0 | EVP_PKEY_free(ret); |
217 | 0 | ret = key->pkey; |
218 | 0 | } else { |
219 | 0 | key->pkey = ret; |
220 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); |
221 | 0 | } |
222 | |
|
223 | 0 | return ret; |
224 | | |
225 | 0 | error: |
226 | 0 | EVP_PKEY_free(ret); |
227 | 0 | return (NULL); |
228 | 0 | } |
229 | | |
230 | | EVP_PKEY * |
231 | | X509_PUBKEY_get(X509_PUBKEY *key) |
232 | 0 | { |
233 | 0 | EVP_PKEY *pkey; |
234 | |
|
235 | 0 | if ((pkey = X509_PUBKEY_get0(key)) == NULL) |
236 | 0 | return (NULL); |
237 | | |
238 | 0 | CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); |
239 | |
|
240 | 0 | return pkey; |
241 | 0 | } |
242 | | |
243 | | /* |
244 | | * Decode an X509_PUBKEY into the specified key type. |
245 | | */ |
246 | | static int |
247 | | pubkey_ex_d2i(int pkey_type, ASN1_VALUE **pval, const unsigned char **in, |
248 | | long len, const ASN1_ITEM *it) |
249 | 0 | { |
250 | 0 | const ASN1_EXTERN_FUNCS *ef = it->funcs; |
251 | 0 | const unsigned char *p = *in; |
252 | 0 | X509_PUBKEY *xpk = NULL; |
253 | 0 | ASN1_VALUE *key = NULL; |
254 | 0 | EVP_PKEY *pkey = NULL; |
255 | 0 | int ret = 0; |
256 | |
|
257 | 0 | if ((xpk = d2i_X509_PUBKEY(NULL, &p, len)) == NULL) |
258 | 0 | goto err; |
259 | 0 | if ((pkey = X509_PUBKEY_get(xpk)) == NULL) |
260 | 0 | goto err; |
261 | | |
262 | 0 | switch (pkey_type) { |
263 | 0 | case EVP_PKEY_NONE: |
264 | 0 | key = (ASN1_VALUE *)pkey; |
265 | 0 | pkey = NULL; |
266 | 0 | break; |
267 | | |
268 | 0 | case EVP_PKEY_DSA: |
269 | 0 | key = (ASN1_VALUE *)EVP_PKEY_get1_DSA(pkey); |
270 | 0 | break; |
271 | | |
272 | 0 | case EVP_PKEY_RSA: |
273 | 0 | key = (ASN1_VALUE *)EVP_PKEY_get1_RSA(pkey); |
274 | 0 | break; |
275 | | |
276 | 0 | case EVP_PKEY_EC: |
277 | 0 | key = (ASN1_VALUE *)EVP_PKEY_get1_EC_KEY(pkey); |
278 | 0 | break; |
279 | | |
280 | 0 | default: |
281 | 0 | goto err; |
282 | 0 | } |
283 | | |
284 | 0 | if (key == NULL) |
285 | 0 | goto err; |
286 | | |
287 | 0 | ef->asn1_ex_free(pval, it); |
288 | |
|
289 | 0 | *pval = key; |
290 | 0 | *in = p; |
291 | 0 | ret = 1; |
292 | |
|
293 | 0 | err: |
294 | 0 | EVP_PKEY_free(pkey); |
295 | 0 | X509_PUBKEY_free(xpk); |
296 | |
|
297 | 0 | return ret; |
298 | 0 | } |
299 | | |
300 | | /* |
301 | | * Encode the specified key type into an X509_PUBKEY. |
302 | | */ |
303 | | static int |
304 | | pubkey_ex_i2d(int pkey_type, ASN1_VALUE **pval, unsigned char **out, |
305 | | const ASN1_ITEM *it) |
306 | 0 | { |
307 | 0 | X509_PUBKEY *xpk = NULL; |
308 | 0 | EVP_PKEY *pkey, *pktmp; |
309 | 0 | int ret = -1; |
310 | |
|
311 | 0 | if ((pkey = pktmp = EVP_PKEY_new()) == NULL) |
312 | 0 | goto err; |
313 | | |
314 | 0 | switch (pkey_type) { |
315 | 0 | case EVP_PKEY_NONE: |
316 | 0 | pkey = (EVP_PKEY *)*pval; |
317 | 0 | break; |
318 | | |
319 | 0 | case EVP_PKEY_DSA: |
320 | 0 | if (!EVP_PKEY_set1_DSA(pkey, (DSA *)*pval)) |
321 | 0 | goto err; |
322 | 0 | break; |
323 | | |
324 | 0 | case EVP_PKEY_RSA: |
325 | 0 | if (!EVP_PKEY_set1_RSA(pkey, (RSA *)*pval)) |
326 | 0 | goto err; |
327 | 0 | break; |
328 | | |
329 | 0 | case EVP_PKEY_EC: |
330 | 0 | if (!EVP_PKEY_set1_EC_KEY(pkey, (EC_KEY*)*pval)) |
331 | 0 | goto err; |
332 | 0 | break; |
333 | | |
334 | 0 | default: |
335 | 0 | goto err; |
336 | 0 | } |
337 | | |
338 | 0 | if (!X509_PUBKEY_set(&xpk, pkey)) |
339 | 0 | goto err; |
340 | | |
341 | 0 | ret = i2d_X509_PUBKEY(xpk, out); |
342 | |
|
343 | 0 | err: |
344 | 0 | EVP_PKEY_free(pktmp); |
345 | 0 | X509_PUBKEY_free(xpk); |
346 | |
|
347 | 0 | return ret; |
348 | 0 | } |
349 | | |
350 | | static int |
351 | | pkey_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
352 | 0 | { |
353 | 0 | if ((*pval = (ASN1_VALUE *)EVP_PKEY_new()) == NULL) |
354 | 0 | return 0; |
355 | | |
356 | 0 | return 1; |
357 | 0 | } |
358 | | |
359 | | static void |
360 | | pkey_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
361 | 0 | { |
362 | 0 | EVP_PKEY_free((EVP_PKEY *)*pval); |
363 | 0 | *pval = NULL; |
364 | 0 | } |
365 | | |
366 | | static int |
367 | | pkey_pubkey_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
368 | | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
369 | 0 | { |
370 | 0 | return pubkey_ex_d2i(EVP_PKEY_NONE, pval, in, len, it); |
371 | 0 | } |
372 | | |
373 | | static int |
374 | | pkey_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, |
375 | | int tag, int aclass) |
376 | 0 | { |
377 | 0 | return pubkey_ex_i2d(EVP_PKEY_NONE, pval, out, it); |
378 | 0 | } |
379 | | |
380 | | const ASN1_EXTERN_FUNCS pkey_pubkey_asn1_ff = { |
381 | | .app_data = NULL, |
382 | | .asn1_ex_new = pkey_pubkey_ex_new, |
383 | | .asn1_ex_free = pkey_pubkey_ex_free, |
384 | | .asn1_ex_clear = NULL, |
385 | | .asn1_ex_d2i = pkey_pubkey_ex_d2i, |
386 | | .asn1_ex_i2d = pkey_pubkey_ex_i2d, |
387 | | .asn1_ex_print = NULL, |
388 | | }; |
389 | | |
390 | | const ASN1_ITEM EVP_PKEY_PUBKEY_it = { |
391 | | .itype = ASN1_ITYPE_EXTERN, |
392 | | .utype = 0, |
393 | | .templates = NULL, |
394 | | .tcount = 0, |
395 | | .funcs = &pkey_pubkey_asn1_ff, |
396 | | .size = 0, |
397 | | .sname = NULL, |
398 | | }; |
399 | | |
400 | | EVP_PKEY * |
401 | | d2i_PUBKEY(EVP_PKEY **pkey, const unsigned char **in, long len) |
402 | 0 | { |
403 | 0 | return (EVP_PKEY *)ASN1_item_d2i((ASN1_VALUE **)pkey, in, len, |
404 | 0 | &EVP_PKEY_PUBKEY_it); |
405 | 0 | } |
406 | | |
407 | | int |
408 | | i2d_PUBKEY(EVP_PKEY *pkey, unsigned char **out) |
409 | 0 | { |
410 | 0 | return ASN1_item_i2d((ASN1_VALUE *)pkey, out, &EVP_PKEY_PUBKEY_it); |
411 | 0 | } |
412 | | |
413 | | EVP_PKEY * |
414 | | d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **pkey) |
415 | 0 | { |
416 | 0 | return (EVP_PKEY *)ASN1_item_d2i_bio(&EVP_PKEY_PUBKEY_it, bp, |
417 | 0 | (ASN1_VALUE **)pkey); |
418 | 0 | } |
419 | | |
420 | | int |
421 | | i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey) |
422 | 0 | { |
423 | 0 | return ASN1_item_i2d_bio(&EVP_PKEY_PUBKEY_it, bp, (ASN1_VALUE *)pkey); |
424 | 0 | } |
425 | | |
426 | | EVP_PKEY * |
427 | | d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **pkey) |
428 | 0 | { |
429 | 0 | return (EVP_PKEY *)ASN1_item_d2i_fp(&EVP_PKEY_PUBKEY_it, fp, |
430 | 0 | (ASN1_VALUE **)pkey); |
431 | 0 | } |
432 | | |
433 | | int |
434 | | i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey) |
435 | 0 | { |
436 | 0 | return ASN1_item_i2d_fp(&EVP_PKEY_PUBKEY_it, fp, (ASN1_VALUE *)pkey); |
437 | 0 | } |
438 | | |
439 | | /* |
440 | | * The following are equivalents but which return RSA and DSA keys. |
441 | | */ |
442 | | #ifndef OPENSSL_NO_RSA |
443 | | |
444 | | static int |
445 | | rsa_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
446 | 0 | { |
447 | 0 | if ((*pval = (ASN1_VALUE *)RSA_new()) == NULL) |
448 | 0 | return 0; |
449 | | |
450 | 0 | return 1; |
451 | 0 | } |
452 | | |
453 | | static void |
454 | | rsa_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
455 | 0 | { |
456 | 0 | RSA_free((RSA *)*pval); |
457 | 0 | *pval = NULL; |
458 | 0 | } |
459 | | |
460 | | static int |
461 | | rsa_pubkey_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
462 | | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
463 | 0 | { |
464 | 0 | return pubkey_ex_d2i(EVP_PKEY_RSA, pval, in, len, it); |
465 | 0 | } |
466 | | |
467 | | static int |
468 | | rsa_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, |
469 | | int tag, int aclass) |
470 | 0 | { |
471 | 0 | return pubkey_ex_i2d(EVP_PKEY_RSA, pval, out, it); |
472 | 0 | } |
473 | | |
474 | | const ASN1_EXTERN_FUNCS rsa_pubkey_asn1_ff = { |
475 | | .app_data = NULL, |
476 | | .asn1_ex_new = rsa_pubkey_ex_new, |
477 | | .asn1_ex_free = rsa_pubkey_ex_free, |
478 | | .asn1_ex_clear = NULL, |
479 | | .asn1_ex_d2i = rsa_pubkey_ex_d2i, |
480 | | .asn1_ex_i2d = rsa_pubkey_ex_i2d, |
481 | | .asn1_ex_print = NULL, |
482 | | }; |
483 | | |
484 | | const ASN1_ITEM RSA_PUBKEY_it = { |
485 | | .itype = ASN1_ITYPE_EXTERN, |
486 | | .utype = 0, |
487 | | .templates = NULL, |
488 | | .tcount = 0, |
489 | | .funcs = &rsa_pubkey_asn1_ff, |
490 | | .size = 0, |
491 | | .sname = NULL, |
492 | | }; |
493 | | |
494 | | RSA * |
495 | | d2i_RSA_PUBKEY(RSA **rsa, const unsigned char **in, long len) |
496 | 0 | { |
497 | 0 | return (RSA *)ASN1_item_d2i((ASN1_VALUE **)rsa, in, len, |
498 | 0 | &RSA_PUBKEY_it); |
499 | 0 | } |
500 | | |
501 | | int |
502 | | i2d_RSA_PUBKEY(RSA *rsa, unsigned char **out) |
503 | 0 | { |
504 | 0 | return ASN1_item_i2d((ASN1_VALUE *)rsa, out, &RSA_PUBKEY_it); |
505 | 0 | } |
506 | | |
507 | | RSA * |
508 | | d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa) |
509 | 0 | { |
510 | 0 | return (RSA *)ASN1_item_d2i_bio(&RSA_PUBKEY_it, bp, (ASN1_VALUE **)rsa); |
511 | 0 | } |
512 | | |
513 | | int |
514 | | i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa) |
515 | 0 | { |
516 | 0 | return ASN1_item_i2d_bio(&RSA_PUBKEY_it, bp, (ASN1_VALUE *)rsa); |
517 | 0 | } |
518 | | |
519 | | RSA * |
520 | | d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa) |
521 | 0 | { |
522 | 0 | return (RSA *)ASN1_item_d2i_fp(&RSA_PUBKEY_it, fp, (ASN1_VALUE **)rsa); |
523 | 0 | } |
524 | | |
525 | | int |
526 | | i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa) |
527 | 0 | { |
528 | 0 | return ASN1_item_i2d_fp(&RSA_PUBKEY_it, fp, (ASN1_VALUE *)rsa); |
529 | 0 | } |
530 | | #endif |
531 | | |
532 | | #ifndef OPENSSL_NO_DSA |
533 | | |
534 | | static int |
535 | | dsa_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
536 | 0 | { |
537 | 0 | if ((*pval = (ASN1_VALUE *)DSA_new()) == NULL) |
538 | 0 | return 0; |
539 | | |
540 | 0 | return 1; |
541 | 0 | } |
542 | | |
543 | | static void |
544 | | dsa_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
545 | 0 | { |
546 | 0 | DSA_free((DSA *)*pval); |
547 | 0 | *pval = NULL; |
548 | 0 | } |
549 | | |
550 | | static int |
551 | | dsa_pubkey_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
552 | | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
553 | 0 | { |
554 | 0 | return pubkey_ex_d2i(EVP_PKEY_DSA, pval, in, len, it); |
555 | 0 | } |
556 | | |
557 | | static int |
558 | | dsa_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, |
559 | | int tag, int aclass) |
560 | 0 | { |
561 | 0 | return pubkey_ex_i2d(EVP_PKEY_DSA, pval, out, it); |
562 | 0 | } |
563 | | |
564 | | const ASN1_EXTERN_FUNCS dsa_pubkey_asn1_ff = { |
565 | | .app_data = NULL, |
566 | | .asn1_ex_new = dsa_pubkey_ex_new, |
567 | | .asn1_ex_free = dsa_pubkey_ex_free, |
568 | | .asn1_ex_clear = NULL, |
569 | | .asn1_ex_d2i = dsa_pubkey_ex_d2i, |
570 | | .asn1_ex_i2d = dsa_pubkey_ex_i2d, |
571 | | .asn1_ex_print = NULL, |
572 | | }; |
573 | | |
574 | | const ASN1_ITEM DSA_PUBKEY_it = { |
575 | | .itype = ASN1_ITYPE_EXTERN, |
576 | | .utype = 0, |
577 | | .templates = NULL, |
578 | | .tcount = 0, |
579 | | .funcs = &dsa_pubkey_asn1_ff, |
580 | | .size = 0, |
581 | | .sname = NULL, |
582 | | }; |
583 | | |
584 | | DSA * |
585 | | d2i_DSA_PUBKEY(DSA **dsa, const unsigned char **in, long len) |
586 | 0 | { |
587 | 0 | return (DSA *)ASN1_item_d2i((ASN1_VALUE **)dsa, in, len, |
588 | 0 | &DSA_PUBKEY_it); |
589 | 0 | } |
590 | | |
591 | | int |
592 | | i2d_DSA_PUBKEY(DSA *dsa, unsigned char **out) |
593 | 0 | { |
594 | 0 | return ASN1_item_i2d((ASN1_VALUE *)dsa, out, &DSA_PUBKEY_it); |
595 | 0 | } |
596 | | |
597 | | DSA * |
598 | | d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa) |
599 | 0 | { |
600 | 0 | return (DSA *)ASN1_item_d2i_bio(&DSA_PUBKEY_it, bp, (ASN1_VALUE **)dsa); |
601 | 0 | } |
602 | | |
603 | | int |
604 | | i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa) |
605 | 0 | { |
606 | 0 | return ASN1_item_i2d_bio(&DSA_PUBKEY_it, bp, (ASN1_VALUE *)dsa); |
607 | 0 | } |
608 | | |
609 | | DSA * |
610 | | d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa) |
611 | 0 | { |
612 | 0 | return (DSA *)ASN1_item_d2i_fp(&DSA_PUBKEY_it, fp, (ASN1_VALUE **)dsa); |
613 | 0 | } |
614 | | |
615 | | int |
616 | | i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa) |
617 | 0 | { |
618 | 0 | return ASN1_item_i2d_fp(&DSA_PUBKEY_it, fp, (ASN1_VALUE *)dsa); |
619 | 0 | } |
620 | | |
621 | | #endif |
622 | | |
623 | | #ifndef OPENSSL_NO_EC |
624 | | |
625 | | static int |
626 | | ec_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
627 | 0 | { |
628 | 0 | if ((*pval = (ASN1_VALUE *)EC_KEY_new()) == NULL) |
629 | 0 | return 0; |
630 | | |
631 | 0 | return 1; |
632 | 0 | } |
633 | | |
634 | | static void |
635 | | ec_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
636 | 0 | { |
637 | 0 | EC_KEY_free((EC_KEY *)*pval); |
638 | 0 | *pval = NULL; |
639 | 0 | } |
640 | | |
641 | | static int |
642 | | ec_pubkey_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
643 | | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
644 | 0 | { |
645 | 0 | return pubkey_ex_d2i(EVP_PKEY_EC, pval, in, len, it); |
646 | 0 | } |
647 | | |
648 | | static int |
649 | | ec_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, |
650 | | int tag, int aclass) |
651 | 0 | { |
652 | 0 | return pubkey_ex_i2d(EVP_PKEY_EC, pval, out, it); |
653 | 0 | } |
654 | | |
655 | | const ASN1_EXTERN_FUNCS ec_pubkey_asn1_ff = { |
656 | | .app_data = NULL, |
657 | | .asn1_ex_new = ec_pubkey_ex_new, |
658 | | .asn1_ex_free = ec_pubkey_ex_free, |
659 | | .asn1_ex_clear = NULL, |
660 | | .asn1_ex_d2i = ec_pubkey_ex_d2i, |
661 | | .asn1_ex_i2d = ec_pubkey_ex_i2d, |
662 | | .asn1_ex_print = NULL, |
663 | | }; |
664 | | |
665 | | const ASN1_ITEM EC_PUBKEY_it = { |
666 | | .itype = ASN1_ITYPE_EXTERN, |
667 | | .utype = 0, |
668 | | .templates = NULL, |
669 | | .tcount = 0, |
670 | | .funcs = &ec_pubkey_asn1_ff, |
671 | | .size = 0, |
672 | | .sname = NULL, |
673 | | }; |
674 | | |
675 | | EC_KEY * |
676 | | d2i_EC_PUBKEY(EC_KEY **ec, const unsigned char **in, long len) |
677 | 0 | { |
678 | 0 | return (EC_KEY *)ASN1_item_d2i((ASN1_VALUE **)ec, in, len, |
679 | 0 | &EC_PUBKEY_it); |
680 | 0 | } |
681 | | |
682 | | int |
683 | | i2d_EC_PUBKEY(EC_KEY *ec, unsigned char **out) |
684 | 0 | { |
685 | 0 | return ASN1_item_i2d((ASN1_VALUE *)ec, out, &EC_PUBKEY_it); |
686 | 0 | } |
687 | | |
688 | | EC_KEY * |
689 | | d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **ec) |
690 | 0 | { |
691 | 0 | return (EC_KEY *)ASN1_item_d2i_bio(&EC_PUBKEY_it, bp, (ASN1_VALUE **)ec); |
692 | 0 | } |
693 | | |
694 | | int |
695 | | i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *ec) |
696 | 0 | { |
697 | 0 | return ASN1_item_i2d_bio(&EC_PUBKEY_it, bp, (ASN1_VALUE *)ec); |
698 | 0 | } |
699 | | |
700 | | EC_KEY * |
701 | | d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **ec) |
702 | 0 | { |
703 | 0 | return (EC_KEY *)ASN1_item_d2i_fp(&EC_PUBKEY_it, fp, (ASN1_VALUE **)ec); |
704 | 0 | } |
705 | | |
706 | | int |
707 | | i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *ec) |
708 | 0 | { |
709 | 0 | return ASN1_item_i2d_fp(&EC_PUBKEY_it, fp, (ASN1_VALUE *)ec); |
710 | 0 | } |
711 | | #endif |
712 | | |
713 | | int |
714 | | X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, int ptype, |
715 | | void *pval, unsigned char *penc, int penclen) |
716 | 0 | { |
717 | 0 | if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval)) |
718 | 0 | return 0; |
719 | | |
720 | 0 | if (penc == NULL) |
721 | 0 | return 1; |
722 | | |
723 | 0 | ASN1_STRING_set0(pub->public_key, penc, penclen); |
724 | |
|
725 | 0 | return asn1_abs_set_unused_bits(pub->public_key, 0); |
726 | 0 | } |
727 | | |
728 | | int |
729 | | X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, const unsigned char **pk, |
730 | | int *ppklen, X509_ALGOR **pa, X509_PUBKEY *pub) |
731 | 0 | { |
732 | 0 | if (ppkalg) |
733 | 0 | *ppkalg = pub->algor->algorithm; |
734 | 0 | if (pk) { |
735 | 0 | *pk = pub->public_key->data; |
736 | 0 | *ppklen = pub->public_key->length; |
737 | 0 | } |
738 | 0 | if (pa) |
739 | 0 | *pa = pub->algor; |
740 | 0 | return 1; |
741 | 0 | } |