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