def verify(self, plaintext: bytes, signature: bytes, strict_type_match: bool = True) -> bool: """ Verifies the `plaintext` against the `signature`. Parameters: plaintext (bytes): Plaintext to verify. signature (bytes): Signature to verify plaintext against. strict_type_match (bool): Whether or not to force use of `hash_obj` vs using the OID provided in the signature. Returns: bool: Whether or not the signature passed verification. """ from samson.utilities.runtime import RUNTIME try: padded = Bytes(self.rsa.encrypt(signature)) der_encoded = self.padder.unpad(padded) items = bytes_to_der_sequence(der_encoded) hash_obj = self.hash_obj if not strict_type_match: hash_obj = INVERSE_HASH_OID_LOOKUP[items[0][0]]() hashed_value = Bytes(items[1]) return RUNTIME.compare_bytes(hashed_value, hash_obj.hash(plaintext)) except Exception as _: return False
def check(buffer: bytes, **kwargs): try: items = bytes_to_der_sequence(buffer) return not PKCS8DSAPrivateKey.check(buffer) and len( items) == 2 and str(items[0][0]) == '1.2.840.10040.4.1' except Exception as _: return False
def decode(buffer: bytes, **kwargs): from samson.public_key.dsa import DSA items = bytes_to_der_sequence(buffer) p, q, g, y, x = [int(item) for item in items[1:6]] dsa = DSA(None, p=p, q=q, g=g, x=x) dsa.y = y return dsa
def decode(buffer: bytes, **kwargs): from samson.protocols.diffie_hellman import DiffieHellman items = bytes_to_der_sequence(buffer) p, g = [int(item) for item in items[1][1]] key, _ = decoder.decode(bytes(items[2])) key = int(key) dh = DiffieHellman(p=p, g=g, key=key) return dh
def decode(buffer: bytes, **kwargs): from samson.public_key.dsa import DSA items = bytes_to_der_sequence(buffer) p, q, g = [int(item) for item in items[1][1]] x, _ = decoder.decode(bytes(items[2])) x = int(x) dsa = DSA(None, p=p, q=q, g=g, x=x) return dsa
def decode(buffer: bytes, **kwargs): from samson.public_key.ecdsa import ECDSA items = bytes_to_der_sequence(buffer) d = Bytes(items[1]).int() x, y, curve = parse_ec_params(items, 2, 3) ecdsa = ECDSA(G=curve.G, hash_obj=None, d=d) ecdsa.Q = curve(x, y) return ecdsa
def decode(buffer: bytes, **kwargs): from samson.public_key.dsa import DSA items = bytes_to_der_sequence(buffer) y_sequence_bytes = Bytes(int(items[1])) y = int(decoder.decode(y_sequence_bytes)[0]) p, q, g = [int(item) for item in items[0][1]] dsa = DSA(None, p=p, q=q, g=g, x=0) dsa.y = y return dsa
def decode(buffer: bytes, **kwargs): from samson.public_key.rsa import RSA items = bytes_to_der_sequence(buffer) items = [int(item) for item in items] n, e = items rsa = RSA(8, e=e) rsa.n = n rsa.bits = rsa.n.bit_length() return rsa
def decode(buffer: bytes, **kwargs): from samson.public_key.eddsa import EdDSA items = bytes_to_der_sequence(buffer) curve_oid = str(items[1][0]) priv_key, _ = decoder.decode(bytes(items[2])) d = Bytes(priv_key).int() curve = EDCURVE_OID_LOOKUP[curve_oid] eddsa = EdDSA(d=d, curve=curve) return eddsa
def decode(buffer: bytes, **kwargs): from samson.public_key.eddsa import EdDSA items = bytes_to_der_sequence(buffer) pub_point = Bytes(int(items[1])) curve_oid = str(items[0][0]) curve = EDCURVE_OID_LOOKUP[curve_oid] eddsa = EdDSA(curve=curve) eddsa.A = eddsa.decode_point(pub_point) return eddsa
def decode(buffer: bytes, **kwargs): from samson.public_key.ecdsa import ECDSA items = bytes_to_der_sequence(buffer) # Move up OID for convenience items[0] = items[0][1] d = 1 x, y, curve = parse_ec_params(items, 0, 1) ecdsa = ECDSA(G=curve.G, hash_obj=None, d=d) ecdsa.Q = curve(x, y) return ecdsa
def decode(buffer: bytes, **kwargs): from samson.protocols.diffie_hellman import DiffieHellman items = bytes_to_der_sequence(buffer) y_sequence_bytes = Bytes(int(items[1])) y = int(decoder.decode(y_sequence_bytes)[0]) p, g = [int(item) for item in items[0][1]] dh = DiffieHellman(p=p, g=g) # Err, where do we put this? dh.y = y return dh
def decode(buffer: bytes, **kwargs): from samson.public_key.rsa import RSA items = bytes_to_der_sequence(buffer) items = [int(item) for item in items] del items[6:] del items[0] _n, e, _d, p, q = items rsa = RSA(0, p=p, q=q, e=e) rsa.bits = rsa.n.bit_length() return rsa
def decode(buffer: bytes, **kwargs): from samson.public_key.ecdsa import ECDSA items = bytes_to_der_sequence(buffer) curve_oid = items[1][1].asTuple() params, _ = decoder.decode(bytes(items[2])) d = Bytes(params[1]).int() x, y = ECDSA.decode_point(Bytes(int(params[2]))) oid_bytes = ber_encoder.encode(ObjectIdentifier(curve_oid))[2:] curve = WS_OID_LOOKUP[oid_bytes] ecdsa = ECDSA(d=d, G=curve.G) ecdsa.Q = curve(x, y) return ecdsa
def decode(buffer: bytes, **kwargs): from samson.public_key.rsa import RSA items = bytes_to_der_sequence(buffer) if type(items[1]) is BitString: if str(items[0][0]) == '1.2.840.113549.1.1.1': bitstring_seq = decoder.decode(Bytes(int(items[1])))[0] items = list(bitstring_seq) else: raise ValueError('Unable to decode RSA key.') n, e = [int(item) for item in items] rsa = RSA(8, e=e) rsa.n = n rsa.bits = rsa.n.bit_length() return rsa
def check(buffer: bytes, **kwargs): try: items = bytes_to_der_sequence(buffer) return len(items) == 2 except Exception as _: return False
def decode(buffer: bytes, **kwargs): from samson.protocols.diffie_hellman import DiffieHellman items = bytes_to_der_sequence(buffer) p, g = [int(item) for item in items] return DiffieHellman(g=g, p=p)
def check(buffer: bytes, **kwargs): items = bytes_to_der_sequence(buffer) return len(items) == 2 and int(items[0]) != 0
def decode(buffer: bytes, **kwargs): items = bytes_to_der_sequence(buffer) return PKCS1RSAPrivateKey.decode(bytes(items[2]))
def check(buffer: bytes, **kwargs): try: items = bytes_to_der_sequence(buffer) return len(items) == 3 and str(items[1][0]) == '1.2.840.113549.1.3.1' except Exception as _: return False
def check(buffer: bytes, **kwargs): try: items = bytes_to_der_sequence(buffer) return len(items) == 3 and str(items[1][0]) == '1.2.840.10045.2.1' except PyAsn1Error as _: return False