155714Skris/* crypto/asn1/t_req.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/buffer.h>
6255714Skris#include <openssl/bn.h>
6355714Skris#include <openssl/objects.h>
6455714Skris#include <openssl/x509.h>
6559191Skris#include <openssl/x509v3.h>
66160814Ssimon#ifndef OPENSSL_NO_RSA
67280304Sjkim# include <openssl/rsa.h>
68160814Ssimon#endif
69160814Ssimon#ifndef OPENSSL_NO_DSA
70280304Sjkim# include <openssl/dsa.h>
71160814Ssimon#endif
7255714Skris
73109998Smarkm#ifndef OPENSSL_NO_FP_API
7455714Skrisint X509_REQ_print_fp(FILE *fp, X509_REQ *x)
75280304Sjkim{
76280304Sjkim    BIO *b;
77280304Sjkim    int ret;
7855714Skris
79280304Sjkim    if ((b = BIO_new(BIO_s_file())) == NULL) {
80280304Sjkim        X509err(X509_F_X509_REQ_PRINT_FP, ERR_R_BUF_LIB);
81280304Sjkim        return (0);
82280304Sjkim    }
83280304Sjkim    BIO_set_fp(b, fp, BIO_NOCLOSE);
84280304Sjkim    ret = X509_REQ_print(b, x);
85280304Sjkim    BIO_free(b);
86280304Sjkim    return (ret);
87280304Sjkim}
8855714Skris#endif
8955714Skris
90280304Sjkimint X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags,
91280304Sjkim                      unsigned long cflag)
92280304Sjkim{
93280304Sjkim    unsigned long l;
94280304Sjkim    int i;
95280304Sjkim    const char *neg;
96280304Sjkim    X509_REQ_INFO *ri;
97280304Sjkim    EVP_PKEY *pkey;
98280304Sjkim    STACK_OF(X509_ATTRIBUTE) *sk;
99280304Sjkim    STACK_OF(X509_EXTENSION) *exts;
100280304Sjkim    char mlch = ' ';
101280304Sjkim    int nmindent = 0;
10255714Skris
103280304Sjkim    if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
104280304Sjkim        mlch = '\n';
105280304Sjkim        nmindent = 12;
106280304Sjkim    }
10755714Skris
108280304Sjkim    if (nmflags == X509_FLAG_COMPAT)
109280304Sjkim        nmindent = 16;
11055714Skris
111280304Sjkim    ri = x->req_info;
112280304Sjkim    if (!(cflag & X509_FLAG_NO_HEADER)) {
113280304Sjkim        if (BIO_write(bp, "Certificate Request:\n", 21) <= 0)
114280304Sjkim            goto err;
115280304Sjkim        if (BIO_write(bp, "    Data:\n", 10) <= 0)
116280304Sjkim            goto err;
117280304Sjkim    }
118280304Sjkim    if (!(cflag & X509_FLAG_NO_VERSION)) {
119280304Sjkim        neg = (ri->version->type == V_ASN1_NEG_INTEGER) ? "-" : "";
120280304Sjkim        l = 0;
121280304Sjkim        for (i = 0; i < ri->version->length; i++) {
122280304Sjkim            l <<= 8;
123280304Sjkim            l += ri->version->data[i];
124280304Sjkim        }
125280304Sjkim        if (BIO_printf(bp, "%8sVersion: %s%lu (%s0x%lx)\n", "", neg, l, neg,
126280304Sjkim                       l) <= 0)
127280304Sjkim            goto err;
128280304Sjkim    }
129280304Sjkim    if (!(cflag & X509_FLAG_NO_SUBJECT)) {
130280304Sjkim        if (BIO_printf(bp, "        Subject:%c", mlch) <= 0)
131280304Sjkim            goto err;
132280304Sjkim        if (X509_NAME_print_ex(bp, ri->subject, nmindent, nmflags) < 0)
133280304Sjkim            goto err;
134280304Sjkim        if (BIO_write(bp, "\n", 1) <= 0)
135280304Sjkim            goto err;
136280304Sjkim    }
137280304Sjkim    if (!(cflag & X509_FLAG_NO_PUBKEY)) {
138280304Sjkim        if (BIO_write(bp, "        Subject Public Key Info:\n", 33) <= 0)
139280304Sjkim            goto err;
140280304Sjkim        if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0)
141280304Sjkim            goto err;
142280304Sjkim        if (i2a_ASN1_OBJECT(bp, ri->pubkey->algor->algorithm) <= 0)
143280304Sjkim            goto err;
144280304Sjkim        if (BIO_puts(bp, "\n") <= 0)
145280304Sjkim            goto err;
14655714Skris
147280304Sjkim        pkey = X509_REQ_get_pubkey(x);
148280304Sjkim        if (pkey == NULL) {
149280304Sjkim            BIO_printf(bp, "%12sUnable to load Public Key\n", "");
150280304Sjkim            ERR_print_errors(bp);
151280304Sjkim        } else {
152280304Sjkim            EVP_PKEY_print_public(bp, pkey, 16, NULL);
153280304Sjkim            EVP_PKEY_free(pkey);
154280304Sjkim        }
155280304Sjkim    }
156109998Smarkm
157280304Sjkim    if (!(cflag & X509_FLAG_NO_ATTRIBUTES)) {
158280304Sjkim        /* may not be */
159280304Sjkim        if (BIO_printf(bp, "%8sAttributes:\n", "") <= 0)
160280304Sjkim            goto err;
16155714Skris
162280304Sjkim        sk = x->req_info->attributes;
163280304Sjkim        if (sk_X509_ATTRIBUTE_num(sk) == 0) {
164280304Sjkim            if (BIO_printf(bp, "%12sa0:00\n", "") <= 0)
165280304Sjkim                goto err;
166280304Sjkim        } else {
167280304Sjkim            for (i = 0; i < sk_X509_ATTRIBUTE_num(sk); i++) {
168280304Sjkim                ASN1_TYPE *at;
169280304Sjkim                X509_ATTRIBUTE *a;
170280304Sjkim                ASN1_BIT_STRING *bs = NULL;
171280304Sjkim                ASN1_TYPE *t;
172280304Sjkim                int j, type = 0, count = 1, ii = 0;
17355714Skris
174280304Sjkim                a = sk_X509_ATTRIBUTE_value(sk, i);
175280304Sjkim                if (X509_REQ_extension_nid(OBJ_obj2nid(a->object)))
176280304Sjkim                    continue;
177280304Sjkim                if (BIO_printf(bp, "%12s", "") <= 0)
178280304Sjkim                    goto err;
179280304Sjkim                if ((j = i2a_ASN1_OBJECT(bp, a->object)) > 0) {
180280304Sjkim                    if (a->single) {
181280304Sjkim                        t = a->value.single;
182280304Sjkim                        type = t->type;
183280304Sjkim                        bs = t->value.bit_string;
184280304Sjkim                    } else {
185280304Sjkim                        ii = 0;
186280304Sjkim                        count = sk_ASN1_TYPE_num(a->value.set);
187280304Sjkim get_next:
188280304Sjkim                        at = sk_ASN1_TYPE_value(a->value.set, ii);
189280304Sjkim                        type = at->type;
190280304Sjkim                        bs = at->value.asn1_string;
191280304Sjkim                    }
192280304Sjkim                }
193280304Sjkim                for (j = 25 - j; j > 0; j--)
194280304Sjkim                    if (BIO_write(bp, " ", 1) != 1)
195280304Sjkim                        goto err;
196280304Sjkim                if (BIO_puts(bp, ":") <= 0)
197280304Sjkim                    goto err;
198280304Sjkim                if ((type == V_ASN1_PRINTABLESTRING) ||
199280304Sjkim                    (type == V_ASN1_T61STRING) ||
200280304Sjkim                    (type == V_ASN1_IA5STRING)) {
201280304Sjkim                    if (BIO_write(bp, (char *)bs->data, bs->length)
202280304Sjkim                        != bs->length)
203280304Sjkim                        goto err;
204280304Sjkim                    BIO_puts(bp, "\n");
205280304Sjkim                } else {
206280304Sjkim                    BIO_puts(bp, "unable to print attribute\n");
207280304Sjkim                }
208280304Sjkim                if (++ii < count)
209280304Sjkim                    goto get_next;
210280304Sjkim            }
211280304Sjkim        }
212280304Sjkim    }
213280304Sjkim    if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
214280304Sjkim        exts = X509_REQ_get_extensions(x);
215280304Sjkim        if (exts) {
216280304Sjkim            BIO_printf(bp, "%8sRequested Extensions:\n", "");
217280304Sjkim            for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
218280304Sjkim                ASN1_OBJECT *obj;
219280304Sjkim                X509_EXTENSION *ex;
220280304Sjkim                int j;
221280304Sjkim                ex = sk_X509_EXTENSION_value(exts, i);
222280304Sjkim                if (BIO_printf(bp, "%12s", "") <= 0)
223280304Sjkim                    goto err;
224280304Sjkim                obj = X509_EXTENSION_get_object(ex);
225280304Sjkim                i2a_ASN1_OBJECT(bp, obj);
226280304Sjkim                j = X509_EXTENSION_get_critical(ex);
227280304Sjkim                if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
228280304Sjkim                    goto err;
229280304Sjkim                if (!X509V3_EXT_print(bp, ex, cflag, 16)) {
230280304Sjkim                    BIO_printf(bp, "%16s", "");
231280304Sjkim                    M_ASN1_OCTET_STRING_print(bp, ex->value);
232280304Sjkim                }
233280304Sjkim                if (BIO_write(bp, "\n", 1) <= 0)
234280304Sjkim                    goto err;
235280304Sjkim            }
236280304Sjkim            sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
237280304Sjkim        }
238280304Sjkim    }
23955714Skris
240280304Sjkim    if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
241280304Sjkim        if (!X509_signature_print(bp, x->sig_alg, x->signature))
242280304Sjkim            goto err;
243280304Sjkim    }
24455714Skris
245280304Sjkim    return (1);
246280304Sjkim err:
247280304Sjkim    X509err(X509_F_X509_REQ_PRINT_EX, ERR_R_BUF_LIB);
248280304Sjkim    return (0);
249280304Sjkim}
25059191Skris
251109998Smarkmint X509_REQ_print(BIO *bp, X509_REQ *x)
252280304Sjkim{
253280304Sjkim    return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
254280304Sjkim}
255