blob: 6d5dd1bc603eb30597f1b86ac663662531cd8b75 [file] [log] [blame]
Rich Salz2039c422016-05-17 14:51:34 -04001/*
Matt Caswell38fc02a2021-06-17 13:24:59 +01002 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 *
Richard Levitte365a2d92018-12-06 13:17:34 +01004 * Licensed under the Apache License 2.0 (the "License"). You may not use
Rich Salz2039c422016-05-17 14:51:34 -04005 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00008 */
9
Paulif41ac0e2020-01-30 07:23:39 +100010/*
11 * DSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000016#include <stdio.h>
Richard Levitteb39fc562015-05-14 16:56:48 +020017#include "internal/cryptlib.h"
Bodo Möllerec577821999-04-23 22:13:45 +000018#include <openssl/bn.h>
19#include <openssl/evp.h>
20#include <openssl/objects.h>
Bodo Möllerf66c3031999-07-24 03:09:01 +000021#include <openssl/asn1.h>
Rich Salz3c272082016-03-18 14:30:20 -040022#include <openssl/rsa.h>
23#include <openssl/dsa.h>
24#include <openssl/ec.h>
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000025
Dr. Matthias St. Pierre25f21382019-09-28 00:45:33 +020026#include "crypto/evp.h"
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +000027
Richard Levitte875a6442004-03-15 23:15:26 +000028EVP_PKEY *d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp,
Matt Caswell0f113f32015-01-22 03:40:55 +000029 long length)
30{
31 EVP_PKEY *ret;
Tomas Mraz615a9b82021-11-18 20:09:57 +010032 EVP_PKEY *copy = NULL;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000033
Matt Caswell0f113f32015-01-22 03:40:55 +000034 if ((a == NULL) || (*a == NULL)) {
35 if ((ret = EVP_PKEY_new()) == NULL) {
Richard Levitte9311d0c2020-11-04 12:23:19 +010036 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
KaoruToda26a7d932017-10-17 23:04:09 +090037 return NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000038 }
Tomas Mraz615a9b82021-11-18 20:09:57 +010039 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +000040 ret = *a;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000041
Tomas Mraz615a9b82021-11-18 20:09:57 +010042#ifndef OPENSSL_NO_EC
43 if (evp_pkey_is_provided(ret)
44 && EVP_PKEY_get_base_id(ret) == EVP_PKEY_EC) {
45 if (!evp_pkey_copy_downgraded(&copy, ret))
46 goto err;
47 }
48#endif
49 }
50
51 if ((type != EVP_PKEY_get_id(ret) || copy != NULL)
52 && !EVP_PKEY_set_type(ret, type)) {
Richard Levitte9311d0c2020-11-04 12:23:19 +010053 ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +000054 goto err;
55 }
Dr. Stephen Hensonc18e51b2009-11-12 19:56:56 +000056
zhaozg8582dcc2022-01-01 22:45:12 +080057 switch (EVP_PKEY_get_base_id(ret)) {
Matt Caswell0f113f32015-01-22 03:40:55 +000058 case EVP_PKEY_RSA:
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +000059 if ((ret->pkey.rsa = d2i_RSAPublicKey(NULL, pp, length)) == NULL) {
Richard Levitte9311d0c2020-11-04 12:23:19 +010060 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +000061 goto err;
62 }
63 break;
Richard Levittecf1b7d92001-02-19 16:06:34 +000064#ifndef OPENSSL_NO_DSA
Matt Caswell0f113f32015-01-22 03:40:55 +000065 case EVP_PKEY_DSA:
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +000066 if (!d2i_DSAPublicKey(&ret->pkey.dsa, pp, length)) {
Richard Levitte9311d0c2020-11-04 12:23:19 +010067 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +000068 goto err;
69 }
70 break;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000071#endif
Bodo Möller14a7cfb2002-08-07 10:49:54 +000072#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +000073 case EVP_PKEY_EC:
Tomas Mraz615a9b82021-11-18 20:09:57 +010074 if (copy != NULL) {
75 /* use downgraded parameters from copy */
76 ret->pkey.ec = copy->pkey.ec;
77 copy->pkey.ec = NULL;
78 }
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +000079 if (!o2i_ECPublicKey(&ret->pkey.ec, pp, length)) {
Richard Levitte9311d0c2020-11-04 12:23:19 +010080 ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
Matt Caswell0f113f32015-01-22 03:40:55 +000081 goto err;
82 }
83 break;
Bodo Möller4d94ae02002-02-13 18:21:51 +000084#endif
Matt Caswell0f113f32015-01-22 03:40:55 +000085 default:
Richard Levitte9311d0c2020-11-04 12:23:19 +010086 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
Matt Caswell0f113f32015-01-22 03:40:55 +000087 goto err;
Matt Caswell0f113f32015-01-22 03:40:55 +000088 }
89 if (a != NULL)
90 (*a) = ret;
Tomas Mraz615a9b82021-11-18 20:09:57 +010091 EVP_PKEY_free(copy);
KaoruToda26a7d932017-10-17 23:04:09 +090092 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +000093 err:
Rich Salzc5ba2d92015-03-28 10:54:15 -040094 if (a == NULL || *a != ret)
Matt Caswell0f113f32015-01-22 03:40:55 +000095 EVP_PKEY_free(ret);
Tomas Mraz615a9b82021-11-18 20:09:57 +010096 EVP_PKEY_free(copy);
KaoruToda26a7d932017-10-17 23:04:09 +090097 return NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +000098}