Example #1
0
 def testEncode1(self):
     # Empty sequence
     der = DerBitString()
     self.assertEqual(der.encode(), b('\x03\x01\x00'))
     # Small payload
     der = DerBitString(b('\x01\x02'))
     self.assertEqual(der.encode(), b('\x03\x03\x00\x01\x02'))
     # Small payload
     der = DerBitString()
     der.value = b('\x01\x02')
     self.assertEqual(der.encode(), b('\x03\x03\x00\x01\x02'))
Example #2
0
 def testEncode1(self):
     # Empty sequence
     der = DerBitString()
     self.assertEquals(der.encode(), b('\x03\x01\x00'))
     # Small payload
     der = DerBitString(b('\x01\x02'))
     self.assertEquals(der.encode(), b('\x03\x03\x00\x01\x02'))
     # Small payload
     der = DerBitString()
     der.value = b('\x01\x02')
     self.assertEquals(der.encode(), b('\x03\x03\x00\x01\x02'))
Example #3
0
def forge(certificate_path: str, key_pem: str):
    """
    given a valid certificate_path and key_pem create a forged key
    """
    public_key_point = get_public_key(certificate_path)
    Q = Point(int(public_key_point[0:96], 16),
              int(public_key_point[96:], 16),
              curve=P384)

    # Generate rogue generator
    privkey_inv = 2
    # we take the private key as being the inverse of 2 modulo the curve order
    private_key = gmpy2.invert(privkey_inv, P384.q)  # pylint: disable=c-extension-no-member
    private_key = unhexlify(f"{private_key:x}".encode())
    # we multply our public key Q with the inverse of our chosen private key value
    roug_g = privkey_inv * Q
    roug_g = unhexlify(b"04" + f"{roug_g.x:x}".encode() +
                       f"{roug_g.y:x}".encode())

    # Generate the file with explicit parameters
    with open(key_pem, mode="rt") as handle:
        keyfile = PEM.decode(handle.read())

    seq_der = DerSequence()
    der = seq_der.decode(keyfile[0])

    # Replace private key
    octet_der = DerOctetString(private_key)
    der[1] = octet_der.encode()

    # Replace public key
    bits_der = DerBitString(unhexlify(b"04" + public_key_point))
    der[3] = b"\xa1\x64" + bits_der.encode()

    # Replace the generator
    seq_der = DerSequence()
    s = seq_der.decode(der[2][4:])  # pylint: disable=invalid-name
    octet_der = DerOctetString(roug_g)
    s[3] = octet_der.encode()
    der[2] = der[2][:4] + s.encode()

    return PEM.encode(der.encode(), "EC PRIVATE KEY")
Example #4
0
 def testInit2(self):
     der = DerBitString(DerInteger(1))
     self.assertEquals(der.encode(), b('\x03\x04\x00\x02\x01\x01'))
Example #5
0
 def testInit1(self):
     der = DerBitString(b("\xFF"))
     self.assertEquals(der.encode(), b('\x03\x02\x00\xFF'))
Example #6
0
 def testInit2(self):
     der = DerBitString(DerInteger(1))
     self.assertEqual(der.encode(), b('\x03\x04\x00\x02\x01\x01'))
Example #7
0
 def testInit1(self):
     der = DerBitString(b("\xFF"))
     self.assertEqual(der.encode(), b('\x03\x02\x00\xFF'))
Example #8
0
# Generate the file with explicit parameters
f = open('p384-key.pem', 'rt')
keyfile = PEM.decode(f.read())
#print(hexlify(keyfile[0]))
f.close()
seq_der = DerSequence()
der = seq_der.decode(keyfile[0])

# Replace private key
octet_der = DerOctetString(privkey)
der[1] = octet_der.encode()

# Replace public key
#print(hexlify(der[3]))
bits_der = DerBitString(unhexlify(b"04" + pubkey))
der[3] = b"\xa1\x64" + bits_der.encode()
#print(hexlify(der[3]))

# Replace the generator
#print(hexlify(der[2]))
seq_der = DerSequence()
s = seq_der.decode(der[2][4:])
octet_der = DerOctetString(rogueG)
s[3] = octet_der.encode()
der[2] = der[2][:4] + s.encode()
#print(hexlify(der[2]))

# Generate new file
f = open('p384-key-rogue.pem', 'w')
#print(hexlify(der.encode()))
keyfile = PEM.encode(der.encode(), 'EC PRIVATE KEY')
Example #9
0
print sys.argv[1]
print sys.argv[2]

modulus = int(sys.argv[1])
exponent = int(sys.argv[2])

# Codificacion HEX-DER del SEQUENCE(OID, NULL)
oid_seq = '300d06092a864886f70d0101010500'

# Codificacion DER del SEQUENCE(INTEGER, INTEGER)
int_seq = DerSequence()
int_seq.append(modulus)
int_seq.append(exponent)
int_seq = int_seq.encode()

# Codificacion DER del BIT STRING(...)
bs_der = DerBitString(int_seq)
bs_der = bs_der.encode()

# Codificacion DER del SEQUENCE(SEQUENCE, BIT STRING)
rsa_seq = DerSequence()
rsa_seq.append(unhexlify(oid_seq))
rsa_seq.append(bs_der)
rsa_seq = rsa_seq.encode()

# Exportar la clave publica formateada en hexadecimal a un fichero
f = open(sys.argv[3] + "/pub_key.txt", "w+")
f.write(hexlify(bytearray(rsa_seq)))
f.close()