Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/x509/t_req.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <assert.h>
5 #include <stdio.h>
6
7 #include <openssl/bn.h>
8 #include <openssl/buffer.h>
9 #include <openssl/err.h>
10 #include <openssl/objects.h>
11 #include <openssl/x509.h>
12
13 #include "internal.h"
14
15
16 int X509_REQ_print_fp(FILE *fp, X509_REQ *x) {
17 BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE);
18 if (bio == NULL) {
19 OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
20 return 0;
21 }
22 int ret = X509_REQ_print(bio, x);
23 BIO_free(bio);
24 return ret;
25 }
26
27 int X509_REQ_print_ex(BIO *bio, X509_REQ *x, unsigned long nmflags,
28 unsigned long cflag) {
29 long l;
30 STACK_OF(X509_ATTRIBUTE) *sk;
31 char mlch = ' ';
32
33 int nmindent = 0;
34
35 if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
36 mlch = '\n';
37 nmindent = 12;
38 }
39
40 if (nmflags == X509_FLAG_COMPAT) {
41 nmindent = 16;
42 }
43
44 X509_REQ_INFO *ri = x->req_info;
45 if (!(cflag & X509_FLAG_NO_HEADER)) {
46 if (BIO_write(bio, "Certificate Request:\n", 21) <= 0 ||
47 BIO_write(bio, " Data:\n", 10) <= 0) {
48 goto err;
49 }
50 }
51 if (!(cflag & X509_FLAG_NO_VERSION)) {
52 l = X509_REQ_get_version(x);
53 // Only zero, |X509_REQ_VERSION_1|, is valid but our parser accepts some
54 // invalid values for compatibility.
55 assert(0 <= l && l <= 2);
56 if (BIO_printf(bio, "%8sVersion: %ld (0x%lx)\n", "", l + 1,
57 (unsigned long)l) <= 0) {
58 goto err;
59 }
60 }
61 if (!(cflag & X509_FLAG_NO_SUBJECT)) {
62 if (BIO_printf(bio, " Subject:%c", mlch) <= 0 ||
63 X509_NAME_print_ex(bio, ri->subject, nmindent, nmflags) < 0 ||
64 BIO_write(bio, "\n", 1) <= 0) {
65 goto err;
66 }
67 }
68 if (!(cflag & X509_FLAG_NO_PUBKEY)) {
69 if (BIO_write(bio, " Subject Public Key Info:\n", 33) <= 0 ||
70 BIO_printf(bio, "%12sPublic Key Algorithm: ", "") <= 0 ||
71 i2a_ASN1_OBJECT(bio, ri->pubkey->algor->algorithm) <= 0 ||
72 BIO_puts(bio, "\n") <= 0) {
73 goto err;
74 }
75
76 const EVP_PKEY *pkey = X509_REQ_get0_pubkey(x);
77 if (pkey == NULL) {
78 BIO_printf(bio, "%12sUnable to load Public Key\n", "");
79 ERR_print_errors(bio);
80 } else {
81 EVP_PKEY_print_public(bio, pkey, 16, NULL);
82 }
83 }
84
85 if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
86 if (BIO_printf(bio, "%8sAttributes:\n", "") <= 0) {
87 goto err;
88 }
89
90 sk = x->req_info->attributes;
91 if (sk_X509_ATTRIBUTE_num(sk) == 0) {
92 if (BIO_printf(bio, "%12sa0:00\n", "") <= 0) {
93 goto err;
94 }
95 } else {
96 size_t i;
97 for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
98 X509_ATTRIBUTE *a = sk_X509_ATTRIBUTE_value(sk, i);
99 ASN1_OBJECT *aobj = X509_ATTRIBUTE_get0_object(a);
100
101 if (X509_REQ_extension_nid(OBJ_obj2nid(aobj))) {
102 continue;
103 }
104
105 if (BIO_printf(bio, "%12s", "") <= 0) {
106 goto err;
107 }
108
109 const int num_attrs = X509_ATTRIBUTE_count(a);
110 const int obj_str_len = i2a_ASN1_OBJECT(bio, aobj);
111 if (obj_str_len <= 0) {
112 if (BIO_puts(bio, "(Unable to print attribute ID.)\n") < 0) {
113 goto err;
114 } else {
115 continue;
116 }
117 }
118
119 int j;
120 for (j = 0; j < num_attrs; j++) {
121 const ASN1_TYPE *at = X509_ATTRIBUTE_get0_type(a, j);
122 const int type = at->type;
123 ASN1_BIT_STRING *bs = at->value.asn1_string;
124
125 int k;
126 for (k = 25 - obj_str_len; k > 0; k--) {
127 if (BIO_write(bio, " ", 1) != 1) {
128 goto err;
129 }
130 }
131
132 if (BIO_puts(bio, ":") <= 0) {
133 goto err;
134 }
135
136 if (type == V_ASN1_PRINTABLESTRING || type == V_ASN1_UTF8STRING ||
137 type == V_ASN1_IA5STRING || type == V_ASN1_T61STRING) {
138 if (BIO_write(bio, (char *)bs->data, bs->length) != bs->length) {
139 goto err;
140 }
141 BIO_puts(bio, "\n");
142 } else {
143 BIO_puts(bio, "unable to print attribute\n");
144 }
145 }
146 }
147 }
148 }
149
150 if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
151 STACK_OF(X509_EXTENSION) *exts = X509_REQ_get_extensions(x);
152 if (exts) {
153 BIO_printf(bio, "%8sRequested Extensions:\n", "");
154
155 for (size_t i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
156 const X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i);
157 if (BIO_printf(bio, "%12s", "") <= 0) {
158 goto err;
159 }
160 const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
161 i2a_ASN1_OBJECT(bio, obj);
162 const int is_critical = X509_EXTENSION_get_critical(ex);
163 if (BIO_printf(bio, ": %s\n", is_critical ? "critical" : "") <= 0) {
164 goto err;
165 }
166 if (!X509V3_EXT_print(bio, ex, cflag, 16)) {
167 BIO_printf(bio, "%16s", "");
168 ASN1_STRING_print(bio, X509_EXTENSION_get_data(ex));
169 }
170 if (BIO_write(bio, "\n", 1) <= 0) {
171 goto err;
172 }
173 }
174 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
175 }
176 }
177
178 if (!(cflag & X509_FLAG_NO_SIGDUMP) &&
179 !X509_signature_print(bio, x->sig_alg, x->signature)) {
180 goto err;
181 }
182
183 return 1;
184
185 err:
186 OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
187 return 0;
188 }
189
190 int X509_REQ_print(BIO *bio, X509_REQ *req) {
191 return X509_REQ_print_ex(bio, req, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
192 }