• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <assert.h>
58 
59 #include <openssl/asn1.h>
60 #include <openssl/bio.h>
61 #include <openssl/digest.h>
62 #include <openssl/err.h>
63 #include <openssl/evp.h>
64 #include <openssl/mem.h>
65 #include <openssl/obj.h>
66 #include <openssl/x509.h>
67 #include <openssl/x509v3.h>
68 
69 #include "internal.h"
70 
71 
X509_print_ex_fp(FILE * fp,X509 * x,unsigned long nmflag,unsigned long cflag)72 int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag,
73                      unsigned long cflag) {
74   BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
75   if (b == NULL) {
76     OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
77     return 0;
78   }
79   int ret = X509_print_ex(b, x, nmflag, cflag);
80   BIO_free(b);
81   return ret;
82 }
83 
X509_print_fp(FILE * fp,X509 * x)84 int X509_print_fp(FILE *fp, X509 *x) {
85   return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
86 }
87 
X509_print(BIO * bp,X509 * x)88 int X509_print(BIO *bp, X509 *x) {
89   return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
90 }
91 
X509_print_ex(BIO * bp,X509 * x,unsigned long nmflags,unsigned long cflag)92 int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags,
93                   unsigned long cflag) {
94   char mlch = ' ';
95   int nmindent = 0;
96   if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
97     mlch = '\n';
98     nmindent = 12;
99   }
100 
101   if (nmflags == X509_FLAG_COMPAT) {
102     nmindent = 16;
103   }
104 
105   const X509_CINF *ci = x->cert_info;
106   if (!(cflag & X509_FLAG_NO_HEADER)) {
107     if (BIO_write(bp, "Certificate:\n", 13) <= 0) {
108       return 0;
109     }
110     if (BIO_write(bp, "    Data:\n", 10) <= 0) {
111       return 0;
112     }
113   }
114   if (!(cflag & X509_FLAG_NO_VERSION)) {
115     long l = X509_get_version(x);
116     assert(X509_VERSION_1 <= l && l <= X509_VERSION_3);
117     if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1,
118                    (unsigned long)l) <= 0) {
119       return 0;
120     }
121   }
122   if (!(cflag & X509_FLAG_NO_SERIAL)) {
123     if (BIO_write(bp, "        Serial Number:", 22) <= 0) {
124       return 0;
125     }
126 
127     const ASN1_INTEGER *serial = X509_get0_serialNumber(x);
128     uint64_t serial_u64;
129     if (ASN1_INTEGER_get_uint64(&serial_u64, serial)) {
130       assert(serial->type != V_ASN1_NEG_INTEGER);
131       if (BIO_printf(bp, " %" PRIu64 " (0x%" PRIx64 ")\n", serial_u64,
132                      serial_u64) <= 0) {
133         return 0;
134       }
135     } else {
136       ERR_clear_error();  // Clear |ASN1_INTEGER_get_uint64|'s error.
137       const char *neg =
138           (serial->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
139       if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0) {
140         return 0;
141       }
142 
143       for (int i = 0; i < serial->length; i++) {
144         if (BIO_printf(bp, "%02x%c", serial->data[i],
145                        ((i + 1 == serial->length) ? '\n' : ':')) <= 0) {
146           return 0;
147         }
148       }
149     }
150   }
151 
152   if (!(cflag & X509_FLAG_NO_SIGNAME)) {
153     if (X509_signature_print(bp, ci->signature, NULL) <= 0) {
154       return 0;
155     }
156   }
157 
158   if (!(cflag & X509_FLAG_NO_ISSUER)) {
159     if (BIO_printf(bp, "        Issuer:%c", mlch) <= 0) {
160       return 0;
161     }
162     if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags) <
163         0) {
164       return 0;
165     }
166     if (BIO_write(bp, "\n", 1) <= 0) {
167       return 0;
168     }
169   }
170   if (!(cflag & X509_FLAG_NO_VALIDITY)) {
171     if (BIO_write(bp, "        Validity\n", 17) <= 0) {
172       return 0;
173     }
174     if (BIO_write(bp, "            Not Before: ", 24) <= 0) {
175       return 0;
176     }
177     if (!ASN1_TIME_print(bp, X509_get_notBefore(x))) {
178       return 0;
179     }
180     if (BIO_write(bp, "\n            Not After : ", 25) <= 0) {
181       return 0;
182     }
183     if (!ASN1_TIME_print(bp, X509_get_notAfter(x))) {
184       return 0;
185     }
186     if (BIO_write(bp, "\n", 1) <= 0) {
187       return 0;
188     }
189   }
190   if (!(cflag & X509_FLAG_NO_SUBJECT)) {
191     if (BIO_printf(bp, "        Subject:%c", mlch) <= 0) {
192       return 0;
193     }
194     if (X509_NAME_print_ex(bp, X509_get_subject_name(x), nmindent, nmflags) <
195         0) {
196       return 0;
197     }
198     if (BIO_write(bp, "\n", 1) <= 0) {
199       return 0;
200     }
201   }
202   if (!(cflag & X509_FLAG_NO_PUBKEY)) {
203     if (BIO_write(bp, "        Subject Public Key Info:\n", 33) <= 0) {
204       return 0;
205     }
206     if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0) {
207       return 0;
208     }
209     if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0) {
210       return 0;
211     }
212     if (BIO_puts(bp, "\n") <= 0) {
213       return 0;
214     }
215 
216     EVP_PKEY *pkey = X509_get_pubkey(x);
217     if (pkey == NULL) {
218       BIO_printf(bp, "%12sUnable to load Public Key\n", "");
219       ERR_print_errors(bp);
220     } else {
221       EVP_PKEY_print_public(bp, pkey, 16, NULL);
222       EVP_PKEY_free(pkey);
223     }
224   }
225 
226   if (!(cflag & X509_FLAG_NO_IDS)) {
227     if (ci->issuerUID) {
228       if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0) {
229         return 0;
230       }
231       if (!X509_signature_dump(bp, ci->issuerUID, 12)) {
232         return 0;
233       }
234     }
235     if (ci->subjectUID) {
236       if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0) {
237         return 0;
238       }
239       if (!X509_signature_dump(bp, ci->subjectUID, 12)) {
240         return 0;
241       }
242     }
243   }
244 
245   if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
246     X509V3_extensions_print(bp, "X509v3 extensions", ci->extensions, cflag, 8);
247   }
248 
249   if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
250     if (X509_signature_print(bp, x->sig_alg, x->signature) <= 0) {
251       return 0;
252     }
253   }
254   if (!(cflag & X509_FLAG_NO_AUX)) {
255     if (!X509_CERT_AUX_print(bp, x->aux, 0)) {
256       return 0;
257     }
258   }
259 
260   return 1;
261 }
262 
X509_signature_print(BIO * bp,const X509_ALGOR * sigalg,const ASN1_STRING * sig)263 int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
264                          const ASN1_STRING *sig) {
265   if (BIO_puts(bp, "    Signature Algorithm: ") <= 0) {
266     return 0;
267   }
268   if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) {
269     return 0;
270   }
271 
272   // RSA-PSS signatures have parameters to print.
273   int sig_nid = OBJ_obj2nid(sigalg->algorithm);
274   if (sig_nid == NID_rsassaPss &&
275       !x509_print_rsa_pss_params(bp, sigalg, 9, 0)) {
276     return 0;
277   }
278 
279   if (sig) {
280     return X509_signature_dump(bp, sig, 9);
281   } else if (BIO_puts(bp, "\n") <= 0) {
282     return 0;
283   }
284   return 1;
285 }
286 
X509_NAME_print(BIO * bp,const X509_NAME * name,int obase)287 int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase) {
288   char *s, *c, *b;
289   int ret = 0, i;
290 
291   b = X509_NAME_oneline(name, NULL, 0);
292   if (!b) {
293     return 0;
294   }
295   if (!*b) {
296     OPENSSL_free(b);
297     return 1;
298   }
299   s = b + 1;  // skip the first slash
300 
301   c = s;
302   for (;;) {
303     if (((*s == '/') && ((s[1] >= 'A') && (s[1] <= 'Z') &&
304                          ((s[2] == '=') || ((s[2] >= 'A') && (s[2] <= 'Z') &&
305                                             (s[3] == '='))))) ||
306         (*s == '\0')) {
307       i = s - c;
308       if (BIO_write(bp, c, i) != i) {
309         goto err;
310       }
311       c = s + 1;  // skip following slash
312       if (*s != '\0') {
313         if (BIO_write(bp, ", ", 2) != 2) {
314           goto err;
315         }
316       }
317     }
318     if (*s == '\0') {
319       break;
320     }
321     s++;
322   }
323 
324   ret = 1;
325   if (0) {
326   err:
327     OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
328   }
329   OPENSSL_free(b);
330   return ret;
331 }
332