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 manages object type indepentent functions. | 5 * This file manages object type indepentent functions. |
6 */ | 6 */ |
7 #include "seccomon.h" | 7 #include "seccomon.h" |
8 #include "secmod.h" | 8 #include "secmod.h" |
9 #include "secmodi.h" | 9 #include "secmodi.h" |
10 #include "secmodti.h" | 10 #include "secmodti.h" |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 return len; | 512 return len; |
513 } | 513 } |
514 | 514 |
515 /* | 515 /* |
516 * get the length of a signature object based on the key | 516 * get the length of a signature object based on the key |
517 */ | 517 */ |
518 int | 518 int |
519 PK11_SignatureLen(SECKEYPrivateKey *key) | 519 PK11_SignatureLen(SECKEYPrivateKey *key) |
520 { | 520 { |
521 int val; | 521 int val; |
522 CK_ATTRIBUTE theTemplate = { CKA_EC_PARAMS, NULL, 0 }; | 522 SECItem attributeItem = {siBuffer, NULL, 0}; |
523 SECItem params = {siBuffer, NULL, 0}; | 523 SECStatus rv; |
524 int length; | 524 int length; |
525 | 525 |
526 switch (key->keyType) { | 526 switch (key->keyType) { |
527 case rsaKey: | 527 case rsaKey: |
528 val = PK11_GetPrivateModulusLen(key); | 528 val = PK11_GetPrivateModulusLen(key); |
529 if (val == -1) { | 529 if (val == -1) { |
530 return pk11_backupGetSignLength(key); | 530 return pk11_backupGetSignLength(key); |
531 } | 531 } |
532 return (unsigned long) val; | 532 return (unsigned long) val; |
533 | 533 |
534 case fortezzaKey: | 534 case fortezzaKey: |
| 535 return 40; |
| 536 |
535 case dsaKey: | 537 case dsaKey: |
536 » return 40; | 538 rv = PK11_ReadAttribute(key->pkcs11Slot, key->pkcs11ID, CKA_SUBPRIME, |
| 539 » » » » NULL, &attributeItem); |
| 540 if (rv == SECSuccess) { |
| 541 » length = attributeItem.len; |
| 542 » if ((length > 0) && attributeItem.data[0] == 0) { |
| 543 » » length--; |
| 544 » } |
| 545 » PORT_Free(attributeItem.data); |
| 546 » return length*2; |
| 547 » } |
| 548 » return pk11_backupGetSignLength(key); |
| 549 |
537 case ecKey: | 550 case ecKey: |
538 » if (PK11_GetAttributes(NULL, key->pkcs11Slot, key->pkcs11ID, | 551 rv = PK11_ReadAttribute(key->pkcs11Slot, key->pkcs11ID, CKA_EC_PARAMS, |
539 » » » &theTemplate, 1) == CKR_OK) { | 552 » » » » NULL, &attributeItem); |
540 » if (theTemplate.pValue != NULL) { | 553 » if (rv == SECSuccess) { |
541 » params.len = theTemplate.ulValueLen; | 554 » length = SECKEY_ECParamsToBasePointOrderLen(&attributeItem); |
542 » » params.data = (unsigned char *) theTemplate.pValue; | 555 » PORT_Free(attributeItem.data); |
543 » length = SECKEY_ECParamsToBasePointOrderLen(¶ms); | 556 » if (length != 0) { |
544 » PORT_Free(theTemplate.pValue); | |
545 » » if (length == 0) { | |
546 » » return pk11_backupGetSignLength(key); | |
547 » » } | |
548 length = ((length + 7)/8) * 2; | 557 length = ((length + 7)/8) * 2; |
549 return length; | 558 return length; |
550 } | 559 } |
551 } | 560 } |
552 » break; | 561 » return pk11_backupGetSignLength(key); |
553 default: | 562 default: |
554 break; | 563 break; |
555 } | 564 } |
556 PORT_SetError( SEC_ERROR_INVALID_KEY ); | 565 PORT_SetError( SEC_ERROR_INVALID_KEY ); |
557 return 0; | 566 return 0; |
558 } | 567 } |
559 | 568 |
560 /* | 569 /* |
561 * copy a key (or any other object) on a token | 570 * copy a key (or any other object) on a token |
562 */ | 571 */ |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 CK_OBJECT_HANDLE id = key->pkcs11ID; | 607 CK_OBJECT_HANDLE id = key->pkcs11ID; |
599 CK_MECHANISM mech = {0, NULL, 0 }; | 608 CK_MECHANISM mech = {0, NULL, 0 }; |
600 PRBool owner = PR_TRUE; | 609 PRBool owner = PR_TRUE; |
601 CK_SESSION_HANDLE session; | 610 CK_SESSION_HANDLE session; |
602 CK_ULONG len; | 611 CK_ULONG len; |
603 CK_RV crv; | 612 CK_RV crv; |
604 | 613 |
605 mech.mechanism = PK11_MapSignKeyType(key->keyType); | 614 mech.mechanism = PK11_MapSignKeyType(key->keyType); |
606 | 615 |
607 if (slot == NULL) { | 616 if (slot == NULL) { |
608 » slot = PK11_GetBestSlot(mech.mechanism,wincx); | 617 » slot = PK11_GetBestSlotWithAttributes(mech.mechanism, |
| 618 » » » » CKF_VERIFY_RECOVER,0,wincx); |
609 if (slot == NULL) { | 619 if (slot == NULL) { |
610 PORT_SetError( SEC_ERROR_NO_MODULE ); | 620 PORT_SetError( SEC_ERROR_NO_MODULE ); |
611 return SECFailure; | 621 return SECFailure; |
612 } | 622 } |
613 id = PK11_ImportPublicKey(slot,key,PR_FALSE); | 623 id = PK11_ImportPublicKey(slot,key,PR_FALSE); |
614 } else { | 624 } else { |
615 PK11_ReferenceSlot(slot); | 625 PK11_ReferenceSlot(slot); |
616 } | 626 } |
617 | 627 |
618 if (id == CK_INVALID_HANDLE) { | 628 if (id == CK_INVALID_HANDLE) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
655 PK11SlotInfo *slot = key->pkcs11Slot; | 665 PK11SlotInfo *slot = key->pkcs11Slot; |
656 CK_OBJECT_HANDLE id = key->pkcs11ID; | 666 CK_OBJECT_HANDLE id = key->pkcs11ID; |
657 CK_MECHANISM mech = {0, NULL, 0 }; | 667 CK_MECHANISM mech = {0, NULL, 0 }; |
658 PRBool owner = PR_TRUE; | 668 PRBool owner = PR_TRUE; |
659 CK_SESSION_HANDLE session; | 669 CK_SESSION_HANDLE session; |
660 CK_RV crv; | 670 CK_RV crv; |
661 | 671 |
662 mech.mechanism = PK11_MapSignKeyType(key->keyType); | 672 mech.mechanism = PK11_MapSignKeyType(key->keyType); |
663 | 673 |
664 if (slot == NULL) { | 674 if (slot == NULL) { |
665 » slot = PK11_GetBestSlot(mech.mechanism,wincx); | 675 » unsigned int length = 0; |
666 | 676 » if ((mech.mechanism == CKM_DSA) && |
| 677 » » » » /* 129 is 1024 bits translated to bytes and |
| 678 » » » » * padded with an optional '0' to maintain a |
| 679 » » » » * positive sign */ |
| 680 » » » » (key->u.dsa.params.prime.len > 129)) { |
| 681 » /* we need to get a slot that not only can do DSA, but can do DSA2 |
| 682 » * key lengths */ |
| 683 » length = key->u.dsa.params.prime.len; |
| 684 » if (key->u.dsa.params.prime.data[0] == 0) { |
| 685 » » length --; |
| 686 » } |
| 687 » } |
| 688 » slot = PK11_GetBestSlotWithAttributes(mech.mechanism, |
| 689 » » » » » » CKF_VERIFY,length,wincx); |
667 if (slot == NULL) { | 690 if (slot == NULL) { |
668 PORT_SetError( SEC_ERROR_NO_MODULE ); | 691 PORT_SetError( SEC_ERROR_NO_MODULE ); |
669 return SECFailure; | 692 return SECFailure; |
670 } | 693 } |
671 id = PK11_ImportPublicKey(slot,key,PR_FALSE); | 694 id = PK11_ImportPublicKey(slot,key,PR_FALSE); |
672 | 695 |
673 } else { | 696 } else { |
674 PK11_ReferenceSlot(slot); | 697 PK11_ReferenceSlot(slot); |
675 } | 698 } |
676 | 699 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 | 746 |
724 session = pk11_GetNewSession(slot,&owner); | 747 session = pk11_GetNewSession(slot,&owner); |
725 if (!owner || !(slot->isThreadSafe)) PK11_EnterSlotMonitor(slot); | 748 if (!owner || !(slot->isThreadSafe)) PK11_EnterSlotMonitor(slot); |
726 crv = PK11_GETTAB(slot)->C_SignInit(session,&mech,key->pkcs11ID); | 749 crv = PK11_GETTAB(slot)->C_SignInit(session,&mech,key->pkcs11ID); |
727 if (crv != CKR_OK) { | 750 if (crv != CKR_OK) { |
728 if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); | 751 if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); |
729 pk11_CloseSession(slot,session,owner); | 752 pk11_CloseSession(slot,session,owner); |
730 PORT_SetError( PK11_MapError(crv) ); | 753 PORT_SetError( PK11_MapError(crv) ); |
731 return SECFailure; | 754 return SECFailure; |
732 } | 755 } |
| 756 /* PKCS11 2.20 says if CKA_ALWAYS_AUTHENTICATE then |
| 757 * do C_Login with CKU_CONTEXT_SPECIFIC |
| 758 * between C_SignInit and C_Sign */ |
| 759 if (SECKEY_HAS_ATTRIBUTE_SET(key,CKA_ALWAYS_AUTHENTICATE)) { |
| 760 PK11_DoPassword(slot, PR_FALSE, key->wincx, PR_TRUE); |
| 761 } |
733 len = sig->len; | 762 len = sig->len; |
734 crv = PK11_GETTAB(slot)->C_Sign(session,hash->data, | 763 crv = PK11_GETTAB(slot)->C_Sign(session,hash->data, |
735 hash->len, sig->data, &len); | 764 hash->len, sig->data, &len); |
736 if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); | 765 if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); |
737 pk11_CloseSession(slot,session,owner); | 766 pk11_CloseSession(slot,session,owner); |
738 sig->len = len; | 767 sig->len = len; |
739 if (crv != CKR_OK) { | 768 if (crv != CKR_OK) { |
740 PORT_SetError( PK11_MapError(crv) ); | 769 PORT_SetError( PK11_MapError(crv) ); |
741 return SECFailure; | 770 return SECFailure; |
742 } | 771 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 } | 804 } |
776 session = pk11_GetNewSession(slot,&owner); | 805 session = pk11_GetNewSession(slot,&owner); |
777 if (!owner || !(slot->isThreadSafe)) PK11_EnterSlotMonitor(slot); | 806 if (!owner || !(slot->isThreadSafe)) PK11_EnterSlotMonitor(slot); |
778 crv = PK11_GETTAB(slot)->C_DecryptInit(session, mech, key->pkcs11ID); | 807 crv = PK11_GETTAB(slot)->C_DecryptInit(session, mech, key->pkcs11ID); |
779 if (crv != CKR_OK) { | 808 if (crv != CKR_OK) { |
780 if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); | 809 if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); |
781 pk11_CloseSession(slot,session,owner); | 810 pk11_CloseSession(slot,session,owner); |
782 PORT_SetError( PK11_MapError(crv) ); | 811 PORT_SetError( PK11_MapError(crv) ); |
783 return SECFailure; | 812 return SECFailure; |
784 } | 813 } |
| 814 /* PKCS11 2.20 says if CKA_ALWAYS_AUTHENTICATE then |
| 815 * do C_Login with CKU_CONTEXT_SPECIFIC |
| 816 * between C_DecryptInit and C_Decrypt */ |
| 817 /* But see note above about servers */ |
| 818 if (SECKEY_HAS_ATTRIBUTE_SET(key,CKA_ALWAYS_AUTHENTICATE)) { |
| 819 PK11_DoPassword(slot, PR_FALSE, key->wincx, PR_TRUE); |
| 820 } |
785 crv = PK11_GETTAB(slot)->C_Decrypt(session,enc, encLen, data, &out); | 821 crv = PK11_GETTAB(slot)->C_Decrypt(session,enc, encLen, data, &out); |
786 if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); | 822 if (!owner || !(slot->isThreadSafe)) PK11_ExitSlotMonitor(slot); |
787 pk11_CloseSession(slot,session,owner); | 823 pk11_CloseSession(slot,session,owner); |
788 *outLen = out; | 824 *outLen = out; |
789 if (crv != CKR_OK) { | 825 if (crv != CKR_OK) { |
790 PORT_SetError( PK11_MapError(crv) ); | 826 PORT_SetError( PK11_MapError(crv) ); |
791 return SECFailure; | 827 return SECFailure; |
792 } | 828 } |
793 return SECSuccess; | 829 return SECSuccess; |
794 } | 830 } |
(...skipping 27 matching lines...) Expand all Loading... |
822 PRBool owner = PR_TRUE; | 858 PRBool owner = PR_TRUE; |
823 CK_SESSION_HANDLE session; | 859 CK_SESSION_HANDLE session; |
824 CK_RV crv; | 860 CK_RV crv; |
825 | 861 |
826 if (!key || key->keyType != rsaKey) { | 862 if (!key || key->keyType != rsaKey) { |
827 PORT_SetError( SEC_ERROR_BAD_KEY ); | 863 PORT_SetError( SEC_ERROR_BAD_KEY ); |
828 return SECFailure; | 864 return SECFailure; |
829 } | 865 } |
830 out = SECKEY_PublicKeyStrength(key); | 866 out = SECKEY_PublicKeyStrength(key); |
831 | 867 |
832 slot = PK11_GetBestSlot(mech->mechanism, wincx); | 868 slot = PK11_GetBestSlotWithAttributes(mech->mechanism,CKF_ENCRYPT,0,wincx); |
833 if (slot == NULL) { | 869 if (slot == NULL) { |
834 PORT_SetError( SEC_ERROR_NO_MODULE ); | 870 PORT_SetError( SEC_ERROR_NO_MODULE ); |
835 return SECFailure; | 871 return SECFailure; |
836 } | 872 } |
837 | 873 |
838 id = PK11_ImportPublicKey(slot,key,PR_FALSE); | 874 id = PK11_ImportPublicKey(slot,key,PR_FALSE); |
839 | 875 |
840 if (id == CK_INVALID_HANDLE) { | 876 if (id == CK_INVALID_HANDLE) { |
841 PK11_FreeSlot(slot); | 877 PK11_FreeSlot(slot); |
842 PORT_SetError( SEC_ERROR_BAD_KEY ); | 878 PORT_SetError( SEC_ERROR_BAD_KEY ); |
(...skipping 947 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1790 PORT_SetError( PK11_MapError(crv) ); | 1826 PORT_SetError( PK11_MapError(crv) ); |
1791 return NULL; | 1827 return NULL; |
1792 } | 1828 } |
1793 | 1829 |
1794 item->data = (unsigned char*) theTemplate[0].pValue; | 1830 item->data = (unsigned char*) theTemplate[0].pValue; |
1795 item->len =theTemplate[0].ulValueLen; | 1831 item->len =theTemplate[0].ulValueLen; |
1796 | 1832 |
1797 return item; | 1833 return item; |
1798 } | 1834 } |
1799 | 1835 |
OLD | NEW |