Exemple #1
0
    def test_private_password(self, mock_encryption):
        mock_enc = mock.Mock()
        mock_encryption.return_value = mock_enc

        key_utils.serialize_key(self.mock_key, encoding='PEM', return_private=True, password=self.password)

        mock_encryption.assert_called_with(self.password)
        self.mock_key.private_bytes.assert_called_with(
            encoding=crypto_serialization.Encoding.PEM,
            format=crypto_serialization.PrivateFormat.TraditionalOpenSSL,
            encryption_algorithm=mock_enc)
 def __init__(self, logger):
     """
     :param logger: EC2 Instance Connect CLI logger to use for log messages
     :type logger: ec2instanceconnectcli.EC2InstanceConnectLogger.EC2InstanceConnectLogger
     """
     self.logger = logger
     key = key_utils.generate_key(2048)
     self.pub_key = key_utils.serialize_key(
         key, encoding='OpenSSH').decode('utf-8')
     priv_key = key_utils.serialize_key(key,
                                        return_private=True).decode('utf-8')
     self.tempf = self._write_priv_key(priv_key)
Exemple #3
0
    def test_invalid_encodings(self):
        try:
            key_utils.serialize_key(self.mock_key, encoding='INVALID')
            self.fail('Invalid encoding accepted')

        except AssertionError:
            pass

        try:
            key_utils.serialize_key(self.mock_key, encoding='OpenSSH', return_private=True)
            self.fail('Private keys shouldn''t accept OpenSSH encoding')

        except AssertionError:
            pass
Exemple #4
0
    def test_private_encodings(self, mock_encryption):
        mock_enc = mock.Mock()
        mock_encryption.return_value = mock_enc

        calls = [mock.call(encoding=crypto_serialization.Encoding.DER,
                           format=crypto_serialization.PrivateFormat.TraditionalOpenSSL,
                           encryption_algorithm=mock_enc),
                 mock.call(encoding=crypto_serialization.Encoding.PEM,
                           format=crypto_serialization.PrivateFormat.TraditionalOpenSSL,
                           encryption_algorithm=mock_enc)]

        key_utils.serialize_key(self.mock_key, encoding='DER', return_private=True)
        key_utils.serialize_key(self.mock_key, encoding='PEM', return_private=True)

        self.mock_key.private_bytes.assert_has_calls(calls)
Exemple #5
0
    def test_public_encodings(self):
        mock_pub_key = mock.Mock()
        self.mock_key.public_key.return_value = mock_pub_key

        calls = [
            mock.call(encoding=crypto_serialization.Encoding.OpenSSH,
                      format=crypto_serialization.PublicFormat.OpenSSH),
            mock.call(
                encoding=crypto_serialization.Encoding.DER,
                format=crypto_serialization.PublicFormat.SubjectPublicKeyInfo),
            mock.call(
                encoding=crypto_serialization.Encoding.PEM,
                format=crypto_serialization.PublicFormat.SubjectPublicKeyInfo)
        ]

        key_utils.serialize_key(self.mock_key,
                                encoding='OpenSSH',
                                return_private=False)
        key_utils.serialize_key(self.mock_key,
                                encoding='DER',
                                return_private=False)
        key_utils.serialize_key(self.mock_key,
                                encoding='PEM',
                                return_private=False)

        mock_pub_key.public_bytes.assert_has_calls(calls)