| 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 * Deal with PKCS #11 Slots. | 5 * Deal with PKCS #11 Slots. |
| 6 */ | 6 */ |
| 7 #include "seccomon.h" | 7 #include "seccomon.h" |
| 8 #include "secmod.h" | 8 #include "secmod.h" |
| 9 #include "nssilock.h" | 9 #include "nssilock.h" |
| 10 #include "secmodi.h" | 10 #include "secmodi.h" |
| 11 #include "secmodti.h" | 11 #include "secmodti.h" |
| 12 #include "pkcs11t.h" | 12 #include "pkcs11t.h" |
| 13 #include "pk11func.h" | 13 #include "pk11func.h" |
| 14 #include "secitem.h" | 14 #include "secitem.h" |
| 15 #include "secerr.h" | 15 #include "secerr.h" |
| 16 | 16 |
| 17 #include "dev.h" | 17 #include "dev.h" |
| 18 #include "dev3hack.h" | 18 #include "dev3hack.h" |
| 19 #include "pkim.h" | 19 #include "pkim.h" |
| 20 | 20 |
| 21 | 21 |
| 22 /************************************************************* | 22 /************************************************************* |
| 23 * local static and global data | 23 * local static and global data |
| 24 *************************************************************/ | 24 *************************************************************/ |
| 25 | 25 |
| 26 /* | 26 /* |
| 27 * This array helps parsing between names, mechanisms, and flags. | 27 * This array helps parsing between names, mechanisms, and flags. |
| 28 * to make the config files understand more entries, add them | 28 * to make the config files understand more entries, add them |
| 29 * to this table. (NOTE: we need function to export this table and its size) | 29 * to this table. |
| 30 */ | 30 */ |
| 31 PK11DefaultArrayEntry PK11_DefaultArray[] = { | 31 PK11DefaultArrayEntry PK11_DefaultArray[] = { |
| 32 { "RSA", SECMOD_RSA_FLAG, CKM_RSA_PKCS }, | 32 { "RSA", SECMOD_RSA_FLAG, CKM_RSA_PKCS }, |
| 33 { "DSA", SECMOD_DSA_FLAG, CKM_DSA }, | 33 { "DSA", SECMOD_DSA_FLAG, CKM_DSA }, |
| 34 { "DH", SECMOD_DH_FLAG, CKM_DH_PKCS_DERIVE }, | 34 { "DH", SECMOD_DH_FLAG, CKM_DH_PKCS_DERIVE }, |
| 35 { "RC2", SECMOD_RC2_FLAG, CKM_RC2_CBC }, | 35 { "RC2", SECMOD_RC2_FLAG, CKM_RC2_CBC }, |
| 36 { "RC4", SECMOD_RC4_FLAG, CKM_RC4 }, | 36 { "RC4", SECMOD_RC4_FLAG, CKM_RC4 }, |
| 37 { "DES", SECMOD_DES_FLAG, CKM_DES_CBC }, | 37 { "DES", SECMOD_DES_FLAG, CKM_DES_CBC }, |
| 38 { "AES", SECMOD_AES_FLAG, CKM_AES_CBC }, | 38 { "AES", SECMOD_AES_FLAG, CKM_AES_CBC }, |
| 39 { "Camellia", SECMOD_CAMELLIA_FLAG, CKM_CAMELLIA_CBC }, | 39 { "Camellia", SECMOD_CAMELLIA_FLAG, CKM_CAMELLIA_CBC }, |
| (...skipping 1910 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1950 if (rv != SECSuccess) { | 1950 if (rv != SECSuccess) { |
| 1951 PK11_DeleteSlotFromList(list,le); | 1951 PK11_DeleteSlotFromList(list,le); |
| 1952 continue; | 1952 continue; |
| 1953 } | 1953 } |
| 1954 } | 1954 } |
| 1955 return list; | 1955 return list; |
| 1956 } | 1956 } |
| 1957 | 1957 |
| 1958 | 1958 |
| 1959 /* | 1959 /* |
| 1960 * find the best slot which supports the given | 1960 * Find the best slot which supports the given set of mechanisms and key sizes. |
| 1961 * Mechanism. In normal cases this should grab the first slot on the list | 1961 * In normal cases this should grab the first slot on the list with no fuss. |
| 1962 * with no fuss. | 1962 * The size array is presumed to match one for one with the mechanism type |
| 1963 * array, which allows you to specify the required key size for each |
| 1964 * mechanism in the list. Whether key size is in bits or bytes is mechanism |
| 1965 * dependent. Typically asymetric keys are in bits and symetric keys are in |
| 1966 * bytes. |
| 1963 */ | 1967 */ |
| 1964 PK11SlotInfo * | 1968 PK11SlotInfo * |
| 1965 PK11_GetBestSlotMultiple(CK_MECHANISM_TYPE *type, int mech_count, void *wincx) | 1969 PK11_GetBestSlotMultipleWithKeySize(CK_MECHANISM_TYPE *type, |
| 1970 unsigned long *size, unsigned int mech_count, void *wincx) |
| 1966 { | 1971 { |
| 1967 PK11SlotList *list = NULL; | 1972 PK11SlotList *list = NULL; |
| 1968 PK11SlotListElement *le ; | 1973 PK11SlotListElement *le ; |
| 1969 PK11SlotInfo *slot = NULL; | 1974 PK11SlotInfo *slot = NULL; |
| 1970 PRBool freeit = PR_FALSE; | 1975 PRBool freeit = PR_FALSE; |
| 1971 PRBool listNeedLogin = PR_FALSE; | 1976 PRBool listNeedLogin = PR_FALSE; |
| 1972 int i; | 1977 int i; |
| 1973 SECStatus rv; | 1978 SECStatus rv; |
| 1974 | 1979 |
| 1975 list = PK11_GetSlotList(type[0]); | 1980 list = PK11_GetSlotList(type[0]); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 2006 | 2011 |
| 2007 for (le = PK11_GetFirstSafe(list); le; | 2012 for (le = PK11_GetFirstSafe(list); le; |
| 2008 le = PK11_GetNextSafe(list,le,PR_TRUE)) { | 2013 le = PK11_GetNextSafe(list,le,PR_TRUE)) { |
| 2009 if (PK11_IsPresent(le->slot)) { | 2014 if (PK11_IsPresent(le->slot)) { |
| 2010 PRBool doExit = PR_FALSE; | 2015 PRBool doExit = PR_FALSE; |
| 2011 for (i=0; i < mech_count; i++) { | 2016 for (i=0; i < mech_count; i++) { |
| 2012 if (!PK11_DoesMechanism(le->slot,type[i])) { | 2017 if (!PK11_DoesMechanism(le->slot,type[i])) { |
| 2013 doExit = PR_TRUE; | 2018 doExit = PR_TRUE; |
| 2014 break; | 2019 break; |
| 2015 } | 2020 } |
| 2021 if (size && size[i]) { |
| 2022 CK_MECHANISM_INFO mechanism_info; |
| 2023 CK_RV crv = PK11_GETTAB(le->slot)->C_GetMechanismInfo( |
| 2024 slot->slotID, type[i], &mechanism_info); |
| 2025 if (crv != CKR_OK |
| 2026 || (mechanism_info.ulMinKeySize > size[i]) |
| 2027 || (mechanism_info.ulMaxKeySize < size[i]) ) { |
| 2028 /* Token can do mechanism, but not at the key size we |
| 2029 * want */ |
| 2030 doExit = PR_TRUE; |
| 2031 break; |
| 2032 } |
| 2033 } |
| 2034 |
| 2016 } | 2035 } |
| 2017 if (doExit) continue; | 2036 if (doExit) continue; |
| 2018 | 2037 |
| 2019 if (listNeedLogin && le->slot->needLogin) { | 2038 if (listNeedLogin && le->slot->needLogin) { |
| 2020 rv = PK11_Authenticate(le->slot,PR_TRUE,wincx); | 2039 rv = PK11_Authenticate(le->slot,PR_TRUE,wincx); |
| 2021 if (rv != SECSuccess) continue; | 2040 if (rv != SECSuccess) continue; |
| 2022 } | 2041 } |
| 2023 slot = le->slot; | 2042 slot = le->slot; |
| 2024 PK11_ReferenceSlot(slot); | 2043 PK11_ReferenceSlot(slot); |
| 2025 PK11_FreeSlotListElement(list,le); | 2044 PK11_FreeSlotListElement(list,le); |
| 2026 if (freeit) { PK11_FreeSlotList(list); } | 2045 if (freeit) { PK11_FreeSlotList(list); } |
| 2027 return slot; | 2046 return slot; |
| 2028 } | 2047 } |
| 2029 } | 2048 } |
| 2030 if (freeit) { PK11_FreeSlotList(list); } | 2049 if (freeit) { PK11_FreeSlotList(list); } |
| 2031 if (PORT_GetError() == 0) { | 2050 if (PORT_GetError() == 0) { |
| 2032 PORT_SetError(SEC_ERROR_NO_TOKEN); | 2051 PORT_SetError(SEC_ERROR_NO_TOKEN); |
| 2033 } | 2052 } |
| 2034 return NULL; | 2053 return NULL; |
| 2035 } | 2054 } |
| 2036 | 2055 |
| 2056 PK11SlotInfo * |
| 2057 PK11_GetBestSlotMultiple(CK_MECHANISM_TYPE *type, |
| 2058 unsigned int mech_count, void *wincx) |
| 2059 { |
| 2060 return PK11_GetBestSlotMultipleWithKeySize(type, NULL, mech_count, wincx); |
| 2061 } |
| 2062 |
| 2037 /* original get best slot now calls the multiple version with only one type */ | 2063 /* original get best slot now calls the multiple version with only one type */ |
| 2038 PK11SlotInfo * | 2064 PK11SlotInfo * |
| 2039 PK11_GetBestSlot(CK_MECHANISM_TYPE type, void *wincx) | 2065 PK11_GetBestSlot(CK_MECHANISM_TYPE type, void *wincx) |
| 2040 { | 2066 { |
| 2041 return PK11_GetBestSlotMultiple(&type, 1, wincx); | 2067 return PK11_GetBestSlotMultipleWithKeySize(&type, NULL, 1, wincx); |
| 2068 } |
| 2069 |
| 2070 PK11SlotInfo * |
| 2071 PK11_GetBestSlotWithKeySize(CK_MECHANISM_TYPE type, unsigned long keySize, |
| 2072 » » » void *wincx) |
| 2073 { |
| 2074 return PK11_GetBestSlotMultipleWithKeySize(&type, &keySize, 1, wincx); |
| 2042 } | 2075 } |
| 2043 | 2076 |
| 2044 int | 2077 int |
| 2045 PK11_GetBestKeyLength(PK11SlotInfo *slot,CK_MECHANISM_TYPE mechanism) | 2078 PK11_GetBestKeyLength(PK11SlotInfo *slot,CK_MECHANISM_TYPE mechanism) |
| 2046 { | 2079 { |
| 2047 CK_MECHANISM_INFO mechanism_info; | 2080 CK_MECHANISM_INFO mechanism_info; |
| 2048 CK_RV crv; | 2081 CK_RV crv; |
| 2049 | 2082 |
| 2050 if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot); | 2083 if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot); |
| 2051 crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, | 2084 crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2288 first_time_set = PR_TRUE; | 2321 first_time_set = PR_TRUE; |
| 2289 } | 2322 } |
| 2290 if ((interval-first_time) > timeout) { | 2323 if ((interval-first_time) > timeout) { |
| 2291 return waitForRemoval ? PK11TokenPresent : PK11TokenRemoved; | 2324 return waitForRemoval ? PK11TokenPresent : PK11TokenRemoved; |
| 2292 } | 2325 } |
| 2293 } | 2326 } |
| 2294 PR_Sleep(latency); | 2327 PR_Sleep(latency); |
| 2295 } | 2328 } |
| 2296 return waitForRemoval ? PK11TokenRemoved : PK11TokenPresent; | 2329 return waitForRemoval ? PK11TokenRemoved : PK11TokenPresent; |
| 2297 } | 2330 } |
| OLD | NEW |