Path: blob/main/crypto/openssl/providers/implementations/encode_decode/decode_pem2der.c
48383 views
/*1* Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/*10* RSA low level APIs are deprecated for public use, but still ok for11* internal use.12*/13#include "internal/deprecated.h"1415#include <string.h>1617#include <openssl/core_dispatch.h>18#include <openssl/core_names.h>19#include <openssl/core_object.h>20#include <openssl/crypto.h>21#include <openssl/err.h>22#include <openssl/params.h>23#include <openssl/pem.h>24#include <openssl/proverr.h>25#include "internal/nelem.h"26#include "internal/sizes.h"27#include "prov/bio.h"28#include "prov/decoders.h"29#include "prov/implementations.h"30#include "endecoder_local.h"3132static int read_pem(PROV_CTX *provctx, OSSL_CORE_BIO *cin,33char **pem_name, char **pem_header,34unsigned char **data, long *len)35{36BIO *in = ossl_bio_new_from_core_bio(provctx, cin);37int ok;3839if (in == NULL)40return 0;41ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);4243BIO_free(in);44return ok;45}4647static OSSL_FUNC_decoder_newctx_fn pem2der_newctx;48static OSSL_FUNC_decoder_freectx_fn pem2der_freectx;49static OSSL_FUNC_decoder_decode_fn pem2der_decode;5051/*52* Context used for PEM to DER decoding.53*/54struct pem2der_ctx_st {55PROV_CTX *provctx;56char data_structure[OSSL_MAX_CODEC_STRUCT_SIZE];57char propq[OSSL_MAX_PROPQUERY_SIZE];58};5960static void *pem2der_newctx(void *provctx)61{62struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));6364if (ctx != NULL)65ctx->provctx = provctx;66return ctx;67}6869static void pem2der_freectx(void *vctx)70{71struct pem2der_ctx_st *ctx = vctx;7273OPENSSL_free(ctx);74}7576static const OSSL_PARAM *pem2der_settable_ctx_params(ossl_unused void *provctx)77{78static const OSSL_PARAM settables[] = {79OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0),80OSSL_PARAM_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, NULL, 0),81OSSL_PARAM_END82};83return settables;84}8586static int pem2der_set_ctx_params(void *vctx, const OSSL_PARAM params[])87{88struct pem2der_ctx_st *ctx = vctx;89const OSSL_PARAM *p;90char *str;9192p = OSSL_PARAM_locate_const(params, OSSL_DECODER_PARAM_PROPERTIES);93str = ctx->propq;94if (p != NULL95&& !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->propq)))96return 0;9798p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_STRUCTURE);99str = ctx->data_structure;100if (p != NULL101&& !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->data_structure)))102return 0;103104return 1;105}106107/* pem_password_cb compatible function */108struct pem2der_pass_data_st {109OSSL_PASSPHRASE_CALLBACK *cb;110void *cbarg;111};112113static int pem2der_pass_helper(char *buf, int num, int w, void *data)114{115struct pem2der_pass_data_st *pass_data = data;116size_t plen;117118if (pass_data == NULL119|| pass_data->cb == NULL120|| !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))121return -1;122return (int)plen;123}124125static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,126OSSL_CALLBACK *data_cb, void *data_cbarg,127OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)128{129/*130* PEM names we recognise. Other PEM names should be recognised by131* other decoder implementations.132*/133static struct pem_name_map_st {134const char *pem_name;135int object_type;136const char *data_type;137const char *data_structure;138} pem_name_map[] = {139/* PKCS#8 and SubjectPublicKeyInfo */140{ PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" },141{ PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" },142#define PKCS8_LAST_IDX 1143{ PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" },144#define SPKI_LAST_IDX 2145/* Our set of type specific PEM types */146{ PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" },147{ PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" },148{ PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" },149{ PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" },150{ PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" },151{ PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" },152{ PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" },153{ PEM_STRING_SM2PRIVATEKEY, OSSL_OBJECT_PKEY, "SM2", "type-specific" },154{ PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" },155{ PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" },156{ PEM_STRING_RSA_PUBLIC, OSSL_OBJECT_PKEY, "RSA", "type-specific" },157158/*159* A few others that there is at least have an object type for, even160* though there is no provider interface to handle such objects, yet.161* However, this is beneficial for the OSSL_STORE result handler.162*/163{ PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" },164{ PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" },165{ PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" },166{ PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" }167};168struct pem2der_ctx_st *ctx = vctx;169char *pem_name = NULL, *pem_header = NULL;170size_t i;171unsigned char *der = NULL;172long der_len = 0;173int ok = 0;174int objtype = OSSL_OBJECT_UNKNOWN;175176ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,177&der, &der_len) > 0;178/* We return "empty handed". This is not an error. */179if (!ok)180return 1;181182/*183* 10 is the number of characters in "Proc-Type:", which184* PEM_get_EVP_CIPHER_INFO() requires to be present.185* If the PEM header has less characters than that, it's186* not worth spending cycles on it.187*/188if (strlen(pem_header) > 10) {189EVP_CIPHER_INFO cipher;190struct pem2der_pass_data_st pass_data;191192ok = 0; /* Assume that we fail */193pass_data.cb = pw_cb;194pass_data.cbarg = pw_cbarg;195if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)196|| !PEM_do_header(&cipher, der, &der_len,197pem2der_pass_helper, &pass_data))198goto end;199}200201/*202* Indicated that we successfully decoded something, or not at all.203* Ending up "empty handed" is not an error.204*/205ok = 1;206207/* Have a look to see if we recognise anything */208for (i = 0; i < OSSL_NELEM(pem_name_map); i++)209if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)210break;211212if (i < OSSL_NELEM(pem_name_map)) {213OSSL_PARAM params[5], *p = params;214/* We expect these to be read only so casting away the const is ok */215char *data_type = (char *)pem_name_map[i].data_type;216char *data_structure = (char *)pem_name_map[i].data_structure;217218/*219* Since this may perform decryption, we need to check the selection to220* avoid password prompts for objects of no interest.221*/222if (i <= PKCS8_LAST_IDX223&& ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)224|| OPENSSL_strcasecmp(ctx->data_structure, "EncryptedPrivateKeyInfo") == 0225|| OPENSSL_strcasecmp(ctx->data_structure, "PrivateKeyInfo") == 0)) {226ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb,227data_cbarg, pw_cb, pw_cbarg,228PROV_LIBCTX_OF(ctx->provctx),229ctx->propq);230goto end;231}232233if (i <= SPKI_LAST_IDX234&& ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)235|| OPENSSL_strcasecmp(ctx->data_structure, "SubjectPublicKeyInfo") == 0)) {236ok = ossl_spki2typespki_der_decode(der, der_len, selection, data_cb,237data_cbarg, pw_cb, pw_cbarg,238PROV_LIBCTX_OF(ctx->provctx),239ctx->propq);240goto end;241}242243objtype = pem_name_map[i].object_type;244if (data_type != NULL)245*p++ =246OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,247data_type, 0);248249/* We expect this to be read only so casting away the const is ok */250if (data_structure != NULL)251*p++ =252OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,253data_structure, 0);254*p++ =255OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,256der, der_len);257*p++ =258OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);259260*p = OSSL_PARAM_construct_end();261262ok = data_cb(params, data_cbarg);263}264265end:266OPENSSL_free(pem_name);267OPENSSL_free(pem_header);268OPENSSL_free(der);269return ok;270}271272const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = {273{ OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx },274{ OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx },275{ OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode },276{ OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,277(void (*)(void))pem2der_settable_ctx_params },278{ OSSL_FUNC_DECODER_SET_CTX_PARAMS,279(void (*)(void))pem2der_set_ctx_params },280OSSL_DISPATCH_END281};282283284