1d65b20ed6189dbaa4d9f2667392cb9a28f7fd6c
[openssl.git] / crypto / asn1 / x_pubkey.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
7  *
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).
14  *
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.
21  *
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 :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
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.
51  *
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>
59 #include "internal/cryptlib.h"
60 #include <openssl/asn1t.h>
61 #include <openssl/x509.h>
62 #include "internal/asn1_int.h"
63 #include "internal/evp_int.h"
64 #ifndef OPENSSL_NO_RSA
65 # include <openssl/rsa.h>
66 #endif
67 #ifndef OPENSSL_NO_DSA
68 # include <openssl/dsa.h>
69 #endif
70
71 /* Minor tweak to operation: free up EVP_PKEY */
72 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
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 }
81
82 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
83         ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
84         ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
85 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
86
87 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
88
89 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
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
115     X509_PUBKEY_free(*x);
116     *x = pk;
117     return 1;
118
119  error:
120     X509_PUBKEY_free(pk);
121     return 0;
122 }
123
124 EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)
125 {
126     EVP_PKEY *ret = NULL;
127
128     if (key == NULL)
129         goto error;
130
131     if (key->pkey != NULL)
132         return key->pkey;
133
134     if (key->public_key == NULL)
135         goto error;
136
137     if ((ret = EVP_PKEY_new()) == NULL) {
138         X509err(X509_F_X509_PUBKEY_GET0, ERR_R_MALLOC_FAILURE);
139         goto error;
140     }
141
142     if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) {
143         X509err(X509_F_X509_PUBKEY_GET0, X509_R_UNSUPPORTED_ALGORITHM);
144         goto error;
145     }
146
147     if (ret->ameth->pub_decode) {
148         if (!ret->ameth->pub_decode(ret, key)) {
149             X509err(X509_F_X509_PUBKEY_GET0, X509_R_PUBLIC_KEY_DECODE_ERROR);
150             goto error;
151         }
152     } else {
153         X509err(X509_F_X509_PUBKEY_GET0, X509_R_METHOD_NOT_SUPPORTED);
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     }
167
168     return ret;
169
170  error:
171     EVP_PKEY_free(ret);
172     return (NULL);
173 }
174
175 EVP_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
183 /*
184  * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
185  * decode as X509_PUBKEY
186  */
187
188 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
189 {
190     X509_PUBKEY *xpk;
191     EVP_PKEY *pktmp;
192     const unsigned char *q;
193     q = *pp;
194     xpk = d2i_X509_PUBKEY(NULL, &q, length);
195     if (!xpk)
196         return NULL;
197     pktmp = X509_PUBKEY_get(xpk);
198     X509_PUBKEY_free(xpk);
199     if (!pktmp)
200         return NULL;
201     *pp = q;
202     if (a) {
203         EVP_PKEY_free(*a);
204         *a = pktmp;
205     }
206     return pktmp;
207 }
208
209 int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
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
224  */
225 #ifndef OPENSSL_NO_RSA
226 RSA *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 }
246
247 int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
248 {
249     EVP_PKEY *pktmp;
250     int ret;
251     if (!a)
252         return 0;
253     pktmp = EVP_PKEY_new();
254     if (pktmp == NULL) {
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 }
263 #endif
264
265 #ifndef OPENSSL_NO_DSA
266 DSA *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 }
286
287 int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
288 {
289     EVP_PKEY *pktmp;
290     int ret;
291     if (!a)
292         return 0;
293     pktmp = EVP_PKEY_new();
294     if (pktmp == NULL) {
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 }
303 #endif
304
305 #ifndef OPENSSL_NO_EC
306 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
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 }
326
327 int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
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 }
342 #endif
343
344 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
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) {
351         OPENSSL_free(pub->public_key->data);
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 }
360
361 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
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 }