Пример #1
0
    def do_native(self, type, src_type=""):
        """
        Fetch a local key

        :param type: Type of key
        :param src_type: How the key is stored
        """
        type = type.lower()
        src_type = src_type.lower()
        if type == "rsa":
            if src_type=="x509":
                _key = x509_rsa_loads(open(self.source).read())
                type = "rsa"
            else:
                _key = rsa_load(self.source)
        elif type == "ec":
            _key = ec_load(self.source)
        else: # Assume hmac
            _key = open(self.source).read()
            type = "hmac"

        try:
            self._key[type].append(_key)
        except KeyError:
            self._key[type] = [_key]
Пример #2
0
def test_dump_jwk():
    _ckey = x509_rsa_loads(open(CERT).read())
    jwk = dump_jwks([{"key": _ckey}])
    print jwk
    _wk = json.loads(jwk)
    assert _wk.keys() == ["keys"]
    assert len(_wk["keys"]) == 1
    assert _eq(_wk["keys"][0].keys(), ["kty", "e", "n"])
Пример #3
0
def test_kspec():
    _ckey = x509_rsa_loads(open(CERT).read())
    _jwk = RSA_key(key=_ckey)
    _jwk.decomp()

    print _jwk
    assert _jwk.kty == "RSA"
    assert _jwk.e == JWK["keys"][0]["e"]
    assert _jwk.n == JWK["keys"][0]["n"]
Пример #4
0
def test_load_jwk():
    _ckey = x509_rsa_loads(open(CERT).read())
    jwk = dump_jwks([{"key": _ckey}])
    wk = load_jwks(jwk)
    print wk
    assert len(wk) == 1
    key = wk[0]
    assert key.kty == "RSA"
    assert isinstance(key.key, M2Crypto.RSA.RSA)
Пример #5
0
def test_x509_rsa_loads_2():
    _ckey = x509_rsa_loads(open(CERT).read())
    _jwk = RSA_key(key=_ckey)
    _jwk.decomp()

    print _jwk

    _n = long_to_mpi(base64_to_long(str(_jwk.n)))

    cn = jwe.hd2ia(hexlify(_ckey.n))
    jn = jwe.hd2ia(hexlify(_n))

    assert cn == jn
Пример #6
0
def test_loads_0():
    keys = load_jwks(json.dumps(JWK))
    assert len(keys) == 1
    key = keys[0]
    assert key.kid == "abc"
    assert key.kty == "RSA"

    _ckey = x509_rsa_loads(open(CERT).read())

    print key
    _n = long_to_mpi(base64_to_long(str(key.n)))
    assert _n == _ckey.n
    _e = long_to_mpi(base64_to_long(str(key.e)))
    assert _e == _ckey.e
Пример #7
0
def test1():
    kj = KeyJar()
    part,res = key_export("http://example.com/keys/", "outbound", "secret",
                          keyjar=kj,
                          sig={"alg":"rsa", "format":["x509", "jwk"]})

    print part
    print res

    cert = "keys/outbound/cert.pem"
    jwk_def = "keys/outbound/jwk.json"

    _ckey = x509_rsa_loads(open(cert).read())

    _jkey = jwk.loads(open(jwk_def).read())[0][1]


    print jwe.hd2ia(hexlify(_ckey.n))
    print jwe.hd2ia(hexlify(_jkey.n))

    assert _ckey.n == _jkey.n
Пример #8
0
                        help="File containing a public RSA key")
    parser.add_argument('-k', dest="hmac_key",
                        help="If using a HMAC algorithm this is the key")
    parser.add_argument('-x', dest="x509_file",
                        help="File containing a X509 certificate")
    parser.add_argument("message", nargs="?",
                        help="The message to verify signature on")


    args = parser.parse_args()

    keys = {}
    if args.rsa_file:
        keys = {"rsa": [rsa_load(args.rsa_file)]}
    elif args.hmac_key:
        keys = {"hmac": [args.hmac_key]}
    elif args.x509_file:
        keys = {"rsa": [x509_rsa_loads(open(args.x509_file).read())]}
    elif args.rsa_pub_file:
        keys = {"rsa": [rsa_pub_load(args.rsa_pub_file)]}

    if args.message == "-":
        message = sys.stdin.read()
    else:
        message = args.message

    if keys:
        print verify(message, keys)
    else:
        print unpack(message)[1]
Пример #9
0
                        dest="hmac_key",
                        help="If using a HMAC algorithm this is the key")
    parser.add_argument('-x',
                        dest="x509_file",
                        help="File containing a X509 certificate")
    parser.add_argument("message",
                        nargs="?",
                        help="The message to verify signature on")

    args = parser.parse_args()

    keys = {}
    if args.rsa_file:
        keys = {"rsa": [rsa_load(args.rsa_file)]}
    elif args.hmac_key:
        keys = {"hmac": [args.hmac_key]}
    elif args.x509_file:
        keys = {"rsa": [x509_rsa_loads(open(args.x509_file).read())]}
    elif args.rsa_pub_file:
        keys = {"rsa": [rsa_pub_load(args.rsa_pub_file)]}

    if args.message == "-":
        message = sys.stdin.read()
    else:
        message = args.message

    if keys:
        print verify(message, keys)
    else:
        print unpack(message)[1]
Пример #10
0
def test_x509_rsa_loads():
    _ckey = x509_rsa_loads(open(CERT).read())
    assert isinstance(_ckey, M2Crypto.RSA.RSA_pub)
Пример #11
0
def test_dumps():
    _ckey = x509_rsa_loads(open(CERT).read())
    jwk = dump_jwk(_ckey)
    assert _eq(jwk.keys(), ["kty", "e", "n"])
Пример #12
0
def x509_rsa_load(txt):
    """ So I get the same output format as loads produces
    :param txt:
    :return:
    """
    return [("rsa", x509_rsa_loads(txt))]