2013-10-07 20:55:35 +00:00
|
|
|
import os, ssl, time, datetime
|
2012-06-27 04:42:00 +00:00
|
|
|
from pyasn1.type import univ, constraint, char, namedtype, tag
|
|
|
|
from pyasn1.codec.der.decoder import decode
|
2012-07-24 02:55:54 +00:00
|
|
|
from pyasn1.error import PyAsn1Error
|
2012-06-27 04:42:00 +00:00
|
|
|
import OpenSSL
|
2012-06-27 20:15:55 +00:00
|
|
|
import tcp
|
2012-06-27 04:42:00 +00:00
|
|
|
|
2013-10-07 20:48:30 +00:00
|
|
|
default_exp = 62208000 # =24 * 60 * 60 * 720
|
|
|
|
default_o = "mitmproxy"
|
|
|
|
default_cn = "mitmproxy"
|
2012-06-27 04:42:00 +00:00
|
|
|
|
2013-10-07 20:48:30 +00:00
|
|
|
def create_ca(o=default_o, cn=default_cn, exp=default_exp):
|
2012-06-27 04:42:00 +00:00
|
|
|
key = OpenSSL.crypto.PKey()
|
|
|
|
key.generate_key(OpenSSL.crypto.TYPE_RSA, 1024)
|
|
|
|
ca = OpenSSL.crypto.X509()
|
|
|
|
ca.set_serial_number(int(time.time()*10000))
|
|
|
|
ca.set_version(2)
|
2013-10-07 20:48:30 +00:00
|
|
|
ca.get_subject().CN = cn
|
|
|
|
ca.get_subject().O = o
|
2012-06-27 04:42:00 +00:00
|
|
|
ca.gmtime_adj_notBefore(0)
|
2013-10-07 20:48:30 +00:00
|
|
|
ca.gmtime_adj_notAfter(exp)
|
2012-06-27 04:42:00 +00:00
|
|
|
ca.set_issuer(ca.get_subject())
|
|
|
|
ca.set_pubkey(key)
|
|
|
|
ca.add_extensions([
|
|
|
|
OpenSSL.crypto.X509Extension("basicConstraints", True,
|
|
|
|
"CA:TRUE"),
|
|
|
|
OpenSSL.crypto.X509Extension("nsCertType", True,
|
|
|
|
"sslCA"),
|
|
|
|
OpenSSL.crypto.X509Extension("extendedKeyUsage", True,
|
|
|
|
"serverAuth,clientAuth,emailProtection,timeStamping,msCodeInd,msCodeCom,msCTLSign,msSGC,msEFS,nsSGC"
|
|
|
|
),
|
|
|
|
OpenSSL.crypto.X509Extension("keyUsage", False,
|
|
|
|
"keyCertSign, cRLSign"),
|
|
|
|
OpenSSL.crypto.X509Extension("subjectKeyIdentifier", False, "hash",
|
|
|
|
subject=ca),
|
|
|
|
])
|
|
|
|
ca.sign(key, "sha1")
|
|
|
|
return key, ca
|
|
|
|
|
|
|
|
|
2013-10-07 20:48:30 +00:00
|
|
|
def dummy_ca(path, o=default_o, cn=default_cn, exp=default_exp):
|
2012-06-27 04:42:00 +00:00
|
|
|
dirname = os.path.dirname(path)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
|
|
|
if path.endswith(".pem"):
|
|
|
|
basename, _ = os.path.splitext(path)
|
2012-07-11 09:09:41 +00:00
|
|
|
basename = os.path.basename(basename)
|
2012-06-27 04:42:00 +00:00
|
|
|
else:
|
2012-07-20 02:45:58 +00:00
|
|
|
basename = os.path.basename(path)
|
2012-06-27 04:42:00 +00:00
|
|
|
|
2013-10-07 20:48:30 +00:00
|
|
|
key, ca = create_ca(o=o, cn=cn, exp=exp)
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
# Dump the CA plus private key
|
2013-06-15 22:28:21 +00:00
|
|
|
f = open(path, "wb")
|
2012-06-27 04:42:00 +00:00
|
|
|
f.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key))
|
|
|
|
f.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, ca))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Dump the certificate in PEM format
|
2013-06-15 22:28:21 +00:00
|
|
|
f = open(os.path.join(dirname, basename + "-cert.pem"), "wb")
|
2012-06-27 04:42:00 +00:00
|
|
|
f.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, ca))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Create a .cer file with the same contents for Android
|
2013-06-15 22:28:21 +00:00
|
|
|
f = open(os.path.join(dirname, basename + "-cert.cer"), "wb")
|
2012-06-27 04:42:00 +00:00
|
|
|
f.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, ca))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Dump the certificate in PKCS12 format for Windows devices
|
2013-06-15 22:28:21 +00:00
|
|
|
f = open(os.path.join(dirname, basename + "-cert.p12"), "wb")
|
2012-06-27 04:42:00 +00:00
|
|
|
p12 = OpenSSL.crypto.PKCS12()
|
|
|
|
p12.set_certificate(ca)
|
|
|
|
p12.set_privatekey(key)
|
|
|
|
f.write(p12.export())
|
|
|
|
f.close()
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2013-08-12 04:03:29 +00:00
|
|
|
def dummy_cert(ca, commonname, sans):
|
2012-06-27 04:42:00 +00:00
|
|
|
"""
|
2013-01-05 12:15:53 +00:00
|
|
|
Generates and writes a certificate to fp.
|
|
|
|
|
2012-06-27 04:42:00 +00:00
|
|
|
ca: Path to the certificate authority file, or None.
|
|
|
|
commonname: Common name for the generated certificate.
|
2013-01-05 12:15:53 +00:00
|
|
|
sans: A list of Subject Alternate Names.
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
Returns cert path if operation succeeded, None if not.
|
|
|
|
"""
|
|
|
|
ss = []
|
|
|
|
for i in sans:
|
|
|
|
ss.append("DNS: %s"%i)
|
|
|
|
ss = ", ".join(ss)
|
|
|
|
|
2013-06-15 22:28:21 +00:00
|
|
|
raw = file(ca, "rb").read()
|
2013-01-05 12:15:53 +00:00
|
|
|
ca = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, raw)
|
|
|
|
key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, raw)
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
cert = OpenSSL.crypto.X509()
|
2014-01-08 01:46:55 +00:00
|
|
|
cert.gmtime_adj_notBefore(-3600*48)
|
2012-06-27 04:42:00 +00:00
|
|
|
cert.gmtime_adj_notAfter(60 * 60 * 24 * 30)
|
|
|
|
cert.set_issuer(ca.get_subject())
|
2013-09-24 19:18:41 +00:00
|
|
|
cert.get_subject().CN = commonname
|
2012-06-27 04:42:00 +00:00
|
|
|
cert.set_serial_number(int(time.time()*10000))
|
|
|
|
if ss:
|
2013-04-19 13:37:14 +00:00
|
|
|
cert.set_version(2)
|
2012-06-27 04:42:00 +00:00
|
|
|
cert.add_extensions([OpenSSL.crypto.X509Extension("subjectAltName", True, ss)])
|
2013-09-24 19:18:41 +00:00
|
|
|
cert.set_pubkey(ca.get_pubkey())
|
2012-06-27 04:42:00 +00:00
|
|
|
cert.sign(key, "sha1")
|
2013-08-12 04:03:29 +00:00
|
|
|
return SSLCert(cert)
|
2012-06-27 04:42:00 +00:00
|
|
|
|
2013-01-05 12:15:53 +00:00
|
|
|
|
|
|
|
class CertStore:
|
|
|
|
"""
|
2013-08-12 04:03:29 +00:00
|
|
|
Implements an in-memory certificate store.
|
2013-01-05 12:15:53 +00:00
|
|
|
"""
|
2014-03-02 00:50:19 +00:00
|
|
|
def __init__(self, cacert):
|
2013-08-12 04:03:29 +00:00
|
|
|
self.certs = {}
|
2014-03-02 00:50:19 +00:00
|
|
|
self.cacert = cacert
|
2013-01-05 12:15:53 +00:00
|
|
|
|
2014-03-02 00:50:19 +00:00
|
|
|
def get_cert(self, commonname, sans):
|
2013-01-05 12:15:53 +00:00
|
|
|
"""
|
2013-08-12 04:03:29 +00:00
|
|
|
Returns an SSLCert object.
|
2013-01-05 12:15:53 +00:00
|
|
|
|
|
|
|
commonname: Common name for the generated certificate. Must be a
|
|
|
|
valid, plain-ASCII, IDNA-encoded domain name.
|
|
|
|
|
|
|
|
sans: A list of Subject Alternate Names.
|
|
|
|
|
2013-01-05 12:34:39 +00:00
|
|
|
Return None if the certificate could not be found or generated.
|
2013-01-05 12:15:53 +00:00
|
|
|
"""
|
2013-08-12 04:03:29 +00:00
|
|
|
if commonname in self.certs:
|
|
|
|
return self.certs[commonname]
|
2014-03-02 00:50:19 +00:00
|
|
|
c = dummy_cert(self.cacert, commonname, sans)
|
2013-08-12 04:03:29 +00:00
|
|
|
self.certs[commonname] = c
|
|
|
|
return c
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _GeneralName(univ.Choice):
|
|
|
|
# We are only interested in dNSNames. We use a default handler to ignore
|
|
|
|
# other types.
|
|
|
|
componentType = namedtype.NamedTypes(
|
|
|
|
namedtype.NamedType('dNSName', char.IA5String().subtype(
|
|
|
|
implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class _GeneralNames(univ.SequenceOf):
|
|
|
|
componentType = _GeneralName()
|
|
|
|
sizeSpec = univ.SequenceOf.sizeSpec + constraint.ValueSizeConstraint(1, 1024)
|
|
|
|
|
|
|
|
|
2014-01-31 00:06:53 +00:00
|
|
|
class SSLCert:
|
2012-06-27 10:11:58 +00:00
|
|
|
def __init__(self, cert):
|
2012-06-27 04:42:00 +00:00
|
|
|
"""
|
|
|
|
Returns a (common name, [subject alternative names]) tuple.
|
|
|
|
"""
|
2012-06-27 10:11:58 +00:00
|
|
|
self.x509 = cert
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_pem(klass, txt):
|
|
|
|
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, txt)
|
|
|
|
return klass(x509)
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_der(klass, der):
|
|
|
|
pem = ssl.DER_cert_to_PEM_cert(der)
|
2012-06-27 10:11:58 +00:00
|
|
|
return klass.from_pem(pem)
|
2012-06-27 04:42:00 +00:00
|
|
|
|
2012-06-28 02:56:21 +00:00
|
|
|
def to_pem(self):
|
|
|
|
return OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, self.x509)
|
|
|
|
|
2012-06-27 04:42:00 +00:00
|
|
|
def digest(self, name):
|
2012-06-27 10:11:58 +00:00
|
|
|
return self.x509.digest(name)
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def issuer(self):
|
2012-06-27 10:11:58 +00:00
|
|
|
return self.x509.get_issuer().get_components()
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def notbefore(self):
|
2012-06-27 10:11:58 +00:00
|
|
|
t = self.x509.get_notBefore()
|
2012-06-27 04:42:00 +00:00
|
|
|
return datetime.datetime.strptime(t, "%Y%m%d%H%M%SZ")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def notafter(self):
|
2012-06-27 10:11:58 +00:00
|
|
|
t = self.x509.get_notAfter()
|
2012-06-27 04:42:00 +00:00
|
|
|
return datetime.datetime.strptime(t, "%Y%m%d%H%M%SZ")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_expired(self):
|
2012-06-27 10:11:58 +00:00
|
|
|
return self.x509.has_expired()
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def subject(self):
|
2012-06-27 10:11:58 +00:00
|
|
|
return self.x509.get_subject().get_components()
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def serial(self):
|
2012-06-27 10:11:58 +00:00
|
|
|
return self.x509.get_serial_number()
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def keyinfo(self):
|
2012-06-27 10:11:58 +00:00
|
|
|
pk = self.x509.get_pubkey()
|
2012-06-27 04:42:00 +00:00
|
|
|
types = {
|
|
|
|
OpenSSL.crypto.TYPE_RSA: "RSA",
|
|
|
|
OpenSSL.crypto.TYPE_DSA: "DSA",
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
types.get(pk.type(), "UNKNOWN"),
|
|
|
|
pk.bits()
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def cn(self):
|
2013-01-20 09:13:38 +00:00
|
|
|
c = None
|
2012-06-27 04:42:00 +00:00
|
|
|
for i in self.subject:
|
|
|
|
if i[0] == "CN":
|
2013-01-20 09:13:38 +00:00
|
|
|
c = i[1]
|
|
|
|
return c
|
2012-06-27 04:42:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def altnames(self):
|
|
|
|
altnames = []
|
2012-06-27 10:11:58 +00:00
|
|
|
for i in range(self.x509.get_extension_count()):
|
|
|
|
ext = self.x509.get_extension(i)
|
2012-06-27 04:42:00 +00:00
|
|
|
if ext.get_short_name() == "subjectAltName":
|
2012-07-24 02:55:54 +00:00
|
|
|
try:
|
|
|
|
dec = decode(ext.get_data(), asn1Spec=_GeneralNames())
|
|
|
|
except PyAsn1Error:
|
|
|
|
continue
|
2012-06-27 04:42:00 +00:00
|
|
|
for i in dec[0]:
|
|
|
|
altnames.append(i[0].asOctets())
|
|
|
|
return altnames
|
|
|
|
|
|
|
|
|
2012-06-27 20:15:55 +00:00
|
|
|
def get_remote_cert(host, port, sni):
|
2014-01-28 16:26:35 +00:00
|
|
|
c = tcp.TCPClient((host, port))
|
2012-06-27 20:15:55 +00:00
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl(sni=sni)
|
|
|
|
return c.cert
|