1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.] */
56 
57 #include <stdio.h>
58 
59 #include <openssl/bn.h>
60 #include <openssl/buffer.h>
61 #include <openssl/err.h>
62 #include <openssl/objects.h>
63 #include <openssl/x509.h>
64 #include <openssl/x509v3.h>
65 
66 
X509_REQ_print_fp(FILE * fp,X509_REQ * x)67 int X509_REQ_print_fp(FILE *fp, X509_REQ *x) {
68   BIO *bio = BIO_new(BIO_s_file());
69   if (bio == NULL) {
70     OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
71     return 0;
72   }
73 
74   BIO_set_fp(bio, fp, BIO_NOCLOSE);
75   int ret = X509_REQ_print(bio, x);
76   BIO_free(bio);
77   return ret;
78 }
79 
X509_REQ_print_ex(BIO * bio,X509_REQ * x,unsigned long nmflags,unsigned long cflag)80 int X509_REQ_print_ex(BIO *bio, X509_REQ *x, unsigned long nmflags,
81                       unsigned long cflag) {
82   long l;
83   EVP_PKEY *pkey;
84   STACK_OF(X509_ATTRIBUTE) * sk;
85   char mlch = ' ';
86 
87   int nmindent = 0;
88 
89   if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
90     mlch = '\n';
91     nmindent = 12;
92   }
93 
94   if (nmflags == X509_FLAG_COMPAT) {
95     nmindent = 16;
96   }
97 
98   X509_REQ_INFO *ri = x->req_info;
99   if (!(cflag & X509_FLAG_NO_HEADER)) {
100     if (BIO_write(bio, "Certificate Request:\n", 21) <= 0 ||
101         BIO_write(bio, "    Data:\n", 10) <= 0) {
102       goto err;
103     }
104   }
105   if (!(cflag & X509_FLAG_NO_VERSION)) {
106     l = X509_REQ_get_version(x);
107     if (BIO_printf(bio, "%8sVersion: %ld (0x%lx)\n", "", l + 1, l) <= 0) {
108       goto err;
109     }
110   }
111   if (!(cflag & X509_FLAG_NO_SUBJECT)) {
112     if (BIO_printf(bio, "        Subject:%c", mlch) <= 0 ||
113         X509_NAME_print_ex(bio, ri->subject, nmindent, nmflags) < 0 ||
114         BIO_write(bio, "\n", 1) <= 0) {
115       goto err;
116     }
117   }
118   if (!(cflag & X509_FLAG_NO_PUBKEY)) {
119     if (BIO_write(bio, "        Subject Public Key Info:\n", 33) <= 0 ||
120         BIO_printf(bio, "%12sPublic Key Algorithm: ", "") <= 0 ||
121         i2a_ASN1_OBJECT(bio, ri->pubkey->algor->algorithm) <= 0 ||
122         BIO_puts(bio, "\n") <= 0) {
123       goto err;
124     }
125 
126     pkey = X509_REQ_get_pubkey(x);
127     if (pkey == NULL) {
128       BIO_printf(bio, "%12sUnable to load Public Key\n", "");
129       ERR_print_errors(bio);
130     } else {
131       EVP_PKEY_print_public(bio, pkey, 16, NULL);
132       EVP_PKEY_free(pkey);
133     }
134   }
135 
136   if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
137     if (BIO_printf(bio, "%8sAttributes:\n", "") <= 0) {
138       goto err;
139     }
140 
141     sk = x->req_info->attributes;
142     if (sk_X509_ATTRIBUTE_num(sk) == 0) {
143       if (BIO_printf(bio, "%12sa0:00\n", "") <= 0) {
144         goto err;
145       }
146     } else {
147       size_t i;
148       for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
149         X509_ATTRIBUTE *a = sk_X509_ATTRIBUTE_value(sk, i);
150         ASN1_OBJECT *aobj = X509_ATTRIBUTE_get0_object(a);
151 
152         if (X509_REQ_extension_nid(OBJ_obj2nid(aobj))) {
153           continue;
154         }
155 
156         if (BIO_printf(bio, "%12s", "") <= 0) {
157           goto err;
158         }
159 
160         const int num_attrs = X509_ATTRIBUTE_count(a);
161         const int obj_str_len = i2a_ASN1_OBJECT(bio, aobj);
162         if (obj_str_len <= 0) {
163           if (BIO_puts(bio, "(Unable to print attribute ID.)\n") < 0) {
164             goto err;
165           } else {
166             continue;
167           }
168         }
169 
170         int j;
171         for (j = 0; j < num_attrs; j++) {
172           const ASN1_TYPE *at = X509_ATTRIBUTE_get0_type(a, j);
173           const int type = at->type;
174           ASN1_BIT_STRING *bs = at->value.asn1_string;
175 
176           int k;
177           for (k = 25 - obj_str_len; k > 0; k--) {
178             if (BIO_write(bio, " ", 1) != 1) {
179               goto err;
180             }
181           }
182 
183           if (BIO_puts(bio, ":") <= 0) {
184             goto err;
185           }
186 
187           if (type == V_ASN1_PRINTABLESTRING ||
188               type == V_ASN1_UTF8STRING ||
189               type == V_ASN1_IA5STRING ||
190               type == V_ASN1_T61STRING) {
191             if (BIO_write(bio, (char *)bs->data, bs->length) != bs->length) {
192               goto err;
193             }
194             BIO_puts(bio, "\n");
195           } else {
196             BIO_puts(bio, "unable to print attribute\n");
197           }
198         }
199       }
200     }
201   }
202 
203   if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
204     STACK_OF(X509_EXTENSION) *exts = X509_REQ_get_extensions(x);
205     if (exts) {
206       BIO_printf(bio, "%8sRequested Extensions:\n", "");
207 
208       size_t i;
209       for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
210         X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i);
211         if (BIO_printf(bio, "%12s", "") <= 0) {
212           goto err;
213         }
214         ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex);
215         i2a_ASN1_OBJECT(bio, obj);
216         const int is_critical = X509_EXTENSION_get_critical(ex);
217         if (BIO_printf(bio, ": %s\n", is_critical ? "critical" : "") <= 0) {
218           goto err;
219         }
220         if (!X509V3_EXT_print(bio, ex, cflag, 16)) {
221           BIO_printf(bio, "%16s", "");
222           ASN1_STRING_print(bio, X509_EXTENSION_get_data(ex));
223         }
224         if (BIO_write(bio, "\n", 1) <= 0) {
225           goto err;
226         }
227       }
228       sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
229     }
230   }
231 
232   if (!(cflag & X509_FLAG_NO_SIGDUMP) &&
233       !X509_signature_print(bio, x->sig_alg, x->signature)) {
234     goto err;
235   }
236 
237   return 1;
238 
239 err:
240   OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
241   return 0;
242 }
243 
X509_REQ_print(BIO * bio,X509_REQ * req)244 int X509_REQ_print(BIO *bio, X509_REQ *req) {
245   return X509_REQ_print_ex(bio, req, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
246 }
247