aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssl/test/ssl_cipher_SUITE.erl
blob: ea1d9dc90ca1cf13bdb1a9622e4c4a37bc770431 (plain) (blame)
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2008-2012. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

-module(ssl_cipher_SUITE).

%% Note: This directive should only be used in test suites.
-compile(export_all).

-include_lib("common_test/include/ct.hrl").

-include("ssl_internal.hrl").
-include("ssl_record.hrl").
-include("ssl_cipher.hrl").
-include("ssl_alert.hrl").

-define(TIMEOUT, 600000).

%% Test server callback functions
%%--------------------------------------------------------------------
%% Function: init_per_suite(Config) -> Config
%% Config - [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Description: Initialization before the whole suite
%%
%% Note: This function is free to add any key/value pairs to the Config
%% variable, but should NOT alter/remove any existing entries.
%%--------------------------------------------------------------------
init_per_suite(Config) ->
    try crypto:start() of
	ok ->
	    Config
    catch _:_  ->
	    {skip, "Crypto did not start"}
    end.
%%--------------------------------------------------------------------
%% Function: end_per_suite(Config) -> _
%% Config - [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Description: Cleanup after the whole suite
%%--------------------------------------------------------------------
end_per_suite(_Config) ->
    ssl:stop(),
    application:stop(crypto).

%%--------------------------------------------------------------------
%% Function: init_per_testcase(TestCase, Config) -> Config
%% Case - atom()
%%   Name of the test case that is about to be run.
%% Config - [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%%
%% Description: Initialization before each test case
%%
%% Note: This function is free to add any key/value pairs to the Config
%% variable, but should NOT alter/remove any existing entries.
%% Description: Initialization before each test case
%%--------------------------------------------------------------------
init_per_testcase(_TestCase, Config0) ->
    Config = lists:keydelete(watchdog, 1, Config0),
    Dog = ssl_test_lib:timetrap(?TIMEOUT),
    [{watchdog, Dog} | Config].

%%--------------------------------------------------------------------
%% Function: end_per_testcase(TestCase, Config) -> _
%% Case - atom()
%%   Name of the test case that is about to be run.
%% Config - [tuple()]
%%   A list of key/value pairs, holding the test case configuration.
%% Description: Cleanup after each test case
%%--------------------------------------------------------------------
end_per_testcase(_TestCase, Config) ->
    Dog = ?config(watchdog, Config),
    case Dog of 
	undefined ->
	    ok;
	_ ->
	    test_server:timetrap_cancel(Dog)
    end.

%%--------------------------------------------------------------------
%% Function: all(Clause) -> TestCases
%% Clause - atom() - suite | doc
%% TestCases - [Case] 
%% Case - atom()
%%   Name of a test case.
%% Description: Returns a list of all test cases in this test suite
%%--------------------------------------------------------------------
suite() -> [{ct_hooks,[ts_install_cth]}].

all() -> 
    [aes_decipher_good, aes_decipher_good_tls11, aes_decipher_fail, aes_decipher_fail_tls11].

groups() -> 
    [].

init_per_group(_GroupName, Config) ->
    Config.

end_per_group(_GroupName, Config) ->
    Config.


%% Test cases starts here.
%%--------------------------------------------------------------------
aes_decipher_good(doc) -> 
    ["Decipher a known cryptotext."];

aes_decipher_good(suite) -> 
    [];

aes_decipher_good(Config) when is_list(Config) ->
    HashSz = 32,
    CipherState = #cipher_state{iv = <<59,201,85,117,188,206,224,136,5,109,46,70,104,79,4,9>>,
				key = <<72,196,247,97,62,213,222,109,210,204,217,186,172,184,197,148>>},
    Fragment = <<220,193,179,139,171,33,143,245,202,47,123,251,13,232,114,8,
		 190,162,74,31,186,227,119,155,94,74,119,79,169,193,240,160,
		 198,181,81,19,98,162,213,228,74,224,253,168,156,59,195,122,
		 108,101,107,242,20,15,169,150,163,107,101,94,93,104,241,165>>,
    Content = <<183,139,16,132,10,209,67,86,168,100,61,217,145,57,36,56, "HELLO\n">>,
    Mac = <<71,136,212,107,223,200,70,232,127,116,148,205,232,35,158,113,237,174,15,217,192,168,35,8,6,107,107,233,25,174,90,111>>,
    Version = {3,0},
    {Content, Mac, _} = ssl_cipher:decipher(?AES, HashSz, CipherState, Fragment, Version),
    Version1 = {3,1},
    {Content, Mac, _} = ssl_cipher:decipher(?AES, HashSz, CipherState, Fragment, Version1),
    ok.

%%--------------------------------------------------------------------

aes_decipher_good_tls11(doc) ->
    ["Decipher a known TLS 1.1 cryptotext."];

aes_decipher_good_tls11(suite) ->
    [];

%% the fragment is actuall a TLS 1.1 record, with
%% Version = TLS 1.1, we get the correct NextIV in #cipher_state
aes_decipher_good_tls11(Config) when is_list(Config) ->
    HashSz = 32,
    CipherState = #cipher_state{iv = <<59,201,85,117,188,206,224,136,5,109,46,70,104,79,4,9>>,
				key = <<72,196,247,97,62,213,222,109,210,204,217,186,172,184,197,148>>},
    Fragment = <<220,193,179,139,171,33,143,245,202,47,123,251,13,232,114,8,
		 190,162,74,31,186,227,119,155,94,74,119,79,169,193,240,160,
		 198,181,81,19,98,162,213,228,74,224,253,168,156,59,195,122,
		 108,101,107,242,20,15,169,150,163,107,101,94,93,104,241,165>>,
    Content = <<"HELLO\n">>,
    NextIV = <<183,139,16,132,10,209,67,86,168,100,61,217,145,57,36,56>>,
    Mac = <<71,136,212,107,223,200,70,232,127,116,148,205,232,35,158,113,237,174,15,217,192,168,35,8,6,107,107,233,25,174,90,111>>,
    Version = {3,2},
    {Content, Mac, #cipher_state{iv = NextIV}} = ssl_cipher:decipher(?AES, HashSz, CipherState, Fragment, Version),
    Version1 = {3,2},
    {Content, Mac, #cipher_state{iv = NextIV}} = ssl_cipher:decipher(?AES, HashSz, CipherState, Fragment, Version1),
    ok.

%%--------------------------------------------------------------------

aes_decipher_fail(doc) -> 
    ["Decipher a known cryptotext."];

aes_decipher_fail(suite) -> 
    [];

%% same as above, last byte of key replaced
aes_decipher_fail(Config) when is_list(Config) ->
    HashSz = 32,
    CipherState = #cipher_state{iv = <<59,201,85,117,188,206,224,136,5,109,46,70,104,79,4,9>>,
				key = <<72,196,247,97,62,213,222,109,210,204,217,186,172,184,197,254>>},
    Fragment = <<220,193,179,139,171,33,143,245,202,47,123,251,13,232,114,8,
		 190,162,74,31,186,227,119,155,94,74,119,79,169,193,240,160,
		 198,181,81,19,98,162,213,228,74,224,253,168,156,59,195,122,
		 108,101,107,242,20,15,169,150,163,107,101,94,93,104,241,165>>,
    Version = {3,0},
    {Content, Mac, _} = ssl_cipher:decipher(?AES, HashSz, CipherState, Fragment, Version),
    32 = byte_size(Content),
    32 = byte_size(Mac),
    Version1 = {3,1},
    {Content1, Mac1, _} = ssl_cipher:decipher(?AES, HashSz, CipherState, Fragment, Version1),
    32 = byte_size(Content1),
    32 = byte_size(Mac1),
    ok.

%%--------------------------------------------------------------------

aes_decipher_fail_tls11(doc) ->
    ["Decipher a known TLS 1.1 cryptotext."];

aes_decipher_fail_tls11(suite) ->
    [];

%% same as above, last byte of key replaced
%% stricter padding checks in TLS 1.1 mean we get an alert instead
aes_decipher_fail_tls11(Config) when is_list(Config) ->
    HashSz = 32,
    CipherState = #cipher_state{iv = <<59,201,85,117,188,206,224,136,5,109,46,70,104,79,4,9>>,
				key = <<72,196,247,97,62,213,222,109,210,204,217,186,172,184,197,254>>},
    Fragment = <<220,193,179,139,171,33,143,245,202,47,123,251,13,232,114,8,
		 190,162,74,31,186,227,119,155,94,74,119,79,169,193,240,160,
		 198,181,81,19,98,162,213,228,74,224,253,168,156,59,195,122,
		 108,101,107,242,20,15,169,150,163,107,101,94,93,104,241,165>>,
    Version = {3,2},
    #alert{level = ?FATAL, description = ?BAD_RECORD_MAC} = ssl_cipher:decipher(?AES, HashSz, CipherState, Fragment, Version),
    Version1 = {3,3},
    #alert{level = ?FATAL, description = ?BAD_RECORD_MAC} = ssl_cipher:decipher(?AES, HashSz, CipherState, Fragment, Version1),
    ok.

%%--------------------------------------------------------------------