/src/openssl/crypto/x509/x_attrib.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-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 <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/objects.h> |
13 | | #include <openssl/asn1t.h> |
14 | | #include <openssl/x509.h> |
15 | | #include "x509_local.h" |
16 | | #include <crypto/x509.h> |
17 | | |
18 | | #include <crypto/asn1.h> |
19 | | |
20 | | /*- |
21 | | * X509_ATTRIBUTE: this has the following form: |
22 | | * |
23 | | * typedef struct x509_attributes_st |
24 | | * { |
25 | | * ASN1_OBJECT *object; |
26 | | * STACK_OF(ASN1_TYPE) *set; |
27 | | * } X509_ATTRIBUTE; |
28 | | * |
29 | | */ |
30 | | |
31 | | ASN1_SEQUENCE(X509_ATTRIBUTE) = { |
32 | | ASN1_SIMPLE(X509_ATTRIBUTE, object, ASN1_OBJECT), |
33 | | ASN1_SET_OF(X509_ATTRIBUTE, set, ASN1_ANY) |
34 | 0 | } ASN1_SEQUENCE_END(X509_ATTRIBUTE) |
35 | 0 |
|
36 | 0 | IMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE) |
37 | 0 | IMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE) |
38 | 0 |
|
39 | 0 | X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value) |
40 | 0 | { |
41 | 0 | X509_ATTRIBUTE *ret = NULL; |
42 | 0 | ASN1_TYPE *val = NULL; |
43 | 0 | ASN1_OBJECT *oid; |
44 | |
|
45 | 0 | if ((oid = OBJ_nid2obj(nid)) == NULL) |
46 | 0 | return NULL; |
47 | 0 | if ((ret = X509_ATTRIBUTE_new()) == NULL) |
48 | 0 | return NULL; |
49 | 0 | ret->object = oid; |
50 | 0 | if ((val = ASN1_TYPE_new()) == NULL) |
51 | 0 | goto err; |
52 | 0 | if (!sk_ASN1_TYPE_push(ret->set, val)) |
53 | 0 | goto err; |
54 | | |
55 | 0 | ASN1_TYPE_set(val, atrtype, value); |
56 | 0 | return ret; |
57 | 0 | err: |
58 | 0 | X509_ATTRIBUTE_free(ret); |
59 | 0 | ASN1_TYPE_free(val); |
60 | 0 | return NULL; |
61 | 0 | } |
62 | | |
63 | | static int print_oid(BIO *out, const ASN1_OBJECT *oid) |
64 | 0 | { |
65 | 0 | const char *ln; |
66 | 0 | char objbuf[80]; |
67 | 0 | int rc; |
68 | |
|
69 | 0 | if (OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1) <= 0) |
70 | 0 | return 0; |
71 | 0 | ln = OBJ_nid2ln(OBJ_obj2nid(oid)); |
72 | 0 | rc = (ln != NULL) |
73 | 0 | ? BIO_printf(out, "%s (%s)", objbuf, ln) |
74 | 0 | : BIO_printf(out, "%s", objbuf); |
75 | 0 | return (rc >= 0); |
76 | 0 | } |
77 | | |
78 | | int ossl_print_attribute_value(BIO *out, |
79 | | int obj_nid, |
80 | | const ASN1_TYPE *av, |
81 | | int indent) |
82 | 0 | { |
83 | 0 | ASN1_STRING *str; |
84 | 0 | unsigned char *value; |
85 | 0 | X509_NAME *xn = NULL; |
86 | 0 | int64_t int_val; |
87 | 0 | int ret = 1; |
88 | |
|
89 | 0 | switch (av->type) { |
90 | 0 | case V_ASN1_BOOLEAN: |
91 | 0 | if (av->value.boolean) { |
92 | 0 | return BIO_printf(out, "%*sTRUE", indent, "") >= 4; |
93 | 0 | } else { |
94 | 0 | return BIO_printf(out, "%*sFALSE", indent, "") >= 5; |
95 | 0 | } |
96 | | |
97 | 0 | case V_ASN1_INTEGER: |
98 | 0 | case V_ASN1_ENUMERATED: |
99 | 0 | if (BIO_printf(out, "%*s", indent, "") < 0) |
100 | 0 | return 0; |
101 | 0 | if (ASN1_ENUMERATED_get_int64(&int_val, av->value.integer) > 0) { |
102 | 0 | return BIO_printf(out, "%lld", (long long int)int_val) > 0; |
103 | 0 | } |
104 | 0 | str = av->value.integer; |
105 | 0 | return ossl_bio_print_hex(out, str->data, str->length); |
106 | | |
107 | 0 | case V_ASN1_BIT_STRING: |
108 | 0 | if (BIO_printf(out, "%*s", indent, "") < 0) |
109 | 0 | return 0; |
110 | 0 | return ossl_bio_print_hex(out, av->value.bit_string->data, |
111 | 0 | av->value.bit_string->length); |
112 | | |
113 | 0 | case V_ASN1_OCTET_STRING: |
114 | 0 | case V_ASN1_VIDEOTEXSTRING: |
115 | 0 | if (BIO_printf(out, "%*s", indent, "") < 0) |
116 | 0 | return 0; |
117 | 0 | return ossl_bio_print_hex(out, av->value.octet_string->data, |
118 | 0 | av->value.octet_string->length); |
119 | | |
120 | 0 | case V_ASN1_NULL: |
121 | 0 | return BIO_printf(out, "%*sNULL", indent, "") >= 4; |
122 | | |
123 | 0 | case V_ASN1_OBJECT: |
124 | 0 | if (BIO_printf(out, "%*s", indent, "") < 0) |
125 | 0 | return 0; |
126 | 0 | return print_oid(out, av->value.object); |
127 | | |
128 | | /* |
129 | | * ObjectDescriptor is an IMPLICIT GraphicString, but GeneralString is a |
130 | | * superset supported by OpenSSL, so we will use that anywhere a |
131 | | * GraphicString is needed here. |
132 | | */ |
133 | 0 | case V_ASN1_GENERALSTRING: |
134 | 0 | case V_ASN1_GRAPHICSTRING: |
135 | 0 | case V_ASN1_OBJECT_DESCRIPTOR: |
136 | 0 | return BIO_printf(out, "%*s%.*s", indent, "", |
137 | 0 | av->value.generalstring->length, |
138 | 0 | av->value.generalstring->data) |
139 | 0 | >= 0; |
140 | | |
141 | | /* EXTERNAL would go here. */ |
142 | | /* EMBEDDED PDV would go here. */ |
143 | | |
144 | 0 | case V_ASN1_UTF8STRING: |
145 | 0 | return BIO_printf(out, "%*s%.*s", indent, "", |
146 | 0 | av->value.utf8string->length, |
147 | 0 | av->value.utf8string->data) |
148 | 0 | >= 0; |
149 | | |
150 | 0 | case V_ASN1_REAL: |
151 | 0 | return BIO_printf(out, "%*sREAL", indent, "") >= 4; |
152 | | |
153 | | /* RELATIVE-OID would go here. */ |
154 | | /* TIME would go here. */ |
155 | | |
156 | 0 | case V_ASN1_SEQUENCE: |
157 | 0 | switch (obj_nid) { |
158 | 0 | case NID_undef: /* Unrecognized OID. */ |
159 | 0 | break; |
160 | | /* Attribute types with DN syntax. */ |
161 | 0 | case NID_member: |
162 | 0 | case NID_roleOccupant: |
163 | 0 | case NID_seeAlso: |
164 | 0 | case NID_manager: |
165 | 0 | case NID_documentAuthor: |
166 | 0 | case NID_secretary: |
167 | 0 | case NID_associatedName: |
168 | 0 | case NID_dITRedirect: |
169 | 0 | case NID_owner: |
170 | | /* |
171 | | * d2i_ functions increment the ppin pointer. See doc/man3/d2i_X509.pod. |
172 | | * This preserves the original pointer. We don't want to corrupt this |
173 | | * value. |
174 | | */ |
175 | 0 | value = av->value.sequence->data; |
176 | 0 | xn = d2i_X509_NAME(NULL, |
177 | 0 | (const unsigned char **)&value, |
178 | 0 | av->value.sequence->length); |
179 | 0 | if (xn == NULL) { |
180 | 0 | BIO_puts(out, "(COULD NOT DECODE DISTINGUISHED NAME)\n"); |
181 | 0 | return 0; |
182 | 0 | } |
183 | 0 | if (X509_NAME_print_ex(out, xn, indent, XN_FLAG_SEP_CPLUS_SPC) <= 0) |
184 | 0 | ret = 0; |
185 | 0 | X509_NAME_free(xn); |
186 | 0 | return ret; |
187 | | |
188 | 0 | default: |
189 | 0 | break; |
190 | 0 | } |
191 | 0 | return ASN1_parse_dump(out, av->value.sequence->data, |
192 | 0 | av->value.sequence->length, indent, 1) |
193 | 0 | > 0; |
194 | | |
195 | 0 | case V_ASN1_SET: |
196 | 0 | return ASN1_parse_dump(out, av->value.set->data, |
197 | 0 | av->value.set->length, indent, 1) |
198 | 0 | > 0; |
199 | | |
200 | | /* |
201 | | * UTCTime ::= [UNIVERSAL 23] IMPLICIT VisibleString |
202 | | * GeneralizedTime ::= [UNIVERSAL 24] IMPLICIT VisibleString |
203 | | * VisibleString is a superset for NumericString, so it will work for that. |
204 | | */ |
205 | 0 | case V_ASN1_VISIBLESTRING: |
206 | 0 | case V_ASN1_UTCTIME: |
207 | 0 | case V_ASN1_GENERALIZEDTIME: |
208 | 0 | case V_ASN1_NUMERICSTRING: |
209 | 0 | return BIO_printf(out, "%*s%.*s", indent, "", |
210 | 0 | av->value.visiblestring->length, |
211 | 0 | av->value.visiblestring->data) |
212 | 0 | >= 0; |
213 | | |
214 | 0 | case V_ASN1_PRINTABLESTRING: |
215 | 0 | return BIO_printf(out, "%*s%.*s", indent, "", |
216 | 0 | av->value.printablestring->length, |
217 | 0 | av->value.printablestring->data) |
218 | 0 | >= 0; |
219 | | |
220 | 0 | case V_ASN1_T61STRING: |
221 | 0 | return BIO_printf(out, "%*s%.*s", indent, "", |
222 | 0 | av->value.t61string->length, |
223 | 0 | av->value.t61string->data) |
224 | 0 | >= 0; |
225 | | |
226 | 0 | case V_ASN1_IA5STRING: |
227 | 0 | return BIO_printf(out, "%*s%.*s", indent, "", |
228 | 0 | av->value.ia5string->length, |
229 | 0 | av->value.ia5string->data) |
230 | 0 | >= 0; |
231 | | |
232 | | /* UniversalString would go here. */ |
233 | | /* CHARACTER STRING would go here. */ |
234 | | /* BMPString would go here. */ |
235 | | /* DATE would go here. */ |
236 | | /* TIME-OF-DAY would go here. */ |
237 | | /* DATE-TIME would go here. */ |
238 | | /* DURATION would go here. */ |
239 | | /* OID-IRI would go here. */ |
240 | | /* RELATIVE-OID-IRI would go here. */ |
241 | | |
242 | | /* Would it be appropriate to just hexdump? */ |
243 | 0 | default: |
244 | 0 | return BIO_printf(out, |
245 | 0 | "%*s<Unsupported tag %d>", |
246 | 0 | indent, |
247 | 0 | "", |
248 | 0 | av->type) |
249 | 0 | >= 0; |
250 | 0 | } |
251 | 0 | } |