| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "crypto.h" |
| 7 |
|
| 8 |
#include "aws/cal/ecc.h" |
| 9 |
#include "aws/cal/ed25519.h" |
| 10 |
#include "aws/cal/hash.h" |
| 11 |
#include "aws/cal/hmac.h" |
| 12 |
#include "aws/cal/rsa.h" |
| 13 |
#include "aws/common/encoding.h" |
| 14 |
#include "aws/io/pem.h" |
| 15 |
|
| 16 |
const char *s_capsule_name_hash = "aws_hash"; |
| 17 |
const char *s_capsule_name_hmac = "aws_hmac"; |
| 18 |
const char *s_capsule_name_rsa = "aws_rsa"; |
| 19 |
const char *s_capsule_name_ed25519 = "aws_ed25519"; |
| 20 |
const char *s_capsule_name_ec = "aws_ec"; |
| 21 |
|
| 22 |
static void s_hash_destructor(PyObject *hash_capsule) { |
| 23 |
assert(PyCapsule_CheckExact(hash_capsule)); |
| 24 |
|
| 25 |
struct aws_hash *hash = PyCapsule_GetPointer(hash_capsule, s_capsule_name_hash); |
| 26 |
assert(hash); |
| 27 |
|
| 28 |
aws_hash_destroy(hash); |
| 29 |
} |
| 30 |
|
| 31 |
static void s_hmac_destructor(PyObject *hmac_capsule) { |
| 32 |
assert(PyCapsule_CheckExact(hmac_capsule)); |
| 33 |
|
| 34 |
struct aws_hmac *hmac = PyCapsule_GetPointer(hmac_capsule, s_capsule_name_hmac); |
| 35 |
assert(hmac); |
| 36 |
|
| 37 |
aws_hmac_destroy(hmac); |
| 38 |
} |
| 39 |
|
| 40 |
PyObject *aws_py_sha1_new(PyObject *self, PyObject *args) { |
| 41 |
(void)self; |
| 42 |
(void)args; |
| 43 |
|
| 44 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 45 |
|
| 46 |
struct aws_hash *sha1 = aws_sha1_new(allocator); |
| 47 |
|
| 48 |
if (!sha1) { |
| 49 |
return PyErr_AwsLastError(); |
| 50 |
} |
| 51 |
|
| 52 |
PyObject *capsule = PyCapsule_New(sha1, s_capsule_name_hash, s_hash_destructor); |
| 53 |
|
| 54 |
if (capsule == NULL) { |
| 55 |
aws_hash_destroy(sha1); |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
return capsule; |
| 60 |
} |
| 61 |
|
| 62 |
PyObject *aws_py_sha256_new(PyObject *self, PyObject *args) { |
| 63 |
(void)self; |
| 64 |
(void)args; |
| 65 |
|
| 66 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 67 |
|
| 68 |
struct aws_hash *sha256 = aws_sha256_new(allocator); |
| 69 |
|
| 70 |
if (!sha256) { |
| 71 |
return PyErr_AwsLastError(); |
| 72 |
} |
| 73 |
|
| 74 |
PyObject *capsule = PyCapsule_New(sha256, s_capsule_name_hash, s_hash_destructor); |
| 75 |
|
| 76 |
if (capsule == NULL) { |
| 77 |
aws_hash_destroy(sha256); |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
return capsule; |
| 82 |
} |
| 83 |
|
| 84 |
PyObject *aws_py_md5_new(PyObject *self, PyObject *args) { |
| 85 |
(void)self; |
| 86 |
(void)args; |
| 87 |
|
| 88 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 89 |
|
| 90 |
struct aws_hash *md5 = aws_md5_new(allocator); |
| 91 |
|
| 92 |
if (!md5) { |
| 93 |
return PyErr_AwsLastError(); |
| 94 |
} |
| 95 |
|
| 96 |
PyObject *capsule = PyCapsule_New(md5, s_capsule_name_hash, s_hash_destructor); |
| 97 |
|
| 98 |
if (capsule == NULL) { |
| 99 |
aws_hash_destroy(md5); |
| 100 |
return NULL; |
| 101 |
} |
| 102 |
|
| 103 |
return capsule; |
| 104 |
} |
| 105 |
|
| 106 |
PyObject *aws_py_hash_update(PyObject *self, PyObject *args) { |
| 107 |
(void)self; |
| 108 |
|
| 109 |
PyObject *hash_capsule = NULL; |
| 110 |
const char *to_hash_c_str; |
| 111 |
Py_ssize_t to_hash_len; |
| 112 |
|
| 113 |
if (!PyArg_ParseTuple(args, "Os#", &hash_capsule, &to_hash_c_str, &to_hash_len)) { |
| 114 |
return PyErr_AwsLastError(); |
| 115 |
} |
| 116 |
|
| 117 |
struct aws_hash *hash = PyCapsule_GetPointer(hash_capsule, s_capsule_name_hash); |
| 118 |
if (!hash) { |
| 119 |
return PyErr_AwsLastError(); |
| 120 |
} |
| 121 |
|
| 122 |
struct aws_byte_cursor to_hash_cursor; |
| 123 |
to_hash_cursor = aws_byte_cursor_from_array(to_hash_c_str, to_hash_len); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
if (to_hash_len > 1024 * 5) { |
| 128 |
int aws_op = AWS_OP_SUCCESS; |
| 129 |
|
| 130 |
|
| 131 |
Py_BEGIN_ALLOW_THREADS |
| 132 |
aws_op = aws_hash_update(hash, &to_hash_cursor); |
| 133 |
Py_END_ALLOW_THREADS |
| 134 |
|
| 135 |
if (aws_op != AWS_OP_SUCCESS) { |
| 136 |
return PyErr_AwsLastError(); |
| 137 |
} |
| 138 |
|
| 139 |
} else { |
| 140 |
if (aws_hash_update(hash, &to_hash_cursor)) { |
| 141 |
return PyErr_AwsLastError(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
Py_RETURN_NONE; |
| 146 |
} |
| 147 |
|
| 148 |
PyObject *aws_py_hash_digest(PyObject *self, PyObject *args) { |
| 149 |
(void)self; |
| 150 |
|
| 151 |
PyObject *hash_capsule = NULL; |
| 152 |
Py_ssize_t truncate_to; |
| 153 |
|
| 154 |
if (!PyArg_ParseTuple(args, "On", &hash_capsule, &truncate_to)) { |
| 155 |
return PyErr_AwsLastError(); |
| 156 |
} |
| 157 |
|
| 158 |
struct aws_hash *hash = PyCapsule_GetPointer(hash_capsule, s_capsule_name_hash); |
| 159 |
if (!hash) { |
| 160 |
return PyErr_AwsLastError(); |
| 161 |
} |
| 162 |
|
| 163 |
uint8_t output[128] = {0}; |
| 164 |
struct aws_byte_buf digest_buf = aws_byte_buf_from_array(output, hash->digest_size); |
| 165 |
digest_buf.len = 0; |
| 166 |
|
| 167 |
if (aws_hash_finalize(hash, &digest_buf, truncate_to)) { |
| 168 |
return PyErr_AwsLastError(); |
| 169 |
} |
| 170 |
|
| 171 |
return PyBytes_FromStringAndSize((const char *)output, digest_buf.len); |
| 172 |
} |
| 173 |
|
| 174 |
PyObject *aws_py_sha256_hmac_new(PyObject *self, PyObject *args) { |
| 175 |
(void)self; |
| 176 |
|
| 177 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 178 |
|
| 179 |
const char *secret_ptr; |
| 180 |
Py_ssize_t secret_len; |
| 181 |
|
| 182 |
if (!PyArg_ParseTuple(args, "s#", &secret_ptr, &secret_len)) { |
| 183 |
return PyErr_AwsLastError(); |
| 184 |
} |
| 185 |
|
| 186 |
struct aws_byte_cursor secret_cursor; |
| 187 |
secret_cursor = aws_byte_cursor_from_array(secret_ptr, secret_len); |
| 188 |
|
| 189 |
struct aws_hmac *sha256_hmac = aws_sha256_hmac_new(allocator, &secret_cursor); |
| 190 |
|
| 191 |
if (!sha256_hmac) { |
| 192 |
return PyErr_AwsLastError(); |
| 193 |
} |
| 194 |
|
| 195 |
return PyCapsule_New(sha256_hmac, s_capsule_name_hmac, s_hmac_destructor); |
| 196 |
} |
| 197 |
|
| 198 |
PyObject *aws_py_hmac_update(PyObject *self, PyObject *args) { |
| 199 |
(void)self; |
| 200 |
|
| 201 |
PyObject *hmac_capsule = NULL; |
| 202 |
const char *to_hmac_ptr; |
| 203 |
Py_ssize_t to_hmac_len; |
| 204 |
|
| 205 |
if (!PyArg_ParseTuple(args, "Os#", &hmac_capsule, &to_hmac_ptr, &to_hmac_len)) { |
| 206 |
return PyErr_AwsLastError(); |
| 207 |
} |
| 208 |
|
| 209 |
struct aws_hmac *hmac = PyCapsule_GetPointer(hmac_capsule, s_capsule_name_hmac); |
| 210 |
if (!hmac) { |
| 211 |
return PyErr_AwsLastError(); |
| 212 |
} |
| 213 |
|
| 214 |
struct aws_byte_cursor to_hmac_cursor; |
| 215 |
to_hmac_cursor = aws_byte_cursor_from_array(to_hmac_ptr, to_hmac_len); |
| 216 |
|
| 217 |
if (aws_hmac_update(hmac, &to_hmac_cursor)) { |
| 218 |
return PyErr_AwsLastError(); |
| 219 |
} |
| 220 |
|
| 221 |
Py_RETURN_NONE; |
| 222 |
} |
| 223 |
|
| 224 |
PyObject *aws_py_hmac_digest(PyObject *self, PyObject *args) { |
| 225 |
(void)self; |
| 226 |
|
| 227 |
PyObject *hmac_capsule = NULL; |
| 228 |
Py_ssize_t truncate_to; |
| 229 |
|
| 230 |
if (!PyArg_ParseTuple(args, "On", &hmac_capsule, &truncate_to)) { |
| 231 |
return PyErr_AwsLastError(); |
| 232 |
} |
| 233 |
|
| 234 |
struct aws_hmac *hmac = PyCapsule_GetPointer(hmac_capsule, s_capsule_name_hmac); |
| 235 |
if (!hmac) { |
| 236 |
return PyErr_AwsLastError(); |
| 237 |
} |
| 238 |
|
| 239 |
uint8_t output[128] = {0}; |
| 240 |
struct aws_byte_buf digest_buf = aws_byte_buf_from_array(output, hmac->digest_size); |
| 241 |
digest_buf.len = 0; |
| 242 |
|
| 243 |
if (aws_hmac_finalize(hmac, &digest_buf, truncate_to)) { |
| 244 |
return PyErr_AwsLastError(); |
| 245 |
} |
| 246 |
|
| 247 |
return PyBytes_FromStringAndSize((const char *)output, digest_buf.len); |
| 248 |
} |
| 249 |
|
| 250 |
static void s_rsa_destructor(PyObject *rsa_capsule) { |
| 251 |
struct aws_rsa_key_pair *key_pair = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa); |
| 252 |
assert(key_pair); |
| 253 |
|
| 254 |
aws_rsa_key_pair_release(key_pair); |
| 255 |
} |
| 256 |
|
| 257 |
struct aws_pem_object *s_find_pem_object(struct aws_array_list *pem_list, enum aws_pem_object_type pem_type) { |
| 258 |
for (size_t i = 0; i < aws_array_list_length(pem_list); ++i) { |
| 259 |
struct aws_pem_object *pem_object = NULL; |
| 260 |
if (aws_array_list_get_at_ptr(pem_list, (void **)&pem_object, 0)) { |
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
if (pem_object->type == pem_type) { |
| 265 |
return pem_object; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return NULL; |
| 270 |
} |
| 271 |
|
| 272 |
PyObject *aws_py_rsa_private_key_from_pem_data(PyObject *self, PyObject *args) { |
| 273 |
(void)self; |
| 274 |
|
| 275 |
struct aws_byte_cursor pem_data_cur; |
| 276 |
if (!PyArg_ParseTuple(args, "s#", &pem_data_cur.ptr, &pem_data_cur.len)) { |
| 277 |
return NULL; |
| 278 |
} |
| 279 |
|
| 280 |
PyObject *capsule = NULL; |
| 281 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 282 |
struct aws_array_list pem_list; |
| 283 |
if (aws_pem_objects_init_from_file_contents(&pem_list, allocator, pem_data_cur)) { |
| 284 |
return PyErr_AwsLastError(); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
struct aws_pem_object *found_pem_object = s_find_pem_object(&pem_list, AWS_PEM_TYPE_PRIVATE_RSA_PKCS1); |
| 290 |
struct aws_rsa_key_pair *key_pair = NULL; |
| 291 |
|
| 292 |
if (found_pem_object != NULL) { |
| 293 |
key_pair = |
| 294 |
aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&found_pem_object->data)); |
| 295 |
} else { |
| 296 |
found_pem_object = s_find_pem_object(&pem_list, AWS_PEM_TYPE_PRIVATE_PKCS8); |
| 297 |
if (found_pem_object != NULL) { |
| 298 |
key_pair = aws_rsa_key_pair_new_from_private_key_pkcs8( |
| 299 |
allocator, aws_byte_cursor_from_buf(&found_pem_object->data)); |
| 300 |
} else { |
| 301 |
PyErr_SetString(PyExc_ValueError, "RSA private key not found in PEM."); |
| 302 |
goto on_done; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (key_pair == NULL) { |
| 307 |
PyErr_AwsLastError(); |
| 308 |
goto on_done; |
| 309 |
} |
| 310 |
|
| 311 |
capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor); |
| 312 |
|
| 313 |
if (capsule == NULL) { |
| 314 |
aws_rsa_key_pair_release(key_pair); |
| 315 |
} |
| 316 |
|
| 317 |
on_done: |
| 318 |
aws_pem_objects_clean_up(&pem_list); |
| 319 |
return capsule; |
| 320 |
} |
| 321 |
|
| 322 |
PyObject *aws_py_rsa_public_key_from_pem_data(PyObject *self, PyObject *args) { |
| 323 |
(void)self; |
| 324 |
|
| 325 |
struct aws_byte_cursor pem_data_cur; |
| 326 |
if (!PyArg_ParseTuple(args, "s#", &pem_data_cur.ptr, &pem_data_cur.len)) { |
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
PyObject *capsule = NULL; |
| 331 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 332 |
struct aws_array_list pem_list; |
| 333 |
if (aws_pem_objects_init_from_file_contents(&pem_list, allocator, pem_data_cur)) { |
| 334 |
return PyErr_AwsLastError(); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
struct aws_pem_object *found_pem_object = s_find_pem_object(&pem_list, AWS_PEM_TYPE_PUBLIC_RSA_PKCS1); |
| 340 |
|
| 341 |
if (found_pem_object == NULL) { |
| 342 |
PyErr_SetString(PyExc_ValueError, "RSA public key not found in PEM."); |
| 343 |
goto on_done; |
| 344 |
} |
| 345 |
|
| 346 |
struct aws_rsa_key_pair *key_pair = |
| 347 |
aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, aws_byte_cursor_from_buf(&found_pem_object->data)); |
| 348 |
|
| 349 |
if (key_pair == NULL) { |
| 350 |
PyErr_AwsLastError(); |
| 351 |
goto on_done; |
| 352 |
} |
| 353 |
|
| 354 |
capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor); |
| 355 |
|
| 356 |
if (capsule == NULL) { |
| 357 |
aws_rsa_key_pair_release(key_pair); |
| 358 |
} |
| 359 |
|
| 360 |
on_done: |
| 361 |
aws_pem_objects_clean_up(&pem_list); |
| 362 |
return capsule; |
| 363 |
} |
| 364 |
|
| 365 |
PyObject *aws_py_rsa_private_key_from_der_data(PyObject *self, PyObject *args) { |
| 366 |
(void)self; |
| 367 |
|
| 368 |
struct aws_byte_cursor der_data_cur; |
| 369 |
if (!PyArg_ParseTuple(args, "s#", &der_data_cur.ptr, &der_data_cur.len)) { |
| 370 |
return NULL; |
| 371 |
} |
| 372 |
|
| 373 |
PyObject *capsule = NULL; |
| 374 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 375 |
|
| 376 |
struct aws_rsa_key_pair *key_pair = aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, der_data_cur); |
| 377 |
|
| 378 |
if (key_pair == NULL) { |
| 379 |
PyErr_AwsLastError(); |
| 380 |
goto on_done; |
| 381 |
} |
| 382 |
|
| 383 |
capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor); |
| 384 |
|
| 385 |
if (capsule == NULL) { |
| 386 |
aws_rsa_key_pair_release(key_pair); |
| 387 |
} |
| 388 |
|
| 389 |
on_done: |
| 390 |
return capsule; |
| 391 |
} |
| 392 |
|
| 393 |
PyObject *aws_py_rsa_public_key_from_der_data(PyObject *self, PyObject *args) { |
| 394 |
(void)self; |
| 395 |
|
| 396 |
struct aws_byte_cursor der_data_cur; |
| 397 |
if (!PyArg_ParseTuple(args, "s#", &der_data_cur.ptr, &der_data_cur.len)) { |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
|
| 401 |
PyObject *capsule = NULL; |
| 402 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 403 |
|
| 404 |
struct aws_rsa_key_pair *key_pair = aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, der_data_cur); |
| 405 |
|
| 406 |
if (key_pair == NULL) { |
| 407 |
PyErr_AwsLastError(); |
| 408 |
goto on_done; |
| 409 |
} |
| 410 |
|
| 411 |
capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor); |
| 412 |
|
| 413 |
if (capsule == NULL) { |
| 414 |
aws_rsa_key_pair_release(key_pair); |
| 415 |
} |
| 416 |
|
| 417 |
on_done: |
| 418 |
return capsule; |
| 419 |
} |
| 420 |
|
| 421 |
PyObject *aws_py_rsa_encrypt(PyObject *self, PyObject *args) { |
| 422 |
(void)self; |
| 423 |
|
| 424 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 425 |
PyObject *rsa_capsule = NULL; |
| 426 |
int encrypt_algo = 0; |
| 427 |
struct aws_byte_cursor plaintext_cur; |
| 428 |
if (!PyArg_ParseTuple(args, "Oiy#", &rsa_capsule, &encrypt_algo, &plaintext_cur.ptr, &plaintext_cur.len)) { |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
|
| 432 |
struct aws_rsa_key_pair *rsa = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa); |
| 433 |
if (rsa == NULL) { |
| 434 |
return NULL; |
| 435 |
} |
| 436 |
|
| 437 |
struct aws_byte_buf result_buf; |
| 438 |
aws_byte_buf_init(&result_buf, allocator, aws_rsa_key_pair_block_length(rsa)); |
| 439 |
|
| 440 |
if (aws_rsa_key_pair_encrypt(rsa, encrypt_algo, plaintext_cur, &result_buf)) { |
| 441 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 442 |
return PyErr_AwsLastError(); |
| 443 |
} |
| 444 |
|
| 445 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 446 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 447 |
return ret; |
| 448 |
} |
| 449 |
|
| 450 |
PyObject *aws_py_rsa_decrypt(PyObject *self, PyObject *args) { |
| 451 |
(void)self; |
| 452 |
|
| 453 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 454 |
PyObject *rsa_capsule = NULL; |
| 455 |
int encrypt_algo = 0; |
| 456 |
struct aws_byte_cursor ciphertext_cur; |
| 457 |
if (!PyArg_ParseTuple(args, "Oiy#", &rsa_capsule, &encrypt_algo, &ciphertext_cur.ptr, &ciphertext_cur.len)) { |
| 458 |
return NULL; |
| 459 |
} |
| 460 |
|
| 461 |
struct aws_rsa_key_pair *rsa = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa); |
| 462 |
if (rsa == NULL) { |
| 463 |
return NULL; |
| 464 |
} |
| 465 |
|
| 466 |
struct aws_byte_buf result_buf; |
| 467 |
aws_byte_buf_init(&result_buf, allocator, aws_rsa_key_pair_block_length(rsa)); |
| 468 |
|
| 469 |
if (aws_rsa_key_pair_decrypt(rsa, encrypt_algo, ciphertext_cur, &result_buf)) { |
| 470 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 471 |
return PyErr_AwsLastError(); |
| 472 |
} |
| 473 |
|
| 474 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 475 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 476 |
return ret; |
| 477 |
} |
| 478 |
|
| 479 |
PyObject *aws_py_rsa_sign(PyObject *self, PyObject *args) { |
| 480 |
(void)self; |
| 481 |
|
| 482 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 483 |
PyObject *rsa_capsule = NULL; |
| 484 |
int sign_algo = 0; |
| 485 |
struct aws_byte_cursor digest_cur; |
| 486 |
if (!PyArg_ParseTuple(args, "Oiy#", &rsa_capsule, &sign_algo, &digest_cur.ptr, &digest_cur.len)) { |
| 487 |
return NULL; |
| 488 |
} |
| 489 |
|
| 490 |
struct aws_rsa_key_pair *rsa = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa); |
| 491 |
if (rsa == NULL) { |
| 492 |
return NULL; |
| 493 |
} |
| 494 |
|
| 495 |
struct aws_byte_buf result_buf; |
| 496 |
aws_byte_buf_init(&result_buf, allocator, aws_rsa_key_pair_signature_length(rsa)); |
| 497 |
|
| 498 |
if (aws_rsa_key_pair_sign_message(rsa, sign_algo, digest_cur, &result_buf)) { |
| 499 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 500 |
return PyErr_AwsLastError(); |
| 501 |
} |
| 502 |
|
| 503 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 504 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 505 |
return ret; |
| 506 |
} |
| 507 |
|
| 508 |
PyObject *aws_py_rsa_verify(PyObject *self, PyObject *args) { |
| 509 |
(void)self; |
| 510 |
|
| 511 |
PyObject *rsa_capsule = NULL; |
| 512 |
int sign_algo = 0; |
| 513 |
struct aws_byte_cursor digest_cur; |
| 514 |
struct aws_byte_cursor signature_cur; |
| 515 |
if (!PyArg_ParseTuple( |
| 516 |
args, |
| 517 |
"Oiy#y#", |
| 518 |
&rsa_capsule, |
| 519 |
&sign_algo, |
| 520 |
&digest_cur.ptr, |
| 521 |
&digest_cur.len, |
| 522 |
&signature_cur.ptr, |
| 523 |
&signature_cur.len)) { |
| 524 |
return NULL; |
| 525 |
} |
| 526 |
|
| 527 |
struct aws_rsa_key_pair *rsa = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa); |
| 528 |
if (rsa == NULL) { |
| 529 |
return NULL; |
| 530 |
} |
| 531 |
|
| 532 |
if (aws_rsa_key_pair_verify_signature(rsa, sign_algo, digest_cur, signature_cur)) { |
| 533 |
if (aws_last_error() == AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED) { |
| 534 |
aws_reset_error(); |
| 535 |
Py_RETURN_FALSE; |
| 536 |
} |
| 537 |
return PyErr_AwsLastError(); |
| 538 |
} |
| 539 |
|
| 540 |
Py_RETURN_TRUE; |
| 541 |
} |
| 542 |
|
| 543 |
static void s_ed25519_destructor(PyObject *ed25519_capsule) { |
| 544 |
struct aws_ed25519_key_pair *key_pair = PyCapsule_GetPointer(ed25519_capsule, s_capsule_name_ed25519); |
| 545 |
assert(key_pair); |
| 546 |
|
| 547 |
aws_ed25519_key_pair_release(key_pair); |
| 548 |
} |
| 549 |
|
| 550 |
PyObject *aws_py_ed25519_new_generate(PyObject *self, PyObject *args) { |
| 551 |
(void)self; |
| 552 |
(void)args; |
| 553 |
|
| 554 |
PyObject *capsule = NULL; |
| 555 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 556 |
|
| 557 |
struct aws_ed25519_key_pair *key_pair = aws_ed25519_key_pair_new_generate(allocator); |
| 558 |
|
| 559 |
if (key_pair == NULL) { |
| 560 |
PyErr_AwsLastError(); |
| 561 |
goto on_done; |
| 562 |
} |
| 563 |
|
| 564 |
capsule = PyCapsule_New(key_pair, s_capsule_name_ed25519, s_ed25519_destructor); |
| 565 |
|
| 566 |
if (capsule == NULL) { |
| 567 |
aws_ed25519_key_pair_release(key_pair); |
| 568 |
} |
| 569 |
|
| 570 |
on_done: |
| 571 |
return capsule; |
| 572 |
} |
| 573 |
|
| 574 |
PyObject *aws_py_ed25519_export_public_key(PyObject *self, PyObject *args) { |
| 575 |
(void)self; |
| 576 |
PyObject *ed25519_capsule = NULL; |
| 577 |
int export_format = 0; |
| 578 |
|
| 579 |
if (!PyArg_ParseTuple(args, "Oi", &ed25519_capsule, &export_format)) { |
| 580 |
return NULL; |
| 581 |
} |
| 582 |
|
| 583 |
struct aws_ed25519_key_pair *ed25519 = PyCapsule_GetPointer(ed25519_capsule, s_capsule_name_ed25519); |
| 584 |
if (ed25519 == NULL) { |
| 585 |
return NULL; |
| 586 |
} |
| 587 |
|
| 588 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 589 |
struct aws_byte_buf result_buf; |
| 590 |
aws_byte_buf_init(&result_buf, allocator, aws_ed25519_key_pair_get_public_key_size(export_format)); |
| 591 |
|
| 592 |
if (aws_ed25519_key_pair_get_public_key(ed25519, export_format, &result_buf)) { |
| 593 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 594 |
return PyErr_AwsLastError(); |
| 595 |
} |
| 596 |
|
| 597 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 598 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 599 |
return ret; |
| 600 |
} |
| 601 |
|
| 602 |
PyObject *aws_py_ed25519_export_private_key(PyObject *self, PyObject *args) { |
| 603 |
(void)self; |
| 604 |
PyObject *ed25519_capsule = NULL; |
| 605 |
int export_format = 0; |
| 606 |
|
| 607 |
if (!PyArg_ParseTuple(args, "Oi", &ed25519_capsule, &export_format)) { |
| 608 |
return NULL; |
| 609 |
} |
| 610 |
|
| 611 |
struct aws_ed25519_key_pair *ed25519 = PyCapsule_GetPointer(ed25519_capsule, s_capsule_name_ed25519); |
| 612 |
if (ed25519 == NULL) { |
| 613 |
return NULL; |
| 614 |
} |
| 615 |
|
| 616 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 617 |
struct aws_byte_buf result_buf; |
| 618 |
aws_byte_buf_init(&result_buf, allocator, aws_ed25519_key_pair_get_private_key_size(export_format)); |
| 619 |
|
| 620 |
if (aws_ed25519_key_pair_get_private_key(ed25519, export_format, &result_buf)) { |
| 621 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 622 |
return PyErr_AwsLastError(); |
| 623 |
} |
| 624 |
|
| 625 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 626 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 627 |
return ret; |
| 628 |
} |
| 629 |
|
| 630 |
static void s_ec_destructor(PyObject *ec_capsule) { |
| 631 |
struct aws_ecc_key_pair *key_pair = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec); |
| 632 |
assert(key_pair); |
| 633 |
|
| 634 |
aws_ecc_key_pair_release(key_pair); |
| 635 |
} |
| 636 |
|
| 637 |
PyObject *aws_py_ec_new_generate(PyObject *self, PyObject *args) { |
| 638 |
(void)self; |
| 639 |
(void)args; |
| 640 |
|
| 641 |
int ec_type = 0; |
| 642 |
if (!PyArg_ParseTuple(args, "i", &ec_type)) { |
| 643 |
return NULL; |
| 644 |
} |
| 645 |
|
| 646 |
PyObject *capsule = NULL; |
| 647 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 648 |
|
| 649 |
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_generate_random(allocator, ec_type); |
| 650 |
|
| 651 |
if (key_pair == NULL) { |
| 652 |
return PyErr_AwsLastError(); |
| 653 |
} |
| 654 |
|
| 655 |
capsule = PyCapsule_New(key_pair, s_capsule_name_ec, s_ec_destructor); |
| 656 |
|
| 657 |
if (capsule == NULL) { |
| 658 |
aws_ecc_key_pair_release(key_pair); |
| 659 |
} |
| 660 |
|
| 661 |
return capsule; |
| 662 |
} |
| 663 |
|
| 664 |
PyObject *aws_py_ec_key_from_der_data(PyObject *self, PyObject *args) { |
| 665 |
(void)self; |
| 666 |
|
| 667 |
struct aws_byte_cursor der_data_cur; |
| 668 |
if (!PyArg_ParseTuple(args, "y#", &der_data_cur.ptr, &der_data_cur.len)) { |
| 669 |
return NULL; |
| 670 |
} |
| 671 |
|
| 672 |
PyObject *capsule = NULL; |
| 673 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 674 |
|
| 675 |
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_from_asn1(allocator, &der_data_cur); |
| 676 |
|
| 677 |
if (key_pair == NULL) { |
| 678 |
PyErr_AwsLastError(); |
| 679 |
goto on_done; |
| 680 |
} |
| 681 |
|
| 682 |
capsule = PyCapsule_New(key_pair, s_capsule_name_ec, s_ec_destructor); |
| 683 |
|
| 684 |
if (capsule == NULL) { |
| 685 |
aws_ecc_key_pair_release(key_pair); |
| 686 |
} |
| 687 |
|
| 688 |
on_done: |
| 689 |
return capsule; |
| 690 |
} |
| 691 |
|
| 692 |
PyObject *aws_py_ec_export_key(PyObject *self, PyObject *args) { |
| 693 |
(void)self; |
| 694 |
PyObject *ec_capsule = NULL; |
| 695 |
int export_format = 0; |
| 696 |
|
| 697 |
if (!PyArg_ParseTuple(args, "Oi", &ec_capsule, &export_format)) { |
| 698 |
return NULL; |
| 699 |
} |
| 700 |
|
| 701 |
struct aws_ecc_key_pair *ec = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec); |
| 702 |
if (ec == NULL) { |
| 703 |
return NULL; |
| 704 |
} |
| 705 |
|
| 706 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 707 |
struct aws_byte_buf result_buf; |
| 708 |
|
| 709 |
aws_byte_buf_init(&result_buf, allocator, 512); |
| 710 |
|
| 711 |
if (aws_ecc_key_pair_export(ec, export_format, &result_buf)) { |
| 712 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 713 |
return PyErr_AwsLastError(); |
| 714 |
} |
| 715 |
|
| 716 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 717 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 718 |
return ret; |
| 719 |
} |
| 720 |
|
| 721 |
PyObject *aws_py_ec_sign(PyObject *self, PyObject *args) { |
| 722 |
(void)self; |
| 723 |
|
| 724 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 725 |
PyObject *ec_capsule = NULL; |
| 726 |
struct aws_byte_cursor digest_cur; |
| 727 |
if (!PyArg_ParseTuple(args, "Oy#", &ec_capsule, &digest_cur.ptr, &digest_cur.len)) { |
| 728 |
return NULL; |
| 729 |
} |
| 730 |
|
| 731 |
struct aws_ecc_key_pair *ec = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec); |
| 732 |
if (ec == NULL) { |
| 733 |
return NULL; |
| 734 |
} |
| 735 |
|
| 736 |
struct aws_byte_buf result_buf; |
| 737 |
aws_byte_buf_init(&result_buf, allocator, aws_ecc_key_pair_signature_length(ec)); |
| 738 |
|
| 739 |
if (aws_ecc_key_pair_sign_message(ec, &digest_cur, &result_buf)) { |
| 740 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 741 |
return PyErr_AwsLastError(); |
| 742 |
} |
| 743 |
|
| 744 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 745 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 746 |
return ret; |
| 747 |
} |
| 748 |
|
| 749 |
PyObject *aws_py_ec_verify(PyObject *self, PyObject *args) { |
| 750 |
(void)self; |
| 751 |
|
| 752 |
PyObject *ec_capsule = NULL; |
| 753 |
struct aws_byte_cursor digest_cur; |
| 754 |
struct aws_byte_cursor signature_cur; |
| 755 |
if (!PyArg_ParseTuple( |
| 756 |
args, "Oy#y#", &ec_capsule, &digest_cur.ptr, &digest_cur.len, &signature_cur.ptr, &signature_cur.len)) { |
| 757 |
return NULL; |
| 758 |
} |
| 759 |
|
| 760 |
struct aws_ecc_key_pair *ec = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec); |
| 761 |
if (ec == NULL) { |
| 762 |
return NULL; |
| 763 |
} |
| 764 |
|
| 765 |
if (aws_ecc_key_pair_verify_signature(ec, &digest_cur, &signature_cur)) { |
| 766 |
if (aws_last_error() == AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED) { |
| 767 |
aws_reset_error(); |
| 768 |
Py_RETURN_FALSE; |
| 769 |
} |
| 770 |
return PyErr_AwsLastError(); |
| 771 |
} |
| 772 |
|
| 773 |
Py_RETURN_TRUE; |
| 774 |
} |
| 775 |
|
| 776 |
PyObject *aws_py_ec_encode_signature(PyObject *self, PyObject *args) { |
| 777 |
(void)self; |
| 778 |
|
| 779 |
PyObject *signature; |
| 780 |
PyObject *r_bytes; |
| 781 |
PyObject *s_bytes; |
| 782 |
|
| 783 |
if (!PyArg_ParseTuple(args, "O", &signature)) |
| 784 |
return NULL; |
| 785 |
|
| 786 |
r_bytes = PyObject_GetAttrString(signature, "r"); |
| 787 |
if (!r_bytes) |
| 788 |
return NULL; |
| 789 |
|
| 790 |
s_bytes = PyObject_GetAttrString(signature, "s"); |
| 791 |
if (!s_bytes) { |
| 792 |
Py_DECREF(r_bytes); |
| 793 |
return NULL; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
if (!PyBytes_Check(r_bytes) || !PyBytes_Check(s_bytes)) { |
| 798 |
PyErr_SetString(PyExc_TypeError, "r and s must be bytes"); |
| 799 |
Py_DECREF(r_bytes); |
| 800 |
Py_DECREF(s_bytes); |
| 801 |
return NULL; |
| 802 |
} |
| 803 |
|
| 804 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 805 |
|
| 806 |
struct aws_byte_cursor r_cur = aws_byte_cursor_from_pybytes(r_bytes); |
| 807 |
struct aws_byte_cursor s_cur = aws_byte_cursor_from_pybytes(s_bytes); |
| 808 |
|
| 809 |
size_t buf_size = r_cur.len + s_cur.len + 32; |
| 810 |
if (buf_size > 256) { |
| 811 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 812 |
goto on_error; |
| 813 |
} |
| 814 |
|
| 815 |
struct aws_byte_buf result_buf; |
| 816 |
aws_byte_buf_init(&result_buf, allocator, buf_size); |
| 817 |
|
| 818 |
if (aws_ecc_encode_signature_raw_to_der(allocator, r_cur, s_cur, &result_buf)) { |
| 819 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 820 |
goto on_error; |
| 821 |
} |
| 822 |
|
| 823 |
Py_DECREF(r_bytes); |
| 824 |
Py_DECREF(s_bytes); |
| 825 |
|
| 826 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 827 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 828 |
return ret; |
| 829 |
|
| 830 |
on_error: |
| 831 |
Py_DECREF(r_bytes); |
| 832 |
Py_DECREF(s_bytes); |
| 833 |
return PyErr_AwsLastError(); |
| 834 |
} |
| 835 |
|
| 836 |
PyObject *aws_py_ec_decode_signature(PyObject *self, PyObject *args) { |
| 837 |
(void)self; |
| 838 |
|
| 839 |
struct aws_byte_cursor signature_cur; |
| 840 |
if (!PyArg_ParseTuple(args, "y#", &signature_cur.ptr, &signature_cur.len)) { |
| 841 |
return NULL; |
| 842 |
} |
| 843 |
|
| 844 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 845 |
struct aws_byte_cursor r_cur = {0}; |
| 846 |
struct aws_byte_cursor s_cur = {0}; |
| 847 |
if (aws_ecc_decode_signature_der_to_raw(allocator, signature_cur, &r_cur, &s_cur)) { |
| 848 |
return PyErr_AwsLastError(); |
| 849 |
} |
| 850 |
|
| 851 |
PyObject *result = PyTuple_New(2); |
| 852 |
if (!result) { |
| 853 |
return NULL; |
| 854 |
} |
| 855 |
|
| 856 |
PyTuple_SetItem(result, 0, PyBytes_FromStringAndSize((char *)r_cur.ptr, r_cur.len)); |
| 857 |
PyTuple_SetItem(result, 1, PyBytes_FromStringAndSize((char *)s_cur.ptr, s_cur.len)); |
| 858 |
|
| 859 |
return result; |
| 860 |
} |
| 861 |
|
| 862 |
PyObject *aws_py_ec_decode_signature_to_padded_pair(PyObject *self, PyObject *args) { |
| 863 |
(void)self; |
| 864 |
|
| 865 |
Py_ssize_t pad_to; |
| 866 |
struct aws_byte_cursor signature_cur; |
| 867 |
if (!PyArg_ParseTuple(args, "y#n", &signature_cur.ptr, &signature_cur.len, &pad_to)) { |
| 868 |
return NULL; |
| 869 |
} |
| 870 |
|
| 871 |
if (pad_to > 256) { |
| 872 |
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 873 |
return PyErr_AwsLastError(); |
| 874 |
} |
| 875 |
|
| 876 |
struct aws_allocator *allocator = aws_py_get_allocator(); |
| 877 |
size_t buf_size = pad_to * 2; |
| 878 |
struct aws_byte_buf result_buf; |
| 879 |
aws_byte_buf_init(&result_buf, allocator, buf_size); |
| 880 |
|
| 881 |
if (aws_ecc_decode_signature_der_to_raw_padded(allocator, signature_cur, &result_buf, pad_to)) { |
| 882 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 883 |
return PyErr_AwsLastError(); |
| 884 |
} |
| 885 |
|
| 886 |
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len); |
| 887 |
aws_byte_buf_clean_up_secure(&result_buf); |
| 888 |
return ret; |
| 889 |
} |
| 890 |
|
| 891 |
PyObject *aws_py_ec_get_public_coords(PyObject *self, PyObject *args) { |
| 892 |
(void)self; |
| 893 |
|
| 894 |
PyObject *ec_capsule = NULL; |
| 895 |
if (!PyArg_ParseTuple(args, "O", &ec_capsule)) { |
| 896 |
return NULL; |
| 897 |
} |
| 898 |
|
| 899 |
struct aws_ecc_key_pair *ec = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec); |
| 900 |
if (ec == NULL) { |
| 901 |
return NULL; |
| 902 |
} |
| 903 |
|
| 904 |
struct aws_byte_cursor x_cur = {0}; |
| 905 |
struct aws_byte_cursor y_cur = {0}; |
| 906 |
aws_ecc_key_pair_get_public_key(ec, &x_cur, &y_cur); |
| 907 |
|
| 908 |
PyObject *result = PyTuple_New(2); |
| 909 |
if (!result) { |
| 910 |
return NULL; |
| 911 |
} |
| 912 |
|
| 913 |
PyTuple_SetItem(result, 0, PyBytes_FromStringAndSize((char *)x_cur.ptr, x_cur.len)); |
| 914 |
PyTuple_SetItem(result, 1, PyBytes_FromStringAndSize((char *)y_cur.ptr, y_cur.len)); |
| 915 |
|
| 916 |
return result; |
| 917 |
} |