11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
51cb0ef41Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
61cb0ef41Sopenharmony_ci * in the file LICENSE in the source distribution or at
71cb0ef41Sopenharmony_ci * https://www.openssl.org/source/license.html
81cb0ef41Sopenharmony_ci */
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#include <openssl/err.h>
111cb0ef41Sopenharmony_ci#include <openssl/ui.h>
121cb0ef41Sopenharmony_ci#include <openssl/core_names.h>
131cb0ef41Sopenharmony_ci#include "internal/cryptlib.h"
141cb0ef41Sopenharmony_ci#include "internal/passphrase.h"
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_civoid ossl_pw_clear_passphrase_data(struct ossl_passphrase_data_st *data)
171cb0ef41Sopenharmony_ci{
181cb0ef41Sopenharmony_ci    if (data != NULL) {
191cb0ef41Sopenharmony_ci        if (data->type == is_expl_passphrase)
201cb0ef41Sopenharmony_ci            OPENSSL_clear_free(data->_.expl_passphrase.passphrase_copy,
211cb0ef41Sopenharmony_ci                               data->_.expl_passphrase.passphrase_len);
221cb0ef41Sopenharmony_ci        ossl_pw_clear_passphrase_cache(data);
231cb0ef41Sopenharmony_ci        memset(data, 0, sizeof(*data));
241cb0ef41Sopenharmony_ci    }
251cb0ef41Sopenharmony_ci}
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_civoid ossl_pw_clear_passphrase_cache(struct ossl_passphrase_data_st *data)
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci    OPENSSL_clear_free(data->cached_passphrase, data->cached_passphrase_len);
301cb0ef41Sopenharmony_ci    data->cached_passphrase = NULL;
311cb0ef41Sopenharmony_ci}
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciint ossl_pw_set_passphrase(struct ossl_passphrase_data_st *data,
341cb0ef41Sopenharmony_ci                           const unsigned char *passphrase,
351cb0ef41Sopenharmony_ci                           size_t passphrase_len)
361cb0ef41Sopenharmony_ci{
371cb0ef41Sopenharmony_ci    if (!ossl_assert(data != NULL && passphrase != NULL)) {
381cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
391cb0ef41Sopenharmony_ci        return 0;
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci    ossl_pw_clear_passphrase_data(data);
421cb0ef41Sopenharmony_ci    data->type = is_expl_passphrase;
431cb0ef41Sopenharmony_ci    data->_.expl_passphrase.passphrase_copy =
441cb0ef41Sopenharmony_ci        passphrase_len != 0 ? OPENSSL_memdup(passphrase, passphrase_len)
451cb0ef41Sopenharmony_ci                            : OPENSSL_malloc(1);
461cb0ef41Sopenharmony_ci    if (data->_.expl_passphrase.passphrase_copy == NULL) {
471cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
481cb0ef41Sopenharmony_ci        return 0;
491cb0ef41Sopenharmony_ci    }
501cb0ef41Sopenharmony_ci    data->_.expl_passphrase.passphrase_len = passphrase_len;
511cb0ef41Sopenharmony_ci    return 1;
521cb0ef41Sopenharmony_ci}
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciint ossl_pw_set_pem_password_cb(struct ossl_passphrase_data_st *data,
551cb0ef41Sopenharmony_ci                                pem_password_cb *cb, void *cbarg)
561cb0ef41Sopenharmony_ci{
571cb0ef41Sopenharmony_ci    if (!ossl_assert(data != NULL && cb != NULL)) {
581cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
591cb0ef41Sopenharmony_ci        return 0;
601cb0ef41Sopenharmony_ci    }
611cb0ef41Sopenharmony_ci    ossl_pw_clear_passphrase_data(data);
621cb0ef41Sopenharmony_ci    data->type = is_pem_password;
631cb0ef41Sopenharmony_ci    data->_.pem_password.password_cb = cb;
641cb0ef41Sopenharmony_ci    data->_.pem_password.password_cbarg = cbarg;
651cb0ef41Sopenharmony_ci    return 1;
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciint ossl_pw_set_ossl_passphrase_cb(struct ossl_passphrase_data_st *data,
691cb0ef41Sopenharmony_ci                                   OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
701cb0ef41Sopenharmony_ci{
711cb0ef41Sopenharmony_ci    if (!ossl_assert(data != NULL && cb != NULL)) {
721cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
731cb0ef41Sopenharmony_ci        return 0;
741cb0ef41Sopenharmony_ci    }
751cb0ef41Sopenharmony_ci    ossl_pw_clear_passphrase_data(data);
761cb0ef41Sopenharmony_ci    data->type = is_ossl_passphrase;
771cb0ef41Sopenharmony_ci    data->_.ossl_passphrase.passphrase_cb = cb;
781cb0ef41Sopenharmony_ci    data->_.ossl_passphrase.passphrase_cbarg = cbarg;
791cb0ef41Sopenharmony_ci    return 1;
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ciint ossl_pw_set_ui_method(struct ossl_passphrase_data_st *data,
831cb0ef41Sopenharmony_ci                          const UI_METHOD *ui_method, void *ui_data)
841cb0ef41Sopenharmony_ci{
851cb0ef41Sopenharmony_ci    if (!ossl_assert(data != NULL && ui_method != NULL)) {
861cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
871cb0ef41Sopenharmony_ci        return 0;
881cb0ef41Sopenharmony_ci    }
891cb0ef41Sopenharmony_ci    ossl_pw_clear_passphrase_data(data);
901cb0ef41Sopenharmony_ci    data->type = is_ui_method;
911cb0ef41Sopenharmony_ci    data->_.ui_method.ui_method = ui_method;
921cb0ef41Sopenharmony_ci    data->_.ui_method.ui_method_data = ui_data;
931cb0ef41Sopenharmony_ci    return 1;
941cb0ef41Sopenharmony_ci}
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ciint ossl_pw_enable_passphrase_caching(struct ossl_passphrase_data_st *data)
971cb0ef41Sopenharmony_ci{
981cb0ef41Sopenharmony_ci    data->flag_cache_passphrase = 1;
991cb0ef41Sopenharmony_ci    return 1;
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciint ossl_pw_disable_passphrase_caching(struct ossl_passphrase_data_st *data)
1031cb0ef41Sopenharmony_ci{
1041cb0ef41Sopenharmony_ci    data->flag_cache_passphrase = 0;
1051cb0ef41Sopenharmony_ci    return 1;
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci/*-
1101cb0ef41Sopenharmony_ci * UI_METHOD processor.  It differs from UI_UTIL_read_pw() like this:
1111cb0ef41Sopenharmony_ci *
1121cb0ef41Sopenharmony_ci * 1.  It constructs a prompt on its own, based on |prompt_info|.
1131cb0ef41Sopenharmony_ci * 2.  It allocates a buffer for password and verification on its own
1141cb0ef41Sopenharmony_ci *     to compensate for NUL terminator in UI password strings.
1151cb0ef41Sopenharmony_ci * 3.  It raises errors.
1161cb0ef41Sopenharmony_ci * 4.  It reports back the length of the prompted pass phrase.
1171cb0ef41Sopenharmony_ci */
1181cb0ef41Sopenharmony_cistatic int do_ui_passphrase(char *pass, size_t pass_size, size_t *pass_len,
1191cb0ef41Sopenharmony_ci                            const char *prompt_info, int verify,
1201cb0ef41Sopenharmony_ci                            const UI_METHOD *ui_method, void *ui_data)
1211cb0ef41Sopenharmony_ci{
1221cb0ef41Sopenharmony_ci    char *prompt = NULL, *ipass = NULL, *vpass = NULL;
1231cb0ef41Sopenharmony_ci    int prompt_idx = -1, verify_idx = -1, res;
1241cb0ef41Sopenharmony_ci    UI *ui = NULL;
1251cb0ef41Sopenharmony_ci    int ret = 0;
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci    if (!ossl_assert(pass != NULL && pass_size != 0 && pass_len != NULL)) {
1281cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
1291cb0ef41Sopenharmony_ci        return 0;
1301cb0ef41Sopenharmony_ci    }
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci    if ((ui = UI_new()) == NULL) {
1331cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1341cb0ef41Sopenharmony_ci        return 0;
1351cb0ef41Sopenharmony_ci    }
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci    if (ui_method != NULL) {
1381cb0ef41Sopenharmony_ci        UI_set_method(ui, ui_method);
1391cb0ef41Sopenharmony_ci        if (ui_data != NULL)
1401cb0ef41Sopenharmony_ci            UI_add_user_data(ui, ui_data);
1411cb0ef41Sopenharmony_ci    }
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci    /* Get an application constructed prompt */
1441cb0ef41Sopenharmony_ci    prompt = UI_construct_prompt(ui, "pass phrase", prompt_info);
1451cb0ef41Sopenharmony_ci    if (prompt == NULL) {
1461cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1471cb0ef41Sopenharmony_ci        goto end;
1481cb0ef41Sopenharmony_ci    }
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci    /* Get a buffer for verification prompt */
1511cb0ef41Sopenharmony_ci    ipass = OPENSSL_zalloc(pass_size + 1);
1521cb0ef41Sopenharmony_ci    if (ipass == NULL) {
1531cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1541cb0ef41Sopenharmony_ci        goto end;
1551cb0ef41Sopenharmony_ci    }
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci    prompt_idx = UI_add_input_string(ui, prompt,
1581cb0ef41Sopenharmony_ci                                     UI_INPUT_FLAG_DEFAULT_PWD,
1591cb0ef41Sopenharmony_ci                                     ipass, 0, pass_size) - 1;
1601cb0ef41Sopenharmony_ci    if (prompt_idx < 0) {
1611cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
1621cb0ef41Sopenharmony_ci        goto end;
1631cb0ef41Sopenharmony_ci    }
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci    if (verify) {
1661cb0ef41Sopenharmony_ci        /* Get a buffer for verification prompt */
1671cb0ef41Sopenharmony_ci        vpass = OPENSSL_zalloc(pass_size + 1);
1681cb0ef41Sopenharmony_ci        if (vpass == NULL) {
1691cb0ef41Sopenharmony_ci            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
1701cb0ef41Sopenharmony_ci            goto end;
1711cb0ef41Sopenharmony_ci        }
1721cb0ef41Sopenharmony_ci        verify_idx = UI_add_verify_string(ui, prompt,
1731cb0ef41Sopenharmony_ci                                          UI_INPUT_FLAG_DEFAULT_PWD,
1741cb0ef41Sopenharmony_ci                                          vpass, 0, pass_size,
1751cb0ef41Sopenharmony_ci                                          ipass) - 1;
1761cb0ef41Sopenharmony_ci        if (verify_idx < 0) {
1771cb0ef41Sopenharmony_ci            ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
1781cb0ef41Sopenharmony_ci            goto end;
1791cb0ef41Sopenharmony_ci        }
1801cb0ef41Sopenharmony_ci    }
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci    switch (UI_process(ui)) {
1831cb0ef41Sopenharmony_ci    case -2:
1841cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERRUPTED_OR_CANCELLED);
1851cb0ef41Sopenharmony_ci        break;
1861cb0ef41Sopenharmony_ci    case -1:
1871cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
1881cb0ef41Sopenharmony_ci        break;
1891cb0ef41Sopenharmony_ci    default:
1901cb0ef41Sopenharmony_ci        res = UI_get_result_length(ui, prompt_idx);
1911cb0ef41Sopenharmony_ci        if (res < 0) {
1921cb0ef41Sopenharmony_ci            ERR_raise(ERR_LIB_CRYPTO, ERR_R_UI_LIB);
1931cb0ef41Sopenharmony_ci            break;
1941cb0ef41Sopenharmony_ci        }
1951cb0ef41Sopenharmony_ci        *pass_len = (size_t)res;
1961cb0ef41Sopenharmony_ci        memcpy(pass, ipass, *pass_len);
1971cb0ef41Sopenharmony_ci        ret = 1;
1981cb0ef41Sopenharmony_ci        break;
1991cb0ef41Sopenharmony_ci    }
2001cb0ef41Sopenharmony_ci
2011cb0ef41Sopenharmony_ci end:
2021cb0ef41Sopenharmony_ci    OPENSSL_clear_free(vpass, pass_size + 1);
2031cb0ef41Sopenharmony_ci    OPENSSL_clear_free(ipass, pass_size + 1);
2041cb0ef41Sopenharmony_ci    OPENSSL_free(prompt);
2051cb0ef41Sopenharmony_ci    UI_free(ui);
2061cb0ef41Sopenharmony_ci    return ret;
2071cb0ef41Sopenharmony_ci}
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ci/* Central pw prompting dispatcher */
2101cb0ef41Sopenharmony_ciint ossl_pw_get_passphrase(char *pass, size_t pass_size, size_t *pass_len,
2111cb0ef41Sopenharmony_ci                           const OSSL_PARAM params[], int verify,
2121cb0ef41Sopenharmony_ci                           struct ossl_passphrase_data_st *data)
2131cb0ef41Sopenharmony_ci{
2141cb0ef41Sopenharmony_ci    const char *source = NULL;
2151cb0ef41Sopenharmony_ci    size_t source_len = 0;
2161cb0ef41Sopenharmony_ci    const char *prompt_info = NULL;
2171cb0ef41Sopenharmony_ci    const UI_METHOD *ui_method = NULL;
2181cb0ef41Sopenharmony_ci    UI_METHOD *allocated_ui_method = NULL;
2191cb0ef41Sopenharmony_ci    void *ui_data = NULL;
2201cb0ef41Sopenharmony_ci    const OSSL_PARAM *p = NULL;
2211cb0ef41Sopenharmony_ci    int ret;
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci    /* Handle explicit and cached passphrases */
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci    if (data->type == is_expl_passphrase) {
2261cb0ef41Sopenharmony_ci        source = data->_.expl_passphrase.passphrase_copy;
2271cb0ef41Sopenharmony_ci        source_len = data->_.expl_passphrase.passphrase_len;
2281cb0ef41Sopenharmony_ci    } else if (data->flag_cache_passphrase && data->cached_passphrase != NULL) {
2291cb0ef41Sopenharmony_ci        source = data->cached_passphrase;
2301cb0ef41Sopenharmony_ci        source_len = data->cached_passphrase_len;
2311cb0ef41Sopenharmony_ci    }
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci    if (source != NULL) {
2341cb0ef41Sopenharmony_ci        if (source_len > pass_size)
2351cb0ef41Sopenharmony_ci            source_len = pass_size;
2361cb0ef41Sopenharmony_ci        memcpy(pass, source, source_len);
2371cb0ef41Sopenharmony_ci        *pass_len = source_len;
2381cb0ef41Sopenharmony_ci        return 1;
2391cb0ef41Sopenharmony_ci    }
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ci    /* Handle the is_ossl_passphrase case...  that's pretty direct */
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci    if (data->type == is_ossl_passphrase) {
2441cb0ef41Sopenharmony_ci        OSSL_PASSPHRASE_CALLBACK *cb = data->_.ossl_passphrase.passphrase_cb;
2451cb0ef41Sopenharmony_ci        void *cbarg = data->_.ossl_passphrase.passphrase_cbarg;
2461cb0ef41Sopenharmony_ci
2471cb0ef41Sopenharmony_ci        ret = cb(pass, pass_size, pass_len, params, cbarg);
2481cb0ef41Sopenharmony_ci        goto do_cache;
2491cb0ef41Sopenharmony_ci    }
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci    /* Handle the is_pem_password and is_ui_method cases */
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ci    if ((p = OSSL_PARAM_locate_const(params,
2541cb0ef41Sopenharmony_ci                                     OSSL_PASSPHRASE_PARAM_INFO)) != NULL) {
2551cb0ef41Sopenharmony_ci        if (p->data_type != OSSL_PARAM_UTF8_STRING) {
2561cb0ef41Sopenharmony_ci            ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
2571cb0ef41Sopenharmony_ci                           "Prompt info data type incorrect");
2581cb0ef41Sopenharmony_ci            return 0;
2591cb0ef41Sopenharmony_ci        }
2601cb0ef41Sopenharmony_ci        prompt_info = p->data;
2611cb0ef41Sopenharmony_ci    }
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci    if (data->type == is_pem_password) {
2641cb0ef41Sopenharmony_ci        /* We use a UI wrapper for PEM */
2651cb0ef41Sopenharmony_ci        pem_password_cb *cb = data->_.pem_password.password_cb;
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_ci        ui_method = allocated_ui_method =
2681cb0ef41Sopenharmony_ci            UI_UTIL_wrap_read_pem_callback(cb, verify);
2691cb0ef41Sopenharmony_ci        ui_data = data->_.pem_password.password_cbarg;
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ci        if (ui_method == NULL) {
2721cb0ef41Sopenharmony_ci            ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
2731cb0ef41Sopenharmony_ci            return 0;
2741cb0ef41Sopenharmony_ci        }
2751cb0ef41Sopenharmony_ci    } else if (data->type == is_ui_method) {
2761cb0ef41Sopenharmony_ci        ui_method = data->_.ui_method.ui_method;
2771cb0ef41Sopenharmony_ci        ui_data = data->_.ui_method.ui_method_data;
2781cb0ef41Sopenharmony_ci    }
2791cb0ef41Sopenharmony_ci
2801cb0ef41Sopenharmony_ci    if (ui_method == NULL) {
2811cb0ef41Sopenharmony_ci        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
2821cb0ef41Sopenharmony_ci                       "No password method specified");
2831cb0ef41Sopenharmony_ci        return 0;
2841cb0ef41Sopenharmony_ci    }
2851cb0ef41Sopenharmony_ci
2861cb0ef41Sopenharmony_ci    ret = do_ui_passphrase(pass, pass_size, pass_len, prompt_info, verify,
2871cb0ef41Sopenharmony_ci                           ui_method, ui_data);
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci    UI_destroy_method(allocated_ui_method);
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_ci do_cache:
2921cb0ef41Sopenharmony_ci    if (ret && data->flag_cache_passphrase) {
2931cb0ef41Sopenharmony_ci        if (data->cached_passphrase == NULL
2941cb0ef41Sopenharmony_ci            || *pass_len > data->cached_passphrase_len) {
2951cb0ef41Sopenharmony_ci            void *new_cache =
2961cb0ef41Sopenharmony_ci                OPENSSL_clear_realloc(data->cached_passphrase,
2971cb0ef41Sopenharmony_ci                                      data->cached_passphrase_len,
2981cb0ef41Sopenharmony_ci                                      *pass_len + 1);
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_ci            if (new_cache == NULL) {
3011cb0ef41Sopenharmony_ci                OPENSSL_cleanse(pass, *pass_len);
3021cb0ef41Sopenharmony_ci                ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
3031cb0ef41Sopenharmony_ci                return 0;
3041cb0ef41Sopenharmony_ci            }
3051cb0ef41Sopenharmony_ci            data->cached_passphrase = new_cache;
3061cb0ef41Sopenharmony_ci        }
3071cb0ef41Sopenharmony_ci        memcpy(data->cached_passphrase, pass, *pass_len);
3081cb0ef41Sopenharmony_ci        data->cached_passphrase[*pass_len] = '\0';
3091cb0ef41Sopenharmony_ci        data->cached_passphrase_len = *pass_len;
3101cb0ef41Sopenharmony_ci    }
3111cb0ef41Sopenharmony_ci
3121cb0ef41Sopenharmony_ci    return ret;
3131cb0ef41Sopenharmony_ci}
3141cb0ef41Sopenharmony_ci
3151cb0ef41Sopenharmony_cistatic int ossl_pw_get_password(char *buf, int size, int rwflag,
3161cb0ef41Sopenharmony_ci                                void *userdata, const char *info)
3171cb0ef41Sopenharmony_ci{
3181cb0ef41Sopenharmony_ci    size_t password_len = 0;
3191cb0ef41Sopenharmony_ci    OSSL_PARAM params[] = {
3201cb0ef41Sopenharmony_ci        OSSL_PARAM_utf8_string(OSSL_PASSPHRASE_PARAM_INFO, NULL, 0),
3211cb0ef41Sopenharmony_ci        OSSL_PARAM_END
3221cb0ef41Sopenharmony_ci    };
3231cb0ef41Sopenharmony_ci
3241cb0ef41Sopenharmony_ci    params[0].data = (void *)info;
3251cb0ef41Sopenharmony_ci    if (ossl_pw_get_passphrase(buf, (size_t)size, &password_len, params,
3261cb0ef41Sopenharmony_ci                               rwflag, userdata))
3271cb0ef41Sopenharmony_ci        return (int)password_len;
3281cb0ef41Sopenharmony_ci    return -1;
3291cb0ef41Sopenharmony_ci}
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ciint ossl_pw_pem_password(char *buf, int size, int rwflag, void *userdata)
3321cb0ef41Sopenharmony_ci{
3331cb0ef41Sopenharmony_ci    return ossl_pw_get_password(buf, size, rwflag, userdata, "PEM");
3341cb0ef41Sopenharmony_ci}
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ciint ossl_pw_pvk_password(char *buf, int size, int rwflag, void *userdata)
3371cb0ef41Sopenharmony_ci{
3381cb0ef41Sopenharmony_ci    return ossl_pw_get_password(buf, size, rwflag, userdata, "PVK");
3391cb0ef41Sopenharmony_ci}
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ciint ossl_pw_passphrase_callback_enc(char *pass, size_t pass_size,
3421cb0ef41Sopenharmony_ci                                    size_t *pass_len,
3431cb0ef41Sopenharmony_ci                                    const OSSL_PARAM params[], void *arg)
3441cb0ef41Sopenharmony_ci{
3451cb0ef41Sopenharmony_ci    return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 1, arg);
3461cb0ef41Sopenharmony_ci}
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ciint ossl_pw_passphrase_callback_dec(char *pass, size_t pass_size,
3491cb0ef41Sopenharmony_ci                                    size_t *pass_len,
3501cb0ef41Sopenharmony_ci                                    const OSSL_PARAM params[], void *arg)
3511cb0ef41Sopenharmony_ci{
3521cb0ef41Sopenharmony_ci    return ossl_pw_get_passphrase(pass, pass_size, pass_len, params, 0, arg);
3531cb0ef41Sopenharmony_ci}
354