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