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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
#include "crypto/s2n_rsa_pss.h"
#include "crypto/s2n_certificate.h"
#include "crypto/s2n_dhe.h"
#include "s2n_test.h"
#include "stuffer/s2n_stuffer.h"
#include "testlib/s2n_testlib.h"
#include "tls/s2n_config.h"
#include "tls/s2n_connection.h"
#include "utils/s2n_random.h"
int s2n_flip_random_bit(struct s2n_blob *blob)
{
/* Flip a random bit in the blob */
uint64_t byte_flip_pos = 0;
POSIX_GUARD_RESULT(s2n_public_random(blob->size, &byte_flip_pos));
uint64_t bit_flip_pos = 0;
POSIX_GUARD_RESULT(s2n_public_random(8, &bit_flip_pos));
uint8_t mask = 0x01 << (uint8_t) bit_flip_pos;
blob->data[byte_flip_pos] ^= mask;
return 0;
}
int main(int argc, char **argv)
{
BEGIN_TEST();
EXPECT_SUCCESS(s2n_disable_tls13_in_test());
/* Don't use RSA-PSS certs if unsupported */
#if !RSA_PSS_CERTS_SUPPORTED
EXPECT_FALSE(s2n_is_rsa_pss_certs_supported());
END_TEST();
#endif
EXPECT_TRUE(s2n_is_rsa_pss_certs_supported());
/* Check that s2n_is_rsa_pss_certs_supported() is a superset of s2n_is_rsa_pss_signing_supported() */
EXPECT_TRUE(s2n_is_rsa_pss_signing_supported());
/* Positive Test: Ensure we can sign and verify a randomly generated signature.
* Pseudocode: assert(SUCCESS == verify(Key1_public, message, sign(Key1_private, message)))
*/
{
struct s2n_config *server_config = NULL;
char *cert_chain_pem = NULL;
char *private_key_pem = NULL;
struct s2n_cert_chain_and_key *chain_and_key = NULL;
struct s2n_pkey public_key = { 0 };
s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(server_config = s2n_config_new());
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
/* Load the Private Key */
EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
/* Load the Public Key */
EXPECT_OK(s2n_asn1der_to_public_key_and_type(&public_key, &pkey_type, &chain_and_key->cert_chain->head->raw));
EXPECT_EQUAL(pkey_type, S2N_PKEY_TYPE_RSA_PSS);
/* Sign and Verify a Random Value to ensure that Public and Private Key Matches */
EXPECT_SUCCESS(s2n_pkey_match(&public_key, chain_and_key->private_key));
/* Release Resources */
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
EXPECT_SUCCESS(s2n_config_free(server_config));
EXPECT_SUCCESS(s2n_pkey_free(&public_key));
free(cert_chain_pem);
free(private_key_pem);
/* Verify repeated key frees.
* (Later calls should be a no-op) */
EXPECT_SUCCESS(s2n_pkey_free(&public_key));
};
/* Negative Test: Loading mismatching RSA PSS Public/Private Keys will fail.
* Pseudocode: assert(FAILURE == load_pem_pair(Key1_public, Key2_private))
*/
{
struct s2n_config *server_config = NULL;
char *leaf_cert_chain_pem = NULL;
char *root_private_key_pem = NULL;
struct s2n_cert_chain_and_key *misconfigured_chain_and_key = NULL;
struct s2n_pkey public_key = { 0 };
EXPECT_NOT_NULL(leaf_cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(root_private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(server_config = s2n_config_new());
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, leaf_cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
/* Incorrectly reading the CA's Private Key from disk, not the Leaf's Private Key */
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_KEY, root_private_key_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(misconfigured_chain_and_key = s2n_cert_chain_and_key_new());
/* Attempting to Load RSA_PSS Certificate with wrong RSA_PSS Key should fail */
EXPECT_FAILURE(s2n_cert_chain_and_key_load_pem(misconfigured_chain_and_key, leaf_cert_chain_pem, root_private_key_pem));
/* Release Resources */
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(misconfigured_chain_and_key));
EXPECT_SUCCESS(s2n_config_free(server_config));
EXPECT_SUCCESS(s2n_pkey_free(&public_key));
free(leaf_cert_chain_pem);
free(root_private_key_pem);
};
/* Negative Test: Ensure flipping a bit in the signature is rejected
* Pseudocode: assert(FAILURE == verify(Key1_public, message, bitflip(sign(Key1_private, message)))
*/
{
struct s2n_config *server_config = NULL;
char *cert_chain_pem = NULL;
char *private_key_pem = NULL;
struct s2n_cert_chain_and_key *chain_and_key = NULL;
struct s2n_pkey public_key = { 0 };
s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(server_config = s2n_config_new());
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_cert_chain(chain_and_key, cert_chain_pem));
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_private_key(chain_and_key, private_key_pem));
/* Parse the leaf cert for the public key and certificate type */
EXPECT_OK(s2n_asn1der_to_public_key_and_type(&public_key, &pkey_type, &chain_and_key->cert_chain->head->raw));
EXPECT_NOT_EQUAL(pkey_type, S2N_PKEY_TYPE_UNKNOWN);
EXPECT_SUCCESS(s2n_cert_set_cert_type(chain_and_key->cert_chain->head, pkey_type));
struct s2n_pkey *private_key = chain_and_key->private_key;
{
EXPECT_NOT_NULL(public_key.pkey);
EXPECT_NOT_NULL(private_key);
EXPECT_NOT_NULL(private_key->pkey);
/* Generate a random blob to sign and verify */
s2n_stack_blob(random_msg, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE);
EXPECT_OK(s2n_get_private_random_data(&random_msg));
/* Sign/Verify API's only accept Hashes, so hash our Random Data */
DEFER_CLEANUP(struct s2n_hash_state sign_hash = { 0 }, s2n_hash_free);
EXPECT_SUCCESS(s2n_hash_new(&sign_hash));
EXPECT_SUCCESS(s2n_hash_init(&sign_hash, S2N_HASH_SHA256));
EXPECT_SUCCESS(s2n_hash_update(&sign_hash, random_msg.data, random_msg.size));
DEFER_CLEANUP(struct s2n_hash_state verify_hash = { 0 }, s2n_hash_free);
EXPECT_SUCCESS(s2n_hash_new(&verify_hash));
EXPECT_SUCCESS(s2n_hash_init(&verify_hash, S2N_HASH_SHA256));
EXPECT_SUCCESS(s2n_hash_update(&verify_hash, random_msg.data, random_msg.size));
/* Sign and Verify the Hash of the Random Blob */
s2n_stack_blob(signature_data, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE);
EXPECT_SUCCESS(s2n_pkey_sign(private_key, S2N_SIGNATURE_RSA_PSS_PSS, &sign_hash, &signature_data));
/* Flip a random bit in the signature */
EXPECT_SUCCESS(s2n_flip_random_bit(&signature_data));
EXPECT_FAILURE(s2n_pkey_verify(&public_key, S2N_SIGNATURE_RSA_PSS_PSS, &verify_hash, &signature_data));
};
/* Release Resources */
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
EXPECT_SUCCESS(s2n_config_free(server_config));
EXPECT_SUCCESS(s2n_pkey_free(&public_key));
free(cert_chain_pem);
free(private_key_pem);
};
/* Negative Test: Ensure Verification with wrong key fails
* Pseudocode: assert(FAILURE == verify(Key2_public, message, sign(Key1_private, message)))
*/
{
struct s2n_config *server_config = NULL;
char *root_cert_chain_pem = NULL;
char *root_private_key_pem = NULL;
char *leaf_cert_chain_pem = NULL;
char *leaf_private_key_pem = NULL;
struct s2n_cert_chain_and_key *root_chain_and_key = NULL;
struct s2n_cert_chain_and_key *leaf_chain_and_key = NULL;
struct s2n_pkey root_public_key = { 0 };
struct s2n_pkey leaf_public_key = { 0 };
s2n_pkey_type root_pkey_type = S2N_PKEY_TYPE_UNKNOWN;
s2n_pkey_type leaf_pkey_type = S2N_PKEY_TYPE_UNKNOWN;
EXPECT_NOT_NULL(root_cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(root_private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(leaf_cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(leaf_private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(server_config = s2n_config_new());
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_CERT, root_cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_KEY, root_private_key_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, leaf_cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_KEY, leaf_private_key_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(root_chain_and_key = s2n_cert_chain_and_key_new());
EXPECT_NOT_NULL(leaf_chain_and_key = s2n_cert_chain_and_key_new());
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_cert_chain(root_chain_and_key, root_cert_chain_pem));
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_private_key(root_chain_and_key, root_private_key_pem));
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_cert_chain(leaf_chain_and_key, leaf_cert_chain_pem));
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_private_key(leaf_chain_and_key, leaf_private_key_pem));
/* Parse the cert for the public key and certificate type */
EXPECT_OK(s2n_asn1der_to_public_key_and_type(&root_public_key, &root_pkey_type, &root_chain_and_key->cert_chain->head->raw));
EXPECT_OK(s2n_asn1der_to_public_key_and_type(&leaf_public_key, &leaf_pkey_type, &leaf_chain_and_key->cert_chain->head->raw));
EXPECT_NOT_EQUAL(root_pkey_type, S2N_PKEY_TYPE_UNKNOWN);
EXPECT_NOT_EQUAL(leaf_pkey_type, S2N_PKEY_TYPE_UNKNOWN);
EXPECT_SUCCESS(s2n_cert_set_cert_type(root_chain_and_key->cert_chain->head, root_pkey_type));
EXPECT_SUCCESS(s2n_cert_set_cert_type(leaf_chain_and_key->cert_chain->head, leaf_pkey_type));
struct s2n_pkey *root_private_key = root_chain_and_key->private_key;
struct s2n_pkey *leaf_private_key = leaf_chain_and_key->private_key;
{
EXPECT_NOT_NULL(root_public_key.pkey);
EXPECT_NOT_NULL(leaf_public_key.pkey);
EXPECT_NOT_NULL(root_private_key);
EXPECT_NOT_NULL(root_private_key->pkey);
EXPECT_NOT_NULL(leaf_private_key);
EXPECT_NOT_NULL(leaf_private_key->pkey);
/* Generate a random blob to sign and verify */
s2n_stack_blob(random_msg, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE);
EXPECT_OK(s2n_get_private_random_data(&random_msg));
/* Sign/Verify API's only accept Hashes, so hash our Random Data */
DEFER_CLEANUP(struct s2n_hash_state sign_hash = { 0 }, s2n_hash_free);
EXPECT_SUCCESS(s2n_hash_new(&sign_hash));
EXPECT_SUCCESS(s2n_hash_init(&sign_hash, S2N_HASH_SHA256));
EXPECT_SUCCESS(s2n_hash_update(&sign_hash, random_msg.data, random_msg.size));
DEFER_CLEANUP(struct s2n_hash_state verify_hash = { 0 }, s2n_hash_free);
EXPECT_SUCCESS(s2n_hash_new(&verify_hash));
EXPECT_SUCCESS(s2n_hash_init(&verify_hash, S2N_HASH_SHA256));
EXPECT_SUCCESS(s2n_hash_update(&verify_hash, random_msg.data, random_msg.size));
/* Sign and Verify the Hash of the Random Blob */
s2n_stack_blob(signature_data, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE);
/* Sign with Root's Key, but verify with Leaf's Key. This should fail. */
EXPECT_SUCCESS(s2n_pkey_sign(root_private_key, S2N_SIGNATURE_RSA_PSS_PSS, &sign_hash, &signature_data));
EXPECT_FAILURE(s2n_pkey_verify(&leaf_public_key, S2N_SIGNATURE_RSA_PSS_PSS, &verify_hash, &signature_data));
};
/* Release Resources */
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(root_chain_and_key));
EXPECT_SUCCESS(s2n_pkey_free(&root_public_key));
free(root_cert_chain_pem);
free(root_private_key_pem);
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(leaf_chain_and_key));
EXPECT_SUCCESS(s2n_config_free(server_config));
EXPECT_SUCCESS(s2n_pkey_free(&leaf_public_key));
free(leaf_cert_chain_pem);
free(leaf_private_key_pem);
};
/* Negative Test: Ensure flipping a bit in message given to verification fails
* Pseudocode: assert(FAILURE == verify(Key1_public, bitflip(message), sign(Key1_private, message)))
*/
{
struct s2n_config *server_config = NULL;
char *cert_chain_pem = NULL;
char *private_key_pem = NULL;
struct s2n_cert_chain_and_key *chain_and_key = NULL;
struct s2n_pkey public_key = { 0 };
s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN;
EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(server_config = s2n_config_new());
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_LEAF_CERT, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_PSS_2048_SHA256_CA_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_cert_chain(chain_and_key, cert_chain_pem));
EXPECT_SUCCESS(s2n_cert_chain_and_key_set_private_key(chain_and_key, private_key_pem));
/* Parse the leaf cert for the public key and certificate type */
EXPECT_OK(s2n_asn1der_to_public_key_and_type(&public_key, &pkey_type, &chain_and_key->cert_chain->head->raw));
EXPECT_NOT_EQUAL(pkey_type, S2N_PKEY_TYPE_UNKNOWN);
EXPECT_SUCCESS(s2n_cert_set_cert_type(chain_and_key->cert_chain->head, pkey_type));
struct s2n_pkey *private_key = chain_and_key->private_key;
{
EXPECT_NOT_NULL(public_key.pkey);
EXPECT_NOT_NULL(private_key);
EXPECT_NOT_NULL(private_key->pkey);
/* Generate a random blob to sign and verify */
s2n_stack_blob(random_msg, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE, RSA_PSS_SIGN_VERIFY_RANDOM_BLOB_SIZE);
EXPECT_OK(s2n_get_private_random_data(&random_msg));
/* Sign/Verify API's only accept Hashes, so hash our Random Data */
DEFER_CLEANUP(struct s2n_hash_state sign_hash = { 0 }, s2n_hash_free);
EXPECT_SUCCESS(s2n_hash_new(&sign_hash));
EXPECT_SUCCESS(s2n_hash_init(&sign_hash, S2N_HASH_SHA256));
EXPECT_SUCCESS(s2n_hash_update(&sign_hash, random_msg.data, random_msg.size));
/* Flip a random bit in the message before verification */
EXPECT_SUCCESS(s2n_flip_random_bit(&random_msg));
DEFER_CLEANUP(struct s2n_hash_state verify_hash = { 0 }, s2n_hash_free);
EXPECT_SUCCESS(s2n_hash_new(&verify_hash));
EXPECT_SUCCESS(s2n_hash_init(&verify_hash, S2N_HASH_SHA256));
EXPECT_SUCCESS(s2n_hash_update(&verify_hash, random_msg.data, random_msg.size));
/* Sign and Verify the Hash of the Random Blob */
s2n_stack_blob(signature_data, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE, RSA_PSS_SIGN_VERIFY_SIGNATURE_SIZE);
EXPECT_SUCCESS(s2n_pkey_sign(private_key, S2N_SIGNATURE_RSA_PSS_PSS, &sign_hash, &signature_data));
EXPECT_FAILURE(s2n_pkey_verify(&public_key, S2N_SIGNATURE_RSA_PSS_PSS, &verify_hash, &signature_data));
};
/* Release Resources */
EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
EXPECT_SUCCESS(s2n_config_free(server_config));
EXPECT_SUCCESS(s2n_pkey_free(&public_key));
free(cert_chain_pem);
free(private_key_pem);
};
END_TEST();
}
|