CTR, HASH and HMAC DRBGs in provider
[openssl.git] / crypto / x509 / t_req.c
1 /*
2  * Copyright 1995-2020 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 DEFINE_STACK_OF(X509_EXTENSION)
21
22 #ifndef OPENSSL_NO_STDIO
23 int X509_REQ_print_fp(FILE *fp, X509_REQ *x)
24 {
25     BIO *b;
26     int ret;
27
28     if ((b = BIO_new(BIO_s_file())) == NULL) {
29         X509err(X509_F_X509_REQ_PRINT_FP, ERR_R_BUF_LIB);
30         return 0;
31     }
32     BIO_set_fp(b, fp, BIO_NOCLOSE);
33     ret = X509_REQ_print(b, x);
34     BIO_free(b);
35     return ret;
36 }
37 #endif
38
39 int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags,
40                       unsigned long cflag)
41 {
42     long l;
43     int i;
44     EVP_PKEY *pkey;
45     STACK_OF(X509_EXTENSION) *exts;
46     char mlch = ' ';
47     int nmindent = 0;
48
49     if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
50         mlch = '\n';
51         nmindent = 12;
52     }
53
54     if (nmflags == X509_FLAG_COMPAT)
55         nmindent = 16;
56
57     if (!(cflag & X509_FLAG_NO_HEADER)) {
58         if (BIO_write(bp, "Certificate Request:\n", 21) <= 0)
59             goto err;
60         if (BIO_write(bp, "    Data:\n", 10) <= 0)
61             goto err;
62     }
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)
67                 goto err;
68         } else {
69             if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0)
70                 goto err;
71         }
72     }
73     if (!(cflag & X509_FLAG_NO_SUBJECT)) {
74         if (BIO_printf(bp, "        Subject:%c", mlch) <= 0)
75             goto err;
76         if (X509_NAME_print_ex(bp, X509_REQ_get_subject_name(x),
77             nmindent, nmflags) < 0)
78             goto err;
79         if (BIO_write(bp, "\n", 1) <= 0)
80             goto err;
81     }
82     if (!(cflag & X509_FLAG_NO_PUBKEY)) {
83         X509_PUBKEY *xpkey;
84         ASN1_OBJECT *koid;
85         if (BIO_write(bp, "        Subject Public Key Info:\n", 33) <= 0)
86             goto err;
87         if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
88             goto err;
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)
92             goto err;
93         if (BIO_puts(bp, "\n") <= 0)
94             goto err;
95
96         pkey = X509_REQ_get0_pubkey(x);
97         if (pkey == NULL) {
98             if (BIO_printf(bp, "%12sUnable to load Public Key\n", "") <= 0)
99                 goto err;
100             ERR_print_errors(bp);
101         } else {
102             if (EVP_PKEY_print_public(bp, pkey, 16, NULL) <= 0)
103                 goto err;
104         }
105     }
106
107     if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
108         /* may not be */
109         if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
110             goto err;
111
112         if (X509_REQ_get_attr_count(x) == 0) {
113             if (BIO_printf(bp, "%12sa0:00\n", "") <= 0)
114                 goto err;
115         } else {
116             for (i = 0; i < X509_REQ_get_attr_count(x); i++) {
117                 ASN1_TYPE *at;
118                 X509_ATTRIBUTE *a;
119                 ASN1_BIT_STRING *bs = NULL;
120                 ASN1_OBJECT *aobj;
121                 int j, type = 0, count = 1, ii = 0;
122
123                 a = X509_REQ_get_attr(x, i);
124                 aobj = X509_ATTRIBUTE_get0_object(a);
125                 if (X509_REQ_extension_nid(OBJ_obj2nid(aobj)))
126                     continue;
127                 if (BIO_printf(bp, "%12s", "") <= 0)
128                     goto err;
129                 if ((j = i2a_ASN1_OBJECT(bp, aobj)) > 0) {
130                     ii = 0;
131                     count = X509_ATTRIBUTE_count(a);
132                     if (count == 0) {
133                       X509err(X509_F_X509_REQ_PRINT_EX, X509_R_INVALID_ATTRIBUTES);
134                       return 0;
135                     }
136  get_next:
137                     at = X509_ATTRIBUTE_get0_type(a, ii);
138                     type = at->type;
139                     bs = at->value.asn1_string;
140                 }
141                 for (j = 25 - j; j > 0; j--)
142                     if (BIO_write(bp, " ", 1) != 1)
143                         goto err;
144                 if (BIO_puts(bp, ":") <= 0)
145                     goto err;
146                 switch (type) {
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)
153                             != bs->length)
154                         goto err;
155                     if (BIO_puts(bp, "\n") <= 0)
156                         goto err;
157                     break;
158                 default:
159                     if (BIO_puts(bp, "unable to print attribute\n") <= 0)
160                         goto err;
161                     break;
162                 }
163                 if (++ii < count)
164                     goto get_next;
165             }
166         }
167     }
168     if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
169         exts = X509_REQ_get_extensions(x);
170         if (exts) {
171             if (BIO_printf(bp, "%8sRequested Extensions:\n", "") <= 0)
172                 goto err;
173             for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
174                 ASN1_OBJECT *obj;
175                 X509_EXTENSION *ex;
176                 int critical;
177                 ex = sk_X509_EXTENSION_value(exts, i);
178                 if (BIO_printf(bp, "%12s", "") <= 0)
179                     goto err;
180                 obj = X509_EXTENSION_get_object(ex);
181                 if (i2a_ASN1_OBJECT(bp, obj) <= 0)
182                     goto err;
183                 critical = X509_EXTENSION_get_critical(ex);
184                 if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
185                     goto err;
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)
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     X509err(X509_F_X509_REQ_PRINT_EX, 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 }