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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include "crypto.h"
#include "aws/cal/ecc.h"
#include "aws/cal/ed25519.h"
#include "aws/cal/hash.h"
#include "aws/cal/hmac.h"
#include "aws/cal/rsa.h"
#include "aws/common/encoding.h"
#include "aws/io/pem.h"
const char *s_capsule_name_hash = "aws_hash";
const char *s_capsule_name_hmac = "aws_hmac";
const char *s_capsule_name_rsa = "aws_rsa";
const char *s_capsule_name_ed25519 = "aws_ed25519";
const char *s_capsule_name_ec = "aws_ec";
static void s_hash_destructor(PyObject *hash_capsule) {
assert(PyCapsule_CheckExact(hash_capsule));
struct aws_hash *hash = PyCapsule_GetPointer(hash_capsule, s_capsule_name_hash);
assert(hash);
aws_hash_destroy(hash);
}
static void s_hmac_destructor(PyObject *hmac_capsule) {
assert(PyCapsule_CheckExact(hmac_capsule));
struct aws_hmac *hmac = PyCapsule_GetPointer(hmac_capsule, s_capsule_name_hmac);
assert(hmac);
aws_hmac_destroy(hmac);
}
PyObject *aws_py_sha1_new(PyObject *self, PyObject *args) {
(void)self;
(void)args;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_hash *sha1 = aws_sha1_new(allocator);
if (!sha1) {
return PyErr_AwsLastError();
}
PyObject *capsule = PyCapsule_New(sha1, s_capsule_name_hash, s_hash_destructor);
if (capsule == NULL) {
aws_hash_destroy(sha1);
return NULL;
}
return capsule;
}
PyObject *aws_py_sha256_new(PyObject *self, PyObject *args) {
(void)self;
(void)args;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_hash *sha256 = aws_sha256_new(allocator);
if (!sha256) {
return PyErr_AwsLastError();
}
PyObject *capsule = PyCapsule_New(sha256, s_capsule_name_hash, s_hash_destructor);
if (capsule == NULL) {
aws_hash_destroy(sha256);
return NULL;
}
return capsule;
}
PyObject *aws_py_md5_new(PyObject *self, PyObject *args) {
(void)self;
(void)args;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_hash *md5 = aws_md5_new(allocator);
if (!md5) {
return PyErr_AwsLastError();
}
PyObject *capsule = PyCapsule_New(md5, s_capsule_name_hash, s_hash_destructor);
if (capsule == NULL) {
aws_hash_destroy(md5);
return NULL;
}
return capsule;
}
PyObject *aws_py_hash_update(PyObject *self, PyObject *args) {
(void)self;
PyObject *hash_capsule = NULL;
const char *to_hash_c_str;
Py_ssize_t to_hash_len;
if (!PyArg_ParseTuple(args, "Os#", &hash_capsule, &to_hash_c_str, &to_hash_len)) {
return PyErr_AwsLastError();
}
struct aws_hash *hash = PyCapsule_GetPointer(hash_capsule, s_capsule_name_hash);
if (!hash) {
return PyErr_AwsLastError();
}
struct aws_byte_cursor to_hash_cursor;
to_hash_cursor = aws_byte_cursor_from_array(to_hash_c_str, to_hash_len);
/* Releasing the GIL for very small buffers is inefficient
and may lower performance */
if (to_hash_len > 1024 * 5) {
int aws_op = AWS_OP_SUCCESS;
/* clang-format off */
Py_BEGIN_ALLOW_THREADS
aws_op = aws_hash_update(hash, &to_hash_cursor);
Py_END_ALLOW_THREADS
if (aws_op != AWS_OP_SUCCESS) {
return PyErr_AwsLastError();
}
/* clang-format on */
} else {
if (aws_hash_update(hash, &to_hash_cursor)) {
return PyErr_AwsLastError();
}
}
Py_RETURN_NONE;
}
PyObject *aws_py_hash_digest(PyObject *self, PyObject *args) {
(void)self;
PyObject *hash_capsule = NULL;
Py_ssize_t truncate_to;
if (!PyArg_ParseTuple(args, "On", &hash_capsule, &truncate_to)) {
return PyErr_AwsLastError();
}
struct aws_hash *hash = PyCapsule_GetPointer(hash_capsule, s_capsule_name_hash);
if (!hash) {
return PyErr_AwsLastError();
}
uint8_t output[128] = {0};
struct aws_byte_buf digest_buf = aws_byte_buf_from_array(output, hash->digest_size);
digest_buf.len = 0;
if (aws_hash_finalize(hash, &digest_buf, truncate_to)) {
return PyErr_AwsLastError();
}
return PyBytes_FromStringAndSize((const char *)output, digest_buf.len);
}
PyObject *aws_py_sha256_hmac_new(PyObject *self, PyObject *args) {
(void)self;
struct aws_allocator *allocator = aws_py_get_allocator();
const char *secret_ptr;
Py_ssize_t secret_len;
if (!PyArg_ParseTuple(args, "s#", &secret_ptr, &secret_len)) {
return PyErr_AwsLastError();
}
struct aws_byte_cursor secret_cursor;
secret_cursor = aws_byte_cursor_from_array(secret_ptr, secret_len);
struct aws_hmac *sha256_hmac = aws_sha256_hmac_new(allocator, &secret_cursor);
if (!sha256_hmac) {
return PyErr_AwsLastError();
}
return PyCapsule_New(sha256_hmac, s_capsule_name_hmac, s_hmac_destructor);
}
PyObject *aws_py_hmac_update(PyObject *self, PyObject *args) {
(void)self;
PyObject *hmac_capsule = NULL;
const char *to_hmac_ptr;
Py_ssize_t to_hmac_len;
if (!PyArg_ParseTuple(args, "Os#", &hmac_capsule, &to_hmac_ptr, &to_hmac_len)) {
return PyErr_AwsLastError();
}
struct aws_hmac *hmac = PyCapsule_GetPointer(hmac_capsule, s_capsule_name_hmac);
if (!hmac) {
return PyErr_AwsLastError();
}
struct aws_byte_cursor to_hmac_cursor;
to_hmac_cursor = aws_byte_cursor_from_array(to_hmac_ptr, to_hmac_len);
if (aws_hmac_update(hmac, &to_hmac_cursor)) {
return PyErr_AwsLastError();
}
Py_RETURN_NONE;
}
PyObject *aws_py_hmac_digest(PyObject *self, PyObject *args) {
(void)self;
PyObject *hmac_capsule = NULL;
Py_ssize_t truncate_to;
if (!PyArg_ParseTuple(args, "On", &hmac_capsule, &truncate_to)) {
return PyErr_AwsLastError();
}
struct aws_hmac *hmac = PyCapsule_GetPointer(hmac_capsule, s_capsule_name_hmac);
if (!hmac) {
return PyErr_AwsLastError();
}
uint8_t output[128] = {0};
struct aws_byte_buf digest_buf = aws_byte_buf_from_array(output, hmac->digest_size);
digest_buf.len = 0;
if (aws_hmac_finalize(hmac, &digest_buf, truncate_to)) {
return PyErr_AwsLastError();
}
return PyBytes_FromStringAndSize((const char *)output, digest_buf.len);
}
static void s_rsa_destructor(PyObject *rsa_capsule) {
struct aws_rsa_key_pair *key_pair = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa);
assert(key_pair);
aws_rsa_key_pair_release(key_pair);
}
struct aws_pem_object *s_find_pem_object(struct aws_array_list *pem_list, enum aws_pem_object_type pem_type) {
for (size_t i = 0; i < aws_array_list_length(pem_list); ++i) {
struct aws_pem_object *pem_object = NULL;
if (aws_array_list_get_at_ptr(pem_list, (void **)&pem_object, 0)) {
return NULL;
}
if (pem_object->type == pem_type) {
return pem_object;
}
}
return NULL;
}
PyObject *aws_py_rsa_private_key_from_pem_data(PyObject *self, PyObject *args) {
(void)self;
struct aws_byte_cursor pem_data_cur;
if (!PyArg_ParseTuple(args, "s#", &pem_data_cur.ptr, &pem_data_cur.len)) {
return NULL;
}
PyObject *capsule = NULL;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_array_list pem_list;
if (aws_pem_objects_init_from_file_contents(&pem_list, allocator, pem_data_cur)) {
return PyErr_AwsLastError();
}
/* From hereon, we need to clean up if errors occur */
struct aws_pem_object *found_pem_object = s_find_pem_object(&pem_list, AWS_PEM_TYPE_PRIVATE_RSA_PKCS1);
struct aws_rsa_key_pair *key_pair = NULL;
if (found_pem_object != NULL) {
key_pair =
aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, aws_byte_cursor_from_buf(&found_pem_object->data));
} else {
found_pem_object = s_find_pem_object(&pem_list, AWS_PEM_TYPE_PRIVATE_PKCS8);
if (found_pem_object != NULL) {
key_pair = aws_rsa_key_pair_new_from_private_key_pkcs8(
allocator, aws_byte_cursor_from_buf(&found_pem_object->data));
} else {
PyErr_SetString(PyExc_ValueError, "RSA private key not found in PEM.");
goto on_done;
}
}
if (key_pair == NULL) {
PyErr_AwsLastError();
goto on_done;
}
capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor);
if (capsule == NULL) {
aws_rsa_key_pair_release(key_pair);
}
on_done:
aws_pem_objects_clean_up(&pem_list);
return capsule;
}
PyObject *aws_py_rsa_public_key_from_pem_data(PyObject *self, PyObject *args) {
(void)self;
struct aws_byte_cursor pem_data_cur;
if (!PyArg_ParseTuple(args, "s#", &pem_data_cur.ptr, &pem_data_cur.len)) {
return NULL;
}
PyObject *capsule = NULL;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_array_list pem_list;
if (aws_pem_objects_init_from_file_contents(&pem_list, allocator, pem_data_cur)) {
return PyErr_AwsLastError();
}
/* From hereon, we need to clean up if errors occur */
struct aws_pem_object *found_pem_object = s_find_pem_object(&pem_list, AWS_PEM_TYPE_PUBLIC_RSA_PKCS1);
if (found_pem_object == NULL) {
PyErr_SetString(PyExc_ValueError, "RSA public key not found in PEM.");
goto on_done;
}
struct aws_rsa_key_pair *key_pair =
aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, aws_byte_cursor_from_buf(&found_pem_object->data));
if (key_pair == NULL) {
PyErr_AwsLastError();
goto on_done;
}
capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor);
if (capsule == NULL) {
aws_rsa_key_pair_release(key_pair);
}
on_done:
aws_pem_objects_clean_up(&pem_list);
return capsule;
}
PyObject *aws_py_rsa_private_key_from_der_data(PyObject *self, PyObject *args) {
(void)self;
struct aws_byte_cursor der_data_cur;
if (!PyArg_ParseTuple(args, "s#", &der_data_cur.ptr, &der_data_cur.len)) {
return NULL;
}
PyObject *capsule = NULL;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_rsa_key_pair *key_pair = aws_rsa_key_pair_new_from_private_key_pkcs1(allocator, der_data_cur);
if (key_pair == NULL) {
PyErr_AwsLastError();
goto on_done;
}
capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor);
if (capsule == NULL) {
aws_rsa_key_pair_release(key_pair);
}
on_done:
return capsule;
}
PyObject *aws_py_rsa_public_key_from_der_data(PyObject *self, PyObject *args) {
(void)self;
struct aws_byte_cursor der_data_cur;
if (!PyArg_ParseTuple(args, "s#", &der_data_cur.ptr, &der_data_cur.len)) {
return NULL;
}
PyObject *capsule = NULL;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_rsa_key_pair *key_pair = aws_rsa_key_pair_new_from_public_key_pkcs1(allocator, der_data_cur);
if (key_pair == NULL) {
PyErr_AwsLastError();
goto on_done;
}
capsule = PyCapsule_New(key_pair, s_capsule_name_rsa, s_rsa_destructor);
if (capsule == NULL) {
aws_rsa_key_pair_release(key_pair);
}
on_done:
return capsule;
}
PyObject *aws_py_rsa_encrypt(PyObject *self, PyObject *args) {
(void)self;
struct aws_allocator *allocator = aws_py_get_allocator();
PyObject *rsa_capsule = NULL;
int encrypt_algo = 0;
struct aws_byte_cursor plaintext_cur;
if (!PyArg_ParseTuple(args, "Oiy#", &rsa_capsule, &encrypt_algo, &plaintext_cur.ptr, &plaintext_cur.len)) {
return NULL;
}
struct aws_rsa_key_pair *rsa = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa);
if (rsa == NULL) {
return NULL;
}
struct aws_byte_buf result_buf;
aws_byte_buf_init(&result_buf, allocator, aws_rsa_key_pair_block_length(rsa));
if (aws_rsa_key_pair_encrypt(rsa, encrypt_algo, plaintext_cur, &result_buf)) {
aws_byte_buf_clean_up_secure(&result_buf);
return PyErr_AwsLastError();
}
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len);
aws_byte_buf_clean_up_secure(&result_buf);
return ret;
}
PyObject *aws_py_rsa_decrypt(PyObject *self, PyObject *args) {
(void)self;
struct aws_allocator *allocator = aws_py_get_allocator();
PyObject *rsa_capsule = NULL;
int encrypt_algo = 0;
struct aws_byte_cursor ciphertext_cur;
if (!PyArg_ParseTuple(args, "Oiy#", &rsa_capsule, &encrypt_algo, &ciphertext_cur.ptr, &ciphertext_cur.len)) {
return NULL;
}
struct aws_rsa_key_pair *rsa = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa);
if (rsa == NULL) {
return NULL;
}
struct aws_byte_buf result_buf;
aws_byte_buf_init(&result_buf, allocator, aws_rsa_key_pair_block_length(rsa));
if (aws_rsa_key_pair_decrypt(rsa, encrypt_algo, ciphertext_cur, &result_buf)) {
aws_byte_buf_clean_up_secure(&result_buf);
return PyErr_AwsLastError();
}
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len);
aws_byte_buf_clean_up_secure(&result_buf);
return ret;
}
PyObject *aws_py_rsa_sign(PyObject *self, PyObject *args) {
(void)self;
struct aws_allocator *allocator = aws_py_get_allocator();
PyObject *rsa_capsule = NULL;
int sign_algo = 0;
struct aws_byte_cursor digest_cur;
if (!PyArg_ParseTuple(args, "Oiy#", &rsa_capsule, &sign_algo, &digest_cur.ptr, &digest_cur.len)) {
return NULL;
}
struct aws_rsa_key_pair *rsa = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa);
if (rsa == NULL) {
return NULL;
}
struct aws_byte_buf result_buf;
aws_byte_buf_init(&result_buf, allocator, aws_rsa_key_pair_signature_length(rsa));
if (aws_rsa_key_pair_sign_message(rsa, sign_algo, digest_cur, &result_buf)) {
aws_byte_buf_clean_up_secure(&result_buf);
return PyErr_AwsLastError();
}
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len);
aws_byte_buf_clean_up_secure(&result_buf);
return ret;
}
PyObject *aws_py_rsa_verify(PyObject *self, PyObject *args) {
(void)self;
PyObject *rsa_capsule = NULL;
int sign_algo = 0;
struct aws_byte_cursor digest_cur;
struct aws_byte_cursor signature_cur;
if (!PyArg_ParseTuple(
args,
"Oiy#y#",
&rsa_capsule,
&sign_algo,
&digest_cur.ptr,
&digest_cur.len,
&signature_cur.ptr,
&signature_cur.len)) {
return NULL;
}
struct aws_rsa_key_pair *rsa = PyCapsule_GetPointer(rsa_capsule, s_capsule_name_rsa);
if (rsa == NULL) {
return NULL;
}
if (aws_rsa_key_pair_verify_signature(rsa, sign_algo, digest_cur, signature_cur)) {
if (aws_last_error() == AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED) {
aws_reset_error();
Py_RETURN_FALSE;
}
return PyErr_AwsLastError();
}
Py_RETURN_TRUE;
}
static void s_ed25519_destructor(PyObject *ed25519_capsule) {
struct aws_ed25519_key_pair *key_pair = PyCapsule_GetPointer(ed25519_capsule, s_capsule_name_ed25519);
assert(key_pair);
aws_ed25519_key_pair_release(key_pair);
}
PyObject *aws_py_ed25519_new_generate(PyObject *self, PyObject *args) {
(void)self;
(void)args;
PyObject *capsule = NULL;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_ed25519_key_pair *key_pair = aws_ed25519_key_pair_new_generate(allocator);
if (key_pair == NULL) {
PyErr_AwsLastError();
goto on_done;
}
capsule = PyCapsule_New(key_pair, s_capsule_name_ed25519, s_ed25519_destructor);
if (capsule == NULL) {
aws_ed25519_key_pair_release(key_pair);
}
on_done:
return capsule;
}
PyObject *aws_py_ed25519_export_public_key(PyObject *self, PyObject *args) {
(void)self;
PyObject *ed25519_capsule = NULL;
int export_format = 0;
if (!PyArg_ParseTuple(args, "Oi", &ed25519_capsule, &export_format)) {
return NULL;
}
struct aws_ed25519_key_pair *ed25519 = PyCapsule_GetPointer(ed25519_capsule, s_capsule_name_ed25519);
if (ed25519 == NULL) {
return NULL;
}
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_byte_buf result_buf;
aws_byte_buf_init(&result_buf, allocator, aws_ed25519_key_pair_get_public_key_size(export_format));
if (aws_ed25519_key_pair_get_public_key(ed25519, export_format, &result_buf)) {
aws_byte_buf_clean_up_secure(&result_buf);
return PyErr_AwsLastError();
}
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len);
aws_byte_buf_clean_up_secure(&result_buf);
return ret;
}
PyObject *aws_py_ed25519_export_private_key(PyObject *self, PyObject *args) {
(void)self;
PyObject *ed25519_capsule = NULL;
int export_format = 0;
if (!PyArg_ParseTuple(args, "Oi", &ed25519_capsule, &export_format)) {
return NULL;
}
struct aws_ed25519_key_pair *ed25519 = PyCapsule_GetPointer(ed25519_capsule, s_capsule_name_ed25519);
if (ed25519 == NULL) {
return NULL;
}
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_byte_buf result_buf;
aws_byte_buf_init(&result_buf, allocator, aws_ed25519_key_pair_get_private_key_size(export_format));
if (aws_ed25519_key_pair_get_private_key(ed25519, export_format, &result_buf)) {
aws_byte_buf_clean_up_secure(&result_buf);
return PyErr_AwsLastError();
}
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len);
aws_byte_buf_clean_up_secure(&result_buf);
return ret;
}
static void s_ec_destructor(PyObject *ec_capsule) {
struct aws_ecc_key_pair *key_pair = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec);
assert(key_pair);
aws_ecc_key_pair_release(key_pair);
}
PyObject *aws_py_ec_new_generate(PyObject *self, PyObject *args) {
(void)self;
(void)args;
int ec_type = 0;
if (!PyArg_ParseTuple(args, "i", &ec_type)) {
return NULL;
}
PyObject *capsule = NULL;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_generate_random(allocator, ec_type);
if (key_pair == NULL) {
return PyErr_AwsLastError();
}
capsule = PyCapsule_New(key_pair, s_capsule_name_ec, s_ec_destructor);
if (capsule == NULL) {
aws_ecc_key_pair_release(key_pair);
}
return capsule;
}
PyObject *aws_py_ec_key_from_der_data(PyObject *self, PyObject *args) {
(void)self;
struct aws_byte_cursor der_data_cur;
if (!PyArg_ParseTuple(args, "y#", &der_data_cur.ptr, &der_data_cur.len)) {
return NULL;
}
PyObject *capsule = NULL;
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_from_asn1(allocator, &der_data_cur);
if (key_pair == NULL) {
PyErr_AwsLastError();
goto on_done;
}
capsule = PyCapsule_New(key_pair, s_capsule_name_ec, s_ec_destructor);
if (capsule == NULL) {
aws_ecc_key_pair_release(key_pair);
}
on_done:
return capsule;
}
PyObject *aws_py_ec_export_key(PyObject *self, PyObject *args) {
(void)self;
PyObject *ec_capsule = NULL;
int export_format = 0;
if (!PyArg_ParseTuple(args, "Oi", &ec_capsule, &export_format)) {
return NULL;
}
struct aws_ecc_key_pair *ec = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec);
if (ec == NULL) {
return NULL;
}
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_byte_buf result_buf;
/* all current curves max out at around 200 bytes in pkcs8, */
aws_byte_buf_init(&result_buf, allocator, 512);
if (aws_ecc_key_pair_export(ec, export_format, &result_buf)) {
aws_byte_buf_clean_up_secure(&result_buf);
return PyErr_AwsLastError();
}
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len);
aws_byte_buf_clean_up_secure(&result_buf);
return ret;
}
PyObject *aws_py_ec_sign(PyObject *self, PyObject *args) {
(void)self;
struct aws_allocator *allocator = aws_py_get_allocator();
PyObject *ec_capsule = NULL;
struct aws_byte_cursor digest_cur;
if (!PyArg_ParseTuple(args, "Oy#", &ec_capsule, &digest_cur.ptr, &digest_cur.len)) {
return NULL;
}
struct aws_ecc_key_pair *ec = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec);
if (ec == NULL) {
return NULL;
}
struct aws_byte_buf result_buf;
aws_byte_buf_init(&result_buf, allocator, aws_ecc_key_pair_signature_length(ec));
if (aws_ecc_key_pair_sign_message(ec, &digest_cur, &result_buf)) {
aws_byte_buf_clean_up_secure(&result_buf);
return PyErr_AwsLastError();
}
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len);
aws_byte_buf_clean_up_secure(&result_buf);
return ret;
}
PyObject *aws_py_ec_verify(PyObject *self, PyObject *args) {
(void)self;
PyObject *ec_capsule = NULL;
struct aws_byte_cursor digest_cur;
struct aws_byte_cursor signature_cur;
if (!PyArg_ParseTuple(
args, "Oy#y#", &ec_capsule, &digest_cur.ptr, &digest_cur.len, &signature_cur.ptr, &signature_cur.len)) {
return NULL;
}
struct aws_ecc_key_pair *ec = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec);
if (ec == NULL) {
return NULL;
}
if (aws_ecc_key_pair_verify_signature(ec, &digest_cur, &signature_cur)) {
if (aws_last_error() == AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED) {
aws_reset_error();
Py_RETURN_FALSE;
}
return PyErr_AwsLastError();
}
Py_RETURN_TRUE;
}
PyObject *aws_py_ec_encode_signature(PyObject *self, PyObject *args) {
(void)self;
PyObject *signature;
PyObject *r_bytes;
PyObject *s_bytes;
if (!PyArg_ParseTuple(args, "O", &signature))
return NULL;
r_bytes = PyObject_GetAttrString(signature, "r");
if (!r_bytes)
return NULL;
s_bytes = PyObject_GetAttrString(signature, "s");
if (!s_bytes) {
Py_DECREF(r_bytes);
return NULL;
}
/* Note: PyBytes_Check does not null check, hence explicit null check before. */
if (!PyBytes_Check(r_bytes) || !PyBytes_Check(s_bytes)) {
PyErr_SetString(PyExc_TypeError, "r and s must be bytes");
Py_DECREF(r_bytes);
Py_DECREF(s_bytes);
return NULL;
}
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_byte_cursor r_cur = aws_byte_cursor_from_pybytes(r_bytes);
struct aws_byte_cursor s_cur = aws_byte_cursor_from_pybytes(s_bytes);
size_t buf_size = r_cur.len + s_cur.len + 32; /* der has static overhead of couple bytes, just overallocate */
if (buf_size > 256) {
aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
goto on_error;
}
struct aws_byte_buf result_buf;
aws_byte_buf_init(&result_buf, allocator, buf_size);
if (aws_ecc_encode_signature_raw_to_der(allocator, r_cur, s_cur, &result_buf)) {
aws_byte_buf_clean_up_secure(&result_buf);
goto on_error;
}
Py_DECREF(r_bytes);
Py_DECREF(s_bytes);
PyObject *ret = PyBytes_FromStringAndSize((const char *)result_buf.buffer, result_buf.len);
aws_byte_buf_clean_up_secure(&result_buf);
return ret;
on_error:
Py_DECREF(r_bytes);
Py_DECREF(s_bytes);
return PyErr_AwsLastError();
}
PyObject *aws_py_ec_decode_signature(PyObject *self, PyObject *args) {
(void)self;
struct aws_byte_cursor signature_cur;
if (!PyArg_ParseTuple(args, "y#", &signature_cur.ptr, &signature_cur.len)) {
return NULL;
}
struct aws_allocator *allocator = aws_py_get_allocator();
struct aws_byte_cursor r_cur = {0};
struct aws_byte_cursor s_cur = {0};
if (aws_ecc_decode_signature_der_to_raw(allocator, signature_cur, &r_cur, &s_cur)) {
return PyErr_AwsLastError();
}
PyObject *result = PyTuple_New(2);
if (!result) {
return NULL;
}
PyTuple_SetItem(result, 0, PyBytes_FromStringAndSize((char *)r_cur.ptr, r_cur.len));
PyTuple_SetItem(result, 1, PyBytes_FromStringAndSize((char *)s_cur.ptr, s_cur.len));
return result;
}
PyObject *aws_py_ec_get_public_coords(PyObject *self, PyObject *args) {
(void)self;
PyObject *ec_capsule = NULL;
if (!PyArg_ParseTuple(args, "O", &ec_capsule)) {
return NULL;
}
struct aws_ecc_key_pair *ec = PyCapsule_GetPointer(ec_capsule, s_capsule_name_ec);
if (ec == NULL) {
return NULL;
}
struct aws_byte_cursor x_cur = {0};
struct aws_byte_cursor y_cur = {0};
aws_ecc_key_pair_get_public_key(ec, &x_cur, &y_cur);
PyObject *result = PyTuple_New(2);
if (!result) {
return NULL;
}
PyTuple_SetItem(result, 0, PyBytes_FromStringAndSize((char *)x_cur.ptr, x_cur.len));
PyTuple_SetItem(result, 1, PyBytes_FromStringAndSize((char *)y_cur.ptr, y_cur.len));
return result;
}
|