Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
asn1crypto.keys.PrivateKeyInfo)
"""
len1 = bit_size // 8
len2 = bit_size // 16
prime1_offset = len1
prime2_offset = prime1_offset + len2
exponent1_offset = prime2_offset + len2
exponent2_offset = exponent1_offset + len2
coefficient_offset = exponent2_offset + len2
private_exponent_offset = coefficient_offset + len2
public_exponent = blob_struct.rsapubkey.pubexp
modulus = int_from_bytes(blob[0:prime1_offset][::-1])
prime1 = int_from_bytes(blob[prime1_offset:prime2_offset][::-1])
prime2 = int_from_bytes(blob[prime2_offset:exponent1_offset][::-1])
exponent1 = int_from_bytes(blob[exponent1_offset:exponent2_offset][::-1])
exponent2 = int_from_bytes(blob[exponent2_offset:coefficient_offset][::-1])
coefficient = int_from_bytes(blob[coefficient_offset:private_exponent_offset][::-1])
private_exponent = int_from_bytes(blob[private_exponent_offset:private_exponent_offset + len1][::-1])
public_key_info = keys.PublicKeyInfo({
'algorithm': keys.PublicKeyAlgorithm({
'algorithm': 'rsa',
}),
'public_key': keys.RSAPublicKey({
'modulus': modulus,
'public_exponent': public_exponent,
}),
})
output = None
dh_params_bytes = None
for record_type, _, record_data in _parse_tls_records(server_handshake_bytes):
if record_type != b'\x16':
continue
for message_type, message_data in _parse_handshake_messages(record_data):
if message_type == b'\x0c':
dh_params_bytes = message_data
break
if dh_params_bytes:
break
if dh_params_bytes:
output = int_from_bytes(dh_params_bytes[0:2]) * 8
return output
:return:
A generator that yields 2-element tuples:
[0] Byte string of extension type
[1] Byte string of extension data
"""
if data == b'':
return
extentions_length = int_from_bytes(data[0:2])
extensions_start = 2
extensions_end = 2 + extentions_length
pointer = extensions_start
while pointer < extensions_end:
extension_type = int_from_bytes(data[pointer:pointer + 2])
extension_length = int_from_bytes(data[pointer + 2:pointer + 4])
yield (
extension_type,
data[pointer + 4:pointer + 4 + extension_length]
)
pointer += 4 + extension_length
private_key must be an RSA key, not %s
''',
algo.upper()
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
rsa_private_key = private_key.asn1['private_key'].parsed
transformed_int = pow(
int_from_bytes(data),
rsa_private_key['private_exponent'].native,
rsa_private_key['modulus'].native
)
return int_to_bytes(transformed_int, width=private_key.asn1.byte_size)
def sig_verify(signature, public_key_info, file_hash_hex, hash_flag=FLAG_NOTHING):
try:
file_hash = binascii.a2b_hex(file_hash_hex)
algorithm0 = public_key_info['algorithm']['algorithm'].dotted
parameters = public_key_info['algorithm']['parameters'].native
if algorithm0 == '1.2.840.113549.1.1.1':
mod = public_key_info['public_key'].native['modulus']
exp = public_key_info['public_key'].native['public_exponent']
enc = bytes_to_long(signature)
dec = _rsa_decode(enc, exp, mod)
decoded_sig = long_to_bytes(dec)
if hash_flag == FLAG_NOTHING:
idx = 0
for byte in decoded_sig:
if byte in [b for b in b'\x00\x01\xff']:
idx += 1
if byte in [b for b in b'\x00']:
break
decoded_bytes = decoded_sig[idx:]
v = algos.DigestInfo.load(decoded_bytes)['digest'].native
# if DEBUG:print binascii.b2a_hex(file_hash), binascii.b2a_hex(v)
return file_hash == v
else:
return _pss_verify(decoded_sig, file_hash, hash_flag)
elif algorithm0 == '1.2.840.10040.4.1':
elif key_type == 'private':
prime1_byte_length = native(int, blob_struct.cbPrime1)
prime2_byte_length = native(int, blob_struct.cbPrime2)
prime1_offset = modulus_offset + modulus_byte_length
prime2_offset = prime1_offset + prime1_byte_length
exponent1_offset = prime2_offset + prime2_byte_length
exponent2_offset = exponent1_offset + prime2_byte_length
coefficient_offset = exponent2_offset + prime2_byte_length
private_exponent_offset = coefficient_offset + prime1_byte_length
prime1 = int_from_bytes(blob[prime1_offset:prime2_offset])
prime2 = int_from_bytes(blob[prime2_offset:exponent1_offset])
exponent1 = int_from_bytes(blob[exponent1_offset:exponent2_offset])
exponent2 = int_from_bytes(blob[exponent2_offset:coefficient_offset])
coefficient = int_from_bytes(blob[coefficient_offset:private_exponent_offset])
private_exponent = int_from_bytes(blob[private_exponent_offset:private_exponent_offset + modulus_byte_length])
rsa_private_key = keys.RSAPrivateKey({
'version': 'two-prime',
'modulus': modulus,
'public_exponent': public_exponent,
'private_exponent': private_exponent,
'prime1': prime1,
'prime2': prime2,
'exponent1': exponent1,
'exponent2': exponent2,
'coefficient': coefficient,
})
return keys.PrivateKeyInfo({
'version': 0,
decoded_bytes = decoded_sig[idx:]
v = algos.DigestInfo.load(decoded_bytes)['digest'].native
# if DEBUG:print binascii.b2a_hex(file_hash), binascii.b2a_hex(v)
return file_hash == v
else:
return _pss_verify(decoded_sig, file_hash, hash_flag)
elif algorithm0 == '1.2.840.10040.4.1':
pub = public_key_info['public_key'].native
p = parameters['p']
q = parameters['q']
g = parameters['g']
# print(encoded)
rs = algos.DSASignature.load(signature)
r = rs['r'].native
s = rs['s'].native
bytes_hign = bytes_to_long(file_hash)
bytes_hign = fix_dsa_hash_length(bytes_hign, q)
return _dsa_verify(p, q, g, pub, bytes_hign, r, s)
elif algorithm0 == '1.2.840.10045.2.1':
# {iso(1) member-body(2) us(840) ansi-x962(10045) keyType(2) ecPublicKey(1)}
pubkey = public_key_info['public_key'].native
cert_curve = parameters
if pubkey[0] != b'\x04'[0]:
# POINT_NULL = (0x00,)
# POINT_COMPRESSED = (0x02, 0x03)
# POINT_UNCOMPRESSED = (0x04,)
return False
ec_curve = EC_CURVE.get(str(cert_curve))
# print(ec_curve)
coord_size_p = int(math.ceil(math.log(ec_curve.get('p'), 2) / 8))
coord_size_n = int(math.ceil(math.log(ec_curve.get('n'), 2) / 8))
coord_size = coord_size_p # p or n ?
:param blob:
A byte string of the binary data contained after the struct
:return:
An asn1crypto.keys.PrivateKeyInfo or asn1crypto.keys.PublicKeyInfo
object, based on the key_type param
"""
public_exponent_byte_length = native(int, blob_struct.cbPublicExp)
modulus_byte_length = native(int, blob_struct.cbModulus)
modulus_offset = public_exponent_byte_length
public_exponent = int_from_bytes(blob[0:modulus_offset])
modulus = int_from_bytes(blob[modulus_offset:modulus_offset + modulus_byte_length])
if key_type == 'public':
return keys.PublicKeyInfo({
'algorithm': keys.PublicKeyAlgorithm({
'algorithm': 'rsa',
}),
'public_key': keys.RSAPublicKey({
'modulus': modulus,
'public_exponent': public_exponent,
}),
})
elif key_type == 'private':
prime1_byte_length = native(int, blob_struct.cbPrime1)
prime2_byte_length = native(int, blob_struct.cbPrime2)
Creates a generator returning tuples of information about each message in
a byte string of data from a TLS handshake record
:param data:
A byte string of a TLS handshake record data
:return:
A generator that yields 2-element tuples:
[0] Byte string of message type
[1] Byte string of message data
"""
pointer = 0
data_len = len(data)
while pointer < data_len:
length = int_from_bytes(data[pointer + 1:pointer + 4])
yield (
data[pointer:pointer + 1],
data[pointer + 4:pointer + 4 + length]
)
pointer += 4 + length
certificate_or_public_key must be an RSA key, not %s
''',
algo.upper()
))
if not isinstance(data, byte_cls):
raise TypeError(pretty_message(
'''
data must be a byte string, not %s
''',
type_name(data)
))
rsa_public_key = certificate_or_public_key.asn1['public_key'].parsed
transformed_int = pow(
int_from_bytes(data),
rsa_public_key['public_exponent'].native,
rsa_public_key['modulus'].native
)
return int_to_bytes(
transformed_int,
width=certificate_or_public_key.asn1.byte_size
)