2 * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/x509.h>
12 #include <openssl/asn1.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/err.h>
15 #include "crypto/asn1.h"
16 #include "crypto/evp.h"
18 ASN1_SEQUENCE(X509_ALGOR
) = {
19 ASN1_SIMPLE(X509_ALGOR
, algorithm
, ASN1_OBJECT
),
20 ASN1_OPT(X509_ALGOR
, parameter
, ASN1_ANY
)
21 } ASN1_SEQUENCE_END(X509_ALGOR
)
23 ASN1_ITEM_TEMPLATE(X509_ALGORS
) =
24 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF
, 0, algorithms
, X509_ALGOR
)
25 ASN1_ITEM_TEMPLATE_END(X509_ALGORS
)
27 IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR
)
28 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS
, X509_ALGORS
, X509_ALGORS
)
29 IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR
)
31 int X509_ALGOR_set0(X509_ALGOR
*alg
, ASN1_OBJECT
*aobj
, int ptype
, void *pval
)
36 if (ptype
!= V_ASN1_UNDEF
) {
37 if (alg
->parameter
== NULL
)
38 alg
->parameter
= ASN1_TYPE_new();
39 if (alg
->parameter
== NULL
)
43 ASN1_OBJECT_free(alg
->algorithm
);
44 alg
->algorithm
= aobj
;
48 if (ptype
== V_ASN1_UNDEF
) {
49 ASN1_TYPE_free(alg
->parameter
);
50 alg
->parameter
= NULL
;
52 ASN1_TYPE_set(alg
->parameter
, ptype
, pval
);
56 void X509_ALGOR_get0(const ASN1_OBJECT
**paobj
, int *pptype
,
57 const void **ppval
, const X509_ALGOR
*algor
)
60 *paobj
= algor
->algorithm
;
62 if (algor
->parameter
== NULL
) {
63 *pptype
= V_ASN1_UNDEF
;
66 *pptype
= algor
->parameter
->type
;
68 *ppval
= algor
->parameter
->value
.ptr
;
72 /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */
74 void X509_ALGOR_set_md(X509_ALGOR
*alg
, const EVP_MD
*md
)
78 if (md
->flags
& EVP_MD_FLAG_DIGALGID_ABSENT
)
79 param_type
= V_ASN1_UNDEF
;
81 param_type
= V_ASN1_NULL
;
83 X509_ALGOR_set0(alg
, OBJ_nid2obj(EVP_MD_type(md
)), param_type
, NULL
);
87 int X509_ALGOR_cmp(const X509_ALGOR
*a
, const X509_ALGOR
*b
)
90 rv
= OBJ_cmp(a
->algorithm
, b
->algorithm
);
93 if (!a
->parameter
&& !b
->parameter
)
95 return ASN1_TYPE_cmp(a
->parameter
, b
->parameter
);
98 int X509_ALGOR_copy(X509_ALGOR
*dest
, const X509_ALGOR
*src
)
100 if (src
== NULL
|| dest
== NULL
)
104 ASN1_OBJECT_free(dest
->algorithm
);
105 dest
->algorithm
= NULL
;
108 ASN1_TYPE_free(dest
->parameter
);
109 dest
->parameter
= NULL
;
112 if ((dest
->algorithm
= OBJ_dup(src
->algorithm
)) == NULL
)
115 if (src
->parameter
!= NULL
) {
116 dest
->parameter
= ASN1_TYPE_new();
117 if (dest
->parameter
== NULL
)
120 /* Assuming this is also correct for a BOOL.
121 * set does copy as a side effect.
123 if (ASN1_TYPE_set1(dest
->parameter
,
124 src
->parameter
->type
, src
->parameter
->value
.ptr
) == 0)
131 /* allocate and set algorithm ID from EVP_MD, default SHA1 */
132 int ossl_x509_algor_new_from_md(X509_ALGOR
**palg
, const EVP_MD
*md
)
134 /* Default is SHA1 so no need to create it - still success */
135 if (md
== NULL
|| EVP_MD_is_a(md
, "SHA1"))
137 *palg
= X509_ALGOR_new();
140 X509_ALGOR_set_md(*palg
, md
);
144 /* convert algorithm ID to EVP_MD, default SHA1 */
145 const EVP_MD
*ossl_x509_algor_get_md(X509_ALGOR
*alg
)
151 md
= EVP_get_digestbyobj(alg
->algorithm
);
153 ERR_raise(ERR_LIB_ASN1
, ASN1_R_UNKNOWN_DIGEST
);
157 X509_ALGOR
*ossl_x509_algor_mgf1_decode(X509_ALGOR
*alg
)
159 if (OBJ_obj2nid(alg
->algorithm
) != NID_mgf1
)
161 return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR
),
165 /* Allocate and set MGF1 algorithm ID from EVP_MD */
166 int ossl_x509_algor_md_to_mgf1(X509_ALGOR
**palg
, const EVP_MD
*mgf1md
)
168 X509_ALGOR
*algtmp
= NULL
;
169 ASN1_STRING
*stmp
= NULL
;
172 if (mgf1md
== NULL
|| EVP_MD_is_a(mgf1md
, "SHA1"))
174 /* need to embed algorithm ID inside another */
175 if (!ossl_x509_algor_new_from_md(&algtmp
, mgf1md
))
177 if (ASN1_item_pack(algtmp
, ASN1_ITEM_rptr(X509_ALGOR
), &stmp
) == NULL
)
179 *palg
= X509_ALGOR_new();
182 X509_ALGOR_set0(*palg
, OBJ_nid2obj(NID_mgf1
), V_ASN1_SEQUENCE
, stmp
);
185 ASN1_STRING_free(stmp
);
186 X509_ALGOR_free(algtmp
);