def get_keypair(hostname): """Generate a dummy keypair for the given hostname. This method generates a dummy DSA keypair for the given hostname. It returns a tuple (pub, priv) where "pub" is a dict of values for the public key, and "priv" is a DSA128Key object containing the private key. Multiple calls to this method for the same hostname are guaranteed to produce the same key. To make this work we take advantage of the fact that DSA key generation is just "generate x by some random method, where 0 < x < q". Replace "some random method" with "sha1 hash of hostname" and we're all set. """ # Use pre-agreed parameters for p, q and g. q = DUMMY_Q p = DUMMY_P g = DUMMY_G # Generate private key x by "some random method". x = to_int(hashlib.sha1(hostname.encode("utf8")).hexdigest(), 16) assert x != 0, "SHA1(hostname) is zero - what are the odds?!" # Calculate public key y as usual. y = pow(g, x, p) data = { "algorithm": "DS", "p": to_hex(p), "q": to_hex(q), "g": to_hex(g), "y": to_hex(y), "x": to_hex(x), } privkey = jwt.DS128Key(data) del data["x"] return data, privkey
def get_keypair(hostname): """Generate a dummy keypair for the given hostname. This method generates a dummy DSA keypair for the given hostname. It returns a tuple (pub, priv) where "pub" is a dict of values for the public key, and "priv" is a DSA128Key object containing the private key. Multiple calls to this method for the same hostname are guaranteed to produce the same key. To make this work we take advantage of the fact that DSA key generation is just "generate x by some random method, where 0 < x < q". Replace "some random method" with "sha1 hash of hostname" and we're all set. """ # Use pre-agreed parameters for p, q and g. q = DUMMY_Q p = DUMMY_P g = DUMMY_G # Generate private key x by "some random method". x = to_int(hashlib.sha1(hostname.encode("utf8")).hexdigest(), 16) assert x != 0, "SHA1(hostname) is zero - what are the odds?!" # Calculate public key y as usual. y = pow(g, x, p) data = {"algorithm": "DS", "p": to_hex(p), "q": to_hex(q), "g": to_hex(g), "y": to_hex(y), "x": to_hex(x)} privkey = jwt.DS128Key(data) del data["x"] return data, privkey
# if unittest2 isn't available, assume that we are python 2.7 try: import unittest2 as unittest except ImportError: import unittest # NOQA # These are values used to generate dummy DSA keys. # I took them directly from the javacript jwcrypto source code, which claims: # """ # the following are based on the first FIPS186-3 test vectors for 1024/160 # SHA-256 under the category A.2.3 Verifiable Canonical Generation of the # Generator g # """ DUMMY_Q = to_int("e21e04f911d1ed7991008ecaab3bf775984309c3", 16) DUMMY_P = to_int( """ ff600483db6abfc5b45eab78594b3533d550d9f1bf2a992a7a8daa6dc34f8045ad4e6e0c429 d334eeeaaefd7e23d4810be00e4cc1492cba325ba81ff2d5a5b305a8d17eb3bf4a06a349d39 2e00d329744a5179380344e82a18c47933438f891e22aeef812d69c8f75e326cb70ea000c3f 776dfdbd604638c2ef717fc26d02e17 """, 16, ) DUMMY_G = to_int( """ c52a4a0ff3b7e61fdf1867ce84138369a6154f4afa92966e3c827e25cfa6cf508b90e5de419
from browserid import jwt # if unittest2 isn't available, assume that we are python 2.7 try: import unittest2 as unittest except ImportError: import unittest # NOQA # These are values used to generate dummy DSA keys. # I took them directly from the javacript jwcrypto source code, which claims: # """ # the following are based on the first FIPS186-3 test vectors for 1024/160 # SHA-256 under the category A.2.3 Verifiable Canonical Generation of the # Generator g # """ DUMMY_Q = to_int("e21e04f911d1ed7991008ecaab3bf775984309c3", 16) DUMMY_P = to_int( """ ff600483db6abfc5b45eab78594b3533d550d9f1bf2a992a7a8daa6dc34f8045ad4e6e0c429 d334eeeaaefd7e23d4810be00e4cc1492cba325ba81ff2d5a5b305a8d17eb3bf4a06a349d39 2e00d329744a5179380344e82a18c47933438f891e22aeef812d69c8f75e326cb70ea000c3f 776dfdbd604638c2ef717fc26d02e17 """, 16) DUMMY_G = to_int( """ c52a4a0ff3b7e61fdf1867ce84138369a6154f4afa92966e3c827e25cfa6cf508b90e5de419 e1337e07a2e9e2a3cd5dea704d175f8ebf6af397d69e110b96afb17c7a03259329e4829b0d0 3bbc7896b15b4ade53e130858cc34d96269aa89041f409136c7242a38895c9d5bccad4f389a f1d7a4bd1398bd072dffa896233397a
HASHMOD = jwt.DS256Key.HASHMOD def _long(value): return long(value.replace(" ", "").replace("\n", "").strip()) def _hex(value): return hex(long(value.replace(" ", "").replace("\n", "").strip())) # These are dummy key data that I generated via PyCrypto. # M2Crypto doesn't seem to let me get at the individual parameters. RS64_KEY_DATA = { "e": to_int("65537"), "n": to_int("""110897663942528265066856163966583557538666146275146 569193074111045116764854772535689458732714049671807506 396649306730328647317126800964431366624486416551078177 528195103050868728550429561392842977259407335332582178 624191611001106449477645116630750398871838788574825885 770446686329706009000279629721965986677219"""), "d": to_int("""295278123166626215026113502482091502365034141401240 159363282304307076544046230487782634982660202141239450 481640966544735782181647417005558287318200095948234745 214183393770321992676297531378428617531522265932631860 693144704788708252936752025413728425562033678747736289 64114133156747686886305629893015763517873"""), }
# You can obtain one at http://mozilla.org/MPL/2.0/. from browserid.tests.support import unittest from browserid.utils import to_int try: from browserid.crypto.m2 import int2mpint import browserid.crypto._m2_monkeypatch as _m2 HAVE_M2CRYPTO = True except ImportError: HAVE_M2CRYPTO = False # Dummy RSA key for testing purposes. DUMMY_RSA_E = to_int("65537") DUMMY_RSA_N = to_int("""110897663942528265066856163966583557538666146275146 569193074111045116764854772535689458732714049671807506 396649306730328647317126800964431366624486416551078177 528195103050868728550429561392842977259407335332582178 624191611001106449477645116630750398871838788574825885 770446686329706009000279629721965986677219""") DUMMY_RSA_D = to_int("""295278123166626215026113502482091502365034141401240 159363282304307076544046230487782634982660202141239450 481640966544735782181647417005558287318200095948234745 214183393770321992676297531378428617531522265932631860 693144704788708252936752025413728425562033678747736289 64114133156747686886305629893015763517873""") class TestM2MonkeyPatch(unittest.TestCase):
# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/. import time from browserid.utils import (encode_bytes, to_int, to_hex) from browserid import jwt MOCKMYID_MODULUS = to_int("15498874758090276039465094105837231567265546373975960480941122651107772824121527483107402353899846252489837024870191707394743196399582959425513904762996756672089693541009892030848825079649783086005554442490232900875792851786203948088457942416978976455297428077460890650409549242124655536986141363719589882160081480785048965686285142002320767066674879737238012064156675899512503143225481933864507793118457805792064445502834162315532113963746801770187685650408560424682654937744713813773896962263709692724630650952159596951348264005004375017610441835956073275708740239518011400991972811669493356682993446554779893834303"); MOCKMYID_PUBLIC_EXPONENT = to_int("65537"); MOCKMYID_PRIVATE_EXPONENT = to_int("6539906961872354450087244036236367269804254381890095841127085551577495913426869112377010004955160417265879626558436936025363204803913318582680951558904318308893730033158178650549970379367915856087364428530828396795995781364659413467784853435450762392157026962694408807947047846891301466649598749901605789115278274397848888140105306063608217776127549926721544215720872305194645129403056801987422794114703255989202755511523434098625000826968430077091984351410839837395828971692109391386427709263149504336916566097901771762648090880994773325283207496645630792248007805177873532441314470502254528486411726581424522838833"); MOCKMYID_KEY_DATA = {'n':MOCKMYID_MODULUS, 'e':MOCKMYID_PUBLIC_EXPONENT, 'd':MOCKMYID_PRIVATE_EXPONENT, 'algorithm':'RS'} data = MOCKMYID_KEY_DATA.copy() MOCKMYID_KEY = jwt.RS256Key(data) data.pop('d') MOCKMYID_PUBKEY = jwt.RS256Key(data) def make_certificate(email, email_public_key, issuer, issuer_private_key, iat=None, exp=None): # Generate the certificate signing the email's public key # with the issuer's private key. if issuer is None: issuer = "mockmyid.com" if iat is None: iat = int(time.time() * 1000) if exp is None: exp = int(iat + 60 * 1000)
# License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/. from browserid.tests.support import unittest from browserid.utils import to_int try: from browserid.crypto.m2 import int2mpint import browserid.crypto._m2_monkeypatch as _m2 HAVE_M2CRYPTO = True except ImportError: HAVE_M2CRYPTO = False # Dummy RSA key for testing purposes. DUMMY_RSA_E = to_int("65537") DUMMY_RSA_N = to_int("""110897663942528265066856163966583557538666146275146 569193074111045116764854772535689458732714049671807506 396649306730328647317126800964431366624486416551078177 528195103050868728550429561392842977259407335332582178 624191611001106449477645116630750398871838788574825885 770446686329706009000279629721965986677219""") DUMMY_RSA_D = to_int("""295278123166626215026113502482091502365034141401240 159363282304307076544046230487782634982660202141239450 481640966544735782181647417005558287318200095948234745 214183393770321992676297531378428617531522265932631860 693144704788708252936752025413728425562033678747736289 64114133156747686886305629893015763517873""") class TestM2MonkeyPatch(unittest.TestCase):
BITLENGTH = jwt.DS256Key.BITLENGTH HASHMOD = jwt.DS256Key.HASHMOD def _long(value): return long(value.replace(" ", "").replace("\n", "").strip()) def _hex(value): return hex(long(value.replace(" ", "").replace("\n", "").strip())) # These are dummy key data that I generated via PyCrypto. # M2Crypto doesn't seem to let me get at the individual parameters. RS64_KEY_DATA = { "e": to_int("65537"), "n": to_int("""110897663942528265066856163966583557538666146275146 569193074111045116764854772535689458732714049671807506 396649306730328647317126800964431366624486416551078177 528195103050868728550429561392842977259407335332582178 624191611001106449477645116630750398871838788574825885 770446686329706009000279629721965986677219"""), "d": to_int("""295278123166626215026113502482091502365034141401240 159363282304307076544046230487782634982660202141239450 481640966544735782181647417005558287318200095948234745 214183393770321992676297531378428617531522265932631860 693144704788708252936752025413728425562033678747736289 64114133156747686886305629893015763517873"""), }