1616"""Crypto-related routines for oauth2client."""
1717
1818import base64
19+ import imp
1920import json
2021import logging
22+ import os
2123import sys
2224import time
2325
@@ -37,7 +39,9 @@ class AppIdentityError(Exception):
3739
3840
3941try :
40- from OpenSSL import crypto
42+ _ , _package_dir , _ = imp .find_module ('OpenSSL' )
43+ if not os .path .isfile (os .path .join (_package_dir , 'crypto.py' )):
44+ raise ImportError ('No module named OpenSSL' )
4145
4246 class OpenSSLVerifier (object ):
4347 """Verifies the signature on a message."""
@@ -61,6 +65,7 @@ def verify(self, message, signature):
6165 True if message was signed by the private key associated with the public
6266 key that this object was constructed with.
6367 """
68+ from OpenSSL import crypto
6469 try :
6570 if isinstance (message , six .text_type ):
6671 message = message .encode ('utf-8' )
@@ -84,6 +89,7 @@ def from_string(key_pem, is_x509_cert):
8489 Raises:
8590 OpenSSL.crypto.Error if the key_pem can't be parsed.
8691 """
92+ from OpenSSL import crypto
8793 if is_x509_cert :
8894 pubkey = crypto .load_certificate (crypto .FILETYPE_PEM , key_pem )
8995 else :
@@ -111,6 +117,7 @@ def sign(self, message):
111117 Returns:
112118 string, The signature of the message for the given key.
113119 """
120+ from OpenSSL import crypto
114121 if isinstance (message , six .text_type ):
115122 message = message .encode ('utf-8' )
116123 return crypto .sign (self ._key , message , 'sha256' )
@@ -129,6 +136,7 @@ def from_string(key, password=b'notasecret'):
129136 Raises:
130137 OpenSSL.crypto.Error if the key can't be parsed.
131138 """
139+ from OpenSSL import crypto
132140 parsed_pem_key = _parse_pem_key (key )
133141 if parsed_pem_key :
134142 pkey = crypto .load_privatekey (crypto .FILETYPE_PEM , parsed_pem_key )
@@ -149,6 +157,7 @@ def pkcs12_key_as_pem(private_key_text, private_key_password):
149157 Returns:
150158 String. PEM contents of ``private_key_text``.
151159 """
160+ from OpenSSL import crypto
152161 decoded_body = base64 .b64decode (private_key_text )
153162 if isinstance (private_key_password , six .string_types ):
154163 private_key_password = private_key_password .encode ('ascii' )
0 commit comments