a583813b5869fdb39987c901003d760a2e82ef59
[openssl.git] / crypto / x509 / x_pubkey.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include "crypto/asn1.h"
15 #include "crypto/evp.h"
16 #include "crypto/x509.h"
17 #include <openssl/rsa.h>
18 #include <openssl/dsa.h>
19 #include <openssl/serializer.h>
20
21 struct X509_pubkey_st {
22     X509_ALGOR *algor;
23     ASN1_BIT_STRING *public_key;
24     EVP_PKEY *pkey;
25 };
26
27 static int x509_pubkey_decode(EVP_PKEY **pk, X509_PUBKEY *key);
28
29 /* Minor tweak to operation: free up EVP_PKEY */
30 static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
31                      void *exarg)
32 {
33     if (operation == ASN1_OP_FREE_POST) {
34         X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
35         EVP_PKEY_free(pubkey->pkey);
36     } else if (operation == ASN1_OP_D2I_POST) {
37         /* Attempt to decode public key and cache in pubkey structure. */
38         X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
39         EVP_PKEY_free(pubkey->pkey);
40         pubkey->pkey = NULL;
41         /*
42          * Opportunistically decode the key but remove any non fatal errors
43          * from the queue. Subsequent explicit attempts to decode/use the key
44          * will return an appropriate error.
45          */
46         ERR_set_mark();
47         if (x509_pubkey_decode(&pubkey->pkey, pubkey) == -1)
48             return 0;
49         ERR_pop_to_mark();
50     }
51     return 1;
52 }
53
54 ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
55         ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
56         ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
57 } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
58
59 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
60 IMPLEMENT_ASN1_DUP_FUNCTION(X509_PUBKEY)
61
62 /* TODO should better be called X509_PUBKEY_set1 */
63 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
64 {
65     X509_PUBKEY *pk = NULL;
66
67     if (x == NULL)
68         return 0;
69
70     if (pkey == NULL)
71         goto unsupported;
72
73     if (pkey->ameth != NULL) {
74         if ((pk = X509_PUBKEY_new()) == NULL) {
75             X509err(X509_F_X509_PUBKEY_SET, ERR_R_MALLOC_FAILURE);
76             goto error;
77         }
78         if (pkey->ameth->pub_encode != NULL) {
79             if (!pkey->ameth->pub_encode(pk, pkey)) {
80                 X509err(X509_F_X509_PUBKEY_SET,
81                         X509_R_PUBLIC_KEY_ENCODE_ERROR);
82                 goto error;
83             }
84         } else {
85             X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
86             goto error;
87         }
88     } else if (pkey->pkeys[0].keymgmt != NULL) {
89         BIO *bmem = BIO_new(BIO_s_mem());
90         const char *serprop = "format=der,type=public";
91         OSSL_SERIALIZER_CTX *sctx =
92             OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, serprop);
93
94         if (OSSL_SERIALIZER_to_bio(sctx, bmem)) {
95             const unsigned char *der = NULL;
96             long derlen = BIO_get_mem_data(bmem, (char **)&der);
97
98             pk = d2i_X509_PUBKEY(NULL, &der, derlen);
99         }
100
101         OSSL_SERIALIZER_CTX_free(sctx);
102         BIO_free(bmem);
103     }
104
105     if (pk == NULL)
106         goto unsupported;
107
108     X509_PUBKEY_free(*x);
109     if (!EVP_PKEY_up_ref(pkey)) {
110         X509err(X509_F_X509_PUBKEY_SET, ERR_R_INTERNAL_ERROR);
111         goto error;
112     }
113     *x = pk;
114
115     /*
116      * pk->pkey is NULL when using the legacy routine, but is non-NULL when
117      * going through the serializer, and for all intents and purposes, it's
118      * a perfect copy of |pkey|, just not the same instance.  In that case,
119      * we could simply return early, right here.
120      * However, in the interest of being cautious leaning on paranoia, some
121      * application might very well depend on the passed |pkey| being used
122      * and none other, so we spend a few more cycles throwing away the newly
123      * created |pk->pkey| and replace it with |pkey|.
124      * TODO(3.0) Investigate if it's safe to change to simply return here
125      * if |pk->pkey != NULL|.
126      */
127     if (pk->pkey != NULL)
128         EVP_PKEY_free(pk->pkey);
129
130     pk->pkey = pkey;
131     return 1;
132
133  unsupported:
134     X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
135
136  error:
137     X509_PUBKEY_free(pk);
138     return 0;
139 }
140
141 /*
142  * Attempt to decode a public key.
143  * Returns 1 on success, 0 for a decode failure and -1 for a fatal
144  * error e.g. malloc failure.
145  */
146
147
148 static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)
149 {
150     EVP_PKEY *pkey = EVP_PKEY_new();
151
152     if (pkey == NULL) {
153         X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);
154         return -1;
155     }
156
157     if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
158         X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);
159         goto error;
160     }
161
162     if (pkey->ameth->pub_decode) {
163         /*
164          * Treat any failure of pub_decode as a decode error. In
165          * future we could have different return codes for decode
166          * errors and fatal errors such as malloc failure.
167          */
168         if (!pkey->ameth->pub_decode(pkey, key)) {
169             X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);
170             goto error;
171         }
172     } else {
173         X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);
174         goto error;
175     }
176
177     *ppkey = pkey;
178     return 1;
179
180  error:
181     EVP_PKEY_free(pkey);
182     return 0;
183 }
184
185 EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)
186 {
187     EVP_PKEY *ret = NULL;
188
189     if (key == NULL || key->public_key == NULL)
190         return NULL;
191
192     if (key->pkey != NULL)
193         return key->pkey;
194
195     /*
196      * When the key ASN.1 is initially parsed an attempt is made to
197      * decode the public key and cache the EVP_PKEY structure. If this
198      * operation fails the cached value will be NULL. Parsing continues
199      * to allow parsing of unknown key types or unsupported forms.
200      * We repeat the decode operation so the appropriate errors are left
201      * in the queue.
202      */
203     x509_pubkey_decode(&ret, key);
204     /* If decode doesn't fail something bad happened */
205     if (ret != NULL) {
206         X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);
207         EVP_PKEY_free(ret);
208     }
209
210     return NULL;
211 }
212
213 EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
214 {
215     EVP_PKEY *ret = X509_PUBKEY_get0(key);
216     if (ret != NULL)
217         EVP_PKEY_up_ref(ret);
218     return ret;
219 }
220
221 /*
222  * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
223  * decode as X509_PUBKEY
224  */
225
226 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
227 {
228     X509_PUBKEY *xpk;
229     EVP_PKEY *pktmp;
230     const unsigned char *q;
231
232     q = *pp;
233     xpk = d2i_X509_PUBKEY(NULL, &q, length);
234     if (xpk == NULL)
235         return NULL;
236     pktmp = X509_PUBKEY_get(xpk);
237     X509_PUBKEY_free(xpk);
238     if (pktmp == NULL)
239         return NULL;
240     *pp = q;
241     if (a != NULL) {
242         EVP_PKEY_free(*a);
243         *a = pktmp;
244     }
245     return pktmp;
246 }
247
248 int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
249 {
250     X509_PUBKEY *xpk = NULL;
251     int ret = -1;
252
253     if (a == NULL)
254         return 0;
255     if ((xpk = X509_PUBKEY_new()) == NULL)
256         return -1;
257     if (a->ameth != NULL && a->ameth->pub_encode != NULL
258         && !a->ameth->pub_encode(xpk, a))
259         goto error;
260     xpk->pkey = (EVP_PKEY *)a;
261     ret = i2d_X509_PUBKEY(xpk, pp);
262     xpk->pkey = NULL;
263  error:
264     X509_PUBKEY_free(xpk);
265     return ret;
266 }
267
268 /*
269  * The following are equivalents but which return RSA and DSA keys
270  */
271 #ifndef OPENSSL_NO_RSA
272 RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
273 {
274     EVP_PKEY *pkey;
275     RSA *key;
276     const unsigned char *q;
277
278     q = *pp;
279     pkey = d2i_PUBKEY(NULL, &q, length);
280     if (pkey == NULL)
281         return NULL;
282     key = EVP_PKEY_get1_RSA(pkey);
283     EVP_PKEY_free(pkey);
284     if (key == NULL)
285         return NULL;
286     *pp = q;
287     if (a != NULL) {
288         RSA_free(*a);
289         *a = key;
290     }
291     return key;
292 }
293
294 int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
295 {
296     EVP_PKEY *pktmp;
297     int ret;
298     if (!a)
299         return 0;
300     pktmp = EVP_PKEY_new();
301     if (pktmp == NULL) {
302         ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
303         return -1;
304     }
305     (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
306     ret = i2d_PUBKEY(pktmp, pp);
307     pktmp->pkey.ptr = NULL;
308     EVP_PKEY_free(pktmp);
309     return ret;
310 }
311 #endif
312
313 #ifndef OPENSSL_NO_DSA
314 DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
315 {
316     EVP_PKEY *pkey;
317     DSA *key;
318     const unsigned char *q;
319
320     q = *pp;
321     pkey = d2i_PUBKEY(NULL, &q, length);
322     if (pkey == NULL)
323         return NULL;
324     key = EVP_PKEY_get1_DSA(pkey);
325     EVP_PKEY_free(pkey);
326     if (key == NULL)
327         return NULL;
328     *pp = q;
329     if (a != NULL) {
330         DSA_free(*a);
331         *a = key;
332     }
333     return key;
334 }
335
336 int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
337 {
338     EVP_PKEY *pktmp;
339     int ret;
340     if (!a)
341         return 0;
342     pktmp = EVP_PKEY_new();
343     if (pktmp == NULL) {
344         ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
345         return -1;
346     }
347     (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
348     ret = i2d_PUBKEY(pktmp, pp);
349     pktmp->pkey.ptr = NULL;
350     EVP_PKEY_free(pktmp);
351     return ret;
352 }
353 #endif
354
355 #ifndef OPENSSL_NO_EC
356 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
357 {
358     EVP_PKEY *pkey;
359     EC_KEY *key;
360     const unsigned char *q;
361
362     q = *pp;
363     pkey = d2i_PUBKEY(NULL, &q, length);
364     if (pkey == NULL)
365         return NULL;
366     key = EVP_PKEY_get1_EC_KEY(pkey);
367     EVP_PKEY_free(pkey);
368     if (key == NULL)
369         return NULL;
370     *pp = q;
371     if (a != NULL) {
372         EC_KEY_free(*a);
373         *a = key;
374     }
375     return key;
376 }
377
378 int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
379 {
380     EVP_PKEY *pktmp;
381     int ret;
382
383     if (a == NULL)
384         return 0;
385     if ((pktmp = EVP_PKEY_new()) == NULL) {
386         ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
387         return -1;
388     }
389     (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
390     ret = i2d_PUBKEY(pktmp, pp);
391     pktmp->pkey.ptr = NULL;
392     EVP_PKEY_free(pktmp);
393     return ret;
394 }
395 #endif
396
397 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
398                            int ptype, void *pval,
399                            unsigned char *penc, int penclen)
400 {
401     if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
402         return 0;
403     if (penc) {
404         OPENSSL_free(pub->public_key->data);
405         pub->public_key->data = penc;
406         pub->public_key->length = penclen;
407         /* Set number of unused bits to zero */
408         pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
409         pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
410     }
411     return 1;
412 }
413
414 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
415                            const unsigned char **pk, int *ppklen,
416                            X509_ALGOR **pa, X509_PUBKEY *pub)
417 {
418     if (ppkalg)
419         *ppkalg = pub->algor->algorithm;
420     if (pk) {
421         *pk = pub->public_key->data;
422         *ppklen = pub->public_key->length;
423     }
424     if (pa)
425         *pa = pub->algor;
426     return 1;
427 }
428
429 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
430 {
431     if (x == NULL)
432         return NULL;
433     return x->cert_info.key->public_key;
434 }