]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/x_pubkey.c
Remove /* foo.c */ comments
[thirdparty/openssl.git] / crypto / asn1 / x_pubkey.c
CommitLineData
58964a49 1/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 7 *
d02b48c6
RE
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 14 *
d02b48c6
RE
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
0f113f3e 21 *
d02b48c6
RE
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
0f113f3e 36 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 39 *
d02b48c6
RE
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
0f113f3e 51 *
d02b48c6
RE
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58#include <stdio.h>
b39fc560 59#include "internal/cryptlib.h"
9d6b1ce6 60#include <openssl/asn1t.h>
f0e8ae72 61#include <openssl/x509.h>
5fe736e5 62#include "internal/asn1_int.h"
3aeb9348 63#include "internal/evp_int.h"
3eeaab4b 64#ifndef OPENSSL_NO_RSA
0f113f3e 65# include <openssl/rsa.h>
3eeaab4b
NL
66#endif
67#ifndef OPENSSL_NO_DSA
0f113f3e 68# include <openssl/dsa.h>
3eeaab4b 69#endif
d02b48c6 70
9d6b1ce6 71/* Minor tweak to operation: free up EVP_PKEY */
24484759 72static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
0f113f3e
MC
73 void *exarg)
74{
75 if (operation == ASN1_OP_FREE_POST) {
76 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
77 EVP_PKEY_free(pubkey->pkey);
78 }
79 return 1;
80}
d02b48c6 81
9d6b1ce6 82ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
0f113f3e
MC
83 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
84 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
d339187b 85} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
d02b48c6 86
9d6b1ce6 87IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
d02b48c6 88
6b691a5c 89int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
0f113f3e
MC
90{
91 X509_PUBKEY *pk = NULL;
92
93 if (x == NULL)
94 return (0);
95
96 if ((pk = X509_PUBKEY_new()) == NULL)
97 goto error;
98
99 if (pkey->ameth) {
100 if (pkey->ameth->pub_encode) {
101 if (!pkey->ameth->pub_encode(pk, pkey)) {
102 X509err(X509_F_X509_PUBKEY_SET,
103 X509_R_PUBLIC_KEY_ENCODE_ERROR);
104 goto error;
105 }
106 } else {
107 X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
108 goto error;
109 }
110 } else {
111 X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
112 goto error;
113 }
114
222561fe 115 X509_PUBKEY_free(*x);
0f113f3e 116 *x = pk;
0f113f3e 117 return 1;
222561fe 118
0f113f3e 119 error:
222561fe 120 X509_PUBKEY_free(pk);
0f113f3e
MC
121 return 0;
122}
d02b48c6 123
c01ff880 124EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)
0f113f3e
MC
125{
126 EVP_PKEY *ret = NULL;
127
128 if (key == NULL)
129 goto error;
130
c01ff880 131 if (key->pkey != NULL)
0f113f3e 132 return key->pkey;
0f113f3e
MC
133
134 if (key->public_key == NULL)
135 goto error;
136
137 if ((ret = EVP_PKEY_new()) == NULL) {
c01ff880 138 X509err(X509_F_X509_PUBKEY_GET0, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
139 goto error;
140 }
141
142 if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) {
c01ff880 143 X509err(X509_F_X509_PUBKEY_GET0, X509_R_UNSUPPORTED_ALGORITHM);
0f113f3e
MC
144 goto error;
145 }
146
147 if (ret->ameth->pub_decode) {
148 if (!ret->ameth->pub_decode(ret, key)) {
c01ff880 149 X509err(X509_F_X509_PUBKEY_GET0, X509_R_PUBLIC_KEY_DECODE_ERROR);
0f113f3e
MC
150 goto error;
151 }
152 } else {
c01ff880 153 X509err(X509_F_X509_PUBKEY_GET0, X509_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
154 goto error;
155 }
156
157 /* Check to see if another thread set key->pkey first */
158 CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
159 if (key->pkey) {
160 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
161 EVP_PKEY_free(ret);
162 ret = key->pkey;
163 } else {
164 key->pkey = ret;
165 CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
166 }
0f113f3e
MC
167
168 return ret;
169
170 error:
c5ba2d99 171 EVP_PKEY_free(ret);
0f113f3e
MC
172 return (NULL);
173}
174
c01ff880
DSH
175EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
176{
177 EVP_PKEY *ret = X509_PUBKEY_get0(key);
178 if (ret != NULL)
179 EVP_PKEY_up_ref(ret);
180 return ret;
181}
182
0f113f3e
MC
183/*
184 * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
185 * decode as X509_PUBKEY
52664f50
DSH
186 */
187
0f113f3e
MC
188EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
189{
190 X509_PUBKEY *xpk;
191 EVP_PKEY *pktmp;
a46c9789
KR
192 const unsigned char *q;
193 q = *pp;
194 xpk = d2i_X509_PUBKEY(NULL, &q, length);
0f113f3e
MC
195 if (!xpk)
196 return NULL;
197 pktmp = X509_PUBKEY_get(xpk);
198 X509_PUBKEY_free(xpk);
199 if (!pktmp)
200 return NULL;
a46c9789 201 *pp = q;
0f113f3e
MC
202 if (a) {
203 EVP_PKEY_free(*a);
204 *a = pktmp;
205 }
206 return pktmp;
207}
52664f50
DSH
208
209int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
0f113f3e
MC
210{
211 X509_PUBKEY *xpk = NULL;
212 int ret;
213 if (!a)
214 return 0;
215 if (!X509_PUBKEY_set(&xpk, a))
216 return 0;
217 ret = i2d_X509_PUBKEY(xpk, pp);
218 X509_PUBKEY_free(xpk);
219 return ret;
220}
221
222/*
223 * The following are equivalents but which return RSA and DSA keys
52664f50 224 */
cf1b7d96 225#ifndef OPENSSL_NO_RSA
0f113f3e
MC
226RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
227{
228 EVP_PKEY *pkey;
229 RSA *key;
230 const unsigned char *q;
231 q = *pp;
232 pkey = d2i_PUBKEY(NULL, &q, length);
233 if (!pkey)
234 return NULL;
235 key = EVP_PKEY_get1_RSA(pkey);
236 EVP_PKEY_free(pkey);
237 if (!key)
238 return NULL;
239 *pp = q;
240 if (a) {
241 RSA_free(*a);
242 *a = key;
243 }
244 return key;
245}
52664f50
DSH
246
247int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
0f113f3e
MC
248{
249 EVP_PKEY *pktmp;
250 int ret;
251 if (!a)
252 return 0;
253 pktmp = EVP_PKEY_new();
90945fa3 254 if (pktmp == NULL) {
0f113f3e
MC
255 ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
256 return 0;
257 }
258 EVP_PKEY_set1_RSA(pktmp, a);
259 ret = i2d_PUBKEY(pktmp, pp);
260 EVP_PKEY_free(pktmp);
261 return ret;
262}
12aefe78 263#endif
52664f50 264
cf1b7d96 265#ifndef OPENSSL_NO_DSA
0f113f3e
MC
266DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
267{
268 EVP_PKEY *pkey;
269 DSA *key;
270 const unsigned char *q;
271 q = *pp;
272 pkey = d2i_PUBKEY(NULL, &q, length);
273 if (!pkey)
274 return NULL;
275 key = EVP_PKEY_get1_DSA(pkey);
276 EVP_PKEY_free(pkey);
277 if (!key)
278 return NULL;
279 *pp = q;
280 if (a) {
281 DSA_free(*a);
282 *a = key;
283 }
284 return key;
285}
52664f50
DSH
286
287int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
0f113f3e
MC
288{
289 EVP_PKEY *pktmp;
290 int ret;
291 if (!a)
292 return 0;
293 pktmp = EVP_PKEY_new();
90945fa3 294 if (pktmp == NULL) {
0f113f3e
MC
295 ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
296 return 0;
297 }
298 EVP_PKEY_set1_DSA(pktmp, a);
299 ret = i2d_PUBKEY(pktmp, pp);
300 EVP_PKEY_free(pktmp);
301 return ret;
302}
4d94ae00
BM
303#endif
304
14a7cfb3 305#ifndef OPENSSL_NO_EC
6343829a 306EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
0f113f3e
MC
307{
308 EVP_PKEY *pkey;
309 EC_KEY *key;
310 const unsigned char *q;
311 q = *pp;
312 pkey = d2i_PUBKEY(NULL, &q, length);
313 if (!pkey)
314 return (NULL);
315 key = EVP_PKEY_get1_EC_KEY(pkey);
316 EVP_PKEY_free(pkey);
317 if (!key)
318 return (NULL);
319 *pp = q;
320 if (a) {
321 EC_KEY_free(*a);
322 *a = key;
323 }
324 return (key);
325}
4d94ae00 326
14a7cfb3 327int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
0f113f3e
MC
328{
329 EVP_PKEY *pktmp;
330 int ret;
331 if (!a)
332 return (0);
333 if ((pktmp = EVP_PKEY_new()) == NULL) {
334 ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
335 return (0);
336 }
337 EVP_PKEY_set1_EC_KEY(pktmp, a);
338 ret = i2d_PUBKEY(pktmp, pp);
339 EVP_PKEY_free(pktmp);
340 return (ret);
341}
12aefe78 342#endif
448be743
DSH
343
344int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
0f113f3e
MC
345 int ptype, void *pval,
346 unsigned char *penc, int penclen)
347{
348 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
349 return 0;
350 if (penc) {
b548a1f1 351 OPENSSL_free(pub->public_key->data);
0f113f3e
MC
352 pub->public_key->data = penc;
353 pub->public_key->length = penclen;
354 /* Set number of unused bits to zero */
355 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
356 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
357 }
358 return 1;
359}
448be743
DSH
360
361int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
0f113f3e
MC
362 const unsigned char **pk, int *ppklen,
363 X509_ALGOR **pa, X509_PUBKEY *pub)
364{
365 if (ppkalg)
366 *ppkalg = pub->algor->algorithm;
367 if (pk) {
368 *pk = pub->public_key->data;
369 *ppklen = pub->public_key->length;
370 }
371 if (pa)
372 *pa = pub->algor;
373 return 1;
374}