|
|
|
@ -3,12 +3,11 @@
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
from Crypto.Signature import pss
|
|
|
|
|
from Crypto.Hash import SHA256
|
|
|
|
|
from Crypto.Cipher import PKCS1_OAEP
|
|
|
|
|
from Crypto.PublicKey import RSA
|
|
|
|
|
from Crypto.Cipher import AES
|
|
|
|
|
from Crypto.Random import get_random_bytes
|
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
|
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
|
|
|
from cryptography.hazmat.primitives import hashes
|
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import padding
|
|
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='encrypt distribution archive'
|
|
|
|
|
' using provisioning key to encrypt and deployment key to sign')
|
|
|
|
@ -22,38 +21,34 @@ parser.add_argument('--passphrase', default=None,
|
|
|
|
|
parser.add_argument('--provisioning-key', default='provisioning_key-pub.der',
|
|
|
|
|
help='file with public key part of provisioning key in DER format')
|
|
|
|
|
|
|
|
|
|
parser.add_argument('--distribution-archive', default='_DIST.AD',
|
|
|
|
|
parser.add_argument('--distribution-archive', default='DIST.AD',
|
|
|
|
|
help='unencrypted version of distribution archive file, usually in ZIP format')
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
with open(args.deployment_key, 'rb') as keyfile:
|
|
|
|
|
deployment_key = RSA.import_key(keyfile.read(), passphrase=args.passphrase)
|
|
|
|
|
with open(args.deployment_key, "rb") as deployment_key_file:
|
|
|
|
|
deployment_key = serialization.load_der_private_key(deployment_key_file.read(), password=args.passphrase)
|
|
|
|
|
|
|
|
|
|
with open(args.provisioning_key, 'rb') as keyfile:
|
|
|
|
|
provisioning_key = RSA.import_key(keyfile.read())
|
|
|
|
|
with open(args.provisioning_key, "rb") as provisioning_key_file:
|
|
|
|
|
provisioning_key = serialization.load_der_public_key(provisioning_key_file.read())
|
|
|
|
|
|
|
|
|
|
distribution_key = get_random_bytes(32)
|
|
|
|
|
distribution_key = AESGCM.generate_key(bit_length=256)
|
|
|
|
|
|
|
|
|
|
oaep = PKCS1_OAEP.new(provisioning_key)
|
|
|
|
|
encrypted_distribution_key = provisioning_key.encrypt(distribution_key,
|
|
|
|
|
padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
|
|
|
|
|
algorithm=hashes.SHA256(), label=None))
|
|
|
|
|
|
|
|
|
|
with open('DIST.KEY', 'wb') as keyfile:
|
|
|
|
|
keyfile.write(oaep.encrypt(distribution_key))
|
|
|
|
|
with open('DIST.KEY', 'wb') as distribution_key_file:
|
|
|
|
|
distribution_key_file.write(encrypted_distribution_key)
|
|
|
|
|
|
|
|
|
|
with open('_DIST.AD', 'rb') as unencrypted_archive:
|
|
|
|
|
# FIXME: this consumes a lot of RAM
|
|
|
|
|
unencrypted_content = unencrypted_archive.read()
|
|
|
|
|
with open(args.distribution_archive, 'rb') as distribution_archive_file:
|
|
|
|
|
distribution_archive = distribution_archive_file.read()
|
|
|
|
|
|
|
|
|
|
nonce = get_random_bytes(12)
|
|
|
|
|
aes_gcm = AES.new(distribution_key, AES.MODE_GCM, nonce=nonce)
|
|
|
|
|
aesgcm = AESGCM(distribution_key)
|
|
|
|
|
nonce = os.urandom(12)
|
|
|
|
|
encrypted_archive = aesgcm.encrypt(nonce, distribution_archive, None)
|
|
|
|
|
distribution_archive = None
|
|
|
|
|
|
|
|
|
|
ciphertext, tag = aes_gcm.encrypt_and_digest(unencrypted_content)
|
|
|
|
|
|
|
|
|
|
with open('DIST.A', 'wb') as distribution_archive:
|
|
|
|
|
distribution_archive.write(ciphertext)
|
|
|
|
|
|
|
|
|
|
hash = SHA256.new(ciphertext)
|
|
|
|
|
signature = pss.new(deployment_key).sign(hash)
|
|
|
|
|
|
|
|
|
|
with open('DIST.SIG', 'wb') as distribution_signature:
|
|
|
|
|
distribution_signature.write(signature)
|
|
|
|
|
with open('DIST.A', 'wb') as encrypted_archive_file:
|
|
|
|
|
encrypted_archive_file.write(nonce)
|
|
|
|
|
encrypted_archive_file.write(encrypted_archive)
|
|
|
|
|