diff options
Diffstat (limited to 'crypto/asn1/x_algor.c')
-rw-r--r-- | crypto/asn1/x_algor.c | 63 |
1 files changed, 34 insertions, 29 deletions
diff --git a/crypto/asn1/x_algor.c b/crypto/asn1/x_algor.c index 2c4a8d4b4ee8..db9dd06e4940 100644 --- a/crypto/asn1/x_algor.c +++ b/crypto/asn1/x_algor.c @@ -1,5 +1,5 @@ /* - * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 1998-2022 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 @@ -33,17 +33,14 @@ int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval) if (alg == NULL) return 0; - if (ptype != V_ASN1_UNDEF) { - if (alg->parameter == NULL) - alg->parameter = ASN1_TYPE_new(); - if (alg->parameter == NULL) - return 0; - } + if (ptype != V_ASN1_UNDEF && alg->parameter == NULL + && (alg->parameter = ASN1_TYPE_new()) == NULL) + return 0; ASN1_OBJECT_free(alg->algorithm); alg->algorithm = aobj; - if (ptype == 0) + if (ptype == V_ASN1_EOC) return 1; if (ptype == V_ASN1_UNDEF) { ASN1_TYPE_free(alg->parameter); @@ -53,6 +50,25 @@ int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval) return 1; } +X509_ALGOR *ossl_X509_ALGOR_from_nid(int nid, int ptype, void *pval) +{ + ASN1_OBJECT *algo = OBJ_nid2obj(nid); + X509_ALGOR *alg = NULL; + + if (algo == NULL) + return NULL; + if ((alg = X509_ALGOR_new()) == NULL) + goto err; + if (X509_ALGOR_set0(alg, algo, ptype, pval)) + return alg; + alg->algorithm = NULL; /* precaution to prevent double free */ + + err: + X509_ALGOR_free(alg); + /* ASN1_OBJECT_free(algo) is not needed due to OBJ_nid2obj() */ + return NULL; +} + void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype, const void **ppval, const X509_ALGOR *algor) { @@ -70,18 +86,12 @@ void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype, } /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */ - void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md) { - int param_type; - - if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT) - param_type = V_ASN1_UNDEF; - else - param_type = V_ASN1_NULL; - - X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_get_type(md)), param_type, NULL); + int type = md->flags & EVP_MD_FLAG_DIGALGID_ABSENT ? V_ASN1_UNDEF + : V_ASN1_NULL; + (void)X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_get_type(md)), type, NULL); } int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b) @@ -131,13 +141,15 @@ int X509_ALGOR_copy(X509_ALGOR *dest, const X509_ALGOR *src) /* allocate and set algorithm ID from EVP_MD, default SHA1 */ int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md) { + X509_ALGOR *alg; + /* Default is SHA1 so no need to create it - still success */ if (md == NULL || EVP_MD_is_a(md, "SHA1")) return 1; - *palg = X509_ALGOR_new(); - if (*palg == NULL) + if ((alg = X509_ALGOR_new()) == NULL) return 0; - X509_ALGOR_set_md(*palg, md); + X509_ALGOR_set_md(alg, md); + *palg = alg; return 1; } @@ -176,19 +188,12 @@ int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) goto err; if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL) goto err; - *palg = X509_ALGOR_new(); + *palg = ossl_X509_ALGOR_from_nid(NID_mgf1, V_ASN1_SEQUENCE, stmp); if (*palg == NULL) goto err; - if (!X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp)) { - X509_ALGOR_free(*palg); - *palg = NULL; - goto err; - } stmp = NULL; err: ASN1_STRING_free(stmp); X509_ALGOR_free(algtmp); - if (*palg != NULL) - return 1; - return 0; + return *palg != NULL; } |