/src/openssl/crypto/x509/t_req.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2021 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.h> |
16 | | #include <openssl/x509v3.h> |
17 | | #include <openssl/rsa.h> |
18 | | #include <openssl/dsa.h> |
19 | | |
20 | | #ifndef OPENSSL_NO_STDIO |
21 | | int X509_REQ_print_fp(FILE *fp, X509_REQ *x) |
22 | 0 | { |
23 | 0 | BIO *b; |
24 | 0 | int ret; |
25 | |
|
26 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
27 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB); |
28 | 0 | return 0; |
29 | 0 | } |
30 | 0 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
31 | 0 | ret = X509_REQ_print(b, x); |
32 | 0 | BIO_free(b); |
33 | 0 | return ret; |
34 | 0 | } |
35 | | #endif |
36 | | |
37 | | int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags, |
38 | | unsigned long cflag) |
39 | 0 | { |
40 | 0 | long l; |
41 | 0 | int i; |
42 | 0 | EVP_PKEY *pkey; |
43 | 0 | STACK_OF(X509_EXTENSION) *exts; |
44 | 0 | char mlch = ' '; |
45 | 0 | int nmindent = 0, printok = 0; |
46 | |
|
47 | 0 | if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { |
48 | 0 | mlch = '\n'; |
49 | 0 | nmindent = 12; |
50 | 0 | } |
51 | |
|
52 | 0 | if (nmflags == XN_FLAG_COMPAT) |
53 | 0 | printok = 1; |
54 | |
|
55 | 0 | if (!(cflag & X509_FLAG_NO_HEADER)) { |
56 | 0 | if (BIO_write(bp, "Certificate Request:\n", 21) <= 0) |
57 | 0 | goto err; |
58 | 0 | if (BIO_write(bp, " Data:\n", 10) <= 0) |
59 | 0 | goto err; |
60 | 0 | } |
61 | 0 | if (!(cflag & X509_FLAG_NO_VERSION)) { |
62 | 0 | l = X509_REQ_get_version(x); |
63 | 0 | if (l == X509_REQ_VERSION_1) { |
64 | 0 | if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0) |
65 | 0 | goto err; |
66 | 0 | } else { |
67 | 0 | if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0) |
68 | 0 | goto err; |
69 | 0 | } |
70 | 0 | } |
71 | 0 | if (!(cflag & X509_FLAG_NO_SUBJECT)) { |
72 | 0 | if (BIO_printf(bp, " Subject:%c", mlch) <= 0) |
73 | 0 | goto err; |
74 | 0 | if (X509_NAME_print_ex(bp, X509_REQ_get_subject_name(x), |
75 | 0 | nmindent, nmflags) < printok) |
76 | 0 | goto err; |
77 | 0 | if (BIO_write(bp, "\n", 1) <= 0) |
78 | 0 | goto err; |
79 | 0 | } |
80 | 0 | if (!(cflag & X509_FLAG_NO_PUBKEY)) { |
81 | 0 | X509_PUBKEY *xpkey; |
82 | 0 | ASN1_OBJECT *koid; |
83 | 0 | if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0) |
84 | 0 | goto err; |
85 | 0 | if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0) |
86 | 0 | goto err; |
87 | 0 | xpkey = X509_REQ_get_X509_PUBKEY(x); |
88 | 0 | X509_PUBKEY_get0_param(&koid, NULL, NULL, NULL, xpkey); |
89 | 0 | if (i2a_ASN1_OBJECT(bp, koid) <= 0) |
90 | 0 | goto err; |
91 | 0 | if (BIO_puts(bp, "\n") <= 0) |
92 | 0 | goto err; |
93 | | |
94 | 0 | pkey = X509_REQ_get0_pubkey(x); |
95 | 0 | if (pkey == NULL) { |
96 | 0 | if (BIO_printf(bp, "%12sUnable to load Public Key\n", "") <= 0) |
97 | 0 | goto err; |
98 | 0 | ERR_print_errors(bp); |
99 | 0 | } else { |
100 | 0 | if (EVP_PKEY_print_public(bp, pkey, 16, NULL) <= 0) |
101 | 0 | goto err; |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | 0 | if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) { |
106 | | /* may not be */ |
107 | 0 | if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0) |
108 | 0 | goto err; |
109 | | |
110 | 0 | if (X509_REQ_get_attr_count(x) == 0) { |
111 | 0 | if (BIO_printf(bp, "%12s(none)\n", "") <= 0) |
112 | 0 | goto err; |
113 | 0 | } else { |
114 | 0 | for (i = 0; i < X509_REQ_get_attr_count(x); i++) { |
115 | 0 | ASN1_TYPE *at; |
116 | 0 | X509_ATTRIBUTE *a; |
117 | 0 | ASN1_BIT_STRING *bs = NULL; |
118 | 0 | ASN1_OBJECT *aobj; |
119 | 0 | int j, type = 0, count = 1, ii = 0; |
120 | |
|
121 | 0 | a = X509_REQ_get_attr(x, i); |
122 | 0 | aobj = X509_ATTRIBUTE_get0_object(a); |
123 | 0 | if (X509_REQ_extension_nid(OBJ_obj2nid(aobj))) |
124 | 0 | continue; |
125 | 0 | if (BIO_printf(bp, "%12s", "") <= 0) |
126 | 0 | goto err; |
127 | 0 | if ((j = i2a_ASN1_OBJECT(bp, aobj)) > 0) { |
128 | 0 | ii = 0; |
129 | 0 | count = X509_ATTRIBUTE_count(a); |
130 | 0 | if (count == 0) { |
131 | 0 | ERR_raise(ERR_LIB_X509, X509_R_INVALID_ATTRIBUTES); |
132 | 0 | return 0; |
133 | 0 | } |
134 | 0 | get_next: |
135 | 0 | at = X509_ATTRIBUTE_get0_type(a, ii); |
136 | 0 | type = at->type; |
137 | 0 | bs = at->value.asn1_string; |
138 | 0 | } |
139 | 0 | for (j = 25 - j; j > 0; j--) |
140 | 0 | if (BIO_write(bp, " ", 1) != 1) |
141 | 0 | goto err; |
142 | 0 | if (BIO_puts(bp, ":") <= 0) |
143 | 0 | goto err; |
144 | 0 | switch (type) { |
145 | 0 | case V_ASN1_PRINTABLESTRING: |
146 | 0 | case V_ASN1_T61STRING: |
147 | 0 | case V_ASN1_NUMERICSTRING: |
148 | 0 | case V_ASN1_UTF8STRING: |
149 | 0 | case V_ASN1_IA5STRING: |
150 | 0 | if (BIO_write(bp, (char *)bs->data, bs->length) |
151 | 0 | != bs->length) |
152 | 0 | goto err; |
153 | 0 | if (BIO_puts(bp, "\n") <= 0) |
154 | 0 | goto err; |
155 | 0 | break; |
156 | 0 | default: |
157 | 0 | if (BIO_puts(bp, "unable to print attribute\n") <= 0) |
158 | 0 | goto err; |
159 | 0 | break; |
160 | 0 | } |
161 | 0 | if (++ii < count) |
162 | 0 | goto get_next; |
163 | 0 | } |
164 | 0 | } |
165 | 0 | } |
166 | 0 | if (!(cflag & X509_FLAG_NO_EXTENSIONS)) { |
167 | 0 | exts = X509_REQ_get_extensions(x); |
168 | 0 | if (exts) { |
169 | 0 | if (BIO_printf(bp, "%12sRequested Extensions:\n", "") <= 0) |
170 | 0 | goto err; |
171 | 0 | for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { |
172 | 0 | ASN1_OBJECT *obj; |
173 | 0 | X509_EXTENSION *ex; |
174 | 0 | int critical; |
175 | 0 | ex = sk_X509_EXTENSION_value(exts, i); |
176 | 0 | if (BIO_printf(bp, "%16s", "") <= 0) |
177 | 0 | goto err; |
178 | 0 | obj = X509_EXTENSION_get_object(ex); |
179 | 0 | if (i2a_ASN1_OBJECT(bp, obj) <= 0) |
180 | 0 | goto err; |
181 | 0 | critical = X509_EXTENSION_get_critical(ex); |
182 | 0 | if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0) |
183 | 0 | goto err; |
184 | 0 | if (!X509V3_EXT_print(bp, ex, cflag, 20)) { |
185 | 0 | if (BIO_printf(bp, "%20s", "") <= 0 |
186 | 0 | || ASN1_STRING_print(bp, |
187 | 0 | X509_EXTENSION_get_data(ex)) <= 0) |
188 | 0 | goto err; |
189 | 0 | } |
190 | 0 | if (BIO_write(bp, "\n", 1) <= 0) |
191 | 0 | goto err; |
192 | 0 | } |
193 | 0 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | 0 | if (!(cflag & X509_FLAG_NO_SIGDUMP)) { |
198 | 0 | const X509_ALGOR *sig_alg; |
199 | 0 | const ASN1_BIT_STRING *sig; |
200 | 0 | X509_REQ_get0_signature(x, &sig, &sig_alg); |
201 | 0 | if (!X509_signature_print(bp, sig_alg, sig)) |
202 | 0 | goto err; |
203 | 0 | } |
204 | | |
205 | 0 | return 1; |
206 | 0 | err: |
207 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB); |
208 | 0 | return 0; |
209 | 0 | } |
210 | | |
211 | | int X509_REQ_print(BIO *bp, X509_REQ *x) |
212 | 0 | { |
213 | 0 | return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT); |
214 | 0 | } |