| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "crypto/s2n_pkey.h" |
| 17 |
|
| 18 |
#include "crypto/s2n_rsa_pss.h" |
| 19 |
#include "s2n_test.h" |
| 20 |
#include "testlib/s2n_testlib.h" |
| 21 |
|
| 22 |
struct s2n_test_pkeys { |
| 23 |
struct s2n_pkey pub_key; |
| 24 |
struct s2n_pkey *priv_key; |
| 25 |
bool supported; |
| 26 |
}; |
| 27 |
|
| 28 |
S2N_RESULT s2n_test_pkeys_init(struct s2n_test_pkeys *pkeys, |
| 29 |
const char *cert_chain_file, const char *private_key_file) |
| 30 |
{ |
| 31 |
DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = NULL, |
| 32 |
s2n_cert_chain_and_key_ptr_free); |
| 33 |
RESULT_GUARD_POSIX(s2n_test_cert_chain_and_key_new(&chain_and_key, |
| 34 |
cert_chain_file, private_key_file)); |
| 35 |
|
| 36 |
|
| 37 |
pkeys->priv_key = chain_and_key->private_key; |
| 38 |
chain_and_key->private_key = NULL; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
s2n_pkey_type type = 0; |
| 44 |
RESULT_GUARD(s2n_asn1der_to_public_key_and_type(&pkeys->pub_key, &type, |
| 45 |
&chain_and_key->cert_chain->head->raw)); |
| 46 |
|
| 47 |
pkeys->supported = true; |
| 48 |
return S2N_RESULT_OK; |
| 49 |
} |
| 50 |
|
| 51 |
S2N_CLEANUP_RESULT s2n_test_pkeys_wipe(struct s2n_test_pkeys *pkeys) |
| 52 |
{ |
| 53 |
if (!pkeys) { |
| 54 |
return S2N_RESULT_OK; |
| 55 |
} |
| 56 |
RESULT_GUARD_POSIX(s2n_pkey_free(&pkeys->pub_key)); |
| 57 |
RESULT_GUARD_POSIX(s2n_pkey_free(pkeys->priv_key)); |
| 58 |
RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &pkeys->priv_key, sizeof(struct s2n_pkey))); |
| 59 |
return S2N_RESULT_OK; |
| 60 |
} |
| 61 |
|
| 62 |
int main(int argc, char **argv) |
| 63 |
{ |
| 64 |
BEGIN_TEST(); |
| 65 |
|
| 66 |
DEFER_CLEANUP(struct s2n_test_pkeys ecdsa_pkeys = { 0 }, s2n_test_pkeys_wipe); |
| 67 |
EXPECT_OK(s2n_test_pkeys_init(&ecdsa_pkeys, |
| 68 |
S2N_ECDSA_P384_PKCS1_CERT_CHAIN, S2N_ECDSA_P384_PKCS1_KEY)); |
| 69 |
|
| 70 |
DEFER_CLEANUP(struct s2n_test_pkeys rsa_pkeys = { 0 }, s2n_test_pkeys_wipe); |
| 71 |
EXPECT_OK(s2n_test_pkeys_init(&rsa_pkeys, |
| 72 |
S2N_RSA_2048_PKCS1_SHA256_CERT_CHAIN, S2N_RSA_2048_PKCS1_SHA256_CERT_KEY)); |
| 73 |
|
| 74 |
DEFER_CLEANUP(struct s2n_test_pkeys rsa_pss_pkeys = { 0 }, s2n_test_pkeys_wipe); |
| 75 |
if (s2n_is_rsa_pss_certs_supported()) { |
| 76 |
EXPECT_OK(s2n_test_pkeys_init(&rsa_pss_pkeys, |
| 77 |
S2N_RSA_PSS_2048_SHA256_LEAF_CERT, S2N_RSA_PSS_2048_SHA256_LEAF_KEY)); |
| 78 |
} |
| 79 |
|
| 80 |
struct s2n_test_pkeys test_pkeys[] = { ecdsa_pkeys, rsa_pkeys, rsa_pss_pkeys }; |
| 81 |
|
| 82 |
|
| 83 |
{ |
| 84 |
DEFER_CLEANUP(struct s2n_test_pkeys other_ecdsa_pkeys = { 0 }, s2n_test_pkeys_wipe); |
| 85 |
EXPECT_OK(s2n_test_pkeys_init(&other_ecdsa_pkeys, |
| 86 |
S2N_ECDSA_P256_PKCS1_CERT_CHAIN, S2N_ECDSA_P256_PKCS1_KEY)); |
| 87 |
|
| 88 |
DEFER_CLEANUP(struct s2n_test_pkeys other_rsa_pkeys = { 0 }, s2n_test_pkeys_wipe); |
| 89 |
EXPECT_OK(s2n_test_pkeys_init(&other_rsa_pkeys, |
| 90 |
S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY)); |
| 91 |
|
| 92 |
DEFER_CLEANUP(struct s2n_test_pkeys other_rsa_pss_pkeys = { 0 }, s2n_test_pkeys_wipe); |
| 93 |
if (s2n_is_rsa_pss_certs_supported()) { |
| 94 |
EXPECT_OK(s2n_test_pkeys_init(&other_rsa_pss_pkeys, |
| 95 |
S2N_RSA_PSS_2048_SHA256_CA_CERT, S2N_RSA_PSS_2048_SHA256_CA_KEY)); |
| 96 |
} |
| 97 |
|
| 98 |
struct s2n_test_pkeys other_pkeys[] = { |
| 99 |
other_ecdsa_pkeys, other_rsa_pkeys, other_rsa_pss_pkeys |
| 100 |
}; |
| 101 |
|
| 102 |
for (size_t pkey_i = 0; pkey_i < s2n_array_len(test_pkeys); pkey_i++) { |
| 103 |
if (!test_pkeys[pkey_i].supported) { |
| 104 |
continue; |
| 105 |
} |
| 106 |
|
| 107 |
EXPECT_SUCCESS(s2n_pkey_match( |
| 108 |
&test_pkeys[pkey_i].pub_key, |
| 109 |
test_pkeys[pkey_i].priv_key)); |
| 110 |
|
| 111 |
for (size_t other_i = 0; other_i < s2n_array_len(other_pkeys); other_i++) { |
| 112 |
if (!other_pkeys[other_i].supported) { |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
EXPECT_FAILURE_WITH_ERRNO( |
| 117 |
s2n_pkey_match(&test_pkeys[pkey_i].pub_key, other_pkeys[other_i].priv_key), |
| 118 |
S2N_ERR_KEY_MISMATCH); |
| 119 |
EXPECT_FAILURE_WITH_ERRNO( |
| 120 |
s2n_pkey_match(&other_pkeys[other_i].pub_key, test_pkeys[pkey_i].priv_key), |
| 121 |
S2N_ERR_KEY_MISMATCH); |
| 122 |
} |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
{ |
| 128 |
|
| 129 |
uint32_t expected_sizes[] = { 104, 256, 256 }; |
| 130 |
EXPECT_EQUAL(s2n_array_len(test_pkeys), s2n_array_len(expected_sizes)); |
| 131 |
|
| 132 |
for (size_t i = 0; i < s2n_array_len(test_pkeys); i++) { |
| 133 |
if (!test_pkeys[i].supported) { |
| 134 |
continue; |
| 135 |
} |
| 136 |
|
| 137 |
uint32_t pub_size = 0; |
| 138 |
EXPECT_OK(s2n_pkey_size(&test_pkeys[i].pub_key, &pub_size)); |
| 139 |
EXPECT_EQUAL(pub_size, expected_sizes[i]); |
| 140 |
|
| 141 |
uint32_t priv_size = 0; |
| 142 |
EXPECT_OK(s2n_pkey_size(test_pkeys[i].priv_key, &priv_size)); |
| 143 |
EXPECT_EQUAL(priv_size, expected_sizes[i]); |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
{ |
| 149 |
struct s2n_blob in = { 0 }, out = { 0 }; |
| 150 |
|
| 151 |
|
| 152 |
{ |
| 153 |
EXPECT_FAILURE_WITH_ERRNO( |
| 154 |
s2n_pkey_encrypt(ecdsa_pkeys.priv_key, &in, &out), |
| 155 |
S2N_ERR_UNIMPLEMENTED); |
| 156 |
EXPECT_FAILURE_WITH_ERRNO( |
| 157 |
s2n_pkey_decrypt(&ecdsa_pkeys.pub_key, &in, &out), |
| 158 |
S2N_ERR_UNIMPLEMENTED); |
| 159 |
}; |
| 160 |
|
| 161 |
|
| 162 |
if (rsa_pss_pkeys.supported) { |
| 163 |
EXPECT_FAILURE_WITH_ERRNO( |
| 164 |
s2n_pkey_encrypt(rsa_pss_pkeys.priv_key, &in, &out), |
| 165 |
S2N_ERR_UNIMPLEMENTED); |
| 166 |
EXPECT_FAILURE_WITH_ERRNO( |
| 167 |
s2n_pkey_decrypt(&rsa_pss_pkeys.pub_key, &in, &out), |
| 168 |
S2N_ERR_UNIMPLEMENTED); |
| 169 |
}; |
| 170 |
|
| 171 |
|
| 172 |
{ |
| 173 |
uint8_t message_bytes[] = "hello world"; |
| 174 |
struct s2n_blob message = { 0 }; |
| 175 |
EXPECT_SUCCESS(s2n_blob_init(&message, message_bytes, sizeof(message_bytes))); |
| 176 |
|
| 177 |
const char ciphertext_hex[] = |
| 178 |
"38 ab 9c 83 57 17 13 46 3d 5b 6f c6 44 30 8e 40 78 ac d2 " |
| 179 |
"c5 69 98 8b 99 51 14 23 c6 af 98 67 6f 58 d5 28 9e 23 12 " |
| 180 |
"6f 5b 73 56 d9 f9 b2 10 4f d6 53 5c 70 a5 a6 c6 a2 53 83 " |
| 181 |
"7d ec 3b 7c c0 ff 6a 30 8d 98 a3 6c be c6 e3 b4 4d b6 3d " |
| 182 |
"cc 94 67 f8 24 9a 91 13 52 57 02 0c d9 5a 00 98 1f ff df " |
| 183 |
"5e 47 e0 ca 15 87 f0 92 1e 95 af ef 49 b2 6b f2 b6 be d5 " |
| 184 |
"3b 65 d3 94 92 f5 c1 f0 65 56 20 85 7f 18 95 a5 d9 e7 6c " |
| 185 |
"43 07 dd 5d 03 60 ac 4d c5 a0 c8 3d f9 99 24 fc 30 8f c2 " |
| 186 |
"66 9d df 5c 80 90 a7 c5 7a 37 ee be 1d 30 a7 a3 67 73 ae " |
| 187 |
"7d ee 64 37 22 77 9a a5 0d 47 f0 a5 50 ee 85 82 2e 88 32 " |
| 188 |
"e9 0b bc 25 5f 09 b7 d3 13 58 88 84 9d 07 03 5e 37 6b af " |
| 189 |
"08 56 14 fd 64 58 29 5b 81 a5 72 72 62 5d c1 72 bb 13 76 " |
| 190 |
"b6 17 96 7b d9 87 ec 49 71 dc 33 3e b2 f5 76 54 ad 13 ed " |
| 191 |
"23 1c 34 53 d1 12 03 be f6"; |
| 192 |
S2N_BLOB_FROM_HEX(ciphertext, ciphertext_hex); |
| 193 |
|
| 194 |
|
| 195 |
{ |
| 196 |
DEFER_CLEANUP(struct s2n_blob output = { 0 }, s2n_free); |
| 197 |
EXPECT_SUCCESS(s2n_alloc(&output, message.size)); |
| 198 |
|
| 199 |
EXPECT_SUCCESS(s2n_pkey_decrypt(rsa_pkeys.priv_key, &ciphertext, &output)); |
| 200 |
EXPECT_BYTEARRAY_EQUAL(output.data, message.data, message.size); |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
{ |
| 205 |
DEFER_CLEANUP(struct s2n_blob encrypt_out = { 0 }, s2n_free); |
| 206 |
EXPECT_SUCCESS(s2n_alloc(&encrypt_out, ciphertext.size)); |
| 207 |
EXPECT_SUCCESS(s2n_pkey_encrypt(&rsa_pkeys.pub_key, &message, &encrypt_out)); |
| 208 |
|
| 209 |
DEFER_CLEANUP(struct s2n_blob decrypt_out = { 0 }, s2n_free); |
| 210 |
EXPECT_SUCCESS(s2n_alloc(&decrypt_out, message.size)); |
| 211 |
EXPECT_SUCCESS(s2n_pkey_decrypt(rsa_pkeys.priv_key, &encrypt_out, &decrypt_out)); |
| 212 |
EXPECT_BYTEARRAY_EQUAL(decrypt_out.data, message.data, message.size); |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
for (size_t i = 0; i < ciphertext.size; i++) { |
| 222 |
DEFER_CLEANUP(struct s2n_blob input = { 0 }, s2n_free); |
| 223 |
EXPECT_SUCCESS(s2n_dup(&ciphertext, &input)); |
| 224 |
input.data[i]++; |
| 225 |
|
| 226 |
DEFER_CLEANUP(struct s2n_blob output = { 0 }, s2n_free); |
| 227 |
EXPECT_SUCCESS(s2n_alloc(&output, message.size)); |
| 228 |
|
| 229 |
EXPECT_SUCCESS(s2n_pkey_decrypt(rsa_pkeys.priv_key, &input, &output)); |
| 230 |
EXPECT_BYTEARRAY_NOT_EQUAL(output.data, message.data, message.size); |
| 231 |
} |
| 232 |
}; |
| 233 |
}; |
| 234 |
|
| 235 |
END_TEST(); |
| 236 |
} |