/src/libressl/crypto/asn1/x_pubkey.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: x_pubkey.c,v 1.37 2024/07/08 14:48:49 beck 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_local.h" |
75 | | #include "evp_local.h" |
76 | | #include "x509_local.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 | 500k | { |
82 | 500k | if (operation == ASN1_OP_FREE_POST) { |
83 | 114k | X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; |
84 | 114k | EVP_PKEY_free(pubkey->pkey); |
85 | 114k | } |
86 | 500k | return 1; |
87 | 500k | } |
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 | | LCRYPTO_ALIAS(X509_PUBKEY_it); |
115 | | |
116 | | X509_PUBKEY * |
117 | | d2i_X509_PUBKEY(X509_PUBKEY **a, const unsigned char **in, long len) |
118 | 0 | { |
119 | 0 | return (X509_PUBKEY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, |
120 | 0 | &X509_PUBKEY_it); |
121 | 0 | } |
122 | | LCRYPTO_ALIAS(d2i_X509_PUBKEY); |
123 | | |
124 | | int |
125 | | i2d_X509_PUBKEY(X509_PUBKEY *a, unsigned char **out) |
126 | 0 | { |
127 | 0 | return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_PUBKEY_it); |
128 | 0 | } |
129 | | LCRYPTO_ALIAS(i2d_X509_PUBKEY); |
130 | | |
131 | | X509_PUBKEY * |
132 | | X509_PUBKEY_new(void) |
133 | 0 | { |
134 | 0 | return (X509_PUBKEY *)ASN1_item_new(&X509_PUBKEY_it); |
135 | 0 | } |
136 | | LCRYPTO_ALIAS(X509_PUBKEY_new); |
137 | | |
138 | | void |
139 | | X509_PUBKEY_free(X509_PUBKEY *a) |
140 | 0 | { |
141 | 0 | ASN1_item_free((ASN1_VALUE *)a, &X509_PUBKEY_it); |
142 | 0 | } |
143 | | LCRYPTO_ALIAS(X509_PUBKEY_free); |
144 | | |
145 | | int |
146 | | X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) |
147 | 0 | { |
148 | 0 | X509_PUBKEY *pk = NULL; |
149 | |
|
150 | 0 | if (x == NULL) |
151 | 0 | return (0); |
152 | 0 | if ((pk = X509_PUBKEY_new()) == NULL) |
153 | 0 | goto error; |
154 | | |
155 | 0 | if (pkey->ameth) { |
156 | 0 | if (pkey->ameth->pub_encode) { |
157 | 0 | if (!pkey->ameth->pub_encode(pk, pkey)) { |
158 | 0 | X509error(X509_R_PUBLIC_KEY_ENCODE_ERROR); |
159 | 0 | goto error; |
160 | 0 | } |
161 | 0 | } else { |
162 | 0 | X509error(X509_R_METHOD_NOT_SUPPORTED); |
163 | 0 | goto error; |
164 | 0 | } |
165 | 0 | } else { |
166 | 0 | X509error(X509_R_UNSUPPORTED_ALGORITHM); |
167 | 0 | goto error; |
168 | 0 | } |
169 | | |
170 | 0 | if (*x != NULL) |
171 | 0 | X509_PUBKEY_free(*x); |
172 | |
|
173 | 0 | *x = pk; |
174 | |
|
175 | 0 | return 1; |
176 | | |
177 | 0 | error: |
178 | 0 | if (pk != NULL) |
179 | 0 | X509_PUBKEY_free(pk); |
180 | 0 | return 0; |
181 | 0 | } |
182 | | LCRYPTO_ALIAS(X509_PUBKEY_set); |
183 | | |
184 | | EVP_PKEY * |
185 | | X509_PUBKEY_get0(X509_PUBKEY *key) |
186 | 40.5k | { |
187 | 40.5k | EVP_PKEY *ret = NULL; |
188 | | |
189 | 40.5k | if (key == NULL) |
190 | 0 | goto error; |
191 | | |
192 | 40.5k | if (key->pkey != NULL) |
193 | 24.5k | return key->pkey; |
194 | | |
195 | 16.0k | if (key->public_key == NULL) |
196 | 0 | goto error; |
197 | | |
198 | 16.0k | if ((ret = EVP_PKEY_new()) == NULL) { |
199 | 0 | X509error(ERR_R_MALLOC_FAILURE); |
200 | 0 | goto error; |
201 | 0 | } |
202 | | |
203 | 16.0k | if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) { |
204 | 4.32k | X509error(X509_R_UNSUPPORTED_ALGORITHM); |
205 | 4.32k | goto error; |
206 | 4.32k | } |
207 | | |
208 | 11.7k | if (ret->ameth->pub_decode) { |
209 | 11.7k | if (!ret->ameth->pub_decode(ret, key)) { |
210 | 2.41k | X509error(X509_R_PUBLIC_KEY_DECODE_ERROR); |
211 | 2.41k | goto error; |
212 | 2.41k | } |
213 | 11.7k | } else { |
214 | 0 | X509error(X509_R_METHOD_NOT_SUPPORTED); |
215 | 0 | goto error; |
216 | 0 | } |
217 | | |
218 | | /* Check to see if another thread set key->pkey first */ |
219 | 9.32k | CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY); |
220 | 9.32k | if (key->pkey) { |
221 | 0 | CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); |
222 | 0 | EVP_PKEY_free(ret); |
223 | 0 | ret = key->pkey; |
224 | 9.32k | } else { |
225 | 9.32k | key->pkey = ret; |
226 | 9.32k | CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY); |
227 | 9.32k | } |
228 | | |
229 | 9.32k | return ret; |
230 | | |
231 | 6.73k | error: |
232 | 6.73k | EVP_PKEY_free(ret); |
233 | 6.73k | return (NULL); |
234 | 11.7k | } |
235 | | LCRYPTO_ALIAS(X509_PUBKEY_get0); |
236 | | |
237 | | EVP_PKEY * |
238 | | X509_PUBKEY_get(X509_PUBKEY *key) |
239 | 13.8k | { |
240 | 13.8k | EVP_PKEY *pkey; |
241 | | |
242 | 13.8k | if ((pkey = X509_PUBKEY_get0(key)) == NULL) |
243 | 5.53k | return (NULL); |
244 | | |
245 | 8.34k | CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); |
246 | | |
247 | 8.34k | return pkey; |
248 | 13.8k | } |
249 | | LCRYPTO_ALIAS(X509_PUBKEY_get); |
250 | | |
251 | | /* |
252 | | * Decode an X509_PUBKEY into the specified key type. |
253 | | */ |
254 | | static int |
255 | | pubkey_ex_d2i(int pkey_type, ASN1_VALUE **pval, const unsigned char **in, |
256 | | long len, const ASN1_ITEM *it) |
257 | 0 | { |
258 | 0 | const ASN1_EXTERN_FUNCS *ef = it->funcs; |
259 | 0 | const unsigned char *p = *in; |
260 | 0 | X509_PUBKEY *xpk = NULL; |
261 | 0 | ASN1_VALUE *key = NULL; |
262 | 0 | EVP_PKEY *pkey = NULL; |
263 | 0 | int ret = 0; |
264 | |
|
265 | 0 | if ((xpk = d2i_X509_PUBKEY(NULL, &p, len)) == NULL) |
266 | 0 | goto err; |
267 | 0 | if ((pkey = X509_PUBKEY_get(xpk)) == NULL) |
268 | 0 | goto err; |
269 | | |
270 | 0 | switch (pkey_type) { |
271 | 0 | case EVP_PKEY_NONE: |
272 | 0 | key = (ASN1_VALUE *)pkey; |
273 | 0 | pkey = NULL; |
274 | 0 | break; |
275 | | |
276 | 0 | case EVP_PKEY_DSA: |
277 | 0 | key = (ASN1_VALUE *)EVP_PKEY_get1_DSA(pkey); |
278 | 0 | break; |
279 | | |
280 | 0 | case EVP_PKEY_RSA: |
281 | 0 | key = (ASN1_VALUE *)EVP_PKEY_get1_RSA(pkey); |
282 | 0 | break; |
283 | | |
284 | 0 | case EVP_PKEY_EC: |
285 | 0 | key = (ASN1_VALUE *)EVP_PKEY_get1_EC_KEY(pkey); |
286 | 0 | break; |
287 | | |
288 | 0 | default: |
289 | 0 | goto err; |
290 | 0 | } |
291 | | |
292 | 0 | if (key == NULL) |
293 | 0 | goto err; |
294 | | |
295 | 0 | ef->asn1_ex_free(pval, it); |
296 | |
|
297 | 0 | *pval = key; |
298 | 0 | *in = p; |
299 | 0 | ret = 1; |
300 | |
|
301 | 0 | err: |
302 | 0 | EVP_PKEY_free(pkey); |
303 | 0 | X509_PUBKEY_free(xpk); |
304 | |
|
305 | 0 | return ret; |
306 | 0 | } |
307 | | |
308 | | /* |
309 | | * Encode the specified key type into an X509_PUBKEY. |
310 | | */ |
311 | | static int |
312 | | pubkey_ex_i2d(int pkey_type, ASN1_VALUE **pval, unsigned char **out, |
313 | | const ASN1_ITEM *it) |
314 | 0 | { |
315 | 0 | X509_PUBKEY *xpk = NULL; |
316 | 0 | EVP_PKEY *pkey, *pktmp; |
317 | 0 | int ret = -1; |
318 | |
|
319 | 0 | if ((pkey = pktmp = EVP_PKEY_new()) == NULL) |
320 | 0 | goto err; |
321 | | |
322 | 0 | switch (pkey_type) { |
323 | 0 | case EVP_PKEY_NONE: |
324 | 0 | pkey = (EVP_PKEY *)*pval; |
325 | 0 | break; |
326 | | |
327 | 0 | case EVP_PKEY_DSA: |
328 | 0 | if (!EVP_PKEY_set1_DSA(pkey, (DSA *)*pval)) |
329 | 0 | goto err; |
330 | 0 | break; |
331 | | |
332 | 0 | case EVP_PKEY_RSA: |
333 | 0 | if (!EVP_PKEY_set1_RSA(pkey, (RSA *)*pval)) |
334 | 0 | goto err; |
335 | 0 | break; |
336 | | |
337 | 0 | case EVP_PKEY_EC: |
338 | 0 | if (!EVP_PKEY_set1_EC_KEY(pkey, (EC_KEY*)*pval)) |
339 | 0 | goto err; |
340 | 0 | break; |
341 | | |
342 | 0 | default: |
343 | 0 | goto err; |
344 | 0 | } |
345 | | |
346 | 0 | if (!X509_PUBKEY_set(&xpk, pkey)) |
347 | 0 | goto err; |
348 | | |
349 | 0 | ret = i2d_X509_PUBKEY(xpk, out); |
350 | |
|
351 | 0 | err: |
352 | 0 | EVP_PKEY_free(pktmp); |
353 | 0 | X509_PUBKEY_free(xpk); |
354 | |
|
355 | 0 | return ret; |
356 | 0 | } |
357 | | |
358 | | static int |
359 | | pkey_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
360 | 0 | { |
361 | 0 | if ((*pval = (ASN1_VALUE *)EVP_PKEY_new()) == NULL) |
362 | 0 | return 0; |
363 | | |
364 | 0 | return 1; |
365 | 0 | } |
366 | | |
367 | | static void |
368 | | pkey_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
369 | 0 | { |
370 | 0 | EVP_PKEY_free((EVP_PKEY *)*pval); |
371 | 0 | *pval = NULL; |
372 | 0 | } |
373 | | |
374 | | static int |
375 | | pkey_pubkey_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
376 | | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
377 | 0 | { |
378 | 0 | return pubkey_ex_d2i(EVP_PKEY_NONE, pval, in, len, it); |
379 | 0 | } |
380 | | |
381 | | static int |
382 | | pkey_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, |
383 | | int tag, int aclass) |
384 | 0 | { |
385 | 0 | return pubkey_ex_i2d(EVP_PKEY_NONE, pval, out, it); |
386 | 0 | } |
387 | | |
388 | | const ASN1_EXTERN_FUNCS pkey_pubkey_asn1_ff = { |
389 | | .app_data = NULL, |
390 | | .asn1_ex_new = pkey_pubkey_ex_new, |
391 | | .asn1_ex_free = pkey_pubkey_ex_free, |
392 | | .asn1_ex_clear = NULL, |
393 | | .asn1_ex_d2i = pkey_pubkey_ex_d2i, |
394 | | .asn1_ex_i2d = pkey_pubkey_ex_i2d, |
395 | | .asn1_ex_print = NULL, |
396 | | }; |
397 | | |
398 | | const ASN1_ITEM EVP_PKEY_PUBKEY_it = { |
399 | | .itype = ASN1_ITYPE_EXTERN, |
400 | | .utype = 0, |
401 | | .templates = NULL, |
402 | | .tcount = 0, |
403 | | .funcs = &pkey_pubkey_asn1_ff, |
404 | | .size = 0, |
405 | | .sname = NULL, |
406 | | }; |
407 | | |
408 | | EVP_PKEY * |
409 | | d2i_PUBKEY(EVP_PKEY **pkey, const unsigned char **in, long len) |
410 | 0 | { |
411 | 0 | return (EVP_PKEY *)ASN1_item_d2i((ASN1_VALUE **)pkey, in, len, |
412 | 0 | &EVP_PKEY_PUBKEY_it); |
413 | 0 | } |
414 | | LCRYPTO_ALIAS(d2i_PUBKEY); |
415 | | |
416 | | int |
417 | | i2d_PUBKEY(EVP_PKEY *pkey, unsigned char **out) |
418 | 0 | { |
419 | 0 | return ASN1_item_i2d((ASN1_VALUE *)pkey, out, &EVP_PKEY_PUBKEY_it); |
420 | 0 | } |
421 | | LCRYPTO_ALIAS(i2d_PUBKEY); |
422 | | |
423 | | EVP_PKEY * |
424 | | d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **pkey) |
425 | 0 | { |
426 | 0 | return (EVP_PKEY *)ASN1_item_d2i_bio(&EVP_PKEY_PUBKEY_it, bp, |
427 | 0 | (ASN1_VALUE **)pkey); |
428 | 0 | } |
429 | | LCRYPTO_ALIAS(d2i_PUBKEY_bio); |
430 | | |
431 | | int |
432 | | i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey) |
433 | 0 | { |
434 | 0 | return ASN1_item_i2d_bio(&EVP_PKEY_PUBKEY_it, bp, (ASN1_VALUE *)pkey); |
435 | 0 | } |
436 | | LCRYPTO_ALIAS(i2d_PUBKEY_bio); |
437 | | |
438 | | EVP_PKEY * |
439 | | d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **pkey) |
440 | 0 | { |
441 | 0 | return (EVP_PKEY *)ASN1_item_d2i_fp(&EVP_PKEY_PUBKEY_it, fp, |
442 | 0 | (ASN1_VALUE **)pkey); |
443 | 0 | } |
444 | | LCRYPTO_ALIAS(d2i_PUBKEY_fp); |
445 | | |
446 | | int |
447 | | i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey) |
448 | 0 | { |
449 | 0 | return ASN1_item_i2d_fp(&EVP_PKEY_PUBKEY_it, fp, (ASN1_VALUE *)pkey); |
450 | 0 | } |
451 | | LCRYPTO_ALIAS(i2d_PUBKEY_fp); |
452 | | |
453 | | /* |
454 | | * The following are equivalents but which return RSA and DSA keys. |
455 | | */ |
456 | | #ifndef OPENSSL_NO_RSA |
457 | | |
458 | | static int |
459 | | rsa_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
460 | 0 | { |
461 | 0 | if ((*pval = (ASN1_VALUE *)RSA_new()) == NULL) |
462 | 0 | return 0; |
463 | | |
464 | 0 | return 1; |
465 | 0 | } |
466 | | |
467 | | static void |
468 | | rsa_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
469 | 0 | { |
470 | 0 | RSA_free((RSA *)*pval); |
471 | 0 | *pval = NULL; |
472 | 0 | } |
473 | | |
474 | | static int |
475 | | rsa_pubkey_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
476 | | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
477 | 0 | { |
478 | 0 | return pubkey_ex_d2i(EVP_PKEY_RSA, pval, in, len, it); |
479 | 0 | } |
480 | | |
481 | | static int |
482 | | rsa_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, |
483 | | int tag, int aclass) |
484 | 0 | { |
485 | 0 | return pubkey_ex_i2d(EVP_PKEY_RSA, pval, out, it); |
486 | 0 | } |
487 | | |
488 | | const ASN1_EXTERN_FUNCS rsa_pubkey_asn1_ff = { |
489 | | .app_data = NULL, |
490 | | .asn1_ex_new = rsa_pubkey_ex_new, |
491 | | .asn1_ex_free = rsa_pubkey_ex_free, |
492 | | .asn1_ex_clear = NULL, |
493 | | .asn1_ex_d2i = rsa_pubkey_ex_d2i, |
494 | | .asn1_ex_i2d = rsa_pubkey_ex_i2d, |
495 | | .asn1_ex_print = NULL, |
496 | | }; |
497 | | |
498 | | const ASN1_ITEM RSA_PUBKEY_it = { |
499 | | .itype = ASN1_ITYPE_EXTERN, |
500 | | .utype = 0, |
501 | | .templates = NULL, |
502 | | .tcount = 0, |
503 | | .funcs = &rsa_pubkey_asn1_ff, |
504 | | .size = 0, |
505 | | .sname = NULL, |
506 | | }; |
507 | | |
508 | | RSA * |
509 | | d2i_RSA_PUBKEY(RSA **rsa, const unsigned char **in, long len) |
510 | 0 | { |
511 | 0 | return (RSA *)ASN1_item_d2i((ASN1_VALUE **)rsa, in, len, |
512 | 0 | &RSA_PUBKEY_it); |
513 | 0 | } |
514 | | LCRYPTO_ALIAS(d2i_RSA_PUBKEY); |
515 | | |
516 | | int |
517 | | i2d_RSA_PUBKEY(RSA *rsa, unsigned char **out) |
518 | 0 | { |
519 | 0 | return ASN1_item_i2d((ASN1_VALUE *)rsa, out, &RSA_PUBKEY_it); |
520 | 0 | } |
521 | | LCRYPTO_ALIAS(i2d_RSA_PUBKEY); |
522 | | |
523 | | RSA * |
524 | | d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa) |
525 | 0 | { |
526 | 0 | return (RSA *)ASN1_item_d2i_bio(&RSA_PUBKEY_it, bp, (ASN1_VALUE **)rsa); |
527 | 0 | } |
528 | | LCRYPTO_ALIAS(d2i_RSA_PUBKEY_bio); |
529 | | |
530 | | int |
531 | | i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa) |
532 | 0 | { |
533 | 0 | return ASN1_item_i2d_bio(&RSA_PUBKEY_it, bp, (ASN1_VALUE *)rsa); |
534 | 0 | } |
535 | | LCRYPTO_ALIAS(i2d_RSA_PUBKEY_bio); |
536 | | |
537 | | RSA * |
538 | | d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa) |
539 | 0 | { |
540 | 0 | return (RSA *)ASN1_item_d2i_fp(&RSA_PUBKEY_it, fp, (ASN1_VALUE **)rsa); |
541 | 0 | } |
542 | | LCRYPTO_ALIAS(d2i_RSA_PUBKEY_fp); |
543 | | |
544 | | int |
545 | | i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa) |
546 | 0 | { |
547 | 0 | return ASN1_item_i2d_fp(&RSA_PUBKEY_it, fp, (ASN1_VALUE *)rsa); |
548 | 0 | } |
549 | | LCRYPTO_ALIAS(i2d_RSA_PUBKEY_fp); |
550 | | #endif |
551 | | |
552 | | #ifndef OPENSSL_NO_DSA |
553 | | |
554 | | static int |
555 | | dsa_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
556 | 0 | { |
557 | 0 | if ((*pval = (ASN1_VALUE *)DSA_new()) == NULL) |
558 | 0 | return 0; |
559 | | |
560 | 0 | return 1; |
561 | 0 | } |
562 | | |
563 | | static void |
564 | | dsa_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
565 | 0 | { |
566 | 0 | DSA_free((DSA *)*pval); |
567 | 0 | *pval = NULL; |
568 | 0 | } |
569 | | |
570 | | static int |
571 | | dsa_pubkey_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
572 | | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
573 | 0 | { |
574 | 0 | return pubkey_ex_d2i(EVP_PKEY_DSA, pval, in, len, it); |
575 | 0 | } |
576 | | |
577 | | static int |
578 | | dsa_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, |
579 | | int tag, int aclass) |
580 | 0 | { |
581 | 0 | return pubkey_ex_i2d(EVP_PKEY_DSA, pval, out, it); |
582 | 0 | } |
583 | | |
584 | | const ASN1_EXTERN_FUNCS dsa_pubkey_asn1_ff = { |
585 | | .app_data = NULL, |
586 | | .asn1_ex_new = dsa_pubkey_ex_new, |
587 | | .asn1_ex_free = dsa_pubkey_ex_free, |
588 | | .asn1_ex_clear = NULL, |
589 | | .asn1_ex_d2i = dsa_pubkey_ex_d2i, |
590 | | .asn1_ex_i2d = dsa_pubkey_ex_i2d, |
591 | | .asn1_ex_print = NULL, |
592 | | }; |
593 | | |
594 | | const ASN1_ITEM DSA_PUBKEY_it = { |
595 | | .itype = ASN1_ITYPE_EXTERN, |
596 | | .utype = 0, |
597 | | .templates = NULL, |
598 | | .tcount = 0, |
599 | | .funcs = &dsa_pubkey_asn1_ff, |
600 | | .size = 0, |
601 | | .sname = NULL, |
602 | | }; |
603 | | |
604 | | DSA * |
605 | | d2i_DSA_PUBKEY(DSA **dsa, const unsigned char **in, long len) |
606 | 0 | { |
607 | 0 | return (DSA *)ASN1_item_d2i((ASN1_VALUE **)dsa, in, len, |
608 | 0 | &DSA_PUBKEY_it); |
609 | 0 | } |
610 | | LCRYPTO_ALIAS(d2i_DSA_PUBKEY); |
611 | | |
612 | | int |
613 | | i2d_DSA_PUBKEY(DSA *dsa, unsigned char **out) |
614 | 0 | { |
615 | 0 | return ASN1_item_i2d((ASN1_VALUE *)dsa, out, &DSA_PUBKEY_it); |
616 | 0 | } |
617 | | LCRYPTO_ALIAS(i2d_DSA_PUBKEY); |
618 | | |
619 | | DSA * |
620 | | d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa) |
621 | 0 | { |
622 | 0 | return (DSA *)ASN1_item_d2i_bio(&DSA_PUBKEY_it, bp, (ASN1_VALUE **)dsa); |
623 | 0 | } |
624 | | LCRYPTO_ALIAS(d2i_DSA_PUBKEY_bio); |
625 | | |
626 | | int |
627 | | i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa) |
628 | 0 | { |
629 | 0 | return ASN1_item_i2d_bio(&DSA_PUBKEY_it, bp, (ASN1_VALUE *)dsa); |
630 | 0 | } |
631 | | LCRYPTO_ALIAS(i2d_DSA_PUBKEY_bio); |
632 | | |
633 | | DSA * |
634 | | d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa) |
635 | 0 | { |
636 | 0 | return (DSA *)ASN1_item_d2i_fp(&DSA_PUBKEY_it, fp, (ASN1_VALUE **)dsa); |
637 | 0 | } |
638 | | LCRYPTO_ALIAS(d2i_DSA_PUBKEY_fp); |
639 | | |
640 | | int |
641 | | i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa) |
642 | 0 | { |
643 | 0 | return ASN1_item_i2d_fp(&DSA_PUBKEY_it, fp, (ASN1_VALUE *)dsa); |
644 | 0 | } |
645 | | LCRYPTO_ALIAS(i2d_DSA_PUBKEY_fp); |
646 | | |
647 | | #endif |
648 | | |
649 | | #ifndef OPENSSL_NO_EC |
650 | | |
651 | | static int |
652 | | ec_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) |
653 | 0 | { |
654 | 0 | if ((*pval = (ASN1_VALUE *)EC_KEY_new()) == NULL) |
655 | 0 | return 0; |
656 | | |
657 | 0 | return 1; |
658 | 0 | } |
659 | | |
660 | | static void |
661 | | ec_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) |
662 | 0 | { |
663 | 0 | EC_KEY_free((EC_KEY *)*pval); |
664 | 0 | *pval = NULL; |
665 | 0 | } |
666 | | |
667 | | static int |
668 | | ec_pubkey_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, |
669 | | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
670 | 0 | { |
671 | 0 | return pubkey_ex_d2i(EVP_PKEY_EC, pval, in, len, it); |
672 | 0 | } |
673 | | |
674 | | static int |
675 | | ec_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, |
676 | | int tag, int aclass) |
677 | 0 | { |
678 | 0 | return pubkey_ex_i2d(EVP_PKEY_EC, pval, out, it); |
679 | 0 | } |
680 | | |
681 | | const ASN1_EXTERN_FUNCS ec_pubkey_asn1_ff = { |
682 | | .app_data = NULL, |
683 | | .asn1_ex_new = ec_pubkey_ex_new, |
684 | | .asn1_ex_free = ec_pubkey_ex_free, |
685 | | .asn1_ex_clear = NULL, |
686 | | .asn1_ex_d2i = ec_pubkey_ex_d2i, |
687 | | .asn1_ex_i2d = ec_pubkey_ex_i2d, |
688 | | .asn1_ex_print = NULL, |
689 | | }; |
690 | | |
691 | | const ASN1_ITEM EC_PUBKEY_it = { |
692 | | .itype = ASN1_ITYPE_EXTERN, |
693 | | .utype = 0, |
694 | | .templates = NULL, |
695 | | .tcount = 0, |
696 | | .funcs = &ec_pubkey_asn1_ff, |
697 | | .size = 0, |
698 | | .sname = NULL, |
699 | | }; |
700 | | |
701 | | EC_KEY * |
702 | | d2i_EC_PUBKEY(EC_KEY **ec, const unsigned char **in, long len) |
703 | 0 | { |
704 | 0 | return (EC_KEY *)ASN1_item_d2i((ASN1_VALUE **)ec, in, len, |
705 | 0 | &EC_PUBKEY_it); |
706 | 0 | } |
707 | | LCRYPTO_ALIAS(d2i_EC_PUBKEY); |
708 | | |
709 | | int |
710 | | i2d_EC_PUBKEY(EC_KEY *ec, unsigned char **out) |
711 | 0 | { |
712 | 0 | return ASN1_item_i2d((ASN1_VALUE *)ec, out, &EC_PUBKEY_it); |
713 | 0 | } |
714 | | LCRYPTO_ALIAS(i2d_EC_PUBKEY); |
715 | | |
716 | | EC_KEY * |
717 | | d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **ec) |
718 | 0 | { |
719 | 0 | return (EC_KEY *)ASN1_item_d2i_bio(&EC_PUBKEY_it, bp, (ASN1_VALUE **)ec); |
720 | 0 | } |
721 | | LCRYPTO_ALIAS(d2i_EC_PUBKEY_bio); |
722 | | |
723 | | int |
724 | | i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *ec) |
725 | 0 | { |
726 | 0 | return ASN1_item_i2d_bio(&EC_PUBKEY_it, bp, (ASN1_VALUE *)ec); |
727 | 0 | } |
728 | | LCRYPTO_ALIAS(i2d_EC_PUBKEY_bio); |
729 | | |
730 | | EC_KEY * |
731 | | d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **ec) |
732 | 0 | { |
733 | 0 | return (EC_KEY *)ASN1_item_d2i_fp(&EC_PUBKEY_it, fp, (ASN1_VALUE **)ec); |
734 | 0 | } |
735 | | LCRYPTO_ALIAS(d2i_EC_PUBKEY_fp); |
736 | | |
737 | | int |
738 | | i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *ec) |
739 | 0 | { |
740 | 0 | return ASN1_item_i2d_fp(&EC_PUBKEY_it, fp, (ASN1_VALUE *)ec); |
741 | 0 | } |
742 | | LCRYPTO_ALIAS(i2d_EC_PUBKEY_fp); |
743 | | #endif |
744 | | |
745 | | int |
746 | | X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, int ptype, |
747 | | void *pval, unsigned char *penc, int penclen) |
748 | 0 | { |
749 | 0 | if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval)) |
750 | 0 | return 0; |
751 | | |
752 | 0 | if (penc == NULL) |
753 | 0 | return 1; |
754 | | |
755 | 0 | ASN1_STRING_set0(pub->public_key, penc, penclen); |
756 | |
|
757 | 0 | return asn1_abs_set_unused_bits(pub->public_key, 0); |
758 | 0 | } |
759 | | LCRYPTO_ALIAS(X509_PUBKEY_set0_param); |
760 | | |
761 | | int |
762 | | X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, const unsigned char **pk, |
763 | | int *ppklen, X509_ALGOR **pa, X509_PUBKEY *pub) |
764 | 11.7k | { |
765 | 11.7k | if (ppkalg) |
766 | 0 | *ppkalg = pub->algor->algorithm; |
767 | 11.7k | if (pk) { |
768 | 11.7k | *pk = pub->public_key->data; |
769 | 11.7k | *ppklen = pub->public_key->length; |
770 | 11.7k | } |
771 | 11.7k | if (pa) |
772 | 11.7k | *pa = pub->algor; |
773 | 11.7k | return 1; |
774 | 11.7k | } |
775 | | LCRYPTO_ALIAS(X509_PUBKEY_get0_param); |