1 /* 2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * 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 8 */ 9 10 #include <stdio.h> 11 #include "internal/cryptlib.h" 12 #include <openssl/evp.h> 13 #include <openssl/asn1.h> 14 #include "internal/asn1.h" 15 #include "crypto/asn1.h" 16 #include "crypto/evp.h" 17 18 EVP_PKEY *d2i_KeyParams(int type, EVP_PKEY **a, const unsigned char **pp, 19 long length) 20 { 21 EVP_PKEY *ret = NULL; 22 23 if ((a == NULL) || (*a == NULL)) { 24 if ((ret = EVP_PKEY_new()) == NULL) 25 return NULL; 26 } else 27 ret = *a; 28 29 if (type != EVP_PKEY_get_id(ret) && !EVP_PKEY_set_type(ret, type)) 30 goto err; 31 32 if (ret->ameth == NULL || ret->ameth->param_decode == NULL) { 33 ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE); 34 goto err; 35 } 36 37 if (!ret->ameth->param_decode(ret, pp, length)) 38 goto err; 39 40 if (a != NULL) 41 (*a) = ret; 42 return ret; 43 err: 44 if (a == NULL || *a != ret) 45 EVP_PKEY_free(ret); 46 return NULL; 47 } 48 49 EVP_PKEY *d2i_KeyParams_bio(int type, EVP_PKEY **a, BIO *in) 50 { 51 BUF_MEM *b = NULL; 52 const unsigned char *p; 53 void *ret = NULL; 54 int len; 55 56 len = asn1_d2i_read_bio(in, &b); 57 if (len < 0) 58 goto err; 59 60 p = (unsigned char *)b->data; 61 ret = d2i_KeyParams(type, a, &p, len); 62 err: 63 BUF_MEM_free(b); 64 return ret; 65 } 66