Work around Travis "virtual memory exhausted" error
[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     size_t 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 < 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->ext.tick_lifetime_hint) {
123         if (BIO_printf(bp,
124                        "\n    TLS session ticket lifetime hint: %ld (seconds)",
125                        x->ext.tick_lifetime_hint) <= 0)
126             goto err;
127     }
128     if (x->ext.tick) {
129         if (BIO_puts(bp, "\n    TLS session ticket:\n") <= 0)
130             goto err;
131         /* TODO(size_t): Convert this call */
132         if (BIO_dump_indent
133             (bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4)
134             <= 0)
135             goto err;
136     }
137 #ifndef OPENSSL_NO_COMP
138     if (x->compress_meth != 0) {
139         SSL_COMP *comp = NULL;
140
141         if (!ssl_cipher_get_evp(x, NULL, NULL, NULL, NULL, &comp, 0))
142             goto err;
143         if (comp == NULL) {
144             if (BIO_printf(bp, "\n    Compression: %d", x->compress_meth) <= 0)
145                 goto err;
146         } else {
147             if (BIO_printf(bp, "\n    Compression: %d (%s)", comp->id,
148                            comp->name) <= 0)
149                 goto err;
150         }
151     }
152 #endif
153     if (x->time != 0L) {
154         if (BIO_printf(bp, "\n    Start Time: %ld", x->time) <= 0)
155             goto err;
156     }
157     if (x->timeout != 0L) {
158         if (BIO_printf(bp, "\n    Timeout   : %ld (sec)", x->timeout) <= 0)
159             goto err;
160     }
161     if (BIO_puts(bp, "\n") <= 0)
162         goto err;
163
164     if (BIO_puts(bp, "    Verify return code: ") <= 0)
165         goto err;
166     if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
167                    X509_verify_cert_error_string(x->verify_result)) <= 0)
168         goto err;
169
170     if (BIO_printf(bp, "    Extended master secret: %s\n",
171                    x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
172         goto err;
173
174     return (1);
175  err:
176     return (0);
177 }
178
179 /*
180  * print session id and master key in NSS keylog format (RSA
181  * Session-ID:<session id> Master-Key:<master key>)
182  */
183 int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
184 {
185     size_t i;
186
187     if (x == NULL)
188         goto err;
189     if (x->session_id_length == 0 || x->master_key_length == 0)
190         goto err;
191
192     /*
193      * the RSA prefix is required by the format's definition although there's
194      * nothing RSA-specific in the output, therefore, we don't have to check if
195      * the cipher suite is based on RSA
196      */
197     if (BIO_puts(bp, "RSA ") <= 0)
198         goto err;
199
200     if (BIO_puts(bp, "Session-ID:") <= 0)
201         goto err;
202     for (i = 0; i < x->session_id_length; i++) {
203         if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
204             goto err;
205     }
206     if (BIO_puts(bp, " Master-Key:") <= 0)
207         goto err;
208     for (i = 0; i < x->master_key_length; i++) {
209         if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
210             goto err;
211     }
212     if (BIO_puts(bp, "\n") <= 0)
213         goto err;
214
215     return (1);
216  err:
217     return (0);
218 }