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