| OLD | NEW |
| 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 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 case CKG_MGF1_SHA384: | 295 case CKG_MGF1_SHA384: |
| 296 return HASH_AlgSHA384; | 296 return HASH_AlgSHA384; |
| 297 case CKM_SHA512: | 297 case CKM_SHA512: |
| 298 case CKG_MGF1_SHA512: | 298 case CKG_MGF1_SHA512: |
| 299 return HASH_AlgSHA512; | 299 return HASH_AlgSHA512; |
| 300 default: | 300 default: |
| 301 return HASH_AlgNULL; | 301 return HASH_AlgNULL; |
| 302 } | 302 } |
| 303 } | 303 } |
| 304 | 304 |
| 305 /* |
| 306 * Returns true if "params" contains a valid set of PSS parameters |
| 307 */ |
| 308 static PRBool |
| 309 sftk_ValidatePssParams(const CK_RSA_PKCS_PSS_PARAMS *params) |
| 310 { |
| 311 if (!params) { |
| 312 return PR_FALSE; |
| 313 } |
| 314 if (GetHashTypeFromMechanism(params->hashAlg) == HASH_AlgNULL || |
| 315 GetHashTypeFromMechanism(params->mgf) == HASH_AlgNULL) { |
| 316 return PR_FALSE; |
| 317 } |
| 318 return PR_TRUE; |
| 319 } |
| 320 |
| 321 /* |
| 322 * Returns true if "params" contains a valid set of OAEP parameters |
| 323 */ |
| 324 static PRBool |
| 325 sftk_ValidateOaepParams(const CK_RSA_PKCS_OAEP_PARAMS *params) |
| 326 { |
| 327 if (!params) { |
| 328 return PR_FALSE; |
| 329 } |
| 330 /* The requirements of ulSourceLen/pSourceData come from PKCS #11, which |
| 331 * state: |
| 332 * If the parameter is empty, pSourceData must be NULL and |
| 333 * ulSourceDataLen must be zero. |
| 334 */ |
| 335 if (params->source != CKZ_DATA_SPECIFIED || |
| 336 (GetHashTypeFromMechanism(params->hashAlg) == HASH_AlgNULL) || |
| 337 (GetHashTypeFromMechanism(params->mgf) == HASH_AlgNULL) || |
| 338 (params->ulSourceDataLen == 0 && params->pSourceData != NULL) || |
| 339 (params->ulSourceDataLen != 0 && params->pSourceData == NULL)) { |
| 340 return PR_FALSE; |
| 341 } |
| 342 return PR_TRUE; |
| 343 } |
| 344 |
| 305 /* | 345 /* |
| 306 * return a context based on the SFTKContext type. | 346 * return a context based on the SFTKContext type. |
| 307 */ | 347 */ |
| 308 SFTKSessionContext * | 348 SFTKSessionContext * |
| 309 sftk_ReturnContextByType(SFTKSession *session, SFTKContextType type) | 349 sftk_ReturnContextByType(SFTKSession *session, SFTKContextType type) |
| 310 { | 350 { |
| 311 switch (type) { | 351 switch (type) { |
| 312 case SFTK_ENCRYPT: | 352 case SFTK_ENCRYPT: |
| 313 case SFTK_DECRYPT: | 353 case SFTK_DECRYPT: |
| 314 return session->enc_context; | 354 return session->enc_context; |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 | 621 |
| 582 PORT_Assert(info->key->keyType == NSSLOWKEYRSAKey); | 622 PORT_Assert(info->key->keyType == NSSLOWKEYRSAKey); |
| 583 if (info->key->keyType != NSSLOWKEYRSAKey) { | 623 if (info->key->keyType != NSSLOWKEYRSAKey) { |
| 584 PORT_SetError(SEC_ERROR_INVALID_KEY); | 624 PORT_SetError(SEC_ERROR_INVALID_KEY); |
| 585 return SECFailure; | 625 return SECFailure; |
| 586 } | 626 } |
| 587 | 627 |
| 588 hashAlg = GetHashTypeFromMechanism(info->params->hashAlg); | 628 hashAlg = GetHashTypeFromMechanism(info->params->hashAlg); |
| 589 maskHashAlg = GetHashTypeFromMechanism(info->params->mgf); | 629 maskHashAlg = GetHashTypeFromMechanism(info->params->mgf); |
| 590 | 630 |
| 591 if (info->params->source != CKZ_DATA_SPECIFIED) { | |
| 592 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | |
| 593 return SECFailure; | |
| 594 } | |
| 595 | |
| 596 return RSA_EncryptOAEP(&info->key->u.rsa, hashAlg, maskHashAlg, | 631 return RSA_EncryptOAEP(&info->key->u.rsa, hashAlg, maskHashAlg, |
| 597 (const unsigned char*)info->params->pSourceData, | 632 (const unsigned char*)info->params->pSourceData, |
| 598 info->params->ulSourceDataLen, NULL, 0, | 633 info->params->ulSourceDataLen, NULL, 0, |
| 599 output, outputLen, maxLen, input, inputLen); | 634 output, outputLen, maxLen, input, inputLen); |
| 600 } | 635 } |
| 601 | 636 |
| 602 static SECStatus | 637 static SECStatus |
| 603 sftk_RSADecryptOAEP(SFTKOAEPDecryptInfo *info, unsigned char *output, | 638 sftk_RSADecryptOAEP(SFTKOAEPDecryptInfo *info, unsigned char *output, |
| 604 unsigned int *outputLen, unsigned int maxLen, | 639 unsigned int *outputLen, unsigned int maxLen, |
| 605 const unsigned char *input, unsigned int inputLen) | 640 const unsigned char *input, unsigned int inputLen) |
| 606 { | 641 { |
| 607 SECStatus rv = SECFailure; | 642 SECStatus rv = SECFailure; |
| 608 HASH_HashType hashAlg; | 643 HASH_HashType hashAlg; |
| 609 HASH_HashType maskHashAlg; | 644 HASH_HashType maskHashAlg; |
| 610 | 645 |
| 611 PORT_Assert(info->key->keyType == NSSLOWKEYRSAKey); | 646 PORT_Assert(info->key->keyType == NSSLOWKEYRSAKey); |
| 612 if (info->key->keyType != NSSLOWKEYRSAKey) { | 647 if (info->key->keyType != NSSLOWKEYRSAKey) { |
| 613 PORT_SetError(SEC_ERROR_INVALID_KEY); | 648 PORT_SetError(SEC_ERROR_INVALID_KEY); |
| 614 return SECFailure; | 649 return SECFailure; |
| 615 } | 650 } |
| 616 | 651 |
| 617 hashAlg = GetHashTypeFromMechanism(info->params->hashAlg); | 652 hashAlg = GetHashTypeFromMechanism(info->params->hashAlg); |
| 618 maskHashAlg = GetHashTypeFromMechanism(info->params->mgf); | 653 maskHashAlg = GetHashTypeFromMechanism(info->params->mgf); |
| 619 | 654 |
| 620 if (info->params->source != CKZ_DATA_SPECIFIED) { | |
| 621 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | |
| 622 return SECFailure; | |
| 623 } | |
| 624 | |
| 625 rv = RSA_DecryptOAEP(&info->key->u.rsa, hashAlg, maskHashAlg, | 655 rv = RSA_DecryptOAEP(&info->key->u.rsa, hashAlg, maskHashAlg, |
| 626 (const unsigned char*)info->params->pSourceData, | 656 (const unsigned char*)info->params->pSourceData, |
| 627 info->params->ulSourceDataLen, | 657 info->params->ulSourceDataLen, |
| 628 output, outputLen, maxLen, input, inputLen); | 658 output, outputLen, maxLen, input, inputLen); |
| 629 if (rv != SECSuccess && PORT_GetError() == SEC_ERROR_LIBRARY_FAILURE) { | 659 if (rv != SECSuccess && PORT_GetError() == SEC_ERROR_LIBRARY_FAILURE) { |
| 630 sftk_fatalError = PR_TRUE; | 660 sftk_fatalError = PR_TRUE; |
| 631 } | 661 } |
| 632 return rv; | 662 return rv; |
| 633 } | 663 } |
| 634 | 664 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 break; | 824 break; |
| 795 } | 825 } |
| 796 context->maxLen = nsslowkey_PrivateModulusLen(privKey); | 826 context->maxLen = nsslowkey_PrivateModulusLen(privKey); |
| 797 context->cipherInfo = (void *)privKey; | 827 context->cipherInfo = (void *)privKey; |
| 798 context->update = (SFTKCipher) | 828 context->update = (SFTKCipher) |
| 799 (pMechanism->mechanism == CKM_RSA_X_509 | 829 (pMechanism->mechanism == CKM_RSA_X_509 |
| 800 ? sftk_RSADecryptRaw : sftk_RSADecrypt); | 830 ? sftk_RSADecryptRaw : sftk_RSADecrypt); |
| 801 } | 831 } |
| 802 context->destroy = sftk_Null; | 832 context->destroy = sftk_Null; |
| 803 break; | 833 break; |
| 804 /* XXX: Disabled until unit tests land. | |
| 805 case CKM_RSA_PKCS_OAEP: | 834 case CKM_RSA_PKCS_OAEP: |
| 806 if (key_type != CKK_RSA) { | 835 if (key_type != CKK_RSA) { |
| 807 crv = CKR_KEY_TYPE_INCONSISTENT; | 836 crv = CKR_KEY_TYPE_INCONSISTENT; |
| 808 break; | 837 break; |
| 809 } | 838 } |
| 810 » context->multi = PR_FALSE; | 839 » if (pMechanism->ulParameterLen != sizeof(CK_RSA_PKCS_OAEP_PARAMS) || |
| 811 » context->rsa = PR_TRUE; | 840 » !sftk_ValidateOaepParams((CK_RSA_PKCS_OAEP_PARAMS*)pMechanism->pPara
meter)) { |
| 812 » if (pMechanism->ulParameterLen != sizeof(CK_RSA_PKCS_OAEP_PARAMS)) { | |
| 813 crv = CKR_MECHANISM_PARAM_INVALID; | 841 crv = CKR_MECHANISM_PARAM_INVALID; |
| 814 break; | 842 break; |
| 815 } | 843 } |
| 816 » /\* XXX: Need Parameter validation here *\/ | 844 » context->multi = PR_FALSE; |
| 845 » context->rsa = PR_TRUE; |
| 817 if (isEncrypt) { | 846 if (isEncrypt) { |
| 818 SFTKOAEPEncryptInfo *info = PORT_New(SFTKOAEPEncryptInfo); | 847 SFTKOAEPEncryptInfo *info = PORT_New(SFTKOAEPEncryptInfo); |
| 819 if (info == NULL) { | 848 if (info == NULL) { |
| 820 crv = CKR_HOST_MEMORY; | 849 crv = CKR_HOST_MEMORY; |
| 821 break; | 850 break; |
| 822 } | 851 } |
| 823 info->params = pMechanism->pParameter; | 852 info->params = pMechanism->pParameter; |
| 824 info->key = sftk_GetPubKey(key, CKK_RSA, &crv); | 853 info->key = sftk_GetPubKey(key, CKK_RSA, &crv); |
| 825 if (info->key == NULL) { | 854 if (info->key == NULL) { |
| 826 PORT_Free(info); | 855 PORT_Free(info); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 842 PORT_Free(info); | 871 PORT_Free(info); |
| 843 crv = CKR_KEY_HANDLE_INVALID; | 872 crv = CKR_KEY_HANDLE_INVALID; |
| 844 break; | 873 break; |
| 845 } | 874 } |
| 846 context->update = (SFTKCipher) sftk_RSADecryptOAEP; | 875 context->update = (SFTKCipher) sftk_RSADecryptOAEP; |
| 847 context->maxLen = nsslowkey_PrivateModulusLen(info->key); | 876 context->maxLen = nsslowkey_PrivateModulusLen(info->key); |
| 848 context->cipherInfo = info; | 877 context->cipherInfo = info; |
| 849 } | 878 } |
| 850 context->destroy = (SFTKDestroy) sftk_Space; | 879 context->destroy = (SFTKDestroy) sftk_Space; |
| 851 break; | 880 break; |
| 852 */ | |
| 853 case CKM_RC2_CBC_PAD: | 881 case CKM_RC2_CBC_PAD: |
| 854 context->doPad = PR_TRUE; | 882 context->doPad = PR_TRUE; |
| 855 /* fall thru */ | 883 /* fall thru */ |
| 856 case CKM_RC2_ECB: | 884 case CKM_RC2_ECB: |
| 857 case CKM_RC2_CBC: | 885 case CKM_RC2_CBC: |
| 858 context->blockSize = 8; | 886 context->blockSize = 8; |
| 859 if (key_type != CKK_RC2) { | 887 if (key_type != CKK_RC2) { |
| 860 crv = CKR_KEY_TYPE_INCONSISTENT; | 888 crv = CKR_KEY_TYPE_INCONSISTENT; |
| 861 break; | 889 break; |
| 862 } | 890 } |
| (...skipping 1636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2499 context->destroy = (SFTKDestroy)sftk_Null; | 2527 context->destroy = (SFTKDestroy)sftk_Null; |
| 2500 } | 2528 } |
| 2501 context->maxLen = nsslowkey_PrivateModulusLen(privKey); | 2529 context->maxLen = nsslowkey_PrivateModulusLen(privKey); |
| 2502 break; | 2530 break; |
| 2503 case CKM_RSA_PKCS_PSS: | 2531 case CKM_RSA_PKCS_PSS: |
| 2504 if (key_type != CKK_RSA) { | 2532 if (key_type != CKK_RSA) { |
| 2505 crv = CKR_KEY_TYPE_INCONSISTENT; | 2533 crv = CKR_KEY_TYPE_INCONSISTENT; |
| 2506 break; | 2534 break; |
| 2507 } | 2535 } |
| 2508 context->rsa = PR_TRUE; | 2536 context->rsa = PR_TRUE; |
| 2509 » if (pMechanism->ulParameterLen != sizeof(CK_RSA_PKCS_PSS_PARAMS)) { | 2537 » if (pMechanism->ulParameterLen != sizeof(CK_RSA_PKCS_PSS_PARAMS) || |
| 2538 » !sftk_ValidatePssParams((const CK_RSA_PKCS_PSS_PARAMS*)pMechanism->p
Parameter)) { |
| 2510 crv = CKR_MECHANISM_PARAM_INVALID; | 2539 crv = CKR_MECHANISM_PARAM_INVALID; |
| 2511 break; | 2540 break; |
| 2512 } | 2541 } |
| 2513 info = PORT_New(SFTKHashSignInfo); | 2542 info = PORT_New(SFTKHashSignInfo); |
| 2514 if (info == NULL) { | 2543 if (info == NULL) { |
| 2515 crv = CKR_HOST_MEMORY; | 2544 crv = CKR_HOST_MEMORY; |
| 2516 break; | 2545 break; |
| 2517 } | 2546 } |
| 2518 info->params = pMechanism->pParameter; | 2547 info->params = pMechanism->pParameter; |
| 2519 info->key = sftk_GetPrivKey(key,CKK_RSA,&crv); | 2548 info->key = sftk_GetPrivKey(key,CKK_RSA,&crv); |
| (...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3136 context->cipherInfo = pubKey; | 3165 context->cipherInfo = pubKey; |
| 3137 context->destroy = sftk_Null; | 3166 context->destroy = sftk_Null; |
| 3138 } | 3167 } |
| 3139 break; | 3168 break; |
| 3140 case CKM_RSA_PKCS_PSS: | 3169 case CKM_RSA_PKCS_PSS: |
| 3141 if (key_type != CKK_RSA) { | 3170 if (key_type != CKK_RSA) { |
| 3142 crv = CKR_KEY_TYPE_INCONSISTENT; | 3171 crv = CKR_KEY_TYPE_INCONSISTENT; |
| 3143 break; | 3172 break; |
| 3144 } | 3173 } |
| 3145 context->rsa = PR_TRUE; | 3174 context->rsa = PR_TRUE; |
| 3146 » if (pMechanism->ulParameterLen != sizeof(CK_RSA_PKCS_PSS_PARAMS)) { | 3175 » if (pMechanism->ulParameterLen != sizeof(CK_RSA_PKCS_PSS_PARAMS) || |
| 3176 » !sftk_ValidatePssParams((const CK_RSA_PKCS_PSS_PARAMS*)pMechanism->p
Parameter)) { |
| 3147 crv = CKR_MECHANISM_PARAM_INVALID; | 3177 crv = CKR_MECHANISM_PARAM_INVALID; |
| 3148 break; | 3178 break; |
| 3149 } | 3179 } |
| 3150 info = PORT_New(SFTKHashVerifyInfo); | 3180 info = PORT_New(SFTKHashVerifyInfo); |
| 3151 if (info == NULL) { | 3181 if (info == NULL) { |
| 3152 crv = CKR_HOST_MEMORY; | 3182 crv = CKR_HOST_MEMORY; |
| 3153 break; | 3183 break; |
| 3154 } | 3184 } |
| 3155 info->params = pMechanism->pParameter; | 3185 info->params = pMechanism->pParameter; |
| 3156 info->key = sftk_GetPubKey(key,CKK_RSA,&crv); | 3186 info->key = sftk_GetPubKey(key,CKK_RSA,&crv); |
| (...skipping 4252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7409 att = sftk_FindAttribute(key,CKA_VALUE); | 7439 att = sftk_FindAttribute(key,CKA_VALUE); |
| 7410 sftk_FreeObject(key); | 7440 sftk_FreeObject(key); |
| 7411 if (!att) { | 7441 if (!att) { |
| 7412 return CKR_KEY_HANDLE_INVALID; | 7442 return CKR_KEY_HANDLE_INVALID; |
| 7413 } | 7443 } |
| 7414 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue, | 7444 crv = NSC_DigestUpdate(hSession,(CK_BYTE_PTR)att->attrib.pValue, |
| 7415 att->attrib.ulValueLen); | 7445 att->attrib.ulValueLen); |
| 7416 sftk_FreeAttribute(att); | 7446 sftk_FreeAttribute(att); |
| 7417 return crv; | 7447 return crv; |
| 7418 } | 7448 } |
| OLD | NEW |