# coding: utf-8

from inspect import getsource
from datetime import datetime

from OpenSSL.crypto import FILETYPE_PEM, TYPE_RSA, X509, PKey, dump_privatekey, dump_certificate

key = PKey()
key.generate_key(TYPE_RSA, 2048)

cert = X509()
issuer = cert.get_issuer()
subject = cert.get_subject()

for dn in [issuer, subject]:
    dn.C = b"TR"
    dn.ST = b"Çorum"
    dn.L = b"Başmakçı"
    dn.CN = b"localhost"
    dn.O = b"Twisted Matrix Labs"
    dn.OU = b"Automated Testing Authority"
    dn.emailAddress = b"security@twistedmatrix.com"

cert.set_serial_number(datetime.now().toordinal())
cert.gmtime_adj_notBefore(0)
cert.gmtime_adj_notAfter(60 * 60 * 24 * 365 * 100)

cert.set_pubkey(key)
cert.sign(key, b"sha1")

import __main__
source = getsource(__main__)
source = source.split(b"\n" + b"-" * 5)[0].rsplit(b"\n", 1)[0]
with open(b"server.pem", "wb") as fObj:
    fObj.write(source)
    fObj.write(b"\n")
    fObj.write(b"'''\n")
    fObj.write(dump_privatekey(FILETYPE_PEM, key))
    fObj.write(dump_certificate(FILETYPE_PEM, cert))
    fObj.write(b"'''\n")
with open(b"key.pem.no_trailing_newline", "wb") as fObj:
    fObj.write(dump_privatekey(FILETYPE_PEM, key).rstrip('\n'))
with open(b"cert.pem.no_trailing_newline", "wb") as fObj:
    fObj.write(dump_certificate(FILETYPE_PEM, cert).rstrip('\n'))
'''
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDwT6kbqtMUI0sM
kx4hI+L780dA59KfksZCqJGmOsMD6hte9EguasfkZzvCF3dk3NhwCjFSOvKx6rCw
iteoWtYkVfo+rSuVNmt7bEsOUDtuTcaxTzIFB+yHOYwAaoz3zQkyVW0c4pzioiLC
GCmfFLdiDBQGGp74tb+7a0V6kC3vMLFoM3L6QWq5uYRB5+xLzlPJ734ltyvfZHL3
Us6pcUbK+3WTWvb4ER0W2RqArAj6Bc/ERQKIAPFEiZi9bIYTwvBH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