aboutsummaryrefslogtreecommitdiff
path: root/tcl_tests/ssl.try
blob: 5bc50872cea482e1fb36f706482f241c5ae7cfc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/tclsh

lappend auto_path [file dirname [info script]]
package require ossltest
if {$argc==2} {
	switch -exact -- [lindex $argv 0]  {
	-serverconf {
		set test::server_conf [lindex $argv 1]
		set test::suffix "-clt"
	} 
	-clientconf {
		set test::client_conf [lindex $argv 1]
		set test::suffix "-srv"
	}
	default {
		puts stderr "invalid command line"
		exit 1;
	}
	}
} elseif $argc {
	puts stderr "invalid command line"
	exit 1
}	

array set protos {
	TLSv1 -tls1
	TLSv1.1 -tls1_1
	TLSv1.2 -tls1_2
}

cd $::test::dir

start_tests "Тесты на SSL-соединение между s_client и s_server"

if {[info exists env(ALG_LIST)]} {
	set alg_list $env(ALG_LIST)
} else {
	switch -exact [engine_name] {
		"ccore" {set alg_list {rsa:1024 gost2001:XA gost2012_256:XA gost2012_512:A}}
		"open" {set alg_list {rsa:1024 gost2001:XA gost2012_256:XA gost2012_512:A}}
	}
}

array set suites {
rsa:1024 {ECDHE-RSA-AES256-SHA@SECLEVEL=0}
gost2001:XA {GOST2001-GOST89-GOST89@SECLEVEL=1 GOST2001-NULL-GOST94@SECLEVEL=0 LEGACY-GOST2012-GOST8912-GOST8912@SECLEVEL=1 IANA-GOST2012-GOST8912-GOST8912@SECLEVEL=1 GOST2012-NULL-GOST12@SECLEVEL=0}
gost2012_256:XA {LEGACY-GOST2012-GOST8912-GOST8912@SECLEVEL=1 GOST2012-NULL-GOST12@SECLEVEL=0}
gost2012_512:A {LEGACY-GOST2012-GOST8912-GOST8912@SECLEVEL=1 GOST2012-NULL-GOST12@SECLEVEL=0}
}

#
# Incompatible cipher suites
#
array set badsuites {
gost2012_256:XA {GOST2001-GOST89-GOST89@SECLEVEL=1 GOST2001-NULL-GOST94@SECLEVEL=0} 
gost2012_512:A {GOST2001-GOST89-GOST89@SECLEVEL=1 GOST2001-NULL-GOST94@SECLEVEL=0}
}

#
# Default cipher suite negotiated for algorithm
#
array set defsuite {
rsa:1024 ECDHE-RSA-AES256-SHA@SECLEVEL=1
#gost94:XA GOST94-GOST89-GOST89
gost2001:XA GOST2012-GOST8912-GOST8912@SECLEVEL=1
gost2012_256:XA LEGACY-GOST2012-GOST8912-GOST8912@SECLEVEL=1
gost2012_512:A LEGACY-GOST2012-GOST8912-GOST8912@SECLEVEL=1
}

array set defsuite_12 {
rsa:1024 ECDHE-RSA-AES256-GCM-SHA384@SECLEVEL=1
#gost94:XA GOST94-GOST89-GOST89
gost2001:XA LEGACY-GOST2012-GOST8912-GOST8912@SECLEVEL=1
gost2012_256:XA GOST2012-MAGMA-MAGMAOMAC@SECLEVEL=1
gost2012_512:A GOST2012-MAGMA-MAGMAOMAC@SECLEVEL=1
}

set proto_list {"TLSv1" "TLSv1.1" "TLSv1.2"}

if {![file exists sslCA/cacert.pem]} {
	makeCA sslCA rsa
} else {
	set ::test::ca sslCA	
}

test -skip {[file exist localhost_rsa/cert.pem]} \
	"Создаем серверный сертификат rsa" {
	makeRegisteredUser localhost_rsa rsa:1024 CN localhost
} 0 1

foreach alg $alg_list {
	set alg_fn [string map {":" "_"} $alg]
	
	test -skip {[file exist localhost_$alg_fn/cert.pem]} \
		"Создаем серверный сертификат $alg" {
		makeRegisteredUser localhost_$alg_fn $alg CN localhost OU $alg_fn
	} 0 1

	test -skip {[file exists ssl_user_$alg_fn/cert.pem]} \
		"Создаем клиентский сертификат $alg" {
		makeRegisteredUser ssl_user_$alg_fn $alg CN ssl_user OU $alg_fn
	} 0 1
}

foreach alg {gost2001:B gost2012_256:B gost2012_512:B} {
	set alg_fn [string map {":" "_"} $alg]
	test -skip {[file exists ssl_user_$alg_fn/cert.pem]} \
		"Создаем клиентский сертификат $alg" {
		makeRegisteredUser ssl_user_$alg_fn $alg CN ssl_user OU $alg_fn
	} 0 1
}


foreach proto $proto_list {
	foreach alg $alg_list {
		set alg_fn [string map {":" "_"} $alg]

		if {[string match *2012* $alg]} {
			foreach suite $badsuites($alg) {

				test "Incompatible suite $alg $suite $proto" {
					set list [client_server [list -connect localhost:4433 \
						-CAfile $::test::ca/cacert.pem \
						-verify_return_error -verify 1 -state -cipher $suite] \
						[list -www -cert localhost_$alg_fn/cert.pem \
						-key localhost_$alg_fn/seckey.pem -cipher $suite \
						$protos($proto)] {}] 
					list [lindex $list 2] [grep "^New," [lindex $list 0]]
				} 0 [list 1 "New, (NONE), Cipher is (NONE)\n"]
			}
		}

		foreach suite $suites($alg) {
			set raw_name [lindex [split $suite @] 0]

			if {![string equal $proto "TLSv1.2"] && [string match *OMAC* $suite]} {
				continue
			}

			if {[string equal $proto "TLSv1.2"] && [string match *OMAC* $suite]} {
				set expected_proto "TLSv1.2"
			} else {
				set expected_proto "TLSv1.0"
			}

			test "Запуск сервера $suite $proto" {
				set f [open_server [list -cert localhost_$alg_fn/cert.pem \
					-key localhost_$alg_fn/seckey.pem -cipher $suite \
					$protos($proto)]]
				stop_server $f
				foreach {out err status} [stop $f] break	
				log "SERVER OUTPUT:\n$out\n----"
				log "SERVER STDERR:\n$err\n----"
				log "SERVER EXIT CODE: $status"
				grep "ACCEPT" $out
			} 0 "ACCEPT\n"
			log $errorInfo
			flush [test_log]

			test "Корректный хэндшейк $suite $proto" {
				set list [client_server [list -connect localhost:4433 \
					-CAfile $::test::ca/cacert.pem -verify_return_error \
					-verify 1 -state -cipher $suite ] \
					[list -www -cert localhost_$alg_fn/cert.pem \
					-key localhost_$alg_fn/seckey.pem \
					-cipher $suite $protos($proto)] {}] 
				if {[regexp -lineanchor \
				{^\s*Protocol\s*:\s*(\S*)\s*$.*^\s*Cipher\s*:\s*(\S*)\s*$} \
				[lindex $list 0] -> result_proto result_cipher]} {
					list [lindex $list 2] $result_proto $result_cipher
				} else {
					lindex $list 1
				}	
			} 0 [list 0 $proto $raw_name]


			test "Несовпадающий шиферсьют DHE-RSA-AES256-SHA $proto" {
				set list [client_server [list -connect localhost:4433 \
					-CAfile $::test::ca/cacert.pem -verify_return_error \
					-verify 1 -state -cipher $suite] \
					[list -www -cert localhost_$alg_fn/cert.pem \
					-key localhost_$alg_fn/seckey.pem \
					-cipher DHE-RSA-AES256-SHA@SECLEVEL=1 $protos($proto)] {}] 
				list [lindex $list 2] [grep ":fatal:" [lindex $list 1]]
			} 0 [list 1 "SSL3 alert read:fatal:handshake failure
"]

			test "Получение странички $suite $proto" {
				set list [client_server [list -connect localhost:4433 \
					-CAfile $::test::ca/cacert.pem -verify_return_error \
					-verify 1 -state -cipher $suite -ign_eof] \
					[list -www -cert localhost_$alg_fn/cert.pem \
					-key localhost_$alg_fn/seckey.pem -cipher $suite \
					$protos($proto)] "GET /\n\n"] 
				grep "^New," [lindex $list 0]
			} 0 "New, $expected_proto, Cipher is $raw_name\nNew, $expected_proto, Cipher is $raw_name\n"

			if {![string match "*-NULL-*" $suite]} {

				test "Сервер поддерживающий много шиферсьютов $proto" {
					set list [client_server [list -connect localhost:4433 \
						-CAfile $::test::ca/cacert.pem -verify_return_error \
						-verify 1 -state -cipher $suite] \
						[list -www -cert localhost_$alg_fn/cert.pem \
						-key localhost_$alg_fn/seckey.pem $protos($proto) -cipher ALL@SECLEVEL=0] {}] 
					if {[regexp -lineanchor \
					{^\s*Protocol\s*:\s*(\S*)\s*$.*^\s*Cipher\s*:\s*(\S*)\s*$} \
					[lindex $list 0] -> result_proto result_cipher]} {
						list [lindex $list 2] $result_proto $result_cipher
					} else {
						lindex $list 1
					}	
				} 0 [list 0 $proto $raw_name]


				test "Сервер c несколькими алгоритмами, клиент $suite $proto" {
					set list [client_server [list -connect localhost:4433 \
						-CAfile $::test::ca/cacert.pem -verify_return_error \
						-verify 1 -state -cipher $suite] \
						[list -www -cert localhost_rsa/cert.pem \
						-key localhost_rsa/seckey.pem \
						-dcert localhost_$alg_fn/cert.pem \
						-dkey localhost_$alg_fn/seckey.pem $protos($proto) -cipher ALL@SECLEVEL=0] {}] 
					if {[regexp -lineanchor \
					{^\s*Protocol\s*:\s*(\S*)\s*$.*^\s*Cipher\s*:\s*(\S*)\s*$} \
					[lindex $list 0] -> result_proto result_cipher]} {
						list [lindex $list 2] $result_proto $result_cipher
					} else {
						lindex $list 1
					}	
				} 0 [list 0 $proto $raw_name]

			}

			test "Сервер c несколькими алгоритмами, клиент AES256-SHA $proto" {
				set list [client_server [list -connect localhost:4433 \
					-CAfile $::test::ca/cacert.pem -verify_return_error \
					-verify 1 -state -cipher AES256-SHA@SECLEVEL=0] \
					[list -www -cert localhost_rsa/cert.pem \
					-key localhost_rsa/seckey.pem \
					-dcert localhost_$alg_fn/cert.pem \
					-dkey localhost_$alg_fn/seckey.pem $protos($proto) -cipher ALL@SECLEVEL=0] {}] 
				if {[regexp -lineanchor \
				{^\s*Protocol\s*:\s*(\S*)\s*$.*^\s*Cipher\s*:\s*(\S*)\s*$} \
				[lindex $list 0] -> result_proto result_cipher]} {
					list [lindex $list 2] $result_proto $result_cipher
				} else {
					lindex $list 1
				}	
			} 0 [list 0 $proto AES256-SHA]



			if {[string match *gost* $alg]} {
				set alg_cli_list [list $alg gost2001:B gost2012_256:B gost2012_512:B]
			} else {
				set alg_cli_list $alg
			}

			foreach alg_cli $alg_cli_list { 
				set alg_cli_fn [string map {":" "_"} $alg_cli]

				test "Сервер $alg, клиент с сертификатом $alg_cli $proto" {
					set list [client_server [list -connect localhost:4433\
						-CAfile $::test::ca/cacert.pem -verify_return_error \
						-verify 1 -state -cert ssl_user_$alg_cli_fn/cert.pem \
						-key ssl_user_$alg_cli_fn/seckey.pem -cipher $suite \
						-ign_eof]\
						[list -cert localhost_$alg_fn/cert.pem \
						-key localhost_$alg_fn/seckey.pem -verify_return_error\
						-Verify 3 -www -CAfile $::test::ca/cacert.pem \
						-cipher $suite $protos($proto)] "GET /\n"]
					list [lindex $list 2] [grep "^New," [lindex $list 0]]
				} 0 [list 0 [string repeat "New, $expected_proto, Cipher is $raw_name\n" 2]]

			}

		}

		if {[string equal $proto "TLSv1.2"]} {
			set etalon $defsuite_12($alg)
		} else {
			set etalon $defsuite($alg)
		}

		if {[string equal $proto "TLSv1.2"] && ![string match *2001* $alg]} {
			set expected_proto "TLSv1.2"
		} else {
			set expected_proto "TLSv1.0"
		}
if {0} {
		test "Умолчательный хендшейк с ключами $alg $proto" {
			set list [client_server [list -connect localhost:4433\
				-CAfile $::test::ca/cacert.pem -verify_return_error -verify 1\
				-state -ign_eof]\
				[list -www -cert localhost_$alg_fn/cert.pem\
				-key localhost_$alg_fn/seckey.pem $protos($proto)] "GET /\n"]
			if {[regexp -lineanchor \
			{^\s*Protocol\s*:\s*(\S*)\s*$.*^\s*Cipher\s*:\s*(\S*)\s*$} \
			[lindex $list 0] -> result_proto result_cipher]} {
				list [lindex $list 2] $result_proto $result_cipher
			} else {
				lindex $list 1
			}	
		} 0 [list 0 $proto $etalon]

		test "Умолчательный хендшейк с клиентской аутентификацией $alg $proto" {
			set list [client_server [list -connect localhost:4433\
				-CAfile $::test::ca/cacert.pem -verify_return_error \
				-verify 1 -state -cert ssl_user_$alg_fn/cert.pem \
				-key ssl_user_$alg_fn/seckey.pem -ign_eof]\
				[list -cert localhost_$alg_fn/cert.pem \
				-key localhost_$alg_fn/seckey.pem -verify_return_error\
				-Verify 3 -www -CAfile $::test::ca/cacert.pem $protos($proto)] \
				"GET /\n"]
			list [lindex $list 2] [grep "^New," [lindex $list 0]]
		} 0 [list 0 [string repeat "New, $expected_proto, Cipher is $etalon\n" 2]]
}; # if {0}
	}
}

end_tests