6.6 MySQL Enterprise Encryption

Note:

MySQL Enterprise Encryption is an extension included in MySQL Enterprise Edition, a commercial product. To learn more about commercial products, https://www.mysql.com/products/.

MySQL Enterprise Edition includes a set of encryption functions based on the OpenSSL library that expose OpenSSL capabilities at the SQL level. These functions enable Enterprise applications to perform the following operations:

MySQL Enterprise Encryption supports the RSA, DSA, and DH cryptographic algorithms.

MySQL Enterprise Encryption is supplied as a library of loadable functions, from which individual functions can be installed individually.

 File: manual.info.tmp, Node: enterprise-encryption-installation, Next: enterprise-encryption-usage, Prev: enterprise-encryption, Up: enterprise-encryption

6.6.1 MySQL Enterprise Encryption Installation

MySQL Enterprise Encryption functions are located in a loadable function library file installed in the plugin directory (the directory named by the 'plugin_dir' system variable). The function library base name is 'openssl_udf' and the suffix is platform dependent. For example, the file name on Linux or Windows is 'openssl_udf.so' or 'openssl_udf.dll', respectively.

To install functions from the library file, use the *note 'CREATE FUNCTION': create-function-loadable. statement. To load all functions from the library, use this set of statements, adjusting the file name suffix as necessary:

 CREATE FUNCTION asymmetric_decrypt RETURNS STRING
   SONAME 'openssl_udf.so';
 CREATE FUNCTION asymmetric_derive RETURNS STRING
   SONAME 'openssl_udf.so';
 CREATE FUNCTION asymmetric_encrypt RETURNS STRING
   SONAME 'openssl_udf.so';
 CREATE FUNCTION asymmetric_sign RETURNS STRING
   SONAME 'openssl_udf.so';
 CREATE FUNCTION asymmetric_verify RETURNS INTEGER
   SONAME 'openssl_udf.so';
 CREATE FUNCTION create_asymmetric_priv_key RETURNS STRING
   SONAME 'openssl_udf.so';
 CREATE FUNCTION create_asymmetric_pub_key RETURNS STRING
   SONAME 'openssl_udf.so';
 CREATE FUNCTION create_dh_parameters RETURNS STRING
   SONAME 'openssl_udf.so';
 CREATE FUNCTION create_digest RETURNS STRING
   SONAME 'openssl_udf.so';

Once installed, the functions remain installed across server restarts. To unload the functions, use the *note 'DROP FUNCTION': drop-function-loadable. statement:

 DROP FUNCTION asymmetric_decrypt;
 DROP FUNCTION asymmetric_derive;
 DROP FUNCTION asymmetric_encrypt;
 DROP FUNCTION asymmetric_sign;
 DROP FUNCTION asymmetric_verify;
 DROP FUNCTION create_asymmetric_priv_key;
 DROP FUNCTION create_asymmetric_pub_key;
 DROP FUNCTION create_dh_parameters;
 DROP FUNCTION create_digest;

In the note 'CREATE FUNCTION': create-function-loadable. and note 'DROP FUNCTION': drop-function-loadable. statements, the function names must be specified in lowercase. This differs from their use at function invocation time, for which you can use any lettercase.

The note 'CREATE FUNCTION': create-function-loadable. and note 'DROP FUNCTION': drop-function-loadable. statements require the 'INSERT' and 'DROP' privilege, respectively, for the 'mysql' database.

 File: manual.info.tmp, Node: enterprise-encryption-usage, Next: enterprise-encryption-function-reference, Prev: enterprise-encryption-installation, Up: enterprise-encryption

6.6.2 MySQL Enterprise Encryption Usage and Examples

To use MySQL Enterprise Encryption in applications, invoke the functions that are appropriate for the operations you wish to perform. This section demonstrates how to carry out some representative tasks:

Create a private/public key pair using RSA encryption

 -- Encryption algorithm; can be 'DSA' or 'DH' instead
 SET @algo = 'RSA';
 -- Key length in bits; make larger for stronger keys
 SET @key_len = 1024;

 -- Create private key
 SET @priv = create_asymmetric_priv_key(@algo, @key_len);
 -- Derive corresponding public key from private key, using same algorithm
 SET @pub = create_asymmetric_pub_key(@algo, @priv);

Now you can use the key pair to encrypt and decrypt data, sign and verify data, or generate symmetric keys.

Use the private key to encrypt data and the public key to decrypt it

This requires that the members of the key pair be RSA keys.

 SET @ciphertext = asymmetric_encrypt(@algo, 'My secret text', @priv);
 SET @plaintext = asymmetric_decrypt(@algo, @ciphertext, @pub);

Conversely, you can encrypt using the public key and decrypt using the private key.

 SET @ciphertext = asymmetric_encrypt(@algo, 'My secret text', @pub);
 SET @plaintext = asymmetric_decrypt(@algo, @ciphertext, @priv);

In either case, the algorithm specified for the encryption and decryption functions must match that used to generate the keys.

Generate a digest from a string

 -- Digest type; can be 'SHA256', 'SHA384', or 'SHA512' instead
 SET @dig_type = 'SHA224';

 -- Generate digest string
 SET @dig = create_digest(@dig_type, 'My text to digest');

Use the digest with a key pair

The key pair can be used to sign data, then verify that the signature matches the digest.

 -- Encryption algorithm; could be 'DSA' instead; keys must
 -- have been created using same algorithm
 SET @algo = 'RSA';

 -- Generate signature for digest and verify signature against digest
 SET @sig = asymmetric_sign(@algo, @dig, @priv, @dig_type);
 -- Verify signature against digest
 SET @verf = asymmetric_verify(@algo, @dig, @sig, @pub, @dig_type);

Create a symmetric key

This requires DH private/public keys as inputs, created using a shared symmetric secret. Create the secret by passing the key length to 'create_dh_parameters()', then pass the secret as the 'key length' to 'create_asymmetric_priv_key()'.

 -- Generate DH shared symmetric secret
 SET @dhp = create_dh_parameters(1024);
 -- Generate DH key pairs
 SET @algo = 'DH';
 SET @priv1 = create_asymmetric_priv_key(@algo, @dhp);
 SET @pub1 = create_asymmetric_pub_key(@algo, @priv1);
 SET @priv2 = create_asymmetric_priv_key(@algo, @dhp);
 SET @pub2 = create_asymmetric_pub_key(@algo, @priv2);

 -- Generate symmetric key using public key of first party,
 -- private key of second party
 SET @sym1 = asymmetric_derive(@pub1, @priv2);

 -- Or use public key of second party, private key of first party
 SET @sym2 = asymmetric_derive(@pub2, @priv1);

Key string values can be created at runtime and stored into a variable or table using note 'SET': set-variable, note 'SELECT': select, or *note 'INSERT': insert.:

 SET @priv1 = create_asymmetric_priv_key('RSA', 1024);
 SELECT create_asymmetric_priv_key('RSA', 1024) INTO @priv2;
 INSERT INTO t (key_col) VALUES(create_asymmetric_priv_key('RSA', 1024));

Key string values stored in files can be read using the 'LOAD_FILE()' function by users who have the 'FILE' privilege.

Digest and signature strings can be handled similarly.

Limit CPU usage by key-generation operations

The 'create_asymmetric_priv_key()' and 'create_dh_parameters()' encryption functions take a key-length parameter, and the amount of CPU resources required by these functions increases as the key length increases. For some installations, this might result in unacceptable CPU usage if applications frequently generate excessively long keys.

OpenSSL imposes a minimum key length of 1,024 bits for all keys. OpenSSL also imposes a maximum key length of 10,000 bits and 16,384 bits for DSA and RSA keys, respectively, for 'create_asymmetric_priv_key()', and a maximum key length of 10,000 bits for 'create_dh_parameters()'. If those maximum values are too high, three environment variables are available as of MySQL 5.7.17 to enable MySQL server administrators to set lower maximum lengths for key generation, and thereby to limit CPU usage:

To use any of these environment variables, set them in the environment of the process that starts the server. If set, their values take precedence over the maximum key lengths imposed by OpenSSL. For example, to set a maximum key length of 4,096 bits for DSA and RSA keys for 'create_asymmetric_priv_key()', set these variables:

 export MYSQL_OPENSSL_UDF_DSA_BITS_THRESHOLD=4096
 export MYSQL_OPENSSL_UDF_RSA_BITS_THRESHOLD=4096

The example uses Bourne shell syntax. The syntax for other shells may differ.

 File: manual.info.tmp, Node: enterprise-encryption-function-reference, Next: enterprise-encryption-functions, Prev: enterprise-encryption-usage, Up: enterprise-encryption

6.6.3 MySQL Enterprise Encryption Function Reference

MySQL Enterprise Encryption Functions

Name Description

'asymmetric_decrypt()'Decrypt ciphertext using private or public key

'asymmetric_derive()'Derive symmetric key from asymmetric keys

'asymmetric_encrypt()'Encrypt cleartext using private or public key

'asymmetric_sign()' Generate signature from digest

'asymmetric_verify()'Verify that signature matches digest

'create_asymmetric_priv_key()'Create private key

'create_asymmetric_pub_key()'Create public key

'create_dh_parameters()'Generate shared DH secret

'create_digest()' Generate digest from string

 File: manual.info.tmp, Node: enterprise-encryption-functions, Prev: enterprise-encryption-function-reference, Up: enterprise-encryption

6.6.4 MySQL Enterprise Encryption Function Descriptions

MySQL Enterprise Encryption functions have these general characteristics:

Several of the functions take an encryption algorithm argument. The following table summarizes the supported algorithms by function.

Supported Algorithms by Function

Function Supported Algorithms

'asymmetric_decrypt()' RSA

'asymmetric_derive()' DH

'asymmetric_encrypt()' RSA

'asymmetric_sign()' RSA, DSA

'asymmetric_verify()' RSA, DSA

'create_asymmetric_priv_key()' RSA, DSA, DH

'create_asymmetric_pub_key()' RSA, DSA, DH

'create_dh_parameters()' DH

Note:

Although you can create keys using any of the RSA, DSA, or DH encryption algorithms, other functions that take key arguments might accept only certain types of keys. For example, 'asymmetric_encrypt()' and 'asymmetric_decrypt()' accept only RSA keys.

The following descriptions describe the calling sequences for MySQL Enterprise Encryption functions. For additional examples and discussion, see *note enterprise-encryption-usage::.

 File: manual.info.tmp, Node: selinux, Prev: enterprise-encryption, Up: security