Debian Codegraph @

crt/s2n/tests/unit/s2n_rsa_pss_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "crypto/s2n_rsa_pss.h"
17
18 #include "crypto/s2n_certificate.h"
19 #include "crypto/s2n_dhe.h"
20 #include "s2n_test.h"
21 #include "stuffer/s2n_stuffer.h"
22 #include "testlib/s2n_testlib.h"
23 #include "tls/s2n_config.h"
24 #include "tls/s2n_connection.h"
25 #include "utils/s2n_random.h"
26
27 int s2n_flip_random_bit(struct s2n_blob *blob)
28 {
29 /* Flip a random bit in the blob */
30 uint64_t byte_flip_pos = 0;
31 POSIX_GUARD_RESULT(s2n_public_random(blob->size, &byte_flip_pos));
32 uint64_t bit_flip_pos = 0;
33 POSIX_GUARD_RESULT(s2n_public_random(8, &bit_flip_pos));
34
35 uint8_t mask = 0x01 << (uint8_t) bit_flip_pos;
36 blob->data[byte_flip_pos] ^= mask;
37
38 return 0;
39 }
40
41 int main(int argc, char **argv)
42 {
43 BEGIN_TEST();
44 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
45
46 /* Don't use RSA-PSS certs if unsupported */
47 #if !RSA_PSS_CERTS_SUPPORTED
48 EXPECT_FALSE(s2n_is_rsa_pss_certs_supported());
49 END_TEST();
50 #endif
51 EXPECT_TRUE(s2n_is_rsa_pss_certs_supported());
52
53 /* Check that s2n_is_rsa_pss_certs_supported() is a superset of s2n_is_rsa_pss_signing_supported() */
54 EXPECT_TRUE(s2n_is_rsa_pss_signing_supported());
55
56 /* Positive Test: Ensure we can sign and verify a randomly generated signature.
57 * Pseudocode: assert(SUCCESS == verify(Key1_public, message, sign(Key1_private, message)))
58 */
59 {
60 struct s2n_config *server_config = NULL;
61 char *cert_chain_pem = NULL;
62 char *private_key_pem = NULL;
63 struct s2n_cert_chain_and_key *chain_and_key = NULL;
64 struct s2n_pkey public_key = { 0 };
65 s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
66
67 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
68 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
69 EXPECT_NOT_NULL(server_config = s2n_config_new());
70
71 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
72 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
73 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
74
75 /* Load the Private Key */
76 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
77
78 /* Load the Public Key */
79 EXPECT_OK(s2n_asn1der_to_public_key_and_type(&public_key, &pkey_type, &chain_and_key->cert_chain->head->raw));
80 EXPECT_EQUAL(pkey_type, S2N_PKEY_TYPE_RSA_PSS);
81
82 /* Sign and Verify a Random Value to ensure that Public and Private Key Matches */
83 EXPECT_SUCCESS(s2n_pkey_match(&public_key, chain_and_key->private_key));
84
85 /* Release Resources */
86 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
87 EXPECT_SUCCESS(s2n_config_free(server_config));
88 EXPECT_SUCCESS(s2n_pkey_free(&public_key));
89 free(cert_chain_pem);
90 free(private_key_pem);
91
92 /* Verify repeated key frees.
93 * (Later calls should be a no-op) */
94 EXPECT_SUCCESS(s2n_pkey_free(&public_key));
95 };
96
97 /* Negative Test: Loading mismatching RSA PSS Public/Private Keys will fail.
98 * Pseudocode: assert(FAILURE == load_pem_pair(Key1_public, Key2_private))
99 */
100 {
101 struct s2n_config *server_config = NULL;
102 char *leaf_cert_chain_pem = NULL;
103 char *root_private_key_pem = NULL;
104 struct s2n_cert_chain_and_key *misconfigured_chain_and_key = NULL;
105 struct s2n_pkey public_key = { 0 };
106
107 EXPECT_NOT_NULL(leaf_cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
108 EXPECT_NOT_NULL(root_private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
109 EXPECT_NOT_NULL(server_config = s2n_config_new());
110
111 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, leaf_cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
112
113 /* Incorrectly reading the CA's Private Key from disk, not the Leaf's Private Key */
114 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_KEY, root_private_key_pem, S2N_MAX_TEST_PEM_SIZE));
115 EXPECT_NOT_NULL(misconfigured_chain_and_key = s2n_cert_chain_and_key_new());
116
117 /* Attempting to Load RSA_PSS Certificate with wrong RSA_PSS Key should fail */
118 EXPECT_FAILURE(s2n_cert_chain_and_key_load_pem(misconfigured_chain_and_key, leaf_cert_chain_pem, root_private_key_pem));
119
120 /* Release Resources */
121 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(misconfigured_chain_and_key));
122 EXPECT_SUCCESS(s2n_config_free(server_config));
123 EXPECT_SUCCESS(s2n_pkey_free(&public_key));
124 free(leaf_cert_chain_pem);
125 free(root_private_key_pem);
126 };
127
128 /* Negative Test: Ensure flipping a bit in the signature is rejected
129 * Pseudocode: assert(FAILURE == verify(Key1_public, message, bitflip(sign(Key1_private, message)))
130 */
131 {
132 struct s2n_config *server_config = NULL;
133 char *cert_chain_pem = NULL;
134 char *private_key_pem = NULL;
135 struct s2n_cert_chain_and_key *chain_and_key = NULL;
136 struct s2n_pkey public_key = { 0 };
137 s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
138
139 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
140 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
141 EXPECT_NOT_NULL(server_config = s2n_config_new());
142
143 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
144 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
145 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
146
147 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_cert_chain(chain_and_key, cert_chain_pem));
148 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_private_key(chain_and_key, private_key_pem));
149
150 /* Parse the leaf cert for the public key and certificate type */
151 EXPECT_OK(s2n_asn1der_to_public_key_and_type(&public_key, &pkey_type, &chain_and_key->cert_chain->head->raw));
152 EXPECT_NOT_EQUAL(pkey_type, S2N_PKEY_TYPE_UNKNOWN);
153 EXPECT_SUCCESS(s2n_cert_set_cert_type(chain_and_key->cert_chain->head, pkey_type));
154
155 struct s2n_pkey *private_key = chain_and_key->private_key;
156 {
157 EXPECT_NOT_NULL(public_key.pkey);
158 EXPECT_NOT_NULL(private_key);
159 EXPECT_NOT_NULL(private_key->pkey);
160
161 /* Generate a random blob to sign and verify */
162 s2n_stack_blob(random_msg, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE);
163 EXPECT_OK(s2n_get_private_random_data(&random_msg));
164
165 /* Sign/Verify API's only accept Hashes, so hash our Random Data */
166 DEFER_CLEANUP(struct s2n_hash_state sign_hash = { 0 }, s2n_hash_free);
167 EXPECT_SUCCESS(s2n_hash_new(&sign_hash));
168 EXPECT_SUCCESS(s2n_hash_init(&sign_hash, S2N_HASH_SHA256));
169 EXPECT_SUCCESS(s2n_hash_update(&sign_hash, random_msg.data, random_msg.size));
170
171 DEFER_CLEANUP(struct s2n_hash_state verify_hash = { 0 }, s2n_hash_free);
172 EXPECT_SUCCESS(s2n_hash_new(&verify_hash));
173 EXPECT_SUCCESS(s2n_hash_init(&verify_hash, S2N_HASH_SHA256));
174 EXPECT_SUCCESS(s2n_hash_update(&verify_hash, random_msg.data, random_msg.size));
175
176 /* Sign and Verify the Hash of the Random Blob */
177 s2n_stack_blob(signature_data, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE);
178 EXPECT_SUCCESS(s2n_pkey_sign(private_key, S2N_SIGNATURE_RSA_PSS_PSS, &sign_hash, &signature_data));
179
180 /* Flip a random bit in the signature */
181 EXPECT_SUCCESS(s2n_flip_random_bit(&signature_data));
182 EXPECT_FAILURE(s2n_pkey_verify(&public_key, S2N_SIGNATURE_RSA_PSS_PSS, &verify_hash, &signature_data));
183 };
184
185 /* Release Resources */
186 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
187 EXPECT_SUCCESS(s2n_config_free(server_config));
188 EXPECT_SUCCESS(s2n_pkey_free(&public_key));
189 free(cert_chain_pem);
190 free(private_key_pem);
191 };
192
193 /* Negative Test: Ensure Verification with wrong key fails
194 * Pseudocode: assert(FAILURE == verify(Key2_public, message, sign(Key1_private, message)))
195 */
196 {
197 struct s2n_config *server_config = NULL;
198 char *root_cert_chain_pem = NULL;
199 char *root_private_key_pem = NULL;
200 char *leaf_cert_chain_pem = NULL;
201 char *leaf_private_key_pem = NULL;
202 struct s2n_cert_chain_and_key *root_chain_and_key = NULL;
203 struct s2n_cert_chain_and_key *leaf_chain_and_key = NULL;
204 struct s2n_pkey root_public_key = { 0 };
205 struct s2n_pkey leaf_public_key = { 0 };
206 s2n_pkey_type root_pkey_type = S2N_PKEY_TYPE_UNKNOWN;
207 s2n_pkey_type leaf_pkey_type = S2N_PKEY_TYPE_UNKNOWN;
208
209 EXPECT_NOT_NULL(root_cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
210 EXPECT_NOT_NULL(root_private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
211 EXPECT_NOT_NULL(leaf_cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
212 EXPECT_NOT_NULL(leaf_private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
213
214 EXPECT_NOT_NULL(server_config = s2n_config_new());
215
216 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_CERT, root_cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
217 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_KEY, root_private_key_pem, S2N_MAX_TEST_PEM_SIZE));
218 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, leaf_cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
219 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_KEY, leaf_private_key_pem, S2N_MAX_TEST_PEM_SIZE));
220 EXPECT_NOT_NULL(root_chain_and_key = s2n_cert_chain_and_key_new());
221 EXPECT_NOT_NULL(leaf_chain_and_key = s2n_cert_chain_and_key_new());
222
223 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_cert_chain(root_chain_and_key, root_cert_chain_pem));
224 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_private_key(root_chain_and_key, root_private_key_pem));
225 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_cert_chain(leaf_chain_and_key, leaf_cert_chain_pem));
226 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_private_key(leaf_chain_and_key, leaf_private_key_pem));
227
228 /* Parse the cert for the public key and certificate type */
229 EXPECT_OK(s2n_asn1der_to_public_key_and_type(&root_public_key, &root_pkey_type, &root_chain_and_key->cert_chain->head->raw));
230 EXPECT_OK(s2n_asn1der_to_public_key_and_type(&leaf_public_key, &leaf_pkey_type, &leaf_chain_and_key->cert_chain->head->raw));
231 EXPECT_NOT_EQUAL(root_pkey_type, S2N_PKEY_TYPE_UNKNOWN);
232 EXPECT_NOT_EQUAL(leaf_pkey_type, S2N_PKEY_TYPE_UNKNOWN);
233
234 EXPECT_SUCCESS(s2n_cert_set_cert_type(root_chain_and_key->cert_chain->head, root_pkey_type));
235 EXPECT_SUCCESS(s2n_cert_set_cert_type(leaf_chain_and_key->cert_chain->head, leaf_pkey_type));
236
237 struct s2n_pkey *root_private_key = root_chain_and_key->private_key;
238 struct s2n_pkey *leaf_private_key = leaf_chain_and_key->private_key;
239 {
240 EXPECT_NOT_NULL(root_public_key.pkey);
241 EXPECT_NOT_NULL(leaf_public_key.pkey);
242
243 EXPECT_NOT_NULL(root_private_key);
244 EXPECT_NOT_NULL(root_private_key->pkey);
245 EXPECT_NOT_NULL(leaf_private_key);
246 EXPECT_NOT_NULL(leaf_private_key->pkey);
247
248 /* Generate a random blob to sign and verify */
249 s2n_stack_blob(random_msg, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE);
250 EXPECT_OK(s2n_get_private_random_data(&random_msg));
251
252 /* Sign/Verify API's only accept Hashes, so hash our Random Data */
253 DEFER_CLEANUP(struct s2n_hash_state sign_hash = { 0 }, s2n_hash_free);
254 EXPECT_SUCCESS(s2n_hash_new(&sign_hash));
255 EXPECT_SUCCESS(s2n_hash_init(&sign_hash, S2N_HASH_SHA256));
256 EXPECT_SUCCESS(s2n_hash_update(&sign_hash, random_msg.data, random_msg.size));
257
258 DEFER_CLEANUP(struct s2n_hash_state verify_hash = { 0 }, s2n_hash_free);
259 EXPECT_SUCCESS(s2n_hash_new(&verify_hash));
260 EXPECT_SUCCESS(s2n_hash_init(&verify_hash, S2N_HASH_SHA256));
261 EXPECT_SUCCESS(s2n_hash_update(&verify_hash, random_msg.data, random_msg.size));
262
263 /* Sign and Verify the Hash of the Random Blob */
264 s2n_stack_blob(signature_data, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE);
265
266 /* Sign with Root's Key, but verify with Leaf's Key. This should fail. */
267 EXPECT_SUCCESS(s2n_pkey_sign(root_private_key, S2N_SIGNATURE_RSA_PSS_PSS, &sign_hash, &signature_data));
268 EXPECT_FAILURE(s2n_pkey_verify(&leaf_public_key, S2N_SIGNATURE_RSA_PSS_PSS, &verify_hash, &signature_data));
269 };
270
271 /* Release Resources */
272 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(root_chain_and_key));
273 EXPECT_SUCCESS(s2n_pkey_free(&root_public_key));
274 free(root_cert_chain_pem);
275 free(root_private_key_pem);
276
277 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(leaf_chain_and_key));
278 EXPECT_SUCCESS(s2n_config_free(server_config));
279 EXPECT_SUCCESS(s2n_pkey_free(&leaf_public_key));
280 free(leaf_cert_chain_pem);
281 free(leaf_private_key_pem);
282 };
283
284 /* Negative Test: Ensure flipping a bit in message given to verification fails
285 * Pseudocode: assert(FAILURE == verify(Key1_public, bitflip(message), sign(Key1_private, message)))
286 */
287 {
288 struct s2n_config *server_config = NULL;
289 char *cert_chain_pem = NULL;
290 char *private_key_pem = NULL;
291 struct s2n_cert_chain_and_key *chain_and_key = NULL;
292 struct s2n_pkey public_key = { 0 };
293 s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
294
295 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
296 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
297 EXPECT_NOT_NULL(server_config = s2n_config_new());
298
299 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
300 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
301 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
302
303 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_cert_chain(chain_and_key, cert_chain_pem));
304 EXPECT_SUCCESS(s2n_cert_chain_and_key_set_private_key(chain_and_key, private_key_pem));
305
306 /* Parse the leaf cert for the public key and certificate type */
307 EXPECT_OK(s2n_asn1der_to_public_key_and_type(&public_key, &pkey_type, &chain_and_key->cert_chain->head->raw));
308 EXPECT_NOT_EQUAL(pkey_type, S2N_PKEY_TYPE_UNKNOWN);
309 EXPECT_SUCCESS(s2n_cert_set_cert_type(chain_and_key->cert_chain->head, pkey_type));
310
311 struct s2n_pkey *private_key = chain_and_key->private_key;
312 {
313 EXPECT_NOT_NULL(public_key.pkey);
314 EXPECT_NOT_NULL(private_key);
315 EXPECT_NOT_NULL(private_key->pkey);
316
317 /* Generate a random blob to sign and verify */
318 s2n_stack_blob(random_msg, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE);
319 EXPECT_OK(s2n_get_private_random_data(&random_msg));
320
321 /* Sign/Verify API's only accept Hashes, so hash our Random Data */
322 DEFER_CLEANUP(struct s2n_hash_state sign_hash = { 0 }, s2n_hash_free);
323 EXPECT_SUCCESS(s2n_hash_new(&sign_hash));
324 EXPECT_SUCCESS(s2n_hash_init(&sign_hash, S2N_HASH_SHA256));
325 EXPECT_SUCCESS(s2n_hash_update(&sign_hash, random_msg.data, random_msg.size));
326
327 /* Flip a random bit in the message before verification */
328 EXPECT_SUCCESS(s2n_flip_random_bit(&random_msg));
329
330 DEFER_CLEANUP(struct s2n_hash_state verify_hash = { 0 }, s2n_hash_free);
331 EXPECT_SUCCESS(s2n_hash_new(&verify_hash));
332 EXPECT_SUCCESS(s2n_hash_init(&verify_hash, S2N_HASH_SHA256));
333 EXPECT_SUCCESS(s2n_hash_update(&verify_hash, random_msg.data, random_msg.size));
334
335 /* Sign and Verify the Hash of the Random Blob */
336 s2n_stack_blob(signature_data, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE);
337 EXPECT_SUCCESS(s2n_pkey_sign(private_key, S2N_SIGNATURE_RSA_PSS_PSS, &sign_hash, &signature_data));
338 EXPECT_FAILURE(s2n_pkey_verify(&public_key, S2N_SIGNATURE_RSA_PSS_PSS, &verify_hash, &signature_data));
339 };
340
341 /* Release Resources */
342 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
343 EXPECT_SUCCESS(s2n_config_free(server_config));
344 EXPECT_SUCCESS(s2n_pkey_free(&public_key));
345 free(cert_chain_pem);
346 free(private_key_pem);
347 };
348
349 END_TEST();
350 }