ssl_txt.c revision 296341
159093Sdfr/* ssl/ssl_txt.c */
2121129Sdfr/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
359093Sdfr * All rights reserved.
459093Sdfr *
559093Sdfr * This package is an SSL implementation written
659093Sdfr * by Eric Young (eay@cryptsoft.com).
759093Sdfr * The implementation was written so as to conform with Netscapes SSL.
859093Sdfr *
959093Sdfr * This library is free for commercial and non-commercial use as long as
1059093Sdfr * the following conditions are aheared to.  The following conditions
1159093Sdfr * apply to all code found in this distribution, be it the RC4, RSA,
1259093Sdfr * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1359093Sdfr * included with this distribution is covered by the same copyright terms
1459093Sdfr * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1559093Sdfr *
1659093Sdfr * Copyright remains Eric Young's, and as such any Copyright notices in
1759093Sdfr * the code are not to be removed.
1859093Sdfr * If this package is used in a product, Eric Young should be given attribution
1959093Sdfr * as the author of the parts of the library used.
2059093Sdfr * This can be in the form of a textual message at program startup or
2159093Sdfr * in documentation (online or textual) provided with the package.
2259093Sdfr *
2359093Sdfr * Redistribution and use in source and binary forms, with or without
2459093Sdfr * modification, are permitted provided that the following conditions
2559093Sdfr * are met:
2659093Sdfr * 1. Redistributions of source code must retain the copyright
27116182Sobrien *    notice, this list of conditions and the following disclaimer.
28116182Sobrien * 2. Redistributions in binary form must reproduce the above copyright
29116182Sobrien *    notice, this list of conditions and the following disclaimer in the
3059093Sdfr *    documentation and/or other materials provided with the distribution.
31121129Sdfr * 3. All advertising materials mentioning features or use of this software
32121129Sdfr *    must display the following acknowledgement:
33121129Sdfr *    "This product includes cryptographic software written by
3459093Sdfr *     Eric Young (eay@cryptsoft.com)"
35121129Sdfr *    The word 'cryptographic' can be left out if the rouines from the library
36118921Scg *    being used are not cryptographic related :-).
3759093Sdfr * 4. If you include any Windows specific code (or a derivative thereof) from
3859093Sdfr *    the apps directory (application code) you must include an acknowledgement:
3959093Sdfr *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4059093Sdfr *
4159093Sdfr * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4259093Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4359093Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4459093Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4559093Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4659093Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4759093Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4859093Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4998105Skbyanc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5098105Skbyanc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5159093Sdfr * SUCH DAMAGE.
5298105Skbyanc *
53118921Scg * The licence and distribution terms for any publically available version or
5498105Skbyanc * derivative of this code cannot be changed.  i.e. this code cannot simply be
55118921Scg * copied and put under another distribution licence
5659093Sdfr * [including the GNU Public Licence.]
5759093Sdfr */
5859093Sdfr/* ====================================================================
59121129Sdfr * Copyright 2005 Nokia. All rights reserved.
60148811Sgrehan *
6159093Sdfr * The portions of the attached software ("Contribution") is developed by
6259093Sdfr * Nokia Corporation and is licensed pursuant to the OpenSSL open source
63227711Smarius * license.
64227711Smarius *
65227711Smarius * The Contribution, originally written by Mika Kousa and Pasi Eronen of
66186347Snwhitehorn * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
67217326Smdf * support (see RFC 4279) to OpenSSL.
68118921Scg *
69118921Scg * No patent licenses or other rights except those expressly stated in
70121129Sdfr * the OpenSSL open source license shall be deemed granted or received
71121129Sdfr * expressly, by implication, estoppel, or otherwise.
72121129Sdfr *
73148811Sgrehan * No assurances are provided by Nokia that the Contribution does not
74148811Sgrehan * infringe the patent or other intellectual property rights of any third
75148811Sgrehan * party or that the license provides you with all the necessary rights
76148811Sgrehan * to make use of the Contribution.
77121129Sdfr *
78121129Sdfr * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
79121129Sdfr * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
80121129Sdfr * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
81121129Sdfr * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
82121129Sdfr * OTHERWISE.
83121129Sdfr */
84121129Sdfr
85121129Sdfr#include <stdio.h>
86121129Sdfr#include <openssl/buffer.h>
87121129Sdfr#include "ssl_locl.h"
88121129Sdfr
89121129Sdfr#ifndef OPENSSL_NO_FP_API
90121129Sdfrint SSL_SESSION_print_fp(FILE *fp, const SSL_SESSION *x)
9159093Sdfr{
9259093Sdfr    BIO *b;
93121129Sdfr    int ret;
9459093Sdfr
9559093Sdfr    if ((b = BIO_new(BIO_s_file_internal())) == NULL) {
9659093Sdfr        SSLerr(SSL_F_SSL_SESSION_PRINT_FP, ERR_R_BUF_LIB);
9759093Sdfr        return (0);
9865173Sdfr    }
9959093Sdfr    BIO_set_fp(b, fp, BIO_NOCLOSE);
10059093Sdfr    ret = SSL_SESSION_print(b, x);
10159093Sdfr    BIO_free(b);
10259093Sdfr    return (ret);
10359093Sdfr}
10459093Sdfr#endif
10559093Sdfr
10659093Sdfrint SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
10759093Sdfr{
10859093Sdfr    unsigned int i;
10959093Sdfr    const char *s;
11059093Sdfr
11159093Sdfr    if (x == NULL)
112227711Smarius        goto err;
113227711Smarius    if (BIO_puts(bp, "SSL-Session:\n") <= 0)
114227711Smarius        goto err;
115227711Smarius    if (x->ssl_version == SSL2_VERSION)
11659093Sdfr        s = "SSLv2";
11759093Sdfr    else if (x->ssl_version == SSL3_VERSION)
11865173Sdfr        s = "SSLv3";
11959093Sdfr    else if (x->ssl_version == TLS1_2_VERSION)
120121129Sdfr        s = "TLSv1.2";
121121129Sdfr    else if (x->ssl_version == TLS1_1_VERSION)
12259093Sdfr        s = "TLSv1.1";
12359093Sdfr    else if (x->ssl_version == TLS1_VERSION)
12459093Sdfr        s = "TLSv1";
12559093Sdfr    else if (x->ssl_version == DTLS1_VERSION)
12659093Sdfr        s = "DTLSv1";
12765173Sdfr    else if (x->ssl_version == DTLS1_BAD_VER)
12865173Sdfr        s = "DTLSv1-bad";
12965173Sdfr    else
13065173Sdfr        s = "unknown";
131186347Snwhitehorn    if (BIO_printf(bp, "    Protocol  : %s\n", s) <= 0)
132121129Sdfr        goto err;
13365173Sdfr
13465173Sdfr    if (x->cipher == NULL) {
13565173Sdfr        if (((x->cipher_id) & 0xff000000) == 0x02000000) {
13665173Sdfr            if (BIO_printf
13765173Sdfr                (bp, "    Cipher    : %06lX\n", x->cipher_id & 0xffffff) <= 0)
138227711Smarius                goto err;
139121129Sdfr        } else {
140186347Snwhitehorn            if (BIO_printf
141121129Sdfr                (bp, "    Cipher    : %04lX\n", x->cipher_id & 0xffff) <= 0)
142121129Sdfr                goto err;
143121129Sdfr        }
144121129Sdfr    } else {
145121129Sdfr        if (BIO_printf
146121129Sdfr            (bp, "    Cipher    : %s\n",
147121129Sdfr             ((x->cipher == NULL) ? "unknown" : x->cipher->name)) <= 0)
148186347Snwhitehorn            goto err;
149121129Sdfr    }
150121129Sdfr    if (BIO_puts(bp, "    Session-ID: ") <= 0)
151121129Sdfr        goto err;
152121129Sdfr    for (i = 0; i < x->session_id_length; i++) {
15365173Sdfr        if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
154186347Snwhitehorn            goto err;
15565173Sdfr    }
15665173Sdfr    if (BIO_puts(bp, "\n    Session-ID-ctx: ") <= 0)
15765173Sdfr        goto err;
15865173Sdfr    for (i = 0; i < x->sid_ctx_length; i++) {
15965173Sdfr        if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
160121129Sdfr            goto err;
161227711Smarius    }
162227711Smarius    if (BIO_puts(bp, "\n    Master-Key: ") <= 0)
163121129Sdfr        goto err;
16465173Sdfr    for (i = 0; i < (unsigned int)x->master_key_length; i++) {
16565173Sdfr        if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
16665173Sdfr            goto err;
16765173Sdfr    }
16865173Sdfr    if (BIO_puts(bp, "\n    Key-Arg   : ") <= 0)
16965173Sdfr        goto err;
17065173Sdfr    if (x->key_arg_length == 0) {
171121129Sdfr        if (BIO_puts(bp, "None") <= 0)
172121129Sdfr            goto err;
17359093Sdfr    } else
174121129Sdfr        for (i = 0; i < x->key_arg_length; i++) {
175121129Sdfr            if (BIO_printf(bp, "%02X", x->key_arg[i]) <= 0)
176121129Sdfr                goto err;
177121129Sdfr        }
178121129Sdfr#ifndef OPENSSL_NO_KRB5
179121129Sdfr    if (BIO_puts(bp, "\n    Krb5 Principal: ") <= 0)
18059093Sdfr        goto err;
18159093Sdfr    if (x->krb5_client_princ_len == 0) {
182121129Sdfr        if (BIO_puts(bp, "None") <= 0)
183188063Simp            goto err;
18459093Sdfr    } else
18559093Sdfr        for (i = 0; i < x->krb5_client_princ_len; i++) {
186121129Sdfr            if (BIO_printf(bp, "%02X", x->krb5_client_princ[i]) <= 0)
187121129Sdfr                goto err;
188121129Sdfr        }
189121129Sdfr#endif                          /* OPENSSL_NO_KRB5 */
190121129Sdfr#ifndef OPENSSL_NO_PSK
191121129Sdfr    if (BIO_puts(bp, "\n    PSK identity: ") <= 0)
192121129Sdfr        goto err;
193121129Sdfr    if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0)
194121129Sdfr        goto err;
195121129Sdfr    if (BIO_puts(bp, "\n    PSK identity hint: ") <= 0)
196121129Sdfr        goto err;
197121129Sdfr    if (BIO_printf
198121129Sdfr        (bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0)
199121129Sdfr        goto err;
200121129Sdfr#endif
201121129Sdfr#ifndef OPENSSL_NO_SRP
202121129Sdfr    if (BIO_puts(bp, "\n    SRP username: ") <= 0)
203121129Sdfr        goto err;
204121129Sdfr    if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0)
205121129Sdfr        goto err;
206188063Simp#endif
207121129Sdfr#ifndef OPENSSL_NO_TLSEXT
208121129Sdfr    if (x->tlsext_tick_lifetime_hint) {
209121129Sdfr        if (BIO_printf(bp,
210121129Sdfr                       "\n    TLS session ticket lifetime hint: %ld (seconds)",
211121129Sdfr                       x->tlsext_tick_lifetime_hint) <= 0)
212121129Sdfr            goto err;
213121129Sdfr    }
214121129Sdfr    if (x->tlsext_tick) {
215121129Sdfr        if (BIO_puts(bp, "\n    TLS session ticket:\n") <= 0)
216121129Sdfr            goto err;
217121129Sdfr        if (BIO_dump_indent(bp, (char *)x->tlsext_tick, x->tlsext_ticklen, 4)
218121129Sdfr            <= 0)
219121129Sdfr            goto err;
220121129Sdfr    }
221121129Sdfr#endif
222153844Sjhb
223121129Sdfr#ifndef OPENSSL_NO_COMP
224121129Sdfr    if (x->compress_meth != 0) {
225121129Sdfr        SSL_COMP *comp = NULL;
226121129Sdfr
227121129Sdfr        ssl_cipher_get_evp(x, NULL, NULL, NULL, NULL, &comp);
228121129Sdfr        if (comp == NULL) {
229121129Sdfr            if (BIO_printf(bp, "\n    Compression: %d", x->compress_meth) <=
230121129Sdfr                0)
231121129Sdfr                goto err;
23259093Sdfr        } else {
23359093Sdfr            if (BIO_printf
23459093Sdfr                (bp, "\n    Compression: %d (%s)", comp->id,
235188063Simp                 comp->method->name) <= 0)
23659093Sdfr                goto err;
237186347Snwhitehorn        }
238186347Snwhitehorn    }
239121129Sdfr#endif
24059093Sdfr    if (x->time != 0L) {
241121129Sdfr        if (BIO_printf(bp, "\n    Start Time: %ld", x->time) <= 0)
242121129Sdfr            goto err;
24359093Sdfr    }
244121129Sdfr    if (x->timeout != 0L) {
245121129Sdfr        if (BIO_printf(bp, "\n    Timeout   : %ld (sec)", x->timeout) <= 0)
246227711Smarius            goto err;
247227711Smarius    }
248121129Sdfr    if (BIO_puts(bp, "\n") <= 0)
24959093Sdfr        goto err;
250121129Sdfr
251121129Sdfr    if (BIO_puts(bp, "    Verify return code: ") <= 0)
252121129Sdfr        goto err;
253121129Sdfr    if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
254188063Simp                   X509_verify_cert_error_string(x->verify_result)) <= 0)
255121129Sdfr        goto err;
256121129Sdfr
257186347Snwhitehorn    return (1);
258121129Sdfr err:
259121129Sdfr    return (0);
260121129Sdfr}
26159093Sdfr