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