xref: /dragonfly/crypto/libressl/crypto/asn1/x_pubkey.c (revision 961e30ea7dc61d1112b778ea4981eac68129fb86)
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
pubkey_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)80 pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
81 {
82           if (operation == ASN1_OP_FREE_POST) {
83                     X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
84                     EVP_PKEY_free(pubkey->pkey);
85           }
86           return 1;
87 }
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 *
d2i_X509_PUBKEY(X509_PUBKEY ** a,const unsigned char ** in,long len)116 d2i_X509_PUBKEY(X509_PUBKEY **a, const unsigned char **in, long len)
117 {
118           return (X509_PUBKEY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
119               &X509_PUBKEY_it);
120 }
121 
122 int
i2d_X509_PUBKEY(X509_PUBKEY * a,unsigned char ** out)123 i2d_X509_PUBKEY(X509_PUBKEY *a, unsigned char **out)
124 {
125           return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_PUBKEY_it);
126 }
127 
128 X509_PUBKEY *
X509_PUBKEY_new(void)129 X509_PUBKEY_new(void)
130 {
131           return (X509_PUBKEY *)ASN1_item_new(&X509_PUBKEY_it);
132 }
133 
134 void
X509_PUBKEY_free(X509_PUBKEY * a)135 X509_PUBKEY_free(X509_PUBKEY *a)
136 {
137           ASN1_item_free((ASN1_VALUE *)a, &X509_PUBKEY_it);
138 }
139 
140 int
X509_PUBKEY_set(X509_PUBKEY ** x,EVP_PKEY * pkey)141 X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
142 {
143           X509_PUBKEY *pk = NULL;
144 
145           if (x == NULL)
146                     return (0);
147           if ((pk = X509_PUBKEY_new()) == NULL)
148                     goto error;
149 
150           if (pkey->ameth) {
151                     if (pkey->ameth->pub_encode) {
152                               if (!pkey->ameth->pub_encode(pk, pkey)) {
153                                         X509error(X509_R_PUBLIC_KEY_ENCODE_ERROR);
154                                         goto error;
155                               }
156                     } else {
157                               X509error(X509_R_METHOD_NOT_SUPPORTED);
158                               goto error;
159                     }
160           } else {
161                     X509error(X509_R_UNSUPPORTED_ALGORITHM);
162                     goto error;
163           }
164 
165           if (*x != NULL)
166                     X509_PUBKEY_free(*x);
167 
168           *x = pk;
169 
170           return 1;
171 
172  error:
173           if (pk != NULL)
174                     X509_PUBKEY_free(pk);
175           return 0;
176 }
177 
178 EVP_PKEY *
X509_PUBKEY_get0(X509_PUBKEY * key)179 X509_PUBKEY_get0(X509_PUBKEY *key)
180 {
181           EVP_PKEY *ret = NULL;
182 
183           if (key == NULL)
184                     goto error;
185 
186           if (key->pkey != NULL)
187                     return key->pkey;
188 
189           if (key->public_key == NULL)
190                     goto error;
191 
192           if ((ret = EVP_PKEY_new()) == NULL) {
193                     X509error(ERR_R_MALLOC_FAILURE);
194                     goto error;
195           }
196 
197           if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) {
198                     X509error(X509_R_UNSUPPORTED_ALGORITHM);
199                     goto error;
200           }
201 
202           if (ret->ameth->pub_decode) {
203                     if (!ret->ameth->pub_decode(ret, key)) {
204                               X509error(X509_R_PUBLIC_KEY_DECODE_ERROR);
205                               goto error;
206                     }
207           } else {
208                     X509error(X509_R_METHOD_NOT_SUPPORTED);
209                     goto error;
210           }
211 
212           /* Check to see if another thread set key->pkey first */
213           CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
214           if (key->pkey) {
215                     CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
216                     EVP_PKEY_free(ret);
217                     ret = key->pkey;
218           } else {
219                     key->pkey = ret;
220                     CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
221           }
222 
223           return ret;
224 
225  error:
226           EVP_PKEY_free(ret);
227           return (NULL);
228 }
229 
230 EVP_PKEY *
X509_PUBKEY_get(X509_PUBKEY * key)231 X509_PUBKEY_get(X509_PUBKEY *key)
232 {
233           EVP_PKEY *pkey;
234 
235           if ((pkey = X509_PUBKEY_get0(key)) == NULL)
236                     return (NULL);
237 
238           CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
239 
240           return pkey;
241 }
242 
243 /*
244  * Decode an X509_PUBKEY into the specified key type.
245  */
246 static int
pubkey_ex_d2i(int pkey_type,ASN1_VALUE ** pval,const unsigned char ** in,long len,const ASN1_ITEM * it)247 pubkey_ex_d2i(int pkey_type, ASN1_VALUE **pval, const unsigned char **in,
248     long len, const ASN1_ITEM *it)
249 {
250           const ASN1_EXTERN_FUNCS *ef = it->funcs;
251           const unsigned char *p = *in;
252           X509_PUBKEY *xpk = NULL;
253           ASN1_VALUE *key = NULL;
254           EVP_PKEY *pkey = NULL;
255           int ret = 0;
256 
257           if ((xpk = d2i_X509_PUBKEY(NULL, &p, len)) == NULL)
258                     goto err;
259           if ((pkey = X509_PUBKEY_get(xpk)) == NULL)
260                     goto err;
261 
262           switch (pkey_type) {
263           case EVP_PKEY_NONE:
264                     key = (ASN1_VALUE *)pkey;
265                     pkey = NULL;
266                     break;
267 
268           case EVP_PKEY_DSA:
269                     key = (ASN1_VALUE *)EVP_PKEY_get1_DSA(pkey);
270                     break;
271 
272           case EVP_PKEY_RSA:
273                     key = (ASN1_VALUE *)EVP_PKEY_get1_RSA(pkey);
274                     break;
275 
276           case EVP_PKEY_EC:
277                     key = (ASN1_VALUE *)EVP_PKEY_get1_EC_KEY(pkey);
278                     break;
279 
280           default:
281                     goto err;
282           }
283 
284           if (key == NULL)
285                     goto err;
286 
287           ef->asn1_ex_free(pval, it);
288 
289           *pval = key;
290           *in = p;
291           ret = 1;
292 
293  err:
294           EVP_PKEY_free(pkey);
295           X509_PUBKEY_free(xpk);
296 
297           return ret;
298 }
299 
300 /*
301  * Encode the specified key type into an X509_PUBKEY.
302  */
303 static int
pubkey_ex_i2d(int pkey_type,ASN1_VALUE ** pval,unsigned char ** out,const ASN1_ITEM * it)304 pubkey_ex_i2d(int pkey_type, ASN1_VALUE **pval, unsigned char **out,
305     const ASN1_ITEM *it)
306 {
307           X509_PUBKEY *xpk = NULL;
308           EVP_PKEY *pkey, *pktmp;
309           int ret = -1;
310 
311           if ((pkey = pktmp = EVP_PKEY_new()) == NULL)
312                     goto err;
313 
314           switch (pkey_type) {
315           case EVP_PKEY_NONE:
316                     pkey = (EVP_PKEY *)*pval;
317                     break;
318 
319           case EVP_PKEY_DSA:
320                     if (!EVP_PKEY_set1_DSA(pkey, (DSA *)*pval))
321                               goto err;
322                     break;
323 
324           case EVP_PKEY_RSA:
325                     if (!EVP_PKEY_set1_RSA(pkey, (RSA *)*pval))
326                               goto err;
327                     break;
328 
329           case EVP_PKEY_EC:
330                     if (!EVP_PKEY_set1_EC_KEY(pkey, (EC_KEY*)*pval))
331                               goto err;
332                     break;
333 
334           default:
335                     goto err;
336           }
337 
338           if (!X509_PUBKEY_set(&xpk, pkey))
339                     goto err;
340 
341           ret = i2d_X509_PUBKEY(xpk, out);
342 
343  err:
344           EVP_PKEY_free(pktmp);
345           X509_PUBKEY_free(xpk);
346 
347           return ret;
348 }
349 
350 static int
pkey_pubkey_ex_new(ASN1_VALUE ** pval,const ASN1_ITEM * it)351 pkey_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
352 {
353           if ((*pval = (ASN1_VALUE *)EVP_PKEY_new()) == NULL)
354                     return 0;
355 
356           return 1;
357 }
358 
359 static void
pkey_pubkey_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)360 pkey_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
361 {
362           EVP_PKEY_free((EVP_PKEY *)*pval);
363           *pval = NULL;
364 }
365 
366 static int
pkey_pubkey_ex_d2i(ASN1_VALUE ** pval,const unsigned char ** in,long len,const ASN1_ITEM * it,int tag,int aclass,char opt,ASN1_TLC * ctx)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 {
370           return pubkey_ex_d2i(EVP_PKEY_NONE, pval, in, len, it);
371 }
372 
373 static int
pkey_pubkey_ex_i2d(ASN1_VALUE ** pval,unsigned char ** out,const ASN1_ITEM * it,int tag,int aclass)374 pkey_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
375     int tag, int aclass)
376 {
377           return pubkey_ex_i2d(EVP_PKEY_NONE, pval, out, it);
378 }
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 *
d2i_PUBKEY(EVP_PKEY ** pkey,const unsigned char ** in,long len)401 d2i_PUBKEY(EVP_PKEY **pkey, const unsigned char **in, long len)
402 {
403           return (EVP_PKEY *)ASN1_item_d2i((ASN1_VALUE **)pkey, in, len,
404               &EVP_PKEY_PUBKEY_it);
405 }
406 
407 int
i2d_PUBKEY(EVP_PKEY * pkey,unsigned char ** out)408 i2d_PUBKEY(EVP_PKEY *pkey, unsigned char **out)
409 {
410           return ASN1_item_i2d((ASN1_VALUE *)pkey, out, &EVP_PKEY_PUBKEY_it);
411 }
412 
413 EVP_PKEY *
d2i_PUBKEY_bio(BIO * bp,EVP_PKEY ** pkey)414 d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **pkey)
415 {
416           return (EVP_PKEY *)ASN1_item_d2i_bio(&EVP_PKEY_PUBKEY_it, bp,
417               (ASN1_VALUE **)pkey);
418 }
419 
420 int
i2d_PUBKEY_bio(BIO * bp,EVP_PKEY * pkey)421 i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey)
422 {
423           return ASN1_item_i2d_bio(&EVP_PKEY_PUBKEY_it, bp, (ASN1_VALUE *)pkey);
424 }
425 
426 EVP_PKEY *
d2i_PUBKEY_fp(FILE * fp,EVP_PKEY ** pkey)427 d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **pkey)
428 {
429           return (EVP_PKEY *)ASN1_item_d2i_fp(&EVP_PKEY_PUBKEY_it, fp,
430               (ASN1_VALUE **)pkey);
431 }
432 
433 int
i2d_PUBKEY_fp(FILE * fp,EVP_PKEY * pkey)434 i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey)
435 {
436           return ASN1_item_i2d_fp(&EVP_PKEY_PUBKEY_it, fp, (ASN1_VALUE *)pkey);
437 }
438 
439 /*
440  * The following are equivalents but which return RSA and DSA keys.
441  */
442 #ifndef OPENSSL_NO_RSA
443 
444 static int
rsa_pubkey_ex_new(ASN1_VALUE ** pval,const ASN1_ITEM * it)445 rsa_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
446 {
447           if ((*pval = (ASN1_VALUE *)RSA_new()) == NULL)
448                     return 0;
449 
450           return 1;
451 }
452 
453 static void
rsa_pubkey_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)454 rsa_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
455 {
456           RSA_free((RSA *)*pval);
457           *pval = NULL;
458 }
459 
460 static int
rsa_pubkey_ex_d2i(ASN1_VALUE ** pval,const unsigned char ** in,long len,const ASN1_ITEM * it,int tag,int aclass,char opt,ASN1_TLC * ctx)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 {
464           return pubkey_ex_d2i(EVP_PKEY_RSA, pval, in, len, it);
465 }
466 
467 static int
rsa_pubkey_ex_i2d(ASN1_VALUE ** pval,unsigned char ** out,const ASN1_ITEM * it,int tag,int aclass)468 rsa_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
469     int tag, int aclass)
470 {
471           return pubkey_ex_i2d(EVP_PKEY_RSA, pval, out, it);
472 }
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 *
d2i_RSA_PUBKEY(RSA ** rsa,const unsigned char ** in,long len)495 d2i_RSA_PUBKEY(RSA **rsa, const unsigned char **in, long len)
496 {
497           return (RSA *)ASN1_item_d2i((ASN1_VALUE **)rsa, in, len,
498               &RSA_PUBKEY_it);
499 }
500 
501 int
i2d_RSA_PUBKEY(RSA * rsa,unsigned char ** out)502 i2d_RSA_PUBKEY(RSA *rsa, unsigned char **out)
503 {
504           return ASN1_item_i2d((ASN1_VALUE *)rsa, out, &RSA_PUBKEY_it);
505 }
506 
507 RSA *
d2i_RSA_PUBKEY_bio(BIO * bp,RSA ** rsa)508 d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa)
509 {
510           return (RSA *)ASN1_item_d2i_bio(&RSA_PUBKEY_it, bp, (ASN1_VALUE **)rsa);
511 }
512 
513 int
i2d_RSA_PUBKEY_bio(BIO * bp,RSA * rsa)514 i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa)
515 {
516           return ASN1_item_i2d_bio(&RSA_PUBKEY_it, bp, (ASN1_VALUE *)rsa);
517 }
518 
519 RSA *
d2i_RSA_PUBKEY_fp(FILE * fp,RSA ** rsa)520 d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa)
521 {
522           return (RSA *)ASN1_item_d2i_fp(&RSA_PUBKEY_it, fp, (ASN1_VALUE **)rsa);
523 }
524 
525 int
i2d_RSA_PUBKEY_fp(FILE * fp,RSA * rsa)526 i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa)
527 {
528           return ASN1_item_i2d_fp(&RSA_PUBKEY_it, fp, (ASN1_VALUE *)rsa);
529 }
530 #endif
531 
532 #ifndef OPENSSL_NO_DSA
533 
534 static int
dsa_pubkey_ex_new(ASN1_VALUE ** pval,const ASN1_ITEM * it)535 dsa_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
536 {
537           if ((*pval = (ASN1_VALUE *)DSA_new()) == NULL)
538                     return 0;
539 
540           return 1;
541 }
542 
543 static void
dsa_pubkey_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)544 dsa_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
545 {
546           DSA_free((DSA *)*pval);
547           *pval = NULL;
548 }
549 
550 static int
dsa_pubkey_ex_d2i(ASN1_VALUE ** pval,const unsigned char ** in,long len,const ASN1_ITEM * it,int tag,int aclass,char opt,ASN1_TLC * ctx)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 {
554           return pubkey_ex_d2i(EVP_PKEY_DSA, pval, in, len, it);
555 }
556 
557 static int
dsa_pubkey_ex_i2d(ASN1_VALUE ** pval,unsigned char ** out,const ASN1_ITEM * it,int tag,int aclass)558 dsa_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
559     int tag, int aclass)
560 {
561           return pubkey_ex_i2d(EVP_PKEY_DSA, pval, out, it);
562 }
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 *
d2i_DSA_PUBKEY(DSA ** dsa,const unsigned char ** in,long len)585 d2i_DSA_PUBKEY(DSA **dsa, const unsigned char **in, long len)
586 {
587           return (DSA *)ASN1_item_d2i((ASN1_VALUE **)dsa, in, len,
588               &DSA_PUBKEY_it);
589 }
590 
591 int
i2d_DSA_PUBKEY(DSA * dsa,unsigned char ** out)592 i2d_DSA_PUBKEY(DSA *dsa, unsigned char **out)
593 {
594           return ASN1_item_i2d((ASN1_VALUE *)dsa, out, &DSA_PUBKEY_it);
595 }
596 
597 DSA *
d2i_DSA_PUBKEY_bio(BIO * bp,DSA ** dsa)598 d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa)
599 {
600           return (DSA *)ASN1_item_d2i_bio(&DSA_PUBKEY_it, bp, (ASN1_VALUE **)dsa);
601 }
602 
603 int
i2d_DSA_PUBKEY_bio(BIO * bp,DSA * dsa)604 i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa)
605 {
606           return ASN1_item_i2d_bio(&DSA_PUBKEY_it, bp, (ASN1_VALUE *)dsa);
607 }
608 
609 DSA *
d2i_DSA_PUBKEY_fp(FILE * fp,DSA ** dsa)610 d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa)
611 {
612           return (DSA *)ASN1_item_d2i_fp(&DSA_PUBKEY_it, fp, (ASN1_VALUE **)dsa);
613 }
614 
615 int
i2d_DSA_PUBKEY_fp(FILE * fp,DSA * dsa)616 i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa)
617 {
618           return ASN1_item_i2d_fp(&DSA_PUBKEY_it, fp, (ASN1_VALUE *)dsa);
619 }
620 
621 #endif
622 
623 #ifndef OPENSSL_NO_EC
624 
625 static int
ec_pubkey_ex_new(ASN1_VALUE ** pval,const ASN1_ITEM * it)626 ec_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
627 {
628           if ((*pval = (ASN1_VALUE *)EC_KEY_new()) == NULL)
629                     return 0;
630 
631           return 1;
632 }
633 
634 static void
ec_pubkey_ex_free(ASN1_VALUE ** pval,const ASN1_ITEM * it)635 ec_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
636 {
637           EC_KEY_free((EC_KEY *)*pval);
638           *pval = NULL;
639 }
640 
641 static int
ec_pubkey_ex_d2i(ASN1_VALUE ** pval,const unsigned char ** in,long len,const ASN1_ITEM * it,int tag,int aclass,char opt,ASN1_TLC * ctx)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 {
645           return pubkey_ex_d2i(EVP_PKEY_EC, pval, in, len, it);
646 }
647 
648 static int
ec_pubkey_ex_i2d(ASN1_VALUE ** pval,unsigned char ** out,const ASN1_ITEM * it,int tag,int aclass)649 ec_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
650     int tag, int aclass)
651 {
652           return pubkey_ex_i2d(EVP_PKEY_EC, pval, out, it);
653 }
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 *
d2i_EC_PUBKEY(EC_KEY ** ec,const unsigned char ** in,long len)676 d2i_EC_PUBKEY(EC_KEY **ec, const unsigned char **in, long len)
677 {
678           return (EC_KEY *)ASN1_item_d2i((ASN1_VALUE **)ec, in, len,
679               &EC_PUBKEY_it);
680 }
681 
682 int
i2d_EC_PUBKEY(EC_KEY * ec,unsigned char ** out)683 i2d_EC_PUBKEY(EC_KEY *ec, unsigned char **out)
684 {
685           return ASN1_item_i2d((ASN1_VALUE *)ec, out, &EC_PUBKEY_it);
686 }
687 
688 EC_KEY *
d2i_EC_PUBKEY_bio(BIO * bp,EC_KEY ** ec)689 d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **ec)
690 {
691           return (EC_KEY *)ASN1_item_d2i_bio(&EC_PUBKEY_it, bp, (ASN1_VALUE **)ec);
692 }
693 
694 int
i2d_EC_PUBKEY_bio(BIO * bp,EC_KEY * ec)695 i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *ec)
696 {
697           return ASN1_item_i2d_bio(&EC_PUBKEY_it, bp, (ASN1_VALUE *)ec);
698 }
699 
700 EC_KEY *
d2i_EC_PUBKEY_fp(FILE * fp,EC_KEY ** ec)701 d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **ec)
702 {
703           return (EC_KEY *)ASN1_item_d2i_fp(&EC_PUBKEY_it, fp, (ASN1_VALUE **)ec);
704 }
705 
706 int
i2d_EC_PUBKEY_fp(FILE * fp,EC_KEY * ec)707 i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *ec)
708 {
709           return ASN1_item_i2d_fp(&EC_PUBKEY_it, fp, (ASN1_VALUE *)ec);
710 }
711 #endif
712 
713 int
X509_PUBKEY_set0_param(X509_PUBKEY * pub,ASN1_OBJECT * aobj,int ptype,void * pval,unsigned char * penc,int penclen)714 X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, int ptype,
715     void *pval, unsigned char *penc, int penclen)
716 {
717           if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
718                     return 0;
719 
720           if (penc == NULL)
721                     return 1;
722 
723           ASN1_STRING_set0(pub->public_key, penc, penclen);
724 
725           return asn1_abs_set_unused_bits(pub->public_key, 0);
726 }
727 
728 int
X509_PUBKEY_get0_param(ASN1_OBJECT ** ppkalg,const unsigned char ** pk,int * ppklen,X509_ALGOR ** pa,X509_PUBKEY * pub)729 X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, const unsigned char **pk,
730     int *ppklen, X509_ALGOR **pa, X509_PUBKEY *pub)
731 {
732           if (ppkalg)
733                     *ppkalg = pub->algor->algorithm;
734           if (pk) {
735                     *pk = pub->public_key->data;
736                     *ppklen = pub->public_key->length;
737           }
738           if (pa)
739                     *pa = pub->algor;
740           return 1;
741 }
742