| Index: third_party/oauth2client/crypt.py
|
| diff --git a/third_party/oauth2client/crypt.py b/third_party/oauth2client/crypt.py
|
| index b5b8cab22e002432388f51233728c2fe044a9c79..2d31815dd6357b5c7573a4eebc6792b168b4d6a4 100644
|
| --- a/third_party/oauth2client/crypt.py
|
| +++ b/third_party/oauth2client/crypt.py
|
| @@ -1,6 +1,7 @@
|
| +#!/usr/bin/python2.4
|
| # -*- coding: utf-8 -*-
|
| #
|
| -# Copyright 2014 Google Inc. All rights reserved.
|
| +# Copyright (C) 2011 Google Inc.
|
| #
|
| # Licensed under the Apache License, Version 2.0 (the "License");
|
| # you may not use this file except in compliance with the License.
|
| @@ -13,15 +14,13 @@
|
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| # See the License for the specific language governing permissions and
|
| # limitations under the License.
|
| -"""Crypto-related routines for oauth2client."""
|
|
|
| import base64
|
| -import json
|
| +import hashlib
|
| import logging
|
| -import sys
|
| import time
|
|
|
| -from third_party import six
|
| +from anyjson import simplejson
|
|
|
|
|
| CLOCK_SKEW_SECS = 300 # 5 minutes in seconds
|
| @@ -39,6 +38,7 @@
|
| try:
|
| from OpenSSL import crypto
|
|
|
| +
|
| class OpenSSLVerifier(object):
|
| """Verifies the signature on a message."""
|
|
|
| @@ -62,8 +62,6 @@
|
| key that this object was constructed with.
|
| """
|
| try:
|
| - if isinstance(message, six.text_type):
|
| - message = message.encode('utf-8')
|
| crypto.verify(self._pubkey, signature, message, 'sha256')
|
| return True
|
| except:
|
| @@ -106,17 +104,15 @@
|
| """Signs a message.
|
|
|
| Args:
|
| - message: bytes, Message to be signed.
|
| + message: string, Message to be signed.
|
|
|
| Returns:
|
| string, The signature of the message for the given key.
|
| """
|
| - if isinstance(message, six.text_type):
|
| - message = message.encode('utf-8')
|
| return crypto.sign(self._key, message, 'sha256')
|
|
|
| @staticmethod
|
| - def from_string(key, password=b'notasecret'):
|
| + def from_string(key, password='notasecret'):
|
| """Construct a Signer instance from a string.
|
|
|
| Args:
|
| @@ -129,45 +125,21 @@
|
| 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)
|
| + if key.startswith('-----BEGIN '):
|
| + pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key)
|
| else:
|
| - if isinstance(password, six.text_type):
|
| - password = password.encode('utf-8')
|
| pkey = crypto.load_pkcs12(key, password).get_privatekey()
|
| return OpenSSLSigner(pkey)
|
|
|
| -
|
| - def pkcs12_key_as_pem(private_key_text, private_key_password):
|
| - """Convert the contents of a PKCS12 key to PEM using OpenSSL.
|
| -
|
| - Args:
|
| - private_key_text: String. Private key.
|
| - private_key_password: String. Password for PKCS12.
|
| -
|
| - Returns:
|
| - String. PEM contents of ``private_key_text``.
|
| - """
|
| - decoded_body = base64.b64decode(private_key_text)
|
| - if isinstance(private_key_password, six.string_types):
|
| - private_key_password = private_key_password.encode('ascii')
|
| -
|
| - pkcs12 = crypto.load_pkcs12(decoded_body, private_key_password)
|
| - return crypto.dump_privatekey(crypto.FILETYPE_PEM,
|
| - pkcs12.get_privatekey())
|
| except ImportError:
|
| OpenSSLVerifier = None
|
| OpenSSLSigner = None
|
| - def pkcs12_key_as_pem(*args, **kwargs):
|
| - raise NotImplementedError('pkcs12_key_as_pem requires OpenSSL.')
|
|
|
|
|
| try:
|
| from Crypto.PublicKey import RSA
|
| from Crypto.Hash import SHA256
|
| from Crypto.Signature import PKCS1_v1_5
|
| - from Crypto.Util.asn1 import DerSequence
|
|
|
|
|
| class PyCryptoVerifier(object):
|
| @@ -209,17 +181,14 @@
|
|
|
| Returns:
|
| Verifier instance.
|
| +
|
| + Raises:
|
| + NotImplementedError if is_x509_cert is true.
|
| """
|
| if is_x509_cert:
|
| - if isinstance(key_pem, six.text_type):
|
| - key_pem = key_pem.encode('ascii')
|
| - pemLines = key_pem.replace(b' ', b'').split()
|
| - certDer = _urlsafe_b64decode(b''.join(pemLines[1:-1]))
|
| - certSeq = DerSequence()
|
| - certSeq.decode(certDer)
|
| - tbsSeq = DerSequence()
|
| - tbsSeq.decode(certSeq[0])
|
| - pubkey = RSA.importKey(tbsSeq[6])
|
| + raise NotImplementedError(
|
| + 'X509 certs are not supported by the PyCrypto library. '
|
| + 'Try using PyOpenSSL if native code is an option.')
|
| else:
|
| pubkey = RSA.importKey(key_pem)
|
| return PyCryptoVerifier(pubkey)
|
| @@ -245,8 +214,6 @@
|
| Returns:
|
| string, The signature of the message for the given key.
|
| """
|
| - if isinstance(message, six.text_type):
|
| - message = message.encode('utf-8')
|
| return PKCS1_v1_5.new(self._key).sign(SHA256.new(message))
|
|
|
| @staticmethod
|
| @@ -263,12 +230,11 @@
|
| Raises:
|
| NotImplementedError if they key isn't in PEM format.
|
| """
|
| - parsed_pem_key = _parse_pem_key(key)
|
| - if parsed_pem_key:
|
| - pkey = RSA.importKey(parsed_pem_key)
|
| + if key.startswith('-----BEGIN '):
|
| + pkey = RSA.importKey(key)
|
| else:
|
| raise NotImplementedError(
|
| - 'PKCS12 format is not supported by the PyCrypto library. '
|
| + 'PKCS12 format is not supported by the PyCrpto library. '
|
| 'Try converting to a "PEM" '
|
| '(openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > privatekey.pem) '
|
| 'or using PyOpenSSL if native code is an option.')
|
| @@ -290,39 +256,19 @@
|
| 'PyOpenSSL, or PyCrypto 2.6 or later')
|
|
|
|
|
| -def _parse_pem_key(raw_key_input):
|
| - """Identify and extract PEM keys.
|
| -
|
| - Determines whether the given key is in the format of PEM key, and extracts
|
| - the relevant part of the key if it is.
|
| -
|
| - Args:
|
| - raw_key_input: The contents of a private key file (either PEM or PKCS12).
|
| -
|
| - Returns:
|
| - string, The actual key if the contents are from a PEM file, or else None.
|
| - """
|
| - offset = raw_key_input.find(b'-----BEGIN ')
|
| - if offset != -1:
|
| - return raw_key_input[offset:]
|
| -
|
| -
|
| def _urlsafe_b64encode(raw_bytes):
|
| - if isinstance(raw_bytes, six.text_type):
|
| - raw_bytes = raw_bytes.encode('utf-8')
|
| - return base64.urlsafe_b64encode(raw_bytes).decode('ascii').rstrip('=')
|
| + return base64.urlsafe_b64encode(raw_bytes).rstrip('=')
|
|
|
|
|
| def _urlsafe_b64decode(b64string):
|
| # Guard against unicode strings, which base64 can't handle.
|
| - if isinstance(b64string, six.text_type):
|
| - b64string = b64string.encode('ascii')
|
| - padded = b64string + b'=' * (4 - len(b64string) % 4)
|
| + b64string = b64string.encode('ascii')
|
| + padded = b64string + '=' * (4 - len(b64string) % 4)
|
| return base64.urlsafe_b64decode(padded)
|
|
|
|
|
| def _json_encode(data):
|
| - return json.dumps(data, separators=(',', ':'))
|
| + return simplejson.dumps(data, separators = (',', ':'))
|
|
|
|
|
| def make_signed_jwt(signer, payload):
|
| @@ -340,8 +286,8 @@
|
| header = {'typ': 'JWT', 'alg': 'RS256'}
|
|
|
| segments = [
|
| - _urlsafe_b64encode(_json_encode(header)),
|
| - _urlsafe_b64encode(_json_encode(payload)),
|
| + _urlsafe_b64encode(_json_encode(header)),
|
| + _urlsafe_b64encode(_json_encode(payload)),
|
| ]
|
| signing_input = '.'.join(segments)
|
|
|
| @@ -372,8 +318,9 @@
|
| """
|
| segments = jwt.split('.')
|
|
|
| - if len(segments) != 3:
|
| - raise AppIdentityError('Wrong number of segments in token: %s' % jwt)
|
| + if (len(segments) != 3):
|
| + raise AppIdentityError(
|
| + 'Wrong number of segments in token: %s' % jwt)
|
| signed = '%s.%s' % (segments[0], segments[1])
|
|
|
| signature = _urlsafe_b64decode(segments[2])
|
| @@ -381,15 +328,15 @@
|
| # Parse token.
|
| json_body = _urlsafe_b64decode(segments[1])
|
| try:
|
| - parsed = json.loads(json_body.decode('utf-8'))
|
| + parsed = simplejson.loads(json_body)
|
| except:
|
| raise AppIdentityError('Can\'t parse token: %s' % json_body)
|
|
|
| # Check signature.
|
| verified = False
|
| - for pem in certs.values():
|
| + for (keyname, pem) in certs.items():
|
| verifier = Verifier.from_string(pem, True)
|
| - if verifier.verify(signed, signature):
|
| + if (verifier.verify(signed, signature)):
|
| verified = True
|
| break
|
| if not verified:
|
| @@ -402,20 +349,21 @@
|
| earliest = iat - CLOCK_SKEW_SECS
|
|
|
| # Check expiration timestamp.
|
| - now = int(time.time())
|
| + now = long(time.time())
|
| exp = parsed.get('exp')
|
| if exp is None:
|
| raise AppIdentityError('No exp field in token: %s' % json_body)
|
| if exp >= now + MAX_TOKEN_LIFETIME_SECS:
|
| - raise AppIdentityError('exp field too far in future: %s' % json_body)
|
| + raise AppIdentityError(
|
| + 'exp field too far in future: %s' % json_body)
|
| latest = exp + CLOCK_SKEW_SECS
|
|
|
| if now < earliest:
|
| raise AppIdentityError('Token used too early, %d < %d: %s' %
|
| - (now, earliest, json_body))
|
| + (now, earliest, json_body))
|
| if now > latest:
|
| raise AppIdentityError('Token used too late, %d > %d: %s' %
|
| - (now, latest, json_body))
|
| + (now, latest, json_body))
|
|
|
| # Check audience.
|
| if audience is not None:
|
| @@ -424,6 +372,6 @@
|
| raise AppIdentityError('No aud field in token: %s' % json_body)
|
| if aud != audience:
|
| raise AppIdentityError('Wrong recipient, %s != %s: %s' %
|
| - (aud, audience, json_body))
|
| -
|
| - return parsed
|
| + (aud, audience, json_body))
|
| +
|
| + return parsed
|
|
|