4 Engine Stored Keys

This chapter describes the support in the crypto application for using public and private keys stored in encryption engines.

4.1  Background

OpenSSL exposes an Engine API, which makes it possible to plug in alternative implementations for some of the cryptographic operations implemented by OpenSSL. See the chapter Engine Load for details and how to load an Engine.

An engine could among other tasks provide a storage for private or public keys. Such a storage could be made safer than the normal file system. Those techniques are not described in this User's Guide. Here we concentrate on how to use private or public keys stored in such an engine.

The storage engine must call ENGINE_set_load_privkey_function and ENGINE_set_load_pubkey_function. See the OpenSSL cryptolib's manpages.

OTP/Crypto requires that the user provides two or three items of information about the key. The application used by the user is usually on a higher level, for example in SSL. If using the crypto application directly, it is required that:

  • an Engine is loaded, see the chapter on Engine Load or the Reference Manual
  • a reference to a key in the Engine is available. This should be an Erlang string or binary and depends on the Engine loaded
  • an Erlang map is constructed with the Engine reference, the key reference and possibly a key passphrase if needed by the Engine. See the Reference Manual for details of the map.

4.2  Use Cases

Sign with an engine stored private key

This example shows how to construct a key reference that is used in a sign operation. The actual key is stored in the engine that is loaded at prompt 1.

1> {ok, EngineRef} = crypto:engine_load(....).
2> PrivKey = #{engine => EngineRef,
               key_id => "id of the private key in Engine"}.
3> Signature = crypto:sign(rsa, sha, <<"The message">>, PrivKey).

Verify with an engine stored public key

Here the signature and message in the last example is verifyed using the public key. The public key is stored in an engine, only to exemplify that it is possible. The public key could of course be handled openly as usual.

4> PublicKey = #{engine => EngineRef,
                 key_id => "id of the public key in Engine"}.
5> crypto:verify(rsa, sha, <<"The message">>, Signature, PublicKey).

Using a password protected private key

The same example as the first sign example, except that a password protects the key down in the Engine.

6> PrivKeyPwd = #{engine => EngineRef,
                  key_id => "id of the pwd protected private key in Engine",
		  password => "password"}.
7> crypto:sign(rsa, sha, <<"The message">>, PrivKeyPwd).