2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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>
20 DEFINE_STACK_OF(X509_EXTENSION)
22 #ifndef OPENSSL_NO_STDIO
23 int X509_REQ_print_fp(FILE *fp, X509_REQ *x)
28 if ((b = BIO_new(BIO_s_file())) == NULL) {
29 X509err(X509_F_X509_REQ_PRINT_FP, ERR_R_BUF_LIB);
32 BIO_set_fp(b, fp, BIO_NOCLOSE);
33 ret = X509_REQ_print(b, x);
39 int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags,
45 STACK_OF(X509_EXTENSION) *exts;
49 if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
54 if (nmflags == X509_FLAG_COMPAT)
57 if (!(cflag & X509_FLAG_NO_HEADER)) {
58 if (BIO_write(bp, "Certificate Request:\n", 21) <= 0)
60 if (BIO_write(bp, " Data:\n", 10) <= 0)
63 if (!(cflag & X509_FLAG_NO_VERSION)) {
64 l = X509_REQ_get_version(x);
65 if (l >= 0 && l <= 2) {
66 if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0)
69 if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
73 if (!(cflag & X509_FLAG_NO_SUBJECT)) {
74 if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
76 if (X509_NAME_print_ex(bp, X509_REQ_get_subject_name(x),
77 nmindent, nmflags) < 0)
79 if (BIO_write(bp, "\n", 1) <= 0)
82 if (!(cflag & X509_FLAG_NO_PUBKEY)) {
85 if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
87 if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
89 xpkey = X509_REQ_get_X509_PUBKEY(x);
90 X509_PUBKEY_get0_param(&koid, NULL, NULL, NULL, xpkey);
91 if (i2a_ASN1_OBJECT(bp, koid) <= 0)
93 if (BIO_puts(bp, "\n") <= 0)
96 pkey = X509_REQ_get0_pubkey(x);
98 if (BIO_printf(bp, "%12sUnable to load Public Key\n", "") <= 0)
100 ERR_print_errors(bp);
102 if (EVP_PKEY_print_public(bp, pkey, 16, NULL) <= 0)
107 if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
109 if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
112 if (X509_REQ_get_attr_count(x) == 0) {
113 if (BIO_printf(bp, "%12sa0:00\n", "") <= 0)
116 for (i = 0; i < X509_REQ_get_attr_count(x); i++) {
119 ASN1_BIT_STRING *bs = NULL;
121 int j, type = 0, count = 1, ii = 0;
123 a = X509_REQ_get_attr(x, i);
124 aobj = X509_ATTRIBUTE_get0_object(a);
125 if (X509_REQ_extension_nid(OBJ_obj2nid(aobj)))
127 if (BIO_printf(bp, "%12s", "") <= 0)
129 if ((j = i2a_ASN1_OBJECT(bp, aobj)) > 0) {
131 count = X509_ATTRIBUTE_count(a);
133 X509err(X509_F_X509_REQ_PRINT_EX, X509_R_INVALID_ATTRIBUTES);
137 at = X509_ATTRIBUTE_get0_type(a, ii);
139 bs = at->value.asn1_string;
141 for (j = 25 - j; j > 0; j--)
142 if (BIO_write(bp, " ", 1) != 1)
144 if (BIO_puts(bp, ":") <= 0)
147 case V_ASN1_PRINTABLESTRING:
148 case V_ASN1_T61STRING:
149 case V_ASN1_NUMERICSTRING:
150 case V_ASN1_UTF8STRING:
151 case V_ASN1_IA5STRING:
152 if (BIO_write(bp, (char *)bs->data, bs->length)
155 if (BIO_puts(bp, "\n") <= 0)
159 if (BIO_puts(bp, "unable to print attribute\n") <= 0)
168 if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
169 exts = X509_REQ_get_extensions(x);
171 if (BIO_printf(bp, "%8sRequested Extensions:\n", "") <= 0)
173 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
177 ex = sk_X509_EXTENSION_value(exts, i);
178 if (BIO_printf(bp, "%12s", "") <= 0)
180 obj = X509_EXTENSION_get_object(ex);
181 if (i2a_ASN1_OBJECT(bp, obj) <= 0)
183 critical = X509_EXTENSION_get_critical(ex);
184 if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
186 if (!X509V3_EXT_print(bp, ex, cflag, 16)) {
187 if (BIO_printf(bp, "%16s", "") <= 0
188 || ASN1_STRING_print(bp,
189 X509_EXTENSION_get_data(ex)) <= 0)
192 if (BIO_write(bp, "\n", 1) <= 0)
195 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
199 if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
200 const X509_ALGOR *sig_alg;
201 const ASN1_BIT_STRING *sig;
202 X509_REQ_get0_signature(x, &sig, &sig_alg);
203 if (!X509_signature_print(bp, sig_alg, sig))
209 X509err(X509_F_X509_REQ_PRINT_EX, ERR_R_BUF_LIB);
213 int X509_REQ_print(BIO *bp, X509_REQ *x)
215 return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);