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_config.h"
20 #include "apu_errno.h"
25 #include "apr_strings.h"
27 #include "apr_buckets.h"
29 #include "apr_crypto_internal.h"
42 #ifdef HAVE_NSS_PK11PUB_H
43 #include <nss/pk11pub.h>
51 const apr_crypto_driver_t *provider;
53 apr_crypto_config_t *config;
58 struct apr_crypto_config_t {
62 struct apr_crypto_key_t {
64 const apr_crypto_driver_t *provider;
65 const apr_crypto_t *f;
66 CK_MECHANISM_TYPE cipherMech;
73 struct apr_crypto_block_t {
75 const apr_crypto_driver_t *provider;
76 const apr_crypto_t *f;
78 apr_crypto_key_t *key;
83 static struct apr_crypto_block_key_type_t key_types[] =
85 { APR_KEY_3DES_192, 24, 8, 8 },
86 { APR_KEY_AES_128, 16, 16, 16 },
87 { APR_KEY_AES_192, 24, 16, 16 },
88 { APR_KEY_AES_256, 32, 16, 16 } };
90 static struct apr_crypto_block_key_mode_t key_modes[] =
95 /* sufficient space to wrap a key */
96 #define BUFFER_SIZE 128
99 * Fetch the most recent error from this driver.
101 static apr_status_t crypto_error(const apu_err_t **result,
102 const apr_crypto_t *f)
109 * Shutdown the crypto library and release resources.
111 * It is safe to shut down twice.
113 static apr_status_t crypto_shutdown(void)
115 if (NSS_IsInitialized()) {
116 SECStatus s = NSS_Shutdown();
117 if (s != SECSuccess) {
118 fprintf(stderr, "NSS failed to shutdown, possible leak: %d: %s",
119 PR_GetError(), PR_ErrorToName(s));
126 static apr_status_t crypto_shutdown_helper(void *data)
128 return crypto_shutdown();
132 * Initialise the crypto library and perform one time initialisation.
134 static apr_status_t crypto_init(apr_pool_t *pool, const char *params,
135 const apu_err_t **result)
138 const char *dir = NULL;
139 const char *keyPrefix = NULL;
140 const char *certPrefix = NULL;
141 const char *secmod = NULL;
152 { "cert7", NULL, 0 },
153 { "secmod", NULL, 0 },
154 { "noinit", NULL, 0 },
165 if (APR_SUCCESS != (status = apr_tokenize_to_argv(params, &elts, pool))) {
168 while ((elt = elts[i])) {
169 ptr = strchr(elt, '=');
171 for (klen = ptr - elt; klen && apr_isspace(elt[klen - 1]); --klen)
176 for (klen = strlen(elt); klen && apr_isspace(elt[klen - 1]); --klen)
181 for (j = 0; fields[j].field != NULL; ++j) {
182 if (klen && !strcasecmp(fields[j].field, elt)) {
185 fields[j].value = ptr;
193 dir = fields[0].value;
194 keyPrefix = fields[1].value;
195 certPrefix = fields[2].value;
196 secmod = fields[3].value;
197 noinit = fields[4].set;
200 /* if we've been asked to bypass, do so here */
205 /* sanity check - we can only initialise NSS once */
206 if (NSS_IsInitialized()) {
210 if (keyPrefix || certPrefix || secmod) {
211 s = NSS_Initialize(dir, certPrefix, keyPrefix, secmod, flags);
214 s = NSS_InitReadWrite(dir);
217 s = NSS_NoDB_Init(NULL);
219 if (s != SECSuccess) {
221 /* Note: all memory must be owned by the caller, in case we're unloaded */
222 apu_err_t *err = apr_pcalloc(pool, sizeof(apu_err_t));
223 err->rc = PR_GetError();
224 err->msg = apr_pstrdup(pool, PR_ErrorToName(s));
225 err->reason = apr_pstrdup(pool, "Error during 'nss' initialisation");
232 apr_pool_cleanup_register(pool, pool, crypto_shutdown_helper,
233 apr_pool_cleanup_null);
240 * @brief Clean encryption / decryption context.
241 * @note After cleanup, a context is free to be reused if necessary.
242 * @param f The context to use.
243 * @return Returns APR_ENOTIMPL if not supported.
245 static apr_status_t crypto_block_cleanup(apr_crypto_block_t *block)
248 if (block->secParam) {
249 SECITEM_FreeItem(block->secParam, PR_TRUE);
250 block->secParam = NULL;
254 PK11_DestroyContext(block->ctx, PR_TRUE);
262 static apr_status_t crypto_block_cleanup_helper(void *data)
264 apr_crypto_block_t *block = (apr_crypto_block_t *) data;
265 return crypto_block_cleanup(block);
268 static apr_status_t crypto_key_cleanup(void *data)
270 apr_crypto_key_t *key = data;
272 PK11_FreeSymKey(key->symKey);
278 * @brief Clean encryption / decryption context.
279 * @note After cleanup, a context is free to be reused if necessary.
280 * @param f The context to use.
281 * @return Returns APR_ENOTIMPL if not supported.
283 static apr_status_t crypto_cleanup(apr_crypto_t *f)
288 static apr_status_t crypto_cleanup_helper(void *data)
290 apr_crypto_t *f = (apr_crypto_t *) data;
291 return crypto_cleanup(f);
295 * @brief Create a context for supporting encryption. Keys, certificates,
296 * algorithms and other parameters will be set per context. More than
297 * one context can be created at one time. A cleanup will be automatically
298 * registered with the given pool to guarantee a graceful shutdown.
299 * @param f - context pointer will be written here
300 * @param provider - provider to use
301 * @param params - parameter string
302 * @param pool - process pool
303 * @return APR_ENOENGINE when the engine specified does not exist. APR_EINITENGINE
304 * if the engine cannot be initialised.
306 static apr_status_t crypto_make(apr_crypto_t **ff,
307 const apr_crypto_driver_t *provider, const char *params,
310 apr_crypto_config_t *config = NULL;
313 f = apr_pcalloc(pool, sizeof(apr_crypto_t));
319 f->provider = provider;
320 config = f->config = apr_pcalloc(pool, sizeof(apr_crypto_config_t));
324 f->result = apr_pcalloc(pool, sizeof(apu_err_t));
329 f->types = apr_hash_make(pool);
333 apr_hash_set(f->types, "3des192", APR_HASH_KEY_STRING, &(key_types[0]));
334 apr_hash_set(f->types, "aes128", APR_HASH_KEY_STRING, &(key_types[1]));
335 apr_hash_set(f->types, "aes192", APR_HASH_KEY_STRING, &(key_types[2]));
336 apr_hash_set(f->types, "aes256", APR_HASH_KEY_STRING, &(key_types[3]));
338 f->modes = apr_hash_make(pool);
342 apr_hash_set(f->modes, "ecb", APR_HASH_KEY_STRING, &(key_modes[0]));
343 apr_hash_set(f->modes, "cbc", APR_HASH_KEY_STRING, &(key_modes[1]));
345 apr_pool_cleanup_register(pool, f, crypto_cleanup_helper,
346 apr_pool_cleanup_null);
353 * @brief Get a hash table of key types, keyed by the name of the type against
354 * a pointer to apr_crypto_block_key_type_t.
356 * @param types - hashtable of key types keyed to constants.
357 * @param f - encryption context
358 * @return APR_SUCCESS for success
360 static apr_status_t crypto_get_block_key_types(apr_hash_t **types,
361 const apr_crypto_t *f)
368 * @brief Get a hash table of key modes, keyed by the name of the mode against
369 * a pointer to apr_crypto_block_key_mode_t.
371 * @param modes - hashtable of key modes keyed to constants.
372 * @param f - encryption context
373 * @return APR_SUCCESS for success
375 static apr_status_t crypto_get_block_key_modes(apr_hash_t **modes,
376 const apr_crypto_t *f)
383 * Work out which mechanism to use.
385 static apr_status_t crypto_cipher_mechanism(apr_crypto_key_t *key,
386 const apr_crypto_block_key_type_e type,
387 const apr_crypto_block_key_mode_e mode, const int doPad)
390 /* decide on what cipher mechanism we will be using */
393 case (APR_KEY_3DES_192):
394 if (APR_MODE_CBC == mode) {
395 key->cipherOid = SEC_OID_DES_EDE3_CBC;
397 else if (APR_MODE_ECB == mode) {
398 return APR_ENOCIPHER;
399 /* No OID for CKM_DES3_ECB; */
403 case (APR_KEY_AES_128):
404 if (APR_MODE_CBC == mode) {
405 key->cipherOid = SEC_OID_AES_128_CBC;
408 key->cipherOid = SEC_OID_AES_128_ECB;
412 case (APR_KEY_AES_192):
413 if (APR_MODE_CBC == mode) {
414 key->cipherOid = SEC_OID_AES_192_CBC;
417 key->cipherOid = SEC_OID_AES_192_ECB;
421 case (APR_KEY_AES_256):
422 if (APR_MODE_CBC == mode) {
423 key->cipherOid = SEC_OID_AES_256_CBC;
426 key->cipherOid = SEC_OID_AES_256_ECB;
431 /* unknown key type, give up */
435 /* AES_128_CBC --> CKM_AES_CBC --> CKM_AES_CBC_PAD */
436 key->cipherMech = PK11_AlgtagToMechanism(key->cipherOid);
437 if (key->cipherMech == CKM_INVALID_MECHANISM) {
438 return APR_ENOCIPHER;
441 CK_MECHANISM_TYPE paddedMech;
442 paddedMech = PK11_GetPadMechanism(key->cipherMech);
443 if (CKM_INVALID_MECHANISM == paddedMech
444 || key->cipherMech == paddedMech) {
447 key->cipherMech = paddedMech;
450 key->ivSize = PK11_GetIVLength(key->cipherMech);
456 * @brief Create a key from the provided secret or passphrase. The key is cleaned
457 * up when the context is cleaned, and may be reused with multiple encryption
458 * or decryption operations.
459 * @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If
460 * *key is not NULL, *key must point at a previously created structure.
461 * @param key The key returned, see note.
462 * @param rec The key record, from which the key will be derived.
463 * @param f The context to use.
464 * @param p The pool to use.
465 * @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend
466 * error occurred while generating the key. APR_ENOCIPHER if the type or mode
467 * is not supported by the particular backend. APR_EKEYTYPE if the key type is
468 * not known. APR_EPADDING if padding was requested but is not supported.
469 * APR_ENOTIMPL if not implemented.
471 static apr_status_t crypto_key(apr_crypto_key_t **k,
472 const apr_crypto_key_rec_t *rec, const apr_crypto_t *f, apr_pool_t *p)
474 apr_status_t rv = APR_SUCCESS;
475 PK11SlotInfo *slot, *tslot;
484 SECAlgorithmID *algid;
485 void *wincx = NULL; /* what is wincx? */
486 apr_crypto_key_t *key;
492 *k = key = apr_pcalloc(p, sizeof *key);
496 apr_pool_cleanup_register(p, key, crypto_key_cleanup,
497 apr_pool_cleanup_null);
501 key->provider = f->provider;
503 /* decide on what cipher mechanism we will be using */
504 rv = crypto_cipher_mechanism(key, rec->type, rec->mode, rec->pad);
505 if (APR_SUCCESS != rv) {
509 switch (rec->ktype) {
511 case APR_CRYPTO_KTYPE_PASSPHRASE: {
513 /* Turn the raw passphrase and salt into SECItems */
514 passItem.data = (unsigned char*) rec->k.passphrase.pass;
515 passItem.len = rec->k.passphrase.passLen;
516 saltItem.data = (unsigned char*) rec->k.passphrase.salt;
517 saltItem.len = rec->k.passphrase.saltLen;
519 /* generate the key */
520 /* pbeAlg and cipherAlg are the same. */
521 algid = PK11_CreatePBEV2AlgorithmID(key->cipherOid, key->cipherOid,
522 SEC_OID_HMAC_SHA1, key->keyLength,
523 rec->k.passphrase.iterations, &saltItem);
525 slot = PK11_GetBestSlot(key->cipherMech, wincx);
527 key->symKey = PK11_PBEKeyGen(slot, algid, &passItem, PR_FALSE,
531 SECOID_DestroyAlgorithmID(algid, PR_TRUE);
537 case APR_CRYPTO_KTYPE_SECRET: {
540 * NSS is by default in FIPS mode, which disallows the use of unencrypted
541 * symmetrical keys. As per http://permalink.gmane.org/gmane.comp.mozilla.crypto/7947
542 * we do the following:
544 * 1. Generate a (temporary) symmetric key in NSS.
545 * 2. Use that symmetric key to encrypt your symmetric key as data.
546 * 3. Unwrap your wrapped symmetric key, using the symmetric key
547 * you generated in Step 1 as the unwrapping key.
549 * http://permalink.gmane.org/gmane.comp.mozilla.crypto/7947
552 /* generate the key */
553 slot = PK11_GetBestSlot(key->cipherMech, NULL);
555 unsigned char data[BUFFER_SIZE];
557 /* sanity check - key correct size? */
558 if (rec->k.secret.secretLen != key->keyLength) {
560 return APR_EKEYLENGTH;
563 tslot = PK11_GetBestSlot(CKM_AES_ECB, NULL);
566 /* generate a temporary wrapping key */
567 tkey = PK11_KeyGen(tslot, CKM_AES_ECB, 0, PK11_GetBestKeyLength(tslot, CKM_AES_ECB), 0);
569 /* prepare the key to wrap */
570 secretItem.data = (unsigned char *) rec->k.secret.secret;
571 secretItem.len = rec->k.secret.secretLen;
573 /* ensure our key matches the blocksize */
574 secParam = PK11_GenerateNewParam(CKM_AES_ECB, tkey);
575 blockSize = PK11_GetBlockSize(CKM_AES_ECB, secParam);
576 remainder = rec->k.secret.secretLen % blockSize;
579 apr_pcalloc(p, rec->k.secret.secretLen + remainder);
580 apr_crypto_clear(p, secretItem.data,
581 rec->k.secret.secretLen);
582 memcpy(secretItem.data, rec->k.secret.secret,
583 rec->k.secret.secretLen);
584 secretItem.len += remainder;
587 /* prepare a space for the wrapped key */
588 wrappedItem.data = data;
591 ctx = PK11_CreateContextBySymKey(CKM_AES_ECB, CKA_ENCRYPT, tkey,
594 s = PK11_CipherOp(ctx, wrappedItem.data,
595 (int *) (&wrappedItem.len), BUFFER_SIZE,
596 secretItem.data, secretItem.len);
597 if (s == SECSuccess) {
599 /* unwrap the key again */
600 key->symKey = PK11_UnwrapSymKeyWithFlags(tkey,
601 CKM_AES_ECB, NULL, &wrappedItem,
602 key->cipherMech, CKA_ENCRYPT,
603 rec->k.secret.secretLen, 0);
607 PK11_DestroyContext(ctx, PR_TRUE);
611 SECITEM_FreeItem(secParam, PR_TRUE);
612 PK11_FreeSymKey(tkey);
613 PK11_FreeSlot(tslot);
632 PRErrorCode perr = PORT_GetError();
634 f->result->rc = perr;
635 f->result->msg = PR_ErrorToName(perr);
644 * @brief Create a key from the given passphrase. By default, the PBKDF2
645 * algorithm is used to generate the key from the passphrase. It is expected
646 * that the same pass phrase will generate the same key, regardless of the
647 * backend crypto platform used. The key is cleaned up when the context
648 * is cleaned, and may be reused with multiple encryption or decryption
650 * @note If *key is NULL, a apr_crypto_key_t will be created from a pool. If
651 * *key is not NULL, *key must point at a previously created structure.
652 * @param key The key returned, see note.
653 * @param ivSize The size of the initialisation vector will be returned, based
654 * on whether an IV is relevant for this type of crypto.
655 * @param pass The passphrase to use.
656 * @param passLen The passphrase length in bytes
657 * @param salt The salt to use.
658 * @param saltLen The salt length in bytes
659 * @param type 3DES_192, AES_128, AES_192, AES_256.
660 * @param mode Electronic Code Book / Cipher Block Chaining.
661 * @param doPad Pad if necessary.
662 * @param iterations Iteration count
663 * @param f The context to use.
664 * @param p The pool to use.
665 * @return Returns APR_ENOKEY if the pass phrase is missing or empty, or if a backend
666 * error occurred while generating the key. APR_ENOCIPHER if the type or mode
667 * is not supported by the particular backend. APR_EKEYTYPE if the key type is
668 * not known. APR_EPADDING if padding was requested but is not supported.
669 * APR_ENOTIMPL if not implemented.
671 static apr_status_t crypto_passphrase(apr_crypto_key_t **k, apr_size_t *ivSize,
672 const char *pass, apr_size_t passLen, const unsigned char * salt,
673 apr_size_t saltLen, const apr_crypto_block_key_type_e type,
674 const apr_crypto_block_key_mode_e mode, const int doPad,
675 const int iterations, const apr_crypto_t *f, apr_pool_t *p)
677 apr_status_t rv = APR_SUCCESS;
681 SECAlgorithmID *algid;
682 void *wincx = NULL; /* what is wincx? */
683 apr_crypto_key_t *key = *k;
686 *k = key = apr_pcalloc(p, sizeof *key);
690 apr_pool_cleanup_register(p, key, crypto_key_cleanup,
691 apr_pool_cleanup_null);
695 key->provider = f->provider;
697 /* decide on what cipher mechanism we will be using */
698 rv = crypto_cipher_mechanism(key, type, mode, doPad);
699 if (APR_SUCCESS != rv) {
703 /* Turn the raw passphrase and salt into SECItems */
704 passItem.data = (unsigned char*) pass;
705 passItem.len = passLen;
706 saltItem.data = (unsigned char*) salt;
707 saltItem.len = saltLen;
709 /* generate the key */
710 /* pbeAlg and cipherAlg are the same. */
711 algid = PK11_CreatePBEV2AlgorithmID(key->cipherOid, key->cipherOid,
712 SEC_OID_HMAC_SHA1, key->keyLength, iterations, &saltItem);
714 slot = PK11_GetBestSlot(key->cipherMech, wincx);
716 key->symKey = PK11_PBEKeyGen(slot, algid, &passItem, PR_FALSE,
720 SECOID_DestroyAlgorithmID(algid, PR_TRUE);
725 PRErrorCode perr = PORT_GetError();
727 f->result->rc = perr;
728 f->result->msg = PR_ErrorToName(perr);
734 *ivSize = key->ivSize;
741 * @brief Initialise a context for encrypting arbitrary data using the given key.
742 * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
743 * *ctx is not NULL, *ctx must point at a previously created structure.
744 * @param ctx The block context returned, see note.
745 * @param iv Optional initialisation vector. If the buffer pointed to is NULL,
746 * an IV will be created at random, in space allocated from the pool.
747 * If the buffer pointed to is not NULL, the IV in the buffer will be
749 * @param key The key structure.
750 * @param blockSize The block size of the cipher.
751 * @param p The pool to use.
752 * @return Returns APR_ENOIV if an initialisation vector is required but not specified.
753 * Returns APR_EINIT if the backend failed to initialise the context. Returns
754 * APR_ENOTIMPL if not implemented.
756 static apr_status_t crypto_block_encrypt_init(apr_crypto_block_t **ctx,
757 const unsigned char **iv, const apr_crypto_key_t *key,
758 apr_size_t *blockSize, apr_pool_t *p)
762 unsigned char * usedIv;
763 apr_crypto_block_t *block = *ctx;
765 *ctx = block = apr_pcalloc(p, sizeof(apr_crypto_block_t));
772 block->provider = key->provider;
774 apr_pool_cleanup_register(p, block, crypto_block_cleanup_helper,
775 apr_pool_cleanup_null);
783 usedIv = apr_pcalloc(p, key->ivSize);
787 apr_crypto_clear(p, usedIv, key->ivSize);
788 s = PK11_GenerateRandom(usedIv, key->ivSize);
789 if (s != SECSuccess) {
795 usedIv = (unsigned char *) *iv;
797 ivItem.data = usedIv;
798 ivItem.len = key->ivSize;
799 block->secParam = PK11_ParamFromIV(key->cipherMech, &ivItem);
802 block->secParam = PK11_GenerateNewParam(key->cipherMech, key->symKey);
804 block->blockSize = PK11_GetBlockSize(key->cipherMech, block->secParam);
805 block->ctx = PK11_CreateContextBySymKey(key->cipherMech, CKA_ENCRYPT,
806 key->symKey, block->secParam);
808 /* did an error occur? */
809 perr = PORT_GetError();
810 if (perr || !block->ctx) {
811 key->f->result->rc = perr;
812 key->f->result->msg = PR_ErrorToName(perr);
817 *blockSize = PK11_GetBlockSize(key->cipherMech, block->secParam);
825 * @brief Encrypt data provided by in, write it to out.
826 * @note The number of bytes written will be written to outlen. If
827 * out is NULL, outlen will contain the maximum size of the
828 * buffer needed to hold the data, including any data
829 * generated by apr_crypto_block_encrypt_finish below. If *out points
830 * to NULL, a buffer sufficiently large will be created from
831 * the pool provided. If *out points to a not-NULL value, this
832 * value will be used as a buffer instead.
833 * @param out Address of a buffer to which data will be written,
835 * @param outlen Length of the output will be written here.
836 * @param in Address of the buffer to read.
837 * @param inlen Length of the buffer to read.
838 * @param ctx The block context to use.
839 * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
842 static apr_status_t crypto_block_encrypt(unsigned char **out,
843 apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
844 apr_crypto_block_t *block)
847 unsigned char *buffer;
848 int outl = (int) *outlen;
851 *outlen = inlen + block->blockSize;
855 buffer = apr_palloc(block->pool, inlen + block->blockSize);
859 apr_crypto_clear(block->pool, buffer, inlen + block->blockSize);
863 s = PK11_CipherOp(block->ctx, *out, &outl, inlen, (unsigned char*) in,
865 if (s != SECSuccess) {
866 PRErrorCode perr = PORT_GetError();
868 block->f->result->rc = perr;
869 block->f->result->msg = PR_ErrorToName(perr);
880 * @brief Encrypt final data block, write it to out.
881 * @note If necessary the final block will be written out after being
882 * padded. Typically the final block will be written to the
883 * same buffer used by apr_crypto_block_encrypt, offset by the
884 * number of bytes returned as actually written by the
885 * apr_crypto_block_encrypt() call. After this call, the context
886 * is cleaned and can be reused by apr_crypto_block_encrypt_init().
887 * @param out Address of a buffer to which data will be written. This
888 * buffer must already exist, and is usually the same
889 * buffer used by apr_evp_crypt(). See note.
890 * @param outlen Length of the output will be written here.
891 * @param ctx The block context to use.
892 * @return APR_ECRYPT if an error occurred.
893 * @return APR_EPADDING if padding was enabled and the block was incorrectly
895 * @return APR_ENOTIMPL if not implemented.
897 static apr_status_t crypto_block_encrypt_finish(unsigned char *out,
898 apr_size_t *outlen, apr_crypto_block_t *block)
901 apr_status_t rv = APR_SUCCESS;
902 unsigned int outl = *outlen;
904 SECStatus s = PK11_DigestFinal(block->ctx, out, &outl, block->blockSize);
907 if (s != SECSuccess) {
908 PRErrorCode perr = PORT_GetError();
910 block->f->result->rc = perr;
911 block->f->result->msg = PR_ErrorToName(perr);
915 crypto_block_cleanup(block);
922 * @brief Initialise a context for decrypting arbitrary data using the given key.
923 * @note If *ctx is NULL, a apr_crypto_block_t will be created from a pool. If
924 * *ctx is not NULL, *ctx must point at a previously created structure.
925 * @param ctx The block context returned, see note.
926 * @param blockSize The block size of the cipher.
927 * @param iv Optional initialisation vector. If the buffer pointed to is NULL,
928 * an IV will be created at random, in space allocated from the pool.
929 * If the buffer is not NULL, the IV in the buffer will be used.
930 * @param key The key structure.
931 * @param p The pool to use.
932 * @return Returns APR_ENOIV if an initialisation vector is required but not specified.
933 * Returns APR_EINIT if the backend failed to initialise the context. Returns
934 * APR_ENOTIMPL if not implemented.
936 static apr_status_t crypto_block_decrypt_init(apr_crypto_block_t **ctx,
937 apr_size_t *blockSize, const unsigned char *iv,
938 const apr_crypto_key_t *key, apr_pool_t *p)
941 apr_crypto_block_t *block = *ctx;
943 *ctx = block = apr_pcalloc(p, sizeof(apr_crypto_block_t));
950 block->provider = key->provider;
952 apr_pool_cleanup_register(p, block, crypto_block_cleanup_helper,
953 apr_pool_cleanup_null);
958 return APR_ENOIV; /* Cannot initialise without an IV */
960 ivItem.data = (unsigned char*) iv;
961 ivItem.len = key->ivSize;
962 block->secParam = PK11_ParamFromIV(key->cipherMech, &ivItem);
965 block->secParam = PK11_GenerateNewParam(key->cipherMech, key->symKey);
967 block->blockSize = PK11_GetBlockSize(key->cipherMech, block->secParam);
968 block->ctx = PK11_CreateContextBySymKey(key->cipherMech, CKA_DECRYPT,
969 key->symKey, block->secParam);
971 /* did an error occur? */
972 perr = PORT_GetError();
973 if (perr || !block->ctx) {
974 key->f->result->rc = perr;
975 key->f->result->msg = PR_ErrorToName(perr);
980 *blockSize = PK11_GetBlockSize(key->cipherMech, block->secParam);
988 * @brief Decrypt data provided by in, write it to out.
989 * @note The number of bytes written will be written to outlen. If
990 * out is NULL, outlen will contain the maximum size of the
991 * buffer needed to hold the data, including any data
992 * generated by apr_crypto_block_decrypt_finish below. If *out points
993 * to NULL, a buffer sufficiently large will be created from
994 * the pool provided. If *out points to a not-NULL value, this
995 * value will be used as a buffer instead.
996 * @param out Address of a buffer to which data will be written,
998 * @param outlen Length of the output will be written here.
999 * @param in Address of the buffer to read.
1000 * @param inlen Length of the buffer to read.
1001 * @param ctx The block context to use.
1002 * @return APR_ECRYPT if an error occurred. Returns APR_ENOTIMPL if
1005 static apr_status_t crypto_block_decrypt(unsigned char **out,
1006 apr_size_t *outlen, const unsigned char *in, apr_size_t inlen,
1007 apr_crypto_block_t *block)
1010 unsigned char *buffer;
1011 int outl = (int) *outlen;
1014 *outlen = inlen + block->blockSize;
1018 buffer = apr_palloc(block->pool, inlen + block->blockSize);
1022 apr_crypto_clear(block->pool, buffer, inlen + block->blockSize);
1026 s = PK11_CipherOp(block->ctx, *out, &outl, inlen, (unsigned char*) in,
1028 if (s != SECSuccess) {
1029 PRErrorCode perr = PORT_GetError();
1031 block->f->result->rc = perr;
1032 block->f->result->msg = PR_ErrorToName(perr);
1043 * @brief Decrypt final data block, write it to out.
1044 * @note If necessary the final block will be written out after being
1045 * padded. Typically the final block will be written to the
1046 * same buffer used by apr_crypto_block_decrypt, offset by the
1047 * number of bytes returned as actually written by the
1048 * apr_crypto_block_decrypt() call. After this call, the context
1049 * is cleaned and can be reused by apr_crypto_block_decrypt_init().
1050 * @param out Address of a buffer to which data will be written. This
1051 * buffer must already exist, and is usually the same
1052 * buffer used by apr_evp_crypt(). See note.
1053 * @param outlen Length of the output will be written here.
1054 * @param ctx The block context to use.
1055 * @return APR_ECRYPT if an error occurred.
1056 * @return APR_EPADDING if padding was enabled and the block was incorrectly
1058 * @return APR_ENOTIMPL if not implemented.
1060 static apr_status_t crypto_block_decrypt_finish(unsigned char *out,
1061 apr_size_t *outlen, apr_crypto_block_t *block)
1064 apr_status_t rv = APR_SUCCESS;
1065 unsigned int outl = *outlen;
1067 SECStatus s = PK11_DigestFinal(block->ctx, out, &outl, block->blockSize);
1070 if (s != SECSuccess) {
1071 PRErrorCode perr = PORT_GetError();
1073 block->f->result->rc = perr;
1074 block->f->result->msg = PR_ErrorToName(perr);
1078 crypto_block_cleanup(block);
1087 APU_MODULE_DECLARE_DATA const apr_crypto_driver_t apr_crypto_nss_driver = {
1088 "nss", crypto_init, crypto_make, crypto_get_block_key_types,
1089 crypto_get_block_key_modes, crypto_passphrase,
1090 crypto_block_encrypt_init, crypto_block_encrypt,
1091 crypto_block_encrypt_finish, crypto_block_decrypt_init,
1092 crypto_block_decrypt, crypto_block_decrypt_finish,
1093 crypto_block_cleanup, crypto_cleanup, crypto_shutdown, crypto_error,