1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include "apr_pools.h"
22 #include "apr_tables.h"
24 #include "apu_errno.h"
32 * @brief APR-UTIL Crypto library
35 * @defgroup APR_Util_Crypto Crypto routines
42 #ifndef APU_CRYPTO_RECOMMENDED_DRIVER
43 #if APU_HAVE_COMMONCRYPTO
44 #define APU_CRYPTO_RECOMMENDED_DRIVER "commoncrypto"
47 #define APU_CRYPTO_RECOMMENDED_DRIVER "openssl"
50 #define APU_CRYPTO_RECOMMENDED_DRIVER "nss"
53 #define APU_CRYPTO_RECOMMENDED_DRIVER "mscng"
56 #define APU_CRYPTO_RECOMMENDED_DRIVER "mscapi"
66 * Symmetric Key types understood by the library.
68 * NOTE: It is expected that this list will grow over time.
70 * Interoperability Matrix:
72 * The matrix is based on the testcrypto.c unit test, which attempts to
73 * test whether a simple encrypt/decrypt will succeed, as well as testing
74 * whether an encrypted string by one library can be decrypted by the
77 * Some libraries will successfully encrypt and decrypt their own data,
78 * but won't decrypt data from another library. It is hoped that over
79 * time these anomalies will be found and fixed, but until then it is
80 * recommended that ciphers are chosen that interoperate across platform.
82 * An X below means the test passes, it does not necessarily mean that
83 * encryption performed is correct or secure. Applications should stick
84 * to ciphers that pass the interoperablity tests on the right hand side
87 * Aligned data is data whose length is a multiple of the block size for
88 * the chosen cipher. Padded data is data that is not aligned by block
89 * size and must be padded by the crypto library.
91 * OpenSSL CommonCrypto NSS Interop
92 * Align Pad Align Pad Align Pad Align Pad
93 * 3DES_192/CBC X X X X X X X X
94 * 3DES_192/ECB X X X X
95 * AES_256/CBC X X X X X X X X
96 * AES_256/ECB X X X X X X
97 * AES_192/CBC X X X X X X
98 * AES_192/ECB X X X X X
99 * AES_128/CBC X X X X X X
100 * AES_128/ECB X X X X X
102 * Conclusion: for padded data, use 3DES_192/CBC or AES_256/CBC. For
103 * aligned data, use 3DES_192/CBC, AES_256/CBC or AES_256/ECB.
108 APR_KEY_NONE, APR_KEY_3DES_192, /** 192 bit (3-Key) 3DES */
109 APR_KEY_AES_128, /** 128 bit AES */
110 APR_KEY_AES_192, /** 192 bit AES */
113 } apr_crypto_block_key_type_e;
117 APR_MODE_NONE, /** An error condition */
118 APR_MODE_ECB, /** Electronic Code Book */
120 /** Cipher Block Chaining */
121 } apr_crypto_block_key_mode_e;
123 /* These are opaque structs. Instantiation is up to each backend */
124 typedef struct apr_crypto_driver_t apr_crypto_driver_t;
125 typedef struct apr_crypto_t apr_crypto_t;
126 typedef struct apr_crypto_config_t apr_crypto_config_t;
127 typedef struct apr_crypto_key_t apr_crypto_key_t;
128 typedef struct apr_crypto_block_t apr_crypto_block_t;
130 typedef struct apr_crypto_block_key_type_t {
131 apr_crypto_block_key_type_e type;
135 } apr_crypto_block_key_type_t;
137 typedef struct apr_crypto_block_key_mode_t {
138 apr_crypto_block_key_mode_e mode;
139 } apr_crypto_block_key_mode_t;
141 typedef struct apr_crypto_passphrase_t {
144 const unsigned char * salt;
147 } apr_crypto_passphrase_t;
149 typedef struct apr_crypto_secret_t {
150 const unsigned char *secret;
151 apr_size_t secretLen;
152 } apr_crypto_secret_t;
155 /** Key is derived from a passphrase */
156 APR_CRYPTO_KTYPE_PASSPHRASE = 1,
157 /** Key is derived from a raw key */
158 APR_CRYPTO_KTYPE_SECRET = 2,
159 } apr_crypto_key_type;
161 typedef struct apr_crypto_key_rec_t {
162 apr_crypto_key_type ktype;
163 apr_crypto_block_key_type_e type;
164 apr_crypto_block_key_mode_e mode;
167 apr_crypto_passphrase_t passphrase;
168 apr_crypto_secret_t secret;
170 } apr_crypto_key_rec_t;
173 * @brief Perform once-only initialisation. Call once only.
175 * @param pool - pool to register any shutdown cleanups, etc
176 * @return APR_NOTIMPL in case of no crypto support.
178 APU_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool);
181 * @brief Zero out the buffer provided when the pool is cleaned up.
183 * @param pool - pool to register the cleanup
184 * @param buffer - buffer to zero out
185 * @param size - size of the buffer to zero out
187 APU_DECLARE(apr_status_t) apr_crypto_clear(apr_pool_t *pool, void *buffer,
191 * @brief Always zero out the buffer provided, without being optimized out by
194 * @param buffer - buffer to zero out
195 * @param size - size of the buffer to zero out
197 APU_DECLARE(apr_status_t) apr_crypto_memzero(void *buffer, apr_size_t size);
200 * @brief Timing attacks safe buffers comparison, where the executing time does
201 * not depend on the bytes compared but solely on the number of bytes.
203 * @param buf1 - first buffer to compare
204 * @param buf2 - second buffer to compare
205 * @param size - size of the buffers to compare
206 * @return 1 if the buffers are equals, 0 otherwise.
208 APU_DECLARE(int) apr_crypto_equals(const void *buf1, const void *buf2,
212 * @brief Get the driver struct for a name
214 * @param driver - pointer to driver struct.
215 * @param name - driver name
216 * @param params - array of initialisation parameters
217 * @param result - result and error message on failure
218 * @param pool - (process) pool to register cleanup
219 * @return APR_SUCCESS for success
220 * @return APR_ENOTIMPL for no driver (when DSO not enabled)
221 * @return APR_EDSOOPEN if DSO driver file can't be opened
222 * @return APR_ESYMNOTFOUND if the driver file doesn't contain a driver
223 * @remarks NSS: the params can have "dir", "key3", "cert7" and "secmod"
224 * keys, each followed by an equal sign and a value. Such key/value pairs can
225 * be delimited by space or tab. If the value contains a space, surround the
226 * whole key value pair in quotes: "dir=My Directory".
227 * @remarks OpenSSL: currently no params are supported.
229 APU_DECLARE(apr_status_t) apr_crypto_get_driver(
230 const apr_crypto_driver_t **driver,
231 const char *name, const char *params, const apu_err_t **result,
235 * @brief Return the name of the driver.
237 * @param driver - The driver in use.
238 * @return The name of the driver.
240 APU_DECLARE(const char *) apr_crypto_driver_name(
241 const apr_crypto_driver_t *driver);
244 * @brief Get the result of the last operation on a context. If the result
245 * is NULL, the operation was successful.
246 * @param result - the result structure
247 * @param f - context pointer
248 * @return APR_SUCCESS for success
250 APU_DECLARE(apr_status_t) apr_crypto_error(const apu_err_t **result,
251 const apr_crypto_t *f);
254 * @brief Create a context for supporting encryption. Keys, certificates,
255 * algorithms and other parameters will be set per context. More than
256 * one context can be created at one time. A cleanup will be automatically
257 * registered with the given pool to guarantee a graceful shutdown.
258 * @param f - context pointer will be written here
259 * @param driver - driver to use
260 * @param params - array of key parameters
261 * @param pool - process pool
262 * @return APR_ENOENGINE when the engine specified does not exist. APR_EINITENGINE
263 * if the engine cannot be initialised.
264 * @remarks NSS: currently no params are supported.
265 * @remarks OpenSSL: the params can have "engine" as a key, followed by an equal
268 APU_DECLARE(apr_status_t) apr_crypto_make(apr_crypto_t **f,
269 const apr_crypto_driver_t *driver, const char *params,
273 * @brief Get a hash table of key types, keyed by the name of the type against
274 * a pointer to apr_crypto_block_key_type_t, which in turn begins with an
277 * @param types - hashtable of key types keyed to constants.
278 * @param f - encryption context
279 * @return APR_SUCCESS for success
281 APU_DECLARE(apr_status_t) apr_crypto_get_block_key_types(apr_hash_t **types,
282 const apr_crypto_t *f);
285 * @brief Get a hash table of key modes, keyed by the name of the mode against
286 * a pointer to apr_crypto_block_key_mode_t, which in turn begins with an
289 * @param modes - hashtable of key modes keyed to constants.
290 * @param f - encryption context
291 * @return APR_SUCCESS for success
293 APU_DECLARE(apr_status_t) apr_crypto_get_block_key_modes(apr_hash_t **modes,
294 const apr_crypto_t *f);
297 * @brief Create a key from the provided secret or passphrase. The key is cleaned
298 * up when the context is cleaned, and may be reused with multiple encryption
299 * or decryption operations.
300 * @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If
301 * *key is not NULL, *key must point at a previously created structure.
302 * @param key The key returned, see note.
303 * @param rec The key record, from which the key will be derived.
304 * @param f The context to use.
305 * @param p The pool to use.
306 * @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend
307 * error occurred while generating the key. APR_ENOCIPHER if the type or mode
308 * is not supported by the particular backend. APR_EKEYTYPE if the key type is
309 * not known. APR_EPADDING if padding was requested but is not supported.
310 * APR_ENOTIMPL if not implemented.
312 APU_DECLARE(apr_status_t) apr_crypto_key(apr_crypto_key_t **key,
313 const apr_crypto_key_rec_t *rec, const apr_crypto_t *f, apr_pool_t *p);
316 * @brief Create a key from the given passphrase. By default, the PBKDF2
317 * algorithm is used to generate the key from the passphrase. It is expected
318 * that the same pass phrase will generate the same key, regardless of the
319 * backend crypto platform used. The key is cleaned up when the context
320 * is cleaned, and may be reused with multiple encryption or decryption
322 * @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If
323 * *key is not NULL, *key must point at a previously created structure.
324 * @param key The key returned, see note.
325 * @param ivSize The size of the initialisation vector will be returned, based
326 * on whether an IV is relevant for this type of crypto.
327 * @param pass The passphrase to use.
328 * @param passLen The passphrase length in bytes
329 * @param salt The salt to use.
330 * @param saltLen The salt length in bytes
331 * @param type 3DES_192, AES_128, AES_192, AES_256.
332 * @param mode Electronic Code Book / Cipher Block Chaining.
333 * @param doPad Pad if necessary.
334 * @param iterations Number of iterations to use in algorithm
335 * @param f The context to use.
336 * @param p The pool to use.
337 * @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend
338 * error occurred while generating the key. APR_ENOCIPHER if the type or mode
339 * is not supported by the particular backend. APR_EKEYTYPE if the key type is
340 * not known. APR_EPADDING if padding was requested but is not supported.
341 * APR_ENOTIMPL if not implemented.
342 * @deprecated Replaced by apr_crypto_key().
344 APU_DECLARE(apr_status_t) apr_crypto_passphrase(apr_crypto_key_t **key,
345 apr_size_t *ivSize, const char *pass, apr_size_t passLen,
346 const unsigned char * salt, apr_size_t saltLen,
347 const apr_crypto_block_key_type_e type,
348 const apr_crypto_block_key_mode_e mode, const int doPad,
349 const int iterations, const apr_crypto_t *f, apr_pool_t *p);
352 * @brief Initialise a context for encrypting arbitrary data using the given key.
353 * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
354 * *ctx is not NULL, *ctx must point at a previously created structure.
355 * @param ctx The block context returned, see note.
356 * @param iv Optional initialisation vector. If the buffer pointed to is NULL,
357 * an IV will be created at random, in space allocated from the pool.
358 * If the buffer pointed to is not NULL, the IV in the buffer will be
360 * @param key The key structure to use.
361 * @param blockSize The block size of the cipher.
362 * @param p The pool to use.
363 * @return Returns APR_ENOIV if an initialisation vector is required but not specified.
364 * Returns APR_EINIT if the backend failed to initialise the context. Returns
365 * APR_ENOTIMPL if not implemented.
367 APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_init(
368 apr_crypto_block_t **ctx, const unsigned char **iv,
369 const apr_crypto_key_t *key, apr_size_t *blockSize, apr_pool_t *p);
372 * @brief Encrypt data provided by in, write it to out.
373 * @note The number of bytes written will be written to outlen. If
374 * out is NULL, outlen will contain the maximum size of the
375 * buffer needed to hold the data, including any data
376 * generated by apr_crypto_block_encrypt_finish below. If *out points
377 * to NULL, a buffer sufficiently large will be created from
378 * the pool provided. If *out points to a not-NULL value, this
379 * value will be used as a buffer instead.
380 * @param out Address of a buffer to which data will be written,
382 * @param outlen Length of the output will be written here.
383 * @param in Address of the buffer to read.
384 * @param inlen Length of the buffer to read.
385 * @param ctx The block context to use.
386 * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
389 APU_DECLARE(apr_status_t) apr_crypto_block_encrypt(unsigned char **out,
390 apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
391 apr_crypto_block_t *ctx);
394 * @brief Encrypt final data block, write it to out.
395 * @note If necessary the final block will be written out after being
396 * padded. Typically the final block will be written to the
397 * same buffer used by apr_crypto_block_encrypt, offset by the
398 * number of bytes returned as actually written by the
399 * apr_crypto_block_encrypt() call. After this call, the context
400 * is cleaned and can be reused by apr_crypto_block_encrypt_init().
401 * @param out Address of a buffer to which data will be written. This
402 * buffer must already exist, and is usually the same
403 * buffer used by apr_evp_crypt(). See note.
404 * @param outlen Length of the output will be written here.
405 * @param ctx The block context to use.
406 * @return APR_ECRYPT if an error occurred.
407 * @return APR_EPADDING if padding was enabled and the block was incorrectly
409 * @return APR_ENOTIMPL if not implemented.
411 APU_DECLARE(apr_status_t) apr_crypto_block_encrypt_finish(unsigned char *out,
412 apr_size_t *outlen, apr_crypto_block_t *ctx);
415 * @brief Initialise a context for decrypting arbitrary data using the given key.
416 * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
417 * *ctx is not NULL, *ctx must point at a previously created structure.
418 * @param ctx The block context returned, see note.
419 * @param blockSize The block size of the cipher.
420 * @param iv Optional initialisation vector.
421 * @param key The key structure to use.
422 * @param p The pool to use.
423 * @return Returns APR_ENOIV if an initialisation vector is required but not specified.
424 * Returns APR_EINIT if the backend failed to initialise the context. Returns
425 * APR_ENOTIMPL if not implemented.
427 APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_init(
428 apr_crypto_block_t **ctx, apr_size_t *blockSize,
429 const unsigned char *iv, const apr_crypto_key_t *key, apr_pool_t *p);
432 * @brief Decrypt data provided by in, write it to out.
433 * @note The number of bytes written will be written to outlen. If
434 * out is NULL, outlen will contain the maximum size of the
435 * buffer needed to hold the data, including any data
436 * generated by apr_crypto_block_decrypt_finish below. If *out points
437 * to NULL, a buffer sufficiently large will be created from
438 * the pool provided. If *out points to a not-NULL value, this
439 * value will be used as a buffer instead.
440 * @param out Address of a buffer to which data will be written,
442 * @param outlen Length of the output will be written here.
443 * @param in Address of the buffer to read.
444 * @param inlen Length of the buffer to read.
445 * @param ctx The block context to use.
446 * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
449 APU_DECLARE(apr_status_t) apr_crypto_block_decrypt(unsigned char **out,
450 apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
451 apr_crypto_block_t *ctx);
454 * @brief Decrypt final data block, write it to out.
455 * @note If necessary the final block will be written out after being
456 * padded. Typically the final block will be written to the
457 * same buffer used by apr_crypto_block_decrypt, offset by the
458 * number of bytes returned as actually written by the
459 * apr_crypto_block_decrypt() call. After this call, the context
460 * is cleaned and can be reused by apr_crypto_block_decrypt_init().
461 * @param out Address of a buffer to which data will be written. This
462 * buffer must already exist, and is usually the same
463 * buffer used by apr_evp_crypt(). See note.
464 * @param outlen Length of the output will be written here.
465 * @param ctx The block context to use.
466 * @return APR_ECRYPT if an error occurred.
467 * @return APR_EPADDING if padding was enabled and the block was incorrectly
469 * @return APR_ENOTIMPL if not implemented.
471 APU_DECLARE(apr_status_t) apr_crypto_block_decrypt_finish(unsigned char *out,
472 apr_size_t *outlen, apr_crypto_block_t *ctx);
475 * @brief Clean encryption / decryption context.
476 * @note After cleanup, a context is free to be reused if necessary.
477 * @param ctx The block context to use.
478 * @return Returns APR_ENOTIMPL if not supported.
480 APU_DECLARE(apr_status_t) apr_crypto_block_cleanup(apr_crypto_block_t *ctx);
483 * @brief Clean encryption / decryption context.
484 * @note After cleanup, a context is free to be reused if necessary.
485 * @param f The context to use.
486 * @return Returns APR_ENOTIMPL if not supported.
488 APU_DECLARE(apr_status_t) apr_crypto_cleanup(apr_crypto_t *f);
491 * @brief Shutdown the crypto library.
492 * @note After shutdown, it is expected that the init function can be called again.
493 * @param driver - driver to use
494 * @return Returns APR_ENOTIMPL if not supported.
496 APU_DECLARE(apr_status_t) apr_crypto_shutdown(
497 const apr_crypto_driver_t *driver);
499 #endif /* APU_HAVE_CRYPTO */