Esempio n. 1
0
    def to_der(self) -> bytes:
        """
        :return: The private key encoded in DER format.
        """
        pk = ECPrivateKey({
            'version':
            'ecPrivkeyVer1',
            'private_key':
            self.to_int(),
            'public_key':
            ECPointBitString(self.public_key.format(compressed=False)),
        })

        return PrivateKeyInfo({
            'version':
            0,
            'private_key_algorithm':
            PrivateKeyAlgorithm({
                'algorithm':
                'ec',
                'parameters':
                ECDomainParameters(name='named', value='1.3.132.0.10'),
            }),
            'private_key':
            pk,
        }).dump()
Esempio n. 2
0
def encode_named_curve_parameters(oid):
    """
    Return DER-encoded ANSI X.62 EC parameters for a named curve.

    Curve names are given by object identifier or common name. Names come
    from `asn1crypto
    <https://github.com/wbond/asn1crypto/blob/master/asn1crypto/keys.py#L338>`_.

    :param str oid: OID or named curve
    :rtype: bytes
    """
    return ECDomainParameters(
        name='named',
        value=NamedCurve.unmap(oid),
    ).dump()
Esempio n. 3
0
def encode_ec_public_key(key):
    """
    Encode a DER-encoded EC public key as stored by OpenSSL.

    :param PublicKey key: EC public key
    :rtype: bytes
    """

    ecparams = ECDomainParameters.load(key[Attribute.EC_PARAMS])
    ecpoint = bytes(OctetString.load(key[Attribute.EC_POINT]))

    return PublicKeyInfo({
        'algorithm': {
            'algorithm': 'ec',
            'parameters': ecparams,
        },
        'public_key': ecpoint,
    }).dump()
Esempio n. 4
0
    def to_der(self):
        pk = ECPrivateKey(
            {
                'version': ensure_unicode('ecPrivkeyVer1'),
                'private_key': self.to_int(),
                'public_key': ECPointBitString(self.public_key.format(compressed=False)),
            }
        )

        return PrivateKeyInfo(
            {
                'version': 0,
                'private_key_algorithm': PrivateKeyAlgorithm(
                    {
                        'algorithm': ensure_unicode('ec'),
                        'parameters': ECDomainParameters(name='named', value=ensure_unicode('1.3.132.0.10')),
                    }
                ),
                'private_key': pk,
            }
        ).dump()
Esempio n. 5
0
pkcs11 = PyKCS11.PyKCS11Lib()
pkcs11.load()

slot = pkcs11.getSlotList(tokenPresent=True)[0]

session = pkcs11.openSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION)
session.login("1234")

key_id = (0x22, )
label = "test"

# Select the curve to be used for the keys
curve = u"secp256r1"

# Setup the domain parameters, unicode conversion needed for the curve string
domain_params = ECDomainParameters(name="named", value=NamedCurve(curve))
ec_params = domain_params.dump()

ec_public_tmpl = [
    (PyKCS11.CKA_CLASS, PyKCS11.CKO_PUBLIC_KEY),
    (PyKCS11.CKA_PRIVATE, PyKCS11.CK_FALSE),
    (PyKCS11.CKA_TOKEN, PyKCS11.CK_TRUE),
    (PyKCS11.CKA_ENCRYPT, PyKCS11.CK_TRUE),
    (PyKCS11.CKA_VERIFY, PyKCS11.CK_TRUE),
    (PyKCS11.CKA_WRAP, PyKCS11.CK_TRUE),
    (PyKCS11.CKA_KEY_TYPE, PyKCS11.CKK_ECDSA),
    (PyKCS11.CKA_EC_PARAMS, ec_params),
    (PyKCS11.CKA_LABEL, label),
    (PyKCS11.CKA_ID, key_id),
]