Exemple #1
0
def sigdecode_der(sig_der, order):
    # return der.encode_sequence(der.encode_integer(r), der.encode_integer(s))
    rs_strings, empty = der.remove_sequence(sig_der)
    if empty != b(""):
        raise der.UnexpectedDER("trailing junk after DER sig: %s" %
                                binascii.hexlify(empty))
    r, rest = der.remove_integer(rs_strings)
    s, empty = der.remove_integer(rest)
    if empty != b(""):
        raise der.UnexpectedDER("trailing junk after DER numbers: %s" %
                                binascii.hexlify(empty))
    return r, s
Exemple #2
0
 def from_der(klass, string):
     # [[oid_ecPublicKey,oid_curve], point_str_bitstring]
     s1,empty = der.remove_sequence(string)
     if empty != "":
         raise der.UnexpectedDER("trailing junk after DER pubkey: %s" %
                                 binascii.hexlify(empty))
     s2,point_str_bitstring = der.remove_sequence(s1)
     # s2 = oid_ecPublicKey,oid_curve
     oid_pk, rest = der.remove_object(s2)
     oid_curve, empty = der.remove_object(rest)
     if empty != "":
         raise der.UnexpectedDER("trailing junk after DER pubkey objects: %s" %
                                 binascii.hexlify(empty))
     assert oid_pk == oid_ecPublicKey, (oid_pk, oid_ecPublicKey)
     curve = find_curve(oid_curve)
     point_str, empty = der.remove_bitstring(point_str_bitstring)
     if empty != "":
         raise der.UnexpectedDER("trailing junk after pubkey pointstring: %s" %
                                 binascii.hexlify(empty))
     assert point_str.startswith("\x00\x04")
     return klass.from_string(point_str[2:], curve)
Exemple #3
0
    def from_der(klass, string, hashfunc=sha1):
        # SEQ([int(1), octetstring(privkey),cont[0], oid(secp224r1),
        #      cont[1],bitstring])
        s, empty = der.remove_sequence(string)
        if empty != b(""):
            raise der.UnexpectedDER("trailing junk after DER privkey: %s" %
                                    binascii.hexlify(empty))
        one, s = der.remove_integer(s)
        if one != 1:
            raise der.UnexpectedDER("expected '1' at start of DER privkey,"
                                    " got %d" % one)
        privkey_str, s = der.remove_octet_string(s)
        tag, curve_oid_str, s = der.remove_constructed(s)
        if tag != 0:
            raise der.UnexpectedDER("expected tag 0 in DER privkey,"
                                    " got %d" % tag)
        curve_oid, empty = der.remove_object(curve_oid_str)
        if empty != b(""):
            raise der.UnexpectedDER("trailing junk after DER privkey "
                                    "curve_oid: %s" % binascii.hexlify(empty))
        curve = find_curve(curve_oid)

        # we don't actually care about the following fields
        #
        # tag, pubkey_bitstring, s = der.remove_constructed(s)
        # if tag != 1:
        #     raise der.UnexpectedDER("expected tag 1 in DER privkey, got %d"
        #                             % tag)
        # pubkey_str = der.remove_bitstring(pubkey_bitstring)
        # if empty != "":
        #     raise der.UnexpectedDER("trailing junk after DER privkey "
        #                             "pubkeystr: %s" % binascii.hexlify(empty))

        # our from_string method likes fixed-length privkey strings
        if len(privkey_str) < curve.baselen:
            privkey_str = b("\x00") * (curve.baselen -
                                       len(privkey_str)) + privkey_str
        return klass.from_string(privkey_str, curve, hashfunc)