2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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 #ifndef OPENSSL_NO_STDIO
21 int X509_REQ_print_fp(FILE *fp, X509_REQ *x)
26 if ((b = BIO_new(BIO_s_file())) == NULL) {
27 X509err(X509_F_X509_REQ_PRINT_FP, ERR_R_BUF_LIB);
30 BIO_set_fp(b, fp, BIO_NOCLOSE);
31 ret = X509_REQ_print(b, x);
37 int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags,
43 STACK_OF(X509_EXTENSION) *exts;
47 if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
52 if (nmflags == X509_FLAG_COMPAT)
55 if (!(cflag & X509_FLAG_NO_HEADER)) {
56 if (BIO_write(bp, "Certificate Request:\n", 21) <= 0)
58 if (BIO_write(bp, " Data:\n", 10) <= 0)
61 if (!(cflag & X509_FLAG_NO_VERSION)) {
62 l = X509_REQ_get_version(x);
63 if (l >= 0 && l <= 2) {
64 if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0)
67 if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
71 if (!(cflag & X509_FLAG_NO_SUBJECT)) {
72 if (BIO_printf(bp, " Subject:%c", mlch) <= 0)
74 if (X509_NAME_print_ex(bp, X509_REQ_get_subject_name(x),
75 nmindent, nmflags) < 0)
77 if (BIO_write(bp, "\n", 1) <= 0)
80 if (!(cflag & X509_FLAG_NO_PUBKEY)) {
83 if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0)
85 if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
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)
91 if (BIO_puts(bp, "\n") <= 0)
94 pkey = X509_REQ_get0_pubkey(x);
96 BIO_printf(bp, "%12sUnable to load Public Key\n", "");
99 EVP_PKEY_print_public(bp, pkey, 16, NULL);
103 if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
105 if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
108 if (X509_REQ_get_attr_count(x) == 0) {
109 if (BIO_printf(bp, "%12sa0:00\n", "") <= 0)
112 for (i = 0; i < X509_REQ_get_attr_count(x); i++) {
115 ASN1_BIT_STRING *bs = NULL;
117 int j, type = 0, count = 1, ii = 0;
119 a = X509_REQ_get_attr(x, i);
120 aobj = X509_ATTRIBUTE_get0_object(a);
121 if (X509_REQ_extension_nid(OBJ_obj2nid(aobj)))
123 if (BIO_printf(bp, "%12s", "") <= 0)
125 if ((j = i2a_ASN1_OBJECT(bp, aobj)) > 0) {
127 count = X509_ATTRIBUTE_count(a);
129 at = X509_ATTRIBUTE_get0_type(a, ii);
131 bs = at->value.asn1_string;
133 for (j = 25 - j; j > 0; j--)
134 if (BIO_write(bp, " ", 1) != 1)
136 if (BIO_puts(bp, ":") <= 0)
138 if ((type == V_ASN1_PRINTABLESTRING) ||
139 (type == V_ASN1_T61STRING) ||
140 (type == V_ASN1_UTF8STRING) ||
141 (type == V_ASN1_IA5STRING)) {
142 if (BIO_write(bp, (char *)bs->data, bs->length)
147 BIO_puts(bp, "unable to print attribute\n");
154 if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
155 exts = X509_REQ_get_extensions(x);
157 BIO_printf(bp, "%8sRequested Extensions:\n", "");
158 for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
162 ex = sk_X509_EXTENSION_value(exts, i);
163 if (BIO_printf(bp, "%12s", "") <= 0)
165 obj = X509_EXTENSION_get_object(ex);
166 i2a_ASN1_OBJECT(bp, obj);
167 critical = X509_EXTENSION_get_critical(ex);
168 if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
170 if (!X509V3_EXT_print(bp, ex, cflag, 16)) {
171 BIO_printf(bp, "%16s", "");
172 ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
174 if (BIO_write(bp, "\n", 1) <= 0)
177 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
181 if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
182 const X509_ALGOR *sig_alg;
183 const ASN1_BIT_STRING *sig;
184 X509_REQ_get0_signature(x, &sig, &sig_alg);
185 if (!X509_signature_print(bp, sig_alg, sig))
191 X509err(X509_F_X509_REQ_PRINT_EX, ERR_R_BUF_LIB);
195 int X509_REQ_print(BIO *bp, X509_REQ *x)
197 return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);