xref: /titanic_51/usr/src/uts/common/inet/kssl/ksslproto.h (revision 9b1bd49f83497d7b339a684a1a76de3aaccf5269)
1c28749e9Skais /*
2c28749e9Skais  * CDDL HEADER START
3c28749e9Skais  *
4c28749e9Skais  * The contents of this file are subject to the terms of the
52bd70d4bSkrishna  * Common Development and Distribution License (the "License").
62bd70d4bSkrishna  * You may not use this file except in compliance with the License.
7c28749e9Skais  *
8c28749e9Skais  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9c28749e9Skais  * or http://www.opensolaris.org/os/licensing.
10c28749e9Skais  * See the License for the specific language governing permissions
11c28749e9Skais  * and limitations under the License.
12c28749e9Skais  *
13c28749e9Skais  * When distributing Covered Code, include this CDDL HEADER in each
14c28749e9Skais  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15c28749e9Skais  * If applicable, add the following below this CDDL HEADER, with the
16c28749e9Skais  * fields enclosed by brackets "[]" replaced with your own identifying
17c28749e9Skais  * information: Portions Copyright [yyyy] [name of copyright owner]
18c28749e9Skais  *
19c28749e9Skais  * CDDL HEADER END
20c28749e9Skais  */
21c28749e9Skais /*
2211d0a659SVladimir Kotal  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23c28749e9Skais  */
24c28749e9Skais 
25c28749e9Skais #ifndef	_INET_KSSL_KSSLPROTO_H
26c28749e9Skais #define	_INET_KSSL_KSSLPROTO_H
27c28749e9Skais 
28c28749e9Skais #ifdef	__cplusplus
29c28749e9Skais extern "C" {
30c28749e9Skais #endif
31c28749e9Skais 
32c28749e9Skais #include <sys/types.h>
33c28749e9Skais #include <sys/stream.h>
34c28749e9Skais #include <sys/md5.h>
35c28749e9Skais #include <sys/sha1.h>
36c28749e9Skais #include <sys/crypto/common.h>
37c28749e9Skais #include <sys/crypto/api.h>
38c28749e9Skais #include <inet/kssl/kssl.h>	/* Cipher suite definitions */
39c28749e9Skais #include <inet/kssl/ksslapi.h>
40c28749e9Skais #include <inet/kssl/ksslimpl.h>
41c28749e9Skais 
42c28749e9Skais #define	SSL3_RANDOM_LENGTH		32
43c28749e9Skais #define	SSL3_SESSIONID_BYTES		32
44c28749e9Skais #define	SSL3_HDR_LEN			5
45*9b1bd49fSVladimir Kotal #define	SSL3_ALERT_LEN			2
46c28749e9Skais #define	SSL3_MAX_RECORD_LENGTH		16384
47c28749e9Skais #define	SSL3_PRE_MASTER_SECRET_LEN	48
48c28749e9Skais #define	SSL3_MASTER_SECRET_LEN		48
49c28749e9Skais #define	SSL3_MD5_PAD_LEN		48
50c28749e9Skais #define	SSL3_SHA1_PAD_LEN		40
51c28749e9Skais 
52c28749e9Skais #define	SSL_MIN_CHALLENGE_BYTES		16
53c28749e9Skais #define	SSL_MAX_CHALLENGE_BYTES		32
54c28749e9Skais 
55c28749e9Skais #define	SHA1_HASH_LEN			20
56c28749e9Skais #define	MD5_HASH_LEN			16
57c28749e9Skais #define	MAX_HASH_LEN			SHA1_HASH_LEN
58c28749e9Skais 
59c28749e9Skais #define	KSSL_READ			0
60c28749e9Skais #define	KSSL_WRITE			1
61c28749e9Skais 
62c28749e9Skais #define	KSSL_ENCRYPT			0
63c28749e9Skais #define	KSSL_DECRYPT			1
64c28749e9Skais 
65c28749e9Skais #define	MSG_INIT			0
66c28749e9Skais #define	MSG_INIT_LEN			1
67c28749e9Skais #define	MSG_BODY			2
68c28749e9Skais 
692bd70d4bSkrishna /*
702bd70d4bSkrishna  * More than enough for the cipher suite that needs the
712bd70d4bSkrishna  * largest key material (AES_256_CBC_SHA needs 136 bytes).
722bd70d4bSkrishna  */
732bd70d4bSkrishna #define	MAX_KEYBLOCK_LENGTH		160
74c28749e9Skais 
75c28749e9Skais #define	TLS_MASTER_SECRET_LABEL		"master secret"
76c28749e9Skais #define	TLS_CLIENT_WRITE_KEY_LABEL	"client write key"
77c28749e9Skais #define	TLS_SERVER_WRITE_KEY_LABEL	"server write key"
78c28749e9Skais #define	TLS_CLIENT_FINISHED_LABEL	"client finished"
79c28749e9Skais #define	TLS_SERVER_FINISHED_LABEL	"server finished"
80c28749e9Skais #define	TLS_KEY_EXPANSION_LABEL		"key expansion"
81c28749e9Skais #define	TLS_IV_BLOCK_LABEL		"IV block"
82c28749e9Skais #define	TLS_MAX_LABEL_SIZE		24
83c28749e9Skais 
84c28749e9Skais #define	TLS_FINISHED_SIZE		12
85c28749e9Skais 
86c28749e9Skais /*
87c28749e9Skais  * The following constants try to insure an input buffer is optimally aligned
88c28749e9Skais  * for MAC hash computation.  SHA1/MD5 code prefers 4 byte alignment of each
89c28749e9Skais  * 64byte input block to avoid a copy. Our goal is to reach 4 byte alignment
90c28749e9Skais  * starting form the 3rd MAC block (input buffer starts in the 3rd block). The
91c28749e9Skais  * 3rd block includes the first 53 (MD5 SSL3 MAC) or 57 (SHA1 SSL3 MAC) bytes
92c28749e9Skais  * of the input buffer. This means input buffer should start at offset 3
93c28749e9Skais  * within a 4 byte word so that its next block is 4 byte aligned. Since the
94c28749e9Skais  * SSL3 record header is 5 bytes long it should start at at offset 2 within a
95c28749e9Skais  * 4 byte word. To insure the next record (for buffers that don't fit into 1
96c28749e9Skais  * SSL3 record) also starts at offset 2 within a 4 byte word the previous
97c28749e9Skais  * record length should be 3 mod 8 since 5 + 3 mod 8 is 0 i.e. the next record
98c28749e9Skais  * starts at the same offset within a 4 byte word as the the previous record.
99c28749e9Skais  */
100c28749e9Skais #define	SSL3_MAX_OPTIMAL_RECORD_LENGTH	(SSL3_MAX_RECORD_LENGTH - 1)
101c28749e9Skais #define	SSL3_OPTIMAL_RECORD_ALIGNMENT	2
102c28749e9Skais 
103c28749e9Skais /* session state */
104c28749e9Skais typedef struct sslSessionIDStr {
105c28749e9Skais 	uchar_t		session_id[SSL3_SESSIONID_BYTES];
106c28749e9Skais 	uchar_t 	master_secret[SSL3_MASTER_SECRET_LEN];
107c28749e9Skais 	clock_t 	time;
1082ec7cc7fSKrishna Yenduri 	in6_addr_t 	client_addr;
109c28749e9Skais 	boolean_t	cached;
110c28749e9Skais 	uint16_t	cipher_suite;
111c28749e9Skais } sslSessionID;
112c28749e9Skais 
113c28749e9Skais /* An element of the session cache */
114c28749e9Skais typedef struct kssl_sid_ent {
115c28749e9Skais 	kmutex_t	se_lock;
116c28749e9Skais 	uint64_t	se_used;	/* Counter to check hash distribution */
117c28749e9Skais 	sslSessionID	se_sid;
118c28749e9Skais } kssl_sid_ent_t;
119c28749e9Skais 
120c28749e9Skais typedef enum {
121c28749e9Skais 	content_change_cipher_spec	= 20,
122c28749e9Skais 	content_alert			= 21,
123c28749e9Skais 	content_handshake		= 22,
124c28749e9Skais 	content_application_data	= 23,
125c28749e9Skais 	content_handshake_v2		= 128
126c28749e9Skais } SSL3ContentType;
127c28749e9Skais 
128c28749e9Skais typedef enum {
129c28749e9Skais 	hello_request		= 0,
130c28749e9Skais 	client_hello		= 1,
131c28749e9Skais 	server_hello		= 2,
132c28749e9Skais 	certificate		= 11,
133c28749e9Skais 	server_key_exchange	= 12,
134c28749e9Skais 	certificate_request	= 13,
135c28749e9Skais 	server_hello_done	= 14,
136c28749e9Skais 	certificate_verify	= 15,
137c28749e9Skais 	client_key_exchange	= 16,
138c28749e9Skais 	finished		= 20
139c28749e9Skais } SSL3HandshakeType;
140c28749e9Skais 
141c28749e9Skais typedef struct SSL3HandshakeMsgStr {
142c28749e9Skais 	int			state;
143c28749e9Skais 	SSL3HandshakeType	type;
144c28749e9Skais 	int			msglen;
145c28749e9Skais 	int			msglen_bytes;
146c28749e9Skais 	mblk_t			*head;
147c28749e9Skais 	mblk_t			*tail;
148c28749e9Skais } SSL3HandshakeMsg;
149c28749e9Skais 
150c28749e9Skais typedef struct KSSLJOBStr {
151c28749e9Skais 	struct ssl_s	*ssl;
152c28749e9Skais 	crypto_req_id_t	kjob;
153c28749e9Skais 	char		*buf;
154c28749e9Skais 	size_t		buflen;
155c28749e9Skais 	int		status;
156c28749e9Skais } KSSLJOB;
157c28749e9Skais 
158c28749e9Skais 
159c28749e9Skais typedef struct {
160c28749e9Skais 	uchar_t md5[MD5_HASH_LEN];
161c28749e9Skais 	uchar_t sha1[SHA1_HASH_LEN];
162c28749e9Skais 	uchar_t tlshash[TLS_FINISHED_SIZE];
163c28749e9Skais } SSL3Hashes;
164c28749e9Skais 
165c28749e9Skais typedef enum {
166c28749e9Skais 	close_notify		= 0,
167c28749e9Skais 	unexpected_message	= 10,
168c28749e9Skais 	bad_record_mac		= 20,
169c28749e9Skais 	decompression_failure	= 30,
170c28749e9Skais 	handshake_failure	= 40,
171c28749e9Skais 	no_certificate		= 41,
172c28749e9Skais 	bad_certificate		= 42,
173c28749e9Skais 	unsupported_certificate	= 43,
174c28749e9Skais 	certificate_revoked	= 44,
175c28749e9Skais 	certificate_expired	= 45,
176c28749e9Skais 	certificate_unknown	= 46,
177c28749e9Skais 	illegal_parameter	= 47,
178c28749e9Skais 	unknown_ca		= 48,
179c28749e9Skais 	access_denied		= 49,
180c28749e9Skais 	decode_error		= 50,
181c28749e9Skais 	decrypt_error		= 51,
182c28749e9Skais 	export_restriction	= 60,
183c28749e9Skais 	protocol_version	= 70,
184c28749e9Skais 	insufficient_security	= 71,
185c28749e9Skais 	internal_error		= 80,
186c28749e9Skais 	user_canceled		= 90,
187c28749e9Skais 	no_renegotiation	= 100
188c28749e9Skais } SSL3AlertDescription;
189c28749e9Skais 
190c28749e9Skais typedef enum {
191c28749e9Skais 	alert_warning = 1,
192c28749e9Skais 	alert_fatal = 2
193c28749e9Skais } SSL3AlertLevel;
194c28749e9Skais 
195c28749e9Skais typedef enum {
196c28749e9Skais 	wait_client_hello = 0,
197c28749e9Skais 	wait_client_key = 1,
198c28749e9Skais 	wait_client_key_done = 2,
199c28749e9Skais 	wait_change_cipher = 3,
200c28749e9Skais 	wait_finished = 4,
201c28749e9Skais 	idle_handshake = 5
202c28749e9Skais } SSL3WaitState;
203c28749e9Skais 
204c28749e9Skais typedef enum {
205c28749e9Skais 	sender_client = 0x434c4e54,
206c28749e9Skais 	sender_server = 0x53525652
207c28749e9Skais } SSL3Sender;
208c28749e9Skais 
209c28749e9Skais typedef enum {
210c28749e9Skais 	mac_md5	= 0,
211c28749e9Skais 	mac_sha	= 1
212c28749e9Skais } SSL3MACAlgorithm;
213c28749e9Skais 
214c28749e9Skais /* The SSL bulk cipher definition */
215c28749e9Skais typedef enum {
216c28749e9Skais 	cipher_null = 0,
217c28749e9Skais 	cipher_rc4 = 1,
218c28749e9Skais 	cipher_des = 2,
2192bd70d4bSkrishna 	cipher_3des = 3,
2202bd70d4bSkrishna 	cipher_aes128 = 4,
2212bd70d4bSkrishna 	cipher_aes256 = 5,
222c28749e9Skais } SSL3BulkCipher;
223c28749e9Skais 
224c28749e9Skais typedef enum { type_stream = 0, type_block = 1 } CipherType;
225c28749e9Skais 
226c28749e9Skais typedef struct ssl3CipherSuiteDefStr {
227c28749e9Skais 	uint16_t		suite;
228c28749e9Skais 	SSL3BulkCipher		calg;
229c28749e9Skais 	SSL3MACAlgorithm	malg;
230c28749e9Skais 	int			keyblksz;
231c28749e9Skais } ssl3CipherSuiteDef;
232c28749e9Skais 
233c28749e9Skais typedef void (*hashinit_func_t)(void *);
234c28749e9Skais typedef void (*hashupdate_func_t)(void *, uchar_t *, uint32_t);
235c28749e9Skais typedef void (*hashfinal_func_t)(uchar_t *, void *);
236c28749e9Skais 
237c28749e9Skais typedef struct KSSLMACDefStr {
238c28749e9Skais 	int			hashsz;
239c28749e9Skais 	int			padsz;
240c28749e9Skais 	hashinit_func_t		HashInit;
241c28749e9Skais 	hashupdate_func_t	HashUpdate;
242c28749e9Skais 	hashfinal_func_t	HashFinal;
243c28749e9Skais } KSSLMACDef;
244c28749e9Skais 
245c28749e9Skais typedef struct KSSLCipherDefStr {
246c28749e9Skais 	CipherType		type;
247c28749e9Skais 	int			bsize;
248c28749e9Skais 	int			keysz;
249c28749e9Skais 	crypto_mech_type_t	mech_type;
250c28749e9Skais } KSSLCipherDef;
251c28749e9Skais 
252c28749e9Skais typedef union KSSL_HASHCTXUnion {
253c28749e9Skais 	SHA1_CTX	sha;
254c28749e9Skais 	MD5_CTX		md5;
255c28749e9Skais } KSSL_HASHCTX;
256c28749e9Skais 
257c28749e9Skais typedef struct KSSLCipherSpecStr {
258c28749e9Skais 	int		mac_hashsz;
259c28749e9Skais 	int		mac_padsz;
260c28749e9Skais 	void		(*MAC_HashInit)(void *);
261c28749e9Skais 	void		(*MAC_HashUpdate)(void *, uchar_t *, uint32_t);
262c28749e9Skais 	void		(*MAC_HashFinal)(uchar_t *, void *);
263c28749e9Skais 
264c28749e9Skais 	CipherType	cipher_type;
265c28749e9Skais 	int		cipher_bsize;
266c28749e9Skais 	int		cipher_keysz;
267c28749e9Skais 
268c28749e9Skais 	crypto_mechanism_t	cipher_mech;
269c28749e9Skais 	crypto_mechanism_t	hmac_mech;	/* for TLS */
270c28749e9Skais 	crypto_key_t		cipher_key;
271c28749e9Skais 	crypto_key_t		hmac_key;	/* for TLS */
272c28749e9Skais 
273c28749e9Skais 	crypto_context_t	cipher_ctx;
274c28749e9Skais 	crypto_data_t		cipher_data;
275c28749e9Skais 
276c28749e9Skais } KSSLCipherSpec;
277c28749e9Skais 
278c28749e9Skais /*
279dd49f125SAnders Persson  * SSL connection state. This one hangs off of a ksslf_t structure.
280c28749e9Skais  */
281c28749e9Skais typedef struct ssl_s {
282c28749e9Skais 	kmutex_t		kssl_lock;
283c28749e9Skais 	struct kssl_entry_s	*kssl_entry;
284c28749e9Skais 	mblk_t			*rec_ass_head;
285c28749e9Skais 	mblk_t			*rec_ass_tail;
2862ec7cc7fSKrishna Yenduri 	in6_addr_t		faddr;
287c28749e9Skais 	uint32_t		tcp_mss;
288c28749e9Skais 	SSL3WaitState		hs_waitstate;
289c28749e9Skais 	boolean_t		resumed;
290*9b1bd49fSVladimir Kotal 	boolean_t		close_notify_clnt;
291*9b1bd49fSVladimir Kotal 	boolean_t		close_notify_srvr;
292c28749e9Skais 	boolean_t		fatal_alert;
293c28749e9Skais 	boolean_t		fatal_error;
294c28749e9Skais 	boolean_t		alert_sent;
295c28749e9Skais 	boolean_t		appdata_sent;
296c28749e9Skais 	boolean_t		activeinput;
297c28749e9Skais 	SSL3AlertLevel		sendalert_level;
298c28749e9Skais 	SSL3AlertDescription	sendalert_desc;
299c28749e9Skais 	mblk_t			*handshake_sendbuf;
300c28749e9Skais 	mblk_t			*alert_sendbuf;
301c28749e9Skais 	kssl_callback_t		cke_callback_func;
302c28749e9Skais 	void			*cke_callback_arg;
303c28749e9Skais 	uint16_t		pending_cipher_suite;
304c28749e9Skais 	SSL3MACAlgorithm	pending_malg;
305c28749e9Skais 	SSL3BulkCipher		pending_calg;
306c28749e9Skais 	int			pending_keyblksz;
307c28749e9Skais 	uint64_t		seq_num[2];
308c28749e9Skais 	SSL3HandshakeMsg	msg;
309c28749e9Skais 	KSSLJOB			job;
310c28749e9Skais 	KSSLCipherSpec		spec[2];
311c28749e9Skais 	uchar_t			pending_keyblock[MAX_KEYBLOCK_LENGTH];
312c28749e9Skais 	uchar_t			mac_secret[2][MAX_HASH_LEN];
313c28749e9Skais 	KSSL_HASHCTX		mac_ctx[2][2];	/* inner 'n outer per dir */
314c28749e9Skais 	sslSessionID		sid;
315c28749e9Skais 	SHA1_CTX		hs_sha1;
316c28749e9Skais 	MD5_CTX			hs_md5;
317c28749e9Skais 	SSL3Hashes		hs_hashes;
318c28749e9Skais 	uchar_t			client_random[SSL3_RANDOM_LENGTH];
319c28749e9Skais 	uchar_t			server_random[SSL3_RANDOM_LENGTH];
320c28749e9Skais 	int			sslcnt;
321c28749e9Skais 	uchar_t			major_version;
322c28749e9Skais 	uchar_t			minor_version;
32311d0a659SVladimir Kotal 	boolean_t		secure_renegotiation;
324dd49f125SAnders Persson 	uint_t			async_ops_pending;
325dd49f125SAnders Persson 	kcondvar_t		async_cv;
326c28749e9Skais } ssl_t;
327c28749e9Skais 
328c28749e9Skais #define	IS_TLS(s) (s->major_version == 3 && s->minor_version == 1)
329c28749e9Skais 
330c28749e9Skais #define	SSL3_REC_SIZE(mp)	(uint8_t *)(mp)->b_rptr + 3
331c28749e9Skais 
332c28749e9Skais extern int kssl_spec_init(ssl_t *, int);
333c28749e9Skais extern void kssl_send_alert(ssl_t *, SSL3AlertLevel, SSL3AlertDescription);
334c28749e9Skais 
335c28749e9Skais #ifdef	__cplusplus
336c28749e9Skais }
337c28749e9Skais #endif
338c28749e9Skais 
339c28749e9Skais #endif	/* _INET_KSSL_KSSLPROTO_H */
340