DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c43b0a75e166)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Firefox Identity Server.
#
# The Initial Developer of the Original Code is JR Conlin
# Portions created by the Initial Developer are Copyright (C) 2011
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
from M2Crypto import RSA, BIO, ASN1
from hashlib import sha256, sha384, sha512
from oidserver import logger

import base64
import hmac
import cjson


class JWSException (Exception):

    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


class JWS:
    _header = None      # header information
    _crypto = None      # usually the signature
    _claim = None       # the payload of the JWS
    _config = {}        # App specific configuration values


    def __init__(self, config = None, **kw):
        if config:
            self._config = config
        else:
            config = {}
        self._sign = {'HS': self._sign_HS,
                 'RS': self._sign_RS,
                 'NO':  self._sign_NONE}
        self._verify = {'HS': self._verify_HS,
                   'RS': self._verify_RS,
                   'NO':  self._verify_NONE}

    def sign(self, payload, header = None, alg = None, **kw):
        if payload is None:
            raise (JWSException("Cannot encode empty payload"))
        header = self.header(alg = alg)
        if alg is None:
            alg = header.get('alg', 'NONE')
        try:
            signer = self._sign.get(alg[:2].upper())
        except KeyError, ex:
            logger.error("Invalid JWS Sign method specified %s", str(ex))
            raise(JWSException("Unsupported encoding method specified"))
        header_str = base64.urlsafe_b64encode(cjson.encode(header))
        payload_str = base64.urlsafe_b64encode(cjson.encode(payload))
        sbs = "%s.%s" % (header_str, payload_str)
        signed = signer(alg, header, sbs)
        if signed:
            return signed
        else:
            return sbs

    def parse(self, jws, **kw):
        if not jws:
            raise(JWSException("Cannot verify empty JWS"))
        if self.verify(jws):
            (head, payload_str, signature) = jws.split('.')
            payload = cjson.decode(base64.b64decode(payload_str))
            return payload
        else:
            raise(JWSException("Invalid JWS"))

    def verify(self, jws, alg = None, **kw):
        if not jws:
            raise (JWSException("Cannot verify empty JWS"))
        try:
            (header_str, payload_str, signature) = jws.split('.')
            header = cjson.decode(base64.b64decode(header_str))
            if alg is None:
                alg = header.get('alg', 'NONE')
            try:
                sigcheck = self._verify.get(alg[:2].upper())
            except KeyError, ex:
                logger.error("Invalid JWS Sign method specified %s", str(ex))
                raise(JWSException("Unsupported encoding method specified"))
            return sigcheck(alg,
                            header,
                            '%s.%s' % (header_str, payload_str),
                            signature)
        except ValueError, ex:
            logger.error("JWS Verification error: %s", ex)
            raise(JWSException("JWS has invalid format"))

    def _sign_NONE(self, alg, header, sbs):
        """ No encryption has no encryption.
            duh.
        """
        return None;

    def _get_sha(self, depth):
        depths = {'256': sha256,
                 '384': sha384,
                 '512': sha512}
        if depth not in depths:
            raise(JWSException('Invalid Depth specified for HS'))
        return depths.get(depth)

    def _sign_HS(self, alg, header, sbs):
        server_secret = self._config.get('jws.server_secret', '')
        signature = hmac.new(server_secret, sbs,
                             self._get_sha(alg[2:])).digest()
        return '%s.%s' % (sbs, base64.urlsafe_b64encode(signature))

    def _sign_RS(self, alg, header, sbs):
        priv_key_u = self._config.get('jws.rsa_key_path', None)
        if priv_key_u is None:
            raise(JWSException("No private key found for RSA signature"))
        bio = BIO.openfile(priv_key_u)
        rsa = RSA.load_key_bio(bio)
        if not rsa.check_key():
            raise(JWSException("Invalid key specified"))
        digest = self._get_sha(alg[2:])(sbs).digest()
        signature = rsa.sign_rsassa_pss(digest)
        return '%s.%s' % (sbs, base64.urlsafe_b64encode(signature))

    def _verify_NONE(self, alg, header, sbs, signature):
        """ There's really no way to verify this. """
        return len(sbs) != 0

    def _verify_HS(self, alg, header, sbs, signature):
        server_secret = self._config.get('jws.server_secret', '')
        tsignature = hmac.new(server_secret,
                              sbs,
                              self._get_sha(alg[2:])).digest()
        return (base64.urlsafe_b64encode(tsignature)) == signature

    def _verify_RS(self, alg, header, sbs, signature, testKey=None):
        #rsa.verify(sbs, pubic_key)
        ## fetch the public key
        if testKey:
            pub = testKey
        else:
            pub = fetch_rsa_pub_key(header)
        rsa = RSA.new_pub_key((pub.get('e'), pub.get('n')))
        digest = self._get_sha(alg[2:])(sbs).digest()
        return rsa.verify_rsassa_pss(digest,
                          base64.urlsafe_b64decode(signature))

    def header(self, header = None, alg = None, **kw):
        """ return the stored header or generate one from scratch. """
        if header:
            self._header = header
        if self._header:
            return self._header
        if not alg:
            alg = self._config.get('jws.default_alg', 'HS256')
        self._header = {
            'alg': alg,
            'typ': self._config.get('jws.default_typ', 'IDAssertion'),
            'jku': kw.get('jku', ''),
            'kid': self._config.get('jws.default_kid', ''),
            'pem': kw.get('pem', ''),
            'x5t': self._config.get('jws.default_x5t', ''),
            'x5u': self._config.get('jws.default_x5u', '')

        }
        return self._header


def create_rsa_jki_entry(pubKey, keyid=None):
    keys = cjson.decode(base64.b64decode(pubKey))
    vinz = {'algorithm': 'RSA',
            'modulus': keys.get('n'),
            'exponent': keys.get('e')}
    if keyid is not None:
        vinz['keyid'] = keyid
    return vinz

##REDO
def fetch_rsa_pub_key(header, **kw):
    ## if 'test' defined, use that value for the returned pub key (blech)
    ## extract the target machine from the header.
    if keytype is None and keyname is None:
        raise JWSException('Must specify either keytype or keyname')
    try:
        if 'pem' in header and header.get('pem', None):
            key = base64.urlsafe_b64decode(header.get('pem')).strip()
            bio = BIO.MemoryBuffer(key)
            pubbit = RSA.load_key_bio(bio).pub()
            pub = {
                'n': int(pubbits[0].encode('hex'), 16),
                'e': int(pubbits[1].encode('hex'), 16)
            }
        elif 'jku' in header and header.get('jku', None):
            key = header['jku']
            if key.lower().startswith('data:'):
                pub = cjson.decode(key[key.index('base64,')+7:])
        return pub
        """
        pub = {
            'n': key.get('modulus', None),
            'e': key.get('exponent', None)
        }
        #"""
        # return pub
    except (AttributeError, KeyError), ex:
        logger.error("Internal RSA error: %s" % str(ex))
        raise(JWSException("Could not extract key"))