summaryrefslogtreecommitdiffstats
path: root/crypto/encode_decode/decoder_pkey.c
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2020-08-02 12:14:19 +0200
committerRichard Levitte <levitte@openssl.org>2020-08-24 10:02:25 +0200
commita517edec0385280e31e2dc2912301501e6b0c4a3 (patch)
tree78538835d6f1fcc93a7c1a637931bf1f1f23d127 /crypto/encode_decode/decoder_pkey.c
parent14c8a3d118e3ec5d0179d45c7f227d29a52f7697 (diff)
CORE: Generalise internal pass phrase prompter
The pass phrase prompter that's part of OSSL_ENCODER and OSSL_DECODER is really a passphrase callback bridge between the diverse forms of prompters that exist within OpenSSL: pem_password_cb, ui_method and OSSL_PASSPHRASE_CALLBACK. This can be generalised, to be re-used by other parts of OpenSSL, and to thereby allow the users to specify whatever form of pass phrase callback they need, while being able to pass that on to other APIs that are called internally, in the form that those APIs demand. Additionally, we throw in the possibility to cache pass phrases during a "session" (we leave it to each API to define what a "session" is). This is useful for any API that implements discovery and therefore may need to get the same password more than once, such as OSSL_DECODER and OSSL_STORE. Reviewed-by: Shane Lontis <shane.lontis@oracle.com> (Merged from https://github.com/openssl/openssl/pull/12512)
Diffstat (limited to 'crypto/encode_decode/decoder_pkey.c')
-rw-r--r--crypto/encode_decode/decoder_pkey.c65
1 files changed, 3 insertions, 62 deletions
diff --git a/crypto/encode_decode/decoder_pkey.c b/crypto/encode_decode/decoder_pkey.c
index db75041d17..92c0d5a6ea 100644
--- a/crypto/encode_decode/decoder_pkey.c
+++ b/crypto/encode_decode/decoder_pkey.c
@@ -20,79 +20,20 @@ int OSSL_DECODER_CTX_set_passphrase(OSSL_DECODER_CTX *ctx,
const unsigned char *kstr,
size_t klen)
{
- if (!ossl_assert(ctx != NULL)) {
- ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
- return 0;
- }
-
- OPENSSL_clear_free(ctx->cached_passphrase, ctx->cached_passphrase_len);
- ctx->cached_passphrase = NULL;
- ctx->cached_passphrase_len = 0;
- if (kstr != NULL) {
- if (klen == 0) {
- ctx->cached_passphrase = OPENSSL_zalloc(1);
- ctx->cached_passphrase_len = 0;
- } else {
- ctx->cached_passphrase = OPENSSL_memdup(kstr, klen);
- ctx->cached_passphrase_len = klen;
- }
- if (ctx->cached_passphrase == NULL) {
- ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
- return 0;
- }
- }
- ctx->flag_user_passphrase = 1;
- return 1;
-}
-
-static void decoder_ctx_reset_passphrase_ui(OSSL_DECODER_CTX *ctx)
-{
- UI_destroy_method(ctx->allocated_ui_method);
- ctx->allocated_ui_method = NULL;
- ctx->ui_method = NULL;
- ctx->ui_data = NULL;
+ return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen);
}
int OSSL_DECODER_CTX_set_passphrase_ui(OSSL_DECODER_CTX *ctx,
const UI_METHOD *ui_method,
void *ui_data)
{
- if (!ossl_assert(ctx != NULL)) {
- ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
- return 0;
- }
-
- decoder_ctx_reset_passphrase_ui(ctx);
- ctx->ui_method = ui_method;
- ctx->ui_data = ui_data;
- return 1;
+ return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data);
}
int OSSL_DECODER_CTX_set_pem_password_cb(OSSL_DECODER_CTX *ctx,
pem_password_cb *cb, void *cbarg)
{
- UI_METHOD *ui_method = NULL;
-
- if (!ossl_assert(ctx != NULL)) {
- ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
- return 0;
- }
-
- /*
- * If |cb| is NULL, it means the caller wants to reset previous
- * password callback info. Otherwise, we only set the new data
- * if a new UI_METHOD could be created for this sort of callback.
- */
- if (cb == NULL
- || (ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) != NULL) {
- decoder_ctx_reset_passphrase_ui(ctx);
- ctx->ui_method = ctx->allocated_ui_method = ui_method;
- ctx->ui_data = cbarg;
- ctx->passphrase_cb = ossl_decoder_passphrase_in_cb;
- return 1;
- }
-
- return 0;
+ return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg);
}
/*