/* Test of EVP_PKEY_ASN1_METHOD ordering */ static int test_asn1_meths(void) { int i; int prev = -1; int good = 1; int pkey_id; const EVP_PKEY_ASN1_METHOD *ameth; for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { ameth = EVP_PKEY_asn1_get0(i); EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, ameth); if (pkey_id < prev) good = 0; prev = pkey_id; } if (!good) { TEST_error("EVP_PKEY_ASN1_METHOD table out of order"); for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { const char *info; ameth = EVP_PKEY_asn1_get0(i); EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, &info, NULL, ameth); if (info == NULL) info = "<NO NAME>"; TEST_note("%d : %s : %s", pkey_id, OBJ_nid2ln(pkey_id), info); } } return good; }
const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe, const char *str, int len) { int i; const EVP_PKEY_ASN1_METHOD *ameth; if (len == -1) len = strlen(str); if (pe) { #ifndef OPENSSL_NO_ENGINE ENGINE *e; ameth = ENGINE_pkey_asn1_find_str(&e, str, len); if (ameth) { /* * Convert structural into functional reference */ if (!ENGINE_init(e)) ameth = NULL; ENGINE_free(e); *pe = e; return ameth; } #endif *pe = NULL; } for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { ameth = EVP_PKEY_asn1_get0(i); if (ameth->pkey_flags & ASN1_PKEY_ALIAS) continue; if (((int)strlen(ameth->pem_str) == len) && (strncasecmp(ameth->pem_str, str, len) == 0)) return ameth; } return NULL; }
/* * Key parameter decoder. */ static OSSL_STORE_INFO *try_decode_params(const char *pem_name, const char *pem_header, const unsigned char *blob, size_t len, void **pctx, int *matchcount, const UI_METHOD *ui_method, void *ui_data) { OSSL_STORE_INFO *store_info = NULL; int slen = 0; EVP_PKEY *pkey = NULL; const EVP_PKEY_ASN1_METHOD *ameth = NULL; int ok = 0; if (pem_name != NULL) { if ((slen = pem_check_suffix(pem_name, "PARAMETERS")) == 0) return NULL; *matchcount = 1; } if (slen > 0) { if ((pkey = EVP_PKEY_new()) == NULL) { OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB); return NULL; } if (EVP_PKEY_set_type_str(pkey, pem_name, slen) && (ameth = EVP_PKEY_get0_asn1(pkey)) != NULL && ameth->param_decode != NULL && ameth->param_decode(pkey, &blob, len)) ok = 1; } else { int i; EVP_PKEY *tmp_pkey = NULL; for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { const unsigned char *tmp_blob = blob; if (tmp_pkey == NULL && (tmp_pkey = EVP_PKEY_new()) == NULL) { OSSL_STOREerr(OSSL_STORE_F_TRY_DECODE_PARAMS, ERR_R_EVP_LIB); break; } ameth = EVP_PKEY_asn1_get0(i); if (ameth->pkey_flags & ASN1_PKEY_ALIAS) continue; if (EVP_PKEY_set_type(tmp_pkey, ameth->pkey_id) && (ameth = EVP_PKEY_get0_asn1(tmp_pkey)) != NULL && ameth->param_decode != NULL && ameth->param_decode(tmp_pkey, &tmp_blob, len)) { if (pkey != NULL) EVP_PKEY_free(tmp_pkey); else pkey = tmp_pkey; tmp_pkey = NULL; (*matchcount)++; } } EVP_PKEY_free(tmp_pkey); if (*matchcount == 1) { ok = 1; } } if (ok) store_info = OSSL_STORE_INFO_new_PARAMS(pkey); if (store_info == NULL) EVP_PKEY_free(pkey); return store_info; }
static OSSL_STORE_INFO *try_decode_PrivateKey(const char *pem_name, const char *pem_header, const unsigned char *blob, size_t len, void **pctx, int *matchcount, const UI_METHOD *ui_method, void *ui_data) { OSSL_STORE_INFO *store_info = NULL; EVP_PKEY *pkey = NULL; const EVP_PKEY_ASN1_METHOD *ameth = NULL; if (pem_name != NULL) { if (strcmp(pem_name, PEM_STRING_PKCS8INF) == 0) { PKCS8_PRIV_KEY_INFO *p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &blob, len); *matchcount = 1; if (p8inf != NULL) pkey = EVP_PKCS82PKEY(p8inf); PKCS8_PRIV_KEY_INFO_free(p8inf); } else { int slen; if ((slen = pem_check_suffix(pem_name, "PRIVATE KEY")) > 0 && (ameth = EVP_PKEY_asn1_find_str(NULL, pem_name, slen)) != NULL) { *matchcount = 1; pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &blob, len); } } } else { int i; for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) { EVP_PKEY *tmp_pkey = NULL; const unsigned char *tmp_blob = blob; ameth = EVP_PKEY_asn1_get0(i); if (ameth->pkey_flags & ASN1_PKEY_ALIAS) continue; tmp_pkey = d2i_PrivateKey(ameth->pkey_id, NULL, &tmp_blob, len); if (tmp_pkey != NULL) { if (pkey != NULL) EVP_PKEY_free(tmp_pkey); else pkey = tmp_pkey; (*matchcount)++; } } if (*matchcount > 1) { EVP_PKEY_free(pkey); pkey = NULL; } } if (pkey == NULL) /* No match */ return NULL; store_info = OSSL_STORE_INFO_new_PKEY(pkey); if (store_info == NULL) EVP_PKEY_free(pkey); return store_info; }