Print <ABSENT> if a STACK is NULL.
[openssl.git] / ssl / ssl_txt.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 /* ====================================================================
11  * Copyright 2005 Nokia. All rights reserved.
12  *
13  * The portions of the attached software ("Contribution") is developed by
14  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
15  * license.
16  *
17  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
18  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
19  * support (see RFC 4279) to OpenSSL.
20  *
21  * No patent licenses or other rights except those expressly stated in
22  * the OpenSSL open source license shall be deemed granted or received
23  * expressly, by implication, estoppel, or otherwise.
24  *
25  * No assurances are provided by Nokia that the Contribution does not
26  * infringe the patent or other intellectual property rights of any third
27  * party or that the license provides you with all the necessary rights
28  * to make use of the Contribution.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
31  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
32  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
33  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
34  * OTHERWISE.
35  */
36
37 #include <stdio.h>
38 #include <openssl/buffer.h>
39 #include "ssl_locl.h"
40
41 #ifndef OPENSSL_NO_STDIO
42 int SSL_SESSION_print_fp(FILE *fp, const SSL_SESSION *x)
43 {
44     BIO *b;
45     int ret;
46
47     if ((b = BIO_new(BIO_s_file())) == NULL) {
48         SSLerr(SSL_F_SSL_SESSION_PRINT_FP, ERR_R_BUF_LIB);
49         return (0);
50     }
51     BIO_set_fp(b, fp, BIO_NOCLOSE);
52     ret = SSL_SESSION_print(b, x);
53     BIO_free(b);
54     return (ret);
55 }
56 #endif
57
58 int SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
59 {
60     unsigned int i;
61     const char *s;
62
63     if (x == NULL)
64         goto err;
65     if (BIO_puts(bp, "SSL-Session:\n") <= 0)
66         goto err;
67     s = ssl_protocol_to_string(x->ssl_version);
68     if (BIO_printf(bp, "    Protocol  : %s\n", s) <= 0)
69         goto err;
70
71     if (x->cipher == NULL) {
72         if (((x->cipher_id) & 0xff000000) == 0x02000000) {
73             if (BIO_printf
74                 (bp, "    Cipher    : %06lX\n", x->cipher_id & 0xffffff) <= 0)
75                 goto err;
76         } else {
77             if (BIO_printf
78                 (bp, "    Cipher    : %04lX\n", x->cipher_id & 0xffff) <= 0)
79                 goto err;
80         }
81     } else {
82         if (BIO_printf
83             (bp, "    Cipher    : %s\n",
84              ((x->cipher == NULL) ? "unknown" : x->cipher->name)) <= 0)
85             goto err;
86     }
87     if (BIO_puts(bp, "    Session-ID: ") <= 0)
88         goto err;
89     for (i = 0; i < x->session_id_length; i++) {
90         if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
91             goto err;
92     }
93     if (BIO_puts(bp, "\n    Session-ID-ctx: ") <= 0)
94         goto err;
95     for (i = 0; i < x->sid_ctx_length; i++) {
96         if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
97             goto err;
98     }
99     if (BIO_puts(bp, "\n    Master-Key: ") <= 0)
100         goto err;
101     for (i = 0; i < (unsigned int)x->master_key_length; i++) {
102         if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
103             goto err;
104     }
105 #ifndef OPENSSL_NO_PSK
106     if (BIO_puts(bp, "\n    PSK identity: ") <= 0)
107         goto err;
108     if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0)
109         goto err;
110     if (BIO_puts(bp, "\n    PSK identity hint: ") <= 0)
111         goto err;
112     if (BIO_printf
113         (bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0)
114         goto err;
115 #endif
116 #ifndef OPENSSL_NO_SRP
117     if (BIO_puts(bp, "\n    SRP username: ") <= 0)
118         goto err;
119     if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0)
120         goto err;
121 #endif
122     if (x->tlsext_tick_lifetime_hint) {
123         if (BIO_printf(bp,
124                        "\n    TLS session ticket lifetime hint: %ld (seconds)",
125                        x->tlsext_tick_lifetime_hint) <= 0)
126             goto err;
127     }
128     if (x->tlsext_tick) {
129         if (BIO_puts(bp, "\n    TLS session ticket:\n") <= 0)
130             goto err;
131         if (BIO_dump_indent
132             (bp, (const char *)x->tlsext_tick, x->tlsext_ticklen, 4)
133             <= 0)
134             goto err;
135     }
136 #ifndef OPENSSL_NO_COMP
137     if (x->compress_meth != 0) {
138         SSL_COMP *comp = NULL;
139
140         if (!ssl_cipher_get_evp(x, NULL, NULL, NULL, NULL, &comp, 0))
141             goto err;
142         if (comp == NULL) {
143             if (BIO_printf(bp, "\n    Compression: %d", x->compress_meth) <= 0)
144                 goto err;
145         } else {
146             if (BIO_printf(bp, "\n    Compression: %d (%s)", comp->id,
147                            comp->name) <= 0)
148                 goto err;
149         }
150     }
151 #endif
152     if (x->time != 0L) {
153         if (BIO_printf(bp, "\n    Start Time: %ld", x->time) <= 0)
154             goto err;
155     }
156     if (x->timeout != 0L) {
157         if (BIO_printf(bp, "\n    Timeout   : %ld (sec)", x->timeout) <= 0)
158             goto err;
159     }
160     if (BIO_puts(bp, "\n") <= 0)
161         goto err;
162
163     if (BIO_puts(bp, "    Verify return code: ") <= 0)
164         goto err;
165     if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
166                    X509_verify_cert_error_string(x->verify_result)) <= 0)
167         goto err;
168
169     if (BIO_printf(bp, "    Extended master secret: %s\n",
170                    x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
171         goto err;
172
173     return (1);
174  err:
175     return (0);
176 }
177
178 /*
179  * print session id and master key in NSS keylog format (RSA
180  * Session-ID:<session id> Master-Key:<master key>)
181  */
182 int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
183 {
184     unsigned int i;
185
186     if (x == NULL)
187         goto err;
188     if (x->session_id_length == 0 || x->master_key_length == 0)
189         goto err;
190
191     /*
192      * the RSA prefix is required by the format's definition although there's
193      * nothing RSA-specific in the output, therefore, we don't have to check if
194      * the cipher suite is based on RSA
195      */
196     if (BIO_puts(bp, "RSA ") <= 0)
197         goto err;
198
199     if (BIO_puts(bp, "Session-ID:") <= 0)
200         goto err;
201     for (i = 0; i < x->session_id_length; i++) {
202         if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
203             goto err;
204     }
205     if (BIO_puts(bp, " Master-Key:") <= 0)
206         goto err;
207     for (i = 0; i < (unsigned int)x->master_key_length; i++) {
208         if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
209             goto err;
210     }
211     if (BIO_puts(bp, "\n") <= 0)
212         goto err;
213
214     return (1);
215  err:
216     return (0);
217 }