|
7 | 7 | from unstructured_client.models import errors, operations, shared
|
8 | 8 | from unstructured_client.types import BaseModel, OptionalNullable, UNSET
|
9 | 9 |
|
| 10 | +# region imports |
| 11 | +from cryptography import x509 |
| 12 | +from cryptography.hazmat.primitives import serialization, hashes |
| 13 | +from cryptography.hazmat.primitives.asymmetric import padding, rsa |
| 14 | +from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes |
| 15 | +from cryptography.hazmat.backends import default_backend |
| 16 | +import os |
| 17 | +import base64 |
| 18 | +# endregion imports |
10 | 19 |
|
11 | 20 | class Users(BaseSDK):
|
12 | 21 | def retrieve(
|
@@ -458,3 +467,160 @@ async def store_secret_async(
|
458 | 467 | http_res_text,
|
459 | 468 | http_res,
|
460 | 469 | )
|
| 470 | + |
| 471 | + # region sdk-class-body |
| 472 | + def _encrypt_rsa_aes( |
| 473 | + self, |
| 474 | + public_key: rsa.RSAPublicKey, |
| 475 | + plaintext: str, |
| 476 | + ) -> dict: |
| 477 | + # Generate a random AES key |
| 478 | + aes_key = os.urandom(32) # 256-bit AES key |
| 479 | + |
| 480 | + # Generate a random IV |
| 481 | + iv = os.urandom(16) |
| 482 | + |
| 483 | + # Encrypt using AES-CFB |
| 484 | + cipher = Cipher( |
| 485 | + algorithms.AES(aes_key), |
| 486 | + modes.CFB(iv), |
| 487 | + ) |
| 488 | + encryptor = cipher.encryptor() |
| 489 | + ciphertext = encryptor.update(plaintext.encode('utf-8')) + encryptor.finalize() |
| 490 | + |
| 491 | + # Encrypt the AES key using the RSA public key |
| 492 | + encrypted_key = public_key.encrypt( |
| 493 | + aes_key, |
| 494 | + padding.OAEP( |
| 495 | + mgf=padding.MGF1(algorithm=hashes.SHA256()), |
| 496 | + algorithm=hashes.SHA256(), |
| 497 | + label=None |
| 498 | + ) |
| 499 | + ) |
| 500 | + |
| 501 | + return { |
| 502 | + 'encrypted_aes_key': base64.b64encode(encrypted_key).decode('utf-8'), |
| 503 | + 'aes_iv': base64.b64encode(iv).decode('utf-8'), |
| 504 | + 'encrypted_value': base64.b64encode(ciphertext).decode('utf-8'), |
| 505 | + 'type': 'rsa_aes', |
| 506 | + } |
| 507 | + |
| 508 | + def _encrypt_rsa( |
| 509 | + self, |
| 510 | + public_key: rsa.RSAPublicKey, |
| 511 | + plaintext: str, |
| 512 | + ) -> dict: |
| 513 | + # Load public RSA key |
| 514 | + ciphertext = public_key.encrypt( |
| 515 | + plaintext.encode(), |
| 516 | + padding.OAEP( |
| 517 | + mgf=padding.MGF1(algorithm=hashes.SHA256()), |
| 518 | + algorithm=hashes.SHA256(), |
| 519 | + label=None |
| 520 | + ), |
| 521 | + ) |
| 522 | + return { |
| 523 | + 'encrypted_value': base64.b64encode(ciphertext).decode('utf-8'), |
| 524 | + 'type': 'rsa', |
| 525 | + 'encrypted_aes_key': "", |
| 526 | + 'aes_iv': "", |
| 527 | + } |
| 528 | + |
| 529 | + def decrypt_secret( |
| 530 | + self, |
| 531 | + private_key_pem: str, |
| 532 | + encrypted_value: str, |
| 533 | + secret_type: str, |
| 534 | + encrypted_aes_key: str, |
| 535 | + aes_iv: str, |
| 536 | + ) -> str: |
| 537 | + private_key = serialization.load_pem_private_key( |
| 538 | + private_key_pem.encode('utf-8'), |
| 539 | + password=None, |
| 540 | + backend=default_backend() |
| 541 | + ) |
| 542 | + |
| 543 | + if not isinstance(private_key, rsa.RSAPrivateKey): |
| 544 | + raise TypeError("Private key must be a RSA private key for decryption.") |
| 545 | + |
| 546 | + if secret_type == 'rsa': |
| 547 | + ciphertext = base64.b64decode(encrypted_value) |
| 548 | + plaintext = private_key.decrypt( |
| 549 | + ciphertext, |
| 550 | + padding.OAEP( |
| 551 | + mgf=padding.MGF1(algorithm=hashes.SHA256()), |
| 552 | + algorithm=hashes.SHA256(), |
| 553 | + label=None |
| 554 | + ) |
| 555 | + ) |
| 556 | + return plaintext.decode('utf-8') |
| 557 | + |
| 558 | + # aes_rsa |
| 559 | + encrypted_aes_key_decoded = base64.b64decode(encrypted_aes_key) |
| 560 | + iv = base64.b64decode(aes_iv) |
| 561 | + ciphertext = base64.b64decode(encrypted_value) |
| 562 | + |
| 563 | + aes_key = private_key.decrypt( |
| 564 | + encrypted_aes_key_decoded, |
| 565 | + padding.OAEP( |
| 566 | + mgf=padding.MGF1(algorithm=hashes.SHA256()), |
| 567 | + algorithm=hashes.SHA256(), |
| 568 | + label=None |
| 569 | + ) |
| 570 | + ) |
| 571 | + cipher = Cipher( |
| 572 | + algorithms.AES(aes_key), |
| 573 | + modes.CFB(iv), |
| 574 | + ) |
| 575 | + decryptor = cipher.decryptor() |
| 576 | + plaintext = decryptor.update(ciphertext) + decryptor.finalize() |
| 577 | + return plaintext.decode('utf-8') |
| 578 | + |
| 579 | + def encrypt_secret( |
| 580 | + self, |
| 581 | + encryption_cert_or_key_pem: str, |
| 582 | + plaintext: str, |
| 583 | + encryption_type: Optional[str] = None, |
| 584 | + ) -> dict: |
| 585 | + """ |
| 586 | + Encrypts a plaintext string for securely sending to the Unstructured API. |
| 587 | + |
| 588 | + Args: |
| 589 | + encryption_cert_or_key_pem (str): A PEM-encoded RSA public key or certificate. |
| 590 | + plaintext (str): The string to encrypt. |
| 591 | + type (str, optional): Encryption type, either "rsa" or "rsa_aes". |
| 592 | +
|
| 593 | + Returns: |
| 594 | + dict: A dictionary with encrypted AES key, iv, and ciphertext (all base64-encoded). |
| 595 | + """ |
| 596 | + # If a cert is provided, extract the public key |
| 597 | + if "BEGIN CERTIFICATE" in encryption_cert_or_key_pem: |
| 598 | + cert = x509.load_pem_x509_certificate( |
| 599 | + encryption_cert_or_key_pem.encode('utf-8'), |
| 600 | + ) |
| 601 | + |
| 602 | + public_key = cert.public_key() # type: ignore[assignment] |
| 603 | + else: |
| 604 | + public_key = serialization.load_pem_public_key( |
| 605 | + encryption_cert_or_key_pem.encode('utf-8'), |
| 606 | + backend=default_backend() |
| 607 | + ) # type: ignore[assignment] |
| 608 | + |
| 609 | + if not isinstance(public_key, rsa.RSAPublicKey): |
| 610 | + raise TypeError("Public key must be a RSA public key for encryption.") |
| 611 | + |
| 612 | + # If the plaintext is short, use RSA directly |
| 613 | + # Otherwise, use a RSA_AES envelope hybrid |
| 614 | + # Use the length of the public key to determine the encryption type |
| 615 | + key_size_bytes = public_key.key_size // 8 |
| 616 | + max_rsa_length = key_size_bytes - 66 # OAEP SHA256 overhead |
| 617 | + print(max_rsa_length) |
| 618 | + |
| 619 | + if not encryption_type: |
| 620 | + encryption_type = "rsa" if len(plaintext) <= max_rsa_length else "rsa_aes" |
| 621 | + |
| 622 | + if encryption_type == "rsa": |
| 623 | + return self._encrypt_rsa(public_key, plaintext) |
| 624 | + |
| 625 | + return self._encrypt_rsa_aes(public_key, plaintext) |
| 626 | + # endregion sdk-class-body |
0 commit comments