Exemplo n.º 1
0
    def from_string(key, password='******'):
        """Construct a Signer instance from a string.

        Args:
            key: string, private key in PEM format.
            password: string, password for private key file. Unused for PEM
                      files.

        Returns:
            Signer instance.

        Raises:
            NotImplementedError if the key isn't in PEM format.
        """
        parsed_pem_key = _parse_pem_key(key)
        if parsed_pem_key:
            pkey = RSA.importKey(parsed_pem_key)
        else:
            raise NotImplementedError(
                'PKCS12 format is not supported by the PyCrypto library. '
                'Try converting to a "PEM" '
                '(openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > '
                'privatekey.pem) '
                'or using PyOpenSSL if native code is an option.')
        return PyCryptoSigner(pkey)
Exemplo n.º 2
0
    def from_string(key, password='******'):
        """Construct a Signer instance from a string.

        Args:
            key: string, private key in PEM format.
            password: string, password for private key file. Unused for PEM
                      files.

        Returns:
            Signer instance.

        Raises:
            NotImplementedError if the key isn't in PEM format.
        """
        parsed_pem_key = _parse_pem_key(key)
        if parsed_pem_key:
            pkey = RSA.importKey(parsed_pem_key)
        else:
            raise NotImplementedError(
                'PKCS12 format is not supported by the PyCrypto library. '
                'Try converting to a "PEM" '
                '(openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > '
                'privatekey.pem) '
                'or using PyOpenSSL if native code is an option.')
        return PyCryptoSigner(pkey)
Exemplo n.º 3
0
  def _succeeds_helper(self, password=None):
    self.assertEqual(True, HAS_OPENSSL)

    credentials = self._make_signed_jwt_creds()
    if password is None:
      password = credentials.private_key_password
    pem_contents = crypt.pkcs12_key_as_pem(credentials.private_key, password)
    pkcs12_key_as_pem = datafile('pem_from_pkcs12.pem')
    pkcs12_key_as_pem = _helpers._parse_pem_key(pkcs12_key_as_pem)
    alternate_pem = datafile('pem_from_pkcs12_alternate.pem')
    self.assertTrue(pem_contents in [pkcs12_key_as_pem, alternate_pem])
Exemplo n.º 4
0
    def _succeeds_helper(self, password=None):
        self.assertEqual(True, client.HAS_OPENSSL)

        credentials = self._make_svc_account_creds()
        if password is None:
            password = credentials._private_key_password
        pem_contents = crypt.pkcs12_key_as_pem(
            credentials._private_key_pkcs12, password)
        pkcs12_key_as_pem = datafile('pem_from_pkcs12.pem')
        pkcs12_key_as_pem = _helpers._parse_pem_key(pkcs12_key_as_pem)
        alternate_pem = datafile('pem_from_pkcs12_alternate.pem')
        self.assertTrue(pem_contents in [pkcs12_key_as_pem, alternate_pem])
Exemplo n.º 5
0
    def from_string(key, password=b'notasecret'):
        """Construct a Signer instance from a string.

        Args:
            key: string, private key in PKCS12 or PEM format.
            password: string, password for the private key file.

        Returns:
            Signer instance.

        Raises:
            OpenSSL.crypto.Error if the key can't be parsed.
        """
        parsed_pem_key = _parse_pem_key(key)
        if parsed_pem_key:
            pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, parsed_pem_key)
        else:
            password = _to_bytes(password, encoding='utf-8')
            pkey = crypto.load_pkcs12(key, password).get_privatekey()
        return OpenSSLSigner(pkey)
    def from_string(key, password='******'):
        """Construct a Signer instance from a string.

        Args:
            key: string, private key in PEM format.
            password: string, password for private key file. Unused for PEM
                      files.

        Returns:
            Signer instance.

        Raises:
            NotImplementedError if the key isn't in PEM format.
        """
        parsed_pem_key = _helpers._parse_pem_key(_helpers._to_bytes(key))
        if parsed_pem_key:
            pkey = RSA.importKey(parsed_pem_key)
        else:
            raise NotImplementedError(
                'No key in PEM format was detected. This implementation '
                'can only use the PyCrypto library for keys in PEM '
                'format.')
        return PyCryptoSigner(pkey)
Exemplo n.º 7
0
    def from_string(key, password='******'):
        """Construct a Signer instance from a string.

        Args:
            key: string, private key in PEM format.
            password: string, password for private key file. Unused for PEM
                      files.

        Returns:
            Signer instance.

        Raises:
            NotImplementedError if the key isn't in PEM format.
        """
        parsed_pem_key = _parse_pem_key(_to_bytes(key))
        if parsed_pem_key:
            pkey = RSA.importKey(parsed_pem_key)
        else:
            raise NotImplementedError(
                'No key in PEM format was detected. This implementation '
                'can only use the PyCrypto library for keys in PEM '
                'format.')
        return PyCryptoSigner(pkey)
Exemplo n.º 8
0
 def test_bad_input(self):
   test_string = b'DOES NOT HAVE DASHES'
   result = _parse_pem_key(test_string)
   self.assertEqual(result, None)
Exemplo n.º 9
0
 def test_valid_input(self):
   test_string = b'1234-----BEGIN FOO BAR BAZ'
   result = _parse_pem_key(test_string)
   self.assertEqual(result, test_string[4:])
Exemplo n.º 10
0
 def test_bad_input(self):
     test_string = b'DOES NOT HAVE DASHES'
     result = _helpers._parse_pem_key(test_string)
     self.assertEqual(result, None)
Exemplo n.º 11
0
 def test_valid_input(self):
     test_string = b'1234-----BEGIN FOO BAR BAZ'
     result = _helpers._parse_pem_key(test_string)
     self.assertEqual(result, test_string[4:])