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.
855714Skris *
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).
1555714Skris *
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.
2255714Skris *
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 :-).
3755714Skris * 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)"
4055714Skris *
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.
5255714Skris *
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
65160814Ssimon#include <openssl/rsa.h>
66160814Ssimon#endif
67160814Ssimon#ifndef OPENSSL_NO_DSA
68160814Ssimon#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,
73238405Sjkim			void *exarg)
74160814Ssimon	{
75160814Ssimon	if (operation == ASN1_OP_FREE_POST)
76160814Ssimon		{
77109998Smarkm		X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
78109998Smarkm		EVP_PKEY_free(pubkey->pkey);
79160814Ssimon		}
80160814Ssimon	return 1;
8155714Skris	}
8255714Skris
83109998SmarkmASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
84109998Smarkm	ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
85109998Smarkm	ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
86109998Smarkm} ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
8755714Skris
88109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
8955714Skris
9055714Skrisint X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
9155714Skris	{
92160814Ssimon	X509_PUBKEY *pk=NULL;
9355714Skris
9455714Skris	if (x == NULL) return(0);
9555714Skris
96238405Sjkim	if ((pk=X509_PUBKEY_new()) == NULL) goto error;
9755714Skris
98238405Sjkim	if (pkey->ameth)
9955714Skris		{
100238405Sjkim		if (pkey->ameth->pub_encode)
10155714Skris			{
102238405Sjkim			if (!pkey->ameth->pub_encode(pk, pkey))
103160814Ssimon				{
104238405Sjkim				X509err(X509_F_X509_PUBKEY_SET,
105238405Sjkim					X509_R_PUBLIC_KEY_ENCODE_ERROR);
106238405Sjkim				goto error;
107160814Ssimon				}
10855714Skris			}
109238405Sjkim		else
110160814Ssimon			{
111238405Sjkim			X509err(X509_F_X509_PUBKEY_SET,
112238405Sjkim				X509_R_METHOD_NOT_SUPPORTED);
113238405Sjkim			goto error;
114160814Ssimon			}
11555714Skris		}
116238405Sjkim	else
11755714Skris		{
11855714Skris		X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
119238405Sjkim		goto error;
12055714Skris		}
12155714Skris
12255714Skris	if (*x != NULL)
12355714Skris		X509_PUBKEY_free(*x);
12455714Skris
12555714Skris	*x=pk;
12655714Skris
127160814Ssimon	return 1;
128238405Sjkimerror:
12955714Skris	if (pk != NULL) X509_PUBKEY_free(pk);
130160814Ssimon	return 0;
13155714Skris	}
13255714Skris
13355714SkrisEVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
13455714Skris	{
13555714Skris	EVP_PKEY *ret=NULL;
13655714Skris
137238405Sjkim	if (key == NULL) goto error;
13855714Skris
13955714Skris	if (key->pkey != NULL)
140160814Ssimon		{
141160814Ssimon		CRYPTO_add(&key->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
142238405Sjkim		return key->pkey;
143160814Ssimon		}
14455714Skris
145238405Sjkim	if (key->public_key == NULL) goto error;
14655714Skris
147160814Ssimon	if ((ret = EVP_PKEY_new()) == NULL)
14855714Skris		{
149160814Ssimon		X509err(X509_F_X509_PUBKEY_GET, ERR_R_MALLOC_FAILURE);
150238405Sjkim		goto error;
15155714Skris		}
15255714Skris
153238405Sjkim	if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm)))
15455714Skris		{
155238405Sjkim		X509err(X509_F_X509_PUBKEY_GET,X509_R_UNSUPPORTED_ALGORITHM);
156238405Sjkim		goto error;
15755714Skris		}
158238405Sjkim
159238405Sjkim	if (ret->ameth->pub_decode)
160160814Ssimon		{
161238405Sjkim		if (!ret->ameth->pub_decode(ret, key))
162160814Ssimon			{
163238405Sjkim			X509err(X509_F_X509_PUBKEY_GET,
164238405Sjkim						X509_R_PUBLIC_KEY_DECODE_ERROR);
165238405Sjkim			goto error;
166160814Ssimon			}
167160814Ssimon		}
168238405Sjkim	else
169160814Ssimon		{
170238405Sjkim		X509err(X509_F_X509_PUBKEY_GET, X509_R_METHOD_NOT_SUPPORTED);
171238405Sjkim		goto error;
172160814Ssimon		}
173160814Ssimon
174237657Sjkim	/* Check to see if another thread set key->pkey first */
175237657Sjkim	CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
176237657Sjkim	if (key->pkey)
177237657Sjkim		{
178246772Sjkim		CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
179237657Sjkim		EVP_PKEY_free(ret);
180237657Sjkim		ret = key->pkey;
181237657Sjkim		}
182237657Sjkim	else
183246772Sjkim		{
184237657Sjkim		key->pkey = ret;
185246772Sjkim		CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
186246772Sjkim		}
187160814Ssimon	CRYPTO_add(&ret->references, 1, CRYPTO_LOCK_EVP_PKEY);
188238405Sjkim
189238405Sjkim	return ret;
190238405Sjkim
191238405Sjkim	error:
19255714Skris	if (ret != NULL)
19355714Skris		EVP_PKEY_free(ret);
19455714Skris	return(NULL);
19555714Skris	}
19655714Skris
19759191Skris/* Now two pseudo ASN1 routines that take an EVP_PKEY structure
19859191Skris * and encode or decode as X509_PUBKEY
19959191Skris */
20059191Skris
201160814SsimonEVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp,
20259191Skris	     long length)
203160814Ssimon	{
20459191Skris	X509_PUBKEY *xpk;
20559191Skris	EVP_PKEY *pktmp;
20659191Skris	xpk = d2i_X509_PUBKEY(NULL, pp, length);
20759191Skris	if(!xpk) return NULL;
20859191Skris	pktmp = X509_PUBKEY_get(xpk);
20959191Skris	X509_PUBKEY_free(xpk);
21059191Skris	if(!pktmp) return NULL;
211160814Ssimon	if(a)
212160814Ssimon		{
21359191Skris		EVP_PKEY_free(*a);
21459191Skris		*a = pktmp;
215160814Ssimon		}
216160814Ssimon	return pktmp;
21759191Skris	}
21859191Skris
21959191Skrisint i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
220160814Ssimon	{
22159191Skris	X509_PUBKEY *xpk=NULL;
22259191Skris	int ret;
22359191Skris	if(!a) return 0;
22459191Skris	if(!X509_PUBKEY_set(&xpk, a)) return 0;
22559191Skris	ret = i2d_X509_PUBKEY(xpk, pp);
22659191Skris	X509_PUBKEY_free(xpk);
22759191Skris	return ret;
228160814Ssimon	}
22959191Skris
23059191Skris/* The following are equivalents but which return RSA and DSA
23159191Skris * keys
23259191Skris */
233109998Smarkm#ifndef OPENSSL_NO_RSA
234160814SsimonRSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp,
23559191Skris	     long length)
236160814Ssimon	{
23759191Skris	EVP_PKEY *pkey;
23859191Skris	RSA *key;
239160814Ssimon	const unsigned char *q;
24059191Skris	q = *pp;
24159191Skris	pkey = d2i_PUBKEY(NULL, &q, length);
242160814Ssimon	if (!pkey) return NULL;
24359191Skris	key = EVP_PKEY_get1_RSA(pkey);
24459191Skris	EVP_PKEY_free(pkey);
245160814Ssimon	if (!key) return NULL;
24659191Skris	*pp = q;
247160814Ssimon	if (a)
248160814Ssimon		{
24959191Skris		RSA_free(*a);
25059191Skris		*a = key;
251160814Ssimon		}
252160814Ssimon	return key;
25359191Skris	}
25459191Skris
25559191Skrisint i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
256160814Ssimon	{
25759191Skris	EVP_PKEY *pktmp;
25859191Skris	int ret;
259160814Ssimon	if (!a) return 0;
26059191Skris	pktmp = EVP_PKEY_new();
261160814Ssimon	if (!pktmp)
262160814Ssimon		{
26359191Skris		ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
26459191Skris		return 0;
265160814Ssimon		}
26659191Skris	EVP_PKEY_set1_RSA(pktmp, a);
26759191Skris	ret = i2d_PUBKEY(pktmp, pp);
26859191Skris	EVP_PKEY_free(pktmp);
26959191Skris	return ret;
270160814Ssimon	}
27159191Skris#endif
27259191Skris
273109998Smarkm#ifndef OPENSSL_NO_DSA
274160814SsimonDSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp,
27559191Skris	     long length)
276160814Ssimon	{
27759191Skris	EVP_PKEY *pkey;
27859191Skris	DSA *key;
279160814Ssimon	const unsigned char *q;
28059191Skris	q = *pp;
28159191Skris	pkey = d2i_PUBKEY(NULL, &q, length);
282160814Ssimon	if (!pkey) return NULL;
28359191Skris	key = EVP_PKEY_get1_DSA(pkey);
28459191Skris	EVP_PKEY_free(pkey);
285160814Ssimon	if (!key) return NULL;
28659191Skris	*pp = q;
287160814Ssimon	if (a)
288160814Ssimon		{
28959191Skris		DSA_free(*a);
29059191Skris		*a = key;
291160814Ssimon		}
292160814Ssimon	return key;
29359191Skris	}
29459191Skris
29559191Skrisint i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
296160814Ssimon	{
29759191Skris	EVP_PKEY *pktmp;
29859191Skris	int ret;
29959191Skris	if(!a) return 0;
30059191Skris	pktmp = EVP_PKEY_new();
301160814Ssimon	if(!pktmp)
302160814Ssimon		{
30359191Skris		ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
30459191Skris		return 0;
305160814Ssimon		}
30659191Skris	EVP_PKEY_set1_DSA(pktmp, a);
30759191Skris	ret = i2d_PUBKEY(pktmp, pp);
30859191Skris	EVP_PKEY_free(pktmp);
30959191Skris	return ret;
310160814Ssimon	}
31159191Skris#endif
312160814Ssimon
313160814Ssimon#ifndef OPENSSL_NO_EC
314160814SsimonEC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
315160814Ssimon	{
316160814Ssimon	EVP_PKEY *pkey;
317160814Ssimon	EC_KEY *key;
318160814Ssimon	const unsigned char *q;
319160814Ssimon	q = *pp;
320160814Ssimon	pkey = d2i_PUBKEY(NULL, &q, length);
321160814Ssimon	if (!pkey) return(NULL);
322160814Ssimon	key = EVP_PKEY_get1_EC_KEY(pkey);
323160814Ssimon	EVP_PKEY_free(pkey);
324160814Ssimon	if (!key)  return(NULL);
325160814Ssimon	*pp = q;
326160814Ssimon	if (a)
327160814Ssimon		{
328160814Ssimon		EC_KEY_free(*a);
329160814Ssimon		*a = key;
330160814Ssimon		}
331160814Ssimon	return(key);
332160814Ssimon	}
333160814Ssimon
334160814Ssimonint i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
335160814Ssimon	{
336160814Ssimon	EVP_PKEY *pktmp;
337160814Ssimon	int ret;
338160814Ssimon	if (!a)	return(0);
339160814Ssimon	if ((pktmp = EVP_PKEY_new()) == NULL)
340160814Ssimon		{
341160814Ssimon		ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
342160814Ssimon		return(0);
343160814Ssimon		}
344160814Ssimon	EVP_PKEY_set1_EC_KEY(pktmp, a);
345160814Ssimon	ret = i2d_PUBKEY(pktmp, pp);
346160814Ssimon	EVP_PKEY_free(pktmp);
347160814Ssimon	return(ret);
348160814Ssimon	}
349160814Ssimon#endif
350238405Sjkim
351238405Sjkimint X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
352238405Sjkim					int ptype, void *pval,
353238405Sjkim					unsigned char *penc, int penclen)
354238405Sjkim	{
355238405Sjkim	if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
356238405Sjkim		return 0;
357238405Sjkim	if (penc)
358238405Sjkim		{
359238405Sjkim		if (pub->public_key->data)
360238405Sjkim			OPENSSL_free(pub->public_key->data);
361238405Sjkim		pub->public_key->data = penc;
362238405Sjkim		pub->public_key->length = penclen;
363238405Sjkim  		/* Set number of unused bits to zero */
364238405Sjkim		pub->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
365238405Sjkim		pub->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;
366238405Sjkim		}
367238405Sjkim	return 1;
368238405Sjkim	}
369238405Sjkim
370238405Sjkimint X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
371238405Sjkim		const unsigned char **pk, int *ppklen,
372238405Sjkim		X509_ALGOR **pa,
373238405Sjkim		X509_PUBKEY *pub)
374238405Sjkim	{
375238405Sjkim	if (ppkalg)
376238405Sjkim		*ppkalg = pub->algor->algorithm;
377238405Sjkim	if (pk)
378238405Sjkim		{
379238405Sjkim		*pk = pub->public_key->data;
380238405Sjkim		*ppklen = pub->public_key->length;
381238405Sjkim		}
382238405Sjkim	if (pa)
383238405Sjkim		*pa = pub->algor;
384238405Sjkim	return 1;
385238405Sjkim	}
386