/src/openssl/crypto/x509/t_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 <stdio.h>  | 
11  |  | #include "internal/cryptlib.h"  | 
12  |  | #include <openssl/buffer.h>  | 
13  |  | #include <openssl/bn.h>  | 
14  |  | #include <openssl/objects.h>  | 
15  |  | #include <openssl/x509_acert.h>  | 
16  |  |  | 
17  |  | static int print_attribute(BIO *bp, X509_ATTRIBUTE *a)  | 
18  | 52.3k  | { | 
19  | 52.3k  |     ASN1_OBJECT *aobj;  | 
20  | 52.3k  |     int i, j, count;  | 
21  | 52.3k  |     int ret = 0;  | 
22  |  |  | 
23  | 52.3k  |     aobj = X509_ATTRIBUTE_get0_object(a);  | 
24  | 52.3k  |     if (BIO_printf(bp, "%12s", "") <= 0)  | 
25  | 0  |         goto err;  | 
26  |  |  | 
27  | 52.3k  |     if ((j = i2a_ASN1_OBJECT(bp, aobj)) <= 0)  | 
28  | 0  |         goto err;  | 
29  |  |  | 
30  | 52.3k  |     count = X509_ATTRIBUTE_count(a);  | 
31  | 52.3k  |     if (count == 0) { | 
32  | 14  |         ERR_raise(ERR_LIB_X509, X509_R_INVALID_ATTRIBUTES);  | 
33  | 14  |         goto err;  | 
34  | 14  |     }  | 
35  |  |  | 
36  | 52.3k  |     if (j < 25 && (BIO_printf(bp, "%*s", 25 - j, " ") <= 0))  | 
37  | 0  |         goto err;  | 
38  |  |  | 
39  | 52.3k  |     if (BIO_puts(bp, ":") <= 0)  | 
40  | 0  |         goto err;  | 
41  |  |  | 
42  | 2.73M  |     for (i = 0; i < count; i++) { | 
43  | 2.67M  |         ASN1_TYPE *at;  | 
44  | 2.67M  |         int type;  | 
45  | 2.67M  |         ASN1_BIT_STRING *bs;  | 
46  |  |  | 
47  | 2.67M  |         at = X509_ATTRIBUTE_get0_type(a, i);  | 
48  | 2.67M  |         type = at->type;  | 
49  |  |  | 
50  | 2.67M  |         switch (type) { | 
51  | 313  |         case V_ASN1_PRINTABLESTRING:  | 
52  | 1.00k  |         case V_ASN1_T61STRING:  | 
53  | 3.63k  |         case V_ASN1_NUMERICSTRING:  | 
54  | 4.43k  |         case V_ASN1_UTF8STRING:  | 
55  | 4.67k  |         case V_ASN1_IA5STRING:  | 
56  | 4.67k  |             bs = at->value.asn1_string;  | 
57  | 4.67k  |             if (BIO_write(bp, (char *)bs->data, bs->length) != bs->length)  | 
58  | 0  |                 goto err;  | 
59  | 4.67k  |             if (BIO_puts(bp, "\n") <= 0)  | 
60  | 0  |                 goto err;  | 
61  | 4.67k  |             break;  | 
62  | 39.9k  |         case V_ASN1_SEQUENCE:  | 
63  | 39.9k  |             if (BIO_puts(bp, "\n") <= 0)  | 
64  | 0  |                 goto err;  | 
65  | 39.9k  |             ASN1_parse_dump(bp, at->value.sequence->data,  | 
66  | 39.9k  |                             at->value.sequence->length, i, 1);  | 
67  | 39.9k  |             break;  | 
68  | 2.63M  |         default:  | 
69  | 2.63M  |             if (BIO_printf(bp, "unable to print attribute of type 0x%X\n",  | 
70  | 2.63M  |                            type) < 0)  | 
71  | 0  |                 goto err;  | 
72  | 2.63M  |             break;  | 
73  | 2.67M  |         }  | 
74  | 2.67M  |     }  | 
75  | 52.3k  |     ret = 1;  | 
76  | 52.3k  | err:  | 
77  | 52.3k  |     return ret;  | 
78  | 52.3k  | }  | 
79  |  |  | 
80  |  | int X509_ACERT_print_ex(BIO *bp, X509_ACERT *x, unsigned long nmflags,  | 
81  |  |                         unsigned long cflag)  | 
82  | 4.03k  | { | 
83  | 4.03k  |     int i;  | 
84  | 4.03k  |     char mlch = ' ';  | 
85  |  |  | 
86  | 4.03k  |     if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { | 
87  | 0  |         mlch = '\n';  | 
88  | 0  |     }  | 
89  |  |  | 
90  | 4.03k  |     if ((cflag & X509_FLAG_NO_HEADER) == 0) { | 
91  | 4.03k  |         if (BIO_printf(bp, "Attribute Certificate:\n") <= 0)  | 
92  | 0  |             goto err;  | 
93  | 4.03k  |         if (BIO_printf(bp, "%4sData:\n", "") <= 0)  | 
94  | 0  |             goto err;  | 
95  | 4.03k  |     }  | 
96  |  |  | 
97  | 4.03k  |     if ((cflag & X509_FLAG_NO_VERSION) == 0) { | 
98  | 4.03k  |         long l;  | 
99  |  |  | 
100  | 4.03k  |         l = X509_ACERT_get_version(x);  | 
101  | 4.03k  |         if (l == X509_ACERT_VERSION_2) { | 
102  | 58  |             if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1,  | 
103  | 58  |                            (unsigned long)l) <= 0)  | 
104  | 0  |                 goto err;  | 
105  | 3.98k  |         } else { | 
106  | 3.98k  |             if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)  | 
107  | 0  |                 goto err;  | 
108  | 3.98k  |         }  | 
109  | 4.03k  |     }  | 
110  |  |  | 
111  | 4.03k  |     if ((cflag & X509_FLAG_NO_SERIAL) == 0) { | 
112  | 4.03k  |         const ASN1_INTEGER *serial;  | 
113  |  |  | 
114  | 4.03k  |         serial = X509_ACERT_get0_serialNumber(x);  | 
115  |  |  | 
116  | 4.03k  |         if (BIO_printf(bp, "%8sSerial Number: ", "") <= 0)  | 
117  | 0  |             goto err;  | 
118  |  |  | 
119  | 4.03k  |         if (i2a_ASN1_INTEGER(bp, serial) <= 0)  | 
120  | 0  |             goto err;  | 
121  |  |  | 
122  | 4.03k  |         if (BIO_write(bp, "\n", 1) <= 0)  | 
123  | 0  |             goto err;  | 
124  | 4.03k  |     }  | 
125  |  |  | 
126  | 4.03k  |     if ((cflag & X509_FLAG_NO_SUBJECT) == 0) { | 
127  | 4.03k  |         const GENERAL_NAMES *holderEntities;  | 
128  | 4.03k  |         const OSSL_ISSUER_SERIAL *holder_bcid;  | 
129  | 4.03k  |         const X509_NAME *holderIssuer = NULL;  | 
130  |  |  | 
131  | 4.03k  |         if (BIO_printf(bp, "%8sHolder:\n", "") <= 0)  | 
132  | 0  |             goto err;  | 
133  |  |  | 
134  | 4.03k  |         holderEntities = X509_ACERT_get0_holder_entityName(x);  | 
135  | 4.03k  |         if (holderEntities != NULL) { | 
136  | 1.84k  |             for (i = 0; i < sk_GENERAL_NAME_num(holderEntities); i++) { | 
137  | 1.06k  |                 GENERAL_NAME *entity;  | 
138  |  |  | 
139  | 1.06k  |                 entity = sk_GENERAL_NAME_value(holderEntities, i);  | 
140  |  |  | 
141  | 1.06k  |                 if (BIO_printf(bp, "%12sName:%c", "", mlch) <= 0)  | 
142  | 0  |                     goto err;  | 
143  | 1.06k  |                 if (GENERAL_NAME_print(bp, entity) <= 0)  | 
144  | 0  |                     goto err;  | 
145  | 1.06k  |                 if (BIO_write(bp, "\n", 1) <= 0)  | 
146  | 0  |                     goto err;  | 
147  | 1.06k  |             }  | 
148  | 777  |         }  | 
149  |  |  | 
150  | 4.03k  |         if ((holder_bcid = X509_ACERT_get0_holder_baseCertId(x)) != NULL)  | 
151  | 776  |             holderIssuer = OSSL_ISSUER_SERIAL_get0_issuer(holder_bcid);  | 
152  |  |  | 
153  | 4.03k  |         if (holderIssuer != NULL) { | 
154  | 665  |             const ASN1_INTEGER *holder_serial;  | 
155  | 665  |             const ASN1_BIT_STRING *iuid;  | 
156  |  |  | 
157  | 665  |             if (BIO_printf(bp, "%12sIssuer:%c", "", mlch) <= 0)  | 
158  | 0  |                 goto err;  | 
159  |  |  | 
160  | 665  |             if (X509_NAME_print_ex(bp, holderIssuer, 0, nmflags) <= 0)  | 
161  | 0  |                 goto err;  | 
162  |  |  | 
163  | 665  |             if (BIO_write(bp, "\n", 1) <= 0)  | 
164  | 0  |                 goto err;  | 
165  |  |  | 
166  | 665  |             if (BIO_printf(bp, "%12sSerial: ", "") <= 0)  | 
167  | 0  |                 goto err;  | 
168  |  |  | 
169  | 665  |             holder_serial = OSSL_ISSUER_SERIAL_get0_serial(holder_bcid);  | 
170  |  |  | 
171  | 665  |             if (i2a_ASN1_INTEGER(bp, holder_serial) <= 0)  | 
172  | 0  |                 goto err;  | 
173  |  |  | 
174  | 665  |             iuid = OSSL_ISSUER_SERIAL_get0_issuerUID(holder_bcid);  | 
175  | 665  |             if (iuid != NULL) { | 
176  | 0  |                 if (BIO_printf(bp, "%12sIssuer UID: ", "") <= 0)  | 
177  | 0  |                     goto err;  | 
178  | 0  |                 if (X509_signature_dump(bp, iuid, 24) <= 0)  | 
179  | 0  |                     goto err;  | 
180  | 0  |             }  | 
181  | 665  |             if (BIO_write(bp, "\n", 1) <= 0)  | 
182  | 0  |                 goto err;  | 
183  | 665  |         }  | 
184  | 4.03k  |     }  | 
185  |  |  | 
186  | 4.03k  |     if ((cflag & X509_FLAG_NO_ISSUER) == 0) { | 
187  | 4.03k  |         const X509_NAME *issuer;  | 
188  |  |  | 
189  | 4.03k  |         if (BIO_printf(bp, "%8sIssuer:%c", "", mlch) <= 0)  | 
190  | 0  |             goto err;  | 
191  | 4.03k  |         issuer = X509_ACERT_get0_issuerName(x);  | 
192  | 4.03k  |         if (issuer) { | 
193  | 442  |             if (X509_NAME_print_ex(bp, issuer, 0, nmflags) < 0)  | 
194  | 0  |                 goto err;  | 
195  | 3.59k  |         } else { | 
196  | 3.59k  |             if (BIO_printf(bp, "Unsupported Issuer Type") <= 0)  | 
197  | 0  |                 goto err;  | 
198  | 3.59k  |         }  | 
199  | 4.03k  |         if (BIO_write(bp, "\n", 1) <= 0)  | 
200  | 0  |             goto err;  | 
201  | 4.03k  |     }  | 
202  |  |  | 
203  | 4.03k  |     if ((cflag & X509_FLAG_NO_VALIDITY) == 0) { | 
204  | 4.03k  |         if (BIO_printf(bp, "%8sValidity\n", "") <= 0)  | 
205  | 0  |             goto err;  | 
206  | 4.03k  |         if (BIO_printf(bp, "%12sNot Before: ", "") <= 0)  | 
207  | 0  |             goto err;  | 
208  | 4.03k  |         if (ASN1_GENERALIZEDTIME_print(bp, X509_ACERT_get0_notBefore(x)) == 0)  | 
209  | 1.09k  |             goto err;  | 
210  | 2.94k  |         if (BIO_printf(bp, "\n%12sNot After : ", "") <= 0)  | 
211  | 0  |             goto err;  | 
212  | 2.94k  |         if (ASN1_GENERALIZEDTIME_print(bp, X509_ACERT_get0_notAfter(x)) == 0)  | 
213  | 60  |             goto err;  | 
214  | 2.88k  |         if (BIO_write(bp, "\n", 1) <= 0)  | 
215  | 0  |             goto err;  | 
216  | 2.88k  |     }  | 
217  |  |  | 
218  | 2.88k  |     if ((cflag & X509_FLAG_NO_ATTRIBUTES) == 0) { | 
219  | 2.88k  |         if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)  | 
220  | 0  |             goto err;  | 
221  |  |  | 
222  | 2.88k  |         if (X509_ACERT_get_attr_count(x) == 0) { | 
223  | 1.04k  |             if (BIO_printf(bp, "%12s(none)\n", "") <= 0)  | 
224  | 0  |                 goto err;  | 
225  | 1.84k  |         } else { | 
226  | 54.1k  |             for (i = 0; i < X509_ACERT_get_attr_count(x); i++) { | 
227  | 52.3k  |                 if (print_attribute(bp, X509_ACERT_get_attr(x, i)) == 0)  | 
228  | 14  |                     goto err;  | 
229  | 52.3k  |             }  | 
230  | 1.84k  |         }  | 
231  | 2.88k  |     }  | 
232  |  |  | 
233  | 2.87k  |     if ((cflag & X509_FLAG_NO_EXTENSIONS) == 0) { | 
234  | 2.87k  |         const STACK_OF(X509_EXTENSION) *exts;  | 
235  |  |  | 
236  | 2.87k  |         exts = X509_ACERT_get0_extensions(x);  | 
237  | 2.87k  |         if (exts != NULL) { | 
238  | 753  |             if (BIO_printf(bp, "%8sExtensions:\n", "") <= 0)  | 
239  | 0  |                 goto err;  | 
240  | 3.41k  |             for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { | 
241  | 2.66k  |                 ASN1_OBJECT *obj;  | 
242  | 2.66k  |                 X509_EXTENSION *ex;  | 
243  | 2.66k  |                 int critical;  | 
244  |  |  | 
245  | 2.66k  |                 ex = sk_X509_EXTENSION_value(exts, i);  | 
246  | 2.66k  |                 if (BIO_printf(bp, "%12s", "") <= 0)  | 
247  | 0  |                     goto err;  | 
248  | 2.66k  |                 obj = X509_EXTENSION_get_object(ex);  | 
249  | 2.66k  |                 if (i2a_ASN1_OBJECT(bp, obj) <= 0)  | 
250  | 0  |                     goto err;  | 
251  | 2.66k  |                 critical = X509_EXTENSION_get_critical(ex);  | 
252  | 2.66k  |                 if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)  | 
253  | 0  |                     goto err;  | 
254  | 2.66k  |                 if (X509V3_EXT_print(bp, ex, cflag, 20) <= 0) { | 
255  | 2.09k  |                     if (BIO_printf(bp, "%16s", "") <= 0)  | 
256  | 0  |                         goto err;  | 
257  | 2.09k  |                     if (ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex)) <= 0)  | 
258  | 0  |                         goto err;  | 
259  | 2.09k  |                 }  | 
260  | 2.66k  |                 if (BIO_write(bp, "\n", 1) <= 0)  | 
261  | 0  |                     goto err;  | 
262  | 2.66k  |             }  | 
263  | 753  |         }  | 
264  | 2.87k  |     }  | 
265  |  |  | 
266  | 2.87k  |     if ((cflag & X509_FLAG_NO_SIGDUMP) == 0) { | 
267  | 2.87k  |         const X509_ALGOR *sig_alg;  | 
268  | 2.87k  |         const ASN1_BIT_STRING *sig;  | 
269  |  |  | 
270  | 2.87k  |         X509_ACERT_get0_signature(x, &sig, &sig_alg);  | 
271  | 2.87k  |         if (X509_signature_print(bp, sig_alg, sig) <= 0)  | 
272  | 0  |             return 0;  | 
273  | 2.87k  |     }  | 
274  |  |  | 
275  | 2.87k  |     return 1;  | 
276  |  |  | 
277  | 1.16k  | err:  | 
278  | 1.16k  |     ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);  | 
279  | 1.16k  |     return 0;  | 
280  | 2.87k  | }  | 
281  |  |  | 
282  |  | int X509_ACERT_print(BIO *bp, X509_ACERT *x)  | 
283  | 4.03k  | { | 
284  | 4.03k  |     return X509_ACERT_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);  | 
285  | 4.03k  | }  |