1 /*	$NetBSD: tls_proxy_context_print.c,v 1.2 2020/03/18 19:05:21 christos Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	tls_proxy_context_print
6 /* SUMMARY
7 /*	write TLS_ATTR_STATE structure to stream
8 /* SYNOPSIS
9 /*	#include <tls_proxy.h>
10 /*
11 /*	int     tls_proxy_context_print(print_fn, stream, flags, ptr)
12 /*	ATTR_PRINT_MASTER_FN print_fn;
13 /*	VSTREAM *stream;
14 /*	int     flags;
15 /*	void    *ptr;
16 /* DESCRIPTION
17 /*	tls_proxy_context_print() writes the public members of a
18 /*	TLS_ATTR_STATE structure to the named stream using the
19 /*	specified attribute print routine. tls_proxy_context_print()
20 /*	is meant to be passed as a call-back to attr_print(), thusly:
21 /*
22 /*	... SEND_ATTR_FUNC(tls_proxy_context_print, (void *) tls_context), ...
23 /* DIAGNOSTICS
24 /*	Fatal: out of memory.
25 /* LICENSE
26 /* .ad
27 /* .fi
28 /*	The Secure Mailer license must be distributed with this software.
29 /* AUTHOR(S)
30 /*	Wietse Venema
31 /*	IBM T.J. Watson Research
32 /*	P.O. Box 704
33 /*	Yorktown Heights, NY 10598, USA
34 /*
35 /*	Wietse Venema
36 /*	Google, Inc.
37 /*	111 8th Avenue
38 /*	New York, NY 10011, USA
39 /*--*/
40 
41 #ifdef USE_TLS
42 
43 /* System library. */
44 
45 #include <sys_defs.h>
46 
47 /* Utility library */
48 
49 #include <attr.h>
50 
51 /* TLS library. */
52 
53 #include <tls.h>
54 #include <tls_proxy.h>
55 
56 /* tls_proxy_context_print - send TLS session state over stream */
57 
58 int     tls_proxy_context_print(ATTR_PRINT_MASTER_FN print_fn, VSTREAM *fp,
59 				        int flags, void *ptr)
60 {
61     TLS_SESS_STATE *tp = (TLS_SESS_STATE *) ptr;
62     int     ret;
63 
64 #define STRING_OR_EMPTY(s) ((s) ? (s) : "")
65 
66     ret = print_fn(fp, flags | ATTR_FLAG_MORE,
67 		   SEND_ATTR_STR(TLS_ATTR_PEER_CN,
68 				 STRING_OR_EMPTY(tp->peer_CN)),
69 		   SEND_ATTR_STR(TLS_ATTR_ISSUER_CN,
70 				 STRING_OR_EMPTY(tp->issuer_CN)),
71 		   SEND_ATTR_STR(TLS_ATTR_PEER_CERT_FPT,
72 				 STRING_OR_EMPTY(tp->peer_cert_fprint)),
73 		   SEND_ATTR_STR(TLS_ATTR_PEER_PKEY_FPT,
74 				 STRING_OR_EMPTY(tp->peer_pkey_fprint)),
75 		   SEND_ATTR_INT(TLS_ATTR_PEER_STATUS,
76 				 tp->peer_status),
77 		   SEND_ATTR_STR(TLS_ATTR_CIPHER_PROTOCOL,
78 				 STRING_OR_EMPTY(tp->protocol)),
79 		   SEND_ATTR_STR(TLS_ATTR_CIPHER_NAME,
80 				 STRING_OR_EMPTY(tp->cipher_name)),
81 		   SEND_ATTR_INT(TLS_ATTR_CIPHER_USEBITS,
82 				 tp->cipher_usebits),
83 		   SEND_ATTR_INT(TLS_ATTR_CIPHER_ALGBITS,
84 				 tp->cipher_algbits),
85 		   SEND_ATTR_STR(TLS_ATTR_KEX_NAME,
86 				 STRING_OR_EMPTY(tp->kex_name)),
87 		   SEND_ATTR_STR(TLS_ATTR_KEX_CURVE,
88 				 STRING_OR_EMPTY(tp->kex_curve)),
89 		   SEND_ATTR_INT(TLS_ATTR_KEX_BITS,
90 				 tp->kex_bits),
91 		   SEND_ATTR_STR(TLS_ATTR_CLNT_SIG_NAME,
92 				 STRING_OR_EMPTY(tp->clnt_sig_name)),
93 		   SEND_ATTR_STR(TLS_ATTR_CLNT_SIG_CURVE,
94 				 STRING_OR_EMPTY(tp->clnt_sig_curve)),
95 		   SEND_ATTR_INT(TLS_ATTR_CLNT_SIG_BITS,
96 				 tp->clnt_sig_bits),
97 		   SEND_ATTR_STR(TLS_ATTR_CLNT_SIG_DGST,
98 				 STRING_OR_EMPTY(tp->clnt_sig_dgst)),
99 		   SEND_ATTR_STR(TLS_ATTR_SRVR_SIG_NAME,
100 				 STRING_OR_EMPTY(tp->srvr_sig_name)),
101 		   SEND_ATTR_STR(TLS_ATTR_SRVR_SIG_CURVE,
102 				 STRING_OR_EMPTY(tp->srvr_sig_curve)),
103 		   SEND_ATTR_INT(TLS_ATTR_SRVR_SIG_BITS,
104 				 tp->srvr_sig_bits),
105 		   SEND_ATTR_STR(TLS_ATTR_SRVR_SIG_DGST,
106 				 STRING_OR_EMPTY(tp->srvr_sig_dgst)),
107 		   SEND_ATTR_STR(TLS_ATTR_NAMADDR,
108 				 STRING_OR_EMPTY(tp->namaddr)),
109 		   ATTR_TYPE_END);
110     /* Do not flush the stream. */
111     return (ret);
112 }
113 
114 #endif
115