This guide describes how to develop a custom cryptographic plugin for use by Barbican.
Barbican supports two storage modes for secrets: a cryptographic mode (detailed on this page), and a secret store mode. The cryptographic mode stores encrypted secrets in Barbican’s data store, utilizing a cryptographic process or appliance (such as a hardware security module (HSM)) to perform the encryption/decryption. Barbican includes a PKCS11-based interface to SafeNet HSMs.
Note that cryptographic plugins are not invoked directly from Barbican core, but rather via a secret store mode plugin adapter class, further described in The Cryptographic Plugin Adapter.
The barbican.plugin.crypto module contains the classes needed to implement a custom plugin. These classes include the CryptoPluginBase abstract base class which custom plugins should inherit from, as well as several Data Transfer Object (DTO) classes used to transfer data between Barbican and the plugin.
The DTO classes are used to wrap data that is passed from Barbican to the plugin as well as data that is returned from the plugin back to Barbican. They provide a level of isolation between the plugins and Barbican’s internal data models.
Key Encryption Key Meta DTO
Key Encryption Keys (KEKs) in Barbican are intended to represent a distinct key that is used to perform encryption on secrets for a particular project.
KEKMetaDTO objects are provided to cryptographic backends by Barbican to allow plugins to persist metadata related to the project’s KEK.
For example, a plugin that interfaces with a Hardware Security Module (HSM) may want to use a different encryption key for each project. Such a plugin could use the KEKMetaDTO object to save the key ID used for that project. Barbican will persist the KEK metadata and ensure that it is provided to the plugin every time a request from that same project is processed.
String attribute used by Barbican to identify the plugin that is bound to the KEK metadata. Plugins should not change this attribute.
String attribute used to label the project’s KEK by the plugin. The value of this attribute should be meaningful to the plugin. Barbican does not use this value.
String attribute used to identify the encryption algorithm used by the plugin. e.g. “AES”, “3DES”, etc. This value should be meaningful to the plugin. Barbican does not use this value.
String attribute used to identify the algorithm mode used by the plugin. e.g. “CBC”, “GCM”, etc. This value should be meaningful to the plugin. Barbican does not use this value.
Integer attribute used to identify the bit length of the KEK by the plugin. This value should be meaningful to the plugin. Barbican does not use this value.
String attribute used to persist any additional metadata that does not fit in any other attribute. The value of this attribute is defined by the plugin. It could be used to store external system references, such as Key IDs in an HSM, URIs to an external service, or any other data that the plugin deems necessary to persist. Because this is just a plain text field, a plug in may even choose to persist data such as key value pairs in a JSON object.
Secret Encryption DTO
Data Transfer Object used to pass all the necessary data for the plugin to perform encryption of a secret.
Currently, this DTO only contains the raw bytes to be encrypted by the plugin, but in the future this may contain more information.
The secret data in Bytes to be encrypted by the plugin.
Secret Decryption DTO
Data Transfer Object used to pass all the necessary data for the plugin to perform decryption of a secret.
Currently, this DTO only contains the data produced by the plugin during encryption, but in the future this DTO will contain more information, such as a transport key for secret wrapping back to the client.
The data that was produced by the plugin during encryption. For some plugins this will be the actual bytes that need to be decrypted to produce the secret. In other implementations, this may just be a reference to some external system that can produce the unencrypted secret.
Secret Generation DTO
Data Transfer Object used to pass all the necessary data for the plugin to generate a secret on behalf of the user.
String attribute used to identify the type of secret that should be generated. This will be either "symmetric" or "asymmetric".
String attribute used to specify what type of algorithm the secret will be used for. e.g. "AES" for a "symmetric" type, or "RSA" for "asymmetric".
String attribute used to specify what algorithm mode the secret will be used for. e.g. "CBC" for "AES" algorithm.
Integer attribute used to specify the bit length of the secret. For example, this attribute could specify the key length for an encryption key to be used in AES-CBC.
Secret Generation DTO
Data Transfer Object used to pass all the necessary data for the plugin to generate a secret on behalf of the user.
String attribute used to identify the type of secret that should be generated. This will be either "symmetric" or "asymmetric".
String attribute used to specify what type of algorithm the secret will be used for. e.g. "AES" for a "symmetric" type, or "RSA" for "asymmetric".
String attribute used to specify what algorithm mode the secret will be used for. e.g. "CBC" for "AES" algorithm.
Integer attribute used to specify the bit length of the secret. For example, this attribute could specify the key length for an encryption key to be used in AES-CBC.
Barbican cryptographic plugins should implement the abstract base class CryptoPluginBase. Concrete implementations of this class should be exposed to barbican using stevedore mechanisms explained in the configuration portion of this guide.
Base class for all Crypto plugins.
Barbican requests operations by invoking the methods on an instance of the implementing class. Barbican’s plugin manager handles the life-cycle of the Data Transfer Objects (DTOs) that are passed into these methods, and persist the data that is assigned to these DTOs by the plugin.
Key Encryption Key Metadata binding function
Bind a key encryption key (KEK) metadata to the sub-system handling encryption/decryption, updating information about the key encryption key (KEK) metadata in the supplied ‘kek_metadata’ data-transfer-object instance, and then returning this instance.
This method is invoked prior to the encrypt() method above. Implementors should fill out the supplied ‘kek_meta_dto’ instance (an instance of KEKMetadata above) as needed to completely describe the kek metadata and to complete the binding process. Barbican will persist the contents of this instance once this method returns.
Parameters: | kek_meta_dto – Key encryption key metadata to bind, with the ‘kek_label’ attribute guaranteed to be unique, and the and ‘plugin_name’ attribute already configured. |
---|---|
Returns: | kek_meta_dto: Returns the specified DTO, after modifications. |
Decrypt encrypted_datum in the context of the provided project.
Parameters: |
|
---|---|
Returns: | str – unencrypted byte data |
Encryption handler function
This method will be called by Barbican when requesting an encryption operation on a secret on behalf of a project.
Parameters: |
|
---|---|
Returns: | A response DTO containing the cyphertext and KEK information. |
Return type: | ResponseDTO |
Create a new asymmetric key.
Parameters: |
|
---|---|
Returns: | A tuple containing objects for private_key, public_key and optionally one for passphrase. The objects will be of type ResponseDTO. Each object containing encrypted data and kek_meta_extended, the former the resultant cypher text, the latter being optional per-secret metadata needed to decrypt (over and above the per-project metadata managed outside of the plugins) |
Generate a new key.
Parameters: |
|
---|---|
Returns: | An object of type ResponseDTO containing encrypted data and kek_meta_extended, the former the resultant cypher text, the latter being optional per-secret metadata needed to decrypt (over and above the per-project metadata managed outside of the plugins) |
Gets user friendly plugin name.
This plugin name is expected to be read from config file. There will be a default defined for plugin name which can be customized in specific deployment if needed.
This name needs to be unique across a deployment.
Used to determine if the plugin supports the requested operation.
Parameters: |
|
---|
Barbican invokes a different sequence of methods on the CryptoPluginBase plugin depending on the requested action. Note that these actions are invoked via the secret store adapter class StoreCryptoAdapterPlugin which is further described in The Cryptographic Plugin Adapter.
For secret storage actions, Barbican core calls the following methods:
For secret decryptions and retrievals, Barbican core will select the same plugin as was used to store the secret, and then invoke its decrypt() method, providing it both the previously-persisted encrypted secret data as well as the project-ID KEK used to encrypt the secret.
For symmetric key generation, Barbican core calls the following methods:
For asymmetric key generation, Barbican core calls the following methods: