'Can create certificate requests, to be signed later by another
certificate with the method
create_cert_signed_certificate. If request is True.
Can also create self signed root certificates if request is False.
This is default behaviour.
:param cert_info:         Contains information about the certificate.
Is a dictionary that must contain the keys:
cn                = Common name. This part
must match the host being authenticated
country_code      = Two letter description
of the country.
state             = State
city              = City
organization      = Organization, can be a
company name.
organization_unit = A unit at the
organization, can be a department.
Example:
cert_info_ca = {
"cn": "company.com",
"country_code": "se",
"state": "AC",
"city": "Dorotea",
"organization":
"Company",
"organization_unit":
"Sales"
:param request:           True if this is a request for certificate,
that should be signed.
False if this is a self signed certificate,
root certificate.
:param valid_from:        When the certificate starts to be valid.
Amount of seconds from when the
certificate is generated.
:param valid_to:          How long the certificate will be valid from
when it is generated.
The value is in seconds. Default is
315360000 seconds, a.k.a 10 years.
:param sn:                Serial number for the certificate. Default
is 1.
:param key_length:        Length of the key to be generated. Defaults
to 1024.
:param hash_alg:          Hash algorithm to use for the key. Default
is sha256.
:param write_to_file:     True if you want to write the certificate
to a file. The method will then return
a tuple with path to certificate file and
path to key file.
False if you want to get the result as
strings. The method will then return a tuple
with the certificate string and the key as
string.
WILL OVERWRITE ALL EXISTING FILES WITHOUT
ASKING!
:param cert_dir:          Where to save the files if write_to_file is
true.
:param cipher_passphrase  A dictionary with cipher and passphrase.
Example::
{"cipher": "blowfish", "passphrase": "qwerty"}
:return:                  string representation of certificate,
string representation of private key
if write_to_file parameter is False otherwise
path to certificate file, path to private
key file'

def create_certificate(self, cert_info, request=False, valid_from=0, valid_to=315360000, sn=1, key_length=1024, hash_alg='sha256', write_to_file=False, cert_dir='', cipher_passphrase=None):

    cn = cert_info['cn']
    c_f = None
    k_f = None
    if write_to_file:
        cert_file = ('%s.crt' % cn)
        key_file = ('%s.key' % cn)
        try:
            remove(cert_file)
        except:
            pass
        try:
            remove(key_file)
        except:
            pass
        c_f = join(cert_dir, cert_file)
        k_f = join(cert_dir, key_file)
    k = crypto.PKey()
    k.generate_key(crypto.TYPE_RSA, key_length)
    cert = crypto.X509()
    if request:
        cert = crypto.X509Req()
    if (len(cert_info['country_code']) != 2):
        raise WrongInput('Country    code    must    be    two    letters!')
    cert.get_subject().C = cert_info['country_code']
    cert.get_subject().ST = cert_info['state']
    cert.get_subject().L = cert_info['city']
    cert.get_subject().O = cert_info['organization']
    cert.get_subject().OU = cert_info['organization_unit']
    cert.get_subject().CN = cn
    if (not request):
        cert.set_serial_number(sn)
        cert.gmtime_adj_notBefore(valid_from)
        cert.gmtime_adj_notAfter(valid_to)
        cert.set_issuer(cert.get_subject())
    cert.set_pubkey(k)
    cert.sign(k, hash_alg)
    filesCreated = False
    try:
        if request:
            tmp_cert = crypto.dump_certificate_request(crypto.FILETYPE_PEM, cert)
        else:
            tmp_cert = crypto.dump_certificate(crypto.FILETYPE_PEM, cert)
        tmp_key = None
        if (cipher_passphrase is not None):
            tmp_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, k, cipher_passphrase['cipher'], cipher_passphrase['passphrase'])
        else:
            tmp_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, k)
        if write_to_file:
            fc = open(c_f, 'wt')
            fk = open(k_f, 'wt')
            if request:
                fc.write(tmp_cert)
            else:
                fc.write(tmp_cert)
            fk.write(tmp_key)
            filesCreated = True
            try:
                fc.close()
            except:
                pass
            try:
                fk.close()
            except:
                pass
            return (c_f, k_f)
        return (tmp_cert, tmp_key)
    except Exception as ex:
        raise CertificateError('Certificate    cannot    be    generated.', ex)
