diff options
Diffstat (limited to 'providers/implementations/encode_decode')
12 files changed, 957 insertions, 984 deletions
diff --git a/providers/implementations/encode_decode/decode_der2key.c b/providers/implementations/encode_decode/decode_der2key.c index 075f90153607..40ba65405676 100644 --- a/providers/implementations/encode_decode/decode_der2key.c +++ b/providers/implementations/encode_decode/decode_der2key.c @@ -19,11 +19,11 @@ #include <openssl/crypto.h> #include <openssl/err.h> #include <openssl/params.h> -#include <openssl/pem.h> /* PEM_BUFSIZE and public PEM functions */ +#include <openssl/pem.h> /* PEM_BUFSIZE and public PEM functions */ #include <openssl/pkcs12.h> #include <openssl/x509.h> #include <openssl/proverr.h> -#include "internal/cryptlib.h" /* ossl_assert() */ +#include "internal/cryptlib.h" /* ossl_assert() */ #include "internal/asn1.h" #include "crypto/dh.h" #include "crypto/dsa.h" @@ -37,12 +37,12 @@ #include "prov/implementations.h" #include "endecoder_local.h" -struct der2key_ctx_st; /* Forward declaration */ +struct der2key_ctx_st; /* Forward declaration */ typedef int check_key_fn(void *, struct der2key_ctx_st *ctx); typedef void adjust_key_fn(void *, struct der2key_ctx_st *ctx); typedef void free_key_fn(void *); typedef void *d2i_PKCS8_fn(void **, const unsigned char **, long, - struct der2key_ctx_st *); + struct der2key_ctx_st *); struct keytype_desc_st { const char *keytype_name; const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */ @@ -63,10 +63,10 @@ struct keytype_desc_st { /* For type specific decoders, we use the corresponding d2i */ d2i_of_void *d2i_private_key; /* From type-specific DER */ - d2i_of_void *d2i_public_key; /* From type-specific DER */ - d2i_of_void *d2i_key_params; /* From type-specific DER */ - d2i_PKCS8_fn *d2i_PKCS8; /* Wrapped in a PrivateKeyInfo */ - d2i_of_void *d2i_PUBKEY; /* Wrapped in a SubjectPublicKeyInfo */ + d2i_of_void *d2i_public_key; /* From type-specific DER */ + d2i_of_void *d2i_key_params; /* From type-specific DER */ + d2i_PKCS8_fn *d2i_PKCS8; /* Wrapped in a PrivateKeyInfo */ + d2i_of_void *d2i_PUBKEY; /* Wrapped in a SubjectPublicKeyInfo */ /* * For any key, we may need to check that the key meets expectations. @@ -97,10 +97,10 @@ struct der2key_ctx_st { }; typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf, - OSSL_LIB_CTX *libctx, const char *propq); + OSSL_LIB_CTX *libctx, const char *propq); static void *der2key_decode_p8(const unsigned char **input_der, - long input_der_len, struct der2key_ctx_st *ctx, - key_from_pkcs8_t *key_from_pkcs8) + long input_der_len, struct der2key_ctx_st *ctx, + key_from_pkcs8_t *key_from_pkcs8) { PKCS8_PRIV_KEY_INFO *p8inf = NULL; const X509_ALGOR *alg = NULL; @@ -144,7 +144,7 @@ static void der2key_freectx(void *vctx) } static int der2key_check_selection(int selection, - const struct keytype_desc_st *desc) + const struct keytype_desc_st *desc) { /* * The selections are kinda sorta "levels", i.e. each selection given @@ -178,8 +178,8 @@ static int der2key_check_selection(int selection, } static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, - OSSL_CALLBACK *data_cb, void *data_cbarg, - OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) + OSSL_CALLBACK *data_cb, void *data_cbarg, + OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { struct der2key_ctx_st *ctx = vctx; unsigned char *der = NULL; @@ -269,7 +269,7 @@ static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, if (key != NULL && ctx->desc->adjust_key != NULL) ctx->desc->adjust_key(key, ctx); - next: +next: /* * Indicated that we successfully decoded something, or not at all. * Ending up "empty handed" is not an error. @@ -288,31 +288,27 @@ static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, OSSL_PARAM params[4]; int object_type = OSSL_OBJECT_PKEY; - params[0] = - OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); + params[0] = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); #ifndef OPENSSL_NO_SM2 if (strcmp(ctx->desc->keytype_name, "EC") == 0 && (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0) - params[1] = - OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, - "SM2", 0); + params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, + "SM2", 0); else #endif - params[1] = - OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, - (char *)ctx->desc->keytype_name, - 0); + params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, + (char *)ctx->desc->keytype_name, + 0); /* The address of the key becomes the octet string */ - params[2] = - OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, - &key, sizeof(key)); + params[2] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, + &key, sizeof(key)); params[3] = OSSL_PARAM_construct_end(); ok = data_cb(params, data_cbarg); } - end: +end: ctx->desc->free_key(key); OPENSSL_free(der); @@ -320,12 +316,11 @@ static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, } static int der2key_export_object(void *vctx, - const void *reference, size_t reference_sz, - OSSL_CALLBACK *export_cb, void *export_cbarg) + const void *reference, size_t reference_sz, + OSSL_CALLBACK *export_cb, void *export_cbarg) { struct der2key_ctx_st *ctx = vctx; - OSSL_FUNC_keymgmt_export_fn *export = - ossl_prov_get_keymgmt_export(ctx->desc->fns); + OSSL_FUNC_keymgmt_export_fn *export = ossl_prov_get_keymgmt_export(ctx->desc->fns); void *keydata; if (reference_sz == sizeof(keydata) && export != NULL) { @@ -344,56 +339,56 @@ static int der2key_export_object(void *vctx, /* ---------------------------------------------------------------------- */ #ifndef OPENSSL_NO_DH -# define dh_evp_type EVP_PKEY_DH -# define dh_d2i_private_key NULL -# define dh_d2i_public_key NULL -# define dh_d2i_key_params (d2i_of_void *)d2i_DHparams +#define dh_evp_type EVP_PKEY_DH +#define dh_d2i_private_key NULL +#define dh_d2i_public_key NULL +#define dh_d2i_key_params (d2i_of_void *)d2i_DHparams static void *dh_d2i_PKCS8(void **key, const unsigned char **der, long der_len, - struct der2key_ctx_st *ctx) + struct der2key_ctx_st *ctx) { return der2key_decode_p8(der, der_len, ctx, - (key_from_pkcs8_t *)ossl_dh_key_from_pkcs8); + (key_from_pkcs8_t *)ossl_dh_key_from_pkcs8); } -# define dh_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DH_PUBKEY -# define dh_free (free_key_fn *)DH_free -# define dh_check NULL +#define dh_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DH_PUBKEY +#define dh_free (free_key_fn *)DH_free +#define dh_check NULL static void dh_adjust(void *key, struct der2key_ctx_st *ctx) { ossl_dh_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); } -# define dhx_evp_type EVP_PKEY_DHX -# define dhx_d2i_private_key NULL -# define dhx_d2i_public_key NULL -# define dhx_d2i_key_params (d2i_of_void *)d2i_DHxparams -# define dhx_d2i_PKCS8 dh_d2i_PKCS8 -# define dhx_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DHx_PUBKEY -# define dhx_free (free_key_fn *)DH_free -# define dhx_check NULL -# define dhx_adjust dh_adjust +#define dhx_evp_type EVP_PKEY_DHX +#define dhx_d2i_private_key NULL +#define dhx_d2i_public_key NULL +#define dhx_d2i_key_params (d2i_of_void *)d2i_DHxparams +#define dhx_d2i_PKCS8 dh_d2i_PKCS8 +#define dhx_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DHx_PUBKEY +#define dhx_free (free_key_fn *)DH_free +#define dhx_check NULL +#define dhx_adjust dh_adjust #endif /* ---------------------------------------------------------------------- */ #ifndef OPENSSL_NO_DSA -# define dsa_evp_type EVP_PKEY_DSA -# define dsa_d2i_private_key (d2i_of_void *)d2i_DSAPrivateKey -# define dsa_d2i_public_key (d2i_of_void *)d2i_DSAPublicKey -# define dsa_d2i_key_params (d2i_of_void *)d2i_DSAparams +#define dsa_evp_type EVP_PKEY_DSA +#define dsa_d2i_private_key (d2i_of_void *)d2i_DSAPrivateKey +#define dsa_d2i_public_key (d2i_of_void *)d2i_DSAPublicKey +#define dsa_d2i_key_params (d2i_of_void *)d2i_DSAparams static void *dsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len, - struct der2key_ctx_st *ctx) + struct der2key_ctx_st *ctx) { return der2key_decode_p8(der, der_len, ctx, - (key_from_pkcs8_t *)ossl_dsa_key_from_pkcs8); + (key_from_pkcs8_t *)ossl_dsa_key_from_pkcs8); } -# define dsa_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DSA_PUBKEY -# define dsa_free (free_key_fn *)DSA_free -# define dsa_check NULL +#define dsa_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DSA_PUBKEY +#define dsa_free (free_key_fn *)DSA_free +#define dsa_check NULL static void dsa_adjust(void *key, struct der2key_ctx_st *ctx) { @@ -404,20 +399,20 @@ static void dsa_adjust(void *key, struct der2key_ctx_st *ctx) /* ---------------------------------------------------------------------- */ #ifndef OPENSSL_NO_EC -# define ec_evp_type EVP_PKEY_EC -# define ec_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey -# define ec_d2i_public_key NULL -# define ec_d2i_key_params (d2i_of_void *)d2i_ECParameters +#define ec_evp_type EVP_PKEY_EC +#define ec_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey +#define ec_d2i_public_key NULL +#define ec_d2i_key_params (d2i_of_void *)d2i_ECParameters static void *ec_d2i_PKCS8(void **key, const unsigned char **der, long der_len, - struct der2key_ctx_st *ctx) + struct der2key_ctx_st *ctx) { return der2key_decode_p8(der, der_len, ctx, - (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8); + (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8); } -# define ec_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY -# define ec_free (free_key_fn *)EC_KEY_free +#define ec_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY +#define ec_free (free_key_fn *)EC_KEY_free static int ec_check(void *key, struct der2key_ctx_st *ctx) { @@ -445,10 +440,10 @@ static void ec_adjust(void *key, struct der2key_ctx_st *ctx) */ static void *ecx_d2i_PKCS8(void **key, const unsigned char **der, long der_len, - struct der2key_ctx_st *ctx) + struct der2key_ctx_st *ctx) { return der2key_decode_p8(der, der_len, ctx, - (key_from_pkcs8_t *)ossl_ecx_key_from_pkcs8); + (key_from_pkcs8_t *)ossl_ecx_key_from_pkcs8); } static void ecx_key_adjust(void *key, struct der2key_ctx_st *ctx) @@ -456,82 +451,82 @@ static void ecx_key_adjust(void *key, struct der2key_ctx_st *ctx) ossl_ecx_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); } -# define ed25519_evp_type EVP_PKEY_ED25519 -# define ed25519_d2i_private_key NULL -# define ed25519_d2i_public_key NULL -# define ed25519_d2i_key_params NULL -# define ed25519_d2i_PKCS8 ecx_d2i_PKCS8 -# define ed25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED25519_PUBKEY -# define ed25519_free (free_key_fn *)ossl_ecx_key_free -# define ed25519_check NULL -# define ed25519_adjust ecx_key_adjust +#define ed25519_evp_type EVP_PKEY_ED25519 +#define ed25519_d2i_private_key NULL +#define ed25519_d2i_public_key NULL +#define ed25519_d2i_key_params NULL +#define ed25519_d2i_PKCS8 ecx_d2i_PKCS8 +#define ed25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED25519_PUBKEY +#define ed25519_free (free_key_fn *)ossl_ecx_key_free +#define ed25519_check NULL +#define ed25519_adjust ecx_key_adjust -# define ed448_evp_type EVP_PKEY_ED448 -# define ed448_d2i_private_key NULL -# define ed448_d2i_public_key NULL -# define ed448_d2i_key_params NULL -# define ed448_d2i_PKCS8 ecx_d2i_PKCS8 -# define ed448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED448_PUBKEY -# define ed448_free (free_key_fn *)ossl_ecx_key_free -# define ed448_check NULL -# define ed448_adjust ecx_key_adjust +#define ed448_evp_type EVP_PKEY_ED448 +#define ed448_d2i_private_key NULL +#define ed448_d2i_public_key NULL +#define ed448_d2i_key_params NULL +#define ed448_d2i_PKCS8 ecx_d2i_PKCS8 +#define ed448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED448_PUBKEY +#define ed448_free (free_key_fn *)ossl_ecx_key_free +#define ed448_check NULL +#define ed448_adjust ecx_key_adjust -# define x25519_evp_type EVP_PKEY_X25519 -# define x25519_d2i_private_key NULL -# define x25519_d2i_public_key NULL -# define x25519_d2i_key_params NULL -# define x25519_d2i_PKCS8 ecx_d2i_PKCS8 -# define x25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X25519_PUBKEY -# define x25519_free (free_key_fn *)ossl_ecx_key_free -# define x25519_check NULL -# define x25519_adjust ecx_key_adjust +#define x25519_evp_type EVP_PKEY_X25519 +#define x25519_d2i_private_key NULL +#define x25519_d2i_public_key NULL +#define x25519_d2i_key_params NULL +#define x25519_d2i_PKCS8 ecx_d2i_PKCS8 +#define x25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X25519_PUBKEY +#define x25519_free (free_key_fn *)ossl_ecx_key_free +#define x25519_check NULL +#define x25519_adjust ecx_key_adjust -# define x448_evp_type EVP_PKEY_X448 -# define x448_d2i_private_key NULL -# define x448_d2i_public_key NULL -# define x448_d2i_key_params NULL -# define x448_d2i_PKCS8 ecx_d2i_PKCS8 -# define x448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X448_PUBKEY -# define x448_free (free_key_fn *)ossl_ecx_key_free -# define x448_check NULL -# define x448_adjust ecx_key_adjust +#define x448_evp_type EVP_PKEY_X448 +#define x448_d2i_private_key NULL +#define x448_d2i_public_key NULL +#define x448_d2i_key_params NULL +#define x448_d2i_PKCS8 ecx_d2i_PKCS8 +#define x448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X448_PUBKEY +#define x448_free (free_key_fn *)ossl_ecx_key_free +#define x448_check NULL +#define x448_adjust ecx_key_adjust -# ifndef OPENSSL_NO_SM2 -# define sm2_evp_type EVP_PKEY_SM2 -# define sm2_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey -# define sm2_d2i_public_key NULL -# define sm2_d2i_key_params (d2i_of_void *)d2i_ECParameters +#ifndef OPENSSL_NO_SM2 +#define sm2_evp_type EVP_PKEY_SM2 +#define sm2_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey +#define sm2_d2i_public_key NULL +#define sm2_d2i_key_params (d2i_of_void *)d2i_ECParameters static void *sm2_d2i_PKCS8(void **key, const unsigned char **der, long der_len, - struct der2key_ctx_st *ctx) + struct der2key_ctx_st *ctx) { return der2key_decode_p8(der, der_len, ctx, - (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8); + (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8); } -# define sm2_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY -# define sm2_free (free_key_fn *)EC_KEY_free -# define sm2_check ec_check -# define sm2_adjust ec_adjust -# endif +#define sm2_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY +#define sm2_free (free_key_fn *)EC_KEY_free +#define sm2_check ec_check +#define sm2_adjust ec_adjust +#endif #endif /* ---------------------------------------------------------------------- */ -#define rsa_evp_type EVP_PKEY_RSA -#define rsa_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey -#define rsa_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey -#define rsa_d2i_key_params NULL +#define rsa_evp_type EVP_PKEY_RSA +#define rsa_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey +#define rsa_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey +#define rsa_d2i_key_params NULL static void *rsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len, - struct der2key_ctx_st *ctx) + struct der2key_ctx_st *ctx) { return der2key_decode_p8(der, der_len, ctx, - (key_from_pkcs8_t *)ossl_rsa_key_from_pkcs8); + (key_from_pkcs8_t *)ossl_rsa_key_from_pkcs8); } -#define rsa_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY -#define rsa_free (free_key_fn *)RSA_free +#define rsa_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY +#define rsa_free (free_key_fn *)RSA_free static int rsa_check(void *key, struct der2key_ctx_st *ctx) { @@ -551,15 +546,15 @@ static void rsa_adjust(void *key, struct der2key_ctx_st *ctx) ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); } -#define rsapss_evp_type EVP_PKEY_RSA_PSS -#define rsapss_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey -#define rsapss_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey -#define rsapss_d2i_key_params NULL -#define rsapss_d2i_PKCS8 rsa_d2i_PKCS8 -#define rsapss_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY -#define rsapss_free (free_key_fn *)RSA_free -#define rsapss_check rsa_check -#define rsapss_adjust rsa_adjust +#define rsapss_evp_type EVP_PKEY_RSA_PSS +#define rsapss_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey +#define rsapss_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey +#define rsapss_d2i_key_params NULL +#define rsapss_d2i_PKCS8 rsa_d2i_PKCS8 +#define rsapss_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY +#define rsapss_free (free_key_fn *)RSA_free +#define rsapss_check rsa_check +#define rsapss_adjust rsa_adjust /* ---------------------------------------------------------------------- */ @@ -567,162 +562,162 @@ static void rsa_adjust(void *key, struct der2key_ctx_st *ctx) * The DO_ macros help define the selection mask and the method functions * for each kind of object we want to decode. */ -#define DO_type_specific_keypair(keytype) \ - "type-specific", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \ - keytype##_d2i_private_key, \ - keytype##_d2i_public_key, \ - NULL, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_type_specific_keypair(keytype) \ + "type-specific", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_KEYPAIR), \ + keytype##_d2i_private_key, \ + keytype##_d2i_public_key, \ + NULL, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_type_specific_pub(keytype) \ - "type-specific", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \ - NULL, \ - keytype##_d2i_public_key, \ - NULL, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_type_specific_pub(keytype) \ + "type-specific", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_PUBLIC_KEY), \ + NULL, \ + keytype##_d2i_public_key, \ + NULL, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_type_specific_priv(keytype) \ - "type-specific", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \ - keytype##_d2i_private_key, \ - NULL, \ - NULL, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_type_specific_priv(keytype) \ + "type-specific", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_PRIVATE_KEY), \ + keytype##_d2i_private_key, \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_type_specific_params(keytype) \ - "type-specific", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \ - NULL, \ - NULL, \ - keytype##_d2i_key_params, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_type_specific_params(keytype) \ + "type-specific", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_ALL_PARAMETERS), \ + NULL, \ + NULL, \ + keytype##_d2i_key_params, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_type_specific(keytype) \ - "type-specific", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_ALL ), \ - keytype##_d2i_private_key, \ - keytype##_d2i_public_key, \ - keytype##_d2i_key_params, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_type_specific(keytype) \ + "type-specific", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_ALL), \ + keytype##_d2i_private_key, \ + keytype##_d2i_public_key, \ + keytype##_d2i_key_params, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_type_specific_no_pub(keytype) \ - "type-specific", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \ - | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \ - keytype##_d2i_private_key, \ - NULL, \ - keytype##_d2i_key_params, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_type_specific_no_pub(keytype) \ + "type-specific", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_PRIVATE_KEY \ + | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS), \ + keytype##_d2i_private_key, \ + NULL, \ + keytype##_d2i_key_params, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_PrivateKeyInfo(keytype) \ - "PrivateKeyInfo", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \ - NULL, \ - NULL, \ - NULL, \ - keytype##_d2i_PKCS8, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_PrivateKeyInfo(keytype) \ + "PrivateKeyInfo", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_PRIVATE_KEY), \ + NULL, \ + NULL, \ + NULL, \ + keytype##_d2i_PKCS8, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_SubjectPublicKeyInfo(keytype) \ - "SubjectPublicKeyInfo", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \ - NULL, \ - NULL, \ - NULL, \ - NULL, \ - keytype##_d2i_PUBKEY, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_SubjectPublicKeyInfo(keytype) \ + "SubjectPublicKeyInfo", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_PUBLIC_KEY), \ + NULL, \ + NULL, \ + NULL, \ + NULL, \ + keytype##_d2i_PUBKEY, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_DH(keytype) \ - "DH", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \ - NULL, \ - NULL, \ - keytype##_d2i_key_params, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_DH(keytype) \ + "DH", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_ALL_PARAMETERS), \ + NULL, \ + NULL, \ + keytype##_d2i_key_params, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_DHX(keytype) \ - "DHX", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \ - NULL, \ - NULL, \ - keytype##_d2i_key_params, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_DHX(keytype) \ + "DHX", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_ALL_PARAMETERS), \ + NULL, \ + NULL, \ + keytype##_d2i_key_params, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_DSA(keytype) \ - "DSA", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_ALL ), \ - keytype##_d2i_private_key, \ - keytype##_d2i_public_key, \ - keytype##_d2i_key_params, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_DSA(keytype) \ + "DSA", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_ALL), \ + keytype##_d2i_private_key, \ + keytype##_d2i_public_key, \ + keytype##_d2i_key_params, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_EC(keytype) \ - "EC", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \ - | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \ - keytype##_d2i_private_key, \ - NULL, \ - keytype##_d2i_key_params, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_EC(keytype) \ + "EC", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_PRIVATE_KEY \ + | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS), \ + keytype##_d2i_private_key, \ + NULL, \ + keytype##_d2i_key_params, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free -#define DO_RSA(keytype) \ - "RSA", keytype##_evp_type, \ - ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \ - keytype##_d2i_private_key, \ - keytype##_d2i_public_key, \ - NULL, \ - NULL, \ - NULL, \ - keytype##_check, \ - keytype##_adjust, \ +#define DO_RSA(keytype) \ + "RSA", keytype##_evp_type, \ + (OSSL_KEYMGMT_SELECT_KEYPAIR), \ + keytype##_d2i_private_key, \ + keytype##_d2i_public_key, \ + NULL, \ + NULL, \ + NULL, \ + keytype##_check, \ + keytype##_adjust, \ keytype##_free /* @@ -742,37 +737,37 @@ static void rsa_adjust(void *key, struct der2key_ctx_st *ctx) * the DO_##kind macros above, to populate the keytype_desc_st * structure. */ -#define MAKE_DECODER(keytype_name, keytype, type, kind) \ - static const struct keytype_desc_st kind##_##keytype##_desc = \ - { keytype_name, ossl_##keytype##_keymgmt_functions, \ - DO_##kind(keytype) }; \ - \ - static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \ - \ - static void *kind##_der2##keytype##_newctx(void *provctx) \ - { \ - return der2key_newctx(provctx, &kind##_##keytype##_desc); \ - } \ - static int kind##_der2##keytype##_does_selection(void *provctx, \ - int selection) \ - { \ - return der2key_check_selection(selection, \ - &kind##_##keytype##_desc); \ - } \ - const OSSL_DISPATCH \ - ossl_##kind##_der_to_##keytype##_decoder_functions[] = { \ - { OSSL_FUNC_DECODER_NEWCTX, \ - (void (*)(void))kind##_der2##keytype##_newctx }, \ - { OSSL_FUNC_DECODER_FREECTX, \ - (void (*)(void))der2key_freectx }, \ - { OSSL_FUNC_DECODER_DOES_SELECTION, \ - (void (*)(void))kind##_der2##keytype##_does_selection }, \ - { OSSL_FUNC_DECODER_DECODE, \ - (void (*)(void))der2key_decode }, \ - { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ - (void (*)(void))der2key_export_object }, \ - { 0, NULL } \ - } +#define MAKE_DECODER(keytype_name, keytype, type, kind) \ + static const struct keytype_desc_st kind##_##keytype##_desc = { keytype_name, ossl_##keytype##_keymgmt_functions, \ + DO_##kind(keytype) }; \ + \ + static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \ + \ + static void *kind##_der2##keytype##_newctx(void *provctx) \ + { \ + return der2key_newctx(provctx, &kind##_##keytype##_desc); \ + } \ + static int kind##_der2##keytype##_does_selection(void *provctx, \ + int selection) \ + { \ + return der2key_check_selection(selection, \ + &kind##_##keytype##_desc); \ + } \ + const OSSL_DISPATCH \ + ossl_##kind##_der_to_##keytype##_decoder_functions[] \ + = { \ + { OSSL_FUNC_DECODER_NEWCTX, \ + (void (*)(void))kind##_der2##keytype##_newctx }, \ + { OSSL_FUNC_DECODER_FREECTX, \ + (void (*)(void))der2key_freectx }, \ + { OSSL_FUNC_DECODER_DOES_SELECTION, \ + (void (*)(void))kind##_der2##keytype##_does_selection }, \ + { OSSL_FUNC_DECODER_DECODE, \ + (void (*)(void))der2key_decode }, \ + { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ + (void (*)(void))der2key_export_object }, \ + { 0, NULL } \ + } #ifndef OPENSSL_NO_DH MAKE_DECODER("DH", dh, dh, PrivateKeyInfo); @@ -803,11 +798,11 @@ MAKE_DECODER("ED25519", ed25519, ecx, PrivateKeyInfo); MAKE_DECODER("ED25519", ed25519, ecx, SubjectPublicKeyInfo); MAKE_DECODER("ED448", ed448, ecx, PrivateKeyInfo); MAKE_DECODER("ED448", ed448, ecx, SubjectPublicKeyInfo); -# ifndef OPENSSL_NO_SM2 +#ifndef OPENSSL_NO_SM2 MAKE_DECODER("SM2", sm2, ec, PrivateKeyInfo); MAKE_DECODER("SM2", sm2, ec, SubjectPublicKeyInfo); MAKE_DECODER("SM2", sm2, sm2, type_specific_no_pub); -# endif +#endif #endif MAKE_DECODER("RSA", rsa, rsa, PrivateKeyInfo); MAKE_DECODER("RSA", rsa, rsa, SubjectPublicKeyInfo); diff --git a/providers/implementations/encode_decode/decode_epki2pki.c b/providers/implementations/encode_decode/decode_epki2pki.c index 9cea80b616d6..eaf078b92705 100644 --- a/providers/implementations/encode_decode/decode_epki2pki.c +++ b/providers/implementations/encode_decode/decode_epki2pki.c @@ -56,8 +56,8 @@ static void epki2pki_freectx(void *vctx) * PrivateKeyInfo. */ static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, - OSSL_CALLBACK *data_cb, void *data_cbarg, - OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) + OSSL_CALLBACK *data_cb, void *data_cbarg, + OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { struct epki2pki_ctx_st *ctx = vctx; BUF_MEM *mem = NULL; @@ -84,7 +84,7 @@ static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, der_len = (long)mem->length; OPENSSL_free(mem); - ok = 1; /* Assume good */ + ok = 1; /* Assume good */ ERR_set_mark(); if ((p8 = d2i_X509_SIG(NULL, &pder, der_len)) != NULL) { char pbuf[1024]; @@ -102,9 +102,9 @@ static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, X509_SIG_get0(p8, &alg, &oct); if (!PKCS12_pbe_crypt_ex(alg, pbuf, plen, - oct->data, oct->length, - &new_der, &new_der_len, 0, - PROV_LIBCTX_OF(ctx->provctx), NULL)) { + oct->data, oct->length, + &new_der, &new_der_len, 0, + PROV_LIBCTX_OF(ctx->provctx), NULL)) { ok = 0; } else { OPENSSL_free(der); @@ -135,11 +135,11 @@ static int epki2pki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, OBJ_obj2txt(keytype, sizeof(keytype), alg->algorithm, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, - keytype, 0); + keytype, 0); *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, - "PrivateKeyInfo", 0); + "PrivateKeyInfo", 0); *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, - der, der_len); + der, der_len); *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype); *p = OSSL_PARAM_construct_end(); diff --git a/providers/implementations/encode_decode/decode_msblob2key.c b/providers/implementations/encode_decode/decode_msblob2key.c index b9d0cabadae2..b3eec691404e 100644 --- a/providers/implementations/encode_decode/decode_msblob2key.c +++ b/providers/implementations/encode_decode/decode_msblob2key.c @@ -20,24 +20,24 @@ #include <openssl/core_object.h> #include <openssl/crypto.h> #include <openssl/params.h> -#include <openssl/pem.h> /* For public PVK functions */ +#include <openssl/pem.h> /* For public PVK functions */ #include <openssl/x509.h> #include <openssl/err.h> #include "internal/passphrase.h" -#include "crypto/pem.h" /* For internal PVK and "blob" headers */ +#include "crypto/pem.h" /* For internal PVK and "blob" headers */ #include "crypto/rsa.h" #include "prov/bio.h" #include "prov/implementations.h" #include "endecoder_local.h" -struct msblob2key_ctx_st; /* Forward declaration */ +struct msblob2key_ctx_st; /* Forward declaration */ typedef void *b2i_of_void_fn(const unsigned char **in, unsigned int bitlen, - int ispub); + int ispub); typedef void adjust_key_fn(void *, struct msblob2key_ctx_st *ctx); typedef void free_key_fn(void *); struct keytype_desc_st { - int type; /* EVP key type */ - const char *name; /* Keytype */ + int type; /* EVP key type */ + const char *name; /* Keytype */ const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */ b2i_of_void_fn *read_private_key; @@ -84,16 +84,15 @@ static int msblob2key_does_selection(void *provctx, int selection) if (selection == 0) return 1; - if ((selection & (OSSL_KEYMGMT_SELECT_PRIVATE_KEY - | OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) != 0) + if ((selection & (OSSL_KEYMGMT_SELECT_PRIVATE_KEY | OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) != 0) return 1; return 0; } static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, - OSSL_CALLBACK *data_cb, void *data_cbarg, - OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) + OSSL_CALLBACK *data_cb, void *data_cbarg, + OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { struct msblob2key_ctx_st *ctx = vctx; BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin); @@ -120,7 +119,7 @@ static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, goto next; ctx->selection = selection; - ok = 0; /* Assume that we fail */ + ok = 0; /* Assume that we fail */ if ((isdss && ctx->desc->type != EVP_PKEY_DSA) || (!isdss && ctx->desc->type != EVP_PKEY_RSA)) @@ -143,7 +142,7 @@ static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, } if ((selection == 0 - || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) + || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) && !ispub && ctx->desc->read_private_key != NULL) { struct ossl_passphrase_data_st pwdata; @@ -156,8 +155,7 @@ static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, if (selection != 0 && key == NULL) goto next; } - if (key == NULL && (selection == 0 - || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) + if (key == NULL && (selection == 0 || (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) && ispub && ctx->desc->read_public_key != NULL) { p = buf; @@ -169,7 +167,7 @@ static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, if (key != NULL && ctx->desc->adjust_key != NULL) ctx->desc->adjust_key(key, ctx); - next: +next: /* * Indicated that we successfully decoded something, or not at all. * Ending up "empty handed" is not an error. @@ -190,21 +188,18 @@ static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, OSSL_PARAM params[4]; int object_type = OSSL_OBJECT_PKEY; - params[0] = - OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); - params[1] = - OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, - (char *)ctx->desc->name, 0); + params[0] = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); + params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, + (char *)ctx->desc->name, 0); /* The address of the key becomes the octet string */ - params[2] = - OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, - &key, sizeof(key)); + params[2] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, + &key, sizeof(key)); params[3] = OSSL_PARAM_construct_end(); ok = data_cb(params, data_cbarg); } - end: +end: BIO_free(in); OPENSSL_free(buf); ctx->desc->free_key(key); @@ -214,12 +209,11 @@ static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, static int msblob2key_export_object(void *vctx, - const void *reference, size_t reference_sz, - OSSL_CALLBACK *export_cb, void *export_cbarg) + const void *reference, size_t reference_sz, + OSSL_CALLBACK *export_cb, void *export_cbarg) { struct msblob2key_ctx_st *ctx = vctx; - OSSL_FUNC_keymgmt_export_fn *export = - ossl_prov_get_keymgmt_export(ctx->desc->fns); + OSSL_FUNC_keymgmt_export_fn *export = ossl_prov_get_keymgmt_export(ctx->desc->fns); void *keydata; if (reference_sz == sizeof(keydata) && export != NULL) { @@ -237,53 +231,54 @@ msblob2key_export_object(void *vctx, /* ---------------------------------------------------------------------- */ -#define dsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header -#define dsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header -#define dsa_adjust NULL -#define dsa_free (void (*)(void *))DSA_free +#define dsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header +#define dsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_DSA_after_header +#define dsa_adjust NULL +#define dsa_free (void (*)(void *)) DSA_free /* ---------------------------------------------------------------------- */ -#define rsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header -#define rsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header +#define rsa_decode_private_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header +#define rsa_decode_public_key (b2i_of_void_fn *)ossl_b2i_RSA_after_header static void rsa_adjust(void *key, struct msblob2key_ctx_st *ctx) { ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); } -#define rsa_free (void (*)(void *))RSA_free +#define rsa_free (void (*)(void *)) RSA_free /* ---------------------------------------------------------------------- */ -#define IMPLEMENT_MSBLOB(KEYTYPE, keytype) \ - static const struct keytype_desc_st mstype##2##keytype##_desc = { \ - EVP_PKEY_##KEYTYPE, #KEYTYPE, \ - ossl_##keytype##_keymgmt_functions, \ - keytype##_decode_private_key, \ - keytype##_decode_public_key, \ - keytype##_adjust, \ - keytype##_free \ - }; \ - static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx; \ - static void *msblob2##keytype##_newctx(void *provctx) \ - { \ - return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \ - } \ - const OSSL_DISPATCH \ - ossl_msblob_to_##keytype##_decoder_functions[] = { \ - { OSSL_FUNC_DECODER_NEWCTX, \ - (void (*)(void))msblob2##keytype##_newctx }, \ - { OSSL_FUNC_DECODER_FREECTX, \ - (void (*)(void))msblob2key_freectx }, \ - { OSSL_FUNC_DECODER_DOES_SELECTION, \ - (void (*)(void))msblob2key_does_selection }, \ - { OSSL_FUNC_DECODER_DECODE, \ - (void (*)(void))msblob2key_decode }, \ - { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ - (void (*)(void))msblob2key_export_object }, \ - { 0, NULL } \ - } +#define IMPLEMENT_MSBLOB(KEYTYPE, keytype) \ + static const struct keytype_desc_st mstype##2##keytype##_desc = { \ + EVP_PKEY_##KEYTYPE, #KEYTYPE, \ + ossl_##keytype##_keymgmt_functions, \ + keytype##_decode_private_key, \ + keytype##_decode_public_key, \ + keytype##_adjust, \ + keytype##_free \ + }; \ + static OSSL_FUNC_decoder_newctx_fn msblob2##keytype##_newctx; \ + static void *msblob2##keytype##_newctx(void *provctx) \ + { \ + return msblob2key_newctx(provctx, &mstype##2##keytype##_desc); \ + } \ + const OSSL_DISPATCH \ + ossl_msblob_to_##keytype##_decoder_functions[] \ + = { \ + { OSSL_FUNC_DECODER_NEWCTX, \ + (void (*)(void))msblob2##keytype##_newctx }, \ + { OSSL_FUNC_DECODER_FREECTX, \ + (void (*)(void))msblob2key_freectx }, \ + { OSSL_FUNC_DECODER_DOES_SELECTION, \ + (void (*)(void))msblob2key_does_selection }, \ + { OSSL_FUNC_DECODER_DECODE, \ + (void (*)(void))msblob2key_decode }, \ + { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ + (void (*)(void))msblob2key_export_object }, \ + { 0, NULL } \ + } #ifndef OPENSSL_NO_DSA IMPLEMENT_MSBLOB(DSA, dsa); diff --git a/providers/implementations/encode_decode/decode_pem2der.c b/providers/implementations/encode_decode/decode_pem2der.c index ce21b6b80388..3bc3954b7957 100644 --- a/providers/implementations/encode_decode/decode_pem2der.c +++ b/providers/implementations/encode_decode/decode_pem2der.c @@ -29,8 +29,8 @@ #include "endecoder_local.h" static int read_pem(PROV_CTX *provctx, OSSL_CORE_BIO *cin, - char **pem_name, char **pem_header, - unsigned char **data, long *len) + char **pem_name, char **pem_header, + unsigned char **data, long *len) { BIO *in = ossl_bio_new_from_core_bio(provctx, cin); int ok; @@ -93,8 +93,8 @@ static int pem2der_pass_helper(char *buf, int num, int w, void *data) * because it's not relevant just to decode PEM to DER. */ static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, - OSSL_CALLBACK *data_cb, void *data_cbarg, - OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) + OSSL_CALLBACK *data_cb, void *data_cbarg, + OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { /* * PEM names we recognise. Other PEM names should be recognised by @@ -143,7 +143,8 @@ static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, int objtype = OSSL_OBJECT_UNKNOWN; ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header, - &der, &der_len) > 0; + &der, &der_len) + > 0; /* We return "empty handed". This is not an error. */ if (!ok) return 1; @@ -158,12 +159,12 @@ static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, EVP_CIPHER_INFO cipher; struct pem2der_pass_data_st pass_data; - ok = 0; /* Assume that we fail */ + ok = 0; /* Assume that we fail */ pass_data.cb = pw_cb; pass_data.cbarg = pw_cbarg; if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher) || !PEM_do_header(&cipher, der, &der_len, - pem2der_pass_helper, &pass_data)) + pem2der_pass_helper, &pass_data)) goto end; } @@ -186,27 +187,23 @@ static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, objtype = pem_name_map[i].object_type; if (data_type != NULL) - *p++ = - OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, - data_type, 0); + *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, + data_type, 0); /* We expect this to be read only so casting away the const is ok */ if (data_structure != NULL) - *p++ = - OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, - data_structure, 0); - *p++ = - OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, - der, der_len); - *p++ = - OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype); + *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, + data_structure, 0); + *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, + der, der_len); + *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype); *p = OSSL_PARAM_construct_end(); ok = data_cb(params, data_cbarg); } - end: +end: OPENSSL_free(pem_name); OPENSSL_free(pem_header); OPENSSL_free(der); diff --git a/providers/implementations/encode_decode/decode_pvk2key.c b/providers/implementations/encode_decode/decode_pvk2key.c index 2d7cb15e53e0..fe616ceffabc 100644 --- a/providers/implementations/encode_decode/decode_pvk2key.c +++ b/providers/implementations/encode_decode/decode_pvk2key.c @@ -21,24 +21,24 @@ #include <openssl/crypto.h> #include <openssl/params.h> #include <openssl/err.h> -#include <openssl/pem.h> /* For public PVK functions */ +#include <openssl/pem.h> /* For public PVK functions */ #include <openssl/x509.h> #include "internal/passphrase.h" -#include "crypto/pem.h" /* For internal PVK and "blob" headers */ +#include "crypto/pem.h" /* For internal PVK and "blob" headers */ #include "crypto/rsa.h" #include "prov/bio.h" #include "prov/implementations.h" #include "endecoder_local.h" -struct pvk2key_ctx_st; /* Forward declaration */ +struct pvk2key_ctx_st; /* Forward declaration */ typedef int check_key_fn(void *, struct pvk2key_ctx_st *ctx); typedef void adjust_key_fn(void *, struct pvk2key_ctx_st *ctx); typedef void *b2i_PVK_of_bio_pw_fn(BIO *in, pem_password_cb *cb, void *u, - OSSL_LIB_CTX *libctx, const char *propq); + OSSL_LIB_CTX *libctx, const char *propq); typedef void free_key_fn(void *); struct keytype_desc_st { - int type; /* EVP key type */ - const char *name; /* Keytype */ + int type; /* EVP key type */ + const char *name; /* Keytype */ const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */ b2i_PVK_of_bio_pw_fn *read_private_key; @@ -84,15 +84,15 @@ static int pvk2key_does_selection(void *provctx, int selection) if (selection == 0) return 1; - if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) + if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) return 1; return 0; } static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, - OSSL_CALLBACK *data_cb, void *data_cbarg, - OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) + OSSL_CALLBACK *data_cb, void *data_cbarg, + OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { struct pvk2key_ctx_st *ctx = vctx; BIO *in = ossl_bio_new_from_core_bio(ctx->provctx, cin); @@ -105,7 +105,7 @@ static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, ctx->selection = selection; if ((selection == 0 - || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) + || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) && ctx->desc->read_private_key != NULL) { struct ossl_passphrase_data_st pwdata; int err, lib, reason; @@ -115,7 +115,7 @@ static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, goto end; key = ctx->desc->read_private_key(in, ossl_pw_pvk_password, &pwdata, - PROV_LIBCTX_OF(ctx->provctx), NULL); + PROV_LIBCTX_OF(ctx->provctx), NULL); /* * Because the PVK API doesn't have a separate decrypt call, we need @@ -140,7 +140,7 @@ static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, if (key != NULL && ctx->desc->adjust_key != NULL) ctx->desc->adjust_key(key, ctx); - next: +next: /* * Indicated that we successfully decoded something, or not at all. * Ending up "empty handed" is not an error. @@ -159,21 +159,18 @@ static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, OSSL_PARAM params[4]; int object_type = OSSL_OBJECT_PKEY; - params[0] = - OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); - params[1] = - OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, - (char *)ctx->desc->name, 0); + params[0] = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type); + params[1] = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, + (char *)ctx->desc->name, 0); /* The address of the key becomes the octet string */ - params[2] = - OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, - &key, sizeof(key)); + params[2] = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE, + &key, sizeof(key)); params[3] = OSSL_PARAM_construct_end(); ok = data_cb(params, data_cbarg); } - end: +end: BIO_free(in); ctx->desc->free_key(key); @@ -181,12 +178,11 @@ static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, } static int pvk2key_export_object(void *vctx, - const void *reference, size_t reference_sz, - OSSL_CALLBACK *export_cb, void *export_cbarg) + const void *reference, size_t reference_sz, + OSSL_CALLBACK *export_cb, void *export_cbarg) { struct pvk2key_ctx_st *ctx = vctx; - OSSL_FUNC_keymgmt_export_fn *export = - ossl_prov_get_keymgmt_export(ctx->desc->fns); + OSSL_FUNC_keymgmt_export_fn *export = ossl_prov_get_keymgmt_export(ctx->desc->fns); void *keydata; if (reference_sz == sizeof(keydata) && export != NULL) { @@ -204,51 +200,53 @@ static int pvk2key_export_object(void *vctx, /* ---------------------------------------------------------------------- */ -#define dsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_DSA_PVK_bio_ex -#define dsa_adjust NULL -#define dsa_free (void (*)(void *))DSA_free +#define dsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_DSA_PVK_bio_ex +#define dsa_adjust NULL +#define dsa_free (void (*)(void *)) DSA_free /* ---------------------------------------------------------------------- */ -#define rsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_RSA_PVK_bio_ex +#define rsa_private_key_bio (b2i_PVK_of_bio_pw_fn *)b2i_RSA_PVK_bio_ex static void rsa_adjust(void *key, struct pvk2key_ctx_st *ctx) { ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx)); } -#define rsa_free (void (*)(void *))RSA_free +#define rsa_free (void (*)(void *)) RSA_free /* ---------------------------------------------------------------------- */ -#define IMPLEMENT_MS(KEYTYPE, keytype) \ - static const struct keytype_desc_st \ - pvk2##keytype##_desc = { \ - EVP_PKEY_##KEYTYPE, #KEYTYPE, \ - ossl_##keytype##_keymgmt_functions, \ - keytype##_private_key_bio, \ - keytype##_adjust, \ - keytype##_free \ - }; \ - static OSSL_FUNC_decoder_newctx_fn pvk2##keytype##_newctx; \ - static void *pvk2##keytype##_newctx(void *provctx) \ - { \ - return pvk2key_newctx(provctx, &pvk2##keytype##_desc); \ - } \ - const OSSL_DISPATCH \ - ossl_##pvk_to_##keytype##_decoder_functions[] = { \ - { OSSL_FUNC_DECODER_NEWCTX, \ - (void (*)(void))pvk2##keytype##_newctx }, \ - { OSSL_FUNC_DECODER_FREECTX, \ - (void (*)(void))pvk2key_freectx }, \ - { OSSL_FUNC_DECODER_DOES_SELECTION, \ - (void (*)(void))pvk2key_does_selection }, \ - { OSSL_FUNC_DECODER_DECODE, \ - (void (*)(void))pvk2key_decode }, \ - { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ - (void (*)(void))pvk2key_export_object }, \ - { 0, NULL } \ - } +#define IMPLEMENT_MS(KEYTYPE, keytype) \ + static const struct keytype_desc_st \ + pvk2##keytype##_desc \ + = { \ + EVP_PKEY_##KEYTYPE, #KEYTYPE, \ + ossl_##keytype##_keymgmt_functions, \ + keytype##_private_key_bio, \ + keytype##_adjust, \ + keytype##_free \ + }; \ + static OSSL_FUNC_decoder_newctx_fn pvk2##keytype##_newctx; \ + static void *pvk2##keytype##_newctx(void *provctx) \ + { \ + return pvk2key_newctx(provctx, &pvk2##keytype##_desc); \ + } \ + const OSSL_DISPATCH \ + ossl_##pvk_to_##keytype##_decoder_functions[] \ + = { \ + { OSSL_FUNC_DECODER_NEWCTX, \ + (void (*)(void))pvk2##keytype##_newctx }, \ + { OSSL_FUNC_DECODER_FREECTX, \ + (void (*)(void))pvk2key_freectx }, \ + { OSSL_FUNC_DECODER_DOES_SELECTION, \ + (void (*)(void))pvk2key_does_selection }, \ + { OSSL_FUNC_DECODER_DECODE, \ + (void (*)(void))pvk2key_decode }, \ + { OSSL_FUNC_DECODER_EXPORT_OBJECT, \ + (void (*)(void))pvk2key_export_object }, \ + { 0, NULL } \ + } #ifndef OPENSSL_NO_DSA IMPLEMENT_MS(DSA, dsa); diff --git a/providers/implementations/encode_decode/decode_spki2typespki.c b/providers/implementations/encode_decode/decode_spki2typespki.c index a5dbbb31adf8..a65eb74402ca 100644 --- a/providers/implementations/encode_decode/decode_spki2typespki.c +++ b/providers/implementations/encode_decode/decode_spki2typespki.c @@ -49,8 +49,8 @@ static void spki2typespki_freectx(void *vctx) } static int spki2typespki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, - OSSL_CALLBACK *data_cb, void *data_cbarg, - OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) + OSSL_CALLBACK *data_cb, void *data_cbarg, + OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) { struct spki2typespki_ctx_st *ctx = vctx; unsigned char *der, *derp; @@ -67,8 +67,7 @@ static int spki2typespki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, return 1; derp = der; xpub = ossl_d2i_X509_PUBKEY_INTERNAL((const unsigned char **)&derp, len, - PROV_LIBCTX_OF(ctx->provctx)); - + PROV_LIBCTX_OF(ctx->provctx)); if (xpub == NULL) { /* We return "empty handed". This is not an error. */ @@ -83,34 +82,30 @@ static int spki2typespki_decode(void *vctx, OSSL_CORE_BIO *cin, int selection, #ifndef OPENSSL_NO_EC /* SM2 abuses the EC oid, so this could actually be SM2 */ if (OBJ_obj2nid(oid) == NID_X9_62_id_ecPublicKey - && ossl_x509_algor_is_sm2(algor)) + && ossl_x509_algor_is_sm2(algor)) strcpy(dataname, "SM2"); else #endif - if (OBJ_obj2txt(dataname, sizeof(dataname), oid, 0) <= 0) + if (OBJ_obj2txt(dataname, sizeof(dataname), oid, 0) <= 0) goto end; ossl_X509_PUBKEY_INTERNAL_free(xpub); xpub = NULL; - *p++ = - OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, - dataname, 0); + *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE, + dataname, 0); - *p++ = - OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, - "SubjectPublicKeyInfo", - 0); - *p++ = - OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, der, len); - *p++ = - OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype); + *p++ = OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE, + "SubjectPublicKeyInfo", + 0); + *p++ = OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA, der, len); + *p++ = OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype); *p = OSSL_PARAM_construct_end(); ok = data_cb(params, data_cbarg); - end: +end: ossl_X509_PUBKEY_INTERNAL_free(xpub); OPENSSL_free(der); return ok; diff --git a/providers/implementations/encode_decode/encode_key2any.c b/providers/implementations/encode_decode/encode_key2any.c index 1430c330cf0b..8752b2a24513 100644 --- a/providers/implementations/encode_decode/encode_key2any.c +++ b/providers/implementations/encode_decode/encode_key2any.c @@ -1,5 +1,5 @@ /* - * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2020-2026 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 @@ -21,7 +21,7 @@ #include <openssl/err.h> #include <openssl/pem.h> #include <openssl/x509.h> -#include <openssl/pkcs12.h> /* PKCS8_encrypt() */ +#include <openssl/pkcs12.h> /* PKCS8_encrypt() */ #include <openssl/dh.h> #include <openssl/dsa.h> #include <openssl/ec.h> @@ -37,7 +37,7 @@ #include "endecoder_local.h" #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC) -# define OPENSSL_NO_KEYPARAMS +#define OPENSSL_NO_KEYPARAMS #endif struct key2any_ctx_st { @@ -56,18 +56,17 @@ struct key2any_ctx_st { typedef int check_key_type_fn(const void *key, int nid); typedef int key_to_paramstring_fn(const void *key, int nid, int save, - void **str, int *strtype); + void **str, int *strtype); typedef int key_to_der_fn(BIO *out, const void *key, - int key_nid, const char *pemname, - key_to_paramstring_fn *p2s, i2d_of_void *k2d, - struct key2any_ctx_st *ctx); + int key_nid, const char *pemname, + key_to_paramstring_fn *p2s, i2d_of_void *k2d, + struct key2any_ctx_st *ctx); typedef int write_bio_of_void_fn(BIO *bp, const void *x); - /* Free the blob allocated during key_to_paramstring_fn */ static void free_asn1_data(int type, void *data) { - switch(type) { + switch (type) { case V_ASN1_OBJECT: ASN1_OBJECT_free(data); break; @@ -78,8 +77,8 @@ static void free_asn1_data(int type, void *data) } static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid, - void *params, int params_type, - i2d_of_void *k2d) + void *params, int params_type, + i2d_of_void *k2d) { /* der, derlen store the key DER output and its length */ unsigned char *der = NULL; @@ -90,7 +89,7 @@ static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid, if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL || (derlen = k2d(key, &der)) <= 0 || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0, - params_type, params, der, derlen)) { + params_type, params, der, derlen)) { ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); PKCS8_PRIV_KEY_INFO_free(p8info); OPENSSL_free(der); @@ -101,7 +100,7 @@ static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid, } static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info, - struct key2any_ctx_st *ctx) + struct key2any_ctx_st *ctx) { X509_SIG *p8 = NULL; char kstr[PEM_BUFSIZE]; @@ -112,7 +111,7 @@ static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info, return NULL; if (!ossl_pw_get_passphrase(kstr, sizeof(kstr), &klen, NULL, 1, - &ctx->pwdata)) { + &ctx->pwdata)) { ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE); return NULL; } @@ -123,11 +122,10 @@ static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info, } static X509_SIG *key_to_encp8(const void *key, int key_nid, - void *params, int params_type, - i2d_of_void *k2d, struct key2any_ctx_st *ctx) + void *params, int params_type, + i2d_of_void *k2d, struct key2any_ctx_st *ctx) { - PKCS8_PRIV_KEY_INFO *p8info = - key_to_p8info(key, key_nid, params, params_type, k2d); + PKCS8_PRIV_KEY_INFO *p8info = key_to_p8info(key, key_nid, params, params_type, k2d); X509_SIG *p8 = NULL; if (p8info == NULL) { @@ -140,8 +138,8 @@ static X509_SIG *key_to_encp8(const void *key, int key_nid, } static X509_PUBKEY *key_to_pubkey(const void *key, int key_nid, - void *params, int params_type, - i2d_of_void k2d) + void *params, int params_type, + i2d_of_void k2d) { /* der, derlen store the key DER output and its length */ unsigned char *der = NULL; @@ -149,11 +147,10 @@ static X509_PUBKEY *key_to_pubkey(const void *key, int key_nid, /* The final X509_PUBKEY */ X509_PUBKEY *xpk = NULL; - if ((xpk = X509_PUBKEY_new()) == NULL || (derlen = k2d(key, &der)) <= 0 || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(key_nid), - params_type, params, der, derlen)) { + params_type, params, der, derlen)) { ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); X509_PUBKEY_free(xpk); OPENSSL_free(der); @@ -168,7 +165,7 @@ static X509_PUBKEY *key_to_pubkey(const void *key, int key_nid, * EncryptedPrivateKeyInfo structure (defined by PKCS#8). They require * that there's an intent to encrypt, anything else is an error. * - * key_to_pki_* primarly produce encoded output with the private key data + * key_to_pki_* primarily produce encoded output with the private key data * in a PrivateKeyInfo structure (also defined by PKCS#8). However, if * there is an intent to encrypt the data, the corresponding key_to_epki_* * function is used instead. @@ -183,11 +180,11 @@ static X509_PUBKEY *key_to_pubkey(const void *key, int key_nid, */ static int key_to_epki_der_priv_bio(BIO *out, const void *key, - int key_nid, - ossl_unused const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; @@ -197,8 +194,7 @@ static int key_to_epki_der_priv_bio(BIO *out, const void *key, if (!ctx->cipher_intent) return 0; - if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, - &str, &strtype)) + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx); @@ -211,11 +207,11 @@ static int key_to_epki_der_priv_bio(BIO *out, const void *key, } static int key_to_epki_pem_priv_bio(BIO *out, const void *key, - int key_nid, - ossl_unused const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; @@ -225,8 +221,7 @@ static int key_to_epki_pem_priv_bio(BIO *out, const void *key, if (!ctx->cipher_intent) return 0; - if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, - &str, &strtype)) + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx); @@ -239,11 +234,11 @@ static int key_to_epki_pem_priv_bio(BIO *out, const void *key, } static int key_to_pki_der_priv_bio(BIO *out, const void *key, - int key_nid, - ossl_unused const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; @@ -252,10 +247,9 @@ static int key_to_pki_der_priv_bio(BIO *out, const void *key, if (ctx->cipher_intent) return key_to_epki_der_priv_bio(out, key, key_nid, pemname, - p2s, k2d, ctx); + p2s, k2d, ctx); - if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, - &str, &strtype)) + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; p8info = key_to_p8info(key, key_nid, str, strtype, k2d); @@ -271,11 +265,11 @@ static int key_to_pki_der_priv_bio(BIO *out, const void *key, } static int key_to_pki_pem_priv_bio(BIO *out, const void *key, - int key_nid, - ossl_unused const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; @@ -284,10 +278,9 @@ static int key_to_pki_pem_priv_bio(BIO *out, const void *key, if (ctx->cipher_intent) return key_to_epki_pem_priv_bio(out, key, key_nid, pemname, - p2s, k2d, ctx); + p2s, k2d, ctx); - if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, - &str, &strtype)) + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; p8info = key_to_p8info(key, key_nid, str, strtype, k2d); @@ -303,19 +296,18 @@ static int key_to_pki_pem_priv_bio(BIO *out, const void *key, } static int key_to_spki_der_pub_bio(BIO *out, const void *key, - int key_nid, - ossl_unused const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; int strtype = V_ASN1_UNDEF; X509_PUBKEY *xpk = NULL; - if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, - &str, &strtype)) + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; xpk = key_to_pubkey(key, key_nid, str, strtype, k2d); @@ -329,19 +321,18 @@ static int key_to_spki_der_pub_bio(BIO *out, const void *key, } static int key_to_spki_pem_pub_bio(BIO *out, const void *key, - int key_nid, - ossl_unused const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { int ret = 0; void *str = NULL; int strtype = V_ASN1_UNDEF; X509_PUBKEY *xpk = NULL; - if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, - &str, &strtype)) + if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, &str, &strtype)) return 0; xpk = key_to_pubkey(key, key_nid, str, strtype, k2d); @@ -369,11 +360,11 @@ static int key_to_spki_pem_pub_bio(BIO *out, const void *key, * on k2d to do the complete work. */ static int key_to_type_specific_der_bio(BIO *out, const void *key, - int key_nid, - ossl_unused const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, + ossl_unused const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { unsigned char *der = NULL; int derlen; @@ -393,47 +384,47 @@ static int key_to_type_specific_der_bio(BIO *out, const void *key, #define key_to_type_specific_der_param_bio key_to_type_specific_der_bio static int key_to_type_specific_pem_bio_cb(BIO *out, const void *key, - int key_nid, const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx, - pem_password_cb *cb, void *cbarg) + int key_nid, const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx, + pem_password_cb *cb, void *cbarg) { - return - PEM_ASN1_write_bio(k2d, pemname, out, key, ctx->cipher, - NULL, 0, cb, cbarg) > 0; + return PEM_ASN1_write_bio(k2d, pemname, out, key, ctx->cipher, + NULL, 0, cb, cbarg) + > 0; } static int key_to_type_specific_pem_priv_bio(BIO *out, const void *key, - int key_nid, const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname, - p2s, k2d, ctx, - ossl_pw_pem_password, &ctx->pwdata); + p2s, k2d, ctx, + ossl_pw_pem_password, &ctx->pwdata); } static int key_to_type_specific_pem_pub_bio(BIO *out, const void *key, - int key_nid, const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname, - p2s, k2d, ctx, NULL, NULL); + p2s, k2d, ctx, NULL, NULL); } #ifndef OPENSSL_NO_KEYPARAMS static int key_to_type_specific_pem_param_bio(BIO *out, const void *key, - int key_nid, const char *pemname, - key_to_paramstring_fn *p2s, - i2d_of_void *k2d, - struct key2any_ctx_st *ctx) + int key_nid, const char *pemname, + key_to_paramstring_fn *p2s, + i2d_of_void *k2d, + struct key2any_ctx_st *ctx) { return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname, - p2s, k2d, ctx, NULL, NULL); + p2s, k2d, ctx, NULL, NULL); } #endif @@ -441,7 +432,7 @@ static int key_to_type_specific_pem_param_bio(BIO *out, const void *key, #ifndef OPENSSL_NO_DH static int prepare_dh_params(const void *dh, int nid, int save, - void **pstr, int *pstrtype) + void **pstr, int *pstrtype) { ASN1_STRING *params = ASN1_STRING_new(); @@ -509,7 +500,7 @@ static int dh_pki_priv_to_der(const void *dh, unsigned char **pder) return ret; } -# define dh_epki_priv_to_der dh_pki_priv_to_der +#define dh_epki_priv_to_der dh_pki_priv_to_der static int dh_type_specific_params_to_der(const void *dh, unsigned char **pder) { @@ -522,30 +513,29 @@ static int dh_type_specific_params_to_der(const void *dh, unsigned char **pder) * DH doesn't have i2d_DHPrivateKey or i2d_DHPublicKey, so we can't make * corresponding functions here. */ -# define dh_type_specific_priv_to_der NULL -# define dh_type_specific_pub_to_der NULL +#define dh_type_specific_priv_to_der NULL +#define dh_type_specific_pub_to_der NULL static int dh_check_key_type(const void *dh, int expected_type) { - int type = - DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH; + int type = DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH; return type == expected_type; } -# define dh_evp_type EVP_PKEY_DH -# define dhx_evp_type EVP_PKEY_DHX -# define dh_input_type "DH" -# define dhx_input_type "DHX" -# define dh_pem_type "DH" -# define dhx_pem_type "X9.42 DH" +#define dh_evp_type EVP_PKEY_DH +#define dhx_evp_type EVP_PKEY_DHX +#define dh_input_type "DH" +#define dhx_input_type "DHX" +#define dh_pem_type "DH" +#define dhx_pem_type "X9.42 DH" #endif /* ---------------------------------------------------------------------- */ #ifndef OPENSSL_NO_DSA static int encode_dsa_params(const void *dsa, int nid, - void **pstr, int *pstrtype) + void **pstr, int *pstrtype) { ASN1_STRING *params = ASN1_STRING_new(); @@ -568,7 +558,7 @@ static int encode_dsa_params(const void *dsa, int nid, } static int prepare_dsa_params(const void *dsa, int nid, int save, - void **pstr, int *pstrtype) + void **pstr, int *pstrtype) { const BIGNUM *p = DSA_get0_p(dsa); const BIGNUM *q = DSA_get0_q(dsa); @@ -624,23 +614,23 @@ static int dsa_pki_priv_to_der(const void *dsa, unsigned char **pder) return ret; } -# define dsa_epki_priv_to_der dsa_pki_priv_to_der +#define dsa_epki_priv_to_der dsa_pki_priv_to_der -# define dsa_type_specific_priv_to_der (i2d_of_void *)i2d_DSAPrivateKey -# define dsa_type_specific_pub_to_der (i2d_of_void *)i2d_DSAPublicKey -# define dsa_type_specific_params_to_der (i2d_of_void *)i2d_DSAparams +#define dsa_type_specific_priv_to_der (i2d_of_void *)i2d_DSAPrivateKey +#define dsa_type_specific_pub_to_der (i2d_of_void *)i2d_DSAPublicKey +#define dsa_type_specific_params_to_der (i2d_of_void *)i2d_DSAparams -# define dsa_check_key_type NULL -# define dsa_evp_type EVP_PKEY_DSA -# define dsa_input_type "DSA" -# define dsa_pem_type "DSA" +#define dsa_check_key_type NULL +#define dsa_evp_type EVP_PKEY_DSA +#define dsa_input_type "DSA" +#define dsa_pem_type "DSA" #endif /* ---------------------------------------------------------------------- */ #ifndef OPENSSL_NO_EC static int prepare_ec_explicit_params(const void *eckey, - void **pstr, int *pstrtype) + void **pstr, int *pstrtype) { ASN1_STRING *params = ASN1_STRING_new(); @@ -666,7 +656,7 @@ static int prepare_ec_explicit_params(const void *eckey, * is a curve name (curve nid) to be found or not. See RFC 3279 for details. */ static int prepare_ec_params(const void *eckey, int nid, int save, - void **pstr, int *pstrtype) + void **pstr, int *pstrtype) { int curve_nid; const EC_GROUP *group = EC_KEY_get0_group(eckey); @@ -728,18 +718,18 @@ static int ec_pki_priv_to_der(const void *veckey, unsigned char **pder) return ret; /* return the length of the der encoded data */ } -# define ec_epki_priv_to_der ec_pki_priv_to_der +#define ec_epki_priv_to_der ec_pki_priv_to_der -# define ec_type_specific_params_to_der (i2d_of_void *)i2d_ECParameters +#define ec_type_specific_params_to_der (i2d_of_void *)i2d_ECParameters /* No ec_type_specific_pub_to_der, there simply is no such thing */ -# define ec_type_specific_priv_to_der (i2d_of_void *)i2d_ECPrivateKey +#define ec_type_specific_priv_to_der (i2d_of_void *)i2d_ECPrivateKey -# define ec_check_key_type NULL -# define ec_evp_type EVP_PKEY_EC -# define ec_input_type "EC" -# define ec_pem_type "EC" +#define ec_check_key_type NULL +#define ec_evp_type EVP_PKEY_EC +#define ec_input_type "EC" +#define ec_pem_type "EC" -# ifndef OPENSSL_NO_SM2 +#ifndef OPENSSL_NO_SM2 /* * Albeit SM2 is a slightly different algorithm than ECDSA, the key type * encoding (in all places where an AlgorithmIdentifier is produced, such @@ -748,16 +738,16 @@ static int ec_pki_priv_to_der(const void *veckey, unsigned char **pder) * This leaves the distinction of SM2 keys to the EC group (which is found * in AlgorithmIdentified.params). */ -# define sm2_evp_type ec_evp_type -# define sm2_input_type "SM2" -# define sm2_pem_type "SM2" -# endif +#define sm2_evp_type ec_evp_type +#define sm2_input_type "SM2" +#define sm2_pem_type "SM2" +#endif #endif /* ---------------------------------------------------------------------- */ #ifndef OPENSSL_NO_EC -# define prepare_ecx_params NULL +#define prepare_ecx_params NULL static int ecx_spki_pub_to_der(const void *vecxkey, unsigned char **pder) { @@ -803,27 +793,27 @@ static int ecx_pki_priv_to_der(const void *vecxkey, unsigned char **pder) return keybloblen; } -# define ecx_epki_priv_to_der ecx_pki_priv_to_der +#define ecx_epki_priv_to_der ecx_pki_priv_to_der /* * ED25519, ED448, X25519 and X448 only has PKCS#8 / SubjectPublicKeyInfo * representation, so we don't define ecx_type_specific_[priv,pub,params]_to_der. */ -# define ecx_check_key_type NULL +#define ecx_check_key_type NULL -# define ed25519_evp_type EVP_PKEY_ED25519 -# define ed448_evp_type EVP_PKEY_ED448 -# define x25519_evp_type EVP_PKEY_X25519 -# define x448_evp_type EVP_PKEY_X448 -# define ed25519_input_type "ED25519" -# define ed448_input_type "ED448" -# define x25519_input_type "X25519" -# define x448_input_type "X448" -# define ed25519_pem_type "ED25519" -# define ed448_pem_type "ED448" -# define x25519_pem_type "X25519" -# define x448_pem_type "X448" +#define ed25519_evp_type EVP_PKEY_ED25519 +#define ed448_evp_type EVP_PKEY_ED448 +#define x25519_evp_type EVP_PKEY_X25519 +#define x448_evp_type EVP_PKEY_X448 +#define ed25519_input_type "ED25519" +#define ed448_input_type "ED448" +#define x25519_input_type "X25519" +#define x448_input_type "X448" +#define ed25519_pem_type "ED25519" +#define ed448_pem_type "ED448" +#define x25519_pem_type "X25519" +#define x448_pem_type "X448" #endif /* ---------------------------------------------------------------------- */ @@ -835,7 +825,7 @@ static int ecx_pki_priv_to_der(const void *vecxkey, unsigned char **pder) */ static int prepare_rsa_params(const void *rsa, int nid, int save, - void **pstr, int *pstrtype) + void **pstr, int *pstrtype) { const RSA_PSS_PARAMS_30 *pss = ossl_rsa_get0_pss_params_30((RSA *)rsa); @@ -896,7 +886,7 @@ static int prepare_rsa_params(const void *rsa, int nid, int save, *pstr = astr; return 1; - err: + err: OPENSSL_free(str); return 0; } @@ -910,11 +900,11 @@ static int prepare_rsa_params(const void *rsa, int nid, int save, * RSA is extremely simple, as PKCS#1 is used for the PKCS#8 |privateKey| * field as well as the SubjectPublicKeyInfo |subjectPublicKey| field. */ -#define rsa_pki_priv_to_der rsa_type_specific_priv_to_der -#define rsa_epki_priv_to_der rsa_type_specific_priv_to_der -#define rsa_spki_pub_to_der rsa_type_specific_pub_to_der -#define rsa_type_specific_priv_to_der (i2d_of_void *)i2d_RSAPrivateKey -#define rsa_type_specific_pub_to_der (i2d_of_void *)i2d_RSAPublicKey +#define rsa_pki_priv_to_der rsa_type_specific_priv_to_der +#define rsa_epki_priv_to_der rsa_type_specific_priv_to_der +#define rsa_spki_pub_to_der rsa_type_specific_pub_to_der +#define rsa_type_specific_priv_to_der (i2d_of_void *)i2d_RSAPrivateKey +#define rsa_type_specific_pub_to_der (i2d_of_void *)i2d_RSAPublicKey #define rsa_type_specific_params_to_der NULL static int rsa_check_key_type(const void *rsa, int expected_type) @@ -930,12 +920,12 @@ static int rsa_check_key_type(const void *rsa, int expected_type) return EVP_PKEY_NONE; } -#define rsa_evp_type EVP_PKEY_RSA -#define rsapss_evp_type EVP_PKEY_RSA_PSS -#define rsa_input_type "RSA" -#define rsapss_input_type "RSA-PSS" -#define rsa_pem_type "RSA" -#define rsapss_pem_type "RSA-PSS" +#define rsa_evp_type EVP_PKEY_RSA +#define rsapss_evp_type EVP_PKEY_RSA_PSS +#define rsa_input_type "RSA" +#define rsapss_input_type "RSA-PSS" +#define rsa_pem_type "RSA" +#define rsapss_pem_type "RSA-PSS" /* ---------------------------------------------------------------------- */ @@ -978,12 +968,9 @@ static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[]) { struct key2any_ctx_st *ctx = vctx; OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(ctx->provctx); - const OSSL_PARAM *cipherp = - OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER); - const OSSL_PARAM *propsp = - OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES); - const OSSL_PARAM *save_paramsp = - OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_SAVE_PARAMETERS); + const OSSL_PARAM *cipherp = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER); + const OSSL_PARAM *propsp = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES); + const OSSL_PARAM *save_paramsp = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_SAVE_PARAMETERS); if (cipherp != NULL) { const char *ciphername = NULL; @@ -998,8 +985,7 @@ static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[]) ctx->cipher = NULL; ctx->cipher_intent = ciphername != NULL; if (ciphername != NULL - && ((ctx->cipher = - EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL)) + && ((ctx->cipher = EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL)) return 0; } @@ -1044,26 +1030,25 @@ static int key2any_check_selection(int selection, int selection_mask) } static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout, - const void *key, int type, const char *pemname, - check_key_type_fn *checker, - key_to_der_fn *writer, - OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg, - key_to_paramstring_fn *key2paramstring, - i2d_of_void *key2der) + const void *key, int type, const char *pemname, + check_key_type_fn *checker, + key_to_der_fn *writer, + OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg, + key_to_paramstring_fn *key2paramstring, + i2d_of_void *key2der) { int ret = 0; if (key == NULL) { ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); } else if (writer != NULL - && (checker == NULL || checker(key, type))) { + && (checker == NULL || checker(key, type))) { BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout); if (out != NULL && (pwcb == NULL || ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, pwcb, pwcbarg))) - ret = - writer(out, key, type, pemname, key2paramstring, key2der, ctx); + ret = writer(out, key, type, pemname, key2paramstring, key2der, ctx); BIO_free(out); } else { @@ -1073,34 +1058,34 @@ static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout, } #define DO_PRIVATE_KEY_selection_mask OSSL_KEYMGMT_SELECT_PRIVATE_KEY -#define DO_PRIVATE_KEY(impl, type, kind, output) \ - if ((selection & DO_PRIVATE_KEY_selection_mask) != 0) \ - return key2any_encode(ctx, cout, key, impl##_evp_type, \ - impl##_pem_type " PRIVATE KEY", \ - type##_check_key_type, \ - key_to_##kind##_##output##_priv_bio, \ - cb, cbarg, prepare_##type##_params, \ - type##_##kind##_priv_to_der); +#define DO_PRIVATE_KEY(impl, type, kind, output) \ + if ((selection & DO_PRIVATE_KEY_selection_mask) != 0) \ + return key2any_encode(ctx, cout, key, impl##_evp_type, \ + impl##_pem_type " PRIVATE KEY", \ + type##_check_key_type, \ + key_to_##kind##_##output##_priv_bio, \ + cb, cbarg, prepare_##type##_params, \ + type##_##kind##_priv_to_der); #define DO_PUBLIC_KEY_selection_mask OSSL_KEYMGMT_SELECT_PUBLIC_KEY -#define DO_PUBLIC_KEY(impl, type, kind, output) \ - if ((selection & DO_PUBLIC_KEY_selection_mask) != 0) \ - return key2any_encode(ctx, cout, key, impl##_evp_type, \ - impl##_pem_type " PUBLIC KEY", \ - type##_check_key_type, \ - key_to_##kind##_##output##_pub_bio, \ - cb, cbarg, prepare_##type##_params, \ - type##_##kind##_pub_to_der); +#define DO_PUBLIC_KEY(impl, type, kind, output) \ + if ((selection & DO_PUBLIC_KEY_selection_mask) != 0) \ + return key2any_encode(ctx, cout, key, impl##_evp_type, \ + impl##_pem_type " PUBLIC KEY", \ + type##_check_key_type, \ + key_to_##kind##_##output##_pub_bio, \ + cb, cbarg, prepare_##type##_params, \ + type##_##kind##_pub_to_der); #define DO_PARAMETERS_selection_mask OSSL_KEYMGMT_SELECT_ALL_PARAMETERS -#define DO_PARAMETERS(impl, type, kind, output) \ - if ((selection & DO_PARAMETERS_selection_mask) != 0) \ - return key2any_encode(ctx, cout, key, impl##_evp_type, \ - impl##_pem_type " PARAMETERS", \ - type##_check_key_type, \ - key_to_##kind##_##output##_param_bio, \ - NULL, NULL, NULL, \ - type##_##kind##_params_to_der); +#define DO_PARAMETERS(impl, type, kind, output) \ + if ((selection & DO_PARAMETERS_selection_mask) != 0) \ + return key2any_encode(ctx, cout, key, impl##_evp_type, \ + impl##_pem_type " PARAMETERS", \ + type##_check_key_type, \ + key_to_##kind##_##output##_param_bio, \ + NULL, NULL, NULL, \ + type##_##kind##_params_to_der); /*- * Implement the kinds of output structure that can be produced. They are @@ -1136,16 +1121,16 @@ static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout, * passphrase callback has been passed to them. */ #define DO_PrivateKeyInfo_selection_mask DO_PRIVATE_KEY_selection_mask -#define DO_PrivateKeyInfo(impl, type, output) \ +#define DO_PrivateKeyInfo(impl, type, output) \ DO_PRIVATE_KEY(impl, type, pki, output) #define DO_EncryptedPrivateKeyInfo_selection_mask DO_PRIVATE_KEY_selection_mask -#define DO_EncryptedPrivateKeyInfo(impl, type, output) \ +#define DO_EncryptedPrivateKeyInfo(impl, type, output) \ DO_PRIVATE_KEY(impl, type, epki, output) /* SubjectPublicKeyInfo is a structure for public keys only */ #define DO_SubjectPublicKeyInfo_selection_mask DO_PUBLIC_KEY_selection_mask -#define DO_SubjectPublicKeyInfo(impl, type, output) \ +#define DO_SubjectPublicKeyInfo(impl, type, output) \ DO_PUBLIC_KEY(impl, type, spki, output) /* @@ -1164,23 +1149,23 @@ static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout, * except public key */ #define DO_type_specific_params_selection_mask DO_PARAMETERS_selection_mask -#define DO_type_specific_params(impl, type, output) \ +#define DO_type_specific_params(impl, type, output) \ DO_PARAMETERS(impl, type, type_specific, output) -#define DO_type_specific_keypair_selection_mask \ - ( DO_PRIVATE_KEY_selection_mask | DO_PUBLIC_KEY_selection_mask ) -#define DO_type_specific_keypair(impl, type, output) \ - DO_PRIVATE_KEY(impl, type, type_specific, output) \ +#define DO_type_specific_keypair_selection_mask \ + (DO_PRIVATE_KEY_selection_mask | DO_PUBLIC_KEY_selection_mask) +#define DO_type_specific_keypair(impl, type, output) \ + DO_PRIVATE_KEY(impl, type, type_specific, output) \ DO_PUBLIC_KEY(impl, type, type_specific, output) -#define DO_type_specific_selection_mask \ - ( DO_type_specific_keypair_selection_mask \ - | DO_type_specific_params_selection_mask ) -#define DO_type_specific(impl, type, output) \ - DO_type_specific_keypair(impl, type, output) \ - DO_type_specific_params(impl, type, output) +#define DO_type_specific_selection_mask \ + (DO_type_specific_keypair_selection_mask \ + | DO_type_specific_params_selection_mask) +#define DO_type_specific(impl, type, output) \ + DO_type_specific_keypair(impl, type, output) \ + DO_type_specific_params(impl, type, output) #define DO_type_specific_no_pub_selection_mask \ - ( DO_PRIVATE_KEY_selection_mask | DO_PARAMETERS_selection_mask) -#define DO_type_specific_no_pub(impl, type, output) \ - DO_PRIVATE_KEY(impl, type, type_specific, output) \ + (DO_PRIVATE_KEY_selection_mask | DO_PARAMETERS_selection_mask) +#define DO_type_specific_no_pub(impl, type, output) \ + DO_PRIVATE_KEY(impl, type, type_specific, output) \ DO_type_specific_params(impl, type, output) /* @@ -1243,71 +1228,72 @@ static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout, * * ossl_##impl##_to_##kind##_##output##_encoder_functions */ -#define MAKE_ENCODER(impl, type, evp_type, kind, output) \ - static OSSL_FUNC_encoder_import_object_fn \ - impl##_to_##kind##_##output##_import_object; \ - static OSSL_FUNC_encoder_free_object_fn \ - impl##_to_##kind##_##output##_free_object; \ - static OSSL_FUNC_encoder_encode_fn \ - impl##_to_##kind##_##output##_encode; \ - \ - static void * \ - impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \ - const OSSL_PARAM params[]) \ - { \ - struct key2any_ctx_st *ctx = vctx; \ - \ - return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \ - ctx->provctx, selection, params); \ - } \ - static void impl##_to_##kind##_##output##_free_object(void *key) \ - { \ - ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \ - } \ - static int impl##_to_##kind##_##output##_does_selection(void *ctx, \ - int selection) \ - { \ - return key2any_check_selection(selection, \ - DO_##kind##_selection_mask); \ - } \ - static int \ - impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \ - const void *key, \ - const OSSL_PARAM key_abstract[], \ - int selection, \ - OSSL_PASSPHRASE_CALLBACK *cb, \ - void *cbarg) \ - { \ - /* We don't deal with abstract objects */ \ - if (key_abstract != NULL) { \ - ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ - return 0; \ - } \ - DO_##kind(impl, type, output) \ - \ - ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ - return 0; \ - } \ - const OSSL_DISPATCH \ - ossl_##impl##_to_##kind##_##output##_encoder_functions[] = { \ - { OSSL_FUNC_ENCODER_NEWCTX, \ - (void (*)(void))key2any_newctx }, \ - { OSSL_FUNC_ENCODER_FREECTX, \ - (void (*)(void))key2any_freectx }, \ - { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \ - (void (*)(void))key2any_settable_ctx_params }, \ - { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \ - (void (*)(void))key2any_set_ctx_params }, \ - { OSSL_FUNC_ENCODER_DOES_SELECTION, \ - (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \ - { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ - (void (*)(void))impl##_to_##kind##_##output##_import_object }, \ - { OSSL_FUNC_ENCODER_FREE_OBJECT, \ - (void (*)(void))impl##_to_##kind##_##output##_free_object }, \ - { OSSL_FUNC_ENCODER_ENCODE, \ - (void (*)(void))impl##_to_##kind##_##output##_encode }, \ - { 0, NULL } \ - } +#define MAKE_ENCODER(impl, type, evp_type, kind, output) \ + static OSSL_FUNC_encoder_import_object_fn \ + impl##_to_##kind##_##output##_import_object; \ + static OSSL_FUNC_encoder_free_object_fn \ + impl##_to_##kind##_##output##_free_object; \ + static OSSL_FUNC_encoder_encode_fn \ + impl##_to_##kind##_##output##_encode; \ + \ + static void * \ + impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \ + const OSSL_PARAM params[]) \ + { \ + struct key2any_ctx_st *ctx = vctx; \ + \ + return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \ + ctx->provctx, selection, params); \ + } \ + static void impl##_to_##kind##_##output##_free_object(void *key) \ + { \ + ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \ + } \ + static int impl##_to_##kind##_##output##_does_selection(void *ctx, \ + int selection) \ + { \ + return key2any_check_selection(selection, \ + DO_##kind##_selection_mask); \ + } \ + static int \ + impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \ + const void *key, \ + const OSSL_PARAM key_abstract[], \ + int selection, \ + OSSL_PASSPHRASE_CALLBACK *cb, \ + void *cbarg) \ + { \ + /* We don't deal with abstract objects */ \ + if (key_abstract != NULL) { \ + ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ + return 0; \ + } \ + DO_##kind(impl, type, output) \ + \ + ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ + return 0; \ + } \ + const OSSL_DISPATCH \ + ossl_##impl##_to_##kind##_##output##_encoder_functions[] \ + = { \ + { OSSL_FUNC_ENCODER_NEWCTX, \ + (void (*)(void))key2any_newctx }, \ + { OSSL_FUNC_ENCODER_FREECTX, \ + (void (*)(void))key2any_freectx }, \ + { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \ + (void (*)(void))key2any_settable_ctx_params }, \ + { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \ + (void (*)(void))key2any_set_ctx_params }, \ + { OSSL_FUNC_ENCODER_DOES_SELECTION, \ + (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \ + { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ + (void (*)(void))impl##_to_##kind##_##output##_import_object }, \ + { OSSL_FUNC_ENCODER_FREE_OBJECT, \ + (void (*)(void))impl##_to_##kind##_##output##_free_object }, \ + { OSSL_FUNC_ENCODER_ENCODE, \ + (void (*)(void))impl##_to_##kind##_##output##_encode }, \ + { 0, NULL } \ + } /* * Replacements for i2d_{TYPE}PrivateKey, i2d_{TYPE}PublicKey, @@ -1323,9 +1309,9 @@ MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, type_specific, der); #endif #ifndef OPENSSL_NO_EC MAKE_ENCODER(ec, ec, EVP_PKEY_EC, type_specific_no_pub, der); -# ifndef OPENSSL_NO_SM2 +#ifndef OPENSSL_NO_SM2 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, type_specific_no_pub, der); -# endif +#endif #endif /* @@ -1342,9 +1328,9 @@ MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, type_specific, pem); #endif #ifndef OPENSSL_NO_EC MAKE_ENCODER(ec, ec, EVP_PKEY_EC, type_specific_no_pub, pem); -# ifndef OPENSSL_NO_SM2 +#ifndef OPENSSL_NO_SM2 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, type_specific_no_pub, pem); -# endif +#endif #endif /* @@ -1396,14 +1382,14 @@ MAKE_ENCODER(ec, ec, EVP_PKEY_EC, PrivateKeyInfo, der); MAKE_ENCODER(ec, ec, EVP_PKEY_EC, PrivateKeyInfo, pem); MAKE_ENCODER(ec, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, der); MAKE_ENCODER(ec, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, pem); -# ifndef OPENSSL_NO_SM2 +#ifndef OPENSSL_NO_SM2 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, EncryptedPrivateKeyInfo, der); MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, EncryptedPrivateKeyInfo, pem); MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, PrivateKeyInfo, der); MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, PrivateKeyInfo, pem); MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, der); MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, pem); -# endif +#endif MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, EncryptedPrivateKeyInfo, der); MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, EncryptedPrivateKeyInfo, pem); MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, PrivateKeyInfo, der); @@ -1453,10 +1439,10 @@ MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, DSA, pem); #ifndef OPENSSL_NO_EC MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EC, der); MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EC, pem); -# ifndef OPENSSL_NO_SM2 +#ifndef OPENSSL_NO_SM2 MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SM2, der); MAKE_ENCODER(sm2, ec, EVP_PKEY_EC, SM2, pem); -# endif +#endif #endif /* Convenience structure names */ diff --git a/providers/implementations/encode_decode/encode_key2blob.c b/providers/implementations/encode_decode/encode_key2blob.c index 550bceb09f58..0ce0401dec1b 100644 --- a/providers/implementations/encode_decode/encode_key2blob.c +++ b/providers/implementations/encode_decode/encode_key2blob.c @@ -27,7 +27,7 @@ #include "endecoder_local.h" static int write_blob(void *provctx, OSSL_CORE_BIO *cout, - void *data, int len) + void *data, int len) { BIO *out = ossl_bio_new_from_core_bio(provctx, cout); int ret; @@ -86,7 +86,7 @@ static int key2blob_check_selection(int selection, int selection_mask) } static int key2blob_encode(void *vctx, const void *key, int selection, - OSSL_CORE_BIO *cout) + OSSL_CORE_BIO *cout) { int pubkey_len = 0, ok = 0; unsigned char *pubkey = NULL; @@ -118,62 +118,62 @@ static int key2blob_encode(void *vctx, const void *key, int selection, * EVP_PKEY_##selection_name are convenience macros that combine "typical" * OSSL_KEYMGMT_SELECT_ macros for a certain type of EVP_PKEY content. */ -#define MAKE_BLOB_ENCODER(impl, type, selection_name) \ - static OSSL_FUNC_encoder_import_object_fn \ - impl##2blob_import_object; \ - static OSSL_FUNC_encoder_free_object_fn impl##2blob_free_object; \ - static OSSL_FUNC_encoder_does_selection_fn \ - impl##2blob_does_selection; \ - static OSSL_FUNC_encoder_encode_fn impl##2blob_encode; \ - \ - static void *impl##2blob_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##2blob_free_object(void *key) \ - { \ - ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \ - } \ - static int impl##2blob_does_selection(void *ctx, int selection) \ - { \ - return key2blob_check_selection(selection, \ - EVP_PKEY_##selection_name); \ - } \ - static int impl##2blob_encode(void *vctx, OSSL_CORE_BIO *cout, \ - const void *key, \ - const OSSL_PARAM key_abstract[], \ - int selection, \ - OSSL_PASSPHRASE_CALLBACK *cb, \ - void *cbarg) \ - { \ - /* We don't deal with abstract objects */ \ - if (key_abstract != NULL) { \ - ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ - return 0; \ - } \ - return key2blob_encode(vctx, key, selection, cout); \ - } \ - const OSSL_DISPATCH ossl_##impl##_to_blob_encoder_functions[] = { \ - { OSSL_FUNC_ENCODER_NEWCTX, \ - (void (*)(void))key2blob_newctx }, \ - { OSSL_FUNC_ENCODER_FREECTX, \ - (void (*)(void))key2blob_freectx }, \ - { OSSL_FUNC_ENCODER_DOES_SELECTION, \ - (void (*)(void))impl##2blob_does_selection }, \ - { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ - (void (*)(void))impl##2blob_import_object }, \ - { OSSL_FUNC_ENCODER_FREE_OBJECT, \ - (void (*)(void))impl##2blob_free_object }, \ - { OSSL_FUNC_ENCODER_ENCODE, \ - (void (*)(void))impl##2blob_encode }, \ - { 0, NULL } \ +#define MAKE_BLOB_ENCODER(impl, type, selection_name) \ + static OSSL_FUNC_encoder_import_object_fn \ + impl##2blob_import_object; \ + static OSSL_FUNC_encoder_free_object_fn impl##2blob_free_object; \ + static OSSL_FUNC_encoder_does_selection_fn \ + impl##2blob_does_selection; \ + static OSSL_FUNC_encoder_encode_fn impl##2blob_encode; \ + \ + static void *impl##2blob_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##2blob_free_object(void *key) \ + { \ + ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \ + } \ + static int impl##2blob_does_selection(void *ctx, int selection) \ + { \ + return key2blob_check_selection(selection, \ + EVP_PKEY_##selection_name); \ + } \ + static int impl##2blob_encode(void *vctx, OSSL_CORE_BIO *cout, \ + const void *key, \ + const OSSL_PARAM key_abstract[], \ + int selection, \ + OSSL_PASSPHRASE_CALLBACK *cb, \ + void *cbarg) \ + { \ + /* We don't deal with abstract objects */ \ + if (key_abstract != NULL) { \ + ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ + return 0; \ + } \ + return key2blob_encode(vctx, key, selection, cout); \ + } \ + const OSSL_DISPATCH ossl_##impl##_to_blob_encoder_functions[] = { \ + { OSSL_FUNC_ENCODER_NEWCTX, \ + (void (*)(void))key2blob_newctx }, \ + { OSSL_FUNC_ENCODER_FREECTX, \ + (void (*)(void))key2blob_freectx }, \ + { OSSL_FUNC_ENCODER_DOES_SELECTION, \ + (void (*)(void))impl##2blob_does_selection }, \ + { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ + (void (*)(void))impl##2blob_import_object }, \ + { OSSL_FUNC_ENCODER_FREE_OBJECT, \ + (void (*)(void))impl##2blob_free_object }, \ + { OSSL_FUNC_ENCODER_ENCODE, \ + (void (*)(void))impl##2blob_encode }, \ + { 0, NULL } \ } #ifndef OPENSSL_NO_EC MAKE_BLOB_ENCODER(ec, ec, PUBLIC_KEY); -# ifndef OPENSSL_NO_SM2 +#ifndef OPENSSL_NO_SM2 MAKE_BLOB_ENCODER(sm2, ec, PUBLIC_KEY); -# endif +#endif #endif diff --git a/providers/implementations/encode_decode/encode_key2ms.c b/providers/implementations/encode_decode/encode_key2ms.c index fe8c2dce4316..54ac414a5193 100644 --- a/providers/implementations/encode_decode/encode_key2ms.c +++ b/providers/implementations/encode_decode/encode_key2ms.c @@ -18,7 +18,7 @@ #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/pem.h> /* Functions for writing MSBLOB and PVK */ #include <openssl/dsa.h> #include "internal/passphrase.h" #include "crypto/rsa.h" @@ -36,7 +36,7 @@ struct key2ms_ctx_st { }; static int write_msblob(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout, - EVP_PKEY *pkey, int ispub) + EVP_PKEY *pkey, int ispub) { BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout); int ret; @@ -50,7 +50,7 @@ static int write_msblob(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout, } static int write_pvk(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout, - EVP_PKEY *pkey) + EVP_PKEY *pkey) { BIO *out = NULL; int ret; @@ -60,7 +60,7 @@ static int write_pvk(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout, if (out == NULL) return 0; ret = i2b_PVK_bio_ex(out, pkey, ctx->pvk_encr_level, - ossl_pw_pvk_password, &ctx->pwdata, libctx, NULL); + ossl_pw_pvk_password, &ctx->pwdata, libctx, NULL); BIO_free(out); return ret; } @@ -126,8 +126,8 @@ static int key2ms_does_selection(void *vctx, int selection) 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) + 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; @@ -139,7 +139,7 @@ static int key2msblob_encode(void *vctx, const void *key, int selection, else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) ispub = 1; else - return 0; /* Error */ + return 0; /* Error */ if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key)) ok = write_msblob(ctx, cout, pkey, ispub); @@ -148,15 +148,15 @@ static int key2msblob_encode(void *vctx, const void *key, int selection, } 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) + 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 */ + return 0; /* Error */ if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key) && (pw_cb == NULL @@ -166,39 +166,39 @@ static int key2pvk_encode(void *vctx, const void *key, int selection, 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 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 \ - { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \ - (void (*)(void))key2pvk_settable_ctx_params }, \ - { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \ - (void (*)(void))key2pvk_set_ctx_params }, +#define pvk_set_params \ + { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \ + (void (*)(void))key2pvk_settable_ctx_params }, \ + { OSSL_FUNC_ENCODER_SET_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; \ + 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[]) \ + 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); \ + 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, \ - OSSL_PASSPHRASE_CALLBACK *cb, \ - void *cbarg) \ + const void *key, \ + const OSSL_PARAM key_abstract[], \ + int selection, \ + OSSL_PASSPHRASE_CALLBACK *cb, \ + void *cbarg) \ { \ /* We don't deal with abstract objects */ \ if (key_abstract != NULL) { \ @@ -206,22 +206,21 @@ static int key2pvk_encode(void *vctx, const void *key, int selection, return 0; \ } \ return key2##output##_encode(vctx, key, selection, cout, type##_set1, \ - cb, cbarg); \ + cb, cbarg); \ } \ const OSSL_DISPATCH ossl_##impl##_to_##output##_encoder_functions[] = { \ { OSSL_FUNC_ENCODER_NEWCTX, \ - (void (*)(void))key2ms_newctx }, \ + (void (*)(void))key2ms_newctx }, \ { OSSL_FUNC_ENCODER_FREECTX, \ - (void (*)(void))key2ms_freectx }, \ - output##_set_params \ - { OSSL_FUNC_ENCODER_DOES_SELECTION, \ - (void (*)(void))key2ms_does_selection }, \ + (void (*)(void))key2ms_freectx }, \ + output##_set_params { OSSL_FUNC_ENCODER_DOES_SELECTION, \ + (void (*)(void))key2ms_does_selection }, \ { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ - (void (*)(void))impl##2##output##_import_object }, \ + (void (*)(void))impl##2##output##_import_object }, \ { OSSL_FUNC_ENCODER_FREE_OBJECT, \ - (void (*)(void))impl##2##output##_free_object }, \ + (void (*)(void))impl##2##output##_free_object }, \ { OSSL_FUNC_ENCODER_ENCODE, \ - (void (*)(void))impl##2##output##_encode }, \ + (void (*)(void))impl##2##output##_encode }, \ { 0, NULL } \ } diff --git a/providers/implementations/encode_decode/encode_key2text.c b/providers/implementations/encode_decode/encode_key2text.c index 363031e83dbe..d23b6152fb37 100644 --- a/providers/implementations/encode_decode/encode_key2text.c +++ b/providers/implementations/encode_decode/encode_key2text.c @@ -1,5 +1,5 @@ /* - * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2020-2026 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 @@ -22,32 +22,32 @@ #include <openssl/safestack.h> #include <openssl/proverr.h> #include "internal/ffc.h" -#include "crypto/bn.h" /* bn_get_words() */ -#include "crypto/dh.h" /* ossl_dh_get0_params() */ -#include "crypto/dsa.h" /* ossl_dsa_get0_params() */ -#include "crypto/ec.h" /* ossl_ec_key_get_libctx */ -#include "crypto/ecx.h" /* ECX_KEY, etc... */ -#include "crypto/rsa.h" /* RSA_PSS_PARAMS_30, etc... */ +#include "crypto/bn.h" /* bn_get_words() */ +#include "crypto/dh.h" /* ossl_dh_get0_params() */ +#include "crypto/dsa.h" /* ossl_dsa_get0_params() */ +#include "crypto/ec.h" /* ossl_ec_key_get_libctx */ +#include "crypto/ecx.h" /* ECX_KEY, etc... */ +#include "crypto/rsa.h" /* RSA_PSS_PARAMS_30, etc... */ #include "prov/bio.h" #include "prov/implementations.h" #include "endecoder_local.h" DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM) -# ifdef SIXTY_FOUR_BIT_LONG -# define BN_FMTu "%lu" -# define BN_FMTx "%lx" -# endif +#ifdef SIXTY_FOUR_BIT_LONG +#define BN_FMTu "%lu" +#define BN_FMTx "%lx" +#endif -# ifdef SIXTY_FOUR_BIT -# define BN_FMTu "%llu" -# define BN_FMTx "%llx" -# endif +#ifdef SIXTY_FOUR_BIT +#define BN_FMTu "%llu" +#define BN_FMTx "%llx" +#endif -# ifdef THIRTY_TWO_BIT -# define BN_FMTu "%u" -# define BN_FMTx "%x" -# endif +#ifdef THIRTY_TWO_BIT +#define BN_FMTu "%u" +#define BN_FMTx "%x" +#endif static int print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn) { @@ -76,7 +76,7 @@ static int print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn) neg = "-"; return BIO_printf(out, "%s%s%s" BN_FMTu " (%s0x" BN_FMTx ")\n", - label, post_label_spc, neg, words[0], neg, words[0]); + label, post_label_spc, neg, words[0], neg, words[0]); } hex_str = BN_bn2hex(bn); @@ -109,11 +109,12 @@ static int print_labeled_bignum(BIO *out, const char *label, const BIGNUM *bn) if ((bytes % 15) == 0 && bytes > 0) { if (BIO_printf(out, ":\n%s", spaces) <= 0) goto err; - use_sep = 0; /* The first byte on the next line doesnt have a : */ + use_sep = 0; /* The first byte on the next line doesn't have a : */ } if (BIO_printf(out, "%s%c%c", use_sep ? ":" : "", - tolower((unsigned char)p[0]), - tolower((unsigned char)p[1])) <= 0) + tolower((unsigned char)p[0]), + tolower((unsigned char)p[1])) + <= 0) goto err; ++bytes; p += 2; @@ -128,11 +129,11 @@ err: } /* Number of octets per line */ -#define LABELED_BUF_PRINT_WIDTH 15 +#define LABELED_BUF_PRINT_WIDTH 15 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC) static int print_labeled_buf(BIO *out, const char *label, - const unsigned char *buf, size_t buflen) + const unsigned char *buf, size_t buflen) { size_t i; @@ -148,7 +149,8 @@ static int print_labeled_buf(BIO *out, const char *label, } if (BIO_printf(out, "%02x%s", buf[i], - (i == buflen - 1) ? "" : ":") <= 0) + (i == buflen - 1) ? "" : ":") + <= 0) return 0; } if (BIO_printf(out, "\n") <= 0) @@ -277,14 +279,15 @@ static int dh_to_text(BIO *out, const void *key, int selection) length = DH_get_length(dh); if (length > 0 && BIO_printf(out, "recommended-private-length: %ld bits\n", - length) <= 0) + length) + <= 0) return 0; return 1; } -# define dh_input_type "DH" -# define dhx_input_type "DHX" +#define dh_input_type "DH" +#define dhx_input_type "DHX" #endif /* ---------------------------------------------------------------------- */ @@ -353,14 +356,14 @@ static int dsa_to_text(BIO *out, const void *key, int selection) return 1; } -# define dsa_input_type "DSA" +#define dsa_input_type "DSA" #endif /* ---------------------------------------------------------------------- */ #ifndef OPENSSL_NO_EC static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group, - BN_CTX *ctx) + BN_CTX *ctx) { const char *plabel = "Prime:"; BIGNUM *p = NULL, *a = NULL, *b = NULL; @@ -387,7 +390,7 @@ static int ec_param_explicit_curve_to_text(BIO *out, const EC_GROUP *group, } static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group, - BN_CTX *ctx) + BN_CTX *ctx) { int ret; size_t buflen; @@ -404,8 +407,8 @@ static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group, switch (form) { case POINT_CONVERSION_COMPRESSED: - glabel = "Generator (compressed):"; - break; + glabel = "Generator (compressed):"; + break; case POINT_CONVERSION_UNCOMPRESSED: glabel = "Generator (uncompressed):"; break; @@ -427,7 +430,7 @@ static int ec_param_explicit_gen_to_text(BIO *out, const EC_GROUP *group, /* Print explicit parameters */ static int ec_param_explicit_to_text(BIO *out, const EC_GROUP *group, - OSSL_LIB_CTX *libctx) + OSSL_LIB_CTX *libctx) { int ret = 0, tmp_nid; BN_CTX *ctx = NULL; @@ -468,7 +471,7 @@ err: } static int ec_param_to_text(BIO *out, const EC_GROUP *group, - OSSL_LIB_CTX *libctx) + OSSL_LIB_CTX *libctx) { if (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE) { const char *curve_name; @@ -483,7 +486,7 @@ static int ec_param_to_text(BIO *out, const EC_GROUP *group, curve_name = EC_curve_nid2nist(curve_nid); return (curve_name == NULL - || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0); + || BIO_printf(out, "%s: %s\n", "NIST CURVE", curve_name) > 0); } else { return ec_param_explicit_to_text(out, group, libctx); } @@ -542,7 +545,8 @@ static int ec_to_text(BIO *out, const void *key, int selection) if (type_label != NULL && BIO_printf(out, "%s: (%d bit)\n", type_label, - EC_GROUP_order_bits(group)) <= 0) + EC_GROUP_order_bits(group)) + <= 0) goto err; if (priv != NULL && !print_labeled_buf(out, "priv:", priv, priv_len)) @@ -558,11 +562,11 @@ err: return ret; } -# define ec_input_type "EC" +#define ec_input_type "EC" -# ifndef OPENSSL_NO_SM2 -# define sm2_input_type "SM2" -# endif +#ifndef OPENSSL_NO_SM2 +#define sm2_input_type "SM2" +#endif #endif /* ---------------------------------------------------------------------- */ @@ -620,10 +624,10 @@ static int ecx_to_text(BIO *out, const void *key, int selection) return 1; } -# define ed25519_input_type "ED25519" -# define ed448_input_type "ED448" -# define x25519_input_type "X25519" -# define x448_input_type "X448" +#define ed25519_input_type "ED25519" +#define ed448_input_type "ED448" +#define x25519_input_type "X25519" +#define x448_input_type "X448" #endif /* ---------------------------------------------------------------------- */ @@ -672,11 +676,13 @@ static int rsa_to_text(BIO *out, const void *key, int selection) if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { if (BIO_printf(out, "%s: (%d bit, %d primes)\n", - type_label, BN_num_bits(rsa_n), primes) <= 0) + type_label, BN_num_bits(rsa_n), primes) + <= 0) goto err; } else { if (BIO_printf(out, "%s: (%d bit)\n", - type_label, BN_num_bits(rsa_n)) <= 0) + type_label, BN_num_bits(rsa_n)) + <= 0) goto err; } @@ -690,35 +696,35 @@ static int rsa_to_text(BIO *out, const void *key, int selection) if (!print_labeled_bignum(out, "privateExponent:", rsa_d)) goto err; if (!print_labeled_bignum(out, "prime1:", - sk_BIGNUM_const_value(factors, 0))) + sk_BIGNUM_const_value(factors, 0))) goto err; if (!print_labeled_bignum(out, "prime2:", - sk_BIGNUM_const_value(factors, 1))) + sk_BIGNUM_const_value(factors, 1))) goto err; if (!print_labeled_bignum(out, "exponent1:", - sk_BIGNUM_const_value(exps, 0))) + sk_BIGNUM_const_value(exps, 0))) goto err; if (!print_labeled_bignum(out, "exponent2:", - sk_BIGNUM_const_value(exps, 1))) + sk_BIGNUM_const_value(exps, 1))) goto err; if (!print_labeled_bignum(out, "coefficient:", - sk_BIGNUM_const_value(coeffs, 0))) + sk_BIGNUM_const_value(coeffs, 0))) goto err; for (i = 2; i < sk_BIGNUM_const_num(factors); i++) { if (BIO_printf(out, "prime%d:", i + 1) <= 0) goto err; if (!print_labeled_bignum(out, NULL, - sk_BIGNUM_const_value(factors, i))) + sk_BIGNUM_const_value(factors, i))) goto err; if (BIO_printf(out, "exponent%d:", i + 1) <= 0) goto err; if (!print_labeled_bignum(out, NULL, - sk_BIGNUM_const_value(exps, i))) + sk_BIGNUM_const_value(exps, i))) goto err; if (BIO_printf(out, "coefficient%d:", i + 1) <= 0) goto err; if (!print_labeled_bignum(out, NULL, - sk_BIGNUM_const_value(coeffs, i - 1))) + sk_BIGNUM_const_value(coeffs, i - 1))) goto err; } } @@ -737,35 +743,38 @@ static int rsa_to_text(BIO *out, const void *key, int selection) goto err; } else { int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss_params); - int maskgenalg_nid = - ossl_rsa_pss_params_30_maskgenalg(pss_params); - int maskgenhashalg_nid = - ossl_rsa_pss_params_30_maskgenhashalg(pss_params); + int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss_params); + int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss_params); int saltlen = ossl_rsa_pss_params_30_saltlen(pss_params); - int trailerfield = - ossl_rsa_pss_params_30_trailerfield(pss_params); + int trailerfield = ossl_rsa_pss_params_30_trailerfield(pss_params); if (BIO_printf(out, "PSS parameter restrictions:\n") <= 0) goto err; if (BIO_printf(out, " Hash Algorithm: %s%s\n", - ossl_rsa_oaeppss_nid2name(hashalg_nid), - (hashalg_nid == NID_sha1 - ? " (default)" : "")) <= 0) + ossl_rsa_oaeppss_nid2name(hashalg_nid), + (hashalg_nid == NID_sha1 + ? " (default)" + : "")) + <= 0) goto err; if (BIO_printf(out, " Mask Algorithm: %s with %s%s\n", - ossl_rsa_mgf_nid2name(maskgenalg_nid), - ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid), - (maskgenalg_nid == NID_mgf1 - && maskgenhashalg_nid == NID_sha1 - ? " (default)" : "")) <= 0) + ossl_rsa_mgf_nid2name(maskgenalg_nid), + ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid), + (maskgenalg_nid == NID_mgf1 + && maskgenhashalg_nid == NID_sha1 + ? " (default)" + : "")) + <= 0) goto err; if (BIO_printf(out, " Minimum Salt Length: %d%s\n", - saltlen, - (saltlen == 20 ? " (default)" : "")) <= 0) + saltlen, + (saltlen == 20 ? " (default)" : "")) + <= 0) goto err; if (BIO_printf(out, " Trailer Field: 0x%x%s\n", - trailerfield, - (trailerfield == 1 ? " (default)" : "")) <= 0) + trailerfield, + (trailerfield == 1 ? " (default)" : "")) + <= 0) goto err; } break; @@ -773,15 +782,15 @@ static int rsa_to_text(BIO *out, const void *key, int selection) } ret = 1; - err: +err: sk_BIGNUM_const_free(factors); sk_BIGNUM_const_free(exps); sk_BIGNUM_const_free(coeffs); return ret; } -#define rsa_input_type "RSA" -#define rsapss_input_type "RSA-PSS" +#define rsa_input_type "RSA" +#define rsapss_input_type "RSA-PSS" /* ---------------------------------------------------------------------- */ @@ -795,10 +804,10 @@ static void key2text_freectx(ossl_unused void *vctx) } static int key2text_encode(void *vctx, const void *key, int selection, - OSSL_CORE_BIO *cout, - int (*key2text)(BIO *out, const void *key, - int selection), - OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) + OSSL_CORE_BIO *cout, + int (*key2text)(BIO *out, const void *key, + int selection), + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) { BIO *out = ossl_bio_new_from_core_bio(vctx, cout); int ret; @@ -812,50 +821,50 @@ static int key2text_encode(void *vctx, const void *key, int selection, return ret; } -#define MAKE_TEXT_ENCODER(impl, type) \ - static OSSL_FUNC_encoder_import_object_fn \ - impl##2text_import_object; \ - static OSSL_FUNC_encoder_free_object_fn \ - impl##2text_free_object; \ - static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \ - \ - static void *impl##2text_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##2text_free_object(void *key) \ - { \ - ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \ - } \ - static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \ - const void *key, \ - const OSSL_PARAM key_abstract[], \ - int selection, \ - OSSL_PASSPHRASE_CALLBACK *cb, \ - void *cbarg) \ - { \ - /* We don't deal with abstract objects */ \ - if (key_abstract != NULL) { \ - ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ - return 0; \ - } \ - return key2text_encode(vctx, key, selection, cout, \ - type##_to_text, cb, cbarg); \ - } \ - const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \ - { OSSL_FUNC_ENCODER_NEWCTX, \ - (void (*)(void))key2text_newctx }, \ - { OSSL_FUNC_ENCODER_FREECTX, \ - (void (*)(void))key2text_freectx }, \ - { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ - (void (*)(void))impl##2text_import_object }, \ - { OSSL_FUNC_ENCODER_FREE_OBJECT, \ - (void (*)(void))impl##2text_free_object }, \ - { OSSL_FUNC_ENCODER_ENCODE, \ - (void (*)(void))impl##2text_encode }, \ - { 0, NULL } \ +#define MAKE_TEXT_ENCODER(impl, type) \ + static OSSL_FUNC_encoder_import_object_fn \ + impl##2text_import_object; \ + static OSSL_FUNC_encoder_free_object_fn \ + impl##2text_free_object; \ + static OSSL_FUNC_encoder_encode_fn impl##2text_encode; \ + \ + static void *impl##2text_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##2text_free_object(void *key) \ + { \ + ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \ + } \ + static int impl##2text_encode(void *vctx, OSSL_CORE_BIO *cout, \ + const void *key, \ + const OSSL_PARAM key_abstract[], \ + int selection, \ + OSSL_PASSPHRASE_CALLBACK *cb, \ + void *cbarg) \ + { \ + /* We don't deal with abstract objects */ \ + if (key_abstract != NULL) { \ + ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ + return 0; \ + } \ + return key2text_encode(vctx, key, selection, cout, \ + type##_to_text, cb, cbarg); \ + } \ + const OSSL_DISPATCH ossl_##impl##_to_text_encoder_functions[] = { \ + { OSSL_FUNC_ENCODER_NEWCTX, \ + (void (*)(void))key2text_newctx }, \ + { OSSL_FUNC_ENCODER_FREECTX, \ + (void (*)(void))key2text_freectx }, \ + { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ + (void (*)(void))impl##2text_import_object }, \ + { OSSL_FUNC_ENCODER_FREE_OBJECT, \ + (void (*)(void))impl##2text_free_object }, \ + { OSSL_FUNC_ENCODER_ENCODE, \ + (void (*)(void))impl##2text_encode }, \ + { 0, NULL } \ } #ifndef OPENSSL_NO_DH @@ -867,9 +876,9 @@ MAKE_TEXT_ENCODER(dsa, dsa); #endif #ifndef OPENSSL_NO_EC MAKE_TEXT_ENCODER(ec, ec); -# ifndef OPENSSL_NO_SM2 +#ifndef OPENSSL_NO_SM2 MAKE_TEXT_ENCODER(sm2, ec); -# endif +#endif MAKE_TEXT_ENCODER(ed25519, ecx); MAKE_TEXT_ENCODER(ed448, ecx); MAKE_TEXT_ENCODER(x25519, ecx); diff --git a/providers/implementations/encode_decode/endecoder_common.c b/providers/implementations/encode_decode/endecoder_common.c index c4ea2f853cfc..2e5b39242a71 100644 --- a/providers/implementations/encode_decode/endecoder_common.c +++ b/providers/implementations/encode_decode/endecoder_common.c @@ -58,12 +58,11 @@ ossl_prov_get_keymgmt_export(const OSSL_DISPATCH *fns) } void *ossl_prov_import_key(const OSSL_DISPATCH *fns, void *provctx, - int selection, const OSSL_PARAM params[]) + int selection, const OSSL_PARAM params[]) { OSSL_FUNC_keymgmt_new_fn *kmgmt_new = ossl_prov_get_keymgmt_new(fns); OSSL_FUNC_keymgmt_free_fn *kmgmt_free = ossl_prov_get_keymgmt_free(fns); - OSSL_FUNC_keymgmt_import_fn *kmgmt_import = - ossl_prov_get_keymgmt_import(fns); + OSSL_FUNC_keymgmt_import_fn *kmgmt_import = ossl_prov_get_keymgmt_import(fns); void *key = NULL; if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) { @@ -84,8 +83,8 @@ void ossl_prov_free_key(const OSSL_DISPATCH *fns, void *key) kmgmt_free(key); } -int ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data, - long *len) +int ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data, + long *len) { BUF_MEM *mem = NULL; BIO *in = ossl_bio_new_from_core_bio(provctx, cin); diff --git a/providers/implementations/encode_decode/endecoder_local.h b/providers/implementations/encode_decode/endecoder_local.h index a65d05ffaeac..2576812be2df 100644 --- a/providers/implementations/encode_decode/endecoder_local.h +++ b/providers/implementations/encode_decode/endecoder_local.h @@ -18,11 +18,11 @@ OSSL_FUNC_keymgmt_import_fn *ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *f OSSL_FUNC_keymgmt_export_fn *ossl_prov_get_keymgmt_export(const OSSL_DISPATCH *fns); int ossl_prov_der_from_p8(unsigned char **new_der, long *new_der_len, - unsigned char *input_der, long input_der_len, - OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg); + unsigned char *input_der, long input_der_len, + OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg); void *ossl_prov_import_key(const OSSL_DISPATCH *fns, void *provctx, - int selection, const OSSL_PARAM params[]); + int selection, const OSSL_PARAM params[]); void ossl_prov_free_key(const OSSL_DISPATCH *fns, void *key); -int ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data, - long *len); +int ossl_read_der(PROV_CTX *provctx, OSSL_CORE_BIO *cin, unsigned char **data, + long *len); |
