Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: nss/lib/softoken/pkcs11c.c

Issue 1843333003: Update NSPR to 4.12 and NSS to 3.23 on iOS (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/nss.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* This Source Code Form is subject to the terms of the Mozilla Public 1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 /* 4 /*
5 * This file implements PKCS 11 on top of our existing security modules 5 * This file implements PKCS 11 on top of our existing security modules
6 * 6 *
7 * For more information about PKCS 11 See PKCS 11 Token Inteface Standard. 7 * For more information about PKCS 11 See PKCS 11 Token Inteface Standard.
8 * This implementation has two slots: 8 * This implementation has two slots:
9 * slot 1 is our generic crypto support. It does not require login. 9 * slot 1 is our generic crypto support. It does not require login.
10 * It supports Public Key ops, and all they bulk ciphers and hashes. 10 * It supports Public Key ops, and all they bulk ciphers and hashes.
(...skipping 18 matching lines...) Expand all
29 #include "secdig.h" 29 #include "secdig.h"
30 #include "lowpbe.h" /* We do PBE below */ 30 #include "lowpbe.h" /* We do PBE below */
31 #include "pkcs11t.h" 31 #include "pkcs11t.h"
32 #include "secoid.h" 32 #include "secoid.h"
33 #include "alghmac.h" 33 #include "alghmac.h"
34 #include "softoken.h" 34 #include "softoken.h"
35 #include "secasn1.h" 35 #include "secasn1.h"
36 #include "secerr.h" 36 #include "secerr.h"
37 37
38 #include "prprf.h" 38 #include "prprf.h"
39 #include "prenv.h"
39 40
40 #define __PASTE(x,y) x##y 41 #define __PASTE(x,y) x##y
41 42
42 /* 43 /*
43 * we renamed all our internal functions, get the correct 44 * we renamed all our internal functions, get the correct
44 * definitions for them... 45 * definitions for them...
45 */ 46 */
46 #undef CK_PKCS11_FUNCTION_INFO 47 #undef CK_PKCS11_FUNCTION_INFO
47 #undef CK_NEED_ARG_LIST 48 #undef CK_NEED_ARG_LIST
48 49
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 info->params->ulSourceDataLen, 660 info->params->ulSourceDataLen,
660 output, outputLen, maxLen, input, inputLen); 661 output, outputLen, maxLen, input, inputLen);
661 if (rv != SECSuccess && PORT_GetError() == SEC_ERROR_LIBRARY_FAILURE) { 662 if (rv != SECSuccess && PORT_GetError() == SEC_ERROR_LIBRARY_FAILURE) {
662 sftk_fatalError = PR_TRUE; 663 sftk_fatalError = PR_TRUE;
663 } 664 }
664 return rv; 665 return rv;
665 } 666 }
666 667
667 static SFTKChaCha20Poly1305Info * 668 static SFTKChaCha20Poly1305Info *
668 sftk_ChaCha20Poly1305_CreateContext(const unsigned char *key, 669 sftk_ChaCha20Poly1305_CreateContext(const unsigned char *key,
669 » » » » unsigned int keyLen, 670 unsigned int keyLen,
670 » » » » const CK_NSS_AEAD_PARAMS* params) 671 const CK_NSS_AEAD_PARAMS *params)
671 { 672 {
672 SFTKChaCha20Poly1305Info *ctx; 673 SFTKChaCha20Poly1305Info *ctx;
673 674
674 if (params->ulIvLen != sizeof(ctx->nonce)) { 675 if (params->ulNonceLen != sizeof(ctx->nonce)) {
675 » PORT_SetError(SEC_ERROR_INPUT_LEN); 676 PORT_SetError(SEC_ERROR_INPUT_LEN);
676 » return NULL; 677 return NULL;
677 } 678 }
678 679
679 ctx = PORT_New(SFTKChaCha20Poly1305Info); 680 ctx = PORT_New(SFTKChaCha20Poly1305Info);
680 if (ctx == NULL) { 681 if (ctx == NULL) {
681 » return NULL; 682 return NULL;
682 } 683 }
683 684
684 if (ChaCha20Poly1305_InitContext(&ctx->freeblCtx, key, keyLen, 685 if (ChaCha20Poly1305_InitContext(&ctx->freeblCtx, key, keyLen,
685 » » » » params->ulTagLen) != SECSuccess) { 686 params->ulTagLen) != SECSuccess) {
686 » PORT_Free(ctx); 687 PORT_Free(ctx);
687 » return NULL; 688 return NULL;
688 } 689 }
689 690
690 memcpy(ctx->nonce, params->pIv, sizeof(ctx->nonce)); 691 PORT_Memcpy(ctx->nonce, params->pNonce, sizeof(ctx->nonce));
691 692
692 if (params->ulAADLen > sizeof(ctx->ad)) { 693 if (params->ulAADLen > sizeof(ctx->ad)) {
693 » /* Need to allocate an overflow buffer for the additional data. */ 694 /* Need to allocate an overflow buffer for the additional data. */
694 » ctx->adOverflow = (unsigned char *)PORT_Alloc(params->ulAADLen); 695 ctx->adOverflow = (unsigned char *)PORT_Alloc(params->ulAADLen);
695 » if (!ctx->adOverflow) { 696 if (!ctx->adOverflow) {
696 » PORT_Free(ctx); 697 PORT_Free(ctx);
697 » return NULL; 698 return NULL;
698 » } 699 }
699 » memcpy(ctx->adOverflow, params->pAAD, params->ulAADLen); 700 PORT_Memcpy(ctx->adOverflow, params->pAAD, params->ulAADLen);
700 } else { 701 } else {
701 » ctx->adOverflow = NULL; 702 ctx->adOverflow = NULL;
702 » memcpy(ctx->ad, params->pAAD, params->ulAADLen); 703 PORT_Memcpy(ctx->ad, params->pAAD, params->ulAADLen);
703 } 704 }
704 ctx->adLen = params->ulAADLen; 705 ctx->adLen = params->ulAADLen;
705 706
706 return ctx; 707 return ctx;
707 } 708 }
708 709
709 static void 710 static void
710 sftk_ChaCha20Poly1305_DestroyContext(SFTKChaCha20Poly1305Info *ctx, 711 sftk_ChaCha20Poly1305_DestroyContext(SFTKChaCha20Poly1305Info *ctx,
711 » » » » PRBool freeit) 712 PRBool freeit)
712 { 713 {
713 ChaCha20Poly1305_DestroyContext(&ctx->freeblCtx, PR_FALSE); 714 ChaCha20Poly1305_DestroyContext(&ctx->freeblCtx, PR_FALSE);
714 if (ctx->adOverflow != NULL) { 715 if (ctx->adOverflow != NULL) {
715 » PORT_Free(ctx->adOverflow); 716 PORT_Free(ctx->adOverflow);
716 » ctx->adOverflow = NULL; 717 ctx->adOverflow = NULL;
717 } 718 }
718 ctx->adLen = 0; 719 ctx->adLen = 0;
719 if (freeit) { 720 if (freeit) {
720 » PORT_Free(ctx); 721 PORT_Free(ctx);
721 } 722 }
722 } 723 }
723 724
724 static SECStatus 725 static SECStatus
725 sftk_ChaCha20Poly1305_Encrypt(const SFTKChaCha20Poly1305Info *ctx, 726 sftk_ChaCha20Poly1305_Encrypt(const SFTKChaCha20Poly1305Info *ctx,
726 » » » unsigned char *output, unsigned int *outputLen, 727 unsigned char *output, unsigned int *outputLen,
727 » » » unsigned int maxOutputLen, 728 unsigned int maxOutputLen,
728 » » » const unsigned char *input, unsigned int inputLen) 729 const unsigned char *input, unsigned int inputLen)
729 { 730 {
730 const unsigned char *ad = ctx->adOverflow; 731 const unsigned char *ad = ctx->adOverflow;
731 732
732 if (ad == NULL) { 733 if (ad == NULL) {
733 » ad = ctx->ad; 734 ad = ctx->ad;
734 } 735 }
735 736
736 return ChaCha20Poly1305_Seal(&ctx->freeblCtx, output, outputLen, 737 return ChaCha20Poly1305_Seal(&ctx->freeblCtx, output, outputLen,
737 » » » » maxOutputLen, input, inputLen, ctx->nonce, 738 maxOutputLen, input, inputLen, ctx->nonce,
738 » » » » sizeof(ctx->nonce), ad, ctx->adLen); 739 sizeof(ctx->nonce), ad, ctx->adLen);
739 } 740 }
740 741
741 static SECStatus 742 static SECStatus
742 sftk_ChaCha20Poly1305_Decrypt(const SFTKChaCha20Poly1305Info *ctx, 743 sftk_ChaCha20Poly1305_Decrypt(const SFTKChaCha20Poly1305Info *ctx,
743 » » » unsigned char *output, unsigned int *outputLen, 744 unsigned char *output, unsigned int *outputLen,
744 » » » unsigned int maxOutputLen, 745 unsigned int maxOutputLen,
745 » » » const unsigned char *input, unsigned int inputLen) 746 const unsigned char *input, unsigned int inputLen)
746 { 747 {
747 const unsigned char *ad = ctx->adOverflow; 748 const unsigned char *ad = ctx->adOverflow;
748 749
749 if (ad == NULL) { 750 if (ad == NULL) {
750 » ad = ctx->ad; 751 ad = ctx->ad;
751 } 752 }
752 753
753 return ChaCha20Poly1305_Open(&ctx->freeblCtx, output, outputLen, 754 return ChaCha20Poly1305_Open(&ctx->freeblCtx, output, outputLen,
754 » » » » maxOutputLen, input, inputLen, ctx->nonce, 755 maxOutputLen, input, inputLen, ctx->nonce,
755 » » » » sizeof(ctx->nonce), ad, ctx->adLen); 756 sizeof(ctx->nonce), ad, ctx->adLen);
756 } 757 }
757 758
758 /** NSC_CryptInit initializes an encryption/Decryption operation. 759 /** NSC_CryptInit initializes an encryption/Decryption operation.
759 * 760 *
760 * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey. 761 * Always called by NSC_EncryptInit, NSC_DecryptInit, NSC_WrapKey,NSC_UnwrapKey.
761 * Called by NSC_SignInit, NSC_VerifyInit (via sftk_InitCBCMac) only for block 762 * Called by NSC_SignInit, NSC_VerifyInit (via sftk_InitCBCMac) only for block
762 * ciphers MAC'ing. 763 * ciphers MAC'ing.
763 */ 764 */
764 static CK_RV 765 static CK_RV
765 sftk_CryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, 766 sftk_CryptInit(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
965 case CKM_CDMF_CBC_PAD: 966 case CKM_CDMF_CBC_PAD:
966 context->doPad = PR_TRUE; 967 context->doPad = PR_TRUE;
967 /* fall thru */ 968 /* fall thru */
968 case CKM_CDMF_ECB: 969 case CKM_CDMF_ECB:
969 case CKM_CDMF_CBC: 970 case CKM_CDMF_CBC:
970 if (key_type != CKK_CDMF) { 971 if (key_type != CKK_CDMF) {
971 crv = CKR_KEY_TYPE_INCONSISTENT; 972 crv = CKR_KEY_TYPE_INCONSISTENT;
972 break; 973 break;
973 } 974 }
974 t = (pMechanism->mechanism == CKM_CDMF_ECB) ? NSS_DES : NSS_DES_CBC; 975 t = (pMechanism->mechanism == CKM_CDMF_ECB) ? NSS_DES : NSS_DES_CBC;
975 if (crv != CKR_OK) break;
976 goto finish_des; 976 goto finish_des;
977 case CKM_DES_ECB: 977 case CKM_DES_ECB:
978 if (key_type != CKK_DES) { 978 if (key_type != CKK_DES) {
979 crv = CKR_KEY_TYPE_INCONSISTENT; 979 crv = CKR_KEY_TYPE_INCONSISTENT;
980 break; 980 break;
981 } 981 }
982 t = NSS_DES; 982 t = NSS_DES;
983 goto finish_des; 983 goto finish_des;
984 case CKM_DES_CBC_PAD: 984 case CKM_DES_CBC_PAD:
985 context->doPad = PR_TRUE; 985 context->doPad = PR_TRUE;
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 break; 1164 break;
1165 } 1165 }
1166 context->cipherInfo = sftk_ChaCha20Poly1305_CreateContext( 1166 context->cipherInfo = sftk_ChaCha20Poly1305_CreateContext(
1167 (unsigned char*) att->attrib.pValue, att->attrib.ulValueLen, 1167 (unsigned char*) att->attrib.pValue, att->attrib.ulValueLen,
1168 (CK_NSS_AEAD_PARAMS*) pMechanism->pParameter); 1168 (CK_NSS_AEAD_PARAMS*) pMechanism->pParameter);
1169 sftk_FreeAttribute(att); 1169 sftk_FreeAttribute(att);
1170 if (context->cipherInfo == NULL) { 1170 if (context->cipherInfo == NULL) {
1171 crv = sftk_MapCryptError(PORT_GetError()); 1171 crv = sftk_MapCryptError(PORT_GetError());
1172 break; 1172 break;
1173 } 1173 }
1174 » context->update = (SFTKCipher) (isEncrypt ? 1174 » context->update = (SFTKCipher) (isEncrypt ? sftk_ChaCha20Poly1305_Encryp t :
1175 » » » » » sftk_ChaCha20Poly1305_Encrypt :
1176 sftk_ChaCha20Poly1305_Decrypt); 1175 sftk_ChaCha20Poly1305_Decrypt);
1177 context->destroy = (SFTKDestroy) sftk_ChaCha20Poly1305_DestroyContext; 1176 context->destroy = (SFTKDestroy) sftk_ChaCha20Poly1305_DestroyContext;
1178 break; 1177 break;
1179 1178
1180 case CKM_NETSCAPE_AES_KEY_WRAP_PAD: 1179 case CKM_NETSCAPE_AES_KEY_WRAP_PAD:
1181 context->doPad = PR_TRUE; 1180 context->doPad = PR_TRUE;
1182 /* fall thru */ 1181 /* fall thru */
1183 case CKM_NETSCAPE_AES_KEY_WRAP: 1182 case CKM_NETSCAPE_AES_KEY_WRAP:
1184 context->multi = PR_FALSE; 1183 context->multi = PR_FALSE;
1185 context->blockSize = 8; 1184 context->blockSize = 8;
(...skipping 1023 matching lines...) Expand 10 before | Expand all | Expand 10 after
2209 2208
2210 return RSA_HashSign(info->hashOid, info->key, sig, sigLen, maxLen, 2209 return RSA_HashSign(info->hashOid, info->key, sig, sigLen, maxLen,
2211 hash, hashLen); 2210 hash, hashLen);
2212 } 2211 }
2213 2212
2214 /* XXX Old template; want to expunge it eventually. */ 2213 /* XXX Old template; want to expunge it eventually. */
2215 static DERTemplate SECAlgorithmIDTemplate[] = { 2214 static DERTemplate SECAlgorithmIDTemplate[] = {
2216 { DER_SEQUENCE, 2215 { DER_SEQUENCE,
2217 0, NULL, sizeof(SECAlgorithmID) }, 2216 0, NULL, sizeof(SECAlgorithmID) },
2218 { DER_OBJECT_ID, 2217 { DER_OBJECT_ID,
2219 » offsetof(SECAlgorithmID,algorithm), }, 2218 » offsetof(SECAlgorithmID,algorithm) },
2220 { DER_OPTIONAL | DER_ANY, 2219 { DER_OPTIONAL | DER_ANY,
2221 » offsetof(SECAlgorithmID,parameters), }, 2220 » offsetof(SECAlgorithmID,parameters) },
2222 { 0, } 2221 { 0 }
2223 }; 2222 };
2224 2223
2225 /* 2224 /*
2226 * XXX OLD Template. Once all uses have been switched over to new one, 2225 * XXX OLD Template. Once all uses have been switched over to new one,
2227 * remove this. 2226 * remove this.
2228 */ 2227 */
2229 static DERTemplate SGNDigestInfoTemplate[] = { 2228 static DERTemplate SGNDigestInfoTemplate[] = {
2230 { DER_SEQUENCE, 2229 { DER_SEQUENCE,
2231 0, NULL, sizeof(SGNDigestInfo) }, 2230 0, NULL, sizeof(SGNDigestInfo) },
2232 { DER_INLINE, 2231 { DER_INLINE,
2233 offsetof(SGNDigestInfo,digestAlgorithm), 2232 offsetof(SGNDigestInfo,digestAlgorithm),
2234 » SECAlgorithmIDTemplate, }, 2233 » SECAlgorithmIDTemplate },
2235 { DER_OCTET_STRING, 2234 { DER_OCTET_STRING,
2236 » offsetof(SGNDigestInfo,digest), }, 2235 » offsetof(SGNDigestInfo,digest) },
2237 { 0, } 2236 { 0 }
2238 }; 2237 };
2239 2238
2240 /* 2239 /*
2241 * encode RSA PKCS #1 Signature data before signing... 2240 * encode RSA PKCS #1 Signature data before signing...
2242 */ 2241 */
2243 SECStatus 2242 SECStatus
2244 RSA_HashSign(SECOidTag hashOid, NSSLOWKEYPrivateKey *key, 2243 RSA_HashSign(SECOidTag hashOid, NSSLOWKEYPrivateKey *key,
2245 unsigned char *sig, unsigned int *sigLen, unsigned int maxLen, 2244 unsigned char *sig, unsigned int *sigLen, unsigned int maxLen,
2246 const unsigned char *hash, unsigned int hashLen) 2245 const unsigned char *hash, unsigned int hashLen)
2247 { 2246 {
(...skipping 1604 matching lines...) Expand 10 before | Expand all | Expand 10 after
3852 3851
3853 /* maybe this should be table driven? */ 3852 /* maybe this should be table driven? */
3854 static CK_RV 3853 static CK_RV
3855 nsc_SetupPBEKeyGen(CK_MECHANISM_PTR pMechanism, NSSPKCS5PBEParameter **pbe, 3854 nsc_SetupPBEKeyGen(CK_MECHANISM_PTR pMechanism, NSSPKCS5PBEParameter **pbe,
3856 CK_KEY_TYPE *key_type, CK_ULONG *key_length) 3855 CK_KEY_TYPE *key_type, CK_ULONG *key_length)
3857 { 3856 {
3858 CK_RV crv = CKR_OK; 3857 CK_RV crv = CKR_OK;
3859 SECOidData *oid; 3858 SECOidData *oid;
3860 CK_PBE_PARAMS *pbe_params = NULL; 3859 CK_PBE_PARAMS *pbe_params = NULL;
3861 NSSPKCS5PBEParameter *params = NULL; 3860 NSSPKCS5PBEParameter *params = NULL;
3861 HASH_HashType hashType = HASH_AlgSHA1;
3862 CK_PKCS5_PBKD2_PARAMS *pbkd2_params = NULL; 3862 CK_PKCS5_PBKD2_PARAMS *pbkd2_params = NULL;
3863 SECItem salt; 3863 SECItem salt;
3864 CK_ULONG iteration = 0; 3864 CK_ULONG iteration = 0;
3865 3865
3866 *pbe = NULL; 3866 *pbe = NULL;
3867 3867
3868 oid = SECOID_FindOIDByMechanism(pMechanism->mechanism); 3868 oid = SECOID_FindOIDByMechanism(pMechanism->mechanism);
3869 if (oid == NULL) { 3869 if (oid == NULL) {
3870 return CKR_MECHANISM_INVALID; 3870 return CKR_MECHANISM_INVALID;
3871 } 3871 }
3872 3872
3873 if (pMechanism->mechanism == CKM_PKCS5_PBKD2) { 3873 if (pMechanism->mechanism == CKM_PKCS5_PBKD2) {
3874 pbkd2_params = (CK_PKCS5_PBKD2_PARAMS *)pMechanism->pParameter; 3874 pbkd2_params = (CK_PKCS5_PBKD2_PARAMS *)pMechanism->pParameter;
3875 if (pbkd2_params == NULL) {
3876 return CKR_MECHANISM_PARAM_INVALID;
3877 }
3878 switch (pbkd2_params->prf) {
3879 case CKP_PKCS5_PBKD2_HMAC_SHA1:
3880 hashType = HASH_AlgSHA1;
3881 break;
3882 case CKP_PKCS5_PBKD2_HMAC_SHA224:
3883 hashType = HASH_AlgSHA224;
3884 break;
3885 case CKP_PKCS5_PBKD2_HMAC_SHA256:
3886 hashType = HASH_AlgSHA256;
3887 break;
3888 case CKP_PKCS5_PBKD2_HMAC_SHA384:
3889 hashType = HASH_AlgSHA384;
3890 break;
3891 case CKP_PKCS5_PBKD2_HMAC_SHA512:
3892 hashType = HASH_AlgSHA512;
3893 break;
3894 default:
3895 return CKR_MECHANISM_PARAM_INVALID;
3896 }
3875 if (pbkd2_params->saltSource != CKZ_SALT_SPECIFIED) { 3897 if (pbkd2_params->saltSource != CKZ_SALT_SPECIFIED) {
3876 return CKR_MECHANISM_PARAM_INVALID; 3898 return CKR_MECHANISM_PARAM_INVALID;
3877 } 3899 }
3878 salt.data = (unsigned char *)pbkd2_params->pSaltSourceData; 3900 salt.data = (unsigned char *)pbkd2_params->pSaltSourceData;
3879 salt.len = (unsigned int)pbkd2_params->ulSaltSourceDataLen; 3901 salt.len = (unsigned int)pbkd2_params->ulSaltSourceDataLen;
3880 iteration = pbkd2_params->iterations; 3902 iteration = pbkd2_params->iterations;
3881 } else { 3903 } else {
3882 pbe_params = (CK_PBE_PARAMS *)pMechanism->pParameter; 3904 pbe_params = (CK_PBE_PARAMS *)pMechanism->pParameter;
3883 salt.data = (unsigned char *)pbe_params->pSalt; 3905 salt.data = (unsigned char *)pbe_params->pSalt;
3884 salt.len = (unsigned int)pbe_params->ulSaltLen; 3906 salt.len = (unsigned int)pbe_params->ulSaltLen;
3885 iteration = pbe_params->ulIteration; 3907 iteration = pbe_params->ulIteration;
3886 } 3908 }
3887 params=nsspkcs5_NewParam(oid->offset, &salt, iteration); 3909 params=nsspkcs5_NewParam(oid->offset, hashType, &salt, iteration);
3888 if (params == NULL) { 3910 if (params == NULL) {
3889 return CKR_MECHANISM_INVALID; 3911 return CKR_MECHANISM_INVALID;
3890 } 3912 }
3891 3913
3892 switch (params->encAlg) { 3914 switch (params->encAlg) {
3893 case SEC_OID_DES_CBC: 3915 case SEC_OID_DES_CBC:
3894 *key_type = CKK_DES; 3916 *key_type = CKK_DES;
3895 *key_length = params->keyLen; 3917 *key_length = params->keyLen;
3896 break; 3918 break;
3897 case SEC_OID_DES_EDE3_CBC: 3919 case SEC_OID_DES_EDE3_CBC:
3898 *key_type = params->is2KeyDES ? CKK_DES2 : CKK_DES3; 3920 *key_type = params->is2KeyDES ? CKK_DES2 : CKK_DES3;
3899 *key_length = params->keyLen; 3921 *key_length = params->keyLen;
3900 break; 3922 break;
3901 case SEC_OID_RC2_CBC: 3923 case SEC_OID_RC2_CBC:
3902 *key_type = CKK_RC2; 3924 *key_type = CKK_RC2;
3903 *key_length = params->keyLen; 3925 *key_length = params->keyLen;
3904 break; 3926 break;
3905 case SEC_OID_RC4: 3927 case SEC_OID_RC4:
3906 *key_type = CKK_RC4; 3928 *key_type = CKK_RC4;
3907 *key_length = params->keyLen; 3929 *key_length = params->keyLen;
3908 break; 3930 break;
3909 case SEC_OID_PKCS5_PBKDF2: 3931 case SEC_OID_PKCS5_PBKDF2:
3910 /* sigh, PKCS #11 currently only defines SHA1 for the KDF hash type.
3911 * we do the check here because this where we would handle multiple
3912 * hash types in the future */
3913 if (pbkd2_params == NULL ||
3914 pbkd2_params->prf != CKP_PKCS5_PBKD2_HMAC_SHA1) {
3915 crv = CKR_MECHANISM_PARAM_INVALID;
3916 break;
3917 }
3918 /* key type must already be set */ 3932 /* key type must already be set */
3919 if (*key_type == CKK_INVALID_KEY_TYPE) { 3933 if (*key_type == CKK_INVALID_KEY_TYPE) {
3920 crv = CKR_TEMPLATE_INCOMPLETE; 3934 crv = CKR_TEMPLATE_INCOMPLETE;
3921 break; 3935 break;
3922 } 3936 }
3923 /* PBKDF2 needs to calculate the key length from the other parameters 3937 /* PBKDF2 needs to calculate the key length from the other parameters
3924 */ 3938 */
3925 if (*key_length == 0) { 3939 if (*key_length == 0) {
3926 *key_length = sftk_MapKeySize(*key_type); 3940 *key_length = sftk_MapKeySize(*key_type);
3927 } 3941 }
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
4159 if (session == NULL) { 4173 if (session == NULL) {
4160 sftk_FreeObject(key); 4174 sftk_FreeObject(key);
4161 return CKR_SESSION_HANDLE_INVALID; 4175 return CKR_SESSION_HANDLE_INVALID;
4162 } 4176 }
4163 4177
4164 /* 4178 /*
4165 * handle the base object stuff 4179 * handle the base object stuff
4166 */ 4180 */
4167 crv = sftk_handleObject(key,session); 4181 crv = sftk_handleObject(key,session);
4168 sftk_FreeSession(session); 4182 sftk_FreeSession(session);
4169 if (sftk_isTrue(key,CKA_SENSITIVE)) { 4183 if (crv == CKR_OK && sftk_isTrue(key,CKA_SENSITIVE)) {
4170 » sftk_forceAttribute(key,CKA_ALWAYS_SENSITIVE,&cktrue,sizeof(CK_BBOOL)); 4184 » crv = sftk_forceAttribute(key,CKA_ALWAYS_SENSITIVE,&cktrue,sizeof(CK_BBO OL));
4171 } 4185 }
4172 if (!sftk_isTrue(key,CKA_EXTRACTABLE)) { 4186 if (crv == CKR_OK && !sftk_isTrue(key,CKA_EXTRACTABLE)) {
4173 » sftk_forceAttribute(key,CKA_NEVER_EXTRACTABLE,&cktrue,sizeof(CK_BBOOL)); 4187 » crv = sftk_forceAttribute(key,CKA_NEVER_EXTRACTABLE,&cktrue,sizeof(CK_BB OOL));
4174 } 4188 }
4175 4189 if (crv == CKR_OK) {
4176 *phKey = key->handle; 4190 » *phKey = key->handle;
4191 }
4177 sftk_FreeObject(key); 4192 sftk_FreeObject(key);
4178 return crv; 4193 return crv;
4179 } 4194 }
4180 4195
4181 #define PAIRWISE_DIGEST_LENGTH SHA1_LENGTH /* 160-bits */ 4196 #define PAIRWISE_DIGEST_LENGTH SHA1_LENGTH /* 160-bits */
4182 #define PAIRWISE_MESSAGE_LENGTH 20 /* 160-bits */ 4197 #define PAIRWISE_MESSAGE_LENGTH 20 /* 160-bits */
4183 4198
4184 /* 4199 /*
4185 * FIPS 140-2 pairwise consistency check utilized to validate key pair. 4200 * FIPS 140-2 pairwise consistency check utilized to validate key pair.
4186 * 4201 *
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after
4873 rv = EC_NewKey(ecParams, &ecPriv); 4888 rv = EC_NewKey(ecParams, &ecPriv);
4874 PORT_FreeArena(ecParams->arena, PR_TRUE); 4889 PORT_FreeArena(ecParams->arena, PR_TRUE);
4875 if (rv != SECSuccess) { 4890 if (rv != SECSuccess) {
4876 if (PORT_GetError() == SEC_ERROR_LIBRARY_FAILURE) { 4891 if (PORT_GetError() == SEC_ERROR_LIBRARY_FAILURE) {
4877 sftk_fatalError = PR_TRUE; 4892 sftk_fatalError = PR_TRUE;
4878 } 4893 }
4879 crv = sftk_MapCryptError(PORT_GetError()); 4894 crv = sftk_MapCryptError(PORT_GetError());
4880 break; 4895 break;
4881 } 4896 }
4882 4897
4883 » if (getenv("NSS_USE_DECODED_CKA_EC_POINT")) { 4898 » if (PR_GetEnvSecure("NSS_USE_DECODED_CKA_EC_POINT")) {
4884 crv = sftk_AddAttributeType(publicKey, CKA_EC_POINT, 4899 crv = sftk_AddAttributeType(publicKey, CKA_EC_POINT,
4885 sftk_item_expand(&ecPriv->publicValue)); 4900 sftk_item_expand(&ecPriv->publicValue));
4886 } else { 4901 } else {
4887 SECItem *pubValue = SEC_ASN1EncodeItem(NULL, NULL, 4902 SECItem *pubValue = SEC_ASN1EncodeItem(NULL, NULL,
4888 &ecPriv->publicValue, 4903 &ecPriv->publicValue,
4889 SEC_ASN1_GET(SEC_OctetStringTemplate)); 4904 SEC_ASN1_GET(SEC_OctetStringTemplate));
4890 if (!pubValue) { 4905 if (!pubValue) {
4891 crv = CKR_ARGUMENTS_BAD; 4906 crv = CKR_ARGUMENTS_BAD;
4892 goto ecgn_done; 4907 goto ecgn_done;
4893 } 4908 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
4965 */ 4980 */
4966 crv = sftk_handleObject(publicKey,session); 4981 crv = sftk_handleObject(publicKey,session);
4967 sftk_FreeSession(session); 4982 sftk_FreeSession(session);
4968 if (crv != CKR_OK) { 4983 if (crv != CKR_OK) {
4969 sftk_FreeObject(publicKey); 4984 sftk_FreeObject(publicKey);
4970 NSC_DestroyObject(hSession,privateKey->handle); 4985 NSC_DestroyObject(hSession,privateKey->handle);
4971 sftk_FreeObject(privateKey); 4986 sftk_FreeObject(privateKey);
4972 return crv; 4987 return crv;
4973 } 4988 }
4974 if (sftk_isTrue(privateKey,CKA_SENSITIVE)) { 4989 if (sftk_isTrue(privateKey,CKA_SENSITIVE)) {
4975 » sftk_forceAttribute(privateKey,CKA_ALWAYS_SENSITIVE, 4990 » crv = sftk_forceAttribute(privateKey,CKA_ALWAYS_SENSITIVE,
4976 &cktrue,sizeof(CK_BBOOL)); 4991 &cktrue,sizeof(CK_BBOOL));
4977 } 4992 }
4978 if (sftk_isTrue(publicKey,CKA_SENSITIVE)) { 4993 if (crv == CKR_OK && sftk_isTrue(publicKey,CKA_SENSITIVE)) {
4979 » sftk_forceAttribute(publicKey,CKA_ALWAYS_SENSITIVE, 4994 » crv = sftk_forceAttribute(publicKey,CKA_ALWAYS_SENSITIVE,
4980 &cktrue,sizeof(CK_BBOOL)); 4995 &cktrue,sizeof(CK_BBOOL));
4981 } 4996 }
4982 if (!sftk_isTrue(privateKey,CKA_EXTRACTABLE)) { 4997 if (crv == CKR_OK && !sftk_isTrue(privateKey,CKA_EXTRACTABLE)) {
4983 » sftk_forceAttribute(privateKey,CKA_NEVER_EXTRACTABLE, 4998 » crv = sftk_forceAttribute(privateKey,CKA_NEVER_EXTRACTABLE,
4984 &cktrue,sizeof(CK_BBOOL)); 4999 &cktrue,sizeof(CK_BBOOL));
4985 } 5000 }
4986 if (!sftk_isTrue(publicKey,CKA_EXTRACTABLE)) { 5001 if (crv == CKR_OK && !sftk_isTrue(publicKey,CKA_EXTRACTABLE)) {
4987 » sftk_forceAttribute(publicKey,CKA_NEVER_EXTRACTABLE, 5002 » crv = sftk_forceAttribute(publicKey,CKA_NEVER_EXTRACTABLE,
4988 &cktrue,sizeof(CK_BBOOL)); 5003 &cktrue,sizeof(CK_BBOOL));
4989 } 5004 }
4990 5005
4991 /* Perform FIPS 140-2 pairwise consistency check. */ 5006 if (crv == CKR_OK) {
4992 crv = sftk_PairwiseConsistencyCheck(hSession, 5007 » /* Perform FIPS 140-2 pairwise consistency check. */
4993 » » » » » publicKey, privateKey, key_type); 5008 » crv = sftk_PairwiseConsistencyCheck(hSession,
5009 » » » » » publicKey, privateKey, key_type);
5010 » if (crv != CKR_OK) {
5011 » if (sftk_audit_enabled) {
5012 » » char msg[128];
5013 » » PR_snprintf(msg,sizeof msg,
5014 » » » "C_GenerateKeyPair(hSession=0x%08lX, "
5015 » » » "pMechanism->mechanism=0x%08lX)=0x%08lX "
5016 » » » "self-test: pair-wise consistency test failed",
5017 » » » (PRUint32)hSession,(PRUint32)pMechanism->mechanism,
5018 » » » (PRUint32)crv);
5019 » » sftk_LogAuditMessage(NSS_AUDIT_ERROR, NSS_AUDIT_SELF_TEST, msg);
5020 » }
5021 » return crv;
5022 » }
5023 }
5024
4994 if (crv != CKR_OK) { 5025 if (crv != CKR_OK) {
4995 NSC_DestroyObject(hSession,publicKey->handle); 5026 NSC_DestroyObject(hSession,publicKey->handle);
4996 sftk_FreeObject(publicKey); 5027 sftk_FreeObject(publicKey);
4997 NSC_DestroyObject(hSession,privateKey->handle); 5028 NSC_DestroyObject(hSession,privateKey->handle);
4998 sftk_FreeObject(privateKey); 5029 sftk_FreeObject(privateKey);
4999 if (sftk_audit_enabled) {
5000 char msg[128];
5001 PR_snprintf(msg,sizeof msg,
5002 "C_GenerateKeyPair(hSession=0x%08lX, "
5003 "pMechanism->mechanism=0x%08lX)=0x%08lX "
5004 "self-test: pair-wise consistency test failed",
5005 (PRUint32)hSession,(PRUint32)pMechanism->mechanism,
5006 (PRUint32)crv);
5007 sftk_LogAuditMessage(NSS_AUDIT_ERROR, NSS_AUDIT_SELF_TEST, msg);
5008 }
5009 return crv;
5010 } 5030 }
5011 5031
5012 *phPrivateKey = privateKey->handle; 5032 *phPrivateKey = privateKey->handle;
5013 *phPublicKey = publicKey->handle; 5033 *phPublicKey = publicKey->handle;
5014 sftk_FreeObject(publicKey); 5034 sftk_FreeObject(publicKey);
5015 sftk_FreeObject(privateKey); 5035 sftk_FreeObject(privateKey);
5016 5036
5017 return CKR_OK; 5037 return CKR_OK;
5018 } 5038 }
5019 5039
(...skipping 2576 matching lines...) Expand 10 before | Expand all | Expand 10 after
7596 att = sftk_FindAttribute(key,CKA_VALUE); 7616 att = sftk_FindAttribute(key,CKA_VALUE);
7597 sftk_FreeObject(key); 7617 sftk_FreeObject(key);
7598 if (!att) { 7618 if (!att) {
7599 return CKR_KEY_HANDLE_INVALID; 7619 return CKR_KEY_HANDLE_INVALID;
7600 } 7620 }
7601 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue, 7621 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue,
7602 att->attrib.ulValueLen); 7622 att->attrib.ulValueLen);
7603 sftk_FreeAttribute(att); 7623 sftk_FreeAttribute(att);
7604 return crv; 7624 return crv;
7605 } 7625 }
OLDNEW
« no previous file with comments | « nss/lib/softoken/pkcs11.c ('k') | nss/lib/softoken/pkcs11i.h » ('j') | nss/lib/util/secoid.c » ('J')

Powered by Google App Engine
This is Rietveld 408576698