def sym_init(spec):
    """
    Initiates a :py:class:`oidcmsg.keybundle.KeyBundle` instance
    containing newly minted SYM keys according to a spec.

    Example of specification::
        {'bytes':24, 'use': ['enc', 'sig'] }

    Using the spec above 2 SYM keys would be minted, one for
    encryption and one for signing.

    :param spec:
    :return: KeyBundle
    """

    try:
        size = int(spec['bytes'])
    except KeyError:
        size = 24

    _kb = KeyBundle(keytype="OCT")
    if 'use' in spec:
        for use in harmonize_usage(spec["use"]):
            _key = new_sym_key(use=use, bytes=size)
            _kb.append(_key)
    else:
        _key = new_sym_key(bytes=size)
        _kb.append(_key)

    return _kb
Example #2
0
def test_mint_new_sym_key():
    key = new_sym_key(bytes=24, use="sig", kid="one")
    assert key
    assert key.use == "sig"
    assert key.kid == "one"
    assert len(key.key) == 24
    assert key.key_len() == 24
Example #3
0
def main():
    """Main function"""
    parser = argparse.ArgumentParser(description="JSON Web Key (JWK) Generator")

    parser.add_argument("--kty", dest="kty", metavar="type", help="Key type", required=True)
    parser.add_argument("--size", dest="keysize", type=int, metavar="size", help="Key size")
    parser.add_argument(
        "--crv",
        dest="crv",
        metavar="curve",
        help="EC curve",
        choices=NIST2SEC.keys(),
        default=DEFAULT_EC_CURVE,
    )
    parser.add_argument(
        "--exp",
        dest="rsa_exp",
        type=int,
        metavar="exponent",
        help="RSA public key exponent (default {})".format(DEFAULT_RSA_EXP),
        default=DEFAULT_RSA_EXP,
    )
    parser.add_argument("--kid", dest="kid", metavar="id", help="Key ID")
    args = parser.parse_args()

    if args.kty.upper() == "RSA":
        if args.keysize is None:
            args.keysize = DEFAULT_RSA_KEYSIZE
        jwk = new_rsa_key(public_exponent=args.rsa_exp, key_size=args.keysize, kid=args.kid)
    elif args.kty.upper() == "EC":
        if args.crv not in NIST2SEC:
            print("Unknown curve: {0}".format(args.crv), file=sys.stderr)
            exit(1)
        jwk = new_ec_key(crv=args.crv, kid=args.kid)
    elif args.kty.upper() == "SYM" or args.kty.upper() == "OCT":
        if args.keysize is None:
            args.keysize = DEFAULT_SYM_KEYSIZE
        jwk = new_sym_key(bytes=args.keysize, kid=args.kid)
    else:
        print("Unknown key type: {}".format(args.kty), file=sys.stderr)
        exit(1)

    jwk_dict = jwk.serialize(private=True)
    print(json.dumps(jwk_dict, sort_keys=True, indent=4))
    print("SHA-256: " + jwk.thumbprint("SHA-256").decode(), file=sys.stderr)
def test_mint_new_sym_key():
    key = new_sym_key(bytes=24, use='sig', kid='one')
    assert key
    assert key.use == 'sig'
    assert key.kid == 'one'
    assert len(key.key) == 24