Path: blob/main/crypto/openssl/providers/implementations/encode_decode/decode_pem2der.c
103857 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)178> 0;179/* We return "empty handed". This is not an error. */180if (!ok)181return 1;182183/*184* 10 is the number of characters in "Proc-Type:", which185* PEM_get_EVP_CIPHER_INFO() requires to be present.186* If the PEM header has less characters than that, it's187* not worth spending cycles on it.188*/189if (strlen(pem_header) > 10) {190EVP_CIPHER_INFO cipher;191struct pem2der_pass_data_st pass_data;192193ok = 0; /* Assume that we fail */194pass_data.cb = pw_cb;195pass_data.cbarg = pw_cbarg;196if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)197|| !PEM_do_header(&cipher, der, &der_len,198pem2der_pass_helper, &pass_data))199goto end;200}201202/*203* Indicated that we successfully decoded something, or not at all.204* Ending up "empty handed" is not an error.205*/206ok = 1;207208/* Have a look to see if we recognise anything */209for (i = 0; i < OSSL_NELEM(pem_name_map); i++)210if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)211break;212213if (i < OSSL_NELEM(pem_name_map)) {214OSSL_PARAM params[5], *p = params;215/* We expect these to be read only so casting away the const is ok */216char *data_type = (char *)pem_name_map[i].data_type;217char *data_structure = (char *)pem_name_map[i].data_structure;218219/*220* Since this may perform decryption, we need to check the selection to221* avoid password prompts for objects of no interest.222*/223if (i <= PKCS8_LAST_IDX224&& ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY)225|| OPENSSL_strcasecmp(ctx->data_structure, "EncryptedPrivateKeyInfo") == 0226|| OPENSSL_strcasecmp(ctx->data_structure, "PrivateKeyInfo") == 0)) {227ok = ossl_epki2pki_der_decode(der, der_len, selection, data_cb,228data_cbarg, pw_cb, pw_cbarg,229PROV_LIBCTX_OF(ctx->provctx),230ctx->propq);231goto end;232}233234if (i <= SPKI_LAST_IDX235&& ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY)236|| OPENSSL_strcasecmp(ctx->data_structure, "SubjectPublicKeyInfo") == 0)) {237ok = ossl_spki2typespki_der_decode(der, der_len, selection, data_cb,238data_cbarg, pw_cb, pw_cbarg,239PROV_LIBCTX_OF(ctx->provctx),240ctx->propq);241goto end;242}243244objtype = pem_name_map[i].object_type;245if (data_type != NULL)246*p++ = OSSL_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++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,252data_structure, 0);253*p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,254der, der_len);255*p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);256257*p = OSSL_PARAM_construct_end();258259ok = data_cb(params, data_cbarg);260}261262end:263OPENSSL_free(pem_name);264OPENSSL_free(pem_header);265OPENSSL_free(der);266return ok;267}268269const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = {270{ OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx },271{ OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx },272{ OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode },273{ OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,274(void (*)(void))pem2der_settable_ctx_params },275{ OSSL_FUNC_DECODER_SET_CTX_PARAMS,276(void (*)(void))pem2der_set_ctx_params },277OSSL_DISPATCH_END278};279280281