def generateKeyPair(self, keyName, params): """ Generate a pair of asymmetric keys. :param Name keyName: The name of the key pair. :param KeyParams params: The parameters of the key. """ if (params.getKeyType() == KeyType.RSA or params.getKeyType() == KeyType.ECDSA): if params.getKeyType() == KeyType.RSA: privateKey = rsa.generate_private_key( public_exponent=65537, key_size=params.getKeySize(), backend=default_backend()) else: privateKey = ec.generate_private_key( PrivateKeyStorage.getEcCurve(params.getKeySize()), default_backend()) self.setPublicKeyForKeyName( keyName, params.getKeyType(), privateKey.public_key().public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo)) self.setPrivateKeyForKeyName( keyName, params.getKeyType(), privateKey.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption())) # TODO generate ECDSA keys else: raise RuntimeError("generateKeyPair: KeyType is not supported")
def generateKeyPair(self, keyName, params): """ Generate a pair of asymmetric keys. :param Name keyName: The name of the key pair. :param KeyParams params: The parameters of the key. """ if (params.getKeyType() == KeyType.RSA or params.getKeyType() == KeyType.ECDSA): if params.getKeyType() == KeyType.RSA: privateKey = rsa.generate_private_key( public_exponent = 65537, key_size = params.getKeySize(), backend = default_backend()) else: privateKey = ec.generate_private_key( PrivateKeyStorage.getEcCurve(params.getKeySize()), default_backend()) self.setPublicKeyForKeyName( keyName, params.getKeyType(), privateKey.public_key().public_bytes( encoding = serialization.Encoding.DER, format = serialization.PublicFormat.SubjectPublicKeyInfo)) self.setPrivateKeyForKeyName( keyName, params.getKeyType(), privateKey.private_bytes( encoding = serialization.Encoding.DER, format = serialization.PrivateFormat.PKCS8, encryption_algorithm = serialization.NoEncryption())) # TODO generate ECDSA keys else: raise RuntimeError("generateKeyPair: KeyType is not supported")
def generateKeyPair(self, keyName, params): """ Generate a pair of asymmetric keys. :param Name keyName: The name of the key pair. :param KeyParams params: The parameters of the key. """ if self.doesKeyExist(keyName, KeyClass.PUBLIC): raise SecurityException("Public key already exists") if self.doesKeyExist(keyName, KeyClass.PRIVATE): raise SecurityException("Private key already exists") publicKeyDer = None privateKeyDer = None if (params.getKeyType() == KeyType.RSA or params.getKeyType() == KeyType.ECDSA): if params.getKeyType() == KeyType.RSA: privateKey = rsa.generate_private_key( public_exponent=65537, key_size=params.getKeySize(), backend=default_backend()) else: privateKey = ec.generate_private_key( PrivateKeyStorage.getEcCurve(params.getKeySize()), default_backend()) publicKeyDer = privateKey.public_key().public_bytes( encoding=serialization.Encoding.DER, format=serialization.PublicFormat.SubjectPublicKeyInfo) privateKeyDer = privateKey.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption()) else: raise SecurityException("Unsupported key type") keyUri = keyName.toUri() keyFilePathNoExtension = self.maintainMapping(keyUri) publicKeyFilePath = keyFilePathNoExtension + ".pub" privateKeyFilePath = keyFilePathNoExtension + ".pri" with open(publicKeyFilePath, 'w') as keyFile: keyFile.write( Blob(base64.b64encode(publicKeyDer), False).toRawStr()) with open(privateKeyFilePath, 'w') as keyFile: keyFile.write( Blob(base64.b64encode(privateKeyDer), False).toRawStr()) os.chmod(publicKeyFilePath, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) os.chmod(privateKeyFilePath, stat.S_IRUSR)
def generateKeyPair(self, keyName, params): """ Generate a pair of asymmetric keys. :param Name keyName: The name of the key pair. :param KeyParams params: The parameters of the key. """ if self.doesKeyExist(keyName, KeyClass.PUBLIC): raise SecurityException("Public key already exists") if self.doesKeyExist(keyName, KeyClass.PRIVATE): raise SecurityException("Private key already exists") publicKeyDer = None privateKeyDer = None if (params.getKeyType() == KeyType.RSA or params.getKeyType() == KeyType.ECDSA): if params.getKeyType() == KeyType.RSA: privateKey = rsa.generate_private_key( public_exponent = 65537, key_size = params.getKeySize(), backend = default_backend()) else: privateKey = ec.generate_private_key( PrivateKeyStorage.getEcCurve(params.getKeySize()), default_backend()) publicKeyDer = privateKey.public_key().public_bytes( encoding = serialization.Encoding.DER, format = serialization.PublicFormat.SubjectPublicKeyInfo) privateKeyDer = privateKey.private_bytes( encoding = serialization.Encoding.DER, format = serialization.PrivateFormat.PKCS8, encryption_algorithm = serialization.NoEncryption()) else: raise SecurityException("Unsupported key type") keyUri = keyName.toUri() keyFilePathNoExtension = self.maintainMapping(keyUri) publicKeyFilePath = keyFilePathNoExtension + ".pub" privateKeyFilePath = keyFilePathNoExtension + ".pri" with open(publicKeyFilePath, 'w') as keyFile: keyFile.write(Blob(base64.b64encode(publicKeyDer), False).toRawStr()) with open(privateKeyFilePath, 'w') as keyFile: keyFile.write(Blob(base64.b64encode(privateKeyDer), False).toRawStr()) os.chmod(publicKeyFilePath, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) os.chmod(privateKeyFilePath, stat.S_IRUSR)