PROV: Add MSBLOB and PVK encoders

This allows 15-test_rsa.t to succeed, and provides the same OSSL_ENCODER
support for these formats as for all other formats supported in OpenSSL.

Fixes #13379

Reviewed-by: Tomas Mraz <>
(Merged from
Richard Levitte 2 years ago
parent 2984445d3a
commit 0cc0164d19

@ -464,6 +464,8 @@ extern "C" {
#define OSSL_ENCODER_PARAM_INPUT_TYPE "input-type"
#define OSSL_ENCODER_PARAM_OUTPUT_TYPE "output-type"
#define OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE "output-structure"
/* Currently PVK only, but reusable for others as needed */
#define OSSL_ENCODER_PARAM_ENCRYPT_LEVEL "encrypt-level"
#define OSSL_DECODER_PARAM_INPUT_TYPE "input-type"

@ -32,7 +32,11 @@
{ _name, \
"provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=text", \
(ossl_##_sym##_to_text_encoder_functions) }
#define ENCODER(_name, _sym, _fips, _output, _structure) \
#define ENCODER(_name, _sym, _fips, _output) \
{ _name, \
"provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=" #_output, \
(ossl_##_sym##_to_##_output##_encoder_functions) }
#define ENCODER_w_structure(_name, _sym, _fips, _output, _structure) \
{ _name, \
"provider=" ENCODER_PROVIDER ",fips=" #_fips ",output=" #_output \
",structure=" ENCODER_STRUCTURE_##_structure, \
@ -83,23 +87,33 @@ ENCODER_TEXT("X448", x448, yes),
/* The RSA encoders only support private key and public key output */
ENCODER("RSA", rsa, yes, der, type_specific_keypair),
ENCODER("RSA", rsa, yes, pem, type_specific_keypair),
ENCODER_w_structure("RSA", rsa, yes, der, type_specific_keypair),
ENCODER_w_structure("RSA", rsa, yes, pem, type_specific_keypair),
/* DH and X9.42 DH only support key parameters output. */
ENCODER("DH", dh, yes, der, type_specific_params),
ENCODER("DH", dh, yes, pem, type_specific_params),
ENCODER("DHX", dhx, yes, der, type_specific_params),
ENCODER("DHX", dhx, yes, pem, type_specific_params),
ENCODER_w_structure("DH", dh, yes, der, type_specific_params),
ENCODER_w_structure("DH", dh, yes, pem, type_specific_params),
ENCODER_w_structure("DHX", dhx, yes, der, type_specific_params),
ENCODER_w_structure("DHX", dhx, yes, pem, type_specific_params),
ENCODER("DSA", dsa, yes, der, type_specific),
ENCODER("DSA", dsa, yes, pem, type_specific),
ENCODER_w_structure("DSA", dsa, yes, der, type_specific),
ENCODER_w_structure("DSA", dsa, yes, pem, type_specific),
/* EC only supports keypair and parameters output. */
ENCODER("EC", ec, yes, der, type_specific_no_pub),
ENCODER("EC", ec, yes, pem, type_specific_no_pub),
ENCODER_w_structure("EC", ec, yes, der, type_specific_no_pub),
ENCODER_w_structure("EC", ec, yes, pem, type_specific_no_pub),
* Entries for the output formats MSBLOB and PVK
ENCODER("RSA", rsa, yes, msblob),
ENCODER("RSA", rsa, yes, pvk),
ENCODER("DSA", dsa, yes, msblob),
ENCODER("DSA", dsa, yes, pvk),
@ -109,60 +123,60 @@ ENCODER("EC", ec, yes, pem, type_specific_no_pub),
* The "pem" ones also support PEM_write_bio_PrivateKey() and
* PEM_write_bio_PUBKEY().
ENCODER("RSA", rsa, yes, der, PKCS8),
ENCODER("RSA", rsa, yes, pem, PKCS8),
ENCODER("RSA", rsa, yes, der, SubjectPublicKeyInfo),
ENCODER("RSA", rsa, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("RSA", rsa, yes, der, PKCS8),
ENCODER_w_structure("RSA", rsa, yes, pem, PKCS8),
ENCODER_w_structure("RSA", rsa, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("RSA", rsa, yes, pem, SubjectPublicKeyInfo),
ENCODER("RSA-PSS", rsapss, yes, der, PKCS8),
ENCODER("RSA-PSS", rsapss, yes, pem, PKCS8),
ENCODER("RSA-PSS", rsapss, yes, der, SubjectPublicKeyInfo),
ENCODER("RSA-PSS", rsapss, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("RSA-PSS", rsapss, yes, der, PKCS8),
ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, PKCS8),
ENCODER_w_structure("RSA-PSS", rsapss, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, SubjectPublicKeyInfo),
ENCODER("DH", dh, yes, der, PKCS8),
ENCODER("DH", dh, yes, pem, PKCS8),
ENCODER("DH", dh, yes, der, SubjectPublicKeyInfo),
ENCODER("DH", dh, yes, pem, SubjectPublicKeyInfo),
ENCODER("DHX", dhx, yes, der, PKCS8),
ENCODER("DHX", dhx, yes, pem, PKCS8),
ENCODER("DHX", dhx, yes, der, SubjectPublicKeyInfo),
ENCODER("DHX", dhx, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("DH", dh, yes, der, PKCS8),
ENCODER_w_structure("DH", dh, yes, pem, PKCS8),
ENCODER_w_structure("DH", dh, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("DH", dh, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("DHX", dhx, yes, der, PKCS8),
ENCODER_w_structure("DHX", dhx, yes, pem, PKCS8),
ENCODER_w_structure("DHX", dhx, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("DHX", dhx, yes, pem, SubjectPublicKeyInfo),
ENCODER("DSA", dsa, yes, der, PKCS8),
ENCODER("DSA", dsa, yes, pem, PKCS8),
ENCODER("DSA", dsa, yes, der, SubjectPublicKeyInfo),
ENCODER("DSA", dsa, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("DSA", dsa, yes, der, PKCS8),
ENCODER_w_structure("DSA", dsa, yes, pem, PKCS8),
ENCODER_w_structure("DSA", dsa, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("DSA", dsa, yes, pem, SubjectPublicKeyInfo),
ENCODER("EC", ec, yes, der, PKCS8),
ENCODER("EC", ec, yes, pem, PKCS8),
ENCODER("EC", ec, yes, der, SubjectPublicKeyInfo),
ENCODER("EC", ec, yes, pem, SubjectPublicKeyInfo),
ENCODER("X25519", x25519, yes, der, PKCS8),
ENCODER("X25519", x25519, yes, pem, PKCS8),
ENCODER("X25519", x25519, yes, der, SubjectPublicKeyInfo),
ENCODER("X25519", x25519, yes, pem, SubjectPublicKeyInfo),
ENCODER("X448", x448, yes, der, PKCS8),
ENCODER("X448", x448, yes, pem, PKCS8),
ENCODER("X448", x448, yes, der, SubjectPublicKeyInfo),
ENCODER("X448", x448, yes, pem, SubjectPublicKeyInfo),
ENCODER("ED25519", ed25519, yes, der, PKCS8),
ENCODER("ED25519", ed25519, yes, pem, PKCS8),
ENCODER("ED25519", ed25519, yes, der, SubjectPublicKeyInfo),
ENCODER("ED25519", ed25519, yes, pem, SubjectPublicKeyInfo),
ENCODER("ED448", ed448, yes, der, PKCS8),
ENCODER("ED448", ed448, yes, pem, PKCS8),
ENCODER("ED448", ed448, yes, der, SubjectPublicKeyInfo),
ENCODER("ED448", ed448, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("EC", ec, yes, der, PKCS8),
ENCODER_w_structure("EC", ec, yes, pem, PKCS8),
ENCODER_w_structure("EC", ec, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("EC", ec, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("X25519", x25519, yes, der, PKCS8),
ENCODER_w_structure("X25519", x25519, yes, pem, PKCS8),
ENCODER_w_structure("X25519", x25519, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("X25519", x25519, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("X448", x448, yes, der, PKCS8),
ENCODER_w_structure("X448", x448, yes, pem, PKCS8),
ENCODER_w_structure("X448", x448, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("X448", x448, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("ED25519", ed25519, yes, der, PKCS8),
ENCODER_w_structure("ED25519", ed25519, yes, pem, PKCS8),
ENCODER_w_structure("ED25519", ed25519, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("ED25519", ed25519, yes, pem, SubjectPublicKeyInfo),
ENCODER_w_structure("ED448", ed448, yes, der, PKCS8),
ENCODER_w_structure("ED448", ed448, yes, pem, PKCS8),
ENCODER_w_structure("ED448", ed448, yes, der, SubjectPublicKeyInfo),
ENCODER_w_structure("ED448", ed448, yes, pem, SubjectPublicKeyInfo),
@ -173,22 +187,22 @@ ENCODER("ED448", ed448, yes, pem, SubjectPublicKeyInfo),
/* The RSA encoders only support private key and public key output */
ENCODER("RSA", rsa, yes, der, RSA),
ENCODER("RSA", rsa, yes, pem, RSA),
ENCODER_w_structure("RSA", rsa, yes, der, RSA),
ENCODER_w_structure("RSA", rsa, yes, pem, RSA),
/* DH and X9.42 DH only support key parameters output. */
ENCODER("DH", dh, yes, der, DH),
ENCODER("DH", dh, yes, pem, DH),
ENCODER("DHX", dhx, yes, der, DHX),
ENCODER("DHX", dhx, yes, pem, DHX),
ENCODER_w_structure("DH", dh, yes, der, DH),
ENCODER_w_structure("DH", dh, yes, pem, DH),
ENCODER_w_structure("DHX", dhx, yes, der, DHX),
ENCODER_w_structure("DHX", dhx, yes, pem, DHX),
ENCODER("DSA", dsa, yes, der, DSA),
ENCODER("DSA", dsa, yes, pem, DSA),
ENCODER_w_structure("DSA", dsa, yes, der, DSA),
ENCODER_w_structure("DSA", dsa, yes, pem, DSA),
ENCODER("EC", ec, yes, der, EC),
ENCODER("EC", ec, yes, pem, EC),
ENCODER_w_structure("EC", ec, yes, der, EC),
ENCODER_w_structure("EC", ec, yes, pem, EC),
@ -198,20 +212,20 @@ ENCODER("EC", ec, yes, pem, EC),
* on libcrypto functionality in any way.
/* PKCS#1 is a well known for plain RSA keys, so we add that too */
ENCODER("RSA", rsa, yes, der, PKCS1),
ENCODER("RSA", rsa, yes, pem, PKCS1),
ENCODER("RSA-PSS", rsapss, yes, der, PKCS1),
ENCODER("RSA-PSS", rsapss, yes, pem, PKCS1),
ENCODER_w_structure("RSA", rsa, yes, der, PKCS1),
ENCODER_w_structure("RSA", rsa, yes, pem, PKCS1),
ENCODER_w_structure("RSA-PSS", rsapss, yes, der, PKCS1),
ENCODER_w_structure("RSA-PSS", rsapss, yes, pem, PKCS1),
/* PKCS#3 defines the format for DH parameters */
ENCODER("DH", dh, yes, der, PKCS3),
ENCODER("DH", dh, yes, pem, PKCS3),
ENCODER_w_structure("DH", dh, yes, der, PKCS3),
ENCODER_w_structure("DH", dh, yes, pem, PKCS3),
/* X9.42 defines the format for DHX parameters */
ENCODER("DHX", dhx, yes, der, X9_42),
ENCODER("DHX", dhx, yes, pem, X9_42),
ENCODER_w_structure("DHX", dhx, yes, der, X9_42),
ENCODER_w_structure("DHX", dhx, yes, pem, X9_42),
/* RFC 5915 defines the format for EC keys and parameters */
ENCODER("EC", ec, yes, der, X9_62),
ENCODER("EC", ec, yes, pem, X9_62),
ENCODER_w_structure("EC", ec, yes, der, X9_62),
ENCODER_w_structure("EC", ec, yes, pem, X9_62),

@ -14,5 +14,5 @@ SOURCE[$ENCODER_GOAL]=endecoder_common.c
SOURCE[$DECODER_GOAL]=decode_der2key.c decode_pem2der.c decode_ms2key.c
SOURCE[$DECODER_GOAL]=encode_key2any.c encode_key2text.c
SOURCE[$ENCODER_GOAL]=encode_key2any.c encode_key2text.c encode_key2ms.c

@ -0,0 +1,264 @@
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
* Licensed under the Apache License 2.0 (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
* Low level APIs are deprecated for public use, but still ok for internal use.
#include "internal/deprecated.h"
#include <string.h>
#include <openssl/core.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/err.h>
#include <openssl/pem.h> /* Functions for writing MSBLOB and PVK */
#include <openssl/dsa.h>
#include "internal/passphrase.h"
#include "crypto/rsa.h"
#include "prov/implementations.h"
#include "prov/bio.h"
#include "prov/provider_ctx.h"
#include "endecoder_local.h"
struct key2ms_ctx_st {
PROV_CTX *provctx;
int pvk_encr_level;
struct ossl_passphrase_data_st pwdata;
static int write_msblob(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
EVP_PKEY *pkey, int ispub)
BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
int ret =
ispub ? i2b_PublicKey_bio(out, pkey) : i2b_PrivateKey_bio(out, pkey);
return ret;
static int write_pvk(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
EVP_PKEY *pkey,
OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
BIO *out = NULL;
int ret = 0;
out = bio_new_from_core_bio(ctx->provctx, cout);
ret = i2b_PVK_bio(out, pkey, ctx->pvk_encr_level,
ossl_pw_pem_password, &ctx->pwdata);
return ret;
static OSSL_FUNC_encoder_freectx_fn key2ms_freectx;
static OSSL_FUNC_encoder_gettable_params_fn key2ms_gettable_params;
static OSSL_FUNC_encoder_does_selection_fn key2ms_does_selection;
static struct key2ms_ctx_st *key2ms_newctx(void *provctx)
struct key2ms_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
if (ctx != NULL) {
ctx->provctx = provctx;
/* This is the strongest encryption level */
ctx->pvk_encr_level = 2;
return ctx;
static void key2ms_freectx(void *vctx)
struct key2ms_ctx_st *ctx = vctx;
static const OSSL_PARAM *key2ms_gettable_params(ossl_unused void *provctx)
static const OSSL_PARAM gettables[] = {
return gettables;
static int key2msblob_get_params(OSSL_PARAM params[])
if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "MSBLOB"))
return 0;
return 1;
static int key2pvk_get_params(OSSL_PARAM params[])
if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PVK"))
return 0;
return 1;
static const OSSL_PARAM *key2pvk_settable_ctx_params(ossl_unused void *provctx)
static const OSSL_PARAM settables[] = {
return settables;
static int key2pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])
struct key2ms_ctx_st *ctx = vctx;
const OSSL_PARAM *p;
if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->pvk_encr_level))
return 0;
return 1;
static int key2ms_does_selection(void *vctx, int selection)
return (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0;
* The real EVP_PKEY_set1_TYPE() functions take a non-const key, while the key
* pointer in the encode function is a const pointer. We violate the constness
* knowingly, since we know that the key comes from the same provider, is never
* actually const, and the implied reference count change is safe.
* EVP_PKEY_assign() can't be used, because there's no way to clear the internal
* key using that function without freeing the existing internal key.
typedef int evp_pkey_set1_fn(EVP_PKEY *, const void *key);
static int key2msblob_encode(void *vctx, const void *key, int selection,
OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
struct key2ms_ctx_st *ctx = vctx;
int ispub = -1;
EVP_PKEY *pkey = NULL;
int ok = 0;
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
ispub = 0;
else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
ispub = 1;
return 0; /* Error */
if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key))
ok = write_msblob(ctx, cout, pkey, ispub);
return ok;
static int key2pvk_encode(void *vctx, const void *key, int selection,
OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
struct key2ms_ctx_st *ctx = vctx;
EVP_PKEY *pkey = NULL;
int ok = 0;
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
return 0; /* Error */
if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key))
ok = write_pvk(ctx, cout, pkey, pw_cb, pw_cbarg);
return ok;
#define dsa_set1 (evp_pkey_set1_fn *)EVP_PKEY_set1_DSA
#define rsa_set1 (evp_pkey_set1_fn *)EVP_PKEY_set1_RSA
#define msblob_set_params
#define pvk_set_params \
(void (*)(void))key2pvk_settable_ctx_params }, \
(void (*)(void))key2pvk_set_ctx_params },
#define MAKE_MS_ENCODER(impl, output, type) \
static OSSL_FUNC_encoder_import_object_fn \
impl##2##output##_import_object; \
static OSSL_FUNC_encoder_free_object_fn impl##2##output##_free_object; \
static OSSL_FUNC_encoder_encode_fn impl##2##output##_encode; \
static void * \
impl##2##output##_import_object(void *ctx, int selection, \
const OSSL_PARAM params[]) \
{ \
return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
ctx, selection, params); \
} \
static void impl##2##output##_free_object(void *key) \
{ \
ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
} \
static int impl##2##output##_encode(void *vctx, OSSL_CORE_BIO *cout, \
const void *key, \
const OSSL_PARAM key_abstract[], \
int selection, \
void *cbarg) \
{ \
/* We don't deal with abstract objects */ \
if (key_abstract != NULL) { \
return 0; \
} \
return key2##output##_encode(vctx, key, selection, cout, type##_set1, \
cb, cbarg); \
} \
const OSSL_DISPATCH ossl_##impl##_to_##output##_encoder_functions[] = { \
(void (*)(void))key2ms_newctx }, \
(void (*)(void))key2ms_freectx }, \
(void (*)(void))key2ms_gettable_params }, \
(void (*)(void))key2##output##_get_params }, \
output##_set_params \
(void (*)(void))key2ms_does_selection }, \
(void (*)(void))impl##2##output##_import_object }, \
(void (*)(void))impl##2##output##_free_object }, \
(void (*)(void))impl##2##output##_encode }, \
{ 0, NULL } \
MAKE_MS_ENCODER(dsa, pvk, dsa);
MAKE_MS_ENCODER(dsa, msblob, dsa);
MAKE_MS_ENCODER(rsa, pvk, rsa);
MAKE_MS_ENCODER(rsa, msblob, rsa);

@ -328,6 +328,8 @@ extern const OSSL_DISPATCH ossl_rsa_to_RSA_der_encoder_functions[];
extern const OSSL_DISPATCH ossl_rsa_to_RSA_pem_encoder_functions[];
extern const OSSL_DISPATCH ossl_rsa_to_SubjectPublicKeyInfo_der_encoder_functions[];
extern const OSSL_DISPATCH ossl_rsa_to_SubjectPublicKeyInfo_pem_encoder_functions[];
extern const OSSL_DISPATCH ossl_rsa_to_msblob_encoder_functions[];
extern const OSSL_DISPATCH ossl_rsa_to_pvk_encoder_functions[];
extern const OSSL_DISPATCH ossl_rsa_to_text_encoder_functions[];
extern const OSSL_DISPATCH ossl_rsa_to_type_specific_keypair_der_encoder_functions[];
extern const OSSL_DISPATCH ossl_rsa_to_type_specific_keypair_pem_encoder_functions[];
@ -372,6 +374,8 @@ extern const OSSL_DISPATCH ossl_dsa_to_SubjectPublicKeyInfo_der_encoder_function
extern const OSSL_DISPATCH ossl_dsa_to_SubjectPublicKeyInfo_pem_encoder_functions[];
extern const OSSL_DISPATCH ossl_dsa_to_type_specific_pem_encoder_functions[];
extern const OSSL_DISPATCH ossl_dsa_to_type_specific_der_encoder_functions[];
extern const OSSL_DISPATCH ossl_dsa_to_msblob_encoder_functions[];
extern const OSSL_DISPATCH ossl_dsa_to_pvk_encoder_functions[];
extern const OSSL_DISPATCH ossl_dsa_to_text_encoder_functions[];
extern const OSSL_DISPATCH ossl_ec_to_EC_der_encoder_functions[];