summaryrefslogtreecommitdiff
path: root/lib/libcrypto/asn1/x_pubkey.c
blob: 69be6ed1f9294a5034eda19774303057b3d9bf16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/* $OpenBSD: x_pubkey.c,v 1.31 2021/12/25 13:17:48 jsing Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 *
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 *
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 *
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

#include <stdio.h>

#include <openssl/opensslconf.h>

#include <openssl/asn1t.h>
#include <openssl/err.h>
#include <openssl/x509.h>

#ifndef OPENSSL_NO_DSA
#include <openssl/dsa.h>
#endif
#ifndef OPENSSL_NO_RSA
#include <openssl/rsa.h>
#endif

#include "asn1_locl.h"
#include "evp_locl.h"
#include "x509_lcl.h"

/* Minor tweak to operation: free up EVP_PKEY */
static int
pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
{
	if (operation == ASN1_OP_FREE_POST) {
		X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
		EVP_PKEY_free(pubkey->pkey);
	}
	return 1;
}

static const ASN1_AUX X509_PUBKEY_aux = {
	.asn1_cb = pubkey_cb,
};
static const ASN1_TEMPLATE X509_PUBKEY_seq_tt[] = {
	{
		.offset = offsetof(X509_PUBKEY, algor),
		.field_name = "algor",
		.item = &X509_ALGOR_it,
	},
	{
		.offset = offsetof(X509_PUBKEY, public_key),
		.field_name = "public_key",
		.item = &ASN1_BIT_STRING_it,
	},
};

const ASN1_ITEM X509_PUBKEY_it = {
	.itype = ASN1_ITYPE_SEQUENCE,
	.utype = V_ASN1_SEQUENCE,
	.templates = X509_PUBKEY_seq_tt,
	.tcount = sizeof(X509_PUBKEY_seq_tt) / sizeof(ASN1_TEMPLATE),
	.funcs = &X509_PUBKEY_aux,
	.size = sizeof(X509_PUBKEY),
	.sname = "X509_PUBKEY",
};

X509_PUBKEY *
d2i_X509_PUBKEY(X509_PUBKEY **a, const unsigned char **in, long len)
{
	return (X509_PUBKEY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
	    &X509_PUBKEY_it);
}

int
i2d_X509_PUBKEY(X509_PUBKEY *a, unsigned char **out)
{
	return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_PUBKEY_it);
}

X509_PUBKEY *
X509_PUBKEY_new(void)
{
	return (X509_PUBKEY *)ASN1_item_new(&X509_PUBKEY_it);
}

void
X509_PUBKEY_free(X509_PUBKEY *a)
{
	ASN1_item_free((ASN1_VALUE *)a, &X509_PUBKEY_it);
}

int
X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
{
	X509_PUBKEY *pk = NULL;

	if (x == NULL)
		return (0);
	if ((pk = X509_PUBKEY_new()) == NULL)
		goto error;

	if (pkey->ameth) {
		if (pkey->ameth->pub_encode) {
			if (!pkey->ameth->pub_encode(pk, pkey)) {
				X509error(X509_R_PUBLIC_KEY_ENCODE_ERROR);
				goto error;
			}
		} else {
			X509error(X509_R_METHOD_NOT_SUPPORTED);
			goto error;
		}
	} else {
		X509error(X509_R_UNSUPPORTED_ALGORITHM);
		goto error;
	}

	if (*x != NULL)
		X509_PUBKEY_free(*x);

	*x = pk;

	return 1;

 error:
	if (pk != NULL)
		X509_PUBKEY_free(pk);
	return 0;
}

EVP_PKEY *
X509_PUBKEY_get0(X509_PUBKEY *key)
{
	EVP_PKEY *ret = NULL;

	if (key == NULL)
		goto error;

	if (key->pkey != NULL)
		return key->pkey;

	if (key->public_key == NULL)
		goto error;

	if ((ret = EVP_PKEY_new()) == NULL) {
		X509error(ERR_R_MALLOC_FAILURE);
		goto error;
	}

	if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) {
		X509error(X509_R_UNSUPPORTED_ALGORITHM);
		goto error;
	}

	if (ret->ameth->pub_decode) {
		if (!ret->ameth->pub_decode(ret, key)) {
			X509error(X509_R_PUBLIC_KEY_DECODE_ERROR);
			goto error;
		}
	} else {
		X509error(X509_R_METHOD_NOT_SUPPORTED);
		goto error;
	}

	/* Check to see if another thread set key->pkey first */
	CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
	if (key->pkey) {
		CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
		EVP_PKEY_free(ret);
		ret = key->pkey;
	} else {
		key->pkey = ret;
		CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
	}

	return ret;

 error:
	EVP_PKEY_free(ret);
	return (NULL);
}

EVP_PKEY *
X509_PUBKEY_get(X509_PUBKEY *key)
{
	EVP_PKEY *pkey;

	if ((pkey = X509_PUBKEY_get0(key)) == NULL)
		return (NULL);

	CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);

	return pkey;
}

/*
 * Decode an X509_PUBKEY into the specified key type.
 */
static int
pubkey_ex_d2i(int pkey_type, ASN1_VALUE **pval, const unsigned char **in,
    long len, const ASN1_ITEM *it)
{
	const ASN1_EXTERN_FUNCS *ef = it->funcs;
	const unsigned char *p = *in;
	X509_PUBKEY *xpk = NULL;
	ASN1_VALUE *key = NULL;
	EVP_PKEY *pkey = NULL;
	int ret = 0;

	if ((xpk = d2i_X509_PUBKEY(NULL, &p, len)) == NULL)
		goto err;
	if ((pkey = X509_PUBKEY_get(xpk)) == NULL)
		goto err;

	switch (pkey_type) {
	case EVP_PKEY_NONE:
		key = (ASN1_VALUE *)pkey;
		pkey = NULL;
		break;

	case EVP_PKEY_DSA:
		key = (ASN1_VALUE *)EVP_PKEY_get1_DSA(pkey);
		break;

	case EVP_PKEY_RSA:
		key = (ASN1_VALUE *)EVP_PKEY_get1_RSA(pkey);
		break;

	case EVP_PKEY_EC:
		key = (ASN1_VALUE *)EVP_PKEY_get1_EC_KEY(pkey);
		break;

	default:
		goto err;
	}

	if (key == NULL)
		goto err;

	ef->asn1_ex_free(pval, it);

	*pval = key;
	*in = p;
	ret = 1;

 err:
	EVP_PKEY_free(pkey);
	X509_PUBKEY_free(xpk);

	return ret;
}

/*
 * Encode the specified key type into an X509_PUBKEY.
 */
static int
pubkey_ex_i2d(int pkey_type, ASN1_VALUE **pval, unsigned char **out,
    const ASN1_ITEM *it)
{
	X509_PUBKEY *xpk = NULL;
	EVP_PKEY *pkey, *pktmp;
	int ret = -1;

	if ((pkey = pktmp = EVP_PKEY_new()) == NULL)
		goto err;

	switch (pkey_type) {
	case EVP_PKEY_NONE:
		pkey = (EVP_PKEY *)*pval;
		break;

	case EVP_PKEY_DSA:
		if (!EVP_PKEY_set1_DSA(pkey, (DSA *)*pval))
			goto err;
		break;

	case EVP_PKEY_RSA:
		if (!EVP_PKEY_set1_RSA(pkey, (RSA *)*pval))
			goto err;
		break;

	case EVP_PKEY_EC:
		if (!EVP_PKEY_set1_EC_KEY(pkey, (EC_KEY*)*pval))
			goto err;
		break;

	default:
		goto err;
	}

	if (!X509_PUBKEY_set(&xpk, pkey))
		goto err;

	ret = i2d_X509_PUBKEY(xpk, out);

 err:
	EVP_PKEY_free(pktmp);
	X509_PUBKEY_free(xpk);

	return ret;
}

static int
pkey_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
	if ((*pval = (ASN1_VALUE *)EVP_PKEY_new()) == NULL)
		return 0;

	return 1;
}

static void
pkey_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
	EVP_PKEY_free((EVP_PKEY *)*pval);
	*pval = NULL;
}

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)
{
	return pubkey_ex_d2i(EVP_PKEY_NONE, pval, in, len, it);
}

static int
pkey_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
    int tag, int aclass)
{
	return pubkey_ex_i2d(EVP_PKEY_NONE, pval, out, it);
}

const ASN1_EXTERN_FUNCS pkey_pubkey_asn1_ff = {
	.app_data = NULL,
	.asn1_ex_new = pkey_pubkey_ex_new,
	.asn1_ex_free = pkey_pubkey_ex_free,
	.asn1_ex_clear = NULL,
	.asn1_ex_d2i = pkey_pubkey_ex_d2i,
	.asn1_ex_i2d = pkey_pubkey_ex_i2d,
	.asn1_ex_print = NULL,
};

const ASN1_ITEM EVP_PKEY_PUBKEY_it = {
	.itype = ASN1_ITYPE_EXTERN,
	.utype = 0,
	.templates = NULL,
	.tcount = 0,
	.funcs = &pkey_pubkey_asn1_ff,
	.size = 0,
	.sname = NULL,
};

EVP_PKEY *
d2i_PUBKEY(EVP_PKEY **pkey, const unsigned char **in, long len)
{
	return (EVP_PKEY *)ASN1_item_d2i((ASN1_VALUE **)pkey, in, len,
	    &EVP_PKEY_PUBKEY_it);
}

int
i2d_PUBKEY(EVP_PKEY *pkey, unsigned char **out)
{
	return ASN1_item_i2d((ASN1_VALUE *)pkey, out, &EVP_PKEY_PUBKEY_it);
}

EVP_PKEY *
d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **pkey)
{
	return (EVP_PKEY *)ASN1_item_d2i_bio(&EVP_PKEY_PUBKEY_it, bp,
	    (ASN1_VALUE **)pkey);
}

int
i2d_PUBKEY_bio(BIO *bp, EVP_PKEY *pkey)
{
	return ASN1_item_i2d_bio(&EVP_PKEY_PUBKEY_it, bp, (ASN1_VALUE *)pkey);
}

EVP_PKEY *
d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **pkey)
{
	return (EVP_PKEY *)ASN1_item_d2i_fp(&EVP_PKEY_PUBKEY_it, fp,
	    (ASN1_VALUE **)pkey);
}

int
i2d_PUBKEY_fp(FILE *fp, EVP_PKEY *pkey)
{
	return ASN1_item_i2d_fp(&EVP_PKEY_PUBKEY_it, fp, (ASN1_VALUE *)pkey);
}

/*
 * The following are equivalents but which return RSA and DSA keys.
 */
#ifndef OPENSSL_NO_RSA

static int
rsa_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
	if ((*pval = (ASN1_VALUE *)RSA_new()) == NULL)
		return 0;

	return 1;
}

static void
rsa_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
	RSA_free((RSA *)*pval);
	*pval = NULL;
}

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)
{
	return pubkey_ex_d2i(EVP_PKEY_RSA, pval, in, len, it);
}

static int
rsa_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
    int tag, int aclass)
{
	return pubkey_ex_i2d(EVP_PKEY_RSA, pval, out, it);
}

const ASN1_EXTERN_FUNCS rsa_pubkey_asn1_ff = {
	.app_data = NULL,
	.asn1_ex_new = rsa_pubkey_ex_new,
	.asn1_ex_free = rsa_pubkey_ex_free,
	.asn1_ex_clear = NULL,
	.asn1_ex_d2i = rsa_pubkey_ex_d2i,
	.asn1_ex_i2d = rsa_pubkey_ex_i2d,
	.asn1_ex_print = NULL,
};

const ASN1_ITEM RSA_PUBKEY_it = {
	.itype = ASN1_ITYPE_EXTERN,
	.utype = 0,
	.templates = NULL,
	.tcount = 0,
	.funcs = &rsa_pubkey_asn1_ff,
	.size = 0,
	.sname = NULL,
};

RSA *
d2i_RSA_PUBKEY(RSA **rsa, const unsigned char **in, long len)
{
	return (RSA *)ASN1_item_d2i((ASN1_VALUE **)rsa, in, len,
	    &RSA_PUBKEY_it);
}

int
i2d_RSA_PUBKEY(RSA *rsa, unsigned char **out)
{
	return ASN1_item_i2d((ASN1_VALUE *)rsa, out, &RSA_PUBKEY_it);
}

RSA *
d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa)
{
	return (RSA *)ASN1_item_d2i_bio(&RSA_PUBKEY_it, bp, (ASN1_VALUE **)rsa);
}

int
i2d_RSA_PUBKEY_bio(BIO *bp, RSA *rsa)
{
	return ASN1_item_i2d_bio(&RSA_PUBKEY_it, bp, (ASN1_VALUE *)rsa);
}

RSA *
d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa)
{
	return (RSA *)ASN1_item_d2i_fp(&RSA_PUBKEY_it, fp, (ASN1_VALUE **)rsa);
}

int
i2d_RSA_PUBKEY_fp(FILE *fp, RSA *rsa)
{
	return ASN1_item_i2d_fp(&RSA_PUBKEY_it, fp, (ASN1_VALUE *)rsa);
}
#endif

#ifndef OPENSSL_NO_DSA

static int
dsa_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
	if ((*pval = (ASN1_VALUE *)DSA_new()) == NULL)
		return 0;

	return 1;
}

static void
dsa_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
	DSA_free((DSA *)*pval);
	*pval = NULL;
}

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)
{
	return pubkey_ex_d2i(EVP_PKEY_DSA, pval, in, len, it);
}

static int
dsa_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
    int tag, int aclass)
{
	return pubkey_ex_i2d(EVP_PKEY_DSA, pval, out, it);
}

const ASN1_EXTERN_FUNCS dsa_pubkey_asn1_ff = {
	.app_data = NULL,
	.asn1_ex_new = dsa_pubkey_ex_new,
	.asn1_ex_free = dsa_pubkey_ex_free,
	.asn1_ex_clear = NULL,
	.asn1_ex_d2i = dsa_pubkey_ex_d2i,
	.asn1_ex_i2d = dsa_pubkey_ex_i2d,
	.asn1_ex_print = NULL,
};

const ASN1_ITEM DSA_PUBKEY_it = {
	.itype = ASN1_ITYPE_EXTERN,
	.utype = 0,
	.templates = NULL,
	.tcount = 0,
	.funcs = &dsa_pubkey_asn1_ff,
	.size = 0,
	.sname = NULL,
};

DSA *
d2i_DSA_PUBKEY(DSA **dsa, const unsigned char **in, long len)
{
	return (DSA *)ASN1_item_d2i((ASN1_VALUE **)dsa, in, len,
	    &DSA_PUBKEY_it);
}

int
i2d_DSA_PUBKEY(DSA *dsa, unsigned char **out)
{
	return ASN1_item_i2d((ASN1_VALUE *)dsa, out, &DSA_PUBKEY_it);
}

DSA *
d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa)
{
	return (DSA *)ASN1_item_d2i_bio(&DSA_PUBKEY_it, bp, (ASN1_VALUE **)dsa);
}

int
i2d_DSA_PUBKEY_bio(BIO *bp, DSA *dsa)
{
	return ASN1_item_i2d_bio(&DSA_PUBKEY_it, bp, (ASN1_VALUE *)dsa);
}

DSA *
d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa)
{
	return (DSA *)ASN1_item_d2i_fp(&DSA_PUBKEY_it, fp, (ASN1_VALUE **)dsa);
}

int
i2d_DSA_PUBKEY_fp(FILE *fp, DSA *dsa)
{
	return ASN1_item_i2d_fp(&DSA_PUBKEY_it, fp, (ASN1_VALUE *)dsa);
}

#endif

#ifndef OPENSSL_NO_EC

static int
ec_pubkey_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
	if ((*pval = (ASN1_VALUE *)EC_KEY_new()) == NULL)
		return 0;

	return 1;
}

static void
ec_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
{
	EC_KEY_free((EC_KEY *)*pval);
	*pval = NULL;
}

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)
{
	return pubkey_ex_d2i(EVP_PKEY_EC, pval, in, len, it);
}

static int
ec_pubkey_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
    int tag, int aclass)
{
	return pubkey_ex_i2d(EVP_PKEY_EC, pval, out, it);
}

const ASN1_EXTERN_FUNCS ec_pubkey_asn1_ff = {
	.app_data = NULL,
	.asn1_ex_new = ec_pubkey_ex_new,
	.asn1_ex_free = ec_pubkey_ex_free,
	.asn1_ex_clear = NULL,
	.asn1_ex_d2i = ec_pubkey_ex_d2i,
	.asn1_ex_i2d = ec_pubkey_ex_i2d,
	.asn1_ex_print = NULL,
};

const ASN1_ITEM EC_PUBKEY_it = {
	.itype = ASN1_ITYPE_EXTERN,
	.utype = 0,
	.templates = NULL,
	.tcount = 0,
	.funcs = &ec_pubkey_asn1_ff,
	.size = 0,
	.sname = NULL,
};

EC_KEY *
d2i_EC_PUBKEY(EC_KEY **ec, const unsigned char **in, long len)
{
	return (EC_KEY *)ASN1_item_d2i((ASN1_VALUE **)ec, in, len,
	    &EC_PUBKEY_it);
}

int
i2d_EC_PUBKEY(EC_KEY *ec, unsigned char **out)
{
	return ASN1_item_i2d((ASN1_VALUE *)ec, out, &EC_PUBKEY_it);
}

EC_KEY *
d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **ec)
{
	return (EC_KEY *)ASN1_item_d2i_bio(&EC_PUBKEY_it, bp, (ASN1_VALUE **)ec);
}

int
i2d_EC_PUBKEY_bio(BIO *bp, EC_KEY *ec)
{
	return ASN1_item_i2d_bio(&EC_PUBKEY_it, bp, (ASN1_VALUE *)ec);
}

EC_KEY *
d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **ec)
{
	return (EC_KEY *)ASN1_item_d2i_fp(&EC_PUBKEY_it, fp, (ASN1_VALUE **)ec);
}

int
i2d_EC_PUBKEY_fp(FILE *fp, EC_KEY *ec)
{
	return ASN1_item_i2d_fp(&EC_PUBKEY_it, fp, (ASN1_VALUE *)ec);
}
#endif

int
X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj, int ptype,
    void *pval, unsigned char *penc, int penclen)
{
	if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
		return 0;
	if (penc) {
		free(pub->public_key->data);
		pub->public_key->data = penc;
		pub->public_key->length = penclen;
		/* Set number of unused bits to zero */
		pub->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
		pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
	}
	return 1;
}

int
X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg, const unsigned char **pk,
    int *ppklen, X509_ALGOR **pa, X509_PUBKEY *pub)
{
	if (ppkalg)
		*ppkalg = pub->algor->algorithm;
	if (pk) {
		*pk = pub->public_key->data;
		*ppklen = pub->public_key->length;
	}
	if (pa)
		*pa = pub->algor;
	return 1;
}