# 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")
'''
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDGnXh/GMCLpNNI
AIvfBWlPrRCLFWxd2ICLYSUq3/jwh31CppGKlfaSmUYnrMxnT4hg2f6gBqlmq1gK
jQqDkqQQtHsSljRQF58NRFtz99w45jRmrGs+F5zoggJuyv7/lKXy/BXOc40NodIl
qRuo/uhoPjeXaUNziwpRj4rByRdLwQ6MfRcfdZ0TpVJ4J7apJ66pZt85L63u8TZi
AZdBgcowX6giola7kUUMG66bSi2X6sIFxdXHwzrWlFNnSbKqOqKlGrjmBHUYqRFr
gLALKkaMpF30olHn7QLqJ2592hMFVkOQAzNr8Xb15mF27BuWFYd7P6TirsxCEd+9
BFFbhl0vAgMBAAECggEAf1ndN3GBlIi9SL/A7+GiYwpPPz8fWxVFZxmFIXa3QlM+
CAyR6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