xref: /openbsd-src/lib/libssl/ssl_txt.c (revision f4fe6251b363bc47c99c75caa60c829516bf905e)
1*f4fe6251Sjsing /* $OpenBSD: ssl_txt.c,v 1.39 2024/07/22 14:47:15 jsing Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker  * All rights reserved.
45b37fcf3Sryker  *
55b37fcf3Sryker  * This package is an SSL implementation written
65b37fcf3Sryker  * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker  * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker  *
95b37fcf3Sryker  * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker  * the following conditions are aheared to.  The following conditions
115b37fcf3Sryker  * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135b37fcf3Sryker  * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker  *
165b37fcf3Sryker  * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker  * the code are not to be removed.
185b37fcf3Sryker  * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker  * as the author of the parts of the library used.
205b37fcf3Sryker  * This can be in the form of a textual message at program startup or
215b37fcf3Sryker  * in documentation (online or textual) provided with the package.
225b37fcf3Sryker  *
235b37fcf3Sryker  * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker  * modification, are permitted provided that the following conditions
255b37fcf3Sryker  * are met:
265b37fcf3Sryker  * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker  * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker  *    documentation and/or other materials provided with the distribution.
315b37fcf3Sryker  * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker  *    must display the following acknowledgement:
335b37fcf3Sryker  *    "This product includes cryptographic software written by
345b37fcf3Sryker  *     Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker  *    The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker  *    being used are not cryptographic related :-).
375b37fcf3Sryker  * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker  *    the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker  *
415b37fcf3Sryker  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker  * SUCH DAMAGE.
525b37fcf3Sryker  *
535b37fcf3Sryker  * The licence and distribution terms for any publically available version or
545b37fcf3Sryker  * derivative of this code cannot be changed.  i.e. this code cannot simply be
555b37fcf3Sryker  * copied and put under another distribution licence
565b37fcf3Sryker  * [including the GNU Public Licence.]
575b37fcf3Sryker  */
580a5d6edeSdjm /* ====================================================================
590a5d6edeSdjm  * Copyright 2005 Nokia. All rights reserved.
600a5d6edeSdjm  *
610a5d6edeSdjm  * The portions of the attached software ("Contribution") is developed by
620a5d6edeSdjm  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
630a5d6edeSdjm  * license.
640a5d6edeSdjm  *
650a5d6edeSdjm  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
660a5d6edeSdjm  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
670a5d6edeSdjm  * support (see RFC 4279) to OpenSSL.
680a5d6edeSdjm  *
690a5d6edeSdjm  * No patent licenses or other rights except those expressly stated in
700a5d6edeSdjm  * the OpenSSL open source license shall be deemed granted or received
710a5d6edeSdjm  * expressly, by implication, estoppel, or otherwise.
720a5d6edeSdjm  *
730a5d6edeSdjm  * No assurances are provided by Nokia that the Contribution does not
740a5d6edeSdjm  * infringe the patent or other intellectual property rights of any third
750a5d6edeSdjm  * party or that the license provides you with all the necessary rights
760a5d6edeSdjm  * to make use of the Contribution.
770a5d6edeSdjm  *
780a5d6edeSdjm  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
790a5d6edeSdjm  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
800a5d6edeSdjm  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
810a5d6edeSdjm  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
820a5d6edeSdjm  * OTHERWISE.
830a5d6edeSdjm  */
845b37fcf3Sryker 
855b37fcf3Sryker #include <stdio.h>
86c5899dbcSjsing 
87913ec974Sbeck #include <openssl/buffer.h>
88c5899dbcSjsing 
89c9675a23Stb #include "ssl_local.h"
905b37fcf3Sryker 
9135fb0677Sjsing int
9235fb0677Sjsing SSL_SESSION_print_fp(FILE *fp, const SSL_SESSION *x)
935b37fcf3Sryker {
945b37fcf3Sryker 	BIO *b;
955b37fcf3Sryker 	int ret;
965b37fcf3Sryker 
97590ebd80Stb 	if ((b = BIO_new(BIO_s_file())) == NULL) {
98c9d7abb7Sbeck 		SSLerrorx(ERR_R_BUF_LIB);
995508d27eStb 		return 0;
1005b37fcf3Sryker 	}
1015b37fcf3Sryker 	BIO_set_fp(b, fp, BIO_NOCLOSE);
1025b37fcf3Sryker 	ret = SSL_SESSION_print(b, x);
1035b37fcf3Sryker 	BIO_free(b);
1045508d27eStb 	return ret;
1055b37fcf3Sryker }
10671e04849Sbeck LSSL_ALIAS(SSL_SESSION_print_fp);
1075b37fcf3Sryker 
10835fb0677Sjsing int
10935fb0677Sjsing SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
1105b37fcf3Sryker {
111387303bbSjsing 	const SSL_CIPHER *cipher;
1127e174615Stb 	size_t i;
1135508d27eStb 	int ret = 0;
1145b37fcf3Sryker 
11535fb0677Sjsing 	if (x == NULL)
11635fb0677Sjsing 		goto err;
1175508d27eStb 
11847f9dd97Slteo 	if (BIO_puts(bp, "SSL-Session:\n") <= 0)
11947f9dd97Slteo 		goto err;
120f6f8cf51Sjsing 
1215508d27eStb 	if (BIO_printf(bp, "    Protocol  : %s\n",
1225508d27eStb 	    ssl_version_string(x->ssl_version)) <= 0)
12347f9dd97Slteo 		goto err;
1245b37fcf3Sryker 
125*f4fe6251Sjsing 	if ((cipher = ssl3_get_cipher_by_value(x->cipher_value)) == NULL) {
126*f4fe6251Sjsing 		if (BIO_printf(bp, "    Cipher    : %04X\n",
127*f4fe6251Sjsing 		    x->cipher_value) <= 0)
128c109e398Sbeck 			goto err;
12935fb0677Sjsing 	} else {
1305508d27eStb 		const char *cipher_name = "unknown";
1315508d27eStb 
132387303bbSjsing 		if (cipher->name != NULL)
133387303bbSjsing 			cipher_name = cipher->name;
1345508d27eStb 
1355508d27eStb 		if (BIO_printf(bp, "    Cipher    : %s\n", cipher_name) <= 0)
136c109e398Sbeck 			goto err;
137c109e398Sbeck 	}
1385508d27eStb 
13947f9dd97Slteo 	if (BIO_puts(bp, "    Session-ID: ") <= 0)
14047f9dd97Slteo 		goto err;
1415508d27eStb 
14235fb0677Sjsing 	for (i = 0; i < x->session_id_length; i++) {
14347f9dd97Slteo 		if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
14447f9dd97Slteo 			goto err;
1455b37fcf3Sryker 	}
1465508d27eStb 
14747f9dd97Slteo 	if (BIO_puts(bp, "\n    Session-ID-ctx: ") <= 0)
14847f9dd97Slteo 		goto err;
1495508d27eStb 
15035fb0677Sjsing 	for (i = 0; i < x->sid_ctx_length; i++) {
151c109e398Sbeck 		if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
152c109e398Sbeck 			goto err;
153913ec974Sbeck 	}
1545508d27eStb 
15547f9dd97Slteo 	if (BIO_puts(bp, "\n    Master-Key: ") <= 0)
15647f9dd97Slteo 		goto err;
1575508d27eStb 
158c794ac0bStb 	for (i = 0; i < x->master_key_length; i++) {
15947f9dd97Slteo 		if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
16047f9dd97Slteo 			goto err;
1615b37fcf3Sryker 	}
1625508d27eStb 
1635508d27eStb 	if (x->tlsext_tick_lifetime_hint > 0) {
1644fcf65c5Sdjm 		if (BIO_printf(bp,
16518631d23Sjsing 		    "\n    TLS session ticket lifetime hint: %u (seconds)",
1664fcf65c5Sdjm 		    x->tlsext_tick_lifetime_hint) <= 0)
1674fcf65c5Sdjm 			goto err;
1684fcf65c5Sdjm 	}
1695508d27eStb 
1705508d27eStb 	if (x->tlsext_tick != NULL) {
17147f9dd97Slteo 		if (BIO_puts(bp, "\n    TLS session ticket:\n") <= 0)
17247f9dd97Slteo 			goto err;
1735508d27eStb 		if (BIO_dump_indent(bp, x->tlsext_tick, x->tlsext_ticklen,
1745508d27eStb 		    4) <= 0)
1754fcf65c5Sdjm 			goto err;
1764fcf65c5Sdjm 	}
1770a5d6edeSdjm 
178aa8444d1Sguenther 	if (x->time != 0) {
1795508d27eStb 		if (BIO_printf(bp, "\n    Start Time: %lld",
1805508d27eStb 		    (long long)x->time) <= 0)
18147f9dd97Slteo 			goto err;
1825b37fcf3Sryker 	}
1835508d27eStb 
1845508d27eStb 	if (x->timeout != 0) {
1855508d27eStb 		if (BIO_printf(bp, "\n    Timeout   : %ld (sec)",
1865508d27eStb 		    x->timeout) <= 0)
18747f9dd97Slteo 			goto err;
1885b37fcf3Sryker 	}
1895508d27eStb 
19047f9dd97Slteo 	if (BIO_puts(bp, "\n") <= 0)
19147f9dd97Slteo 		goto err;
192ba5406e9Sbeck 
1935508d27eStb 	if (BIO_printf(bp, "    Verify return code: %ld (%s)\n",
1945508d27eStb 	    x->verify_result,
19547f9dd97Slteo 	    X509_verify_cert_error_string(x->verify_result)) <= 0)
19647f9dd97Slteo 		goto err;
1975b37fcf3Sryker 
1985508d27eStb 	ret = 1;
1995b37fcf3Sryker  err:
2005508d27eStb 	return ret;
2015b37fcf3Sryker }
20271e04849Sbeck LSSL_ALIAS(SSL_SESSION_print);
203