/src/openssl30/crypto/asn1/x_algor.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include <stddef.h> |
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" |
17 | | |
18 | | ASN1_SEQUENCE(X509_ALGOR) = { |
19 | | ASN1_SIMPLE(X509_ALGOR, algorithm, ASN1_OBJECT), |
20 | | ASN1_OPT(X509_ALGOR, parameter, ASN1_ANY) |
21 | 35.6M | } ASN1_SEQUENCE_END(X509_ALGOR) |
22 | 35.6M | |
23 | 35.6M | ASN1_ITEM_TEMPLATE(X509_ALGORS) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, algorithms, X509_ALGOR) |
24 | 35.6M | ASN1_ITEM_TEMPLATE_END(X509_ALGORS) |
25 | | |
26 | | IMPLEMENT_ASN1_FUNCTIONS(X509_ALGOR) |
27 | | IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(X509_ALGORS, X509_ALGORS, X509_ALGORS) |
28 | | IMPLEMENT_ASN1_DUP_FUNCTION(X509_ALGOR) |
29 | | |
30 | | int X509_ALGOR_set0(X509_ALGOR *alg, ASN1_OBJECT *aobj, int ptype, void *pval) |
31 | 30 | { |
32 | 30 | if (alg == NULL) |
33 | 0 | return 0; |
34 | | |
35 | 30 | if (ptype != V_ASN1_UNDEF) { |
36 | 19 | if (alg->parameter == NULL) |
37 | 19 | alg->parameter = ASN1_TYPE_new(); |
38 | 19 | if (alg->parameter == NULL) |
39 | 0 | return 0; |
40 | 19 | } |
41 | | |
42 | 30 | ASN1_OBJECT_free(alg->algorithm); |
43 | 30 | alg->algorithm = aobj; |
44 | | |
45 | 30 | if (ptype == 0) |
46 | 0 | return 1; |
47 | 30 | if (ptype == V_ASN1_UNDEF) { |
48 | 11 | ASN1_TYPE_free(alg->parameter); |
49 | 11 | alg->parameter = NULL; |
50 | 11 | } else |
51 | 19 | ASN1_TYPE_set(alg->parameter, ptype, pval); |
52 | 30 | return 1; |
53 | 30 | } |
54 | | |
55 | | void X509_ALGOR_get0(const ASN1_OBJECT **paobj, int *pptype, |
56 | | const void **ppval, const X509_ALGOR *algor) |
57 | 2.60M | { |
58 | 2.60M | if (paobj) |
59 | 1.13M | *paobj = algor->algorithm; |
60 | 2.60M | if (pptype) { |
61 | 1.62M | if (algor->parameter == NULL) { |
62 | 48.2k | *pptype = V_ASN1_UNDEF; |
63 | 48.2k | return; |
64 | 48.2k | } else |
65 | 1.57M | *pptype = algor->parameter->type; |
66 | 1.57M | if (ppval) |
67 | 1.57M | *ppval = algor->parameter->value.ptr; |
68 | 1.57M | } |
69 | 2.60M | } |
70 | | |
71 | | /* Set up an X509_ALGOR DigestAlgorithmIdentifier from an EVP_MD */ |
72 | | |
73 | | void X509_ALGOR_set_md(X509_ALGOR *alg, const EVP_MD *md) |
74 | 0 | { |
75 | 0 | int param_type; |
76 | |
|
77 | 0 | if (md->flags & EVP_MD_FLAG_DIGALGID_ABSENT) |
78 | 0 | param_type = V_ASN1_UNDEF; |
79 | 0 | else |
80 | 0 | param_type = V_ASN1_NULL; |
81 | |
|
82 | 0 | X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_get_type(md)), param_type, NULL); |
83 | 0 | } |
84 | | |
85 | | int X509_ALGOR_cmp(const X509_ALGOR *a, const X509_ALGOR *b) |
86 | 7.33k | { |
87 | 7.33k | int rv; |
88 | 7.33k | rv = OBJ_cmp(a->algorithm, b->algorithm); |
89 | 7.33k | if (rv) |
90 | 4.40k | return rv; |
91 | 2.93k | if (!a->parameter && !b->parameter) |
92 | 11 | return 0; |
93 | 2.92k | return ASN1_TYPE_cmp(a->parameter, b->parameter); |
94 | 2.93k | } |
95 | | |
96 | | int X509_ALGOR_copy(X509_ALGOR *dest, const X509_ALGOR *src) |
97 | 0 | { |
98 | 0 | if (src == NULL || dest == NULL) |
99 | 0 | return 0; |
100 | | |
101 | 0 | if (dest->algorithm) |
102 | 0 | ASN1_OBJECT_free(dest->algorithm); |
103 | 0 | dest->algorithm = NULL; |
104 | |
|
105 | 0 | if (dest->parameter) |
106 | 0 | ASN1_TYPE_free(dest->parameter); |
107 | 0 | dest->parameter = NULL; |
108 | |
|
109 | 0 | if (src->algorithm) |
110 | 0 | if ((dest->algorithm = OBJ_dup(src->algorithm)) == NULL) |
111 | 0 | return 0; |
112 | | |
113 | 0 | if (src->parameter != NULL) { |
114 | 0 | dest->parameter = ASN1_TYPE_new(); |
115 | 0 | if (dest->parameter == NULL) |
116 | 0 | return 0; |
117 | | |
118 | | /* Assuming this is also correct for a BOOL. |
119 | | * set does copy as a side effect. |
120 | | */ |
121 | 0 | if (ASN1_TYPE_set1(dest->parameter, src->parameter->type, |
122 | 0 | src->parameter->value.ptr) |
123 | 0 | == 0) |
124 | 0 | return 0; |
125 | 0 | } |
126 | | |
127 | 0 | return 1; |
128 | 0 | } |
129 | | |
130 | | /* allocate and set algorithm ID from EVP_MD, default SHA1 */ |
131 | | int ossl_x509_algor_new_from_md(X509_ALGOR **palg, const EVP_MD *md) |
132 | 0 | { |
133 | | /* Default is SHA1 so no need to create it - still success */ |
134 | 0 | if (md == NULL || EVP_MD_is_a(md, "SHA1")) |
135 | 0 | return 1; |
136 | 0 | *palg = X509_ALGOR_new(); |
137 | 0 | if (*palg == NULL) |
138 | 0 | return 0; |
139 | 0 | X509_ALGOR_set_md(*palg, md); |
140 | 0 | return 1; |
141 | 0 | } |
142 | | |
143 | | /* convert algorithm ID to EVP_MD, default SHA1 */ |
144 | | const EVP_MD *ossl_x509_algor_get_md(X509_ALGOR *alg) |
145 | 31.8k | { |
146 | 31.8k | const EVP_MD *md; |
147 | | |
148 | 31.8k | if (alg == NULL) |
149 | 31.5k | return EVP_sha1(); |
150 | 358 | md = EVP_get_digestbyobj(alg->algorithm); |
151 | 358 | if (md == NULL) |
152 | 358 | ERR_raise(ERR_LIB_ASN1, ASN1_R_UNKNOWN_DIGEST); |
153 | 358 | return md; |
154 | 31.8k | } |
155 | | |
156 | | X509_ALGOR *ossl_x509_algor_mgf1_decode(X509_ALGOR *alg) |
157 | 988 | { |
158 | 988 | if (OBJ_obj2nid(alg->algorithm) != NID_mgf1) |
159 | 173 | return NULL; |
160 | 815 | return ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR), |
161 | 815 | alg->parameter); |
162 | 988 | } |
163 | | |
164 | | /* Allocate and set MGF1 algorithm ID from EVP_MD */ |
165 | | int ossl_x509_algor_md_to_mgf1(X509_ALGOR **palg, const EVP_MD *mgf1md) |
166 | 0 | { |
167 | 0 | X509_ALGOR *algtmp = NULL; |
168 | 0 | ASN1_STRING *stmp = NULL; |
169 | |
|
170 | 0 | *palg = NULL; |
171 | 0 | if (mgf1md == NULL || EVP_MD_is_a(mgf1md, "SHA1")) |
172 | 0 | return 1; |
173 | | /* need to embed algorithm ID inside another */ |
174 | 0 | if (!ossl_x509_algor_new_from_md(&algtmp, mgf1md)) |
175 | 0 | goto err; |
176 | 0 | if (ASN1_item_pack(algtmp, ASN1_ITEM_rptr(X509_ALGOR), &stmp) == NULL) |
177 | 0 | goto err; |
178 | 0 | *palg = X509_ALGOR_new(); |
179 | 0 | if (*palg == NULL) |
180 | 0 | goto err; |
181 | 0 | if (!X509_ALGOR_set0(*palg, OBJ_nid2obj(NID_mgf1), V_ASN1_SEQUENCE, stmp)) { |
182 | 0 | X509_ALGOR_free(*palg); |
183 | 0 | *palg = NULL; |
184 | 0 | goto err; |
185 | 0 | } |
186 | 0 | stmp = NULL; |
187 | 0 | err: |
188 | 0 | ASN1_STRING_free(stmp); |
189 | 0 | X509_ALGOR_free(algtmp); |
190 | 0 | if (*palg != NULL) |
191 | 0 | return 1; |
192 | 0 | return 0; |
193 | 0 | } |