155714Skris/* crypto/asn1/x_pubkey.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
61109998Smarkm#include <openssl/asn1t.h>
6255714Skris#include <openssl/x509.h>
63238405Sjkim#include "asn1_locl.h"
64160814Ssimon#ifndef OPENSSL_NO_RSA
65280304Sjkim# include <openssl/rsa.h>
66160814Ssimon#endif
67160814Ssimon#ifndef OPENSSL_NO_DSA
68280304Sjkim# include <openssl/dsa.h>
69160814Ssimon#endif
7055714Skris
71109998Smarkm/* Minor tweak to operation: free up EVP_PKEY */
72238405Sjkimstatic int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
73280304Sjkim                     void *exarg)
74280304Sjkim{
75280304Sjkim    if (operation == ASN1_OP_FREE_POST) {
76280304Sjkim        X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
77280304Sjkim        EVP_PKEY_free(pubkey->pkey);
78280304Sjkim    }
79280304Sjkim    return 1;
80280304Sjkim}
8155714Skris
82109998SmarkmASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
83280304Sjkim        ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
84280304Sjkim        ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
85109998Smarkm} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
8655714Skris
87109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
8855714Skris
8955714Skrisint X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
90280304Sjkim{
91280304Sjkim    X509_PUBKEY *pk = NULL;
9255714Skris
93280304Sjkim    if (x == NULL)
94280304Sjkim        return (0);
9555714Skris
96280304Sjkim    if ((pk = X509_PUBKEY_new()) == NULL)
97280304Sjkim        goto error;
9855714Skris
99280304Sjkim    if (pkey->ameth) {
100280304Sjkim        if (pkey->ameth->pub_encode) {
101280304Sjkim            if (!pkey->ameth->pub_encode(pk, pkey)) {
102280304Sjkim                X509err(X509_F_X509_PUBKEY_SET,
103280304Sjkim                        X509_R_PUBLIC_KEY_ENCODE_ERROR);
104280304Sjkim                goto error;
105280304Sjkim            }
106280304Sjkim        } else {
107280304Sjkim            X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
108280304Sjkim            goto error;
109280304Sjkim        }
110280304Sjkim    } else {
111280304Sjkim        X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
112280304Sjkim        goto error;
113280304Sjkim    }
11455714Skris
115280304Sjkim    if (*x != NULL)
116280304Sjkim        X509_PUBKEY_free(*x);
11755714Skris
118280304Sjkim    *x = pk;
11955714Skris
120280304Sjkim    return 1;
121280304Sjkim error:
122280304Sjkim    if (pk != NULL)
123280304Sjkim        X509_PUBKEY_free(pk);
124280304Sjkim    return 0;
125280304Sjkim}
12655714Skris
12755714SkrisEVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
128280304Sjkim{
129280304Sjkim    EVP_PKEY *ret = NULL;
13055714Skris
131280304Sjkim    if (key == NULL)
132280304Sjkim        goto error;
13355714Skris
134280304Sjkim    if (key->pkey != NULL) {
135280304Sjkim        CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
136280304Sjkim        return key->pkey;
137280304Sjkim    }
13855714Skris
139280304Sjkim    if (key->public_key == NULL)
140280304Sjkim        goto error;
14155714Skris
142280304Sjkim    if ((ret = EVP_PKEY_new()) == NULL) {
143280304Sjkim        X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
144280304Sjkim        goto error;
145280304Sjkim    }
14655714Skris
147280304Sjkim    if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) {
148280304Sjkim        X509err(X509_F_X509_PUBKEY_GET, X509_R_UNSUPPORTED_ALGORITHM);
149280304Sjkim        goto error;
150280304Sjkim    }
151238405Sjkim
152280304Sjkim    if (ret->ameth->pub_decode) {
153280304Sjkim        if (!ret->ameth->pub_decode(ret, key)) {
154280304Sjkim            X509err(X509_F_X509_PUBKEY_GET, X509_R_PUBLIC_KEY_DECODE_ERROR);
155280304Sjkim            goto error;
156280304Sjkim        }
157280304Sjkim    } else {
158280304Sjkim        X509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED);
159280304Sjkim        goto error;
160280304Sjkim    }
161160814Ssimon
162280304Sjkim    /* Check to see if another thread set key->pkey first */
163280304Sjkim    CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
164280304Sjkim    if (key->pkey) {
165280304Sjkim        CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
166280304Sjkim        EVP_PKEY_free(ret);
167280304Sjkim        ret = key->pkey;
168280304Sjkim    } else {
169280304Sjkim        key->pkey = ret;
170280304Sjkim        CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
171280304Sjkim    }
172280304Sjkim    CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);
173238405Sjkim
174280304Sjkim    return ret;
175238405Sjkim
176280304Sjkim error:
177280304Sjkim    if (ret != NULL)
178280304Sjkim        EVP_PKEY_free(ret);
179280304Sjkim    return (NULL);
180280304Sjkim}
18155714Skris
182280304Sjkim/*
183280304Sjkim * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
184280304Sjkim * decode as X509_PUBKEY
18559191Skris */
18659191Skris
187280304SjkimEVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
188280304Sjkim{
189280304Sjkim    X509_PUBKEY *xpk;
190280304Sjkim    EVP_PKEY *pktmp;
191291721Sjkim    const unsigned char *q;
192291721Sjkim    q = *pp;
193291721Sjkim    xpk = d2i_X509_PUBKEY(NULL, &q, length);
194280304Sjkim    if (!xpk)
195280304Sjkim        return NULL;
196280304Sjkim    pktmp = X509_PUBKEY_get(xpk);
197280304Sjkim    X509_PUBKEY_free(xpk);
198280304Sjkim    if (!pktmp)
199280304Sjkim        return NULL;
200291721Sjkim    *pp = q;
201280304Sjkim    if (a) {
202280304Sjkim        EVP_PKEY_free(*a);
203280304Sjkim        *a = pktmp;
204280304Sjkim    }
205280304Sjkim    return pktmp;
206280304Sjkim}
20759191Skris
20859191Skrisint i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
209280304Sjkim{
210280304Sjkim    X509_PUBKEY *xpk = NULL;
211280304Sjkim    int ret;
212280304Sjkim    if (!a)
213280304Sjkim        return 0;
214280304Sjkim    if (!X509_PUBKEY_set(&xpk, a))
215280304Sjkim        return 0;
216280304Sjkim    ret = i2d_X509_PUBKEY(xpk, pp);
217280304Sjkim    X509_PUBKEY_free(xpk);
218280304Sjkim    return ret;
219280304Sjkim}
22059191Skris
221280304Sjkim/*
222280304Sjkim * The following are equivalents but which return RSA and DSA keys
22359191Skris */
224109998Smarkm#ifndef OPENSSL_NO_RSA
225280304SjkimRSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
226280304Sjkim{
227280304Sjkim    EVP_PKEY *pkey;
228280304Sjkim    RSA *key;
229280304Sjkim    const unsigned char *q;
230280304Sjkim    q = *pp;
231280304Sjkim    pkey = d2i_PUBKEY(NULL, &q, length);
232280304Sjkim    if (!pkey)
233280304Sjkim        return NULL;
234280304Sjkim    key = EVP_PKEY_get1_RSA(pkey);
235280304Sjkim    EVP_PKEY_free(pkey);
236280304Sjkim    if (!key)
237280304Sjkim        return NULL;
238280304Sjkim    *pp = q;
239280304Sjkim    if (a) {
240280304Sjkim        RSA_free(*a);
241280304Sjkim        *a = key;
242280304Sjkim    }
243280304Sjkim    return key;
244280304Sjkim}
24559191Skris
24659191Skrisint i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
247280304Sjkim{
248280304Sjkim    EVP_PKEY *pktmp;
249280304Sjkim    int ret;
250280304Sjkim    if (!a)
251280304Sjkim        return 0;
252280304Sjkim    pktmp = EVP_PKEY_new();
253280304Sjkim    if (!pktmp) {
254280304Sjkim        ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
255280304Sjkim        return 0;
256280304Sjkim    }
257280304Sjkim    EVP_PKEY_set1_RSA(pktmp, a);
258280304Sjkim    ret = i2d_PUBKEY(pktmp, pp);
259280304Sjkim    EVP_PKEY_free(pktmp);
260280304Sjkim    return ret;
261280304Sjkim}
26259191Skris#endif
26359191Skris
264109998Smarkm#ifndef OPENSSL_NO_DSA
265280304SjkimDSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
266280304Sjkim{
267280304Sjkim    EVP_PKEY *pkey;
268280304Sjkim    DSA *key;
269280304Sjkim    const unsigned char *q;
270280304Sjkim    q = *pp;
271280304Sjkim    pkey = d2i_PUBKEY(NULL, &q, length);
272280304Sjkim    if (!pkey)
273280304Sjkim        return NULL;
274280304Sjkim    key = EVP_PKEY_get1_DSA(pkey);
275280304Sjkim    EVP_PKEY_free(pkey);
276280304Sjkim    if (!key)
277280304Sjkim        return NULL;
278280304Sjkim    *pp = q;
279280304Sjkim    if (a) {
280280304Sjkim        DSA_free(*a);
281280304Sjkim        *a = key;
282280304Sjkim    }
283280304Sjkim    return key;
284280304Sjkim}
28559191Skris
28659191Skrisint i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
287280304Sjkim{
288280304Sjkim    EVP_PKEY *pktmp;
289280304Sjkim    int ret;
290280304Sjkim    if (!a)
291280304Sjkim        return 0;
292280304Sjkim    pktmp = EVP_PKEY_new();
293280304Sjkim    if (!pktmp) {
294280304Sjkim        ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
295280304Sjkim        return 0;
296280304Sjkim    }
297280304Sjkim    EVP_PKEY_set1_DSA(pktmp, a);
298280304Sjkim    ret = i2d_PUBKEY(pktmp, pp);
299280304Sjkim    EVP_PKEY_free(pktmp);
300280304Sjkim    return ret;
301280304Sjkim}
30259191Skris#endif
303160814Ssimon
304160814Ssimon#ifndef OPENSSL_NO_EC
305160814SsimonEC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
306280304Sjkim{
307280304Sjkim    EVP_PKEY *pkey;
308280304Sjkim    EC_KEY *key;
309280304Sjkim    const unsigned char *q;
310280304Sjkim    q = *pp;
311280304Sjkim    pkey = d2i_PUBKEY(NULL, &q, length);
312280304Sjkim    if (!pkey)
313280304Sjkim        return (NULL);
314280304Sjkim    key = EVP_PKEY_get1_EC_KEY(pkey);
315280304Sjkim    EVP_PKEY_free(pkey);
316280304Sjkim    if (!key)
317280304Sjkim        return (NULL);
318280304Sjkim    *pp = q;
319280304Sjkim    if (a) {
320280304Sjkim        EC_KEY_free(*a);
321280304Sjkim        *a = key;
322280304Sjkim    }
323280304Sjkim    return (key);
324280304Sjkim}
325160814Ssimon
326160814Ssimonint i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
327280304Sjkim{
328280304Sjkim    EVP_PKEY *pktmp;
329280304Sjkim    int ret;
330280304Sjkim    if (!a)
331280304Sjkim        return (0);
332280304Sjkim    if ((pktmp = EVP_PKEY_new()) == NULL) {
333280304Sjkim        ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
334280304Sjkim        return (0);
335280304Sjkim    }
336280304Sjkim    EVP_PKEY_set1_EC_KEY(pktmp, a);
337280304Sjkim    ret = i2d_PUBKEY(pktmp, pp);
338280304Sjkim    EVP_PKEY_free(pktmp);
339280304Sjkim    return (ret);
340280304Sjkim}
341160814Ssimon#endif
342238405Sjkim
343238405Sjkimint X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
344280304Sjkim                           int ptype, void *pval,
345280304Sjkim                           unsigned char *penc, int penclen)
346280304Sjkim{
347280304Sjkim    if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
348280304Sjkim        return 0;
349280304Sjkim    if (penc) {
350280304Sjkim        if (pub->public_key->data)
351280304Sjkim            OPENSSL_free(pub->public_key->data);
352280304Sjkim        pub->public_key->data = penc;
353280304Sjkim        pub->public_key->length = penclen;
354280304Sjkim        /* Set number of unused bits to zero */
355280304Sjkim        pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
356280304Sjkim        pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
357280304Sjkim    }
358280304Sjkim    return 1;
359280304Sjkim}
360238405Sjkim
361238405Sjkimint X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
362280304Sjkim                           const unsigned char **pk, int *ppklen,
363280304Sjkim                           X509_ALGOR **pa, X509_PUBKEY *pub)
364280304Sjkim{
365280304Sjkim    if (ppkalg)
366280304Sjkim        *ppkalg = pub->algor->algorithm;
367280304Sjkim    if (pk) {
368280304Sjkim        *pk = pub->public_key->data;
369280304Sjkim        *ppklen = pub->public_key->length;
370280304Sjkim    }
371280304Sjkim    if (pa)
372280304Sjkim        *pa = pub->algor;
373280304Sjkim    return 1;
374280304Sjkim}
375