11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * Copyright 2006-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/* We need to use some engine deprecated APIs */
111cb0ef41Sopenharmony_ci#define OPENSSL_SUPPRESS_DEPRECATED
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#include "internal/cryptlib.h"
141cb0ef41Sopenharmony_ci#include <stdio.h>
151cb0ef41Sopenharmony_ci#include <openssl/asn1t.h>
161cb0ef41Sopenharmony_ci#include <openssl/x509.h>
171cb0ef41Sopenharmony_ci#include <openssl/engine.h>
181cb0ef41Sopenharmony_ci#include "crypto/asn1.h"
191cb0ef41Sopenharmony_ci#include "crypto/evp.h"
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci#include "standard_methods.h"
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_citypedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
241cb0ef41Sopenharmony_cistatic STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciDECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
271cb0ef41Sopenharmony_ci                           const EVP_PKEY_ASN1_METHOD *, ameth);
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cistatic int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
301cb0ef41Sopenharmony_ci                     const EVP_PKEY_ASN1_METHOD *const *b)
311cb0ef41Sopenharmony_ci{
321cb0ef41Sopenharmony_ci    return ((*a)->pkey_id - (*b)->pkey_id);
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciIMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
361cb0ef41Sopenharmony_ci                             const EVP_PKEY_ASN1_METHOD *, ameth);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ciint EVP_PKEY_asn1_get_count(void)
391cb0ef41Sopenharmony_ci{
401cb0ef41Sopenharmony_ci    int num = OSSL_NELEM(standard_methods);
411cb0ef41Sopenharmony_ci    if (app_methods)
421cb0ef41Sopenharmony_ci        num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
431cb0ef41Sopenharmony_ci    return num;
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciconst EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
471cb0ef41Sopenharmony_ci{
481cb0ef41Sopenharmony_ci    int num = OSSL_NELEM(standard_methods);
491cb0ef41Sopenharmony_ci    if (idx < 0)
501cb0ef41Sopenharmony_ci        return NULL;
511cb0ef41Sopenharmony_ci    if (idx < num)
521cb0ef41Sopenharmony_ci        return standard_methods[idx];
531cb0ef41Sopenharmony_ci    idx -= num;
541cb0ef41Sopenharmony_ci    return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_cistatic const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
581cb0ef41Sopenharmony_ci{
591cb0ef41Sopenharmony_ci    EVP_PKEY_ASN1_METHOD tmp;
601cb0ef41Sopenharmony_ci    const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci    tmp.pkey_id = type;
631cb0ef41Sopenharmony_ci    if (app_methods) {
641cb0ef41Sopenharmony_ci        int idx;
651cb0ef41Sopenharmony_ci        idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
661cb0ef41Sopenharmony_ci        if (idx >= 0)
671cb0ef41Sopenharmony_ci            return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
681cb0ef41Sopenharmony_ci    }
691cb0ef41Sopenharmony_ci    ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
701cb0ef41Sopenharmony_ci    if (ret == NULL || *ret == NULL)
711cb0ef41Sopenharmony_ci        return NULL;
721cb0ef41Sopenharmony_ci    return *ret;
731cb0ef41Sopenharmony_ci}
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci/*
761cb0ef41Sopenharmony_ci * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
771cb0ef41Sopenharmony_ci * search through engines and set *pe to a functional reference to the engine
781cb0ef41Sopenharmony_ci * implementing 'type' or NULL if no engine implements it.
791cb0ef41Sopenharmony_ci */
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciconst EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
821cb0ef41Sopenharmony_ci{
831cb0ef41Sopenharmony_ci    const EVP_PKEY_ASN1_METHOD *t;
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    for (;;) {
861cb0ef41Sopenharmony_ci        t = pkey_asn1_find(type);
871cb0ef41Sopenharmony_ci        if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
881cb0ef41Sopenharmony_ci            break;
891cb0ef41Sopenharmony_ci        type = t->pkey_base_id;
901cb0ef41Sopenharmony_ci    }
911cb0ef41Sopenharmony_ci    if (pe) {
921cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
931cb0ef41Sopenharmony_ci        ENGINE *e;
941cb0ef41Sopenharmony_ci        /* type will contain the final unaliased type */
951cb0ef41Sopenharmony_ci        e = ENGINE_get_pkey_asn1_meth_engine(type);
961cb0ef41Sopenharmony_ci        if (e) {
971cb0ef41Sopenharmony_ci            *pe = e;
981cb0ef41Sopenharmony_ci            return ENGINE_get_pkey_asn1_meth(e, type);
991cb0ef41Sopenharmony_ci        }
1001cb0ef41Sopenharmony_ci#endif
1011cb0ef41Sopenharmony_ci        *pe = NULL;
1021cb0ef41Sopenharmony_ci    }
1031cb0ef41Sopenharmony_ci    return t;
1041cb0ef41Sopenharmony_ci}
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciconst EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
1071cb0ef41Sopenharmony_ci                                                   const char *str, int len)
1081cb0ef41Sopenharmony_ci{
1091cb0ef41Sopenharmony_ci    int i;
1101cb0ef41Sopenharmony_ci    const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci    if (len == -1)
1131cb0ef41Sopenharmony_ci        len = strlen(str);
1141cb0ef41Sopenharmony_ci    if (pe) {
1151cb0ef41Sopenharmony_ci#ifndef OPENSSL_NO_ENGINE
1161cb0ef41Sopenharmony_ci        ENGINE *e;
1171cb0ef41Sopenharmony_ci        ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
1181cb0ef41Sopenharmony_ci        if (ameth) {
1191cb0ef41Sopenharmony_ci            /*
1201cb0ef41Sopenharmony_ci             * Convert structural into functional reference
1211cb0ef41Sopenharmony_ci             */
1221cb0ef41Sopenharmony_ci            if (!ENGINE_init(e))
1231cb0ef41Sopenharmony_ci                ameth = NULL;
1241cb0ef41Sopenharmony_ci            ENGINE_free(e);
1251cb0ef41Sopenharmony_ci            *pe = e;
1261cb0ef41Sopenharmony_ci            return ameth;
1271cb0ef41Sopenharmony_ci        }
1281cb0ef41Sopenharmony_ci#endif
1291cb0ef41Sopenharmony_ci        *pe = NULL;
1301cb0ef41Sopenharmony_ci    }
1311cb0ef41Sopenharmony_ci    for (i = EVP_PKEY_asn1_get_count(); i-- > 0; ) {
1321cb0ef41Sopenharmony_ci        ameth = EVP_PKEY_asn1_get0(i);
1331cb0ef41Sopenharmony_ci        if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
1341cb0ef41Sopenharmony_ci            continue;
1351cb0ef41Sopenharmony_ci        if ((int)strlen(ameth->pem_str) == len
1361cb0ef41Sopenharmony_ci            && OPENSSL_strncasecmp(ameth->pem_str, str, len) == 0)
1371cb0ef41Sopenharmony_ci            return ameth;
1381cb0ef41Sopenharmony_ci    }
1391cb0ef41Sopenharmony_ci    return NULL;
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ciint EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
1431cb0ef41Sopenharmony_ci{
1441cb0ef41Sopenharmony_ci    EVP_PKEY_ASN1_METHOD tmp = { 0, };
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci    /*
1471cb0ef41Sopenharmony_ci     * One of the following must be true:
1481cb0ef41Sopenharmony_ci     *
1491cb0ef41Sopenharmony_ci     * pem_str == NULL AND ASN1_PKEY_ALIAS is set
1501cb0ef41Sopenharmony_ci     * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
1511cb0ef41Sopenharmony_ci     *
1521cb0ef41Sopenharmony_ci     * Anything else is an error and may lead to a corrupt ASN1 method table
1531cb0ef41Sopenharmony_ci     */
1541cb0ef41Sopenharmony_ci    if (!((ameth->pem_str == NULL
1551cb0ef41Sopenharmony_ci           && (ameth->pkey_flags & ASN1_PKEY_ALIAS) != 0)
1561cb0ef41Sopenharmony_ci          || (ameth->pem_str != NULL
1571cb0ef41Sopenharmony_ci              && (ameth->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
1581cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
1591cb0ef41Sopenharmony_ci        return 0;
1601cb0ef41Sopenharmony_ci    }
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci    if (app_methods == NULL) {
1631cb0ef41Sopenharmony_ci        app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
1641cb0ef41Sopenharmony_ci        if (app_methods == NULL)
1651cb0ef41Sopenharmony_ci            return 0;
1661cb0ef41Sopenharmony_ci    }
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci    tmp.pkey_id = ameth->pkey_id;
1691cb0ef41Sopenharmony_ci    if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp) >= 0) {
1701cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_EVP,
1711cb0ef41Sopenharmony_ci                  EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED);
1721cb0ef41Sopenharmony_ci        return 0;
1731cb0ef41Sopenharmony_ci    }
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ci    if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
1761cb0ef41Sopenharmony_ci        return 0;
1771cb0ef41Sopenharmony_ci    sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
1781cb0ef41Sopenharmony_ci    return 1;
1791cb0ef41Sopenharmony_ci}
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ciint EVP_PKEY_asn1_add_alias(int to, int from)
1821cb0ef41Sopenharmony_ci{
1831cb0ef41Sopenharmony_ci    EVP_PKEY_ASN1_METHOD *ameth;
1841cb0ef41Sopenharmony_ci    ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
1851cb0ef41Sopenharmony_ci    if (ameth == NULL)
1861cb0ef41Sopenharmony_ci        return 0;
1871cb0ef41Sopenharmony_ci    ameth->pkey_base_id = to;
1881cb0ef41Sopenharmony_ci    if (!EVP_PKEY_asn1_add0(ameth)) {
1891cb0ef41Sopenharmony_ci        EVP_PKEY_asn1_free(ameth);
1901cb0ef41Sopenharmony_ci        return 0;
1911cb0ef41Sopenharmony_ci    }
1921cb0ef41Sopenharmony_ci    return 1;
1931cb0ef41Sopenharmony_ci}
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ciint EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
1961cb0ef41Sopenharmony_ci                            int *ppkey_flags, const char **pinfo,
1971cb0ef41Sopenharmony_ci                            const char **ppem_str,
1981cb0ef41Sopenharmony_ci                            const EVP_PKEY_ASN1_METHOD *ameth)
1991cb0ef41Sopenharmony_ci{
2001cb0ef41Sopenharmony_ci    if (!ameth)
2011cb0ef41Sopenharmony_ci        return 0;
2021cb0ef41Sopenharmony_ci    if (ppkey_id)
2031cb0ef41Sopenharmony_ci        *ppkey_id = ameth->pkey_id;
2041cb0ef41Sopenharmony_ci    if (ppkey_base_id)
2051cb0ef41Sopenharmony_ci        *ppkey_base_id = ameth->pkey_base_id;
2061cb0ef41Sopenharmony_ci    if (ppkey_flags)
2071cb0ef41Sopenharmony_ci        *ppkey_flags = ameth->pkey_flags;
2081cb0ef41Sopenharmony_ci    if (pinfo)
2091cb0ef41Sopenharmony_ci        *pinfo = ameth->info;
2101cb0ef41Sopenharmony_ci    if (ppem_str)
2111cb0ef41Sopenharmony_ci        *ppem_str = ameth->pem_str;
2121cb0ef41Sopenharmony_ci    return 1;
2131cb0ef41Sopenharmony_ci}
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ciconst EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
2161cb0ef41Sopenharmony_ci{
2171cb0ef41Sopenharmony_ci    return pkey->ameth;
2181cb0ef41Sopenharmony_ci}
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ciEVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
2211cb0ef41Sopenharmony_ci                                        const char *pem_str, const char *info)
2221cb0ef41Sopenharmony_ci{
2231cb0ef41Sopenharmony_ci    EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
2241cb0ef41Sopenharmony_ci
2251cb0ef41Sopenharmony_ci    if (ameth == NULL) {
2261cb0ef41Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
2271cb0ef41Sopenharmony_ci        return NULL;
2281cb0ef41Sopenharmony_ci    }
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci    ameth->pkey_id = id;
2311cb0ef41Sopenharmony_ci    ameth->pkey_base_id = id;
2321cb0ef41Sopenharmony_ci    ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci    if (info) {
2351cb0ef41Sopenharmony_ci        ameth->info = OPENSSL_strdup(info);
2361cb0ef41Sopenharmony_ci        if (ameth->info == NULL)
2371cb0ef41Sopenharmony_ci            goto err;
2381cb0ef41Sopenharmony_ci    }
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci    if (pem_str) {
2411cb0ef41Sopenharmony_ci        ameth->pem_str = OPENSSL_strdup(pem_str);
2421cb0ef41Sopenharmony_ci        if (ameth->pem_str == NULL)
2431cb0ef41Sopenharmony_ci            goto err;
2441cb0ef41Sopenharmony_ci    }
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci    return ameth;
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci err:
2491cb0ef41Sopenharmony_ci    EVP_PKEY_asn1_free(ameth);
2501cb0ef41Sopenharmony_ci    ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
2511cb0ef41Sopenharmony_ci    return NULL;
2521cb0ef41Sopenharmony_ci}
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
2551cb0ef41Sopenharmony_ci                        const EVP_PKEY_ASN1_METHOD *src)
2561cb0ef41Sopenharmony_ci{
2571cb0ef41Sopenharmony_ci    int pkey_id = dst->pkey_id;
2581cb0ef41Sopenharmony_ci    int pkey_base_id = dst->pkey_base_id;
2591cb0ef41Sopenharmony_ci    unsigned long pkey_flags = dst->pkey_flags;
2601cb0ef41Sopenharmony_ci    char *pem_str = dst->pem_str;
2611cb0ef41Sopenharmony_ci    char *info = dst->info;
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci    *dst = *src;
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci    /* We only copy the function pointers so restore the other values */
2661cb0ef41Sopenharmony_ci    dst->pkey_id = pkey_id;
2671cb0ef41Sopenharmony_ci    dst->pkey_base_id = pkey_base_id;
2681cb0ef41Sopenharmony_ci    dst->pkey_flags = pkey_flags;
2691cb0ef41Sopenharmony_ci    dst->pem_str = pem_str;
2701cb0ef41Sopenharmony_ci    dst->info = info;
2711cb0ef41Sopenharmony_ci}
2721cb0ef41Sopenharmony_ci
2731cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
2741cb0ef41Sopenharmony_ci{
2751cb0ef41Sopenharmony_ci    if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
2761cb0ef41Sopenharmony_ci        OPENSSL_free(ameth->pem_str);
2771cb0ef41Sopenharmony_ci        OPENSSL_free(ameth->info);
2781cb0ef41Sopenharmony_ci        OPENSSL_free(ameth);
2791cb0ef41Sopenharmony_ci    }
2801cb0ef41Sopenharmony_ci}
2811cb0ef41Sopenharmony_ci
2821cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
2831cb0ef41Sopenharmony_ci                              int (*pub_decode) (EVP_PKEY *pk,
2841cb0ef41Sopenharmony_ci                                                 const X509_PUBKEY *pub),
2851cb0ef41Sopenharmony_ci                              int (*pub_encode) (X509_PUBKEY *pub,
2861cb0ef41Sopenharmony_ci                                                 const EVP_PKEY *pk),
2871cb0ef41Sopenharmony_ci                              int (*pub_cmp) (const EVP_PKEY *a,
2881cb0ef41Sopenharmony_ci                                              const EVP_PKEY *b),
2891cb0ef41Sopenharmony_ci                              int (*pub_print) (BIO *out,
2901cb0ef41Sopenharmony_ci                                                const EVP_PKEY *pkey,
2911cb0ef41Sopenharmony_ci                                                int indent, ASN1_PCTX *pctx),
2921cb0ef41Sopenharmony_ci                              int (*pkey_size) (const EVP_PKEY *pk),
2931cb0ef41Sopenharmony_ci                              int (*pkey_bits) (const EVP_PKEY *pk))
2941cb0ef41Sopenharmony_ci{
2951cb0ef41Sopenharmony_ci    ameth->pub_decode = pub_decode;
2961cb0ef41Sopenharmony_ci    ameth->pub_encode = pub_encode;
2971cb0ef41Sopenharmony_ci    ameth->pub_cmp = pub_cmp;
2981cb0ef41Sopenharmony_ci    ameth->pub_print = pub_print;
2991cb0ef41Sopenharmony_ci    ameth->pkey_size = pkey_size;
3001cb0ef41Sopenharmony_ci    ameth->pkey_bits = pkey_bits;
3011cb0ef41Sopenharmony_ci}
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
3041cb0ef41Sopenharmony_ci                               int (*priv_decode) (EVP_PKEY *pk,
3051cb0ef41Sopenharmony_ci                                                   const PKCS8_PRIV_KEY_INFO
3061cb0ef41Sopenharmony_ci                                                   *p8inf),
3071cb0ef41Sopenharmony_ci                               int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
3081cb0ef41Sopenharmony_ci                                                   const EVP_PKEY *pk),
3091cb0ef41Sopenharmony_ci                               int (*priv_print) (BIO *out,
3101cb0ef41Sopenharmony_ci                                                  const EVP_PKEY *pkey,
3111cb0ef41Sopenharmony_ci                                                  int indent,
3121cb0ef41Sopenharmony_ci                                                  ASN1_PCTX *pctx))
3131cb0ef41Sopenharmony_ci{
3141cb0ef41Sopenharmony_ci    ameth->priv_decode = priv_decode;
3151cb0ef41Sopenharmony_ci    ameth->priv_encode = priv_encode;
3161cb0ef41Sopenharmony_ci    ameth->priv_print = priv_print;
3171cb0ef41Sopenharmony_ci}
3181cb0ef41Sopenharmony_ci
3191cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
3201cb0ef41Sopenharmony_ci                             int (*param_decode) (EVP_PKEY *pkey,
3211cb0ef41Sopenharmony_ci                                                  const unsigned char **pder,
3221cb0ef41Sopenharmony_ci                                                  int derlen),
3231cb0ef41Sopenharmony_ci                             int (*param_encode) (const EVP_PKEY *pkey,
3241cb0ef41Sopenharmony_ci                                                  unsigned char **pder),
3251cb0ef41Sopenharmony_ci                             int (*param_missing) (const EVP_PKEY *pk),
3261cb0ef41Sopenharmony_ci                             int (*param_copy) (EVP_PKEY *to,
3271cb0ef41Sopenharmony_ci                                                const EVP_PKEY *from),
3281cb0ef41Sopenharmony_ci                             int (*param_cmp) (const EVP_PKEY *a,
3291cb0ef41Sopenharmony_ci                                               const EVP_PKEY *b),
3301cb0ef41Sopenharmony_ci                             int (*param_print) (BIO *out,
3311cb0ef41Sopenharmony_ci                                                 const EVP_PKEY *pkey,
3321cb0ef41Sopenharmony_ci                                                 int indent, ASN1_PCTX *pctx))
3331cb0ef41Sopenharmony_ci{
3341cb0ef41Sopenharmony_ci    ameth->param_decode = param_decode;
3351cb0ef41Sopenharmony_ci    ameth->param_encode = param_encode;
3361cb0ef41Sopenharmony_ci    ameth->param_missing = param_missing;
3371cb0ef41Sopenharmony_ci    ameth->param_copy = param_copy;
3381cb0ef41Sopenharmony_ci    ameth->param_cmp = param_cmp;
3391cb0ef41Sopenharmony_ci    ameth->param_print = param_print;
3401cb0ef41Sopenharmony_ci}
3411cb0ef41Sopenharmony_ci
3421cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
3431cb0ef41Sopenharmony_ci                            void (*pkey_free) (EVP_PKEY *pkey))
3441cb0ef41Sopenharmony_ci{
3451cb0ef41Sopenharmony_ci    ameth->pkey_free = pkey_free;
3461cb0ef41Sopenharmony_ci}
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
3491cb0ef41Sopenharmony_ci                            int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
3501cb0ef41Sopenharmony_ci                                              long arg1, void *arg2))
3511cb0ef41Sopenharmony_ci{
3521cb0ef41Sopenharmony_ci    ameth->pkey_ctrl = pkey_ctrl;
3531cb0ef41Sopenharmony_ci}
3541cb0ef41Sopenharmony_ci
3551cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
3561cb0ef41Sopenharmony_ci                                     int (*pkey_security_bits) (const EVP_PKEY
3571cb0ef41Sopenharmony_ci                                                                *pk))
3581cb0ef41Sopenharmony_ci{
3591cb0ef41Sopenharmony_ci    ameth->pkey_security_bits = pkey_security_bits;
3601cb0ef41Sopenharmony_ci}
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
3631cb0ef41Sopenharmony_ci                            int (*item_verify) (EVP_MD_CTX *ctx,
3641cb0ef41Sopenharmony_ci                                                const ASN1_ITEM *it,
3651cb0ef41Sopenharmony_ci                                                const void *data,
3661cb0ef41Sopenharmony_ci                                                const X509_ALGOR *a,
3671cb0ef41Sopenharmony_ci                                                const ASN1_BIT_STRING *sig,
3681cb0ef41Sopenharmony_ci                                                EVP_PKEY *pkey),
3691cb0ef41Sopenharmony_ci                            int (*item_sign) (EVP_MD_CTX *ctx,
3701cb0ef41Sopenharmony_ci                                              const ASN1_ITEM *it,
3711cb0ef41Sopenharmony_ci                                              const void *data,
3721cb0ef41Sopenharmony_ci                                              X509_ALGOR *alg1,
3731cb0ef41Sopenharmony_ci                                              X509_ALGOR *alg2,
3741cb0ef41Sopenharmony_ci                                              ASN1_BIT_STRING *sig))
3751cb0ef41Sopenharmony_ci{
3761cb0ef41Sopenharmony_ci    ameth->item_sign = item_sign;
3771cb0ef41Sopenharmony_ci    ameth->item_verify = item_verify;
3781cb0ef41Sopenharmony_ci}
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
3811cb0ef41Sopenharmony_ci                              int (*siginf_set) (X509_SIG_INFO *siginf,
3821cb0ef41Sopenharmony_ci                                                 const X509_ALGOR *alg,
3831cb0ef41Sopenharmony_ci                                                 const ASN1_STRING *sig))
3841cb0ef41Sopenharmony_ci{
3851cb0ef41Sopenharmony_ci    ameth->siginf_set = siginf_set;
3861cb0ef41Sopenharmony_ci}
3871cb0ef41Sopenharmony_ci
3881cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
3891cb0ef41Sopenharmony_ci                             int (*pkey_check) (const EVP_PKEY *pk))
3901cb0ef41Sopenharmony_ci{
3911cb0ef41Sopenharmony_ci    ameth->pkey_check = pkey_check;
3921cb0ef41Sopenharmony_ci}
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
3951cb0ef41Sopenharmony_ci                                    int (*pkey_pub_check) (const EVP_PKEY *pk))
3961cb0ef41Sopenharmony_ci{
3971cb0ef41Sopenharmony_ci    ameth->pkey_public_check = pkey_pub_check;
3981cb0ef41Sopenharmony_ci}
3991cb0ef41Sopenharmony_ci
4001cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
4011cb0ef41Sopenharmony_ci                                   int (*pkey_param_check) (const EVP_PKEY *pk))
4021cb0ef41Sopenharmony_ci{
4031cb0ef41Sopenharmony_ci    ameth->pkey_param_check = pkey_param_check;
4041cb0ef41Sopenharmony_ci}
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
4071cb0ef41Sopenharmony_ci                                    int (*set_priv_key) (EVP_PKEY *pk,
4081cb0ef41Sopenharmony_ci                                                         const unsigned char
4091cb0ef41Sopenharmony_ci                                                            *priv,
4101cb0ef41Sopenharmony_ci                                                         size_t len))
4111cb0ef41Sopenharmony_ci{
4121cb0ef41Sopenharmony_ci    ameth->set_priv_key = set_priv_key;
4131cb0ef41Sopenharmony_ci}
4141cb0ef41Sopenharmony_ci
4151cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
4161cb0ef41Sopenharmony_ci                                   int (*set_pub_key) (EVP_PKEY *pk,
4171cb0ef41Sopenharmony_ci                                                       const unsigned char *pub,
4181cb0ef41Sopenharmony_ci                                                       size_t len))
4191cb0ef41Sopenharmony_ci{
4201cb0ef41Sopenharmony_ci    ameth->set_pub_key = set_pub_key;
4211cb0ef41Sopenharmony_ci}
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
4241cb0ef41Sopenharmony_ci                                    int (*get_priv_key) (const EVP_PKEY *pk,
4251cb0ef41Sopenharmony_ci                                                         unsigned char *priv,
4261cb0ef41Sopenharmony_ci                                                         size_t *len))
4271cb0ef41Sopenharmony_ci{
4281cb0ef41Sopenharmony_ci    ameth->get_priv_key = get_priv_key;
4291cb0ef41Sopenharmony_ci}
4301cb0ef41Sopenharmony_ci
4311cb0ef41Sopenharmony_civoid EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
4321cb0ef41Sopenharmony_ci                                   int (*get_pub_key) (const EVP_PKEY *pk,
4331cb0ef41Sopenharmony_ci                                                       unsigned char *pub,
4341cb0ef41Sopenharmony_ci                                                       size_t *len))
4351cb0ef41Sopenharmony_ci{
4361cb0ef41Sopenharmony_ci    ameth->get_pub_key = get_pub_key;
4371cb0ef41Sopenharmony_ci}
438