def test_verify_signature_public_key(self): # correct key v1 args = self.VerifyArgs('1', self._open('ecdsa_secure_boot_signing_pubkey.pem'), self._open('bootloader_signed.bin')) espsecure.verify_signature(args) # correct key v2 args = self.VerifyArgs('2', self._open('rsa_secure_boot_signing_pubkey.pem'), self._open('bootloader_signed_v2.bin')) espsecure.verify_signature(args) # wrong key v1 args = self.VerifyArgs('1', self._open('ecdsa_secure_boot_signing_pubkey2.pem'), self._open('bootloader_signed.bin')) with self.assertRaises(esptool.FatalError) as cm: espsecure.verify_signature(args) self.assertIn("Signature is not valid", str(cm.exception)) # wrong key v2 args = self.VerifyArgs('2', self._open('rsa_secure_boot_signing_pubkey2.pem'), self._open('bootloader_signed_v2.bin')) with self.assertRaises(esptool.FatalError) as cm: espsecure.verify_signature(args) self.assertIn("Signature could not be verified with the provided key.", str(cm.exception)) # multi-signed wrong key v2 args = self.VerifyArgs('2', self._open('rsa_secure_boot_signing_pubkey4.pem'), self._open('bootloader_multi_signed_v2.bin')) with self.assertRaises(esptool.FatalError) as cm: espsecure.verify_signature(args) self.assertIn("Signature could not be verified with the provided key.", str(cm.exception))
def test_extract_binary_public_key(self): ExtractKeyArgs = namedtuple('extract_public_key_args', ['version', 'keyfile', 'public_keyfile']) pub_keyfile = tempfile.NamedTemporaryFile(delete=False) pub_keyfile2 = tempfile.NamedTemporaryFile(delete=False) try: args = ExtractKeyArgs( '1', self._open('ecdsa_secure_boot_signing_key.pem'), pub_keyfile) espsecure.extract_public_key(args) args = ExtractKeyArgs( '1', self._open('ecdsa_secure_boot_signing_key2.pem'), pub_keyfile2) espsecure.extract_public_key(args) pub_keyfile.seek(0) pub_keyfile2.seek(0) # use correct extracted public key to verify args = self.VerifyArgs('1', pub_keyfile, self._open('bootloader_signed.bin')) espsecure.verify_signature(args) # use wrong extracted public key to try and verify args = self.VerifyArgs('1', pub_keyfile2, self._open('bootloader_signed.bin')) with self.assertRaises(esptool.FatalError) as cm: espsecure.verify_signature(args) self.assertIn("Signature is not valid", str(cm.exception)) finally: os.unlink(pub_keyfile.name) os.unlink(pub_keyfile2.name)
def test_extract_binary_public_key(self): with tempfile.NamedTemporaryFile( ) as pub_keyfile, tempfile.NamedTemporaryFile() as pub_keyfile2: args = self.ExtractKeyArgs( '1', self._open('ecdsa_secure_boot_signing_key.pem'), pub_keyfile) espsecure.extract_public_key(args) args = self.ExtractKeyArgs( '1', self._open('ecdsa_secure_boot_signing_key2.pem'), pub_keyfile2) espsecure.extract_public_key(args) pub_keyfile.seek(0) pub_keyfile2.seek(0) # use correct extracted public key to verify args = self.VerifyArgs('1', pub_keyfile, self._open('bootloader_signed.bin')) espsecure.verify_signature(args) # use wrong extracted public key to try and verify args = self.VerifyArgs('1', pub_keyfile2, self._open('bootloader_signed.bin')) with self.assertRaises(esptool.FatalError) as cm: espsecure.verify_signature(args) self.assertIn("Signature is not valid", str(cm.exception))
def test_extract_binary_public_key(self): ExtractKeyArgs = namedtuple('extract_public_key_args', [ 'keyfile', 'public_keyfile' ]) pub_keyfile = tempfile.NamedTemporaryFile(delete=False) pub_keyfile2 = tempfile.NamedTemporaryFile(delete=False) try: args = ExtractKeyArgs(self._open('ecdsa_secure_boot_signing_key.pem'), pub_keyfile) espsecure.extract_public_key(args) args = ExtractKeyArgs(self._open('ecdsa_secure_boot_signing_key2.pem'), pub_keyfile2) espsecure.extract_public_key(args) pub_keyfile.seek(0) pub_keyfile2.seek(0) # use correct extracted public key to verify args = self.VerifyArgs(pub_keyfile, self._open('bootloader_signed.bin')) espsecure.verify_signature(args) # use wrong extracted public key to try and verify args = self.VerifyArgs(pub_keyfile2, self._open('bootloader_signed.bin')) with self.assertRaises(esptool.FatalError) as cm: espsecure.verify_signature(args) self.assertIn("Signature is not valid", str(cm.exception)) finally: os.unlink(pub_keyfile.name) os.unlink(pub_keyfile2.name)
def test_sign_v2_data(self): with tempfile.NamedTemporaryFile() as output_file: args = self.SignArgs('2', [self._open('rsa_secure_boot_signing_key.pem')], output_file.name, False, self._open('bootloader_unsigned_v2.bin')) espsecure.sign_data(args) args = self.VerifyArgs('2', self._open('rsa_secure_boot_signing_key.pem'), output_file) espsecure.verify_signature(args)
def test_verify_signature_public_key(self): # correct key args = self.VerifyArgs(self._open('ecdsa_secure_boot_signing_pubkey.pem'), self._open('bootloader_signed.bin')) espsecure.verify_signature(args) # wrong key args = self.VerifyArgs(self._open('ecdsa_secure_boot_signing_pubkey2.pem'), self._open('bootloader_signed.bin')) with self.assertRaises(esptool.FatalError) as cm: espsecure.verify_signature(args) self.assertIn("Signature is not valid", str(cm.exception))
def test_sign_v2_append_signatures_multiple_steps(self): # similar to previous test, but sign in two invocations with tempfile.NamedTemporaryFile() as output_file1, tempfile.NamedTemporaryFile() as output_file2: args = self.SignArgs('2', [self._open('rsa_secure_boot_signing_key2.pem')], output_file1.name, True, self._open('bootloader_signed_v2.bin')) espsecure.sign_data(args) args = self.SignArgs('2', [self._open('rsa_secure_boot_signing_key3.pem')], output_file2.name, True, output_file1) espsecure.sign_data(args) args = self.VerifyArgs('2', self._open('rsa_secure_boot_signing_key.pem'), output_file2) espsecure.verify_signature(args) output_file2.seek(0) args = self.VerifyArgs('2', self._open('rsa_secure_boot_signing_key2.pem'), output_file2) espsecure.verify_signature(args) output_file2.seek(0) args = self.VerifyArgs('2', self._open('rsa_secure_boot_signing_key3.pem'), output_file2) espsecure.verify_signature(args)
def test_sign_v2_append_signatures(self): # Append signatures + Verify with an appended key (bootloader_signed_v2.bin already signed with rsa_secure_boot_signing_key.pem) with tempfile.NamedTemporaryFile() as output_file: args = self.SignArgs('2', [ self._open('rsa_secure_boot_signing_key2.pem'), self._open('rsa_secure_boot_signing_key3.pem') ], output_file.name, True, self._open('bootloader_signed_v2.bin')) espsecure.sign_data(args) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key.pem'), output_file) espsecure.verify_signature(args) output_file.seek(0) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key2.pem'), output_file) espsecure.verify_signature(args) output_file.seek(0) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key3.pem'), output_file) espsecure.verify_signature(args)
def test_sign_v2_data(self): SignArgs = namedtuple('sign_data_args', ['version', 'keyfile', 'output', 'datafile']) try: output_file = tempfile.NamedTemporaryFile(delete=False) # Note: signing bootloader is not actually needed # for ESP32, it's just a handy file to sign args = SignArgs('2', [self._open('rsa_secure_boot_signing_key.pem')], output_file.name, self._open('bootloader_unsigned_v2.bin')) espsecure.sign_data(args) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key.pem'), output_file) espsecure.verify_signature(args) finally: output_file.close() os.unlink(output_file.name)
def test_sign_v2_data(self): SignArgs = namedtuple( 'sign_data_args', ['version', 'keyfile', 'output', 'append_signatures', 'datafile']) try: output_file = tempfile.NamedTemporaryFile(delete=False) args = SignArgs('2', [self._open('rsa_secure_boot_signing_key.pem')], output_file.name, False, self._open('bootloader_unsigned_v2.bin')) espsecure.sign_data(args) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key.pem'), output_file) espsecure.verify_signature(args) finally: output_file.close() os.unlink(output_file.name) # 3 keys + Verify with 3rd key try: output_file = tempfile.NamedTemporaryFile(delete=False) args = SignArgs('2', [ self._open('rsa_secure_boot_signing_key.pem'), self._open('rsa_secure_boot_signing_key2.pem'), self._open('rsa_secure_boot_signing_key3.pem') ], output_file.name, False, self._open('bootloader_unsigned_v2.bin')) espsecure.sign_data(args) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key3.pem'), output_file) espsecure.verify_signature(args) output_file.seek(0) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key2.pem'), output_file) espsecure.verify_signature(args) output_file.seek(0) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key.pem'), output_file) espsecure.verify_signature(args) finally: output_file.close() os.unlink(output_file.name) # Append signatures + Verify with an appended key (bootloader_signed_v2.bin already signed with rsa_secure_boot_signing_key.pem) try: output_file = tempfile.NamedTemporaryFile(delete=False) args = SignArgs('2', [ self._open('rsa_secure_boot_signing_key2.pem'), self._open('rsa_secure_boot_signing_key3.pem') ], output_file.name, True, self._open('bootloader_signed_v2.bin')) espsecure.sign_data(args) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key.pem'), output_file) espsecure.verify_signature(args) output_file.seek(0) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key2.pem'), output_file) espsecure.verify_signature(args) output_file.seek(0) args = self.VerifyArgs( '2', self._open('rsa_secure_boot_signing_key3.pem'), output_file) espsecure.verify_signature(args) finally: output_file.close() os.unlink(output_file.name)