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