/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.47M | } ASN1_SEQUENCE_END(X509_ATTRIBUTE) |
33 | 3.47M | |
34 | 3.47M | IMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE) |
35 | 3.47M | IMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE) |
36 | 3.47M | |
37 | 3.47M | X509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value) |
38 | 3.47M | { |
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 | 5.13k | { |
63 | 5.13k | int result = 1; |
64 | 5.13k | char *hexbuf; |
65 | | |
66 | 5.13k | if (len == 0) |
67 | 2.76k | return 1; |
68 | | |
69 | 2.36k | hexbuf = OPENSSL_buf2hexstr(buf, len); |
70 | 2.36k | if (hexbuf == NULL) |
71 | 0 | return 0; |
72 | 2.36k | result = BIO_puts(out, hexbuf) > 0; |
73 | | |
74 | 2.36k | OPENSSL_free(hexbuf); |
75 | 2.36k | return result; |
76 | 2.36k | } |
77 | | |
78 | | static int print_oid(BIO *out, const ASN1_OBJECT *oid) |
79 | 9.95k | { |
80 | 9.95k | const char *ln; |
81 | 9.95k | char objbuf[80]; |
82 | 9.95k | int rc; |
83 | | |
84 | 9.95k | if (OBJ_obj2txt(objbuf, sizeof(objbuf), oid, 1) <= 0) |
85 | 1 | return 0; |
86 | 9.95k | ln = OBJ_nid2ln(OBJ_obj2nid(oid)); |
87 | 9.95k | rc = (ln != NULL) |
88 | 9.95k | ? BIO_printf(out, "%s (%s)", objbuf, ln) |
89 | 9.95k | : BIO_printf(out, "%s", objbuf); |
90 | 9.95k | return (rc >= 0); |
91 | 9.95k | } |
92 | | |
93 | | int ossl_print_attribute_value(BIO *out, |
94 | | int obj_nid, |
95 | | const ASN1_TYPE *av, |
96 | | int indent) |
97 | 470k | { |
98 | 470k | ASN1_STRING *str; |
99 | 470k | unsigned char *value; |
100 | 470k | X509_NAME *xn = NULL; |
101 | 470k | int64_t int_val; |
102 | 470k | int ret = 1; |
103 | | |
104 | 470k | switch (av->type) { |
105 | 15.1k | case V_ASN1_BOOLEAN: |
106 | 15.1k | if (av->value.boolean) { |
107 | 12.3k | return BIO_printf(out, "%*sTRUE", indent, "") >= 4; |
108 | 12.3k | } else { |
109 | 2.84k | return BIO_printf(out, "%*sFALSE", indent, "") >= 5; |
110 | 2.84k | } |
111 | | |
112 | 8.80k | case V_ASN1_INTEGER: |
113 | 17.8k | case V_ASN1_ENUMERATED: |
114 | 17.8k | if (BIO_printf(out, "%*s", indent, "") < 0) |
115 | 0 | return 0; |
116 | 17.8k | if (ASN1_ENUMERATED_get_int64(&int_val, av->value.integer) > 0) { |
117 | 7.51k | return BIO_printf(out, "%lld", (long long int)int_val) > 0; |
118 | 7.51k | } |
119 | 10.3k | str = av->value.integer; |
120 | 10.3k | return print_hex(out, str->data, str->length); |
121 | | |
122 | 9.58k | case V_ASN1_BIT_STRING: |
123 | 9.58k | if (BIO_printf(out, "%*s", indent, "") < 0) |
124 | 0 | return 0; |
125 | 9.58k | return print_hex(out, av->value.bit_string->data, |
126 | 9.58k | av->value.bit_string->length); |
127 | | |
128 | 7.96k | case V_ASN1_OCTET_STRING: |
129 | 15.5k | case V_ASN1_VIDEOTEXSTRING: |
130 | 15.5k | if (BIO_printf(out, "%*s", indent, "") < 0) |
131 | 0 | return 0; |
132 | 15.5k | return print_hex(out, av->value.octet_string->data, |
133 | 15.5k | av->value.octet_string->length); |
134 | | |
135 | 954 | case V_ASN1_NULL: |
136 | 954 | return BIO_printf(out, "%*sNULL", indent, "") >= 4; |
137 | | |
138 | 9.95k | case V_ASN1_OBJECT: |
139 | 9.95k | if (BIO_printf(out, "%*s", indent, "") < 0) |
140 | 0 | return 0; |
141 | 9.95k | return print_oid(out, av->value.object); |
142 | | |
143 | | /* |
144 | | * ObjectDescriptor is an IMPLICIT GraphicString, but GeneralString is a |
145 | | * superset supported by OpenSSL, so we will use that anywhere a |
146 | | * GraphicString is needed here. |
147 | | */ |
148 | 5.31k | case V_ASN1_GENERALSTRING: |
149 | 9.99k | case V_ASN1_GRAPHICSTRING: |
150 | 29.3k | case V_ASN1_OBJECT_DESCRIPTOR: |
151 | 29.3k | return BIO_printf(out, "%*s%.*s", indent, "", |
152 | 29.3k | av->value.generalstring->length, |
153 | 29.3k | av->value.generalstring->data) |
154 | 29.3k | >= 0; |
155 | | |
156 | | /* EXTERNAL would go here. */ |
157 | | /* EMBEDDED PDV would go here. */ |
158 | | |
159 | 4.49k | case V_ASN1_UTF8STRING: |
160 | 4.49k | return BIO_printf(out, "%*s%.*s", indent, "", |
161 | 4.49k | av->value.utf8string->length, |
162 | 4.49k | av->value.utf8string->data) |
163 | 4.49k | >= 0; |
164 | | |
165 | 1.76k | case V_ASN1_REAL: |
166 | 1.76k | return BIO_printf(out, "%*sREAL", indent, "") >= 4; |
167 | | |
168 | | /* RELATIVE-OID would go here. */ |
169 | | /* TIME would go here. */ |
170 | | |
171 | 120k | case V_ASN1_SEQUENCE: |
172 | 120k | switch (obj_nid) { |
173 | 105k | case NID_undef: /* Unrecognized OID. */ |
174 | 105k | break; |
175 | | /* Attribute types with DN syntax. */ |
176 | 352 | case NID_member: |
177 | 694 | case NID_roleOccupant: |
178 | 3.44k | case NID_seeAlso: |
179 | 3.44k | case NID_manager: |
180 | 3.44k | case NID_documentAuthor: |
181 | 3.44k | case NID_secretary: |
182 | 3.44k | case NID_associatedName: |
183 | 3.44k | case NID_dITRedirect: |
184 | 8.55k | case NID_owner: |
185 | | /* |
186 | | * d2i_ functions increment the ppin pointer. See doc/man3/d2i_X509.pod. |
187 | | * This preserves the original pointer. We don't want to corrupt this |
188 | | * value. |
189 | | */ |
190 | 8.55k | value = av->value.sequence->data; |
191 | 8.55k | xn = d2i_X509_NAME(NULL, |
192 | 8.55k | (const unsigned char **)&value, |
193 | 8.55k | av->value.sequence->length); |
194 | 8.55k | if (xn == NULL) { |
195 | 70 | BIO_puts(out, "(COULD NOT DECODE DISTINGUISHED NAME)\n"); |
196 | 70 | return 0; |
197 | 70 | } |
198 | 8.48k | if (X509_NAME_print_ex(out, xn, indent, XN_FLAG_SEP_CPLUS_SPC) <= 0) |
199 | 0 | ret = 0; |
200 | 8.48k | X509_NAME_free(xn); |
201 | 8.48k | return ret; |
202 | | |
203 | 6.24k | default: |
204 | 6.24k | break; |
205 | 120k | } |
206 | 111k | return ASN1_parse_dump(out, av->value.sequence->data, |
207 | 111k | av->value.sequence->length, indent, 1) |
208 | 111k | > 0; |
209 | | |
210 | 73.4k | case V_ASN1_SET: |
211 | 73.4k | return ASN1_parse_dump(out, av->value.set->data, |
212 | 73.4k | av->value.set->length, indent, 1) |
213 | 73.4k | > 0; |
214 | | |
215 | | /* |
216 | | * UTCTime ::= [UNIVERSAL 23] IMPLICIT VisibleString |
217 | | * GeneralizedTime ::= [UNIVERSAL 24] IMPLICIT VisibleString |
218 | | * VisibleString is a superset for NumericString, so it will work for that. |
219 | | */ |
220 | 2.76k | case V_ASN1_VISIBLESTRING: |
221 | 3.85k | case V_ASN1_UTCTIME: |
222 | 9.96k | case V_ASN1_GENERALIZEDTIME: |
223 | 24.8k | case V_ASN1_NUMERICSTRING: |
224 | 24.8k | return BIO_printf(out, "%*s%.*s", indent, "", |
225 | 24.8k | av->value.visiblestring->length, |
226 | 24.8k | av->value.visiblestring->data) |
227 | 24.8k | >= 0; |
228 | | |
229 | 2.93k | case V_ASN1_PRINTABLESTRING: |
230 | 2.93k | return BIO_printf(out, "%*s%.*s", indent, "", |
231 | 2.93k | av->value.printablestring->length, |
232 | 2.93k | av->value.printablestring->data) |
233 | 2.93k | >= 0; |
234 | | |
235 | 17.4k | case V_ASN1_T61STRING: |
236 | 17.4k | return BIO_printf(out, "%*s%.*s", indent, "", |
237 | 17.4k | av->value.t61string->length, |
238 | 17.4k | av->value.t61string->data) |
239 | 17.4k | >= 0; |
240 | | |
241 | 2.88k | case V_ASN1_IA5STRING: |
242 | 2.88k | return BIO_printf(out, "%*s%.*s", indent, "", |
243 | 2.88k | av->value.ia5string->length, |
244 | 2.88k | av->value.ia5string->data) |
245 | 2.88k | >= 0; |
246 | | |
247 | | /* UniversalString would go here. */ |
248 | | /* CHARACTER STRING would go here. */ |
249 | | /* BMPString would go here. */ |
250 | | /* DATE would go here. */ |
251 | | /* TIME-OF-DAY would go here. */ |
252 | | /* DATE-TIME would go here. */ |
253 | | /* DURATION would go here. */ |
254 | | /* OID-IRI would go here. */ |
255 | | /* RELATIVE-OID-IRI would go here. */ |
256 | | |
257 | | /* Would it be appropriate to just hexdump? */ |
258 | 124k | default: |
259 | 124k | return BIO_printf(out, |
260 | 124k | "%*s<Unsupported tag %d>", |
261 | 124k | indent, |
262 | 124k | "", |
263 | 124k | av->type) |
264 | 124k | >= 0; |
265 | 470k | } |
266 | 470k | } |