Rechercher dans le manuel MySQL

6.4.4.8 General-Purpose Keyring Key-Management Functions

MySQL Server supports a keyring service that enables internal server components and plugins to securely store sensitive information for later retrieval.

MySQL Server also includes an SQL interface for keyring key management, implemented as a set of general-purpose user-defined functions (UDFs) that access the functions provided by the internal keyring service. The keyring UDFs are contained in a plugin library file, which also contains a keyring_udf plugin that must be enabled prior to UDF invocation. For these UDFs to be used, a keyring plugin such as keyring_file or keyring_okv must be enabled.

The UDFs described here are general purpose and intended for use with any keyring plugin. A given keyring plugin might have UDFs of its own that are intended for use only with that plugin; see Section 6.4.4.9, “Plugin-Specific Keyring Key-Management Functions”.

The following sections provide installation instructions for the keyring UDFs and demonstrate how to use them. For information about the keyring service functions invoked by the UDFs, see Section 29.3.2, “The Keyring Service”. For general keyring information, see Section 6.4.4, “The MySQL Keyring”.

Installing or Uninstalling General-Purpose Keyring Functions

This section describes how to install or uninstall the keyring user-defined functions (UDFs), which are implemented in a plugin library file that also contains a keyring_udf plugin. For general information about installing or uninstalling plugins and UDFs, see Section 5.6.1, “Installing and Uninstalling Plugins”, and Section 5.7.1, “Installing and Uninstalling User-Defined Functions”.

The keyring UDFs enable keyring key management operations, but the keyring_udf plugin must also be installed because the UDFs will not work correctly without it. Attempts to use the UDFs without the keyring_udf plugin result in an error.

To be usable by the server, the plugin library file must be located in the MySQL plugin directory (the directory named by the plugin_dir system variable). If necessary, configure the plugin directory location by setting the value of plugin_dir at server startup.

The plugin library file base name is keyring_udf. The file name suffix differs per platform (for example, .so for Unix and Unix-like systems, .dll for Windows).

To install the keyring_udf plugin and the UDFs, use the INSTALL PLUGIN and CREATE FUNCTION statements (adjust the .so suffix for your platform as necessary):

  1. INSTALL PLUGIN keyring_udf SONAME 'keyring_udf.so';
  2. CREATE FUNCTION keyring_key_generate RETURNS INTEGER
  3.   SONAME 'keyring_udf.so';
  4. CREATE FUNCTION keyring_key_fetch RETURNS STRING
  5.   SONAME 'keyring_udf.so';
  6. CREATE FUNCTION keyring_key_length_fetch RETURNS INTEGER
  7.   SONAME 'keyring_udf.so';
  8. CREATE FUNCTION keyring_key_type_fetch RETURNS STRING
  9.   SONAME 'keyring_udf.so';
  10. CREATE FUNCTION keyring_key_store RETURNS INTEGER
  11.   SONAME 'keyring_udf.so';
  12. CREATE FUNCTION keyring_key_remove RETURNS INTEGER
  13.   SONAME 'keyring_udf.so';

If the plugin and UDFs are used on a master replication server, install them on all slave servers as well to avoid replication problems.

Once installed as just described, the plugin and UDFs remain installed until uninstalled. To remove them, use the UNINSTALL PLUGIN and DROP FUNCTION statements:

  1. UNINSTALL PLUGIN keyring_udf;
  2. DROP FUNCTION keyring_key_generate;
  3. DROP FUNCTION keyring_key_fetch;
  4. DROP FUNCTION keyring_key_length_fetch;
  5. DROP FUNCTION keyring_key_type_fetch;
  6. DROP FUNCTION keyring_key_store;
  7. DROP FUNCTION keyring_key_remove;

Table des matières Haut

Using General-Purpose Keyring Functions

Before using the keyring user-defined functions (UDFs), install them according to the instructions provided in Installing or Uninstalling General-Purpose Keyring Functions.

The keyring UDFs are subject to these constraints:

  • To use any keyring UDF, the keyring_udf plugin must be enabled. Otherwise, an error occurs:

    ERROR 1123 (HY000): Can't initialize function 'keyring_key_generate';
    This function requires keyring_udf plugin which is not installed.
    Please install

    To install the keyring_udf plugin, see Installing or Uninstalling General-Purpose Keyring Functions.

  • The keyring UDFs invoke keyring service functions (see Section 29.3.2, “The Keyring Service”). The service functions in turn use whatever keyring plugin is installed (for example, keyring_file or keyring_okv). Therefore, to use any keyring UDF, some underlying keyring plugin must be enabled. Otherwise, an error occurs:

    ERROR 3188 (HY000): Function 'keyring_key_generate' failed because
    underlying keyring service returned an error. Please check if a
    keyring plugin is installed and that provided arguments are valid
    for the keyring you are using.

    To install a keyring plugin, see Section 6.4.4.1, “Keyring Plugin Installation”.

  • To use any keyring UDF, a user must possess the global EXECUTE privilege. Otherwise, an error occurs:

    ERROR 1123 (HY000): Can't initialize function 'keyring_key_generate';
    The user is not privileged to execute this function. User needs to
    have EXECUTE

    To grant the global EXECUTE privilege to a user, use this statement:

    1. GRANT EXECUTE ON *.* TO user;

    Alternatively, should you prefer to avoid granting the global EXECUTE privilege while still permitting users to access specific key-management operations, wrapper stored programs can be defined (a technique described later in this section).

  • A key stored in the keyring by a given user can be manipulated later only by the same user. That is, the value of the CURRENT_USER() function at the time of key manipulation must have the same value as when the key was stored in the keyring. (This constraint rules out the use of the keyring UDFs for manipulation of instance-wide keys, such as those created by InnoDB to support tablespace encryption.)

    To enable multiple users to perform operations on the same key, wrapper stored programs can be defined (a technique described later in this section).

  • Keyring UDFs support the key types and lengths supported by the underlying keyring plugin, with the additional constraint that keys cannot be longer than 2,048 bytes (16,384 bits), due to limitations of the UDF interface. See Section 6.4.4.7, “Supported Keyring Key Types”.

To create a new random key and store it in the keyring, call keyring_key_generate(), passing to it an ID for the key, along with the key type (encryption method) and its length in bytes. The following call creates a 2,048-bit DSA-encrypted key named MyKey:

  1. mysql> SELECT keyring_key_generate('MyKey', 'DSA', 256);
  2. +-------------------------------------------+
  3. | keyring_key_generate('MyKey', 'DSA', 256) |
  4. +-------------------------------------------+
  5. |                                         1 |
  6. +-------------------------------------------+

A return value of 1 indicates success. If the key cannot be created, the return value is NULL and an error occurs. One reason this might be is that the underlying keyring plugin does not support the specified combination of key type and key length; see Section 6.4.4.7, “Supported Keyring Key Types”.

To be able to check the return type regardless of whether an error occurs, use SELECT ... INTO @var_name and test the variable value:

  1. mysql> SELECT keyring_key_generate('', '', -1) INTO @x;
  2. ERROR 3188 (HY000): Function 'keyring_key_generate' failed because
  3. underlying keyring service returned an error. Please check if a
  4. keyring plugin is installed and that provided arguments are valid
  5. for the keyring you are using.
  6. mysql> SELECT @x;
  7. +------+
  8. | @x   |
  9. +------+
  10. | NULL |
  11. +------+
  12. mysql> SELECT keyring_key_generate('x', 'AES', 16) INTO @x;
  13. mysql> SELECT @x;
  14. +------+
  15. | @x   |
  16. +------+
  17. |    1 |
  18. +------+

This technique also applies to other keyring UDFs that for failure return a value and an error.

The ID passed to keyring_key_generate() provides a means by which to refer to the key in subsequent UDF calls. For example, use the key ID to retrieve its type as a string or its length in bytes as an integer:

  1. mysql> SELECT keyring_key_type_fetch('MyKey');
  2. +---------------------------------+
  3. | keyring_key_type_fetch('MyKey') |
  4. +---------------------------------+
  5. | DSA                             |
  6. +---------------------------------+
  7. mysql> SELECT keyring_key_length_fetch('MyKey');
  8. +-----------------------------------+
  9. | keyring_key_length_fetch('MyKey') |
  10. +-----------------------------------+
  11. |                               256 |
  12. +-----------------------------------+

To retrieve a key value, pass the key ID to keyring_key_fetch(). The following example uses HEX() to display the key value because it may contain nonprintable characters. The example also uses a short key for brevity, but be aware that longer keys provide better security:

  1. mysql> SELECT keyring_key_generate('MyShortKey', 'DSA', 8);
  2. +----------------------------------------------+
  3. | keyring_key_generate('MyShortKey', 'DSA', 8) |
  4. +----------------------------------------------+
  5. |                                            1 |
  6. +----------------------------------------------+
  7. mysql> SELECT HEX(keyring_key_fetch('MyShortKey'));
  8. +--------------------------------------+
  9. | HEX(keyring_key_fetch('MyShortKey')) |
  10. +--------------------------------------+
  11. | 1DB3B0FC3328A24C                     |
  12. +--------------------------------------+

Keyring UDFs treat key IDs, types, and values as binary strings, so comparisons are case-sensitive. For example, IDs of MyKey and mykey refer to different keys.

To remove a key, pass the key ID to keyring_key_remove():

  1. mysql> SELECT keyring_key_remove('MyKey');
  2. +-----------------------------+
  3. | keyring_key_remove('MyKey') |
  4. +-----------------------------+
  5. |                           1 |
  6. +-----------------------------+

To obfuscate and store a key that you provide, pass the key ID, type, and value to keyring_key_store():

  1. mysql> SELECT keyring_key_store('AES_key', 'AES', 'Secret string');
  2. +------------------------------------------------------+
  3. | keyring_key_store('AES_key', 'AES', 'Secret string') |
  4. +------------------------------------------------------+
  5. |                                                    1 |
  6. +------------------------------------------------------+

As indicated previously, a user must have the global EXECUTE privilege to call keyring UDFs, and the user who stores a key in the keyring initially must be the same user who performs subsequent operations on the key later, as determined from the CURRENT_USER() value in effect for each UDF call. To permit key operations to users who do not have the global EXECUTE privilege or who may not be the key owner, use this technique:

  1. Define wrapper stored programs that encapsulate the required key operations and have a DEFINER value equal to the key owner.

  2. Grant the EXECUTE privilege for specific stored programs to the individual users who should be able to invoke them.

  3. If the operations implemented by the wrapper stored programs do not include key creation, create any necessary keys in advance, using the account named as the DEFINER in the stored program definitions.

This technique enables keys to be shared among users and provides to DBAs more fine-grained control over who can do what with keys, without having to grant global privileges.

The following example shows how to set up a shared key named SharedKey that is owned by the DBA, and a get_shared_key() stored function that provides access to the current key value. The value can be retrieved by any user with the EXECUTE privilege for that function, which is created in the key_schema schema.

From a MySQL administrative account ('root'@'localhost' in this example), create the administrative schema and the stored function to access the key:

  1. mysql> CREATE SCHEMA key_schema;
  2.  
  3. mysql> CREATE DEFINER = 'root'@'localhost'
  4.        FUNCTION key_schema.get_shared_key()
  5.        RETURN keyring_key_fetch('SharedKey');

From the administrative account, ensure that the shared key exists:

  1. mysql> SELECT keyring_key_generate('SharedKey', 'DSA', 8);
  2. +---------------------------------------------+
  3. | keyring_key_generate('SharedKey', 'DSA', 8) |
  4. +---------------------------------------------+
  5. |                                           1 |
  6. +---------------------------------------------+

From the administrative account, create an ordinary user account to which key access is to be granted:

  1. mysql> CREATE USER 'key_user'@'localhost'
  2.        IDENTIFIED BY 'key_user_pwd';

From the key_user account, verify that, without the proper EXECUTE privilege, the new account cannot access the shared key:

  1. mysql> SELECT HEX(key_schema.get_shared_key());
  2. ERROR 1370 (42000): execute command denied to user 'key_user'@'localhost'
  3. for routine 'key_schema.get_shared_key'

From the administrative account, grant EXECUTE to key_user for the stored function:

  1. mysql> GRANT EXECUTE ON FUNCTION key_schema.get_shared_key
  2.        TO 'key_user'@'localhost';

From the key_user account, verify that the key is now accessible:

  1. mysql> SELECT HEX(key_schema.get_shared_key());
  2. +----------------------------------+
  3. | HEX(key_schema.get_shared_key()) |
  4. +----------------------------------+
  5. | 9BAFB9E75CEEB013                 |
  6. +----------------------------------+

Table des matières Haut

General-Purpose Keyring Function Reference

For each general-purpose keyring user-defined function (UDF), this section describes its purpose, calling sequence, and return value. For information about the conditions under which these UDFs can be invoked, see Using General-Purpose Keyring Functions.

  • keyring_key_fetch(key_id)

    Given a key ID, deobfuscates and returns the key value.

    Arguments:

    • key_id: A string that specifies the key ID.

    Return value:

    Returns the key value as a string for success, NULL if the key does not exist, or NULL and an error for failure.

    Note

    Keyring values retrieved using keyring_key_fetch() are limited to 2,048 bytes, due to limitations of the UDF interface. A keyring value longer than that length can be stored using a keyring service function (see Section 29.3.2, “The Keyring Service”), but if retrieved using keyring_key_fetch(), is truncated to 2,048 bytes.

    Example:

    1. mysql> SELECT keyring_key_generate('RSA_key', 'RSA', 16);
    2. +--------------------------------------------+
    3. | keyring_key_generate('RSA_key', 'RSA', 16) |
    4. +--------------------------------------------+
    5. |                                          1 |
    6. +--------------------------------------------+
    7. mysql> SELECT HEX(keyring_key_fetch('RSA_key'));
    8. +-----------------------------------+
    9. | HEX(keyring_key_fetch('RSA_key')) |
    10. +-----------------------------------+
    11. | 91C2253B696064D3556984B6630F891A  |
    12. +-----------------------------------+
    13. mysql> SELECT keyring_key_type_fetch('RSA_key');
    14. +-----------------------------------+
    15. | keyring_key_type_fetch('RSA_key') |
    16. +-----------------------------------+
    17. | RSA                               |
    18. +-----------------------------------+
    19. mysql> SELECT keyring_key_length_fetch('RSA_key');
    20. +-------------------------------------+
    21. | keyring_key_length_fetch('RSA_key') |
    22. +-------------------------------------+
    23. |                                  16 |
    24. +-------------------------------------+

    The example uses HEX() to display the key value because it may contain nonprintable characters. The example also uses a short key for brevity, but be aware that longer keys provide better security.

  • keyring_key_generate(key_id, key_type, key_length)

    Generates a new random key with a given ID, type, and length, and stores it in the keyring. The type and length values must be consistent with the values supported by the underlying keyring plugin, with the additional constraint that keys cannot be longer than 2,048 bytes (16,384 bits), due to limitations of the UDF interface. For the permitted types per plugin, see Section 29.3.2, “The Keyring Service”.

    Arguments:

    • key_id: A string that specifies the key ID.

    • key_type: A string that specifies the key type.

    • key_length: An integer that specifies the key length in bytes. The maximum length is 2,048.

    Return value:

    Returns 1 for success, or NULL and an error for failure.

    Example:

    1. mysql> SELECT keyring_key_generate('RSA_key', 'RSA', 384);
    2. +---------------------------------------------+
    3. | keyring_key_generate('RSA_key', 'RSA', 384) |
    4. +---------------------------------------------+
    5. |                                           1 |
    6. +---------------------------------------------+
  • keyring_key_length_fetch(key_id)

    Given a key ID, returns the key length.

    Arguments:

    • key_id: A string that specifies the key ID.

    Return value:

    Returns the key length in bytes as an integer for success, NULL if the key does not exist, or NULL and an error for failure.

    Example:

    See the description of keyring_key_fetch().

  • keyring_key_remove(key_id)

    Removes the key with a given ID from the keyring.

    Arguments:

    • key_id: A string that specifies the key ID.

    Return value:

    Returns 1 for success, or NULL for failure.

    Example:

    1. mysql> SELECT keyring_key_remove('AES_key');
    2. +-------------------------------+
    3. | keyring_key_remove('AES_key') |
    4. +-------------------------------+
    5. |                             1 |
    6. +-------------------------------+
  • keyring_key_store(key_id, key_type, key)

    Obfuscates and stores a key in the keyring.

    Arguments:

    • key_id: A string that specifies the key ID.

    • key_type: A string that specifies the key type.

    • key: A string that specifies the key value.

    Return value:

    Returns 1 for success, or NULL and an error for failure.

    Example:

    1. mysql> SELECT keyring_key_store('new key', 'DSA', 'My key value');
    2. +-----------------------------------------------------+
    3. | keyring_key_store('new key', 'DSA', 'My key value') |
    4. +-----------------------------------------------------+
    5. |                                                   1 |
    6. +-----------------------------------------------------+
  • keyring_key_type_fetch(key_id)

    Given a key ID, returns the key type.

    Arguments:

    • key_id: A string that specifies the key ID.

    Return value:

    Returns the key type as a string for success, NULL if the key does not exist, or NULL and an error for failure.

    Example:

    See the description of keyring_key_fetch().


Rechercher dans le manuel MySQL

Traduction non disponible

Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.

Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-keyring-udfs-general-purpose.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Références

  1. Consulter le document html Langue du document :en Manuel MySQL : https://dev.mysql.com/

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut