2016-05-18 02:51:34 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2016-05-18 02:51:34 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
1999-07-24 11:09:01 +08:00
|
|
|
#include <openssl/asn1.h>
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/dsa.h>
|
|
|
|
#include <openssl/ec.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-01-19 08:21:12 +08:00
|
|
|
#include "internal/evp_int.h"
|
|
|
|
|
2004-03-16 07:15:26 +08:00
|
|
|
EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
|
2015-01-22 11:40:55 +08:00
|
|
|
long length)
|
|
|
|
{
|
|
|
|
EVP_PKEY *ret;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((a == NULL) || (*a == NULL)) {
|
|
|
|
if ((ret = EVP_PKEY_new()) == NULL) {
|
|
|
|
ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_EVP_LIB);
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
ret = *a;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!EVP_PKEY_set_type(ret, type)) {
|
|
|
|
ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_EVP_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
2009-11-13 03:56:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
switch (EVP_PKEY_id(ret)) {
|
2001-02-20 00:06:34 +08:00
|
|
|
#ifndef OPENSSL_NO_RSA
|
2015-01-22 11:40:55 +08:00
|
|
|
case EVP_PKEY_RSA:
|
2016-01-19 08:21:12 +08:00
|
|
|
if ((ret->pkey.rsa = d2i_RSAPublicKey(NULL, pp, length)) == NULL) {
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
1998-12-21 18:52:47 +08:00
|
|
|
#endif
|
2001-02-20 00:06:34 +08:00
|
|
|
#ifndef OPENSSL_NO_DSA
|
2015-01-22 11:40:55 +08:00
|
|
|
case EVP_PKEY_DSA:
|
|
|
|
/* TMP UGLY CAST */
|
2016-01-19 08:21:12 +08:00
|
|
|
if (!d2i_DSAPublicKey(&ret->pkey.dsa, pp, length)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
2002-02-14 02:21:51 +08:00
|
|
|
#endif
|
2002-08-07 18:49:54 +08:00
|
|
|
#ifndef OPENSSL_NO_EC
|
2015-01-22 11:40:55 +08:00
|
|
|
case EVP_PKEY_EC:
|
2016-01-19 08:21:12 +08:00
|
|
|
if (!o2i_ECPublicKey(&ret->pkey.ec, pp, length)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1err(ASN1_F_D2I_PUBLICKEY, ERR_R_ASN1_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
break;
|
1998-12-21 18:52:47 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
default:
|
|
|
|
ASN1err(ASN1_F_D2I_PUBLICKEY, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (a != NULL)
|
|
|
|
(*a) = ret;
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
err:
|
2015-03-28 22:54:15 +08:00
|
|
|
if (a == NULL || *a != ret)
|
2015-01-22 11:40:55 +08:00
|
|
|
EVP_PKEY_free(ret);
|
2017-10-17 22:04:09 +08:00
|
|
|
return NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|