diff --git a/oauth2client/_pycrypto_crypt.py b/oauth2client/_pycrypto_crypt.py index ed7141caa..5ee7046db 100644 --- a/oauth2client/_pycrypto_crypt.py +++ b/oauth2client/_pycrypto_crypt.py @@ -120,9 +120,7 @@ def from_string(key, password='notasecret'): 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.') + 'No key in PEM format was detected. This implementation ' + 'can only use the PyCrypto library for keys in PEM ' + 'format.') return PyCryptoSigner(pkey) diff --git a/oauth2client/service_account.py b/oauth2client/service_account.py index 6c0bd6eec..b4d1dc8a9 100644 --- a/oauth2client/service_account.py +++ b/oauth2client/service_account.py @@ -240,6 +240,8 @@ def _from_p12_keyfile_contents(cls, service_account_email, """ if private_key_password is None: private_key_password = _PASSWORD_DEFAULT + if crypt.Signer is not crypt.OpenSSLSigner: + raise NotImplementedError(_PKCS12_ERROR) signer = crypt.Signer.from_string(private_key_pkcs12, private_key_password) credentials = cls(service_account_email, signer, scopes=scopes) diff --git a/tests/test_service_account.py b/tests/test_service_account.py index 27423eaa0..3d9e7db5c 100644 --- a/tests/test_service_account.py +++ b/tests/test_service_account.py @@ -165,6 +165,21 @@ def _from_p12_keyfile_helper(self, private_key_password=None, scopes=''): self.assertEqual(creds._private_key_password, private_key_password) self.assertEqual(creds._scopes, ' '.join(scopes)) + def _p12_not_implemented_helper(self): + service_account_email = 'name@email.com' + filename = data_filename('privatekey.p12') + with self.assertRaises(NotImplementedError): + ServiceAccountCredentials.from_p12_keyfile( + service_account_email, filename) + + @mock.patch('oauth2client.crypt.Signer', new=crypt.PyCryptoSigner) + def test_from_p12_keyfile_with_pycrypto(self): + self._p12_not_implemented_helper() + + @mock.patch('oauth2client.crypt.Signer', new=crypt.RsaSigner) + def test_from_p12_keyfile_with_rsa(self): + self._p12_not_implemented_helper() + def test_from_p12_keyfile_defaults(self): self._from_p12_keyfile_helper()