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.
19 #include "apu_errno.h"
25 #include "apr_strings.h"
27 #include "apr_buckets.h"
29 #include "apr_crypto_internal.h"
33 #include <openssl/evp.h>
34 #include <openssl/rand.h>
35 #include <openssl/engine.h>
37 #define LOG_PREFIX "apr_crypto_openssl: "
39 #ifndef APR_USE_OPENSSL_PRE_1_1_API
40 #if defined(LIBRESSL_VERSION_NUMBER)
41 /* LibreSSL declares OPENSSL_VERSION_NUMBER == 2.0 but does not include most
42 * changes from OpenSSL >= 1.1 (new functions, macros, deprecations, ...), so
43 * we have to work around this...
45 #define APR_USE_OPENSSL_PRE_1_1_API (1)
47 #define APR_USE_OPENSSL_PRE_1_1_API (OPENSSL_VERSION_NUMBER < 0x10100000L)
53 const apr_crypto_driver_t *provider;
55 apr_crypto_config_t *config;
60 struct apr_crypto_config_t {
64 struct apr_crypto_key_t {
66 const apr_crypto_driver_t *provider;
67 const apr_crypto_t *f;
68 const EVP_CIPHER * cipher;
75 struct apr_crypto_block_t {
77 const apr_crypto_driver_t *provider;
78 const apr_crypto_t *f;
79 EVP_CIPHER_CTX *cipherCtx;
86 static struct apr_crypto_block_key_type_t key_types[] =
88 { APR_KEY_3DES_192, 24, 8, 8 },
89 { APR_KEY_AES_128, 16, 16, 16 },
90 { APR_KEY_AES_192, 24, 16, 16 },
91 { APR_KEY_AES_256, 32, 16, 16 } };
93 static struct apr_crypto_block_key_mode_t key_modes[] =
98 /* sufficient space to wrap a key */
99 #define BUFFER_SIZE 128
102 * Fetch the most recent error from this driver.
104 static apr_status_t crypto_error(const apu_err_t **result,
105 const apr_crypto_t *f)
112 * Shutdown the crypto library and release resources.
114 static apr_status_t crypto_shutdown(void)
122 static apr_status_t crypto_shutdown_helper(void *data)
124 return crypto_shutdown();
128 * Initialise the crypto library and perform one time initialisation.
130 static apr_status_t crypto_init(apr_pool_t *pool, const char *params,
131 const apu_err_t **result)
133 #if APR_USE_OPENSSL_PRE_1_1_API
134 (void)CRYPTO_malloc_init();
136 OPENSSL_malloc_init();
138 ERR_load_crypto_strings();
139 /* SSL_load_error_strings(); */
140 OpenSSL_add_all_algorithms();
141 ENGINE_load_builtin_engines();
142 ENGINE_register_all_complete();
144 apr_pool_cleanup_register(pool, pool, crypto_shutdown_helper,
145 apr_pool_cleanup_null);
150 #if OPENSSL_VERSION_NUMBER < 0x0090802fL
152 /* Code taken from OpenSSL 0.9.8b, see
153 * https://github.com/openssl/openssl/commit/cf6bc84148cb15af09b292394aaf2b45f0d5af0d
156 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
158 EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof *ctx);
160 EVP_CIPHER_CTX_init(ctx);
164 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
167 EVP_CIPHER_CTX_cleanup(ctx);
175 * @brief Clean encryption / decryption context.
176 * @note After cleanup, a context is free to be reused if necessary.
177 * @param ctx The block context to use.
178 * @return Returns APR_ENOTIMPL if not supported.
180 static apr_status_t crypto_block_cleanup(apr_crypto_block_t *ctx)
183 if (ctx->initialised) {
184 EVP_CIPHER_CTX_free(ctx->cipherCtx);
185 ctx->initialised = 0;
192 static apr_status_t crypto_block_cleanup_helper(void *data)
194 apr_crypto_block_t *block = (apr_crypto_block_t *) data;
195 return crypto_block_cleanup(block);
199 * @brief Clean encryption / decryption context.
200 * @note After cleanup, a context is free to be reused if necessary.
201 * @param f The context to use.
202 * @return Returns APR_ENOTIMPL if not supported.
204 static apr_status_t crypto_cleanup(apr_crypto_t *f)
207 if (f->config->engine) {
208 ENGINE_finish(f->config->engine);
209 ENGINE_free(f->config->engine);
210 f->config->engine = NULL;
216 static apr_status_t crypto_cleanup_helper(void *data)
218 apr_crypto_t *f = (apr_crypto_t *) data;
219 return crypto_cleanup(f);
223 * @brief Create a context for supporting encryption. Keys, certificates,
224 * algorithms and other parameters will be set per context. More than
225 * one context can be created at one time. A cleanup will be automatically
226 * registered with the given pool to guarantee a graceful shutdown.
227 * @param f - context pointer will be written here
228 * @param provider - provider to use
229 * @param params - array of key parameters
230 * @param pool - process pool
231 * @return APR_ENOENGINE when the engine specified does not exist. APR_EINITENGINE
232 * if the engine cannot be initialised.
234 static apr_status_t crypto_make(apr_crypto_t **ff,
235 const apr_crypto_driver_t *provider, const char *params,
238 apr_crypto_config_t *config = NULL;
239 apr_crypto_t *f = apr_pcalloc(pool, sizeof(apr_crypto_t));
241 const char *engine = NULL;
248 { "engine", NULL, 0 },
259 if (APR_SUCCESS != (status = apr_tokenize_to_argv(params, &elts, pool))) {
262 while ((elt = elts[i])) {
263 ptr = strchr(elt, '=');
265 for (klen = ptr - elt; klen && apr_isspace(elt[klen - 1]); --klen)
270 for (klen = strlen(elt); klen && apr_isspace(elt[klen - 1]); --klen)
275 for (j = 0; fields[j].field != NULL; ++j) {
276 if (!strcasecmp(fields[j].field, elt)) {
279 fields[j].value = ptr;
287 engine = fields[0].value;
295 f->provider = provider;
296 config = f->config = apr_pcalloc(pool, sizeof(apr_crypto_config_t));
301 f->result = apr_pcalloc(pool, sizeof(apu_err_t));
306 f->types = apr_hash_make(pool);
310 apr_hash_set(f->types, "3des192", APR_HASH_KEY_STRING, &(key_types[0]));
311 apr_hash_set(f->types, "aes128", APR_HASH_KEY_STRING, &(key_types[1]));
312 apr_hash_set(f->types, "aes192", APR_HASH_KEY_STRING, &(key_types[2]));
313 apr_hash_set(f->types, "aes256", APR_HASH_KEY_STRING, &(key_types[3]));
315 f->modes = apr_hash_make(pool);
319 apr_hash_set(f->modes, "ecb", APR_HASH_KEY_STRING, &(key_modes[0]));
320 apr_hash_set(f->modes, "cbc", APR_HASH_KEY_STRING, &(key_modes[1]));
322 apr_pool_cleanup_register(pool, f, crypto_cleanup_helper,
323 apr_pool_cleanup_null);
326 config->engine = ENGINE_by_id(engine);
327 if (!config->engine) {
328 return APR_ENOENGINE;
330 if (!ENGINE_init(config->engine)) {
331 ENGINE_free(config->engine);
332 config->engine = NULL;
333 return APR_EINITENGINE;
342 * @brief Get a hash table of key types, keyed by the name of the type against
343 * a pointer to apr_crypto_block_key_type_t.
345 * @param types - hashtable of key types keyed to constants.
346 * @param f - encryption context
347 * @return APR_SUCCESS for success
349 static apr_status_t crypto_get_block_key_types(apr_hash_t **types,
350 const apr_crypto_t *f)
357 * @brief Get a hash table of key modes, keyed by the name of the mode against
358 * a pointer to apr_crypto_block_key_mode_t.
360 * @param modes - hashtable of key modes keyed to constants.
361 * @param f - encryption context
362 * @return APR_SUCCESS for success
364 static apr_status_t crypto_get_block_key_modes(apr_hash_t **modes,
365 const apr_crypto_t *f)
372 * Work out which mechanism to use.
374 static apr_status_t crypto_cipher_mechanism(apr_crypto_key_t *key,
375 const apr_crypto_block_key_type_e type,
376 const apr_crypto_block_key_mode_e mode, const int doPad, apr_pool_t *p)
378 /* determine the cipher to be used */
381 case (APR_KEY_3DES_192):
384 if (mode == APR_MODE_CBC) {
385 key->cipher = EVP_des_ede3_cbc();
388 key->cipher = EVP_des_ede3_ecb();
392 case (APR_KEY_AES_128):
394 if (mode == APR_MODE_CBC) {
395 key->cipher = EVP_aes_128_cbc();
398 key->cipher = EVP_aes_128_ecb();
402 case (APR_KEY_AES_192):
404 if (mode == APR_MODE_CBC) {
405 key->cipher = EVP_aes_192_cbc();
408 key->cipher = EVP_aes_192_ecb();
412 case (APR_KEY_AES_256):
414 if (mode == APR_MODE_CBC) {
415 key->cipher = EVP_aes_256_cbc();
418 key->cipher = EVP_aes_256_ecb();
424 /* unknown key type, give up */
429 /* find the length of the key we need */
430 key->keyLen = EVP_CIPHER_key_length(key->cipher);
432 /* make space for the key */
433 key->key = apr_pcalloc(p, key->keyLen);
437 apr_crypto_clear(p, key->key, key->keyLen);
443 * @brief Create a key from the provided secret or passphrase. The key is cleaned
444 * up when the context is cleaned, and may be reused with multiple encryption
445 * or decryption operations.
446 * @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If
447 * *key is not NULL, *key must point at a previously created structure.
448 * @param key The key returned, see note.
449 * @param rec The key record, from which the key will be derived.
450 * @param f The context to use.
451 * @param p The pool to use.
452 * @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend
453 * error occurred while generating the key. APR_ENOCIPHER if the type or mode
454 * is not supported by the particular backend. APR_EKEYTYPE if the key type is
455 * not known. APR_EPADDING if padding was requested but is not supported.
456 * APR_ENOTIMPL if not implemented.
458 static apr_status_t crypto_key(apr_crypto_key_t **k,
459 const apr_crypto_key_rec_t *rec, const apr_crypto_t *f, apr_pool_t *p)
461 apr_crypto_key_t *key = *k;
465 *k = key = apr_pcalloc(p, sizeof *key);
472 key->provider = f->provider;
474 /* decide on what cipher mechanism we will be using */
475 rv = crypto_cipher_mechanism(key, rec->type, rec->mode, rec->pad, p);
476 if (APR_SUCCESS != rv) {
480 switch (rec->ktype) {
482 case APR_CRYPTO_KTYPE_PASSPHRASE: {
484 /* generate the key */
485 if (PKCS5_PBKDF2_HMAC_SHA1(rec->k.passphrase.pass,
486 rec->k.passphrase.passLen,
487 (unsigned char *) rec->k.passphrase.salt,
488 rec->k.passphrase.saltLen, rec->k.passphrase.iterations,
489 key->keyLen, key->key) == 0) {
496 case APR_CRYPTO_KTYPE_SECRET: {
498 /* sanity check - key correct size? */
499 if (rec->k.secret.secretLen != key->keyLen) {
500 return APR_EKEYLENGTH;
504 memcpy(key->key, rec->k.secret.secret, rec->k.secret.secretLen);
516 key->doPad = rec->pad;
518 /* note: openssl incorrectly returns non zero IV size values for ECB
519 * algorithms, so work around this by ignoring the IV size.
521 if (APR_MODE_ECB != rec->mode) {
522 key->ivSize = EVP_CIPHER_iv_length(key->cipher);
529 * @brief Create a key from the given passphrase. By default, the PBKDF2
530 * algorithm is used to generate the key from the passphrase. It is expected
531 * that the same pass phrase will generate the same key, regardless of the
532 * backend crypto platform used. The key is cleaned up when the context
533 * is cleaned, and may be reused with multiple encryption or decryption
535 * @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If
536 * *key is not NULL, *key must point at a previously created structure.
537 * @param key The key returned, see note.
538 * @param ivSize The size of the initialisation vector will be returned, based
539 * on whether an IV is relevant for this type of crypto.
540 * @param pass The passphrase to use.
541 * @param passLen The passphrase length in bytes
542 * @param salt The salt to use.
543 * @param saltLen The salt length in bytes
544 * @param type 3DES_192, AES_128, AES_192, AES_256.
545 * @param mode Electronic Code Book / Cipher Block Chaining.
546 * @param doPad Pad if necessary.
547 * @param iterations Iteration count
548 * @param f The context to use.
549 * @param p The pool to use.
550 * @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend
551 * error occurred while generating the key. APR_ENOCIPHER if the type or mode
552 * is not supported by the particular backend. APR_EKEYTYPE if the key type is
553 * not known. APR_EPADDING if padding was requested but is not supported.
554 * APR_ENOTIMPL if not implemented.
556 static apr_status_t crypto_passphrase(apr_crypto_key_t **k, apr_size_t *ivSize,
557 const char *pass, apr_size_t passLen, const unsigned char * salt,
558 apr_size_t saltLen, const apr_crypto_block_key_type_e type,
559 const apr_crypto_block_key_mode_e mode, const int doPad,
560 const int iterations, const apr_crypto_t *f, apr_pool_t *p)
562 apr_crypto_key_t *key = *k;
566 *k = key = apr_pcalloc(p, sizeof *key);
573 key->provider = f->provider;
575 /* decide on what cipher mechanism we will be using */
576 rv = crypto_cipher_mechanism(key, type, mode, doPad, p);
577 if (APR_SUCCESS != rv) {
581 /* generate the key */
582 if (PKCS5_PBKDF2_HMAC_SHA1(pass, passLen, (unsigned char *) salt, saltLen,
583 iterations, key->keyLen, key->key) == 0) {
589 /* note: openssl incorrectly returns non zero IV size values for ECB
590 * algorithms, so work around this by ignoring the IV size.
592 if (APR_MODE_ECB != mode) {
593 key->ivSize = EVP_CIPHER_iv_length(key->cipher);
596 *ivSize = key->ivSize;
603 * @brief Initialise a context for encrypting arbitrary data using the given key.
604 * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
605 * *ctx is not NULL, *ctx must point at a previously created structure.
606 * @param ctx The block context returned, see note.
607 * @param iv Optional initialisation vector. If the buffer pointed to is NULL,
608 * an IV will be created at random, in space allocated from the pool.
609 * If the buffer pointed to is not NULL, the IV in the buffer will be
611 * @param key The key structure.
612 * @param blockSize The block size of the cipher.
613 * @param p The pool to use.
614 * @return Returns APR_ENOIV if an initialisation vector is required but not specified.
615 * Returns APR_EINIT if the backend failed to initialise the context. Returns
616 * APR_ENOTIMPL if not implemented.
618 static apr_status_t crypto_block_encrypt_init(apr_crypto_block_t **ctx,
619 const unsigned char **iv, const apr_crypto_key_t *key,
620 apr_size_t *blockSize, apr_pool_t *p)
622 unsigned char *usedIv;
623 apr_crypto_config_t *config = key->f->config;
624 apr_crypto_block_t *block = *ctx;
626 *ctx = block = apr_pcalloc(p, sizeof(apr_crypto_block_t));
633 block->provider = key->provider;
635 apr_pool_cleanup_register(p, block, crypto_block_cleanup_helper,
636 apr_pool_cleanup_null);
638 /* create a new context for encryption */
639 if (!block->initialised) {
640 block->cipherCtx = EVP_CIPHER_CTX_new();
641 block->initialised = 1;
644 /* generate an IV, if necessary */
651 usedIv = apr_pcalloc(p, key->ivSize);
655 apr_crypto_clear(p, usedIv, key->ivSize);
656 if (!((RAND_status() == 1)
657 && (RAND_bytes(usedIv, key->ivSize) == 1))) {
663 usedIv = (unsigned char *) *iv;
667 /* set up our encryption context */
668 #if CRYPTO_OPENSSL_CONST_BUFFERS
669 if (!EVP_EncryptInit_ex(block->cipherCtx, key->cipher, config->engine,
672 if (!EVP_EncryptInit_ex(block->cipherCtx, key->cipher, config->engine, (unsigned char *) key->key, (unsigned char *) usedIv)) {
677 /* Clear up any read padding */
678 if (!EVP_CIPHER_CTX_set_padding(block->cipherCtx, key->doPad)) {
683 *blockSize = EVP_CIPHER_block_size(key->cipher);
691 * @brief Encrypt data provided by in, write it to out.
692 * @note The number of bytes written will be written to outlen. If
693 * out is NULL, outlen will contain the maximum size of the
694 * buffer needed to hold the data, including any data
695 * generated by apr_crypto_block_encrypt_finish below. If *out points
696 * to NULL, a buffer sufficiently large will be created from
697 * the pool provided. If *out points to a not-NULL value, this
698 * value will be used as a buffer instead.
699 * @param out Address of a buffer to which data will be written,
701 * @param outlen Length of the output will be written here.
702 * @param in Address of the buffer to read.
703 * @param inlen Length of the buffer to read.
704 * @param ctx The block context to use.
705 * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
708 static apr_status_t crypto_block_encrypt(unsigned char **out,
709 apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
710 apr_crypto_block_t *ctx)
713 unsigned char *buffer;
715 /* are we after the maximum size of the out buffer? */
717 *outlen = inlen + EVP_MAX_BLOCK_LENGTH;
721 /* must we allocate the output buffer from a pool? */
723 buffer = apr_palloc(ctx->pool, inlen + EVP_MAX_BLOCK_LENGTH);
727 apr_crypto_clear(ctx->pool, buffer, inlen + EVP_MAX_BLOCK_LENGTH);
731 #if CRYPT_OPENSSL_CONST_BUFFERS
732 if (!EVP_EncryptUpdate(ctx->cipherCtx, (*out), &outl, in, inlen)) {
734 if (!EVP_EncryptUpdate(ctx->cipherCtx, (*out), &outl,
735 (unsigned char *) in, inlen)) {
737 #if APR_USE_OPENSSL_PRE_1_1_API
738 EVP_CIPHER_CTX_cleanup(ctx->cipherCtx);
740 EVP_CIPHER_CTX_reset(ctx->cipherCtx);
751 * @brief Encrypt final data block, write it to out.
752 * @note If necessary the final block will be written out after being
753 * padded. Typically the final block will be written to the
754 * same buffer used by apr_crypto_block_encrypt, offset by the
755 * number of bytes returned as actually written by the
756 * apr_crypto_block_encrypt() call. After this call, the context
757 * is cleaned and can be reused by apr_crypto_block_encrypt_init().
758 * @param out Address of a buffer to which data will be written. This
759 * buffer must already exist, and is usually the same
760 * buffer used by apr_evp_crypt(). See note.
761 * @param outlen Length of the output will be written here.
762 * @param ctx The block context to use.
763 * @return APR_ECRYPT if an error occurred.
764 * @return APR_EPADDING if padding was enabled and the block was incorrectly
766 * @return APR_ENOTIMPL if not implemented.
768 static apr_status_t crypto_block_encrypt_finish(unsigned char *out,
769 apr_size_t *outlen, apr_crypto_block_t *ctx)
771 apr_status_t rc = APR_SUCCESS;
774 if (EVP_EncryptFinal_ex(ctx->cipherCtx, out, &len) == 0) {
780 #if APR_USE_OPENSSL_PRE_1_1_API
781 EVP_CIPHER_CTX_cleanup(ctx->cipherCtx);
783 EVP_CIPHER_CTX_reset(ctx->cipherCtx);
791 * @brief Initialise a context for decrypting arbitrary data using the given key.
792 * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
793 * *ctx is not NULL, *ctx must point at a previously created structure.
794 * @param ctx The block context returned, see note.
795 * @param blockSize The block size of the cipher.
796 * @param iv Optional initialisation vector. If the buffer pointed to is NULL,
797 * an IV will be created at random, in space allocated from the pool.
798 * If the buffer is not NULL, the IV in the buffer will be used.
799 * @param key The key structure.
800 * @param p The pool to use.
801 * @return Returns APR_ENOIV if an initialisation vector is required but not specified.
802 * Returns APR_EINIT if the backend failed to initialise the context. Returns
803 * APR_ENOTIMPL if not implemented.
805 static apr_status_t crypto_block_decrypt_init(apr_crypto_block_t **ctx,
806 apr_size_t *blockSize, const unsigned char *iv,
807 const apr_crypto_key_t *key, apr_pool_t *p)
809 apr_crypto_config_t *config = key->f->config;
810 apr_crypto_block_t *block = *ctx;
812 *ctx = block = apr_pcalloc(p, sizeof(apr_crypto_block_t));
819 block->provider = key->provider;
821 apr_pool_cleanup_register(p, block, crypto_block_cleanup_helper,
822 apr_pool_cleanup_null);
824 /* create a new context for encryption */
825 if (!block->initialised) {
826 block->cipherCtx = EVP_CIPHER_CTX_new();
827 block->initialised = 1;
830 /* generate an IV, if necessary */
837 /* set up our encryption context */
838 #if CRYPTO_OPENSSL_CONST_BUFFERS
839 if (!EVP_DecryptInit_ex(block->cipherCtx, key->cipher, config->engine,
842 if (!EVP_DecryptInit_ex(block->cipherCtx, key->cipher, config->engine, (unsigned char *) key->key, (unsigned char *) iv)) {
847 /* Clear up any read padding */
848 if (!EVP_CIPHER_CTX_set_padding(block->cipherCtx, key->doPad)) {
853 *blockSize = EVP_CIPHER_block_size(key->cipher);
861 * @brief Decrypt data provided by in, write it to out.
862 * @note The number of bytes written will be written to outlen. If
863 * out is NULL, outlen will contain the maximum size of the
864 * buffer needed to hold the data, including any data
865 * generated by apr_crypto_block_decrypt_finish below. If *out points
866 * to NULL, a buffer sufficiently large will be created from
867 * the pool provided. If *out points to a not-NULL value, this
868 * value will be used as a buffer instead.
869 * @param out Address of a buffer to which data will be written,
871 * @param outlen Length of the output will be written here.
872 * @param in Address of the buffer to read.
873 * @param inlen Length of the buffer to read.
874 * @param ctx The block context to use.
875 * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
878 static apr_status_t crypto_block_decrypt(unsigned char **out,
879 apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
880 apr_crypto_block_t *ctx)
883 unsigned char *buffer;
885 /* are we after the maximum size of the out buffer? */
887 *outlen = inlen + EVP_MAX_BLOCK_LENGTH;
891 /* must we allocate the output buffer from a pool? */
893 buffer = apr_palloc(ctx->pool, inlen + EVP_MAX_BLOCK_LENGTH);
897 apr_crypto_clear(ctx->pool, buffer, inlen + EVP_MAX_BLOCK_LENGTH);
901 #if CRYPT_OPENSSL_CONST_BUFFERS
902 if (!EVP_DecryptUpdate(ctx->cipherCtx, *out, &outl, in, inlen)) {
904 if (!EVP_DecryptUpdate(ctx->cipherCtx, *out, &outl, (unsigned char *) in,
907 #if APR_USE_OPENSSL_PRE_1_1_API
908 EVP_CIPHER_CTX_cleanup(ctx->cipherCtx);
910 EVP_CIPHER_CTX_reset(ctx->cipherCtx);
921 * @brief Decrypt final data block, write it to out.
922 * @note If necessary the final block will be written out after being
923 * padded. Typically the final block will be written to the
924 * same buffer used by apr_crypto_block_decrypt, offset by the
925 * number of bytes returned as actually written by the
926 * apr_crypto_block_decrypt() call. After this call, the context
927 * is cleaned and can be reused by apr_crypto_block_decrypt_init().
928 * @param out Address of a buffer to which data will be written. This
929 * buffer must already exist, and is usually the same
930 * buffer used by apr_evp_crypt(). See note.
931 * @param outlen Length of the output will be written here.
932 * @param ctx The block context to use.
933 * @return APR_ECRYPT if an error occurred.
934 * @return APR_EPADDING if padding was enabled and the block was incorrectly
936 * @return APR_ENOTIMPL if not implemented.
938 static apr_status_t crypto_block_decrypt_finish(unsigned char *out,
939 apr_size_t *outlen, apr_crypto_block_t *ctx)
941 apr_status_t rc = APR_SUCCESS;
944 if (EVP_DecryptFinal_ex(ctx->cipherCtx, out, &len) == 0) {
950 #if APR_USE_OPENSSL_PRE_1_1_API
951 EVP_CIPHER_CTX_cleanup(ctx->cipherCtx);
953 EVP_CIPHER_CTX_reset(ctx->cipherCtx);
963 APU_MODULE_DECLARE_DATA const apr_crypto_driver_t apr_crypto_openssl_driver = {
964 "openssl", crypto_init, crypto_make, crypto_get_block_key_types,
965 crypto_get_block_key_modes, crypto_passphrase,
966 crypto_block_encrypt_init, crypto_block_encrypt,
967 crypto_block_encrypt_finish, crypto_block_decrypt_init,
968 crypto_block_decrypt, crypto_block_decrypt_finish,
969 crypto_block_cleanup, crypto_cleanup, crypto_shutdown, crypto_error,