/src/openssl/crypto/x509/x509_acert.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2021-2024 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 <string.h> |
11 | | #include <crypto/ctype.h> |
12 | | #include <openssl/asn1t.h> |
13 | | #include <openssl/err.h> |
14 | | #include <openssl/x509.h> |
15 | | #include <openssl/x509v3.h> |
16 | | #include "x509_acert.h" |
17 | | |
18 | | /* |
19 | | * OpenSSL ASN.1 template translation of RFC 5755 4.1. |
20 | | */ |
21 | | |
22 | | ASN1_SEQUENCE(OSSL_OBJECT_DIGEST_INFO) = { |
23 | | ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, digestedObjectType, ASN1_ENUMERATED), |
24 | | ASN1_OPT(OSSL_OBJECT_DIGEST_INFO, otherObjectTypeID, ASN1_OBJECT), |
25 | | ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, digestAlgorithm, X509_ALGOR), |
26 | | ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, objectDigest, ASN1_BIT_STRING), |
27 | | } ASN1_SEQUENCE_END(OSSL_OBJECT_DIGEST_INFO) |
28 | | |
29 | | ASN1_SEQUENCE(OSSL_ISSUER_SERIAL) = { |
30 | | ASN1_SEQUENCE_OF(OSSL_ISSUER_SERIAL, issuer, GENERAL_NAME), |
31 | | ASN1_EMBED(OSSL_ISSUER_SERIAL, serial, ASN1_INTEGER), |
32 | | ASN1_OPT(OSSL_ISSUER_SERIAL, issuerUID, ASN1_BIT_STRING), |
33 | | } ASN1_SEQUENCE_END(OSSL_ISSUER_SERIAL) |
34 | | |
35 | | ASN1_SEQUENCE(X509_ACERT_ISSUER_V2FORM) = { |
36 | | ASN1_SEQUENCE_OF_OPT(X509_ACERT_ISSUER_V2FORM, issuerName, GENERAL_NAME), |
37 | | ASN1_IMP_OPT(X509_ACERT_ISSUER_V2FORM, baseCertificateId, OSSL_ISSUER_SERIAL, 0), |
38 | | ASN1_IMP_OPT(X509_ACERT_ISSUER_V2FORM, objectDigestInfo, OSSL_OBJECT_DIGEST_INFO, 1), |
39 | | } ASN1_SEQUENCE_END(X509_ACERT_ISSUER_V2FORM) |
40 | | |
41 | | ASN1_CHOICE(X509_ACERT_ISSUER) = { |
42 | | ASN1_SEQUENCE_OF(X509_ACERT_ISSUER, u.v1Form, GENERAL_NAME), |
43 | | ASN1_IMP(X509_ACERT_ISSUER, u.v2Form, X509_ACERT_ISSUER_V2FORM, 0), |
44 | | } ASN1_CHOICE_END(X509_ACERT_ISSUER) |
45 | | |
46 | | ASN1_SEQUENCE(X509_HOLDER) = { |
47 | | ASN1_IMP_OPT(X509_HOLDER, baseCertificateID, OSSL_ISSUER_SERIAL, 0), |
48 | | ASN1_IMP_SEQUENCE_OF_OPT(X509_HOLDER, entityName, GENERAL_NAME, 1), |
49 | | ASN1_IMP_OPT(X509_HOLDER, objectDigestInfo, OSSL_OBJECT_DIGEST_INFO, 2), |
50 | | } ASN1_SEQUENCE_END(X509_HOLDER) |
51 | | |
52 | | ASN1_SEQUENCE(X509_ACERT_INFO) = { |
53 | | ASN1_EMBED(X509_ACERT_INFO, version, ASN1_INTEGER), |
54 | | ASN1_EMBED(X509_ACERT_INFO, holder, X509_HOLDER), |
55 | | ASN1_EMBED(X509_ACERT_INFO, issuer, X509_ACERT_ISSUER), |
56 | | ASN1_EMBED(X509_ACERT_INFO, signature, X509_ALGOR), |
57 | | ASN1_EMBED(X509_ACERT_INFO, serialNumber, ASN1_INTEGER), |
58 | | ASN1_EMBED(X509_ACERT_INFO, validityPeriod, X509_VAL), |
59 | | ASN1_SEQUENCE_OF(X509_ACERT_INFO, attributes, X509_ATTRIBUTE), |
60 | | ASN1_OPT(X509_ACERT_INFO, issuerUID, ASN1_BIT_STRING), |
61 | | ASN1_SEQUENCE_OF_OPT(X509_ACERT_INFO, extensions, X509_EXTENSION), |
62 | | } ASN1_SEQUENCE_END(X509_ACERT_INFO) |
63 | | |
64 | | ASN1_SEQUENCE(X509_ACERT) = { |
65 | | ASN1_SIMPLE(X509_ACERT, acinfo, X509_ACERT_INFO), |
66 | | ASN1_EMBED(X509_ACERT, sig_alg, X509_ALGOR), |
67 | | ASN1_EMBED(X509_ACERT, signature, ASN1_BIT_STRING), |
68 | | } ASN1_SEQUENCE_END(X509_ACERT) |
69 | | |
70 | | IMPLEMENT_ASN1_FUNCTIONS(X509_ACERT) |
71 | | IMPLEMENT_ASN1_DUP_FUNCTION(X509_ACERT) |
72 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X509_ACERT_INFO) |
73 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(OSSL_ISSUER_SERIAL) |
74 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(OSSL_OBJECT_DIGEST_INFO) |
75 | | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X509_ACERT_ISSUER_V2FORM) |
76 | | |
77 | | IMPLEMENT_PEM_rw(X509_ACERT, X509_ACERT, PEM_STRING_ACERT, X509_ACERT) |
78 | | |
79 | | static X509_NAME *get_dirName(const GENERAL_NAMES *names) |
80 | 0 | { |
81 | 0 | GENERAL_NAME *dirName; |
82 | |
|
83 | 0 | if (sk_GENERAL_NAME_num(names) != 1) |
84 | 0 | return NULL; |
85 | | |
86 | 0 | dirName = sk_GENERAL_NAME_value(names, 0); |
87 | 0 | if (dirName->type != GEN_DIRNAME) |
88 | 0 | return NULL; |
89 | | |
90 | 0 | return dirName->d.directoryName; |
91 | 0 | } |
92 | | |
93 | | void OSSL_OBJECT_DIGEST_INFO_get0_digest(const OSSL_OBJECT_DIGEST_INFO *o, |
94 | | int *digestedObjectType, |
95 | | const X509_ALGOR **digestAlgorithm, |
96 | | const ASN1_BIT_STRING **digest) |
97 | 0 | { |
98 | 0 | if (digestedObjectType != NULL) |
99 | 0 | *digestedObjectType = ASN1_ENUMERATED_get(&o->digestedObjectType); |
100 | 0 | if (digestAlgorithm != NULL) |
101 | 0 | *digestAlgorithm = &o->digestAlgorithm; |
102 | 0 | if (digest != NULL) |
103 | 0 | *digest = &o->objectDigest; |
104 | 0 | } |
105 | | |
106 | | const X509_NAME *OSSL_ISSUER_SERIAL_get0_issuer(const OSSL_ISSUER_SERIAL *isss) |
107 | 0 | { |
108 | 0 | return get_dirName(isss->issuer); |
109 | 0 | } |
110 | | |
111 | | const ASN1_INTEGER *OSSL_ISSUER_SERIAL_get0_serial(const OSSL_ISSUER_SERIAL *isss) |
112 | 0 | { |
113 | 0 | return &isss->serial; |
114 | 0 | } |
115 | | |
116 | | const ASN1_BIT_STRING *OSSL_ISSUER_SERIAL_get0_issuerUID(const OSSL_ISSUER_SERIAL *isss) |
117 | 0 | { |
118 | 0 | return isss->issuerUID; |
119 | 0 | } |
120 | | |
121 | | long X509_ACERT_get_version(const X509_ACERT *x) |
122 | 0 | { |
123 | 0 | return ASN1_INTEGER_get(&x->acinfo->version); |
124 | 0 | } |
125 | | |
126 | | void X509_ACERT_get0_signature(const X509_ACERT *x, |
127 | | const ASN1_BIT_STRING **psig, |
128 | | const X509_ALGOR **palg) |
129 | 0 | { |
130 | 0 | if (psig != NULL) |
131 | 0 | *psig = &x->signature; |
132 | 0 | if (palg != NULL) |
133 | 0 | *palg = &x->sig_alg; |
134 | 0 | } |
135 | | |
136 | | int X509_ACERT_get_signature_nid(const X509_ACERT *x) |
137 | 0 | { |
138 | 0 | return OBJ_obj2nid(x->sig_alg.algorithm); |
139 | 0 | } |
140 | | |
141 | | const GENERAL_NAMES *X509_ACERT_get0_holder_entityName(const X509_ACERT *x) |
142 | 0 | { |
143 | 0 | return x->acinfo->holder.entityName; |
144 | 0 | } |
145 | | |
146 | | const OSSL_ISSUER_SERIAL *X509_ACERT_get0_holder_baseCertId(const X509_ACERT *x) |
147 | 0 | { |
148 | 0 | return x->acinfo->holder.baseCertificateID; |
149 | 0 | } |
150 | | |
151 | | const OSSL_OBJECT_DIGEST_INFO *X509_ACERT_get0_holder_digest(const X509_ACERT *x) |
152 | 0 | { |
153 | 0 | return x->acinfo->holder.objectDigestInfo; |
154 | 0 | } |
155 | | |
156 | | const X509_NAME *X509_ACERT_get0_issuerName(const X509_ACERT *x) |
157 | 0 | { |
158 | 0 | if (x->acinfo->issuer.type != X509_ACERT_ISSUER_V2 |
159 | 0 | || x->acinfo->issuer.u.v2Form == NULL) |
160 | 0 | return NULL; |
161 | | |
162 | 0 | return get_dirName(x->acinfo->issuer.u.v2Form->issuerName); |
163 | 0 | } |
164 | | |
165 | | const ASN1_BIT_STRING *X509_ACERT_get0_issuerUID(const X509_ACERT *x) |
166 | 0 | { |
167 | 0 | return x->acinfo->issuerUID; |
168 | 0 | } |
169 | | |
170 | | const X509_ALGOR *X509_ACERT_get0_info_sigalg(const X509_ACERT *x) |
171 | 0 | { |
172 | 0 | return &x->acinfo->signature; |
173 | 0 | } |
174 | | |
175 | | const ASN1_INTEGER *X509_ACERT_get0_serialNumber(const X509_ACERT *x) |
176 | 0 | { |
177 | 0 | return &x->acinfo->serialNumber; |
178 | 0 | } |
179 | | |
180 | | const ASN1_GENERALIZEDTIME *X509_ACERT_get0_notBefore(const X509_ACERT *x) |
181 | 0 | { |
182 | 0 | return x->acinfo->validityPeriod.notBefore; |
183 | 0 | } |
184 | | |
185 | | const ASN1_GENERALIZEDTIME *X509_ACERT_get0_notAfter(const X509_ACERT *x) |
186 | 0 | { |
187 | 0 | return x->acinfo->validityPeriod.notAfter; |
188 | 0 | } |
189 | | |
190 | | /* Attribute management functions */ |
191 | | |
192 | | int X509_ACERT_get_attr_count(const X509_ACERT *x) |
193 | 0 | { |
194 | 0 | return X509at_get_attr_count(x->acinfo->attributes); |
195 | 0 | } |
196 | | |
197 | | int X509_ACERT_get_attr_by_NID(const X509_ACERT *x, int nid, int lastpos) |
198 | 0 | { |
199 | 0 | return X509at_get_attr_by_NID(x->acinfo->attributes, nid, lastpos); |
200 | 0 | } |
201 | | |
202 | | int X509_ACERT_get_attr_by_OBJ(const X509_ACERT *x, const ASN1_OBJECT *obj, |
203 | | int lastpos) |
204 | 0 | { |
205 | 0 | return X509at_get_attr_by_OBJ(x->acinfo->attributes, obj, lastpos); |
206 | 0 | } |
207 | | |
208 | | X509_ATTRIBUTE *X509_ACERT_get_attr(const X509_ACERT *x, int loc) |
209 | 0 | { |
210 | 0 | return X509at_get_attr(x->acinfo->attributes, loc); |
211 | 0 | } |
212 | | |
213 | | X509_ATTRIBUTE *X509_ACERT_delete_attr(X509_ACERT *x, int loc) |
214 | 0 | { |
215 | 0 | return X509at_delete_attr(x->acinfo->attributes, loc); |
216 | 0 | } |
217 | | |
218 | | int X509_ACERT_add1_attr(X509_ACERT *x, X509_ATTRIBUTE *attr) |
219 | 0 | { |
220 | 0 | STACK_OF(X509_ATTRIBUTE) **attrs = &x->acinfo->attributes; |
221 | |
|
222 | 0 | return X509at_add1_attr(attrs, attr) != NULL; |
223 | 0 | } |
224 | | |
225 | | int X509_ACERT_add1_attr_by_OBJ(X509_ACERT *x, const ASN1_OBJECT *obj, |
226 | | int type, const void *bytes, int len) |
227 | 0 | { |
228 | 0 | STACK_OF(X509_ATTRIBUTE) **attrs = &x->acinfo->attributes; |
229 | |
|
230 | 0 | return X509at_add1_attr_by_OBJ(attrs, obj, type, bytes, len) != NULL; |
231 | 0 | } |
232 | | |
233 | | int X509_ACERT_add1_attr_by_NID(X509_ACERT *x, int nid, int type, |
234 | | const void *bytes, int len) |
235 | 0 | { |
236 | 0 | STACK_OF(X509_ATTRIBUTE) **attrs = &x->acinfo->attributes; |
237 | |
|
238 | 0 | return X509at_add1_attr_by_NID(attrs, nid, type, bytes, len) != NULL; |
239 | 0 | } |
240 | | |
241 | | int X509_ACERT_add1_attr_by_txt(X509_ACERT *x, const char *attrname, int type, |
242 | | const unsigned char *bytes, int len) |
243 | 0 | { |
244 | 0 | STACK_OF(X509_ATTRIBUTE) **attrs = &x->acinfo->attributes; |
245 | |
|
246 | 0 | return X509at_add1_attr_by_txt(attrs, attrname, type, bytes, len) != NULL; |
247 | 0 | } |
248 | | |
249 | | static int check_asn1_attribute(const char **value) |
250 | 0 | { |
251 | 0 | const char *p = *value; |
252 | |
|
253 | 0 | if (strncmp(p, "ASN1:", 5) != 0) |
254 | 0 | return 0; |
255 | | |
256 | 0 | p += 5; |
257 | 0 | while (ossl_isspace(*p)) |
258 | 0 | p++; |
259 | |
|
260 | 0 | *value = p; |
261 | 0 | return 1; |
262 | 0 | } |
263 | | |
264 | | int X509_ACERT_add_attr_nconf(CONF *conf, const char *section, |
265 | | X509_ACERT *acert) |
266 | 0 | { |
267 | 0 | int ret = 0, i; |
268 | 0 | STACK_OF(CONF_VALUE) *attr_sk = NCONF_get_section(conf, section); |
269 | |
|
270 | 0 | if (attr_sk == NULL) |
271 | 0 | goto err; |
272 | | |
273 | 0 | for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) { |
274 | 0 | CONF_VALUE *v = sk_CONF_VALUE_value(attr_sk, i); |
275 | 0 | const char *value = v->value; |
276 | |
|
277 | 0 | if (value == NULL) { |
278 | 0 | ERR_raise_data(ERR_LIB_X509, X509_R_INVALID_ATTRIBUTES, |
279 | 0 | "name=%s,section=%s",v->name, section); |
280 | 0 | goto err; |
281 | 0 | } |
282 | | |
283 | 0 | if (check_asn1_attribute(&value) == 1) { |
284 | 0 | int att_len; |
285 | 0 | unsigned char *att_data = NULL; |
286 | 0 | ASN1_TYPE *asn1 = ASN1_generate_nconf(value, conf); |
287 | |
|
288 | 0 | if (asn1 == NULL) |
289 | 0 | goto err; |
290 | | |
291 | 0 | att_len = i2d_ASN1_TYPE(asn1, &att_data); |
292 | |
|
293 | 0 | ret = X509_ACERT_add1_attr_by_txt(acert, v->name, V_ASN1_SEQUENCE, |
294 | 0 | att_data, att_len); |
295 | 0 | OPENSSL_free(att_data); |
296 | 0 | ASN1_TYPE_free(asn1); |
297 | |
|
298 | 0 | if (!ret) |
299 | 0 | goto err; |
300 | 0 | } else { |
301 | 0 | ret = X509_ACERT_add1_attr_by_txt(acert, v->name, |
302 | 0 | V_ASN1_OCTET_STRING, |
303 | 0 | (unsigned char *)value, |
304 | 0 | strlen(value)); |
305 | 0 | if (!ret) |
306 | 0 | goto err; |
307 | 0 | } |
308 | 0 | } |
309 | 0 | ret = 1; |
310 | 0 | err: |
311 | 0 | return ret; |
312 | 0 | } |
313 | | |
314 | | void *X509_ACERT_get_ext_d2i(const X509_ACERT *x, int nid, int *crit, int *idx) |
315 | 0 | { |
316 | 0 | return X509V3_get_d2i(x->acinfo->extensions, nid, crit, idx); |
317 | 0 | } |
318 | | |
319 | | int X509_ACERT_add1_ext_i2d(X509_ACERT *x, int nid, void *value, int crit, |
320 | | unsigned long flags) |
321 | 0 | { |
322 | 0 | return X509V3_add1_i2d(&x->acinfo->extensions, nid, value, crit, flags); |
323 | 0 | } |
324 | | |
325 | | const STACK_OF(X509_EXTENSION) *X509_ACERT_get0_extensions(const X509_ACERT *x) |
326 | 0 | { |
327 | 0 | return x->acinfo->extensions; |
328 | 0 | } |