michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: /* michael@0: * This file implements PKCS 11 on top of our existing security modules michael@0: * michael@0: * For more information about PKCS 11 See PKCS 11 Token Inteface Standard. michael@0: * This implementation has two slots: michael@0: * slot 1 is our generic crypto support. It does not require login. michael@0: * It supports Public Key ops, and all they bulk ciphers and hashes. michael@0: * It can also support Private Key ops for imported Private keys. It does michael@0: * not have any token storage. michael@0: * slot 2 is our private key support. It requires a login before use. It michael@0: * can store Private Keys and Certs as token objects. Currently only private michael@0: * keys and their associated Certificates are saved on the token. michael@0: * michael@0: * In this implementation, session objects are only visible to the session michael@0: * that created or generated them. michael@0: */ michael@0: michael@0: #include "sdb.h" michael@0: #include "pkcs11t.h" michael@0: #include "seccomon.h" michael@0: #include michael@0: #include "prthread.h" michael@0: #include "prio.h" michael@0: #include michael@0: #include "secport.h" michael@0: #include "prmon.h" michael@0: #include "prenv.h" michael@0: #include "prprf.h" michael@0: #include "prsystem.h" /* for PR_GetDirectorySeparator() */ michael@0: #include michael@0: #if defined(_WIN32) michael@0: #include michael@0: #include michael@0: #elif defined(XP_UNIX) michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef SQLITE_UNSAFE_THREADS michael@0: #include "prlock.h" michael@0: /* michael@0: * SQLite can be compiled to be thread safe or not. michael@0: * turn on SQLITE_UNSAFE_THREADS if the OS does not support michael@0: * a thread safe version of sqlite. michael@0: */ michael@0: static PRLock *sqlite_lock = NULL; michael@0: michael@0: #define LOCK_SQLITE() PR_Lock(sqlite_lock); michael@0: #define UNLOCK_SQLITE() PR_Unlock(sqlite_lock); michael@0: #else michael@0: #define LOCK_SQLITE() michael@0: #define UNLOCK_SQLITE() michael@0: #endif michael@0: michael@0: typedef enum { michael@0: SDB_CERT = 1, michael@0: SDB_KEY = 2 michael@0: } sdbDataType; michael@0: michael@0: /* michael@0: * defines controlling how long we wait to acquire locks. michael@0: * michael@0: * SDB_SQLITE_BUSY_TIMEOUT specifies how long (in milliseconds) michael@0: * sqlite will wait on lock. If that timeout expires, sqlite will michael@0: * return SQLITE_BUSY. michael@0: * SDB_BUSY_RETRY_TIME specifies how many seconds the sdb_ code waits michael@0: * after receiving a busy before retrying. michael@0: * SDB_MAX_BUSY_RETRIES specifies how many times the sdb_ will retry on michael@0: * a busy condition. michael@0: * michael@0: * SDB_SQLITE_BUSY_TIMEOUT affects all opertions, both manual michael@0: * (prepare/step/reset/finalize) and automatic (sqlite3_exec()). michael@0: * SDB_BUSY_RETRY_TIME and SDB_MAX_BUSY_RETRIES only affect manual operations michael@0: * michael@0: * total wait time for automatic operations: michael@0: * 1 second (SDB_SQLITE_BUSY_TIMEOUT/1000). michael@0: * total wait time for manual operations: michael@0: * (1 second + 5 seconds) * 10 = 60 seconds. michael@0: * (SDB_SQLITE_BUSY_TIMEOUT/1000 + SDB_BUSY_RETRY_TIME)*SDB_MAX_BUSY_RETRIES michael@0: */ michael@0: #define SDB_SQLITE_BUSY_TIMEOUT 1000 /* milliseconds */ michael@0: #define SDB_BUSY_RETRY_TIME 5 /* seconds */ michael@0: #define SDB_MAX_BUSY_RETRIES 10 michael@0: michael@0: /* michael@0: * Note on use of sqlReadDB: Only one thread at a time may have an actual michael@0: * operation going on given sqlite3 * database. An operation is defined as michael@0: * the time from a sqlite3_prepare() until the sqlite3_finalize(). michael@0: * Multiple sqlite3 * databases can be open and have simultaneous operations michael@0: * going. We use the sqlXactDB for all write operations. This database michael@0: * is only opened when we first create a transaction and closed when the michael@0: * transaction is complete. sqlReadDB is open when we first opened the database michael@0: * and is used for all read operation. It's use is protected by a monitor. This michael@0: * is because an operation can span the use of FindObjectsInit() through the michael@0: * call to FindObjectsFinal(). In the intermediate time it is possible to call michael@0: * other operations like NSC_GetAttributeValue */ michael@0: michael@0: struct SDBPrivateStr { michael@0: char *sqlDBName; /* invariant, path to this database */ michael@0: sqlite3 *sqlXactDB; /* access protected by dbMon, use protected michael@0: * by the transaction. Current transaction db*/ michael@0: PRThread *sqlXactThread; /* protected by dbMon, michael@0: * current transaction thread */ michael@0: sqlite3 *sqlReadDB; /* use protected by dbMon, value invariant */ michael@0: PRIntervalTime lastUpdateTime; /* last time the cache was updated */ michael@0: PRIntervalTime updateInterval; /* how long the cache can go before it michael@0: * must be updated again */ michael@0: sdbDataType type; /* invariant, database type */ michael@0: char *table; /* invariant, SQL table which contains the db */ michael@0: char *cacheTable; /* invariant, SQL table cache of db */ michael@0: PRMonitor *dbMon; /* invariant, monitor to protect michael@0: * sqlXact* fields, and use of the sqlReadDB */ michael@0: }; michael@0: michael@0: typedef struct SDBPrivateStr SDBPrivate; michael@0: michael@0: /* michael@0: * known attributes michael@0: */ michael@0: static const CK_ATTRIBUTE_TYPE known_attributes[] = { michael@0: CKA_CLASS, CKA_TOKEN, CKA_PRIVATE, CKA_LABEL, CKA_APPLICATION, michael@0: CKA_VALUE, CKA_OBJECT_ID, CKA_CERTIFICATE_TYPE, CKA_ISSUER, michael@0: CKA_SERIAL_NUMBER, CKA_AC_ISSUER, CKA_OWNER, CKA_ATTR_TYPES, CKA_TRUSTED, michael@0: CKA_CERTIFICATE_CATEGORY, CKA_JAVA_MIDP_SECURITY_DOMAIN, CKA_URL, michael@0: CKA_HASH_OF_SUBJECT_PUBLIC_KEY, CKA_HASH_OF_ISSUER_PUBLIC_KEY, michael@0: CKA_CHECK_VALUE, CKA_KEY_TYPE, CKA_SUBJECT, CKA_ID, CKA_SENSITIVE, michael@0: CKA_ENCRYPT, CKA_DECRYPT, CKA_WRAP, CKA_UNWRAP, CKA_SIGN, CKA_SIGN_RECOVER, michael@0: CKA_VERIFY, CKA_VERIFY_RECOVER, CKA_DERIVE, CKA_START_DATE, CKA_END_DATE, michael@0: CKA_MODULUS, CKA_MODULUS_BITS, CKA_PUBLIC_EXPONENT, CKA_PRIVATE_EXPONENT, michael@0: CKA_PRIME_1, CKA_PRIME_2, CKA_EXPONENT_1, CKA_EXPONENT_2, CKA_COEFFICIENT, michael@0: CKA_PRIME, CKA_SUBPRIME, CKA_BASE, CKA_PRIME_BITS, michael@0: CKA_SUB_PRIME_BITS, CKA_VALUE_BITS, CKA_VALUE_LEN, CKA_EXTRACTABLE, michael@0: CKA_LOCAL, CKA_NEVER_EXTRACTABLE, CKA_ALWAYS_SENSITIVE, michael@0: CKA_KEY_GEN_MECHANISM, CKA_MODIFIABLE, CKA_EC_PARAMS, michael@0: CKA_EC_POINT, CKA_SECONDARY_AUTH, CKA_AUTH_PIN_FLAGS, michael@0: CKA_ALWAYS_AUTHENTICATE, CKA_WRAP_WITH_TRUSTED, CKA_WRAP_TEMPLATE, michael@0: CKA_UNWRAP_TEMPLATE, CKA_HW_FEATURE_TYPE, CKA_RESET_ON_INIT, michael@0: CKA_HAS_RESET, CKA_PIXEL_X, CKA_PIXEL_Y, CKA_RESOLUTION, CKA_CHAR_ROWS, michael@0: CKA_CHAR_COLUMNS, CKA_COLOR, CKA_BITS_PER_PIXEL, CKA_CHAR_SETS, michael@0: CKA_ENCODING_METHODS, CKA_MIME_TYPES, CKA_MECHANISM_TYPE, michael@0: CKA_REQUIRED_CMS_ATTRIBUTES, CKA_DEFAULT_CMS_ATTRIBUTES, michael@0: CKA_SUPPORTED_CMS_ATTRIBUTES, CKA_NETSCAPE_URL, CKA_NETSCAPE_EMAIL, michael@0: CKA_NETSCAPE_SMIME_INFO, CKA_NETSCAPE_SMIME_TIMESTAMP, michael@0: CKA_NETSCAPE_PKCS8_SALT, CKA_NETSCAPE_PASSWORD_CHECK, CKA_NETSCAPE_EXPIRES, michael@0: CKA_NETSCAPE_KRL, CKA_NETSCAPE_PQG_COUNTER, CKA_NETSCAPE_PQG_SEED, michael@0: CKA_NETSCAPE_PQG_H, CKA_NETSCAPE_PQG_SEED_BITS, CKA_NETSCAPE_MODULE_SPEC, michael@0: CKA_TRUST_DIGITAL_SIGNATURE, CKA_TRUST_NON_REPUDIATION, michael@0: CKA_TRUST_KEY_ENCIPHERMENT, CKA_TRUST_DATA_ENCIPHERMENT, michael@0: CKA_TRUST_KEY_AGREEMENT, CKA_TRUST_KEY_CERT_SIGN, CKA_TRUST_CRL_SIGN, michael@0: CKA_TRUST_SERVER_AUTH, CKA_TRUST_CLIENT_AUTH, CKA_TRUST_CODE_SIGNING, michael@0: CKA_TRUST_EMAIL_PROTECTION, CKA_TRUST_IPSEC_END_SYSTEM, michael@0: CKA_TRUST_IPSEC_TUNNEL, CKA_TRUST_IPSEC_USER, CKA_TRUST_TIME_STAMPING, michael@0: CKA_TRUST_STEP_UP_APPROVED, CKA_CERT_SHA1_HASH, CKA_CERT_MD5_HASH, michael@0: CKA_NETSCAPE_DB, CKA_NETSCAPE_TRUST, CKA_NSS_OVERRIDE_EXTENSIONS michael@0: }; michael@0: michael@0: static int known_attributes_size= sizeof(known_attributes)/ michael@0: sizeof(known_attributes[0]); michael@0: michael@0: /* Magic for an explicit NULL. NOTE: ideally this should be michael@0: * out of band data. Since it's not completely out of band, pick michael@0: * a value that has no meaning to any existing PKCS #11 attributes. michael@0: * This value is 1) not a valid string (imbedded '\0'). 2) not a U_LONG michael@0: * or a normal key (too short). 3) not a bool (too long). 4) not an RSA michael@0: * public exponent (too many bits). michael@0: */ michael@0: const unsigned char SQLITE_EXPLICIT_NULL[] = { 0xa5, 0x0, 0x5a }; michael@0: #define SQLITE_EXPLICIT_NULL_LEN 3 michael@0: michael@0: /* michael@0: * determine when we've completed our tasks michael@0: */ michael@0: static int michael@0: sdb_done(int err, int *count) michael@0: { michael@0: /* allow as many rows as the database wants to give */ michael@0: if (err == SQLITE_ROW) { michael@0: *count = 0; michael@0: return 0; michael@0: } michael@0: if (err != SQLITE_BUSY) { michael@0: return 1; michael@0: } michael@0: /* err == SQLITE_BUSY, Dont' retry forever in this case */ michael@0: if (++(*count) >= SDB_MAX_BUSY_RETRIES) { michael@0: return 1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: * find out where sqlite stores the temp tables. We do this by replicating michael@0: * the logic from sqlite. michael@0: */ michael@0: #if defined(_WIN32) michael@0: static char * michael@0: sdb_getFallbackTempDir(void) michael@0: { michael@0: /* sqlite uses sqlite3_temp_directory if it is not NULL. We don't have michael@0: * access to sqlite3_temp_directory because it is not exported from michael@0: * sqlite3.dll. Assume sqlite3_win32_set_directory isn't called and michael@0: * sqlite3_temp_directory is NULL. michael@0: */ michael@0: char path[MAX_PATH]; michael@0: DWORD rv; michael@0: size_t len; michael@0: michael@0: rv = GetTempPathA(MAX_PATH, path); michael@0: if (rv > MAX_PATH || rv == 0) michael@0: return NULL; michael@0: len = strlen(path); michael@0: if (len == 0) michael@0: return NULL; michael@0: /* The returned string ends with a backslash, for example, "C:\TEMP\". */ michael@0: if (path[len - 1] == '\\') michael@0: path[len - 1] = '\0'; michael@0: return PORT_Strdup(path); michael@0: } michael@0: #elif defined(XP_UNIX) michael@0: static char * michael@0: sdb_getFallbackTempDir(void) michael@0: { michael@0: const char *azDirs[] = { michael@0: NULL, michael@0: NULL, michael@0: "/var/tmp", michael@0: "/usr/tmp", michael@0: "/tmp", michael@0: NULL /* List terminator */ michael@0: }; michael@0: unsigned int i; michael@0: struct stat buf; michael@0: const char *zDir = NULL; michael@0: michael@0: azDirs[0] = sqlite3_temp_directory; michael@0: azDirs[1] = getenv("TMPDIR"); michael@0: michael@0: for (i = 0; i < PR_ARRAY_SIZE(azDirs); i++) { michael@0: zDir = azDirs[i]; michael@0: if (zDir == NULL) continue; michael@0: if (stat(zDir, &buf)) continue; michael@0: if (!S_ISDIR(buf.st_mode)) continue; michael@0: if (access(zDir, 07)) continue; michael@0: break; michael@0: } michael@0: michael@0: if (zDir == NULL) michael@0: return NULL; michael@0: return PORT_Strdup(zDir); michael@0: } michael@0: #else michael@0: #error "sdb_getFallbackTempDir not implemented" michael@0: #endif michael@0: michael@0: #ifndef SQLITE_FCNTL_TEMPFILENAME michael@0: /* SQLITE_FCNTL_TEMPFILENAME was added in SQLite 3.7.15 */ michael@0: #define SQLITE_FCNTL_TEMPFILENAME 16 michael@0: #endif michael@0: michael@0: static char * michael@0: sdb_getTempDir(sqlite3 *sqlDB) michael@0: { michael@0: int sqlrv; michael@0: char *result = NULL; michael@0: char *tempName = NULL; michael@0: char *foundSeparator = NULL; michael@0: michael@0: /* Obtain temporary filename in sqlite's directory for temporary tables */ michael@0: sqlrv = sqlite3_file_control(sqlDB, 0, SQLITE_FCNTL_TEMPFILENAME, michael@0: (void*)&tempName); michael@0: if (sqlrv == SQLITE_NOTFOUND) { michael@0: /* SQLITE_FCNTL_TEMPFILENAME not implemented because we are using michael@0: * an older SQLite. */ michael@0: return sdb_getFallbackTempDir(); michael@0: } michael@0: if (sqlrv != SQLITE_OK) { michael@0: return NULL; michael@0: } michael@0: michael@0: /* We'll extract the temporary directory from tempName */ michael@0: foundSeparator = PORT_Strrchr(tempName, PR_GetDirectorySeparator()); michael@0: if (foundSeparator) { michael@0: /* We shorten the temp filename string to contain only michael@0: * the directory name (including the trailing separator). michael@0: * We know the byte after the foundSeparator position is michael@0: * safe to use, in the shortest scenario it contains the michael@0: * end-of-string byte. michael@0: * By keeping the separator at the found position, it will michael@0: * even work if tempDir consists of the separator, only. michael@0: * (In this case the toplevel directory will be used for michael@0: * access speed testing). */ michael@0: ++foundSeparator; michael@0: *foundSeparator = 0; michael@0: michael@0: /* Now we copy the directory name for our caller */ michael@0: result = PORT_Strdup(tempName); michael@0: } michael@0: michael@0: sqlite3_free(tempName); michael@0: return result; michael@0: } michael@0: michael@0: /* michael@0: * Map SQL_LITE errors to PKCS #11 errors as best we can. michael@0: */ michael@0: static CK_RV michael@0: sdb_mapSQLError(sdbDataType type, int sqlerr) michael@0: { michael@0: switch (sqlerr) { michael@0: /* good matches */ michael@0: case SQLITE_OK: michael@0: case SQLITE_DONE: michael@0: return CKR_OK; michael@0: case SQLITE_NOMEM: michael@0: return CKR_HOST_MEMORY; michael@0: case SQLITE_READONLY: michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: /* close matches */ michael@0: case SQLITE_AUTH: michael@0: case SQLITE_PERM: michael@0: /*return CKR_USER_NOT_LOGGED_IN; */ michael@0: case SQLITE_CANTOPEN: michael@0: case SQLITE_NOTFOUND: michael@0: /* NSS distiguishes between failure to open the cert and the key db */ michael@0: return type == SDB_CERT ? michael@0: CKR_NETSCAPE_CERTDB_FAILED : CKR_NETSCAPE_KEYDB_FAILED; michael@0: case SQLITE_IOERR: michael@0: return CKR_DEVICE_ERROR; michael@0: default: michael@0: break; michael@0: } michael@0: return CKR_GENERAL_ERROR; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * build up database name from a directory, prefix, name, version and flags. michael@0: */ michael@0: static char *sdb_BuildFileName(const char * directory, michael@0: const char *prefix, const char *type, michael@0: int version) michael@0: { michael@0: char *dbname = NULL; michael@0: /* build the full dbname */ michael@0: dbname = sqlite3_mprintf("%s%c%s%s%d.db", directory, michael@0: (int)(unsigned char)PR_GetDirectorySeparator(), michael@0: prefix, type, version); michael@0: return dbname; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * find out how expensive the access system call is for non-existant files michael@0: * in the given directory. Return the number of operations done in 33 ms. michael@0: */ michael@0: static PRUint32 michael@0: sdb_measureAccess(const char *directory) michael@0: { michael@0: PRUint32 i; michael@0: PRIntervalTime time; michael@0: PRIntervalTime delta; michael@0: PRIntervalTime duration = PR_MillisecondsToInterval(33); michael@0: const char *doesntExistName = "_dOeSnotExist_.db"; michael@0: char *temp, *tempStartOfFilename; michael@0: size_t maxTempLen, maxFileNameLen, directoryLength; michael@0: michael@0: /* no directory, just return one */ michael@0: if (directory == NULL) { michael@0: return 1; michael@0: } michael@0: michael@0: /* our calculation assumes time is a 4 bytes == 32 bit integer */ michael@0: PORT_Assert(sizeof(time) == 4); michael@0: michael@0: directoryLength = strlen(directory); michael@0: michael@0: maxTempLen = directoryLength + strlen(doesntExistName) michael@0: + 1 /* potential additional separator char */ michael@0: + 11 /* max chars for 32 bit int plus potential sign */ michael@0: + 1; /* zero terminator */ michael@0: michael@0: temp = PORT_Alloc(maxTempLen); michael@0: if (!temp) { michael@0: return 1; michael@0: } michael@0: michael@0: /* We'll copy directory into temp just once, then ensure it ends michael@0: * with the directory separator, then remember the position after michael@0: * the separator, and calculate the number of remaining bytes. */ michael@0: michael@0: strcpy(temp, directory); michael@0: if (directory[directoryLength - 1] != PR_GetDirectorySeparator()) { michael@0: temp[directoryLength++] = PR_GetDirectorySeparator(); michael@0: } michael@0: tempStartOfFilename = temp + directoryLength; michael@0: maxFileNameLen = maxTempLen - directoryLength; michael@0: michael@0: /* measure number of Access operations that can be done in 33 milliseconds michael@0: * (1/30'th of a second), or 10000 operations, which ever comes first. michael@0: */ michael@0: time = PR_IntervalNow(); michael@0: for (i=0; i < 10000u; i++) { michael@0: PRIntervalTime next; michael@0: michael@0: /* We'll use the variable part first in the filename string, just in michael@0: * case it's longer than assumed, so if anything gets cut off, it michael@0: * will be cut off from the constant part. michael@0: * This code assumes the directory name at the beginning of michael@0: * temp remains unchanged during our loop. */ michael@0: PR_snprintf(tempStartOfFilename, maxFileNameLen, michael@0: ".%lu%s", (PRUint32)(time+i), doesntExistName); michael@0: PR_Access(temp,PR_ACCESS_EXISTS); michael@0: next = PR_IntervalNow(); michael@0: delta = next - time; michael@0: if (delta >= duration) michael@0: break; michael@0: } michael@0: michael@0: PORT_Free(temp); michael@0: michael@0: /* always return 1 or greater */ michael@0: return i ? i : 1u; michael@0: } michael@0: michael@0: /* michael@0: * some file sytems are very slow to run sqlite3 on, particularly if the michael@0: * access count is pretty high. On these filesystems is faster to create michael@0: * a temporary database on the local filesystem and access that. This michael@0: * code uses a temporary table to create that cache. Temp tables are michael@0: * automatically cleared when the database handle it was created on michael@0: * Is freed. michael@0: */ michael@0: static const char DROP_CACHE_CMD[] = "DROP TABLE %s"; michael@0: static const char CREATE_CACHE_CMD[] = michael@0: "CREATE TEMPORARY TABLE %s AS SELECT * FROM %s"; michael@0: static const char CREATE_ISSUER_INDEX_CMD[] = michael@0: "CREATE INDEX issuer ON %s (a81)"; michael@0: static const char CREATE_SUBJECT_INDEX_CMD[] = michael@0: "CREATE INDEX subject ON %s (a101)"; michael@0: static const char CREATE_LABEL_INDEX_CMD[] = "CREATE INDEX label ON %s (a3)"; michael@0: static const char CREATE_ID_INDEX_CMD[] = "CREATE INDEX ckaid ON %s (a102)"; michael@0: michael@0: static CK_RV michael@0: sdb_buildCache(sqlite3 *sqlDB, sdbDataType type, michael@0: const char *cacheTable, const char *table) michael@0: { michael@0: char *newStr; michael@0: int sqlerr = SQLITE_OK; michael@0: michael@0: newStr = sqlite3_mprintf(CREATE_CACHE_CMD, cacheTable, table); michael@0: if (newStr == NULL) { michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: if (sqlerr != SQLITE_OK) { michael@0: return sdb_mapSQLError(type, sqlerr); michael@0: } michael@0: /* failure to create the indexes is not an issue */ michael@0: newStr = sqlite3_mprintf(CREATE_ISSUER_INDEX_CMD, cacheTable); michael@0: if (newStr == NULL) { michael@0: return CKR_OK; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: newStr = sqlite3_mprintf(CREATE_SUBJECT_INDEX_CMD, cacheTable); michael@0: if (newStr == NULL) { michael@0: return CKR_OK; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: newStr = sqlite3_mprintf(CREATE_LABEL_INDEX_CMD, cacheTable); michael@0: if (newStr == NULL) { michael@0: return CKR_OK; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: newStr = sqlite3_mprintf(CREATE_ID_INDEX_CMD, cacheTable); michael@0: if (newStr == NULL) { michael@0: return CKR_OK; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * update the cache and the data records describing it. michael@0: * The cache is updated by dropping the temp database and recreating it. michael@0: */ michael@0: static CK_RV michael@0: sdb_updateCache(SDBPrivate *sdb_p) michael@0: { michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: char *newStr; michael@0: michael@0: /* drop the old table */ michael@0: newStr = sqlite3_mprintf(DROP_CACHE_CMD, sdb_p->cacheTable); michael@0: if (newStr == NULL) { michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: sqlerr = sqlite3_exec(sdb_p->sqlReadDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: if ((sqlerr != SQLITE_OK) && (sqlerr != SQLITE_ERROR )) { michael@0: /* something went wrong with the drop, don't try to refresh... michael@0: * NOTE: SQLITE_ERROR is returned if the table doesn't exist. In michael@0: * that case, we just continue on and try to reload it */ michael@0: return sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: } michael@0: michael@0: michael@0: /* set up the new table */ michael@0: error = sdb_buildCache(sdb_p->sqlReadDB,sdb_p->type, michael@0: sdb_p->cacheTable,sdb_p->table ); michael@0: if (error == CKR_OK) { michael@0: /* we have a new cache! */ michael@0: sdb_p->lastUpdateTime = PR_IntervalNow(); michael@0: } michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * The sharing of sqlite3 handles across threads is tricky. Older versions michael@0: * couldn't at all, but newer ones can under strict conditions. Basically michael@0: * no 2 threads can use the same handle while another thread has an open michael@0: * stmt running. Once the sqlite3_stmt is finalized, another thread can then michael@0: * use the database handle. michael@0: * michael@0: * We use monitors to protect against trying to use a database before michael@0: * it's sqlite3_stmt is finalized. This is preferable to the opening and michael@0: * closing the database each operation because there is significant overhead michael@0: * in the open and close. Also continually opening and closing the database michael@0: * defeats the cache code as the cache table is lost on close (thus michael@0: * requiring us to have to reinitialize the cache every operation). michael@0: * michael@0: * An execption to the shared handle is transations. All writes happen michael@0: * through a transaction. When we are in a transaction, we must use the michael@0: * same database pointer for that entire transation. In this case we save michael@0: * the transaction database and use it for all accesses on the transaction michael@0: * thread. Other threads use the common database. michael@0: * michael@0: * There can only be once active transaction on the database at a time. michael@0: * michael@0: * sdb_openDBLocal() provides us with a valid database handle for whatever michael@0: * state we are in (reading or in a transaction), and acquires any locks michael@0: * appropriate to that state. It also decides when it's time to refresh michael@0: * the cache before we start an operation. Any database handle returned michael@0: * just eventually be closed with sdb_closeDBLocal(). michael@0: * michael@0: * The table returned either points to the database's physical table, or michael@0: * to the cached shadow. Tranactions always return the physical table michael@0: * and read operations return either the physical table or the cache michael@0: * depending on whether or not the cache exists. michael@0: */ michael@0: static CK_RV michael@0: sdb_openDBLocal(SDBPrivate *sdb_p, sqlite3 **sqlDB, const char **table) michael@0: { michael@0: *sqlDB = NULL; michael@0: michael@0: PR_EnterMonitor(sdb_p->dbMon); michael@0: michael@0: if (table) { michael@0: *table = sdb_p->table; michael@0: } michael@0: michael@0: /* We're in a transaction, use the transaction DB */ michael@0: if ((sdb_p->sqlXactDB) && (sdb_p->sqlXactThread == PR_GetCurrentThread())) { michael@0: *sqlDB =sdb_p->sqlXactDB; michael@0: /* only one thread can get here, safe to unlock */ michael@0: PR_ExitMonitor(sdb_p->dbMon); michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* michael@0: * if we are just reading from the table, we may have the table michael@0: * cached in a temporary table (especially if it's on a shared FS). michael@0: * In that case we want to see updates to the table, the the granularity michael@0: * is on order of human scale, not computer scale. michael@0: */ michael@0: if (table && sdb_p->cacheTable) { michael@0: PRIntervalTime now = PR_IntervalNow(); michael@0: if ((now - sdb_p->lastUpdateTime) > sdb_p->updateInterval) { michael@0: sdb_updateCache(sdb_p); michael@0: } michael@0: *table = sdb_p->cacheTable; michael@0: } michael@0: michael@0: *sqlDB = sdb_p->sqlReadDB; michael@0: michael@0: /* leave holding the lock. only one thread can actually use a given michael@0: * database connection at once */ michael@0: michael@0: return CKR_OK; michael@0: } michael@0: michael@0: /* closing the local database currenly means unlocking the monitor */ michael@0: static CK_RV michael@0: sdb_closeDBLocal(SDBPrivate *sdb_p, sqlite3 *sqlDB) michael@0: { michael@0: if (sdb_p->sqlXactDB != sqlDB) { michael@0: /* if we weren't in a transaction, we got a lock */ michael@0: PR_ExitMonitor(sdb_p->dbMon); michael@0: } michael@0: return CKR_OK; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * wrapper to sqlite3_open which also sets the busy_timeout michael@0: */ michael@0: static int michael@0: sdb_openDB(const char *name, sqlite3 **sqlDB, int flags) michael@0: { michael@0: int sqlerr; michael@0: /* michael@0: * in sqlite3 3.5.0, there is a new open call that allows us michael@0: * to specify read only. Most new OS's are still on 3.3.x (including michael@0: * NSS's internal version and the version shipped with Firefox). michael@0: */ michael@0: *sqlDB = NULL; michael@0: sqlerr = sqlite3_open(name, sqlDB); michael@0: if (sqlerr != SQLITE_OK) { michael@0: return sqlerr; michael@0: } michael@0: michael@0: sqlerr = sqlite3_busy_timeout(*sqlDB, SDB_SQLITE_BUSY_TIMEOUT); michael@0: if (sqlerr != SQLITE_OK) { michael@0: sqlite3_close(*sqlDB); michael@0: *sqlDB = NULL; michael@0: return sqlerr; michael@0: } michael@0: return SQLITE_OK; michael@0: } michael@0: michael@0: /* Sigh, if we created a new table since we opened the database, michael@0: * the database handle will not see the new table, we need to close this michael@0: * database and reopen it. Caller must be in a transaction or holding michael@0: * the dbMon. sqlDB is changed on success. */ michael@0: static int michael@0: sdb_reopenDBLocal(SDBPrivate *sdb_p, sqlite3 **sqlDB) { michael@0: sqlite3 *newDB; michael@0: int sqlerr; michael@0: michael@0: /* open a new database */ michael@0: sqlerr = sdb_openDB(sdb_p->sqlDBName, &newDB, SDB_RDONLY); michael@0: if (sqlerr != SQLITE_OK) { michael@0: return sqlerr; michael@0: } michael@0: michael@0: /* if we are in a transaction, we may not be holding the monitor. michael@0: * grab it before we update the transaction database. This is michael@0: * safe since are using monitors. */ michael@0: PR_EnterMonitor(sdb_p->dbMon); michael@0: /* update our view of the database */ michael@0: if (sdb_p->sqlReadDB == *sqlDB) { michael@0: sdb_p->sqlReadDB = newDB; michael@0: } else if (sdb_p->sqlXactDB == *sqlDB) { michael@0: sdb_p->sqlXactDB = newDB; michael@0: } michael@0: PR_ExitMonitor(sdb_p->dbMon); michael@0: michael@0: /* close the old one */ michael@0: sqlite3_close(*sqlDB); michael@0: michael@0: *sqlDB = newDB; michael@0: return SQLITE_OK; michael@0: } michael@0: michael@0: struct SDBFindStr { michael@0: sqlite3 *sqlDB; michael@0: sqlite3_stmt *findstmt; michael@0: }; michael@0: michael@0: michael@0: static const char FIND_OBJECTS_CMD[] = "SELECT ALL * FROM %s WHERE %s;"; michael@0: static const char FIND_OBJECTS_ALL_CMD[] = "SELECT ALL * FROM %s;"; michael@0: CK_RV michael@0: sdb_FindObjectsInit(SDB *sdb, const CK_ATTRIBUTE *template, CK_ULONG count, michael@0: SDBFind **find) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = NULL; michael@0: const char *table; michael@0: char *newStr, *findStr = NULL; michael@0: sqlite3_stmt *findstmt = NULL; michael@0: char *join=""; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: int i; michael@0: michael@0: LOCK_SQLITE() michael@0: *find = NULL; michael@0: error = sdb_openDBLocal(sdb_p, &sqlDB, &table); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: michael@0: findStr = sqlite3_mprintf(""); michael@0: for (i=0; findStr && i < count; i++) { michael@0: newStr = sqlite3_mprintf("%s%sa%x=$DATA%d", findStr, join, michael@0: template[i].type, i); michael@0: join=" AND "; michael@0: sqlite3_free(findStr); michael@0: findStr = newStr; michael@0: } michael@0: michael@0: if (findStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: michael@0: if (count == 0) { michael@0: newStr = sqlite3_mprintf(FIND_OBJECTS_ALL_CMD, table); michael@0: } else { michael@0: newStr = sqlite3_mprintf(FIND_OBJECTS_CMD, table, findStr); michael@0: } michael@0: sqlite3_free(findStr); michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_prepare_v2(sqlDB, newStr, -1, &findstmt, NULL); michael@0: sqlite3_free(newStr); michael@0: for (i=0; sqlerr == SQLITE_OK && i < count; i++) { michael@0: const void *blobData = template[i].pValue; michael@0: unsigned int blobSize = template[i].ulValueLen; michael@0: if (blobSize == 0) { michael@0: blobSize = SQLITE_EXPLICIT_NULL_LEN; michael@0: blobData = SQLITE_EXPLICIT_NULL; michael@0: } michael@0: sqlerr = sqlite3_bind_blob(findstmt, i+1, blobData, blobSize, michael@0: SQLITE_TRANSIENT); michael@0: } michael@0: if (sqlerr == SQLITE_OK) { michael@0: *find = PORT_New(SDBFind); michael@0: if (*find == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: (*find)->findstmt = findstmt; michael@0: (*find)->sqlDB = sqlDB; michael@0: UNLOCK_SQLITE() michael@0: return CKR_OK; michael@0: } michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: michael@0: loser: michael@0: if (findstmt) { michael@0: sqlite3_reset(findstmt); michael@0: sqlite3_finalize(findstmt); michael@0: } michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: UNLOCK_SQLITE() michael@0: return error; michael@0: } michael@0: michael@0: michael@0: CK_RV michael@0: sdb_FindObjects(SDB *sdb, SDBFind *sdbFind, CK_OBJECT_HANDLE *object, michael@0: CK_ULONG arraySize, CK_ULONG *count) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3_stmt *stmt = sdbFind->findstmt; michael@0: int sqlerr = SQLITE_OK; michael@0: int retry = 0; michael@0: michael@0: *count = 0; michael@0: michael@0: if (arraySize == 0) { michael@0: return CKR_OK; michael@0: } michael@0: LOCK_SQLITE() michael@0: michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: if (sqlerr == SQLITE_ROW) { michael@0: /* only care about the id */ michael@0: *object++= sqlite3_column_int(stmt, 0); michael@0: arraySize--; michael@0: (*count)++; michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry) && (arraySize > 0)); michael@0: michael@0: /* we only have some of the objects, there is probably more, michael@0: * set the sqlerr to an OK value so we return CKR_OK */ michael@0: if (sqlerr == SQLITE_ROW && arraySize == 0) { michael@0: sqlerr = SQLITE_DONE; michael@0: } michael@0: UNLOCK_SQLITE() michael@0: michael@0: return sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: } michael@0: michael@0: CK_RV michael@0: sdb_FindObjectsFinal(SDB *sdb, SDBFind *sdbFind) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3_stmt *stmt = sdbFind->findstmt; michael@0: sqlite3 *sqlDB = sdbFind->sqlDB; michael@0: int sqlerr = SQLITE_OK; michael@0: michael@0: LOCK_SQLITE() michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlerr = sqlite3_finalize(stmt); michael@0: } michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: PORT_Free(sdbFind); michael@0: michael@0: UNLOCK_SQLITE() michael@0: return sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: } michael@0: michael@0: static const char GET_ATTRIBUTE_CMD[] = "SELECT ALL %s FROM %s WHERE id=$ID;"; michael@0: CK_RV michael@0: sdb_GetAttributeValueNoLock(SDB *sdb, CK_OBJECT_HANDLE object_id, michael@0: CK_ATTRIBUTE *template, CK_ULONG count) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = NULL; michael@0: sqlite3_stmt *stmt = NULL; michael@0: char *getStr = NULL; michael@0: char *newStr = NULL; michael@0: const char *table = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: int found = 0; michael@0: int retry = 0; michael@0: int i; michael@0: michael@0: michael@0: /* open a new db if necessary */ michael@0: error = sdb_openDBLocal(sdb_p, &sqlDB, &table); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: michael@0: for (i=0; i < count; i++) { michael@0: getStr = sqlite3_mprintf("a%x", template[i].type); michael@0: michael@0: if (getStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: michael@0: newStr = sqlite3_mprintf(GET_ATTRIBUTE_CMD, getStr, table); michael@0: sqlite3_free(getStr); michael@0: getStr = NULL; michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: michael@0: sqlerr = sqlite3_prepare_v2(sqlDB, newStr, -1, &stmt, NULL); michael@0: sqlite3_free(newStr); michael@0: newStr = NULL; michael@0: if (sqlerr == SQLITE_ERROR) { michael@0: template[i].ulValueLen = -1; michael@0: error = CKR_ATTRIBUTE_TYPE_INVALID; michael@0: continue; michael@0: } else if (sqlerr != SQLITE_OK) { goto loser; } michael@0: michael@0: sqlerr = sqlite3_bind_int(stmt, 1, object_id); michael@0: if (sqlerr != SQLITE_OK) { goto loser; } michael@0: michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: if (sqlerr == SQLITE_ROW) { michael@0: int blobSize; michael@0: const char *blobData; michael@0: michael@0: blobSize = sqlite3_column_bytes(stmt, 0); michael@0: blobData = sqlite3_column_blob(stmt, 0); michael@0: if (blobData == NULL) { michael@0: template[i].ulValueLen = -1; michael@0: error = CKR_ATTRIBUTE_TYPE_INVALID; michael@0: break; michael@0: } michael@0: /* If the blob equals our explicit NULL value, then the michael@0: * attribute is a NULL. */ michael@0: if ((blobSize == SQLITE_EXPLICIT_NULL_LEN) && michael@0: (PORT_Memcmp(blobData, SQLITE_EXPLICIT_NULL, michael@0: SQLITE_EXPLICIT_NULL_LEN) == 0)) { michael@0: blobSize = 0; michael@0: } michael@0: if (template[i].pValue) { michael@0: if (template[i].ulValueLen < blobSize) { michael@0: template[i].ulValueLen = -1; michael@0: error = CKR_BUFFER_TOO_SMALL; michael@0: break; michael@0: } michael@0: PORT_Memcpy(template[i].pValue, blobData, blobSize); michael@0: } michael@0: template[i].ulValueLen = blobSize; michael@0: found = 1; michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry)); michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: stmt = NULL; michael@0: } michael@0: michael@0: loser: michael@0: /* fix up the error if necessary */ michael@0: if (error == CKR_OK) { michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: if (!found && error == CKR_OK) { michael@0: error = CKR_OBJECT_HANDLE_INVALID; michael@0: } michael@0: } michael@0: michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: } michael@0: michael@0: /* if we had to open a new database, free it now */ michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: return error; michael@0: } michael@0: michael@0: CK_RV michael@0: sdb_GetAttributeValue(SDB *sdb, CK_OBJECT_HANDLE object_id, michael@0: CK_ATTRIBUTE *template, CK_ULONG count) michael@0: { michael@0: CK_RV crv; michael@0: michael@0: if (count == 0) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: LOCK_SQLITE() michael@0: crv = sdb_GetAttributeValueNoLock(sdb, object_id, template, count); michael@0: UNLOCK_SQLITE() michael@0: return crv; michael@0: } michael@0: michael@0: static const char SET_ATTRIBUTE_CMD[] = "UPDATE %s SET %s WHERE id=$ID;"; michael@0: CK_RV michael@0: sdb_SetAttributeValue(SDB *sdb, CK_OBJECT_HANDLE object_id, michael@0: const CK_ATTRIBUTE *template, CK_ULONG count) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = NULL; michael@0: sqlite3_stmt *stmt = NULL; michael@0: char *setStr = NULL; michael@0: char *newStr = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: int retry = 0; michael@0: CK_RV error = CKR_OK; michael@0: int i; michael@0: michael@0: if ((sdb->sdb_flags & SDB_RDONLY) != 0) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: if (count == 0) { michael@0: return CKR_OK; michael@0: } michael@0: michael@0: LOCK_SQLITE() michael@0: setStr = sqlite3_mprintf(""); michael@0: for (i=0; setStr && i < count; i++) { michael@0: if (i==0) { michael@0: sqlite3_free(setStr); michael@0: setStr = sqlite3_mprintf("a%x=$VALUE%d", michael@0: template[i].type, i); michael@0: continue; michael@0: } michael@0: newStr = sqlite3_mprintf("%s,a%x=$VALUE%d", setStr, michael@0: template[i].type, i); michael@0: sqlite3_free(setStr); michael@0: setStr = newStr; michael@0: } michael@0: newStr = NULL; michael@0: michael@0: if (setStr == NULL) { michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: newStr = sqlite3_mprintf(SET_ATTRIBUTE_CMD, sdb_p->table, setStr); michael@0: sqlite3_free(setStr); michael@0: if (newStr == NULL) { michael@0: UNLOCK_SQLITE() michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: error = sdb_openDBLocal(sdb_p, &sqlDB, NULL); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_prepare_v2(sqlDB, newStr, -1, &stmt, NULL); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: for (i=0; i < count; i++) { michael@0: if (template[i].ulValueLen != 0) { michael@0: sqlerr = sqlite3_bind_blob(stmt, i+1, template[i].pValue, michael@0: template[i].ulValueLen, SQLITE_STATIC); michael@0: } else { michael@0: sqlerr = sqlite3_bind_blob(stmt, i+2, SQLITE_EXPLICIT_NULL, michael@0: SQLITE_EXPLICIT_NULL_LEN, SQLITE_STATIC); michael@0: } michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: } michael@0: sqlerr = sqlite3_bind_int(stmt, i+1, object_id); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry)); michael@0: michael@0: loser: michael@0: if (newStr) { michael@0: sqlite3_free(newStr); michael@0: } michael@0: if (error == CKR_OK) { michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: } michael@0: michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: } michael@0: michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: michael@0: UNLOCK_SQLITE() michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * check to see if a candidate object handle already exists. michael@0: */ michael@0: static PRBool michael@0: sdb_objectExists(SDB *sdb, CK_OBJECT_HANDLE candidate) michael@0: { michael@0: CK_RV crv; michael@0: CK_ATTRIBUTE template = { CKA_LABEL, NULL, 0 }; michael@0: michael@0: crv = sdb_GetAttributeValueNoLock(sdb,candidate,&template, 1); michael@0: if (crv == CKR_OBJECT_HANDLE_INVALID) { michael@0: return PR_FALSE; michael@0: } michael@0: return PR_TRUE; michael@0: } michael@0: michael@0: /* michael@0: * if we're here, we are in a transaction, so it's safe michael@0: * to examine the current state of the database michael@0: */ michael@0: static CK_OBJECT_HANDLE michael@0: sdb_getObjectId(SDB *sdb) michael@0: { michael@0: CK_OBJECT_HANDLE candidate; michael@0: static CK_OBJECT_HANDLE next_obj = CK_INVALID_HANDLE; michael@0: int count; michael@0: /* michael@0: * get an initial object handle to use michael@0: */ michael@0: if (next_obj == CK_INVALID_HANDLE) { michael@0: PRTime time; michael@0: time = PR_Now(); michael@0: michael@0: next_obj = (CK_OBJECT_HANDLE)(time & 0x3fffffffL); michael@0: } michael@0: candidate = next_obj++; michael@0: /* detect that we've looped through all the handles... */ michael@0: for (count = 0; count < 0x40000000; count++, candidate = next_obj++) { michael@0: /* mask off excess bits */ michael@0: candidate &= 0x3fffffff; michael@0: /* if we hit zero, go to the next entry */ michael@0: if (candidate == CK_INVALID_HANDLE) { michael@0: continue; michael@0: } michael@0: /* make sure we aren't already using */ michael@0: if (!sdb_objectExists(sdb, candidate)) { michael@0: /* this one is free */ michael@0: return candidate; michael@0: } michael@0: } michael@0: michael@0: /* no handle is free, fail */ michael@0: return CK_INVALID_HANDLE; michael@0: } michael@0: michael@0: static const char CREATE_CMD[] = "INSERT INTO %s (id%s) VALUES($ID%s);"; michael@0: CK_RV michael@0: sdb_CreateObject(SDB *sdb, CK_OBJECT_HANDLE *object_id, michael@0: const CK_ATTRIBUTE *template, CK_ULONG count) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = NULL; michael@0: sqlite3_stmt *stmt = NULL; michael@0: char *columnStr = NULL; michael@0: char *valueStr = NULL; michael@0: char *newStr = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: CK_OBJECT_HANDLE this_object = CK_INVALID_HANDLE; michael@0: int retry = 0; michael@0: int i; michael@0: michael@0: if ((sdb->sdb_flags & SDB_RDONLY) != 0) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: LOCK_SQLITE() michael@0: if ((*object_id != CK_INVALID_HANDLE) && michael@0: !sdb_objectExists(sdb, *object_id)) { michael@0: this_object = *object_id; michael@0: } else { michael@0: this_object = sdb_getObjectId(sdb); michael@0: } michael@0: if (this_object == CK_INVALID_HANDLE) { michael@0: UNLOCK_SQLITE(); michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: columnStr = sqlite3_mprintf(""); michael@0: valueStr = sqlite3_mprintf(""); michael@0: *object_id = this_object; michael@0: for (i=0; columnStr && valueStr && i < count; i++) { michael@0: newStr = sqlite3_mprintf("%s,a%x", columnStr, template[i].type); michael@0: sqlite3_free(columnStr); michael@0: columnStr = newStr; michael@0: newStr = sqlite3_mprintf("%s,$VALUE%d", valueStr, i); michael@0: sqlite3_free(valueStr); michael@0: valueStr = newStr; michael@0: } michael@0: newStr = NULL; michael@0: if ((columnStr == NULL) || (valueStr == NULL)) { michael@0: if (columnStr) { michael@0: sqlite3_free(columnStr); michael@0: } michael@0: if (valueStr) { michael@0: sqlite3_free(valueStr); michael@0: } michael@0: UNLOCK_SQLITE() michael@0: return CKR_HOST_MEMORY; michael@0: } michael@0: newStr = sqlite3_mprintf(CREATE_CMD, sdb_p->table, columnStr, valueStr); michael@0: sqlite3_free(columnStr); michael@0: sqlite3_free(valueStr); michael@0: error = sdb_openDBLocal(sdb_p, &sqlDB, NULL); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_prepare_v2(sqlDB, newStr, -1, &stmt, NULL); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: sqlerr = sqlite3_bind_int(stmt, 1, *object_id); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: for (i=0; i < count; i++) { michael@0: if (template[i].ulValueLen) { michael@0: sqlerr = sqlite3_bind_blob(stmt, i+2, template[i].pValue, michael@0: template[i].ulValueLen, SQLITE_STATIC); michael@0: } else { michael@0: sqlerr = sqlite3_bind_blob(stmt, i+2, SQLITE_EXPLICIT_NULL, michael@0: SQLITE_EXPLICIT_NULL_LEN, SQLITE_STATIC); michael@0: } michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: } michael@0: michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry)); michael@0: michael@0: loser: michael@0: if (newStr) { michael@0: sqlite3_free(newStr); michael@0: } michael@0: if (error == CKR_OK) { michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: } michael@0: michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: } michael@0: michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: UNLOCK_SQLITE() michael@0: michael@0: return error; michael@0: } michael@0: michael@0: static const char DESTROY_CMD[] = "DELETE FROM %s WHERE (id=$ID);"; michael@0: CK_RV michael@0: sdb_DestroyObject(SDB *sdb, CK_OBJECT_HANDLE object_id) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = NULL; michael@0: sqlite3_stmt *stmt = NULL; michael@0: char *newStr = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: int retry = 0; michael@0: michael@0: if ((sdb->sdb_flags & SDB_RDONLY) != 0) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: LOCK_SQLITE() michael@0: error = sdb_openDBLocal(sdb_p, &sqlDB, NULL); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: newStr = sqlite3_mprintf(DESTROY_CMD, sdb_p->table); michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: sqlerr =sqlite3_prepare_v2(sqlDB, newStr, -1, &stmt, NULL); michael@0: sqlite3_free(newStr); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: sqlerr =sqlite3_bind_int(stmt, 1, object_id); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry)); michael@0: michael@0: loser: michael@0: if (error == CKR_OK) { michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: } michael@0: michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: } michael@0: michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: michael@0: UNLOCK_SQLITE() michael@0: return error; michael@0: } michael@0: michael@0: static const char BEGIN_CMD[] = "BEGIN IMMEDIATE TRANSACTION;"; michael@0: /* michael@0: * start a transaction. michael@0: * michael@0: * We need to open a new database, then store that new database into michael@0: * the private data structure. We open the database first, then use locks michael@0: * to protect storing the data to prevent deadlocks. michael@0: */ michael@0: CK_RV michael@0: sdb_Begin(SDB *sdb) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = NULL; michael@0: sqlite3_stmt *stmt = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: int retry = 0; michael@0: michael@0: michael@0: if ((sdb->sdb_flags & SDB_RDONLY) != 0) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: michael@0: LOCK_SQLITE() michael@0: michael@0: /* get a new version that we will use for the entire transaction */ michael@0: sqlerr = sdb_openDB(sdb_p->sqlDBName, &sqlDB, SDB_RDWR); michael@0: if (sqlerr != SQLITE_OK) { michael@0: goto loser; michael@0: } michael@0: michael@0: sqlerr =sqlite3_prepare_v2(sqlDB, BEGIN_CMD, -1, &stmt, NULL); michael@0: michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry)); michael@0: michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: } michael@0: michael@0: loser: michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: michael@0: /* we are starting a new transaction, michael@0: * and if we succeeded, then save this database for the rest of michael@0: * our transaction */ michael@0: if (error == CKR_OK) { michael@0: /* we hold a 'BEGIN TRANSACTION' and a sdb_p->lock. At this point michael@0: * sdb_p->sqlXactDB MUST be null */ michael@0: PR_EnterMonitor(sdb_p->dbMon); michael@0: PORT_Assert(sdb_p->sqlXactDB == NULL); michael@0: sdb_p->sqlXactDB = sqlDB; michael@0: sdb_p->sqlXactThread = PR_GetCurrentThread(); michael@0: PR_ExitMonitor(sdb_p->dbMon); michael@0: } else { michael@0: /* we failed to start our transaction, michael@0: * free any databases we opened. */ michael@0: if (sqlDB) { michael@0: sqlite3_close(sqlDB); michael@0: } michael@0: } michael@0: michael@0: UNLOCK_SQLITE() michael@0: return error; michael@0: } michael@0: michael@0: /* michael@0: * Complete a transaction. Basically undo everything we did in begin. michael@0: * There are 2 flavors Abort and Commit. Basically the only differerence between michael@0: * these 2 are what the database will show. (no change in to former, change in michael@0: * the latter). michael@0: */ michael@0: static CK_RV michael@0: sdb_complete(SDB *sdb, const char *cmd) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = NULL; michael@0: sqlite3_stmt *stmt = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: int retry = 0; michael@0: michael@0: michael@0: if ((sdb->sdb_flags & SDB_RDONLY) != 0) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: /* We must have a transation database, or we shouldn't have arrived here */ michael@0: PR_EnterMonitor(sdb_p->dbMon); michael@0: PORT_Assert(sdb_p->sqlXactDB); michael@0: if (sdb_p->sqlXactDB == NULL) { michael@0: PR_ExitMonitor(sdb_p->dbMon); michael@0: return CKR_GENERAL_ERROR; /* shouldn't happen */ michael@0: } michael@0: PORT_Assert( sdb_p->sqlXactThread == PR_GetCurrentThread()); michael@0: if ( sdb_p->sqlXactThread != PR_GetCurrentThread()) { michael@0: PR_ExitMonitor(sdb_p->dbMon); michael@0: return CKR_GENERAL_ERROR; /* shouldn't happen */ michael@0: } michael@0: sqlDB = sdb_p->sqlXactDB; michael@0: sdb_p->sqlXactDB = NULL; /* no one else can get to this DB, michael@0: * safe to unlock */ michael@0: sdb_p->sqlXactThread = NULL; michael@0: PR_ExitMonitor(sdb_p->dbMon); michael@0: michael@0: sqlerr =sqlite3_prepare_v2(sqlDB, cmd, -1, &stmt, NULL); michael@0: michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry)); michael@0: michael@0: /* Pending BEGIN TRANSACTIONS Can move forward at this point. */ michael@0: michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: } michael@0: michael@0: /* we we have a cached DB image, update it as well */ michael@0: if (sdb_p->cacheTable) { michael@0: PR_EnterMonitor(sdb_p->dbMon); michael@0: sdb_updateCache(sdb_p); michael@0: PR_ExitMonitor(sdb_p->dbMon); michael@0: } michael@0: michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: michael@0: /* We just finished a transaction. michael@0: * Free the database, and remove it from the list */ michael@0: sqlite3_close(sqlDB); michael@0: michael@0: return error; michael@0: } michael@0: michael@0: static const char COMMIT_CMD[] = "COMMIT TRANSACTION;"; michael@0: CK_RV michael@0: sdb_Commit(SDB *sdb) michael@0: { michael@0: CK_RV crv; michael@0: LOCK_SQLITE() michael@0: crv = sdb_complete(sdb,COMMIT_CMD); michael@0: UNLOCK_SQLITE() michael@0: return crv; michael@0: } michael@0: michael@0: static const char ROLLBACK_CMD[] = "ROLLBACK TRANSACTION;"; michael@0: CK_RV michael@0: sdb_Abort(SDB *sdb) michael@0: { michael@0: CK_RV crv; michael@0: LOCK_SQLITE() michael@0: crv = sdb_complete(sdb,ROLLBACK_CMD); michael@0: UNLOCK_SQLITE() michael@0: return crv; michael@0: } michael@0: michael@0: static int tableExists(sqlite3 *sqlDB, const char *tableName); michael@0: michael@0: static const char GET_PW_CMD[] = "SELECT ALL * FROM metaData WHERE id=$ID;"; michael@0: CK_RV michael@0: sdb_GetMetaData(SDB *sdb, const char *id, SECItem *item1, SECItem *item2) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = sdb_p->sqlXactDB; michael@0: sqlite3_stmt *stmt = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: int found = 0; michael@0: int retry = 0; michael@0: michael@0: LOCK_SQLITE() michael@0: error = sdb_openDBLocal(sdb_p, &sqlDB, NULL); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* handle 'test' versions of the sqlite db */ michael@0: sqlerr = sqlite3_prepare_v2(sqlDB, GET_PW_CMD, -1, &stmt, NULL); michael@0: /* Sigh, if we created a new table since we opened the database, michael@0: * the database handle will not see the new table, we need to close this michael@0: * database and reopen it. This is safe because we are holding the lock michael@0: * still. */ michael@0: if (sqlerr == SQLITE_SCHEMA) { michael@0: sqlerr = sdb_reopenDBLocal(sdb_p, &sqlDB); michael@0: if (sqlerr != SQLITE_OK) { michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_prepare_v2(sqlDB, GET_PW_CMD, -1, &stmt, NULL); michael@0: } michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: sqlerr = sqlite3_bind_text(stmt, 1, id, PORT_Strlen(id), SQLITE_STATIC); michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: if (sqlerr == SQLITE_ROW) { michael@0: const char *blobData; michael@0: unsigned int len = item1->len; michael@0: item1->len = sqlite3_column_bytes(stmt, 1); michael@0: if (item1->len > len) { michael@0: error = CKR_BUFFER_TOO_SMALL; michael@0: continue; michael@0: } michael@0: blobData = sqlite3_column_blob(stmt, 1); michael@0: PORT_Memcpy(item1->data,blobData, item1->len); michael@0: if (item2) { michael@0: len = item2->len; michael@0: item2->len = sqlite3_column_bytes(stmt, 2); michael@0: if (item2->len > len) { michael@0: error = CKR_BUFFER_TOO_SMALL; michael@0: continue; michael@0: } michael@0: blobData = sqlite3_column_blob(stmt, 2); michael@0: PORT_Memcpy(item2->data,blobData, item2->len); michael@0: } michael@0: found = 1; michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry)); michael@0: michael@0: loser: michael@0: /* fix up the error if necessary */ michael@0: if (error == CKR_OK) { michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: if (!found && error == CKR_OK) { michael@0: error = CKR_OBJECT_HANDLE_INVALID; michael@0: } michael@0: } michael@0: michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: } michael@0: michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: UNLOCK_SQLITE() michael@0: michael@0: return error; michael@0: } michael@0: michael@0: static const char PW_CREATE_TABLE_CMD[] = michael@0: "CREATE TABLE metaData (id PRIMARY KEY UNIQUE ON CONFLICT REPLACE, item1, item2);"; michael@0: static const char PW_CREATE_CMD[] = michael@0: "INSERT INTO metaData (id,item1,item2) VALUES($ID,$ITEM1,$ITEM2);"; michael@0: static const char MD_CREATE_CMD[] = michael@0: "INSERT INTO metaData (id,item1) VALUES($ID,$ITEM1);"; michael@0: CK_RV michael@0: sdb_PutMetaData(SDB *sdb, const char *id, const SECItem *item1, michael@0: const SECItem *item2) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = sdb_p->sqlXactDB; michael@0: sqlite3_stmt *stmt = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: int retry = 0; michael@0: const char *cmd = PW_CREATE_CMD; michael@0: michael@0: if ((sdb->sdb_flags & SDB_RDONLY) != 0) { michael@0: return CKR_TOKEN_WRITE_PROTECTED; michael@0: } michael@0: michael@0: LOCK_SQLITE() michael@0: error = sdb_openDBLocal(sdb_p, &sqlDB, NULL); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: michael@0: if (!tableExists(sqlDB, "metaData")) { michael@0: sqlerr = sqlite3_exec(sqlDB, PW_CREATE_TABLE_CMD, NULL, 0, NULL); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: } michael@0: if (item2 == NULL) { michael@0: cmd = MD_CREATE_CMD; michael@0: } michael@0: sqlerr = sqlite3_prepare_v2(sqlDB, cmd, -1, &stmt, NULL); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: sqlerr = sqlite3_bind_text(stmt, 1, id, PORT_Strlen(id), SQLITE_STATIC); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: sqlerr = sqlite3_bind_blob(stmt, 2, item1->data, item1->len, SQLITE_STATIC); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: if (item2) { michael@0: sqlerr = sqlite3_bind_blob(stmt, 3, item2->data, michael@0: item2->len, SQLITE_STATIC); michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: } michael@0: michael@0: do { michael@0: sqlerr = sqlite3_step(stmt); michael@0: if (sqlerr == SQLITE_BUSY) { michael@0: PR_Sleep(SDB_BUSY_RETRY_TIME); michael@0: } michael@0: } while (!sdb_done(sqlerr,&retry)); michael@0: michael@0: loser: michael@0: /* fix up the error if necessary */ michael@0: if (error == CKR_OK) { michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: } michael@0: michael@0: if (stmt) { michael@0: sqlite3_reset(stmt); michael@0: sqlite3_finalize(stmt); michael@0: } michael@0: michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: UNLOCK_SQLITE() michael@0: michael@0: return error; michael@0: } michael@0: michael@0: static const char RESET_CMD[] = "DROP TABLE IF EXISTS %s;"; michael@0: CK_RV michael@0: sdb_Reset(SDB *sdb) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: sqlite3 *sqlDB = NULL; michael@0: char *newStr; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: michael@0: /* only Key databases can be reset */ michael@0: if (sdb_p->type != SDB_KEY) { michael@0: return CKR_OBJECT_HANDLE_INVALID; michael@0: } michael@0: michael@0: LOCK_SQLITE() michael@0: error = sdb_openDBLocal(sdb_p, &sqlDB, NULL); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: michael@0: /* delete the key table */ michael@0: newStr = sqlite3_mprintf(RESET_CMD, sdb_p->table); michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: michael@0: if (sqlerr != SQLITE_OK) goto loser; michael@0: michael@0: /* delete the password entry table */ michael@0: sqlerr = sqlite3_exec(sqlDB, "DROP TABLE IF EXISTS metaData;", michael@0: NULL, 0, NULL); michael@0: michael@0: loser: michael@0: /* fix up the error if necessary */ michael@0: if (error == CKR_OK) { michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: } michael@0: michael@0: if (sqlDB) { michael@0: sdb_closeDBLocal(sdb_p, sqlDB) ; michael@0: } michael@0: michael@0: UNLOCK_SQLITE() michael@0: return error; michael@0: } michael@0: michael@0: michael@0: CK_RV michael@0: sdb_Close(SDB *sdb) michael@0: { michael@0: SDBPrivate *sdb_p = sdb->private; michael@0: int sqlerr = SQLITE_OK; michael@0: sdbDataType type = sdb_p->type; michael@0: michael@0: sqlerr = sqlite3_close(sdb_p->sqlReadDB); michael@0: PORT_Free(sdb_p->sqlDBName); michael@0: if (sdb_p->cacheTable) { michael@0: sqlite3_free(sdb_p->cacheTable); michael@0: } michael@0: if (sdb_p->dbMon) { michael@0: PR_DestroyMonitor(sdb_p->dbMon); michael@0: } michael@0: free(sdb_p); michael@0: free(sdb); michael@0: return sdb_mapSQLError(type, sqlerr); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * functions to support open michael@0: */ michael@0: michael@0: static const char CHECK_TABLE_CMD[] = "SELECT ALL * FROM %s LIMIT 0;"; michael@0: /* return 1 if sqlDB contains table 'tableName */ michael@0: static int tableExists(sqlite3 *sqlDB, const char *tableName) michael@0: { michael@0: char * cmd = sqlite3_mprintf(CHECK_TABLE_CMD, tableName); michael@0: int sqlerr = SQLITE_OK; michael@0: michael@0: if (cmd == NULL) { michael@0: return 0; michael@0: } michael@0: michael@0: sqlerr = sqlite3_exec(sqlDB, cmd, NULL, 0, 0); michael@0: sqlite3_free(cmd); michael@0: michael@0: return (sqlerr == SQLITE_OK) ? 1 : 0; michael@0: } michael@0: michael@0: void sdb_SetForkState(PRBool forked) michael@0: { michael@0: /* XXXright now this is a no-op. The global fork state in the softokn3 michael@0: * shared library is already taken care of at the PKCS#11 level. michael@0: * If and when we add fork state to the sqlite shared library and extern michael@0: * interface, we will need to set it and reset it from here */ michael@0: } michael@0: michael@0: /* michael@0: * initialize a single database michael@0: */ michael@0: static const char INIT_CMD[] = michael@0: "CREATE TABLE %s (id PRIMARY KEY UNIQUE ON CONFLICT ABORT%s)"; michael@0: static const char ALTER_CMD[] = michael@0: "ALTER TABLE %s ADD COLUMN a%x"; michael@0: michael@0: CK_RV michael@0: sdb_init(char *dbname, char *table, sdbDataType type, int *inUpdate, michael@0: int *newInit, int flags, PRUint32 accessOps, SDB **pSdb) michael@0: { michael@0: int i; michael@0: char *initStr = NULL; michael@0: char *newStr; michael@0: int inTransaction = 0; michael@0: SDB *sdb = NULL; michael@0: SDBPrivate *sdb_p = NULL; michael@0: sqlite3 *sqlDB = NULL; michael@0: int sqlerr = SQLITE_OK; michael@0: CK_RV error = CKR_OK; michael@0: char *cacheTable = NULL; michael@0: PRIntervalTime now = 0; michael@0: char *env; michael@0: PRBool enableCache = PR_FALSE; michael@0: PRBool create; michael@0: michael@0: *pSdb = NULL; michael@0: *inUpdate = 0; michael@0: michael@0: /* sqlite3 doesn't have a flag to specify that we want to michael@0: * open the database read only. If the db doesn't exist, michael@0: * sqlite3 will always create it. michael@0: */ michael@0: LOCK_SQLITE(); michael@0: create = (PR_Access(dbname, PR_ACCESS_EXISTS) != PR_SUCCESS); michael@0: if ((flags == SDB_RDONLY) && create) { michael@0: error = sdb_mapSQLError(type, SQLITE_CANTOPEN); michael@0: goto loser; michael@0: } michael@0: sqlerr = sdb_openDB(dbname, &sqlDB, flags); michael@0: if (sqlerr != SQLITE_OK) { michael@0: error = sdb_mapSQLError(type, sqlerr); michael@0: goto loser; michael@0: } michael@0: /* sql created the file, but it doesn't set appropriate modes for michael@0: * a database */ michael@0: if (create) { michael@0: /* NO NSPR call for this? :( */ michael@0: chmod (dbname, 0600); michael@0: } michael@0: michael@0: if (flags != SDB_RDONLY) { michael@0: sqlerr = sqlite3_exec(sqlDB, BEGIN_CMD, NULL, 0, NULL); michael@0: if (sqlerr != SQLITE_OK) { michael@0: error = sdb_mapSQLError(type, sqlerr); michael@0: goto loser; michael@0: } michael@0: inTransaction = 1; michael@0: } michael@0: if (!tableExists(sqlDB,table)) { michael@0: *newInit = 1; michael@0: if (flags != SDB_CREATE) { michael@0: error = sdb_mapSQLError(type, SQLITE_CANTOPEN); michael@0: goto loser; michael@0: } michael@0: initStr = sqlite3_mprintf(""); michael@0: for (i=0; initStr && i < known_attributes_size; i++) { michael@0: newStr = sqlite3_mprintf("%s, a%x",initStr, known_attributes[i]); michael@0: sqlite3_free(initStr); michael@0: initStr = newStr; michael@0: } michael@0: if (initStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: michael@0: newStr = sqlite3_mprintf(INIT_CMD, table, initStr); michael@0: sqlite3_free(initStr); michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: if (sqlerr != SQLITE_OK) { michael@0: error = sdb_mapSQLError(type, sqlerr); michael@0: goto loser; michael@0: } michael@0: michael@0: newStr = sqlite3_mprintf(CREATE_ISSUER_INDEX_CMD, table); michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: if (sqlerr != SQLITE_OK) { michael@0: error = sdb_mapSQLError(type, sqlerr); michael@0: goto loser; michael@0: } michael@0: michael@0: newStr = sqlite3_mprintf(CREATE_SUBJECT_INDEX_CMD, table); michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: if (sqlerr != SQLITE_OK) { michael@0: error = sdb_mapSQLError(type, sqlerr); michael@0: goto loser; michael@0: } michael@0: michael@0: newStr = sqlite3_mprintf(CREATE_LABEL_INDEX_CMD, table); michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: if (sqlerr != SQLITE_OK) { michael@0: error = sdb_mapSQLError(type, sqlerr); michael@0: goto loser; michael@0: } michael@0: michael@0: newStr = sqlite3_mprintf(CREATE_ID_INDEX_CMD, table); michael@0: if (newStr == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL); michael@0: sqlite3_free(newStr); michael@0: if (sqlerr != SQLITE_OK) { michael@0: error = sdb_mapSQLError(type, sqlerr); michael@0: goto loser; michael@0: } michael@0: } michael@0: /* michael@0: * detect the case where we have created the database, but have michael@0: * not yet updated it. michael@0: * michael@0: * We only check the Key database because only the key database has michael@0: * a metaData table. The metaData table is created when a password michael@0: * is set, or in the case of update, when a password is supplied. michael@0: * If no key database exists, then the update would have happened immediately michael@0: * on noticing that the cert database didn't exist (see newInit set above). michael@0: */ michael@0: if (type == SDB_KEY && !tableExists(sqlDB, "metaData")) { michael@0: *newInit = 1; michael@0: } michael@0: michael@0: /* access to network filesystems are significantly slower than local ones michael@0: * for database operations. In those cases we need to create a cached copy michael@0: * of the database in a temporary location on the local disk. SQLITE michael@0: * already provides a way to create a temporary table and initialize it, michael@0: * so we use it for the cache (see sdb_buildCache for how it's done).*/ michael@0: michael@0: /* michael@0: * we decide whether or not to use the cache based on the following input. michael@0: * michael@0: * NSS_SDB_USE_CACHE environment variable is non-existant or set to michael@0: * anything other than "no" or "yes" ("auto", for instance). michael@0: * This is the normal case. NSS will measure the performance of access michael@0: * to the temp database versus the access to the users passed in michael@0: * database location. If the temp database location is "significantly" michael@0: * faster we will use the cache. michael@0: * michael@0: * NSS_SDB_USE_CACHE environment variable is set to "no": cache will not michael@0: * be used. michael@0: * michael@0: * NSS_SDB_USE_CACHE environment variable is set to "yes": cache will michael@0: * always be used. michael@0: * michael@0: * It is expected that most applications would use the "auto" selection, michael@0: * the environment variable is primarily to simplify testing, and to michael@0: * correct potential corner cases where */ michael@0: michael@0: env = PR_GetEnv("NSS_SDB_USE_CACHE"); michael@0: michael@0: if (env && PORT_Strcasecmp(env,"no") == 0) { michael@0: enableCache = PR_FALSE; michael@0: } else if (env && PORT_Strcasecmp(env,"yes") == 0) { michael@0: enableCache = PR_TRUE; michael@0: } else { michael@0: char *tempDir = NULL; michael@0: PRUint32 tempOps = 0; michael@0: /* michael@0: * Use PR_Access to determine how expensive it michael@0: * is to check for the existance of a local file compared to the same michael@0: * check in the temp directory. If the temp directory is faster, cache michael@0: * the database there. */ michael@0: tempDir = sdb_getTempDir(sqlDB); michael@0: if (tempDir) { michael@0: tempOps = sdb_measureAccess(tempDir); michael@0: PORT_Free(tempDir); michael@0: michael@0: /* There is a cost to continually copying the database. michael@0: * Account for that cost with the arbitrary factor of 10 */ michael@0: enableCache = (PRBool)(tempOps > accessOps * 10); michael@0: } michael@0: } michael@0: michael@0: if (enableCache) { michael@0: /* try to set the temp store to memory.*/ michael@0: sqlite3_exec(sqlDB, "PRAGMA temp_store=MEMORY", NULL, 0, NULL); michael@0: /* Failure to set the temp store to memory is not fatal, michael@0: * ignore the error */ michael@0: michael@0: cacheTable = sqlite3_mprintf("%sCache",table); michael@0: if (cacheTable == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: /* build the cache table */ michael@0: error = sdb_buildCache(sqlDB, type, cacheTable, table); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: /* initialize the last cache build time */ michael@0: now = PR_IntervalNow(); michael@0: } michael@0: michael@0: sdb = (SDB *) malloc(sizeof(SDB)); michael@0: sdb_p = (SDBPrivate *) malloc(sizeof(SDBPrivate)); michael@0: michael@0: /* invariant fields */ michael@0: sdb_p->sqlDBName = PORT_Strdup(dbname); michael@0: sdb_p->type = type; michael@0: sdb_p->table = table; michael@0: sdb_p->cacheTable = cacheTable; michael@0: sdb_p->lastUpdateTime = now; michael@0: /* set the cache delay time. This is how long we will wait before we michael@0: * decide the existing cache is stale. Currently set to 10 sec */ michael@0: sdb_p->updateInterval = PR_SecondsToInterval(10); michael@0: sdb_p->dbMon = PR_NewMonitor(); michael@0: /* these fields are protected by the lock */ michael@0: sdb_p->sqlXactDB = NULL; michael@0: sdb_p->sqlXactThread = NULL; michael@0: sdb->private = sdb_p; michael@0: sdb->version = 0; michael@0: sdb->sdb_flags = flags | SDB_HAS_META; michael@0: sdb->app_private = NULL; michael@0: sdb->sdb_FindObjectsInit = sdb_FindObjectsInit; michael@0: sdb->sdb_FindObjects = sdb_FindObjects; michael@0: sdb->sdb_FindObjectsFinal = sdb_FindObjectsFinal; michael@0: sdb->sdb_GetAttributeValue = sdb_GetAttributeValue; michael@0: sdb->sdb_SetAttributeValue = sdb_SetAttributeValue; michael@0: sdb->sdb_CreateObject = sdb_CreateObject; michael@0: sdb->sdb_DestroyObject = sdb_DestroyObject; michael@0: sdb->sdb_GetMetaData = sdb_GetMetaData; michael@0: sdb->sdb_PutMetaData = sdb_PutMetaData; michael@0: sdb->sdb_Begin = sdb_Begin; michael@0: sdb->sdb_Commit = sdb_Commit; michael@0: sdb->sdb_Abort = sdb_Abort; michael@0: sdb->sdb_Reset = sdb_Reset; michael@0: sdb->sdb_Close = sdb_Close; michael@0: sdb->sdb_SetForkState = sdb_SetForkState; michael@0: michael@0: if (inTransaction) { michael@0: sqlerr = sqlite3_exec(sqlDB, COMMIT_CMD, NULL, 0, NULL); michael@0: if (sqlerr != SQLITE_OK) { michael@0: error = sdb_mapSQLError(sdb_p->type, sqlerr); michael@0: goto loser; michael@0: } michael@0: inTransaction = 0; michael@0: } michael@0: michael@0: sdb_p->sqlReadDB = sqlDB; michael@0: michael@0: *pSdb = sdb; michael@0: UNLOCK_SQLITE(); michael@0: return CKR_OK; michael@0: michael@0: loser: michael@0: /* lots of stuff to do */ michael@0: if (inTransaction) { michael@0: sqlite3_exec(sqlDB, ROLLBACK_CMD, NULL, 0, NULL); michael@0: } michael@0: if (sdb) { michael@0: free(sdb); michael@0: } michael@0: if (sdb_p) { michael@0: free(sdb_p); michael@0: } michael@0: if (sqlDB) { michael@0: sqlite3_close(sqlDB); michael@0: } michael@0: UNLOCK_SQLITE(); michael@0: return error; michael@0: michael@0: } michael@0: michael@0: michael@0: /* sdbopen */ michael@0: CK_RV michael@0: s_open(const char *directory, const char *certPrefix, const char *keyPrefix, michael@0: int cert_version, int key_version, int flags, michael@0: SDB **certdb, SDB **keydb, int *newInit) michael@0: { michael@0: char *cert = sdb_BuildFileName(directory, certPrefix, michael@0: "cert", cert_version); michael@0: char *key = sdb_BuildFileName(directory, keyPrefix, michael@0: "key", key_version); michael@0: CK_RV error = CKR_OK; michael@0: int inUpdate; michael@0: PRUint32 accessOps; michael@0: michael@0: if (certdb) michael@0: *certdb = NULL; michael@0: if (keydb) michael@0: *keydb = NULL; michael@0: *newInit = 0; michael@0: michael@0: #ifdef SQLITE_UNSAFE_THREADS michael@0: if (sqlite_lock == NULL) { michael@0: sqlite_lock = PR_NewLock(); michael@0: if (sqlite_lock == NULL) { michael@0: error = CKR_HOST_MEMORY; michael@0: goto loser; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* how long does it take to test for a non-existant file in our working michael@0: * directory? Allows us to test if we may be on a network file system */ michael@0: accessOps = 1; michael@0: { michael@0: char *env; michael@0: env = PR_GetEnv("NSS_SDB_USE_CACHE"); michael@0: /* If the environment variable is set to yes or no, sdb_init() will michael@0: * ignore the value of accessOps, and we can skip the measuring.*/ michael@0: if (!env || ((PORT_Strcasecmp(env, "no") != 0) && michael@0: (PORT_Strcasecmp(env, "yes") != 0))){ michael@0: accessOps = sdb_measureAccess(directory); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * open the cert data base michael@0: */ michael@0: if (certdb) { michael@0: /* initialize Certificate database */ michael@0: error = sdb_init(cert, "nssPublic", SDB_CERT, &inUpdate, michael@0: newInit, flags, accessOps, certdb); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * open the key data base: michael@0: * NOTE:if we want to implement a single database, we open michael@0: * the same database file as the certificate here. michael@0: * michael@0: * cert an key db's have different tables, so they will not michael@0: * conflict. michael@0: */ michael@0: if (keydb) { michael@0: /* initialize the Key database */ michael@0: error = sdb_init(key, "nssPrivate", SDB_KEY, &inUpdate, michael@0: newInit, flags, accessOps, keydb); michael@0: if (error != CKR_OK) { michael@0: goto loser; michael@0: } michael@0: } michael@0: michael@0: michael@0: loser: michael@0: if (cert) { michael@0: sqlite3_free(cert); michael@0: } michael@0: if (key) { michael@0: sqlite3_free(key); michael@0: } michael@0: michael@0: if (error != CKR_OK) { michael@0: /* currently redundant, but could be necessary if more code is added michael@0: * just before loser */ michael@0: if (keydb && *keydb) { michael@0: sdb_Close(*keydb); michael@0: } michael@0: if (certdb && *certdb) { michael@0: sdb_Close(*certdb); michael@0: } michael@0: } michael@0: michael@0: return error; michael@0: } michael@0: michael@0: CK_RV michael@0: s_shutdown() michael@0: { michael@0: #ifdef SQLITE_UNSAFE_THREADS michael@0: if (sqlite_lock) { michael@0: PR_DestroyLock(sqlite_lock); michael@0: sqlite_lock = NULL; michael@0: } michael@0: #endif michael@0: return CKR_OK; michael@0: }