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 #include "utilpars.h" |
20 | 21 |
21 | 22 |
22 /************************************************************* | 23 /************************************************************* |
23 * local static and global data | 24 * local static and global data |
24 *************************************************************/ | 25 *************************************************************/ |
25 | 26 |
26 /* | 27 /* |
27 * This array helps parsing between names, mechanisms, and flags. | 28 * This array helps parsing between names, mechanisms, and flags. |
28 * to make the config files understand more entries, add them | 29 * 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) | 30 * to this table. |
30 */ | 31 */ |
31 PK11DefaultArrayEntry PK11_DefaultArray[] = { | 32 PK11DefaultArrayEntry PK11_DefaultArray[] = { |
32 { "RSA", SECMOD_RSA_FLAG, CKM_RSA_PKCS }, | 33 { "RSA", SECMOD_RSA_FLAG, CKM_RSA_PKCS }, |
33 { "DSA", SECMOD_DSA_FLAG, CKM_DSA }, | 34 { "DSA", SECMOD_DSA_FLAG, CKM_DSA }, |
34 { "DH", SECMOD_DH_FLAG, CKM_DH_PKCS_DERIVE }, | 35 { "DH", SECMOD_DH_FLAG, CKM_DH_PKCS_DERIVE }, |
35 { "RC2", SECMOD_RC2_FLAG, CKM_RC2_CBC }, | 36 { "RC2", SECMOD_RC2_FLAG, CKM_RC2_CBC }, |
36 { "RC4", SECMOD_RC4_FLAG, CKM_RC4 }, | 37 { "RC4", SECMOD_RC4_FLAG, CKM_RC4 }, |
37 { "DES", SECMOD_DES_FLAG, CKM_DES_CBC }, | 38 { "DES", SECMOD_DES_FLAG, CKM_DES_CBC }, |
38 { "AES", SECMOD_AES_FLAG, CKM_AES_CBC }, | 39 { "AES", SECMOD_AES_FLAG, CKM_AES_CBC }, |
39 { "Camellia", SECMOD_CAMELLIA_FLAG, CKM_CAMELLIA_CBC }, | 40 { "Camellia", SECMOD_CAMELLIA_FLAG, CKM_CAMELLIA_CBC }, |
(...skipping 1908 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1948 * free the element later */ | 1949 * free the element later */ |
1949 rv = PK11_Authenticate(le->slot,PR_TRUE,wincx); | 1950 rv = PK11_Authenticate(le->slot,PR_TRUE,wincx); |
1950 if (rv != SECSuccess) { | 1951 if (rv != SECSuccess) { |
1951 PK11_DeleteSlotFromList(list,le); | 1952 PK11_DeleteSlotFromList(list,le); |
1952 continue; | 1953 continue; |
1953 } | 1954 } |
1954 } | 1955 } |
1955 return list; | 1956 return list; |
1956 } | 1957 } |
1957 | 1958 |
| 1959 /* |
| 1960 * returns true if the slot doesn't conform to the requested attributes |
| 1961 */ |
| 1962 PRBool |
| 1963 pk11_filterSlot(PK11SlotInfo *slot, CK_MECHANISM_TYPE mechanism, |
| 1964 CK_FLAGS mechanismInfoFlags, unsigned int keySize) |
| 1965 { |
| 1966 CK_MECHANISM_INFO mechanism_info; |
| 1967 CK_RV crv = CKR_OK; |
| 1968 |
| 1969 /* handle the only case where we don't actually fetch the mechanisms |
| 1970 * on the fly */ |
| 1971 if ((keySize == 0) && (mechanism == CKM_RSA_PKCS) && (slot->hasRSAInfo)) { |
| 1972 mechanism_info.flags = slot->RSAInfoFlags; |
| 1973 } else { |
| 1974 if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot); |
| 1975 crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, mechanism, |
| 1976 &mechanism_info); |
| 1977 if (!slot->isThreadSafe) PK11_ExitSlotMonitor(slot); |
| 1978 /* if we were getting the RSA flags, save them */ |
| 1979 if ((crv == CKR_OK) && (mechanism == CKM_RSA_PKCS) |
| 1980 && (!slot->hasRSAInfo)) { |
| 1981 slot->RSAInfoFlags = mechanism_info.flags; |
| 1982 slot->hasRSAInfo = PR_TRUE; |
| 1983 } |
| 1984 } |
| 1985 /* couldn't get the mechanism info */ |
| 1986 if (crv != CKR_OK ) { |
| 1987 return PR_TRUE; |
| 1988 } |
| 1989 if (keySize && ((mechanism_info.ulMinKeySize > keySize) |
| 1990 || (mechanism_info.ulMaxKeySize < keySize)) ) { |
| 1991 /* Token can do mechanism, but not at the key size we |
| 1992 * want */ |
| 1993 return PR_TRUE; |
| 1994 } |
| 1995 if (mechanismInfoFlags && ((mechanism_info.flags & mechanismInfoFlags) != |
| 1996 mechanismInfoFlags) ) { |
| 1997 return PR_TRUE; |
| 1998 } |
| 1999 return PR_FALSE; |
| 2000 } |
| 2001 |
1958 | 2002 |
1959 /* | 2003 /* |
1960 * find the best slot which supports the given | 2004 * 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 | 2005 * In normal cases this should grab the first slot on the list with no fuss. |
1962 * with no fuss. | 2006 * The size array is presumed to match one for one with the mechanism type |
| 2007 * array, which allows you to specify the required key size for each |
| 2008 * mechanism in the list. Whether key size is in bits or bytes is mechanism |
| 2009 * dependent. Typically asymetric keys are in bits and symetric keys are in |
| 2010 * bytes. |
1963 */ | 2011 */ |
1964 PK11SlotInfo * | 2012 PK11SlotInfo * |
1965 PK11_GetBestSlotMultiple(CK_MECHANISM_TYPE *type, int mech_count, void *wincx) | 2013 PK11_GetBestSlotMultipleWithAttributes(CK_MECHANISM_TYPE *type, |
| 2014 » » CK_FLAGS *mechanismInfoFlags, unsigned int *keySize, |
| 2015 » » unsigned int mech_count, void *wincx) |
1966 { | 2016 { |
1967 PK11SlotList *list = NULL; | 2017 PK11SlotList *list = NULL; |
1968 PK11SlotListElement *le ; | 2018 PK11SlotListElement *le ; |
1969 PK11SlotInfo *slot = NULL; | 2019 PK11SlotInfo *slot = NULL; |
1970 PRBool freeit = PR_FALSE; | 2020 PRBool freeit = PR_FALSE; |
1971 PRBool listNeedLogin = PR_FALSE; | 2021 PRBool listNeedLogin = PR_FALSE; |
1972 int i; | 2022 int i; |
1973 SECStatus rv; | 2023 SECStatus rv; |
1974 | 2024 |
1975 list = PK11_GetSlotList(type[0]); | 2025 list = PK11_GetSlotList(type[0]); |
(...skipping 30 matching lines...) Expand all Loading... |
2006 | 2056 |
2007 for (le = PK11_GetFirstSafe(list); le; | 2057 for (le = PK11_GetFirstSafe(list); le; |
2008 le = PK11_GetNextSafe(list,le,PR_TRUE)) { | 2058 le = PK11_GetNextSafe(list,le,PR_TRUE)) { |
2009 if (PK11_IsPresent(le->slot)) { | 2059 if (PK11_IsPresent(le->slot)) { |
2010 PRBool doExit = PR_FALSE; | 2060 PRBool doExit = PR_FALSE; |
2011 for (i=0; i < mech_count; i++) { | 2061 for (i=0; i < mech_count; i++) { |
2012 if (!PK11_DoesMechanism(le->slot,type[i])) { | 2062 if (!PK11_DoesMechanism(le->slot,type[i])) { |
2013 doExit = PR_TRUE; | 2063 doExit = PR_TRUE; |
2014 break; | 2064 break; |
2015 } | 2065 } |
| 2066 if ((mechanismInfoFlags && mechanismInfoFlags[i]) || |
| 2067 (keySize && keySize[i])) { |
| 2068 if (pk11_filterSlot(le->slot, type[i], |
| 2069 mechanismInfoFlags ? mechanismInfoFlags[i] : 0, |
| 2070 keySize ? keySize[i] : 0)) { |
| 2071 doExit = PR_TRUE; |
| 2072 break; |
| 2073 } |
| 2074 } |
2016 } | 2075 } |
| 2076 |
2017 if (doExit) continue; | 2077 if (doExit) continue; |
2018 | 2078 |
2019 if (listNeedLogin && le->slot->needLogin) { | 2079 if (listNeedLogin && le->slot->needLogin) { |
2020 rv = PK11_Authenticate(le->slot,PR_TRUE,wincx); | 2080 rv = PK11_Authenticate(le->slot,PR_TRUE,wincx); |
2021 if (rv != SECSuccess) continue; | 2081 if (rv != SECSuccess) continue; |
2022 } | 2082 } |
2023 slot = le->slot; | 2083 slot = le->slot; |
2024 PK11_ReferenceSlot(slot); | 2084 PK11_ReferenceSlot(slot); |
2025 PK11_FreeSlotListElement(list,le); | 2085 PK11_FreeSlotListElement(list,le); |
2026 if (freeit) { PK11_FreeSlotList(list); } | 2086 if (freeit) { PK11_FreeSlotList(list); } |
2027 return slot; | 2087 return slot; |
2028 } | 2088 } |
2029 } | 2089 } |
2030 if (freeit) { PK11_FreeSlotList(list); } | 2090 if (freeit) { PK11_FreeSlotList(list); } |
2031 if (PORT_GetError() == 0) { | 2091 if (PORT_GetError() == 0) { |
2032 PORT_SetError(SEC_ERROR_NO_TOKEN); | 2092 PORT_SetError(SEC_ERROR_NO_TOKEN); |
2033 } | 2093 } |
2034 return NULL; | 2094 return NULL; |
2035 } | 2095 } |
2036 | 2096 |
| 2097 PK11SlotInfo * |
| 2098 PK11_GetBestSlotMultiple(CK_MECHANISM_TYPE *type, |
| 2099 unsigned int mech_count, void *wincx) |
| 2100 { |
| 2101 return PK11_GetBestSlotMultipleWithAttributes(type, NULL, NULL, |
| 2102 mech_count, wincx); |
| 2103 } |
| 2104 |
2037 /* original get best slot now calls the multiple version with only one type */ | 2105 /* original get best slot now calls the multiple version with only one type */ |
2038 PK11SlotInfo * | 2106 PK11SlotInfo * |
2039 PK11_GetBestSlot(CK_MECHANISM_TYPE type, void *wincx) | 2107 PK11_GetBestSlot(CK_MECHANISM_TYPE type, void *wincx) |
2040 { | 2108 { |
2041 return PK11_GetBestSlotMultiple(&type, 1, wincx); | 2109 return PK11_GetBestSlotMultipleWithAttributes(&type, NULL, NULL, 1, wincx); |
| 2110 } |
| 2111 |
| 2112 PK11SlotInfo * |
| 2113 PK11_GetBestSlotWithAttributes(CK_MECHANISM_TYPE type, CK_FLAGS mechanismFlags, |
| 2114 » » unsigned int keySize, void *wincx) |
| 2115 { |
| 2116 return PK11_GetBestSlotMultipleWithAttributes(&type, &mechanismFlags, |
| 2117 » » » » » » &keySize, 1, wincx); |
2042 } | 2118 } |
2043 | 2119 |
2044 int | 2120 int |
2045 PK11_GetBestKeyLength(PK11SlotInfo *slot,CK_MECHANISM_TYPE mechanism) | 2121 PK11_GetBestKeyLength(PK11SlotInfo *slot,CK_MECHANISM_TYPE mechanism) |
2046 { | 2122 { |
2047 CK_MECHANISM_INFO mechanism_info; | 2123 CK_MECHANISM_INFO mechanism_info; |
2048 CK_RV crv; | 2124 CK_RV crv; |
2049 | 2125 |
2050 if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot); | 2126 if (!slot->isThreadSafe) PK11_EnterSlotMonitor(slot); |
2051 crv = PK11_GETTAB(slot)->C_GetMechanismInfo(slot->slotID, | 2127 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; | 2364 first_time_set = PR_TRUE; |
2289 } | 2365 } |
2290 if ((interval-first_time) > timeout) { | 2366 if ((interval-first_time) > timeout) { |
2291 return waitForRemoval ? PK11TokenPresent : PK11TokenRemoved; | 2367 return waitForRemoval ? PK11TokenPresent : PK11TokenRemoved; |
2292 } | 2368 } |
2293 } | 2369 } |
2294 PR_Sleep(latency); | 2370 PR_Sleep(latency); |
2295 } | 2371 } |
2296 return waitForRemoval ? PK11TokenRemoved : PK11TokenPresent; | 2372 return waitForRemoval ? PK11TokenRemoved : PK11TokenPresent; |
2297 } | 2373 } |
OLD | NEW |