Constify X509_TRUST_add method.
[openssl.git] / crypto / x509 / t_req.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
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         X509err(X509_F_X509_REQ_PRINT_FP, 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;
46
47     if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
48         mlch = '\n';
49         nmindent = 12;
50     }
51
52     if (nmflags == X509_FLAG_COMPAT)
53         nmindent = 16;
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 (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, l) <= 0)
64             goto err;
65     }
66     if (!(cflag & X509_FLAG_NO_SUBJECT)) {
67         if (BIO_printf(bp, "        Subject:%c", mlch) <= 0)
68             goto err;
69         if (X509_NAME_print_ex(bp, X509_REQ_get_subject_name(x),
70             nmindent, nmflags) < 0)
71             goto err;
72         if (BIO_write(bp, "\n", 1) <= 0)
73             goto err;
74     }
75     if (!(cflag & X509_FLAG_NO_PUBKEY)) {
76         X509_PUBKEY *xpkey;
77         ASN1_OBJECT *koid;
78         if (BIO_write(bp, "        Subject Public Key Info:\n", 33) <= 0)
79             goto err;
80         if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
81             goto err;
82         xpkey = X509_REQ_get_X509_PUBKEY(x);
83         X509_PUBKEY_get0_param(&koid, NULL, NULL, NULL, xpkey);
84         if (i2a_ASN1_OBJECT(bp, koid) <= 0)
85             goto err;
86         if (BIO_puts(bp, "\n") <= 0)
87             goto err;
88
89         pkey = X509_REQ_get_pubkey(x);
90         if (pkey == NULL) {
91             BIO_printf(bp, "%12sUnable to load Public Key\n", "");
92             ERR_print_errors(bp);
93         } else {
94             EVP_PKEY_print_public(bp, pkey, 16, NULL);
95             EVP_PKEY_free(pkey);
96         }
97     }
98
99     if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
100         /* may not be */
101         if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
102             goto err;
103
104         if (X509_REQ_get_attr_count(x) == 0) {
105             if (BIO_printf(bp, "%12sa0:00\n", "") <= 0)
106                 goto err;
107         } else {
108             for (i = 0; i < X509_REQ_get_attr_count(x); i++) {
109                 ASN1_TYPE *at;
110                 X509_ATTRIBUTE *a;
111                 ASN1_BIT_STRING *bs = NULL;
112                 ASN1_OBJECT *aobj;
113                 int j, type = 0, count = 1, ii = 0;
114
115                 a = X509_REQ_get_attr(x, i);
116                 aobj = X509_ATTRIBUTE_get0_object(a);
117                 if (X509_REQ_extension_nid(OBJ_obj2nid(aobj)))
118                     continue;
119                 if (BIO_printf(bp, "%12s", "") <= 0)
120                     goto err;
121                 if ((j = i2a_ASN1_OBJECT(bp, aobj)) > 0) {
122                     ii = 0;
123                     count = X509_ATTRIBUTE_count(a);
124  get_next:
125                     at = X509_ATTRIBUTE_get0_type(a, ii);
126                     type = at->type;
127                     bs = at->value.asn1_string;
128                 }
129                 for (j = 25 - j; j > 0; j--)
130                     if (BIO_write(bp, " ", 1) != 1)
131                         goto err;
132                 if (BIO_puts(bp, ":") <= 0)
133                     goto err;
134                 if ((type == V_ASN1_PRINTABLESTRING) ||
135                     (type == V_ASN1_T61STRING) ||
136                     (type == V_ASN1_UTF8STRING) ||
137                     (type == V_ASN1_IA5STRING)) {
138                     if (BIO_write(bp, (char *)bs->data, bs->length)
139                         != bs->length)
140                         goto err;
141                     BIO_puts(bp, "\n");
142                 } else {
143                     BIO_puts(bp, "unable to print attribute\n");
144                 }
145                 if (++ii < count)
146                     goto get_next;
147             }
148         }
149     }
150     if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
151         exts = X509_REQ_get_extensions(x);
152         if (exts) {
153             BIO_printf(bp, "%8sRequested Extensions:\n", "");
154             for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
155                 ASN1_OBJECT *obj;
156                 X509_EXTENSION *ex;
157                 int critical;
158                 ex = sk_X509_EXTENSION_value(exts, i);
159                 if (BIO_printf(bp, "%12s", "") <= 0)
160                     goto err;
161                 obj = X509_EXTENSION_get_object(ex);
162                 i2a_ASN1_OBJECT(bp, obj);
163                 critical = X509_EXTENSION_get_critical(ex);
164                 if (BIO_printf(bp, ": %s\n", critical ? "critical" : "") <= 0)
165                     goto err;
166                 if (!X509V3_EXT_print(bp, ex, cflag, 16)) {
167                     BIO_printf(bp, "%16s", "");
168                     ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
169                 }
170                 if (BIO_write(bp, "\n", 1) <= 0)
171                     goto err;
172             }
173             sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
174         }
175     }
176
177     if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
178         X509_ALGOR *sig_alg;
179         ASN1_BIT_STRING *sig;
180         X509_REQ_get0_signature(&sig, &sig_alg, x);
181         if (!X509_signature_print(bp, sig_alg, sig))
182             goto err;
183     }
184
185     return (1);
186  err:
187     X509err(X509_F_X509_REQ_PRINT_EX, ERR_R_BUF_LIB);
188     return (0);
189 }
190
191 int X509_REQ_print(BIO *bp, X509_REQ *x)
192 {
193     return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
194 }