def build_key_bundle(key_conf, kid_template=""):
    """
    Builds a :py:class:`oidcmsg.key_bundle.KeyBundle` instance based on a key
    specification.

    An example of such a specification::

        keys = [
            {"type": "RSA", "key": "cp_keys/key.pem", "use": ["enc", "sig"]},
            {"type": "EC", "crv": "P-256", "use": ["sig"], "kid": "ec.1"},
            {"type": "EC", "crv": "P-256", "use": ["enc"], "kid": "ec.2"}
        ]

    Keys in this specification are:

    type
        The type of key. Presently only 'rsa' and 'ec' supported.

    key
        A name of a file where a key can be found. Only works with PEM encoded
        RSA keys

    use
        What the key should be used for

    crv
        The elliptic curve that should be used. Only applies to elliptic curve
        keys :-)

    kid
        Key ID, can only be used with one usage type is specified. If there
        are more the one usage type specified 'kid' will just be ignored.

    :param key_conf: The key configuration
    :param kid_template: A template by which to build the key IDs. If no
        kid_template is given then the built-in function add_kid() will be used.
    :return: A KeyBundle instance
    """

    kid = 0

    tot_kb = KeyBundle()
    for spec in key_conf:
        typ = spec["type"].upper()

        if typ == "RSA":
            if "key" in spec:
                error_to_catch = (OSError, IOError,
                                  DeSerializationNotPossible)
                try:
                    kb = KeyBundle(source="file://%s" % spec["key"],
                                   fileformat="der",
                                   keytype=typ, keyusage=spec["use"])
                except error_to_catch:
                    kb = rsa_init(spec)
                except Exception:
                    raise
            else:
                kb = rsa_init(spec)
        elif typ == "EC":
            kb = ec_init(spec)
        else:
            continue

        if 'kid' in spec and len(kb) == 1:
            ks = kb.keys()
            ks[0].kid = spec['kid']
        else:
            for k in kb.keys():
                if kid_template:
                    k.kid = kid_template % kid
                    kid += 1
                else:
                    k.add_kid()

        tot_kb.extend(kb.keys())

    return tot_kb
def build_key_bundle ( key_conf , kid_template = "" ) : kid = 0 tot_kb = KeyBundle ( ) for spec in key_conf : typ = spec [ "type" ] . upper ( ) if typ == "RSA" : if "key" in spec : error_to_catch = ( OSError , IOError , DeSerializationNotPossible ) try : kb = KeyBundle ( source = "file://%s" % spec [ "key" ] , fileformat = "der" , keytype = typ , keyusage = spec [ "use" ] ) except error_to_catch : kb = rsa_init ( spec ) except Exception : raise else : kb = rsa_init ( spec ) elif typ == "EC" : kb = ec_init ( spec ) else : continue if 'kid' in spec and len ( kb ) == 1 : ks = kb . keys ( ) ks [ 0 ] . kid = spec [ 'kid' ] else : for k in kb . keys ( ) : if kid_template : k . kid = kid_template % kid kid += 1 else : k . add_kid ( ) tot_kb . extend ( kb . keys ( ) ) return tot_kb
Builds a :py:class:`oidcmsg.key_bundle.KeyBundle` instance based on a key
    specification.

    An example of such a specification::

        keys = [
            {"type": "RSA", "key": "cp_keys/key.pem", "use": ["enc", "sig"]},
            {"type": "EC", "crv": "P-256", "use": ["sig"], "kid": "ec.1"},
            {"type": "EC", "crv": "P-256", "use": ["enc"], "kid": "ec.2"}
        ]

    Keys in this specification are:

    type
        The type of key. Presently only 'rsa' and 'ec' supported.

    key
        A name of a file where a key can be found. Only works with PEM encoded
        RSA keys

    use
        What the key should be used for

    crv
        The elliptic curve that should be used. Only applies to elliptic curve
        keys :-)

    kid
        Key ID, can only be used with one usage type is specified. If there
        are more the one usage type specified 'kid' will just be ignored.

    :param key_conf: The key configuration
    :param kid_template: A template by which to build the key IDs. If no
        kid_template is given then the built-in function add_kid() will be used.
    :return: A KeyBundle instance