def getKeyPair(pubkeyfile, privkeyfile): """ This function looks for RSA keypair files in the current directory. If they do not exist, the keypair is created. """ if not (os.path.exists(pubkeyfile) and os.path.exists(privkeyfile)): # No keypair exists. Generate a new RSA keypair from Crypto.PublicKey import RSA rsa_key = Key(RSA.generate(_KEY_LENGTH)) public_key_string = rsa_key.public().toString(type="OPENSSH") private_key_string = rsa_key.toString(type="OPENSSH") # save keys for the future. with open(privkeyfile, 'wt') as pfile: pfile.write(private_key_string) print("Created SSH private key in '{}'".format(_PRIVATE_KEY_FILE)) with open(pubkeyfile, 'wt') as pfile: pfile.write(public_key_string) print("Created SSH public key in '{}'".format(_PUBLIC_KEY_FILE)) else: with open(pubkeyfile) as pfile: public_key_string = pfile.read() with open(privkeyfile) as pfile: private_key_string = pfile.read() return Key.fromString(public_key_string), Key.fromString( private_key_string)
def getKeyPair(pubkeyfile, privkeyfile): """ This function looks for RSA keypair files in the current directory. If they do not exist, the keypair is created. """ if not (os.path.exists(pubkeyfile) and os.path.exists(privkeyfile)): # No keypair exists. Generate a new RSA keypair print(" Generating SSH RSA keypair ...", end=' ') from Crypto.PublicKey import RSA KEY_LENGTH = 1024 rsaKey = Key(RSA.generate(KEY_LENGTH)) publicKeyString = rsaKey.public().toString(type="OPENSSH") privateKeyString = rsaKey.toString(type="OPENSSH") # save keys for the future. file(pubkeyfile, 'w+b').write(publicKeyString) file(privkeyfile, 'w+b').write(privateKeyString) print(" done.") else: publicKeyString = file(pubkeyfile).read() privateKeyString = file(privkeyfile).read() return Key.fromString(publicKeyString), Key.fromString(privateKeyString)
def getKeyPair(pubkeyfile, privkeyfile): """ This function looks for RSA keypair files in the current directory. If they do not exist, the keypair is created. """ if not (os.path.exists(pubkeyfile) and os.path.exists(privkeyfile)): # No keypair exists. Generate a new RSA keypair from Crypto.PublicKey import RSA rsa_key = Key(RSA.generate(_KEY_LENGTH)) public_key_string = rsa_key.public().toString(type="OPENSSH") private_key_string = rsa_key.toString(type="OPENSSH") # save keys for the future. with open(privkeyfile, 'wt') as pfile: pfile.write(private_key_string) print("Created SSH private key in '{}'".format(_PRIVATE_KEY_FILE)) with open(pubkeyfile, 'wt') as pfile: pfile.write(public_key_string) print("Created SSH public key in '{}'".format(_PUBLIC_KEY_FILE)) else: with open(pubkeyfile) as pfile: public_key_string = pfile.read() with open(privkeyfile) as pfile: private_key_string = pfile.read() return Key.fromString(public_key_string), Key.fromString(private_key_string)
def getKeyPair(pubkeyfile, privkeyfile): """ This function looks for RSA keypair files in the current directory. If they do not exist, the keypair is created. """ if not (os.path.exists(pubkeyfile) and os.path.exists(privkeyfile)): # No keypair exists. Generate a new RSA keypair from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.asymmetric import rsa rsa_key = Key( rsa.generate_private_key(public_exponent=65537, key_size=_KEY_LENGTH, backend=default_backend())) public_key_string = rsa_key.public().toString(type="OPENSSH").decode() private_key_string = rsa_key.toString(type="OPENSSH").decode() # save keys for the future. with open(privkeyfile, "wt") as pfile: pfile.write(private_key_string) print("Created SSH private key in '{}'".format(_PRIVATE_KEY_FILE)) with open(pubkeyfile, "wt") as pfile: pfile.write(public_key_string) print("Created SSH public key in '{}'".format(_PUBLIC_KEY_FILE)) else: with open(pubkeyfile) as pfile: public_key_string = pfile.read() with open(privkeyfile) as pfile: private_key_string = pfile.read() return Key.fromString(public_key_string), Key.fromString( private_key_string)
def verify_SSL_key_and_cert(keyfile, certfile): """ This function looks for RSA key and certificate in the current directory. If files ssl.key and ssl.cert does not exist, they are created. """ if not (os.path.exists(keyfile) and os.path.exists(certfile)): # key/cert does not exist. Create. import subprocess from Crypto.PublicKey import RSA from twisted.conch.ssh.keys import Key print(" Creating SSL key and certificate ... ", end=' ') try: # create the RSA key and store it. KEY_LENGTH = 1024 rsaKey = Key(RSA.generate(KEY_LENGTH)) keyString = rsaKey.toString(type="OPENSSH") file(keyfile, 'w+b').write(keyString) except Exception as err: print(NO_AUTOGEN.format(err=err, keyfile=keyfile)) sys.exit(5) # try to create the certificate CERT_EXPIRE = 365 * 20 # twenty years validity # default: # openssl req -new -x509 -key ssl.key -out ssl.cert -days 7300 exestring = "openssl req -new -x509 -key %s -out %s -days %s" % (keyfile, certfile, CERT_EXPIRE) try: subprocess.call(exestring) except OSError as err: raise OSError(NO_AUTOCERT.format(err=err, certfile=certfile, keyfile=keyfile, exestring=exestring)) print("done.")
def verify_SSL_key_and_cert(keyfile, certfile): """ This function looks for RSA key and certificate in the current directory. If files ssl.key and ssl.cert does not exist, they are created. """ if not (os.path.exists(keyfile) and os.path.exists(certfile)): # key/cert does not exist. Create. import subprocess from Crypto.PublicKey import RSA from twisted.conch.ssh.keys import Key print(" Creating SSL key and certificate ... ", end=' ') try: # create the RSA key and store it. KEY_LENGTH = 1024 rsaKey = Key(RSA.generate(KEY_LENGTH)) keyString = rsaKey.toString(type="OPENSSH") file(keyfile, 'w+b').write(keyString) except Exception as e: print( "rsaKey error: %(e)s\n WARNING: Evennia could not auto-generate SSL private key." % {'e': e}) print( "If this error persists, create game/%(keyfile)s yourself using third-party tools." % {'keyfile': keyfile}) sys.exit(5) # try to create the certificate CERT_EXPIRE = 365 * 20 # twenty years validity # default: #openssl req -new -x509 -key ssl.key -out ssl.cert -days 7300 exestring = "openssl req -new -x509 -key %s -out %s -days %s" % ( keyfile, certfile, CERT_EXPIRE) try: #, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) subprocess.call(exestring) except OSError as e: string = "\n".join([ " %s\n" % e, " Evennia's SSL context factory could not automatically", " create an SSL certificate game/%(cert)s." % { 'cert': certfile }, " A private key 'ssl.key' was already created. Please", " create %(cert)s manually using the commands valid" % { 'cert': certfile }, " for your operating system.", " Example (linux, using the openssl program): ", " %s" % exestring ]) print(string) sys.exit(5) print("done.")
def verify_SSL_key_and_cert(keyfile, certfile): """ This function looks for RSA key and certificate in the current directory. If files ssl.key and ssl.cert does not exist, they are created. """ if not (os.path.exists(keyfile) and os.path.exists(certfile)): # key/cert does not exist. Create. import subprocess from Crypto.PublicKey import RSA from twisted.conch.ssh.keys import Key print " Creating SSL key and certificate ... ", try: # create the RSA key and store it. KEY_LENGTH = 1024 rsaKey = Key(RSA.generate(KEY_LENGTH)) keyString = rsaKey.toString(type="OPENSSH") file(keyfile, 'w+b').write(keyString) except Exception, e: print "rsaKey error: %(e)s\n WARNING: Evennia could not auto-generate SSL private key." % {'e': e} print "If this error persists, create game/%(keyfile)s yourself using third-party tools." % {'keyfile': keyfile} sys.exit(5) # try to create the certificate CERT_EXPIRE = 365 * 20 # twenty years validity # default: #openssl req -new -x509 -key ssl.key -out ssl.cert -days 7300 exestring = "openssl req -new -x509 -key %s -out %s -days %s" % (keyfile, certfile, CERT_EXPIRE) #print "exestring:", exestring try: #, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) subprocess.call(exestring) except OSError, e: string = "\n".join([ " %s\n" % e, " Evennia's SSL context factory could not automatically", " create an SSL certificate game/%(cert)s." % {'cert': certfile}, " A private key 'ssl.key' was already created. Please", " create %(cert)s manually using the commands valid" % {'cert': certfile}, " for your operating system.", " Example (linux, using the openssl program): ", " %s" % exestring]) print string sys.exit(5)
def verify_SSL_key_and_cert(keyfile, certfile): """ This function looks for RSA key and certificate in the current directory. If files ssl.key and ssl.cert does not exist, they are created. """ if not (os.path.exists(keyfile) and os.path.exists(certfile)): # key/cert does not exist. Create. import subprocess from Crypto.PublicKey import RSA from twisted.conch.ssh.keys import Key print(" Creating SSL key and certificate ... ", end=" ") try: # create the RSA key and store it. KEY_LENGTH = 1024 rsaKey = Key(RSA.generate(KEY_LENGTH)) keyString = rsaKey.toString(type="OPENSSH") file(keyfile, "w+b").write(keyString) except Exception as err: print(NO_AUTOGEN.format(err=err, keyfile=keyfile)) sys.exit(5) # try to create the certificate CERT_EXPIRE = 365 * 20 # twenty years validity # default: # openssl req -new -x509 -key ssl.key -out ssl.cert -days 7300 exestring = "openssl req -new -x509 -key %s -out %s -days %s" % ( keyfile, certfile, CERT_EXPIRE, ) try: subprocess.call(exestring) except OSError as err: raise OSError( NO_AUTOCERT.format(err=err, certfile=certfile, keyfile=keyfile, exestring=exestring)) print("done.")