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