d2i_pu.c revision 109998
155682Smarkm/* crypto/asn1/d2i_pu.c */
255682Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355682Smarkm * All rights reserved.
455682Smarkm *
555682Smarkm * This package is an SSL implementation written
655682Smarkm * by Eric Young (eay@cryptsoft.com).
755682Smarkm * The implementation was written so as to conform with Netscapes SSL.
855682Smarkm *
955682Smarkm * This library is free for commercial and non-commercial use as long as
1055682Smarkm * the following conditions are aheared to.  The following conditions
1155682Smarkm * apply to all code found in this distribution, be it the RC4, RSA,
1255682Smarkm * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355682Smarkm * included with this distribution is covered by the same copyright terms
1455682Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555682Smarkm *
1655682Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in
1755682Smarkm * the code are not to be removed.
1855682Smarkm * If this package is used in a product, Eric Young should be given attribution
1955682Smarkm * as the author of the parts of the library used.
2055682Smarkm * This can be in the form of a textual message at program startup or
2155682Smarkm * in documentation (online or textual) provided with the package.
2255682Smarkm *
2355682Smarkm * Redistribution and use in source and binary forms, with or without
2455682Smarkm * modification, are permitted provided that the following conditions
2555682Smarkm * are met:
2655682Smarkm * 1. Redistributions of source code must retain the copyright
2755682Smarkm *    notice, this list of conditions and the following disclaimer.
2855682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
2955682Smarkm *    notice, this list of conditions and the following disclaimer in the
3055682Smarkm *    documentation and/or other materials provided with the distribution.
3155682Smarkm * 3. All advertising materials mentioning features or use of this software
3255682Smarkm *    must display the following acknowledgement:
3355682Smarkm *    "This product includes cryptographic software written by
3455682Smarkm *     Eric Young (eay@cryptsoft.com)"
3555682Smarkm *    The word 'cryptographic' can be left out if the rouines from the library
36178825Sdfr *    being used are not cryptographic related :-).
3755682Smarkm * 4. If you include any Windows specific code (or a derivative thereof) from
3855682Smarkm *    the apps directory (application code) you must include an acknowledgement:
3955682Smarkm *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055682Smarkm *
4155682Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42178825Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48178825Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155682Smarkm * SUCH DAMAGE.
5255682Smarkm *
5355682Smarkm * The licence and distribution terms for any publically available version or
5455682Smarkm * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555682Smarkm * copied and put under another distribution licence
5655682Smarkm * [including the GNU Public Licence.]
5755682Smarkm */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/evp.h>
63#include <openssl/objects.h>
64#include <openssl/asn1.h>
65#ifndef OPENSSL_NO_RSA
66#include <openssl/rsa.h>
67#endif
68#ifndef OPENSSL_NO_DSA
69#include <openssl/dsa.h>
70#endif
71
72EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, unsigned char **pp,
73	     long length)
74	{
75	EVP_PKEY *ret;
76
77	if ((a == NULL) || (*a == NULL))
78		{
79		if ((ret=EVP_PKEY_new()) == NULL)
80			{
81			ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_EVP_LIB);
82			return(NULL);
83			}
84		}
85	else	ret= *a;
86
87	ret->save_type=type;
88	ret->type=EVP_PKEY_type(type);
89	switch (ret->type)
90		{
91#ifndef OPENSSL_NO_RSA
92	case EVP_PKEY_RSA:
93		if ((ret->pkey.rsa=d2i_RSAPublicKey(NULL,
94			(const unsigned char **)pp,length)) == NULL) /* TMP UGLY CAST */
95			{
96			ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
97			goto err;
98			}
99		break;
100#endif
101#ifndef OPENSSL_NO_DSA
102	case EVP_PKEY_DSA:
103		if ((ret->pkey.dsa=d2i_DSAPublicKey(NULL,
104			(const unsigned char **)pp,length)) == NULL) /* TMP UGLY CAST */
105			{
106			ASN1err(ASN1_F_D2I_PUBLICKEY,ERR_R_ASN1_LIB);
107			goto err;
108			}
109		break;
110#endif
111	default:
112		ASN1err(ASN1_F_D2I_PUBLICKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
113		goto err;
114		/* break; */
115		}
116	if (a != NULL) (*a)=ret;
117	return(ret);
118err:
119	if ((ret != NULL) && ((a == NULL) || (*a != ret))) EVP_PKEY_free(ret);
120	return(NULL);
121	}
122
123