d2i_pu.c revision 238405
1123123Simp/* crypto/asn1/d2i_pu.c */
2123123Simp/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3123123Simp * All rights reserved.
4123123Simp *
5123123Simp * This package is an SSL implementation written
6123123Simp * by Eric Young (eay@cryptsoft.com).
7155064Srik * The implementation was written so as to conform with Netscapes SSL.
8123123Simp *
9123123Simp * This library is free for commercial and non-commercial use as long as
10123123Simp * the following conditions are aheared to.  The following conditions
11123123Simp * apply to all code found in this distribution, be it the RC4, RSA,
12123123Simp * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13123123Simp * included with this distribution is covered by the same copyright terms
14123123Simp * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15123123Simp *
16123123Simp * Copyright remains Eric Young's, and as such any Copyright notices in
17155064Srik * the code are not to be removed.
18123123Simp * If this package is used in a product, Eric Young should be given attribution
19162253Scharnier * as the author of the parts of the library used.
20162253Scharnier * This can be in the form of a textual message at program startup or
21162253Scharnier * in documentation (online or textual) provided with the package.
22162253Scharnier *
23123123Simp * Redistribution and use in source and binary forms, with or without
24123123Simp * modification, are permitted provided that the following conditions
25123123Simp * are met:
26123123Simp * 1. Redistributions of source code must retain the copyright
27123123Simp *    notice, this list of conditions and the following disclaimer.
28123123Simp * 2. Redistributions in binary form must reproduce the above copyright
29123123Simp *    notice, this list of conditions and the following disclaimer in the
30123123Simp *    documentation and/or other materials provided with the distribution.
31123123Simp * 3. All advertising materials mentioning features or use of this software
32123123Simp *    must display the following acknowledgement:
33123123Simp *    "This product includes cryptographic software written by
34123123Simp *     Eric Young (eay@cryptsoft.com)"
35123123Simp *    The word 'cryptographic' can be left out if the rouines from the library
36123123Simp *    being used are not cryptographic related :-).
37123123Simp * 4. If you include any Windows specific code (or a derivative thereof) from
38123123Simp *    the apps directory (application code) you must include an acknowledgement:
39123123Simp *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40123123Simp *
41155064Srik * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42155064Srik * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43123123Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44123123Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45123123Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46123123Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47123123Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48123123Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49123123Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50155064Srik * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51123123Simp * SUCH DAMAGE.
52123123Simp *
53123123Simp * The licence and distribution terms for any publically available version or
54123123Simp * derivative of this code cannot be changed.  i.e. this code cannot simply be
55123123Simp * copied and put under another distribution licence
56123123Simp * [including the GNU Public Licence.]
57123123Simp */
58123123Simp
59123123Simp#include <stdio.h>
60123123Simp#include "cryptlib.h"
61123123Simp#include <openssl/bn.h>
62123123Simp#include <openssl/evp.h>
63123123Simp#include <openssl/objects.h>
64123123Simp#include <openssl/asn1.h>
65123123Simp#ifndef OPENSSL_NO_RSA
66123123Simp#include <openssl/rsa.h>
67123123Simp#endif
68123123Simp#ifndef OPENSSL_NO_DSA
69123123Simp#include <openssl/dsa.h>
70123123Simp#endif
71123123Simp#ifndef OPENSSL_NO_EC
72123123Simp#include <openssl/ec.h>
73123123Simp#endif
74123123Simp
75123123SimpEVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
76123123Simp	     long length)
77123123Simp	{
78123123Simp	EVP_PKEY *ret;
79123123Simp
80123123Simp	if ((a == NULL) || (*a == NULL))
81123123Simp		{
82123123Simp		if ((ret=EVP_PKEY_new()) == NULL)
83123123Simp			{
84123123Simp			ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB);
85123123Simp			return(NULL);
86123123Simp			}
87123123Simp		}
88123123Simp	else	ret= *a;
89123123Simp
90123123Simp	if (!EVP_PKEY_set_type(ret, type))
91123123Simp		{
92123123Simp		ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB);
93123123Simp		goto err;
94123123Simp		}
95123123Simp
96123123Simp	switch (EVP_PKEY_id(ret))
97123123Simp		{
98123123Simp#ifndef OPENSSL_NO_RSA
99123123Simp	case EVP_PKEY_RSA:
100123123Simp		if ((ret->pkey.rsa=d2i_RSAPublicKey(NULL,
101123123Simp			(const unsigned char **)pp,length)) == NULL) /* TMP UGLY CAST */
102123123Simp			{
103123123Simp			ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
104123123Simp			goto err;
105123123Simp			}
106155064Srik		break;
107155064Srik#endif
108155064Srik#ifndef OPENSSL_NO_DSA
109155064Srik	case EVP_PKEY_DSA:
110123123Simp		if (!d2i_DSAPublicKey(&(ret->pkey.dsa),
111123123Simp			(const unsigned char **)pp,length)) /* TMP UGLY CAST */
112123123Simp			{
113123123Simp			ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
114155064Srik			goto err;
115123123Simp			}
116123123Simp		break;
117123123Simp#endif
118123123Simp#ifndef OPENSSL_NO_EC
119123123Simp	case EVP_PKEY_EC:
120123123Simp		if (!o2i_ECPublicKey(&(ret->pkey.ec),
121123123Simp				     (const unsigned char **)pp, length))
122123123Simp			{
123123123Simp			ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
124123123Simp			goto err;
125123123Simp			}
126123123Simp	break;
127123123Simp#endif
128123123Simp	default:
129123123Simp		ASN1err(ASN1_F_D2I_PUBLICKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
130123123Simp		goto err;
131123123Simp		/* break; */
132123123Simp		}
133123123Simp	if (a != NULL) (*a)=ret;
134123123Simp	return(ret);
135123123Simperr:
136123123Simp	if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);
137123123Simp	return(NULL);
138123123Simp	}
139123123Simp
140123123Simp