]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/t_req.c
constify *_dup() and *i2d_*() and related functions as far as possible, introducing...
[thirdparty/openssl.git] / crypto / x509 / t_req.c
CommitLineData
b1322259 1/*
3c7d0945 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
3e4b43b9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/buffer.h>
13#include <openssl/bn.h>
14#include <openssl/objects.h>
15#include <openssl/x509.h>
87c49f62 16#include <openssl/x509v3.h>
3c27208f
RS
17#include <openssl/rsa.h>
18#include <openssl/dsa.h>
d02b48c6 19
4b618848 20#ifndef OPENSSL_NO_STDIO
6b691a5c 21int X509_REQ_print_fp(FILE *fp, X509_REQ *x)
0f113f3e
MC
22{
23 BIO *b;
24 int ret;
d02b48c6 25
0f113f3e
MC
26 if ((b = BIO_new(BIO_s_file())) == NULL) {
27 X509err(X509_F_X509_REQ_PRINT_FP, ERR_R_BUF_LIB);
26a7d938 28 return 0;
0f113f3e
MC
29 }
30 BIO_set_fp(b, fp, BIO_NOCLOSE);
31 ret = X509_REQ_print(b, x);
32 BIO_free(b);
26a7d938 33 return ret;
0f113f3e 34}
d02b48c6
RE
35#endif
36
0f113f3e
MC
37int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags,
38 unsigned long cflag)
39{
40 long l;
41 int i;
0f113f3e 42 EVP_PKEY *pkey;
0f113f3e
MC
43 STACK_OF(X509_EXTENSION) *exts;
44 char mlch = ' ';
45 int nmindent = 0;
fc85ac20 46
0f113f3e
MC
47 if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
48 mlch = '\n';
49 nmindent = 12;
50 }
fc85ac20 51
0f113f3e
MC
52 if (nmflags == X509_FLAG_COMPAT)
53 nmindent = 16;
d02b48c6 54
0f113f3e
MC
55 if (!(cflag & X509_FLAG_NO_HEADER)) {
56 if (BIO_write(bp, "Certificate Request:\n", 21) <= 0)
57 goto err;
58 if (BIO_write(bp, " Data:\n", 10) <= 0)
59 goto err;
60 }
61 if (!(cflag & X509_FLAG_NO_VERSION)) {
62 l = X509_REQ_get_version(x);
c2ce477f
KR
63 if (l >= 0 && l <= 2) {
64 if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0)
65 goto err;
66 } else {
67 if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
68 goto err;
69 }
0f113f3e
MC
70 }
71 if (!(cflag & X509_FLAG_NO_SUBJECT)) {
72 if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
73 goto err;
7880e143
DSH
74 if (X509_NAME_print_ex(bp, X509_REQ_get_subject_name(x),
75 nmindent, nmflags) < 0)
0f113f3e
MC
76 goto err;
77 if (BIO_write(bp, "\n", 1) <= 0)
78 goto err;
79 }
80 if (!(cflag & X509_FLAG_NO_PUBKEY)) {
1f143e08
DSH
81 X509_PUBKEY *xpkey;
82 ASN1_OBJECT *koid;
0f113f3e
MC
83 if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
84 goto err;
85 if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
86 goto err;
1f143e08
DSH
87 xpkey = X509_REQ_get_X509_PUBKEY(x);
88 X509_PUBKEY_get0_param(&koid, NULL, NULL, NULL, xpkey);
89 if (i2a_ASN1_OBJECT(bp, koid) <= 0)
0f113f3e
MC
90 goto err;
91 if (BIO_puts(bp, "\n") <= 0)
92 goto err;
fc85ac20 93
1c72f70d 94 pkey = X509_REQ_get0_pubkey(x);
0f113f3e 95 if (pkey == NULL) {
ae880ae6
BE
96 if (BIO_printf(bp, "%12sUnable to load Public Key\n", "") <= 0)
97 goto err;
0f113f3e
MC
98 ERR_print_errors(bp);
99 } else {
ae880ae6
BE
100 if (EVP_PKEY_print_public(bp, pkey, 16, NULL) <= 0)
101 goto err;
0f113f3e
MC
102 }
103 }
d02b48c6 104
0f113f3e
MC
105 if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
106 /* may not be */
107 if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
108 goto err;
fc85ac20 109
7880e143 110 if (X509_REQ_get_attr_count(x) == 0) {
0f113f3e
MC
111 if (BIO_printf(bp, "%12sa0:00\n", "") <= 0)
112 goto err;
113 } else {
7880e143 114 for (i = 0; i < X509_REQ_get_attr_count(x); i++) {
0f113f3e
MC
115 ASN1_TYPE *at;
116 X509_ATTRIBUTE *a;
117 ASN1_BIT_STRING *bs = NULL;
9b0a4531 118 ASN1_OBJECT *aobj;
0f113f3e 119 int j, type = 0, count = 1, ii = 0;
fc85ac20 120
7880e143 121 a = X509_REQ_get_attr(x, i);
9b0a4531
DSH
122 aobj = X509_ATTRIBUTE_get0_object(a);
123 if (X509_REQ_extension_nid(OBJ_obj2nid(aobj)))
0f113f3e
MC
124 continue;
125 if (BIO_printf(bp, "%12s", "") <= 0)
126 goto err;
9b0a4531
DSH
127 if ((j = i2a_ASN1_OBJECT(bp, aobj)) > 0) {
128 ii = 0;
129 count = X509_ATTRIBUTE_count(a);
0f113f3e 130 get_next:
9b0a4531
DSH
131 at = X509_ATTRIBUTE_get0_type(a, ii);
132 type = at->type;
133 bs = at->value.asn1_string;
0f113f3e
MC
134 }
135 for (j = 25 - j; j > 0; j--)
136 if (BIO_write(bp, " ", 1) != 1)
137 goto err;
138 if (BIO_puts(bp, ":") <= 0)
139 goto err;
7838b413
RS
140 switch (type) {
141 case V_ASN1_PRINTABLESTRING:
142 case V_ASN1_T61STRING:
40cea0a4 143 case V_ASN1_NUMERICSTRING:
7838b413
RS
144 case V_ASN1_UTF8STRING:
145 case V_ASN1_IA5STRING:
0f113f3e 146 if (BIO_write(bp, (char *)bs->data, bs->length)
7838b413 147 != bs->length)
0f113f3e 148 goto err;
ae880ae6
BE
149 if (BIO_puts(bp, "\n") <= 0)
150 goto err;
7838b413
RS
151 break;
152 default:
ae880ae6
BE
153 if (BIO_puts(bp, "unable to print attribute\n") <= 0)
154 goto err;
7838b413 155 break;
0f113f3e
MC
156 }
157 if (++ii < count)
158 goto get_next;
159 }
160 }
161 }
162 if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
163 exts = X509_REQ_get_extensions(x);
164 if (exts) {
ae880ae6
BE
165 if (BIO_printf(bp, "%8sRequested Extensions:\n", "") <= 0)
166 goto err;
0f113f3e
MC
167 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
168 ASN1_OBJECT *obj;
169 X509_EXTENSION *ex;
fbff6716 170 int critical;
0f113f3e
MC
171 ex = sk_X509_EXTENSION_value(exts, i);
172 if (BIO_printf(bp, "%12s", "") <= 0)
173 goto err;
174 obj = X509_EXTENSION_get_object(ex);
ae880ae6
BE
175 if (i2a_ASN1_OBJECT(bp, obj) <= 0)
176 goto err;
fbff6716
DM
177 critical = X509_EXTENSION_get_critical(ex);
178 if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
0f113f3e
MC
179 goto err;
180 if (!X509V3_EXT_print(bp, ex, cflag, 16)) {
ae880ae6
BE
181 if (BIO_printf(bp, "%16s", "") <= 0
182 || ASN1_STRING_print(bp,
183 X509_EXTENSION_get_data(ex)) <= 0)
184 goto err;
0f113f3e
MC
185 }
186 if (BIO_write(bp, "\n", 1) <= 0)
187 goto err;
188 }
189 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
190 }
191 }
d02b48c6 192
0f113f3e 193 if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
11222483
DSH
194 const X509_ALGOR *sig_alg;
195 const ASN1_BIT_STRING *sig;
196 X509_REQ_get0_signature(x, &sig, &sig_alg);
7880e143 197 if (!X509_signature_print(bp, sig_alg, sig))
0f113f3e
MC
198 goto err;
199 }
d02b48c6 200
208fb891 201 return 1;
0f113f3e
MC
202 err:
203 X509err(X509_F_X509_REQ_PRINT_EX, ERR_R_BUF_LIB);
26a7d938 204 return 0;
0f113f3e 205}
fc85ac20
DSH
206
207int X509_REQ_print(BIO *bp, X509_REQ *x)
0f113f3e
MC
208{
209 return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
210}