|
|
285d04 |
diff --git a/cmd/dbtool/Makefile b/cmd/dbtool/Makefile
|
|
|
285d04 |
new file mode 100644
|
|
|
285d04 |
--- /dev/null
|
|
|
285d04 |
+++ b/cmd/dbtool/Makefile
|
|
|
285d04 |
@@ -0,0 +1,46 @@
|
|
|
285d04 |
+#! gmake
|
|
|
285d04 |
+#
|
|
|
285d04 |
+# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
285d04 |
+# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
285d04 |
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
285d04 |
+
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+# (1) Include initial platform-independent assignments (MANDATORY). #
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+
|
|
|
285d04 |
+include manifest.mn
|
|
|
285d04 |
+
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+# (2) Include "global" configuration information. (OPTIONAL) #
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+
|
|
|
285d04 |
+include $(CORE_DEPTH)/coreconf/config.mk
|
|
|
285d04 |
+
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+# (3) Include "component" configuration information. (OPTIONAL) #
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+# (4) Include "local" platform-dependent assignments (OPTIONAL). #
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+
|
|
|
285d04 |
+include ../platlibs.mk
|
|
|
285d04 |
+
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+# (5) Execute "global" rules. (OPTIONAL) #
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+
|
|
|
285d04 |
+include $(CORE_DEPTH)/coreconf/rules.mk
|
|
|
285d04 |
+
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+# (6) Execute "component" rules. (OPTIONAL) #
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+
|
|
|
285d04 |
+#include ../platlibs.mk
|
|
|
285d04 |
+
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+# (7) Execute "local" rules. (OPTIONAL). #
|
|
|
285d04 |
+#######################################################################
|
|
|
285d04 |
+
|
|
|
285d04 |
+include ../platrules.mk
|
|
|
285d04 |
+
|
|
|
285d04 |
diff --git a/cmd/dbtool/dbtool.c b/cmd/dbtool/dbtool.c
|
|
|
285d04 |
new file mode 100644
|
|
|
285d04 |
--- /dev/null
|
|
|
285d04 |
+++ b/cmd/dbtool/dbtool.c
|
|
|
285d04 |
@@ -0,0 +1,806 @@
|
|
|
285d04 |
+/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
285d04 |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
285d04 |
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+** dbtool.c
|
|
|
285d04 |
+**
|
|
|
285d04 |
+** tool to dump the underlying encoding of a database. This tool duplicates
|
|
|
285d04 |
+** some private functions in softoken. It uses libsec and libutil, but no
|
|
|
285d04 |
+** other portions of NSS. It currently only works on sqlite databases. For
|
|
|
285d04 |
+** an even more primitive dump, use sqlite3 on the individual files.
|
|
|
285d04 |
+**
|
|
|
285d04 |
+** TODO: dump the meta data for the databases.
|
|
|
285d04 |
+** optionally dump more PKCS5 information (KDF/salt/iterations)
|
|
|
285d04 |
+** take a password and decode encrypted attributes/verify signed
|
|
|
285d04 |
+** attributes.
|
|
|
285d04 |
+*/
|
|
|
285d04 |
+#include <stdio.h>
|
|
|
285d04 |
+#include <string.h>
|
|
|
285d04 |
+
|
|
|
285d04 |
+#if defined(WIN32)
|
|
|
285d04 |
+#include "fcntl.h"
|
|
|
285d04 |
+#include "io.h"
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+
|
|
|
285d04 |
+#include "secutil.h"
|
|
|
285d04 |
+#include "pk11pub.h"
|
|
|
285d04 |
+
|
|
|
285d04 |
+#if defined(XP_UNIX)
|
|
|
285d04 |
+#include <unistd.h>
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+
|
|
|
285d04 |
+#include "nspr.h"
|
|
|
285d04 |
+#include "prtypes.h"
|
|
|
285d04 |
+#include "certdb.h"
|
|
|
285d04 |
+#include "nss.h"
|
|
|
285d04 |
+#include "../modutil/modutil.h"
|
|
|
285d04 |
+#include "pk11table.h"
|
|
|
285d04 |
+#include "sftkdbt.h"
|
|
|
285d04 |
+#include "sdb.h"
|
|
|
285d04 |
+#include "secoid.h"
|
|
|
285d04 |
+
|
|
|
285d04 |
+#include "plgetopt.h"
|
|
|
285d04 |
+
|
|
|
285d04 |
+static char *progName;
|
|
|
285d04 |
+
|
|
|
285d04 |
+char *dbDir = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+static void
|
|
|
285d04 |
+Usage()
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ printf("Usage: %s [-c certprefix] [-k keyprefix] "
|
|
|
285d04 |
+ "[-V certversion] [-v keyversion]\n"
|
|
|
285d04 |
+ " [-d dbdir]\n",
|
|
|
285d04 |
+ progName);
|
|
|
285d04 |
+ printf("%-20s Directory with cert database (default is .)\n",
|
|
|
285d04 |
+ "-d certdir");
|
|
|
285d04 |
+ printf("%-20s prefix for the cert database (default is \"\")\n",
|
|
|
285d04 |
+ "-c certprefix");
|
|
|
285d04 |
+ printf("%-20s prefix for the key database (default is \"\")\n",
|
|
|
285d04 |
+ "-k keyprefix");
|
|
|
285d04 |
+ printf("%-20s version of the cert database (default is 9)\n",
|
|
|
285d04 |
+ "-V certversion");
|
|
|
285d04 |
+ printf("%-20s version of the key database (default is 4)\n",
|
|
|
285d04 |
+ "-v keyversion");
|
|
|
285d04 |
+ exit(1);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+#define SFTK_KEYDB_TYPE 0x40000000
|
|
|
285d04 |
+#define SFTK_TOKEN_TYPE 0x80000000
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * known attributes
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static const CK_ATTRIBUTE_TYPE known_attributes[] = {
|
|
|
285d04 |
+ CKA_CLASS, CKA_TOKEN, CKA_PRIVATE, CKA_LABEL, CKA_APPLICATION,
|
|
|
285d04 |
+ CKA_VALUE, CKA_OBJECT_ID, CKA_CERTIFICATE_TYPE, CKA_ISSUER,
|
|
|
285d04 |
+ CKA_SERIAL_NUMBER, CKA_AC_ISSUER, CKA_OWNER, CKA_ATTR_TYPES, CKA_TRUSTED,
|
|
|
285d04 |
+ CKA_CERTIFICATE_CATEGORY, CKA_JAVA_MIDP_SECURITY_DOMAIN, CKA_URL,
|
|
|
285d04 |
+ CKA_HASH_OF_SUBJECT_PUBLIC_KEY, CKA_HASH_OF_ISSUER_PUBLIC_KEY,
|
|
|
285d04 |
+ CKA_CHECK_VALUE, CKA_KEY_TYPE, CKA_SUBJECT, CKA_ID, CKA_SENSITIVE,
|
|
|
285d04 |
+ CKA_ENCRYPT, CKA_DECRYPT, CKA_WRAP, CKA_UNWRAP, CKA_SIGN, CKA_SIGN_RECOVER,
|
|
|
285d04 |
+ CKA_VERIFY, CKA_VERIFY_RECOVER, CKA_DERIVE, CKA_START_DATE, CKA_END_DATE,
|
|
|
285d04 |
+ CKA_MODULUS, CKA_MODULUS_BITS, CKA_PUBLIC_EXPONENT, CKA_PRIVATE_EXPONENT,
|
|
|
285d04 |
+ CKA_PRIME_1, CKA_PRIME_2, CKA_EXPONENT_1, CKA_EXPONENT_2, CKA_COEFFICIENT,
|
|
|
285d04 |
+ CKA_PRIME, CKA_SUBPRIME, CKA_BASE, CKA_PRIME_BITS,
|
|
|
285d04 |
+ CKA_SUB_PRIME_BITS, CKA_VALUE_BITS, CKA_VALUE_LEN, CKA_EXTRACTABLE,
|
|
|
285d04 |
+ CKA_LOCAL, CKA_NEVER_EXTRACTABLE, CKA_ALWAYS_SENSITIVE,
|
|
|
285d04 |
+ CKA_KEY_GEN_MECHANISM, CKA_MODIFIABLE, CKA_EC_PARAMS,
|
|
|
285d04 |
+ CKA_EC_POINT, CKA_SECONDARY_AUTH, CKA_AUTH_PIN_FLAGS,
|
|
|
285d04 |
+ CKA_ALWAYS_AUTHENTICATE, CKA_WRAP_WITH_TRUSTED, CKA_WRAP_TEMPLATE,
|
|
|
285d04 |
+ CKA_UNWRAP_TEMPLATE, CKA_HW_FEATURE_TYPE, CKA_RESET_ON_INIT,
|
|
|
285d04 |
+ CKA_HAS_RESET, CKA_PIXEL_X, CKA_PIXEL_Y, CKA_RESOLUTION, CKA_CHAR_ROWS,
|
|
|
285d04 |
+ CKA_CHAR_COLUMNS, CKA_COLOR, CKA_BITS_PER_PIXEL, CKA_CHAR_SETS,
|
|
|
285d04 |
+ CKA_ENCODING_METHODS, CKA_MIME_TYPES, CKA_MECHANISM_TYPE,
|
|
|
285d04 |
+ CKA_REQUIRED_CMS_ATTRIBUTES, CKA_DEFAULT_CMS_ATTRIBUTES,
|
|
|
285d04 |
+ CKA_SUPPORTED_CMS_ATTRIBUTES, CKA_NSS_URL, CKA_NSS_EMAIL,
|
|
|
285d04 |
+ CKA_NSS_SMIME_INFO, CKA_NSS_SMIME_TIMESTAMP,
|
|
|
285d04 |
+ CKA_NSS_PKCS8_SALT, CKA_NSS_PASSWORD_CHECK, CKA_NSS_EXPIRES,
|
|
|
285d04 |
+ CKA_NSS_KRL, CKA_NSS_PQG_COUNTER, CKA_NSS_PQG_SEED,
|
|
|
285d04 |
+ CKA_NSS_PQG_H, CKA_NSS_PQG_SEED_BITS, CKA_NSS_MODULE_SPEC,
|
|
|
285d04 |
+ CKA_TRUST_DIGITAL_SIGNATURE, CKA_TRUST_NON_REPUDIATION,
|
|
|
285d04 |
+ CKA_TRUST_KEY_ENCIPHERMENT, CKA_TRUST_DATA_ENCIPHERMENT,
|
|
|
285d04 |
+ CKA_TRUST_KEY_AGREEMENT, CKA_TRUST_KEY_CERT_SIGN, CKA_TRUST_CRL_SIGN,
|
|
|
285d04 |
+ CKA_TRUST_SERVER_AUTH, CKA_TRUST_CLIENT_AUTH, CKA_TRUST_CODE_SIGNING,
|
|
|
285d04 |
+ CKA_TRUST_EMAIL_PROTECTION, CKA_TRUST_IPSEC_END_SYSTEM,
|
|
|
285d04 |
+ CKA_TRUST_IPSEC_TUNNEL, CKA_TRUST_IPSEC_USER, CKA_TRUST_TIME_STAMPING,
|
|
|
285d04 |
+ CKA_TRUST_STEP_UP_APPROVED, CKA_CERT_SHA1_HASH, CKA_CERT_MD5_HASH,
|
|
|
285d04 |
+ CKA_NSS_DB, CKA_NSS_TRUST, CKA_NSS_OVERRIDE_EXTENSIONS,
|
|
|
285d04 |
+ CKA_PUBLIC_KEY_INFO
|
|
|
285d04 |
+};
|
|
|
285d04 |
+
|
|
|
285d04 |
+static unsigned int known_attributes_size = sizeof(known_attributes) /
|
|
|
285d04 |
+ sizeof(known_attributes[0]);
|
|
|
285d04 |
+
|
|
|
285d04 |
+PRBool
|
|
|
285d04 |
+isULONGAttribute(CK_ATTRIBUTE_TYPE type)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ switch (type) {
|
|
|
285d04 |
+ case CKA_CERTIFICATE_CATEGORY:
|
|
|
285d04 |
+ case CKA_CERTIFICATE_TYPE:
|
|
|
285d04 |
+ case CKA_CLASS:
|
|
|
285d04 |
+ case CKA_JAVA_MIDP_SECURITY_DOMAIN:
|
|
|
285d04 |
+ case CKA_KEY_GEN_MECHANISM:
|
|
|
285d04 |
+ case CKA_KEY_TYPE:
|
|
|
285d04 |
+ case CKA_MECHANISM_TYPE:
|
|
|
285d04 |
+ case CKA_MODULUS_BITS:
|
|
|
285d04 |
+ case CKA_PRIME_BITS:
|
|
|
285d04 |
+ case CKA_SUBPRIME_BITS:
|
|
|
285d04 |
+ case CKA_VALUE_BITS:
|
|
|
285d04 |
+ case CKA_VALUE_LEN:
|
|
|
285d04 |
+
|
|
|
285d04 |
+ case CKA_TRUST_DIGITAL_SIGNATURE:
|
|
|
285d04 |
+ case CKA_TRUST_NON_REPUDIATION:
|
|
|
285d04 |
+ case CKA_TRUST_KEY_ENCIPHERMENT:
|
|
|
285d04 |
+ case CKA_TRUST_DATA_ENCIPHERMENT:
|
|
|
285d04 |
+ case CKA_TRUST_KEY_AGREEMENT:
|
|
|
285d04 |
+ case CKA_TRUST_KEY_CERT_SIGN:
|
|
|
285d04 |
+ case CKA_TRUST_CRL_SIGN:
|
|
|
285d04 |
+
|
|
|
285d04 |
+ case CKA_TRUST_SERVER_AUTH:
|
|
|
285d04 |
+ case CKA_TRUST_CLIENT_AUTH:
|
|
|
285d04 |
+ case CKA_TRUST_CODE_SIGNING:
|
|
|
285d04 |
+ case CKA_TRUST_EMAIL_PROTECTION:
|
|
|
285d04 |
+ case CKA_TRUST_IPSEC_END_SYSTEM:
|
|
|
285d04 |
+ case CKA_TRUST_IPSEC_TUNNEL:
|
|
|
285d04 |
+ case CKA_TRUST_IPSEC_USER:
|
|
|
285d04 |
+ case CKA_TRUST_TIME_STAMPING:
|
|
|
285d04 |
+ case CKA_TRUST_STEP_UP_APPROVED:
|
|
|
285d04 |
+ return PR_TRUE;
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return PR_FALSE;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* are the attributes private? */
|
|
|
285d04 |
+static PRBool
|
|
|
285d04 |
+isPrivateAttribute(CK_ATTRIBUTE_TYPE type)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ switch (type) {
|
|
|
285d04 |
+ case CKA_VALUE:
|
|
|
285d04 |
+ case CKA_PRIVATE_EXPONENT:
|
|
|
285d04 |
+ case CKA_PRIME_1:
|
|
|
285d04 |
+ case CKA_PRIME_2:
|
|
|
285d04 |
+ case CKA_EXPONENT_1:
|
|
|
285d04 |
+ case CKA_EXPONENT_2:
|
|
|
285d04 |
+ case CKA_COEFFICIENT:
|
|
|
285d04 |
+ return PR_TRUE;
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return PR_FALSE;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* These attributes must be authenticated with an hmac. */
|
|
|
285d04 |
+static PRBool
|
|
|
285d04 |
+isAuthenticatedAttribute(CK_ATTRIBUTE_TYPE type)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ switch (type) {
|
|
|
285d04 |
+ case CKA_MODULUS:
|
|
|
285d04 |
+ case CKA_PUBLIC_EXPONENT:
|
|
|
285d04 |
+ case CKA_CERT_SHA1_HASH:
|
|
|
285d04 |
+ case CKA_CERT_MD5_HASH:
|
|
|
285d04 |
+ case CKA_TRUST_SERVER_AUTH:
|
|
|
285d04 |
+ case CKA_TRUST_CLIENT_AUTH:
|
|
|
285d04 |
+ case CKA_TRUST_EMAIL_PROTECTION:
|
|
|
285d04 |
+ case CKA_TRUST_CODE_SIGNING:
|
|
|
285d04 |
+ case CKA_TRUST_STEP_UP_APPROVED:
|
|
|
285d04 |
+ case CKA_NSS_OVERRIDE_EXTENSIONS:
|
|
|
285d04 |
+ return PR_TRUE;
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return PR_FALSE;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * convert a database ulong back to a native ULONG. (reverse of the above
|
|
|
285d04 |
+ * function.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static CK_ULONG
|
|
|
285d04 |
+sdbULong2ULong(unsigned char *data)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ int i;
|
|
|
285d04 |
+ CK_ULONG value = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ for (i = 0; i < SDB_ULONG_SIZE; i++) {
|
|
|
285d04 |
+ value |= (((CK_ULONG)data[i]) << (SDB_ULONG_SIZE - 1 - i)
|
|
|
285d04 |
+ * PR_BITS_PER_BYTE);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return value;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* PBE defines and functions */
|
|
|
285d04 |
+
|
|
|
285d04 |
+typedef struct EncryptedDataInfoStr {
|
|
|
285d04 |
+ SECAlgorithmID algorithm;
|
|
|
285d04 |
+ SECItem encryptedData;
|
|
|
285d04 |
+} EncryptedDataInfo;
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const SEC_ASN1Template encryptedDataInfoTemplate[] = {
|
|
|
285d04 |
+ { SEC_ASN1_SEQUENCE,
|
|
|
285d04 |
+ 0, NULL, sizeof(EncryptedDataInfo) },
|
|
|
285d04 |
+ { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
|
|
|
285d04 |
+ offsetof(EncryptedDataInfo, algorithm),
|
|
|
285d04 |
+ SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
|
|
|
285d04 |
+ { SEC_ASN1_OCTET_STRING,
|
|
|
285d04 |
+ offsetof(EncryptedDataInfo, encryptedData) },
|
|
|
285d04 |
+ { 0 }
|
|
|
285d04 |
+};
|
|
|
285d04 |
+
|
|
|
285d04 |
+typedef struct PBEParameterStr {
|
|
|
285d04 |
+ SECAlgorithmID prfAlg;
|
|
|
285d04 |
+ SECItem salt;
|
|
|
285d04 |
+ SECItem iteration;
|
|
|
285d04 |
+ SECItem keyLength;
|
|
|
285d04 |
+} PBEParameter;
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const SEC_ASN1Template pkcs5V1PBEParameterTemplate[] =
|
|
|
285d04 |
+ {
|
|
|
285d04 |
+ { SEC_ASN1_SEQUENCE,
|
|
|
285d04 |
+ 0, NULL, sizeof(PBEParameter) },
|
|
|
285d04 |
+ { SEC_ASN1_OCTET_STRING,
|
|
|
285d04 |
+ offsetof(PBEParameter, salt) },
|
|
|
285d04 |
+ { SEC_ASN1_INTEGER,
|
|
|
285d04 |
+ offsetof(PBEParameter, iteration) },
|
|
|
285d04 |
+ { 0 }
|
|
|
285d04 |
+ };
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const SEC_ASN1Template pkcs12V2PBEParameterTemplate[] =
|
|
|
285d04 |
+ {
|
|
|
285d04 |
+ { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(PBEParameter) },
|
|
|
285d04 |
+ { SEC_ASN1_OCTET_STRING, offsetof(PBEParameter, salt) },
|
|
|
285d04 |
+ { SEC_ASN1_INTEGER, offsetof(PBEParameter, iteration) },
|
|
|
285d04 |
+ { 0 }
|
|
|
285d04 |
+ };
|
|
|
285d04 |
+
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const SEC_ASN1Template pkcs5V2PBEParameterTemplate[] =
|
|
|
285d04 |
+ {
|
|
|
285d04 |
+ { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(PBEParameter) },
|
|
|
285d04 |
+ /* this is really a choice, but since we don't understand any other
|
|
|
285d04 |
+ * choice, just inline it. */
|
|
|
285d04 |
+ { SEC_ASN1_OCTET_STRING, offsetof(PBEParameter, salt) },
|
|
|
285d04 |
+ { SEC_ASN1_INTEGER, offsetof(PBEParameter, iteration) },
|
|
|
285d04 |
+ { SEC_ASN1_INTEGER, offsetof(PBEParameter, keyLength) },
|
|
|
285d04 |
+ { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
|
|
|
285d04 |
+ offsetof(PBEParameter, prfAlg),
|
|
|
285d04 |
+ SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
|
|
|
285d04 |
+ { 0 }
|
|
|
285d04 |
+ };
|
|
|
285d04 |
+
|
|
|
285d04 |
+typedef struct Pkcs5v2PBEParameterStr {
|
|
|
285d04 |
+ SECAlgorithmID keyParams; /* parameters of the key generation */
|
|
|
285d04 |
+ SECAlgorithmID algParams; /* parameters for the encryption or mac op */
|
|
|
285d04 |
+} Pkcs5v2PBEParameter;
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const SEC_ASN1Template pkcs5v2PBES2ParameterTemplate[] = {
|
|
|
285d04 |
+ { SEC_ASN1_SEQUENCE, 0, NULL, sizeof(Pkcs5v2PBEParameter) },
|
|
|
285d04 |
+ { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
|
|
|
285d04 |
+ offsetof(Pkcs5v2PBEParameter, keyParams),
|
|
|
285d04 |
+ SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
|
|
|
285d04 |
+ { SEC_ASN1_INLINE | SEC_ASN1_XTRN,
|
|
|
285d04 |
+ offsetof(Pkcs5v2PBEParameter, algParams),
|
|
|
285d04 |
+ SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) },
|
|
|
285d04 |
+ { 0 }
|
|
|
285d04 |
+};
|
|
|
285d04 |
+
|
|
|
285d04 |
+static inline PRBool
|
|
|
285d04 |
+isPKCS12PBE(SECOidTag alg) {
|
|
|
285d04 |
+ switch (alg) {
|
|
|
285d04 |
+ case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_2KEY_TRIPLE_DES_CBC:
|
|
|
285d04 |
+ case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_3KEY_TRIPLE_DES_CBC:
|
|
|
285d04 |
+ case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC2_CBC:
|
|
|
285d04 |
+ case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC2_CBC:
|
|
|
285d04 |
+ case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_128_BIT_RC4:
|
|
|
285d04 |
+ case SEC_OID_PKCS12_V2_PBE_WITH_SHA1_AND_40_BIT_RC4:
|
|
|
285d04 |
+ return PR_TRUE;
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return PR_FALSE;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* helper functions */
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* output an NSS specific attribute or name that wasn't found in our
|
|
|
285d04 |
+ * pkcs #11 table */
|
|
|
285d04 |
+const char *
|
|
|
285d04 |
+makeNSSVendorName(CK_ATTRIBUTE_TYPE attribute, const char *nameType)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ static char nss_name[256];
|
|
|
285d04 |
+ const char *name = NULL;
|
|
|
285d04 |
+ if ((attribute >= CKA_NSS) && (attribute <= 0xffffffff)) {
|
|
|
285d04 |
+ sprintf(nss_name,"%s+%d", nameType, (int)(attribute-CKA_NSS));
|
|
|
285d04 |
+ name = nss_name;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return name;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* turn and attribute into a name */
|
|
|
285d04 |
+const char *
|
|
|
285d04 |
+AttributeName(CK_ATTRIBUTE_TYPE attribute)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ const char *name = getNameFromAttribute(attribute);
|
|
|
285d04 |
+ if (!name) {
|
|
|
285d04 |
+ name = makeNSSVendorName(attribute, "CKA_NSS");
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return name ? name : "UNKNOWN_ATTRIBUTE_TYPE";
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* turn and error code into a name */
|
|
|
285d04 |
+const char *
|
|
|
285d04 |
+ErrorName(CK_RV crv)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ const char *error = getName(crv, ConstResult);
|
|
|
285d04 |
+ if (!error) {
|
|
|
285d04 |
+ error = makeNSSVendorName(crv, "CKR_NSS");
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return error ? error : "UNKNOWN_ERROR";
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* turn an oud tag into a string */
|
|
|
285d04 |
+const char *
|
|
|
285d04 |
+oid2string(SECOidTag alg)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ const char *oidstring = SECOID_FindOIDTagDescription(alg);
|
|
|
285d04 |
+ const char *def="Invalid oid tag"; /* future build a dotted oid string value here */
|
|
|
285d04 |
+ return oidstring ? oidstring : def;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* dump an arbitary data blob. Dump it has hex with ascii on the side */
|
|
|
285d04 |
+#define ASCCHAR(val) ((val) >= ' ' && (val) <= 0x7e ? (val) : '.')
|
|
|
285d04 |
+#define LINE_LENGTH 16
|
|
|
285d04 |
+void
|
|
|
285d04 |
+dumpValue(const unsigned char *v, int len)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ int i, next = 0;
|
|
|
285d04 |
+ char string[LINE_LENGTH+1];
|
|
|
285d04 |
+ char space[LINE_LENGTH*2+1];
|
|
|
285d04 |
+ char *nl = "";
|
|
|
285d04 |
+ char *sp = "";
|
|
|
285d04 |
+ PORT_Memset(string, 0, sizeof(string));
|
|
|
285d04 |
+
|
|
|
285d04 |
+ for (i=0; i < len; i++) {
|
|
|
285d04 |
+ if ((i % LINE_LENGTH) == 0) {
|
|
|
285d04 |
+ printf("%s%s%s ", sp, string, nl);
|
|
|
285d04 |
+ PORT_Memset(string, 0, sizeof(string));
|
|
|
285d04 |
+ next = 0;
|
|
|
285d04 |
+ nl = "\n";
|
|
|
285d04 |
+ sp = " ";
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ printf("%02x", v[i]);
|
|
|
285d04 |
+ string[next++] = ASCCHAR(v[i]);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ PORT_Memset(space, 0, sizeof(space));
|
|
|
285d04 |
+ i = LINE_LENGTH - (len % LINE_LENGTH);
|
|
|
285d04 |
+ if (i != LINE_LENGTH) {
|
|
|
285d04 |
+ int j;
|
|
|
285d04 |
+ for (j=0 ; j < i; j++) {
|
|
|
285d04 |
+ space[j*2] = ' ';
|
|
|
285d04 |
+ space[j*2+1] = ' ';
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ printf("%s%s%s%s", space, sp, string, nl);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* dump a PKCS5/12 PBE blob */
|
|
|
285d04 |
+void
|
|
|
285d04 |
+dumpPKCS(unsigned char *val, CK_ULONG len, PRBool *hasSig)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ EncryptedDataInfo edi;
|
|
|
285d04 |
+ SECStatus rv;
|
|
|
285d04 |
+ SECItem data;
|
|
|
285d04 |
+ PLArenaPool *arena;
|
|
|
285d04 |
+ SECOidTag alg, prfAlg;
|
|
|
285d04 |
+ PBEParameter pbeParam;
|
|
|
285d04 |
+ unsigned char zero = 0;
|
|
|
285d04 |
+ const SEC_ASN1Template *template = pkcs5V1PBEParameterTemplate;
|
|
|
285d04 |
+ int iter, keyLen, i;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (hasSig) { *hasSig = PR_FALSE; }
|
|
|
285d04 |
+
|
|
|
285d04 |
+
|
|
|
285d04 |
+ data.data = val;
|
|
|
285d04 |
+ data.len = len;
|
|
|
285d04 |
+ arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE);
|
|
|
285d04 |
+ if (arena == NULL) {
|
|
|
285d04 |
+ printf("Couldn't allocate arena\n");
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* initialize default values */
|
|
|
285d04 |
+ PORT_Memset(&pbeParam, 0, sizeof(pbeParam));
|
|
|
285d04 |
+ pbeParam.keyLength.data = &zer;;
|
|
|
285d04 |
+ pbeParam.keyLength.len = sizeof(zero);
|
|
|
285d04 |
+ SECOID_SetAlgorithmID(arena, &pbeParam.prfAlg, SEC_OID_SHA1, NULL);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* first crack the encrypted data from the PBE algorithm ID */
|
|
|
285d04 |
+ rv = SEC_QuickDERDecodeItem(arena, &edi, encryptedDataInfoTemplate, &data);
|
|
|
285d04 |
+ if (rv != SECSuccess) {
|
|
|
285d04 |
+ printf("Encrypted Data, failed to decode\n");
|
|
|
285d04 |
+ dumpValue(val,len);
|
|
|
285d04 |
+ PORT_FreeArena(arena, PR_FALSE);
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* now use the pbe secalg to dump info on the pbe */
|
|
|
285d04 |
+ alg = SECOID_GetAlgorithmTag(&edi.algorithm);
|
|
|
285d04 |
+ if ((alg == SEC_OID_PKCS5_PBES2) || (alg == SEC_OID_PKCS5_PBMAC1)){
|
|
|
285d04 |
+ Pkcs5v2PBEParameter param;
|
|
|
285d04 |
+ SECOidTag palg;
|
|
|
285d04 |
+ const char *typeName = (alg == SEC_OID_PKCS5_PBES2) ?
|
|
|
285d04 |
+ "Encrypted Data PBES2" :
|
|
|
285d04 |
+ "Mac Data PBMAC1";
|
|
|
285d04 |
+
|
|
|
285d04 |
+ rv = SEC_QuickDERDecodeItem(arena, ¶m,
|
|
|
285d04 |
+ pkcs5v2PBES2ParameterTemplate,
|
|
|
285d04 |
+ &edi.algorithm.parameters);
|
|
|
285d04 |
+ if (rv != SECSuccess) {
|
|
|
285d04 |
+ printf("%s, failed to decode\n", typeName);
|
|
|
285d04 |
+ dumpValue(val,len);
|
|
|
285d04 |
+ PORT_FreeArena(arena, PR_FALSE);
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ palg = SECOID_GetAlgorithmTag(¶m.algParams);
|
|
|
285d04 |
+ printf("%s alg=%s ", typeName, oid2string(palg));
|
|
|
285d04 |
+ if (hasSig && palg == SEC_OID_AES_256_CBC) {
|
|
|
285d04 |
+ *hasSig = PR_TRUE;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ template = pkcs5V2PBEParameterTemplate;
|
|
|
285d04 |
+ edi.algorithm.parameters = param.keyParams.parameters;
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ printf("Encrypted Data alg=%s ", oid2string(alg));
|
|
|
285d04 |
+ if (alg == SEC_OID_PKCS5_PBKDF2) {
|
|
|
285d04 |
+ template = pkcs5V2PBEParameterTemplate;
|
|
|
285d04 |
+ } else if (isPKCS12PBE(alg)) {
|
|
|
285d04 |
+ template = pkcs12V2PBEParameterTemplate;
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ template = pkcs5V1PBEParameterTemplate;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ rv = SEC_QuickDERDecodeItem(arena, &pbeParam,
|
|
|
285d04 |
+ template,
|
|
|
285d04 |
+ &edi.algorithm.parameters);
|
|
|
285d04 |
+ if (rv != SECSuccess) {
|
|
|
285d04 |
+ printf("( failed to decode params)\n");
|
|
|
285d04 |
+ PORT_FreeArena(arena, PR_FALSE);
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* dump the pbe parmeters */
|
|
|
285d04 |
+ iter = DER_GetInteger(&pbeParam.iteration);
|
|
|
285d04 |
+ keyLen = DER_GetInteger(&pbeParam.keyLength);
|
|
|
285d04 |
+ prfAlg = SECOID_GetAlgorithmTag(&pbeParam.prfAlg);
|
|
|
285d04 |
+ printf("(prf=%s iter=%d keyLen=%d salt=0x",
|
|
|
285d04 |
+ oid2string(prfAlg), iter, keyLen);
|
|
|
285d04 |
+ for(i=0;i < pbeParam.salt.len; i++) printf("%02x",pbeParam.salt.data[i]);
|
|
|
285d04 |
+ printf(")\n");
|
|
|
285d04 |
+ /* finally dump the raw encrypted data */
|
|
|
285d04 |
+ dumpValue(edi.encryptedData.data, edi.encryptedData.len);
|
|
|
285d04 |
+ PORT_FreeArena(arena, PR_FALSE);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* dump a long attribute, convert to an unsigned long. PKCS #11 Longs are
|
|
|
285d04 |
+ * limited to 32 bits by the spec, even if the CK_ULONG is longer */
|
|
|
285d04 |
+void
|
|
|
285d04 |
+dumpLongAttribute(CK_ATTRIBUTE_TYPE type, CK_ULONG value)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ const char *nameType = "CK_NSS";
|
|
|
285d04 |
+ ConstType constType = ConstNone;
|
|
|
285d04 |
+ const char *valueName = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ switch (type) {
|
|
|
285d04 |
+ case CKA_CLASS:
|
|
|
285d04 |
+ nameType = "CKO_NSS";
|
|
|
285d04 |
+ constType = ConstObject;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ case CKA_CERTIFICATE_TYPE:
|
|
|
285d04 |
+ nameType = "CKC_NSS";
|
|
|
285d04 |
+ constType = ConstCertType;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ case CKA_KEY_TYPE:
|
|
|
285d04 |
+ nameType = "CKK_NSS";
|
|
|
285d04 |
+ constType = ConstKeyType;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ case CKA_MECHANISM_TYPE:
|
|
|
285d04 |
+ nameType = "CKM_NSS";
|
|
|
285d04 |
+ constType = ConstMechanism;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ case CKA_TRUST_SERVER_AUTH:
|
|
|
285d04 |
+ case CKA_TRUST_CLIENT_AUTH:
|
|
|
285d04 |
+ case CKA_TRUST_CODE_SIGNING:
|
|
|
285d04 |
+ case CKA_TRUST_EMAIL_PROTECTION:
|
|
|
285d04 |
+ case CKA_TRUST_IPSEC_END_SYSTEM:
|
|
|
285d04 |
+ case CKA_TRUST_IPSEC_TUNNEL:
|
|
|
285d04 |
+ case CKA_TRUST_IPSEC_USER:
|
|
|
285d04 |
+ case CKA_TRUST_TIME_STAMPING:
|
|
|
285d04 |
+ nameType = "CKT_NSS";
|
|
|
285d04 |
+ constType = ConstTrust;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* if value has a symbolic name, use it */
|
|
|
285d04 |
+ if (constType != ConstNone) {
|
|
|
285d04 |
+ valueName = getName(value, constType);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (!valueName) {
|
|
|
285d04 |
+ valueName = makeNSSVendorName(value, nameType);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (!valueName) {
|
|
|
285d04 |
+ printf("%d (0x%08x)\n", (int) value, (int)value);
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ printf("%s (0x%08x)\n", valueName, (int)value);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* dump a signature for an object */
|
|
|
285d04 |
+static const char META_SIG_TEMPLATE[] = "sig_%s_%08x_%08x";
|
|
|
285d04 |
+void
|
|
|
285d04 |
+dumpSignature(CK_ATTRIBUTE_TYPE attribute, SDB *keydb, PRBool isKey,
|
|
|
285d04 |
+ CK_OBJECT_HANDLE objectID, PRBool force)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ char id[30];
|
|
|
285d04 |
+ CK_RV crv;
|
|
|
285d04 |
+ SECItem signText;
|
|
|
285d04 |
+ unsigned char signData[SDB_MAX_META_DATA_LEN];
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (!force && !isAuthenticatedAttribute(attribute)) {
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sprintf(id, META_SIG_TEMPLATE,
|
|
|
285d04 |
+ isKey ? "key" : "cert",
|
|
|
285d04 |
+ (unsigned int)objectID, (unsigned int)attribute);
|
|
|
285d04 |
+ printf(" Signature %s:",id);
|
|
|
285d04 |
+ signText.data = signData;
|
|
|
285d04 |
+ signText.len = sizeof(signData);
|
|
|
285d04 |
+
|
|
|
285d04 |
+
|
|
|
285d04 |
+ crv = (*keydb->sdb_GetMetaData)(keydb, id, &signText, NULL);
|
|
|
285d04 |
+ if ((crv != CKR_OK) && isKey) {
|
|
|
285d04 |
+ sprintf(id, META_SIG_TEMPLATE,
|
|
|
285d04 |
+ isKey ? "key" : "cert", (unsigned int)
|
|
|
285d04 |
+ (objectID | SFTK_KEYDB_TYPE | SFTK_TOKEN_TYPE),
|
|
|
285d04 |
+ (unsigned int)attribute);
|
|
|
285d04 |
+ crv = (*keydb->sdb_GetMetaData)(keydb, id, &signText, NULL);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (crv != CKR_OK) {
|
|
|
285d04 |
+ printf(" FAILED %s with %s (0x%08x)\n", id, ErrorName(crv), (int) crv);
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ dumpPKCS(signText.data, signText.len, NULL);
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* dump an attribute. use the helper functions above */
|
|
|
285d04 |
+void
|
|
|
285d04 |
+dumpAttribute(CK_ATTRIBUTE *template, SDB *keydb, PRBool isKey,
|
|
|
285d04 |
+ CK_OBJECT_HANDLE id)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ CK_ATTRIBUTE_TYPE attribute = template->type;
|
|
|
285d04 |
+ printf(" %s(0x%08x): ", AttributeName(attribute), (int)attribute);
|
|
|
285d04 |
+ if (template->pValue == NULL) {
|
|
|
285d04 |
+ printf("NULL (%d)\n", (int)template->ulValueLen);
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (template->ulValueLen == SDB_ULONG_SIZE
|
|
|
285d04 |
+ && isULONGAttribute(attribute)) {
|
|
|
285d04 |
+ CK_ULONG value=sdbULong2ULong(template->pValue);
|
|
|
285d04 |
+ dumpLongAttribute(attribute, value);
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (template->ulValueLen == 1) {
|
|
|
285d04 |
+ unsigned char val = *(unsigned char *)template->pValue;
|
|
|
285d04 |
+ switch (val) {
|
|
|
285d04 |
+ case 0:
|
|
|
285d04 |
+ printf("CK_FALSE\n");
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ case 1:
|
|
|
285d04 |
+ printf("CK_TRUE\n");
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ printf("%d 0x%02x %c\n", val, val, ASCCHAR(val));
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (isKey && isPrivateAttribute(attribute)) {
|
|
|
285d04 |
+ PRBool hasSig = PR_FALSE;
|
|
|
285d04 |
+ dumpPKCS(template->pValue, template->ulValueLen, &hasSig);
|
|
|
285d04 |
+ if (hasSig) {
|
|
|
285d04 |
+ dumpSignature(attribute, keydb, isKey, id, PR_TRUE);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (template->ulValueLen == 0) { printf("empty"); }
|
|
|
285d04 |
+ printf("\n");
|
|
|
285d04 |
+ dumpValue(template->pValue, template->ulValueLen);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* dump all the attributes in an object */
|
|
|
285d04 |
+void
|
|
|
285d04 |
+dumpObject(CK_OBJECT_HANDLE id, SDB *db, SDB *keydb, PRBool isKey)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ CK_RV crv;
|
|
|
285d04 |
+ int i;
|
|
|
285d04 |
+ CK_ATTRIBUTE template;
|
|
|
285d04 |
+ char buffer[2048];
|
|
|
285d04 |
+ char * alloc = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ printf(" Object 0x%08x:\n", (int)id);
|
|
|
285d04 |
+ for (i = 0; i < known_attributes_size; i++) {
|
|
|
285d04 |
+ CK_ATTRIBUTE_TYPE attribute = known_attributes[i];
|
|
|
285d04 |
+ template.type = attribute;
|
|
|
285d04 |
+ template.pValue = NULL;
|
|
|
285d04 |
+ template.ulValueLen = 0;
|
|
|
285d04 |
+ crv = (*db->sdb_GetAttributeValue)(db, id, &template, 1);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (crv != CKR_OK) {
|
|
|
285d04 |
+ if (crv != CKR_ATTRIBUTE_TYPE_INVALID) {
|
|
|
285d04 |
+ PR_fprintf(PR_STDERR, " "
|
|
|
285d04 |
+ "Get Attribute %s (0x%08x):FAILED\"%s\"(0x%08x)\n",
|
|
|
285d04 |
+ AttributeName(attribute), (int)attribute,
|
|
|
285d04 |
+ ErrorName(crv), (int)crv);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (template.ulValueLen < sizeof(buffer)) {
|
|
|
285d04 |
+ template.pValue = buffer;
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ alloc = PORT_Alloc(template.ulValueLen);
|
|
|
285d04 |
+ template.pValue = alloc;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (template.pValue == NULL) {
|
|
|
285d04 |
+ PR_fprintf(PR_STDERR, " "
|
|
|
285d04 |
+ "Could allocate %d bytes for Attribute %s (0x%08x)\n",
|
|
|
285d04 |
+ (int) template.ulValueLen,
|
|
|
285d04 |
+ AttributeName(attribute), (int)attribute);
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ crv = (*db->sdb_GetAttributeValue)(db, id, &template, 1);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (crv != CKR_OK) {
|
|
|
285d04 |
+ if (crv != CKR_ATTRIBUTE_TYPE_INVALID) {
|
|
|
285d04 |
+ PR_fprintf(PR_STDERR, " "
|
|
|
285d04 |
+ "Get Attribute %s (0x%08x):FAILED\"%s\"(0x%08x)\n",
|
|
|
285d04 |
+ AttributeName(attribute), (int)attribute,
|
|
|
285d04 |
+ ErrorName(crv), (int)crv);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (alloc) {
|
|
|
285d04 |
+ PORT_Free(alloc);
|
|
|
285d04 |
+ alloc = NULL;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ dumpAttribute(&template, keydb, isKey, id);
|
|
|
285d04 |
+ dumpSignature(template.type, keydb, isKey, id, PR_FALSE);
|
|
|
285d04 |
+ if (alloc) {
|
|
|
285d04 |
+ PORT_Free(alloc);
|
|
|
285d04 |
+ alloc = NULL;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* dump all the objects in a database */
|
|
|
285d04 |
+void
|
|
|
285d04 |
+dumpDB(SDB *db, const char *name, SDB *keydb, PRBool isKey)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBFind *findHandle= NULL;
|
|
|
285d04 |
+ CK_BBOOL isTrue = 1;
|
|
|
285d04 |
+ CK_ATTRIBUTE allObjectTemplate = {CKA_TOKEN, NULL, 1 };
|
|
|
285d04 |
+ CK_ULONG allObjectTemplateCount = 1;
|
|
|
285d04 |
+ PRBool recordFound = PR_FALSE;
|
|
|
285d04 |
+ CK_RV crv = CKR_OK;
|
|
|
285d04 |
+ CK_ULONG objectCount = 0;
|
|
|
285d04 |
+ printf("%s:\n",name);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ allObjectTemplate.pValue = &isTrue;
|
|
|
285d04 |
+ crv = (*db->sdb_FindObjectsInit)(db, &allObjectTemplate,
|
|
|
285d04 |
+ allObjectTemplateCount, &findHandle);
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ CK_OBJECT_HANDLE id;
|
|
|
285d04 |
+ recordFound = PR_FALSE;
|
|
|
285d04 |
+ crv =(*db->sdb_FindObjects)(db, findHandle, &id, 1, &objectCount);
|
|
|
285d04 |
+ if ((crv == CKR_OK) && (objectCount == 1)) {
|
|
|
285d04 |
+ recordFound = PR_TRUE;
|
|
|
285d04 |
+ dumpObject(id, db, keydb, isKey);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (recordFound);
|
|
|
285d04 |
+ if (crv != CKR_OK) {
|
|
|
285d04 |
+ PR_fprintf(PR_STDERR,
|
|
|
285d04 |
+ "Last record return PKCS #11 error = %s (0x%08x)\n",
|
|
|
285d04 |
+ ErrorName(crv), (int)crv);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ (*db->sdb_FindObjectsFinal)(db,findHandle);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+int
|
|
|
285d04 |
+main(int argc, char **argv)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ PLOptState *optstate;
|
|
|
285d04 |
+ PLOptStatus optstatus;
|
|
|
285d04 |
+ char *certPrefix="", *keyPrefix="";
|
|
|
285d04 |
+ int cert_version = 9;
|
|
|
285d04 |
+ int key_version = 4;
|
|
|
285d04 |
+ SDB *certdb = NULL;
|
|
|
285d04 |
+ SDB *keydb = NULL;
|
|
|
285d04 |
+ PRBool isNew = PR_FALSE;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ CK_RV crv;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ progName = strrchr(argv[0], '/');
|
|
|
285d04 |
+ if (!progName)
|
|
|
285d04 |
+ progName = strrchr(argv[0], '\\');
|
|
|
285d04 |
+ progName = progName ? progName + 1 : argv[0];
|
|
|
285d04 |
+
|
|
|
285d04 |
+ optstate = PL_CreateOptState(argc, argv, "d:c:k:v:V:h");
|
|
|
285d04 |
+
|
|
|
285d04 |
+ while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
|
|
|
285d04 |
+ switch (optstate->option) {
|
|
|
285d04 |
+ case 'h':
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ Usage();
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ case 'd':
|
|
|
285d04 |
+ dbDir = PORT_Strdup(optstate->value);
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ case 'c':
|
|
|
285d04 |
+ certPrefix = PORT_Strdup(optstate->value);
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ case 'k':
|
|
|
285d04 |
+ keyPrefix = PORT_Strdup(optstate->value);
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ case 'v':
|
|
|
285d04 |
+ key_version = atoi(optstate->value);
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ case 'V':
|
|
|
285d04 |
+ cert_version = atoi(optstate->value);
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ PL_DestroyOptState(optstate);
|
|
|
285d04 |
+ if (optstatus == PL_OPT_BAD)
|
|
|
285d04 |
+ Usage();
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (dbDir) {
|
|
|
285d04 |
+ char *tmp = dbDir;
|
|
|
285d04 |
+ dbDir = SECU_ConfigDirectory(tmp);
|
|
|
285d04 |
+ PORT_Free(tmp);
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ /* Look in $SSL_DIR */
|
|
|
285d04 |
+ dbDir = SECU_ConfigDirectory(SECU_DefaultSSLDir());
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ PR_fprintf(PR_STDERR, "dbdir selected is %s\n\n", dbDir);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (dbDir[0] == '\0') {
|
|
|
285d04 |
+ PR_fprintf(PR_STDERR, errStrings[DIR_DOESNT_EXIST_ERR], dbDir);
|
|
|
285d04 |
+ return 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
|
|
|
285d04 |
+ SECOID_Init();
|
|
|
285d04 |
+
|
|
|
285d04 |
+ crv = s_open(dbDir, certPrefix, keyPrefix, cert_version, key_version,
|
|
|
285d04 |
+ SDB_RDONLY, &certdb, &keydb, &isNew);
|
|
|
285d04 |
+ if (crv != CKR_OK) {
|
|
|
285d04 |
+ PR_fprintf(PR_STDERR,
|
|
|
285d04 |
+ "Couldn't open databased in %s, error=%s (0x%08x)\n",
|
|
|
285d04 |
+ dbDir, ErrorName(crv), (int)crv);
|
|
|
285d04 |
+ return 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* now dump the objects in the cert database */
|
|
|
285d04 |
+ dumpDB(certdb, "CertDB", keydb, PR_FALSE);
|
|
|
285d04 |
+ dumpDB(keydb, "KeyDB", keydb, PR_TRUE);
|
|
|
285d04 |
+ return 0;
|
|
|
285d04 |
+}
|
|
|
285d04 |
diff --git a/cmd/dbtool/dbtool.gyp b/cmd/dbtool/dbtool.gyp
|
|
|
285d04 |
new file mode 100644
|
|
|
285d04 |
--- /dev/null
|
|
|
285d04 |
+++ b/cmd/dbtool/dbtool.gyp
|
|
|
285d04 |
@@ -0,0 +1,25 @@
|
|
|
285d04 |
+# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
285d04 |
+# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
285d04 |
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ 'includes': [
|
|
|
285d04 |
+ '../../coreconf/config.gypi',
|
|
|
285d04 |
+ '../../cmd/platlibs.gypi'
|
|
|
285d04 |
+ ],
|
|
|
285d04 |
+ 'targets': [
|
|
|
285d04 |
+ {
|
|
|
285d04 |
+ 'target_name': 'dbtest',
|
|
|
285d04 |
+ 'type': 'executable',
|
|
|
285d04 |
+ 'sources': [
|
|
|
285d04 |
+ 'dbtest.c'
|
|
|
285d04 |
+ ],
|
|
|
285d04 |
+ 'dependencies': [
|
|
|
285d04 |
+ '<(DEPTH)/exports.gyp:dbm_exports',
|
|
|
285d04 |
+ '<(DEPTH)/exports.gyp:nss_exports'
|
|
|
285d04 |
+ ]
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ ],
|
|
|
285d04 |
+ 'variables': {
|
|
|
285d04 |
+ 'module': 'nss'
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+}
|
|
|
285d04 |
\ No newline at end of file
|
|
|
285d04 |
diff --git a/cmd/dbtool/manifest.mn b/cmd/dbtool/manifest.mn
|
|
|
285d04 |
new file mode 100644
|
|
|
285d04 |
--- /dev/null
|
|
|
285d04 |
+++ b/cmd/dbtool/manifest.mn
|
|
|
285d04 |
@@ -0,0 +1,18 @@
|
|
|
285d04 |
+#
|
|
|
285d04 |
+# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
285d04 |
+# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
285d04 |
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
285d04 |
+
|
|
|
285d04 |
+CORE_DEPTH = ../..
|
|
|
285d04 |
+
|
|
|
285d04 |
+# MODULE public and private header directories are implicitly REQUIRED.
|
|
|
285d04 |
+MODULE = nss
|
|
|
285d04 |
+
|
|
|
285d04 |
+USE_STATIC_LIBS = 1
|
|
|
285d04 |
+
|
|
|
285d04 |
+# DIRS =
|
|
|
285d04 |
+
|
|
|
285d04 |
+CSRCS = dbtool.c sdb.c
|
|
|
285d04 |
+
|
|
|
285d04 |
+PROGRAM = dbtool
|
|
|
285d04 |
+
|
|
|
285d04 |
diff --git a/cmd/dbtool/sdb.c b/cmd/dbtool/sdb.c
|
|
|
285d04 |
new file mode 100644
|
|
|
285d04 |
--- /dev/null
|
|
|
285d04 |
+++ b/cmd/dbtool/sdb.c
|
|
|
285d04 |
@@ -0,0 +1,2469 @@
|
|
|
285d04 |
+/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
285d04 |
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
285d04 |
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * This file implements PKCS 11 on top of our existing security modules
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * For more information about PKCS 11 See PKCS 11 Token Inteface Standard.
|
|
|
285d04 |
+ * This implementation has two slots:
|
|
|
285d04 |
+ * slot 1 is our generic crypto support. It does not require login.
|
|
|
285d04 |
+ * It supports Public Key ops, and all they bulk ciphers and hashes.
|
|
|
285d04 |
+ * It can also support Private Key ops for imported Private keys. It does
|
|
|
285d04 |
+ * not have any token storage.
|
|
|
285d04 |
+ * slot 2 is our private key support. It requires a login before use. It
|
|
|
285d04 |
+ * can store Private Keys and Certs as token objects. Currently only private
|
|
|
285d04 |
+ * keys and their associated Certificates are saved on the token.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * In this implementation, session objects are only visible to the session
|
|
|
285d04 |
+ * that created or generated them.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+
|
|
|
285d04 |
+#include "sdb.h"
|
|
|
285d04 |
+#include "pkcs11t.h"
|
|
|
285d04 |
+#include "seccomon.h"
|
|
|
285d04 |
+#include <sqlite3.h>
|
|
|
285d04 |
+#include "prthread.h"
|
|
|
285d04 |
+#include "prio.h"
|
|
|
285d04 |
+#include <stdio.h>
|
|
|
285d04 |
+#include "secport.h"
|
|
|
285d04 |
+#include "prmon.h"
|
|
|
285d04 |
+#include "prenv.h"
|
|
|
285d04 |
+#include "prprf.h"
|
|
|
285d04 |
+#include "prsystem.h" /* for PR_GetDirectorySeparator() */
|
|
|
285d04 |
+#include <sys/stat.h>
|
|
|
285d04 |
+#if defined(_WIN32)
|
|
|
285d04 |
+#include <io.h>
|
|
|
285d04 |
+#include <windows.h>
|
|
|
285d04 |
+#elif defined(XP_UNIX)
|
|
|
285d04 |
+#include <unistd.h>
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+#if defined(LINUX) && !defined(ANDROID)
|
|
|
285d04 |
+#include <linux/magic.h>
|
|
|
285d04 |
+#include <sys/vfs.h>
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+#include "utilpars.h"
|
|
|
285d04 |
+
|
|
|
285d04 |
+#ifdef SQLITE_UNSAFE_THREADS
|
|
|
285d04 |
+#include "prlock.h"
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * SQLite can be compiled to be thread safe or not.
|
|
|
285d04 |
+ * turn on SQLITE_UNSAFE_THREADS if the OS does not support
|
|
|
285d04 |
+ * a thread safe version of sqlite.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static PRLock *sqlite_lock = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+#define LOCK_SQLITE() PR_Lock(sqlite_lock);
|
|
|
285d04 |
+#define UNLOCK_SQLITE() PR_Unlock(sqlite_lock);
|
|
|
285d04 |
+#else
|
|
|
285d04 |
+#define LOCK_SQLITE()
|
|
|
285d04 |
+#define UNLOCK_SQLITE()
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+
|
|
|
285d04 |
+typedef enum {
|
|
|
285d04 |
+ SDB_CERT = 1,
|
|
|
285d04 |
+ SDB_KEY = 2
|
|
|
285d04 |
+} sdbDataType;
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * defines controlling how long we wait to acquire locks.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * SDB_SQLITE_BUSY_TIMEOUT specifies how long (in milliseconds)
|
|
|
285d04 |
+ * sqlite will wait on lock. If that timeout expires, sqlite will
|
|
|
285d04 |
+ * return SQLITE_BUSY.
|
|
|
285d04 |
+ * SDB_BUSY_RETRY_TIME specifies how many seconds the sdb_ code waits
|
|
|
285d04 |
+ * after receiving a busy before retrying.
|
|
|
285d04 |
+ * SDB_MAX_BUSY_RETRIES specifies how many times the sdb_ will retry on
|
|
|
285d04 |
+ * a busy condition.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * SDB_SQLITE_BUSY_TIMEOUT affects all opertions, both manual
|
|
|
285d04 |
+ * (prepare/step/reset/finalize) and automatic (sqlite3_exec()).
|
|
|
285d04 |
+ * SDB_BUSY_RETRY_TIME and SDB_MAX_BUSY_RETRIES only affect manual operations
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * total wait time for automatic operations:
|
|
|
285d04 |
+ * 1 second (SDB_SQLITE_BUSY_TIMEOUT/1000).
|
|
|
285d04 |
+ * total wait time for manual operations:
|
|
|
285d04 |
+ * (1 second + SDB_BUSY_RETRY_TIME) * 30 = 30 seconds.
|
|
|
285d04 |
+ * (SDB_SQLITE_BUSY_TIMEOUT/1000 + SDB_BUSY_RETRY_TIME)*SDB_MAX_BUSY_RETRIES
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+#define SDB_SQLITE_BUSY_TIMEOUT 1000 /* milliseconds */
|
|
|
285d04 |
+#define SDB_BUSY_RETRY_TIME 5 /* 'ticks', varies by platforms */
|
|
|
285d04 |
+#define SDB_MAX_BUSY_RETRIES 30
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * known attributes
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static const CK_ATTRIBUTE_TYPE known_attributes[] = {
|
|
|
285d04 |
+ CKA_CLASS, CKA_TOKEN, CKA_PRIVATE, CKA_LABEL, CKA_APPLICATION,
|
|
|
285d04 |
+ CKA_VALUE, CKA_OBJECT_ID, CKA_CERTIFICATE_TYPE, CKA_ISSUER,
|
|
|
285d04 |
+ CKA_SERIAL_NUMBER, CKA_AC_ISSUER, CKA_OWNER, CKA_ATTR_TYPES, CKA_TRUSTED,
|
|
|
285d04 |
+ CKA_CERTIFICATE_CATEGORY, CKA_JAVA_MIDP_SECURITY_DOMAIN, CKA_URL,
|
|
|
285d04 |
+ CKA_HASH_OF_SUBJECT_PUBLIC_KEY, CKA_HASH_OF_ISSUER_PUBLIC_KEY,
|
|
|
285d04 |
+ CKA_CHECK_VALUE, CKA_KEY_TYPE, CKA_SUBJECT, CKA_ID, CKA_SENSITIVE,
|
|
|
285d04 |
+ CKA_ENCRYPT, CKA_DECRYPT, CKA_WRAP, CKA_UNWRAP, CKA_SIGN, CKA_SIGN_RECOVER,
|
|
|
285d04 |
+ CKA_VERIFY, CKA_VERIFY_RECOVER, CKA_DERIVE, CKA_START_DATE, CKA_END_DATE,
|
|
|
285d04 |
+ CKA_MODULUS, CKA_MODULUS_BITS, CKA_PUBLIC_EXPONENT, CKA_PRIVATE_EXPONENT,
|
|
|
285d04 |
+ CKA_PRIME_1, CKA_PRIME_2, CKA_EXPONENT_1, CKA_EXPONENT_2, CKA_COEFFICIENT,
|
|
|
285d04 |
+ CKA_PUBLIC_KEY_INFO, CKA_PRIME, CKA_SUBPRIME, CKA_BASE, CKA_PRIME_BITS,
|
|
|
285d04 |
+ CKA_SUB_PRIME_BITS, CKA_VALUE_BITS, CKA_VALUE_LEN, CKA_EXTRACTABLE,
|
|
|
285d04 |
+ CKA_LOCAL, CKA_NEVER_EXTRACTABLE, CKA_ALWAYS_SENSITIVE,
|
|
|
285d04 |
+ CKA_KEY_GEN_MECHANISM, CKA_MODIFIABLE, CKA_EC_PARAMS,
|
|
|
285d04 |
+ CKA_EC_POINT, CKA_SECONDARY_AUTH, CKA_AUTH_PIN_FLAGS,
|
|
|
285d04 |
+ CKA_ALWAYS_AUTHENTICATE, CKA_WRAP_WITH_TRUSTED, CKA_HW_FEATURE_TYPE,
|
|
|
285d04 |
+ CKA_RESET_ON_INIT, CKA_HAS_RESET, CKA_PIXEL_X, CKA_PIXEL_Y,
|
|
|
285d04 |
+ CKA_RESOLUTION, CKA_CHAR_ROWS, CKA_CHAR_COLUMNS, CKA_COLOR,
|
|
|
285d04 |
+ CKA_BITS_PER_PIXEL, CKA_CHAR_SETS, CKA_ENCODING_METHODS, CKA_MIME_TYPES,
|
|
|
285d04 |
+ CKA_MECHANISM_TYPE, CKA_REQUIRED_CMS_ATTRIBUTES,
|
|
|
285d04 |
+ CKA_DEFAULT_CMS_ATTRIBUTES, CKA_SUPPORTED_CMS_ATTRIBUTES,
|
|
|
285d04 |
+ CKA_WRAP_TEMPLATE, CKA_UNWRAP_TEMPLATE, CKA_NSS_TRUST, CKA_NSS_URL,
|
|
|
285d04 |
+ CKA_NSS_EMAIL, CKA_NSS_SMIME_INFO, CKA_NSS_SMIME_TIMESTAMP,
|
|
|
285d04 |
+ CKA_NSS_PKCS8_SALT, CKA_NSS_PASSWORD_CHECK, CKA_NSS_EXPIRES,
|
|
|
285d04 |
+ CKA_NSS_KRL, CKA_NSS_PQG_COUNTER, CKA_NSS_PQG_SEED,
|
|
|
285d04 |
+ CKA_NSS_PQG_H, CKA_NSS_PQG_SEED_BITS, CKA_NSS_MODULE_SPEC,
|
|
|
285d04 |
+ CKA_NSS_OVERRIDE_EXTENSIONS, CKA_NSS_SERVER_DISTRUST_AFTER,
|
|
|
285d04 |
+ CKA_NSS_EMAIL_DISTRUST_AFTER, CKA_TRUST_DIGITAL_SIGNATURE,
|
|
|
285d04 |
+ CKA_TRUST_NON_REPUDIATION, CKA_TRUST_KEY_ENCIPHERMENT,
|
|
|
285d04 |
+ CKA_TRUST_DATA_ENCIPHERMENT, CKA_TRUST_KEY_AGREEMENT,
|
|
|
285d04 |
+ CKA_TRUST_KEY_CERT_SIGN, CKA_TRUST_CRL_SIGN, CKA_TRUST_SERVER_AUTH,
|
|
|
285d04 |
+ CKA_TRUST_CLIENT_AUTH, CKA_TRUST_CODE_SIGNING, CKA_TRUST_EMAIL_PROTECTION,
|
|
|
285d04 |
+ CKA_TRUST_IPSEC_END_SYSTEM, CKA_TRUST_IPSEC_TUNNEL, CKA_TRUST_IPSEC_USER,
|
|
|
285d04 |
+ CKA_TRUST_TIME_STAMPING, CKA_TRUST_STEP_UP_APPROVED, CKA_CERT_SHA1_HASH,
|
|
|
285d04 |
+ CKA_CERT_MD5_HASH, CKA_NSS_DB
|
|
|
285d04 |
+};
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const int known_attributes_size = PR_ARRAY_SIZE(known_attributes);
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * Note on use of sqlReadDB: Only one thread at a time may have an actual
|
|
|
285d04 |
+ * operation going on given sqlite3 * database. An operation is defined as
|
|
|
285d04 |
+ * the time from a sqlite3_prepare() until the sqlite3_finalize().
|
|
|
285d04 |
+ * Multiple sqlite3 * databases can be open and have simultaneous operations
|
|
|
285d04 |
+ * going. We use the sqlXactDB for all write operations. This database
|
|
|
285d04 |
+ * is only opened when we first create a transaction and closed when the
|
|
|
285d04 |
+ * transaction is complete. sqlReadDB is open when we first opened the database
|
|
|
285d04 |
+ * and is used for all read operation. It's use is protected by a monitor. This
|
|
|
285d04 |
+ * is because an operation can span the use of FindObjectsInit() through the
|
|
|
285d04 |
+ * call to FindObjectsFinal(). In the intermediate time it is possible to call
|
|
|
285d04 |
+ * other operations like NSC_GetAttributeValue */
|
|
|
285d04 |
+
|
|
|
285d04 |
+struct SDBPrivateStr {
|
|
|
285d04 |
+ char *sqlDBName; /* invariant, path to this database */
|
|
|
285d04 |
+ sqlite3 *sqlXactDB; /* access protected by dbMon, use protected
|
|
|
285d04 |
+ * by the transaction. Current transaction db*/
|
|
|
285d04 |
+ PRThread *sqlXactThread; /* protected by dbMon,
|
|
|
285d04 |
+ * current transaction thread */
|
|
|
285d04 |
+ sqlite3 *sqlReadDB; /* use protected by dbMon, value invariant */
|
|
|
285d04 |
+ PRIntervalTime lastUpdateTime; /* last time the cache was updated */
|
|
|
285d04 |
+ PRIntervalTime updateInterval; /* how long the cache can go before it
|
|
|
285d04 |
+ * must be updated again */
|
|
|
285d04 |
+ sdbDataType type; /* invariant, database type */
|
|
|
285d04 |
+ char *table; /* invariant, SQL table which contains the db */
|
|
|
285d04 |
+ char *cacheTable; /* invariant, SQL table cache of db */
|
|
|
285d04 |
+ PRMonitor *dbMon; /* invariant, monitor to protect
|
|
|
285d04 |
+ * sqlXact* fields, and use of the sqlReadDB */
|
|
|
285d04 |
+ CK_ATTRIBUTE_TYPE *schemaAttrs; /* Attribute columns that exist in the table. */
|
|
|
285d04 |
+ unsigned int numSchemaAttrs;
|
|
|
285d04 |
+};
|
|
|
285d04 |
+
|
|
|
285d04 |
+typedef struct SDBPrivateStr SDBPrivate;
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* Magic for an explicit NULL. NOTE: ideally this should be
|
|
|
285d04 |
+ * out of band data. Since it's not completely out of band, pick
|
|
|
285d04 |
+ * a value that has no meaning to any existing PKCS #11 attributes.
|
|
|
285d04 |
+ * This value is 1) not a valid string (imbedded '\0'). 2) not a U_LONG
|
|
|
285d04 |
+ * or a normal key (too short). 3) not a bool (too long). 4) not an RSA
|
|
|
285d04 |
+ * public exponent (too many bits).
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+const unsigned char SQLITE_EXPLICIT_NULL[] = { 0xa5, 0x0, 0x5a };
|
|
|
285d04 |
+#define SQLITE_EXPLICIT_NULL_LEN 3
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * determine when we've completed our tasks
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static int
|
|
|
285d04 |
+sdb_done(int err, int *count)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ /* allow as many rows as the database wants to give */
|
|
|
285d04 |
+ if (err == SQLITE_ROW) {
|
|
|
285d04 |
+ *count = 0;
|
|
|
285d04 |
+ return 0;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (err != SQLITE_BUSY) {
|
|
|
285d04 |
+ return 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* err == SQLITE_BUSY, Dont' retry forever in this case */
|
|
|
285d04 |
+ if (++(*count) >= SDB_MAX_BUSY_RETRIES) {
|
|
|
285d04 |
+ return 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return 0;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+#if defined(_WIN32)
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * NSPR functions and narrow CRT functions do not handle UTF-8 file paths that
|
|
|
285d04 |
+ * sqlite3 expects.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+
|
|
|
285d04 |
+static int
|
|
|
285d04 |
+sdb_chmod(const char *filename, int pmode)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ int result;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (!filename) {
|
|
|
285d04 |
+ return -1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ wchar_t *filenameWide = _NSSUTIL_UTF8ToWide(filename);
|
|
|
285d04 |
+ if (!filenameWide) {
|
|
|
285d04 |
+ return -1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ result = _wchmod(filenameWide, pmode);
|
|
|
285d04 |
+ PORT_Free(filenameWide);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return result;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+#else
|
|
|
285d04 |
+#define sdb_chmod(filename, pmode) chmod((filename), (pmode))
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * find out where sqlite stores the temp tables. We do this by replicating
|
|
|
285d04 |
+ * the logic from sqlite.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+#if defined(_WIN32)
|
|
|
285d04 |
+static char *
|
|
|
285d04 |
+sdb_getFallbackTempDir(void)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ /* sqlite uses sqlite3_temp_directory if it is not NULL. We don't have
|
|
|
285d04 |
+ * access to sqlite3_temp_directory because it is not exported from
|
|
|
285d04 |
+ * sqlite3.dll. Assume sqlite3_win32_set_directory isn't called and
|
|
|
285d04 |
+ * sqlite3_temp_directory is NULL.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ char path[MAX_PATH];
|
|
|
285d04 |
+ DWORD rv;
|
|
|
285d04 |
+ size_t len;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ rv = GetTempPathA(MAX_PATH, path);
|
|
|
285d04 |
+ if (rv > MAX_PATH || rv == 0)
|
|
|
285d04 |
+ return NULL;
|
|
|
285d04 |
+ len = strlen(path);
|
|
|
285d04 |
+ if (len == 0)
|
|
|
285d04 |
+ return NULL;
|
|
|
285d04 |
+ /* The returned string ends with a backslash, for example, "C:\TEMP\". */
|
|
|
285d04 |
+ if (path[len - 1] == '\\')
|
|
|
285d04 |
+ path[len - 1] = '\0';
|
|
|
285d04 |
+ return PORT_Strdup(path);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+#elif defined(XP_UNIX)
|
|
|
285d04 |
+static char *
|
|
|
285d04 |
+sdb_getFallbackTempDir(void)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ const char *azDirs[] = {
|
|
|
285d04 |
+ NULL,
|
|
|
285d04 |
+ NULL,
|
|
|
285d04 |
+ "/var/tmp",
|
|
|
285d04 |
+ "/usr/tmp",
|
|
|
285d04 |
+ "/tmp",
|
|
|
285d04 |
+ NULL /* List terminator */
|
|
|
285d04 |
+ };
|
|
|
285d04 |
+ unsigned int i;
|
|
|
285d04 |
+ struct stat buf;
|
|
|
285d04 |
+ const char *zDir = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ azDirs[0] = sqlite3_temp_directory;
|
|
|
285d04 |
+ azDirs[1] = PR_GetEnvSecure("TMPDIR");
|
|
|
285d04 |
+
|
|
|
285d04 |
+ for (i = 0; i < PR_ARRAY_SIZE(azDirs); i++) {
|
|
|
285d04 |
+ zDir = azDirs[i];
|
|
|
285d04 |
+ if (zDir == NULL)
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ if (stat(zDir, &buf))
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ if (!S_ISDIR(buf.st_mode))
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ if (access(zDir, 07))
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (zDir == NULL)
|
|
|
285d04 |
+ return NULL;
|
|
|
285d04 |
+ return PORT_Strdup(zDir);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+#else
|
|
|
285d04 |
+#error "sdb_getFallbackTempDir not implemented"
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+
|
|
|
285d04 |
+#ifndef SQLITE_FCNTL_TEMPFILENAME
|
|
|
285d04 |
+/* SQLITE_FCNTL_TEMPFILENAME was added in SQLite 3.7.15 */
|
|
|
285d04 |
+#define SQLITE_FCNTL_TEMPFILENAME 16
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+
|
|
|
285d04 |
+static char *
|
|
|
285d04 |
+sdb_getTempDir(sqlite3 *sqlDB)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ int sqlrv;
|
|
|
285d04 |
+ char *result = NULL;
|
|
|
285d04 |
+ char *tempName = NULL;
|
|
|
285d04 |
+ char *foundSeparator = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Obtain temporary filename in sqlite's directory for temporary tables */
|
|
|
285d04 |
+ sqlrv = sqlite3_file_control(sqlDB, 0, SQLITE_FCNTL_TEMPFILENAME,
|
|
|
285d04 |
+ (void *)&tempName);
|
|
|
285d04 |
+ if (sqlrv == SQLITE_NOTFOUND) {
|
|
|
285d04 |
+ /* SQLITE_FCNTL_TEMPFILENAME not implemented because we are using
|
|
|
285d04 |
+ * an older SQLite. */
|
|
|
285d04 |
+ return sdb_getFallbackTempDir();
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlrv != SQLITE_OK) {
|
|
|
285d04 |
+ return NULL;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* We'll extract the temporary directory from tempName */
|
|
|
285d04 |
+ foundSeparator = PORT_Strrchr(tempName, PR_GetDirectorySeparator());
|
|
|
285d04 |
+ if (foundSeparator) {
|
|
|
285d04 |
+ /* We shorten the temp filename string to contain only
|
|
|
285d04 |
+ * the directory name (including the trailing separator).
|
|
|
285d04 |
+ * We know the byte after the foundSeparator position is
|
|
|
285d04 |
+ * safe to use, in the shortest scenario it contains the
|
|
|
285d04 |
+ * end-of-string byte.
|
|
|
285d04 |
+ * By keeping the separator at the found position, it will
|
|
|
285d04 |
+ * even work if tempDir consists of the separator, only.
|
|
|
285d04 |
+ * (In this case the toplevel directory will be used for
|
|
|
285d04 |
+ * access speed testing). */
|
|
|
285d04 |
+ ++foundSeparator;
|
|
|
285d04 |
+ *foundSeparator = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Now we copy the directory name for our caller */
|
|
|
285d04 |
+ result = PORT_Strdup(tempName);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sqlite3_free(tempName);
|
|
|
285d04 |
+ return result;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * Map SQL_LITE errors to PKCS #11 errors as best we can.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static CK_RV
|
|
|
285d04 |
+sdb_mapSQLError(sdbDataType type, int sqlerr)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ switch (sqlerr) {
|
|
|
285d04 |
+ /* good matches */
|
|
|
285d04 |
+ case SQLITE_OK:
|
|
|
285d04 |
+ case SQLITE_DONE:
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ case SQLITE_NOMEM:
|
|
|
285d04 |
+ return CKR_HOST_MEMORY;
|
|
|
285d04 |
+ case SQLITE_READONLY:
|
|
|
285d04 |
+ return CKR_TOKEN_WRITE_PROTECTED;
|
|
|
285d04 |
+ /* close matches */
|
|
|
285d04 |
+ case SQLITE_AUTH:
|
|
|
285d04 |
+ case SQLITE_PERM:
|
|
|
285d04 |
+ /*return CKR_USER_NOT_LOGGED_IN; */
|
|
|
285d04 |
+ case SQLITE_CANTOPEN:
|
|
|
285d04 |
+ case SQLITE_NOTFOUND:
|
|
|
285d04 |
+ /* NSS distiguishes between failure to open the cert and the key db */
|
|
|
285d04 |
+ return type == SDB_CERT ? CKR_NSS_CERTDB_FAILED : CKR_NSS_KEYDB_FAILED;
|
|
|
285d04 |
+ case SQLITE_IOERR:
|
|
|
285d04 |
+ return CKR_DEVICE_ERROR;
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return CKR_GENERAL_ERROR;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * build up database name from a directory, prefix, name, version and flags.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static char *
|
|
|
285d04 |
+sdb_BuildFileName(const char *directory,
|
|
|
285d04 |
+ const char *prefix, const char *type,
|
|
|
285d04 |
+ int version)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ char *dbname = NULL;
|
|
|
285d04 |
+ /* build the full dbname */
|
|
|
285d04 |
+ dbname = sqlite3_mprintf("%s%c%s%s%d.db", directory,
|
|
|
285d04 |
+ (int)(unsigned char)PR_GetDirectorySeparator(),
|
|
|
285d04 |
+ prefix, type, version);
|
|
|
285d04 |
+ return dbname;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * find out how expensive the access system call is for non-existant files
|
|
|
285d04 |
+ * in the given directory. Return the number of operations done in 33 ms.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static PRUint32
|
|
|
285d04 |
+sdb_measureAccess(const char *directory)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ PRUint32 i;
|
|
|
285d04 |
+ PRIntervalTime time;
|
|
|
285d04 |
+ PRIntervalTime delta;
|
|
|
285d04 |
+ PRIntervalTime duration = PR_MillisecondsToInterval(33);
|
|
|
285d04 |
+ const char *doesntExistName = "_dOeSnotExist_.db";
|
|
|
285d04 |
+ char *temp, *tempStartOfFilename;
|
|
|
285d04 |
+ size_t maxTempLen, maxFileNameLen, directoryLength, tmpdirLength = 0;
|
|
|
285d04 |
+#ifdef SDB_MEASURE_USE_TEMP_DIR
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * on some OS's and Filesystems, creating a bunch of files and deleting
|
|
|
285d04 |
+ * them messes up the systems's caching, but if we create the files in
|
|
|
285d04 |
+ * a temp directory which we later delete, then the cache gets cleared
|
|
|
285d04 |
+ * up. This code uses several OS dependent calls, and it's not clear
|
|
|
285d04 |
+ * that temp directory use won't mess up other filesystems and OS caching,
|
|
|
285d04 |
+ * so if you need this for your OS, you can turn on the
|
|
|
285d04 |
+ * 'SDB_MEASURE_USE_TEMP_DIR' define in coreconf
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ const char template[] = "dbTemp.XXXXXX";
|
|
|
285d04 |
+ tmpdirLength = sizeof(template);
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+ /* no directory, just return one */
|
|
|
285d04 |
+ if (directory == NULL) {
|
|
|
285d04 |
+ return 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* our calculation assumes time is a 4 bytes == 32 bit integer */
|
|
|
285d04 |
+ PORT_Assert(sizeof(time) == 4);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ directoryLength = strlen(directory);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ maxTempLen = directoryLength + 1 /* dirname + / */
|
|
|
285d04 |
+ + tmpdirLength /* tmpdirname includes / */
|
|
|
285d04 |
+ + strlen(doesntExistName) /* filename base */
|
|
|
285d04 |
+ + 11 /* max chars for 32 bit int plus potential sign */
|
|
|
285d04 |
+ + 1; /* zero terminator */
|
|
|
285d04 |
+
|
|
|
285d04 |
+ temp = PORT_ZAlloc(maxTempLen);
|
|
|
285d04 |
+ if (!temp) {
|
|
|
285d04 |
+ return 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* We'll copy directory into temp just once, then ensure it ends
|
|
|
285d04 |
+ * with the directory separator. */
|
|
|
285d04 |
+
|
|
|
285d04 |
+ strcpy(temp, directory);
|
|
|
285d04 |
+ if (directory[directoryLength - 1] != PR_GetDirectorySeparator()) {
|
|
|
285d04 |
+ temp[directoryLength++] = PR_GetDirectorySeparator();
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+#ifdef SDB_MEASURE_USE_TEMP_DIR
|
|
|
285d04 |
+ /* add the template for a temporary subdir, and create it */
|
|
|
285d04 |
+ strcat(temp, template);
|
|
|
285d04 |
+ if (!mkdtemp(temp)) {
|
|
|
285d04 |
+ PORT_Free(temp);
|
|
|
285d04 |
+ return 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* and terminate that tmp subdir with a / */
|
|
|
285d04 |
+ strcat(temp, "/");
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Remember the position after the last separator, and calculate the
|
|
|
285d04 |
+ * number of remaining bytes. */
|
|
|
285d04 |
+ tempStartOfFilename = temp + directoryLength + tmpdirLength;
|
|
|
285d04 |
+ maxFileNameLen = maxTempLen - directoryLength;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* measure number of Access operations that can be done in 33 milliseconds
|
|
|
285d04 |
+ * (1/30'th of a second), or 10000 operations, which ever comes first.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ time = PR_IntervalNow();
|
|
|
285d04 |
+ for (i = 0; i < 10000u; i++) {
|
|
|
285d04 |
+ PRIntervalTime next;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* We'll use the variable part first in the filename string, just in
|
|
|
285d04 |
+ * case it's longer than assumed, so if anything gets cut off, it
|
|
|
285d04 |
+ * will be cut off from the constant part.
|
|
|
285d04 |
+ * This code assumes the directory name at the beginning of
|
|
|
285d04 |
+ * temp remains unchanged during our loop. */
|
|
|
285d04 |
+ PR_snprintf(tempStartOfFilename, maxFileNameLen,
|
|
|
285d04 |
+ ".%lu%s", (PRUint32)(time + i), doesntExistName);
|
|
|
285d04 |
+ PR_Access(temp, PR_ACCESS_EXISTS);
|
|
|
285d04 |
+ next = PR_IntervalNow();
|
|
|
285d04 |
+ delta = next - time;
|
|
|
285d04 |
+ if (delta >= duration)
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+#ifdef SDB_MEASURE_USE_TEMP_DIR
|
|
|
285d04 |
+ /* turn temp back into our tmpdir path by removing doesntExistName, and
|
|
|
285d04 |
+ * remove the tmp dir */
|
|
|
285d04 |
+ *tempStartOfFilename = '\0';
|
|
|
285d04 |
+ (void)rmdir(temp);
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+ PORT_Free(temp);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* always return 1 or greater */
|
|
|
285d04 |
+ return i ? i : 1u;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * some file sytems are very slow to run sqlite3 on, particularly if the
|
|
|
285d04 |
+ * access count is pretty high. On these filesystems is faster to create
|
|
|
285d04 |
+ * a temporary database on the local filesystem and access that. This
|
|
|
285d04 |
+ * code uses a temporary table to create that cache. Temp tables are
|
|
|
285d04 |
+ * automatically cleared when the database handle it was created on
|
|
|
285d04 |
+ * Is freed.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static const char DROP_CACHE_CMD[] = "DROP TABLE %s";
|
|
|
285d04 |
+static const char CREATE_CACHE_CMD[] =
|
|
|
285d04 |
+ "CREATE TEMPORARY TABLE %s AS SELECT * FROM %s";
|
|
|
285d04 |
+static const char CREATE_ISSUER_INDEX_CMD[] =
|
|
|
285d04 |
+ "CREATE INDEX issuer ON %s (a81)";
|
|
|
285d04 |
+static const char CREATE_SUBJECT_INDEX_CMD[] =
|
|
|
285d04 |
+ "CREATE INDEX subject ON %s (a101)";
|
|
|
285d04 |
+static const char CREATE_LABEL_INDEX_CMD[] = "CREATE INDEX label ON %s (a3)";
|
|
|
285d04 |
+static const char CREATE_ID_INDEX_CMD[] = "CREATE INDEX ckaid ON %s (a102)";
|
|
|
285d04 |
+
|
|
|
285d04 |
+static CK_RV
|
|
|
285d04 |
+sdb_buildCache(sqlite3 *sqlDB, sdbDataType type,
|
|
|
285d04 |
+ const char *cacheTable, const char *table)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ char *newStr;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_CACHE_CMD, cacheTable, table);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ return CKR_HOST_MEMORY;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ return sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* failure to create the indexes is not an issue */
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_ISSUER_INDEX_CMD, cacheTable);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_SUBJECT_INDEX_CMD, cacheTable);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_LABEL_INDEX_CMD, cacheTable);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_ID_INDEX_CMD, cacheTable);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * update the cache and the data records describing it.
|
|
|
285d04 |
+ * The cache is updated by dropping the temp database and recreating it.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static CK_RV
|
|
|
285d04 |
+sdb_updateCache(SDBPrivate *sdb_p)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ char *newStr;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* drop the old table */
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(DROP_CACHE_CMD, sdb_p->cacheTable);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ return CKR_HOST_MEMORY;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sdb_p->sqlReadDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ if ((sqlerr != SQLITE_OK) && (sqlerr != SQLITE_ERROR)) {
|
|
|
285d04 |
+ /* something went wrong with the drop, don't try to refresh...
|
|
|
285d04 |
+ * NOTE: SQLITE_ERROR is returned if the table doesn't exist. In
|
|
|
285d04 |
+ * that case, we just continue on and try to reload it */
|
|
|
285d04 |
+ return sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* set up the new table */
|
|
|
285d04 |
+ error = sdb_buildCache(sdb_p->sqlReadDB, sdb_p->type,
|
|
|
285d04 |
+ sdb_p->cacheTable, sdb_p->table);
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ /* we have a new cache! */
|
|
|
285d04 |
+ sdb_p->lastUpdateTime = PR_IntervalNow();
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * The sharing of sqlite3 handles across threads is tricky. Older versions
|
|
|
285d04 |
+ * couldn't at all, but newer ones can under strict conditions. Basically
|
|
|
285d04 |
+ * no 2 threads can use the same handle while another thread has an open
|
|
|
285d04 |
+ * stmt running. Once the sqlite3_stmt is finalized, another thread can then
|
|
|
285d04 |
+ * use the database handle.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * We use monitors to protect against trying to use a database before
|
|
|
285d04 |
+ * it's sqlite3_stmt is finalized. This is preferable to the opening and
|
|
|
285d04 |
+ * closing the database each operation because there is significant overhead
|
|
|
285d04 |
+ * in the open and close. Also continually opening and closing the database
|
|
|
285d04 |
+ * defeats the cache code as the cache table is lost on close (thus
|
|
|
285d04 |
+ * requiring us to have to reinitialize the cache every operation).
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * An execption to the shared handle is transations. All writes happen
|
|
|
285d04 |
+ * through a transaction. When we are in a transaction, we must use the
|
|
|
285d04 |
+ * same database pointer for that entire transation. In this case we save
|
|
|
285d04 |
+ * the transaction database and use it for all accesses on the transaction
|
|
|
285d04 |
+ * thread. Other threads use the common database.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * There can only be once active transaction on the database at a time.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * sdb_openDBLocal() provides us with a valid database handle for whatever
|
|
|
285d04 |
+ * state we are in (reading or in a transaction), and acquires any locks
|
|
|
285d04 |
+ * appropriate to that state. It also decides when it's time to refresh
|
|
|
285d04 |
+ * the cache before we start an operation. Any database handle returned
|
|
|
285d04 |
+ * just eventually be closed with sdb_closeDBLocal().
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * The table returned either points to the database's physical table, or
|
|
|
285d04 |
+ * to the cached shadow. Tranactions always return the physical table
|
|
|
285d04 |
+ * and read operations return either the physical table or the cache
|
|
|
285d04 |
+ * depending on whether or not the cache exists.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static CK_RV
|
|
|
285d04 |
+sdb_openDBLocal(SDBPrivate *sdb_p, sqlite3 **sqlDB, const char **table)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ *sqlDB = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ PR_EnterMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (table) {
|
|
|
285d04 |
+ *table = sdb_p->table;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* We're in a transaction, use the transaction DB */
|
|
|
285d04 |
+ if ((sdb_p->sqlXactDB) && (sdb_p->sqlXactThread == PR_GetCurrentThread())) {
|
|
|
285d04 |
+ *sqlDB = sdb_p->sqlXactDB;
|
|
|
285d04 |
+ /* only one thread can get here, safe to unlock */
|
|
|
285d04 |
+ PR_ExitMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * if we are just reading from the table, we may have the table
|
|
|
285d04 |
+ * cached in a temporary table (especially if it's on a shared FS).
|
|
|
285d04 |
+ * In that case we want to see updates to the table, the the granularity
|
|
|
285d04 |
+ * is on order of human scale, not computer scale.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ if (table && sdb_p->cacheTable) {
|
|
|
285d04 |
+ PRIntervalTime now = PR_IntervalNow();
|
|
|
285d04 |
+ if ((now - sdb_p->lastUpdateTime) > sdb_p->updateInterval) {
|
|
|
285d04 |
+ sdb_updateCache(sdb_p);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ *table = sdb_p->cacheTable;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ *sqlDB = sdb_p->sqlReadDB;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* leave holding the lock. only one thread can actually use a given
|
|
|
285d04 |
+ * database connection at once */
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* closing the local database currenly means unlocking the monitor */
|
|
|
285d04 |
+static CK_RV
|
|
|
285d04 |
+sdb_closeDBLocal(SDBPrivate *sdb_p, sqlite3 *sqlDB)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ if (sdb_p->sqlXactDB != sqlDB) {
|
|
|
285d04 |
+ /* if we weren't in a transaction, we got a lock */
|
|
|
285d04 |
+ PR_ExitMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * wrapper to sqlite3_open which also sets the busy_timeout
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static int
|
|
|
285d04 |
+sdb_openDB(const char *name, sqlite3 **sqlDB, int flags)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ int sqlerr;
|
|
|
285d04 |
+ int openFlags;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ *sqlDB = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (flags & SDB_RDONLY) {
|
|
|
285d04 |
+ openFlags = SQLITE_OPEN_READONLY;
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ openFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
|
|
|
285d04 |
+ /* sqlite 3.34 seem to incorrectly open readwrite.
|
|
|
285d04 |
+ * when the file is readonly. Explicitly reject that issue here */
|
|
|
285d04 |
+ if ((_NSSUTIL_Access(name, PR_ACCESS_EXISTS) == PR_SUCCESS) && (_NSSUTIL_Access(name, PR_ACCESS_WRITE_OK) != PR_SUCCESS)) {
|
|
|
285d04 |
+ return SQLITE_READONLY;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Requires SQLite 3.5.0 or newer. */
|
|
|
285d04 |
+ sqlerr = sqlite3_open_v2(name, sqlDB, openFlags, NULL);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ return sqlerr;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sqlerr = sqlite3_busy_timeout(*sqlDB, SDB_SQLITE_BUSY_TIMEOUT);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ sqlite3_close(*sqlDB);
|
|
|
285d04 |
+ *sqlDB = NULL;
|
|
|
285d04 |
+ return sqlerr;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return SQLITE_OK;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* Sigh, if we created a new table since we opened the database,
|
|
|
285d04 |
+ * the database handle will not see the new table, we need to close this
|
|
|
285d04 |
+ * database and reopen it. Caller must be in a transaction or holding
|
|
|
285d04 |
+ * the dbMon. sqlDB is changed on success. */
|
|
|
285d04 |
+static int
|
|
|
285d04 |
+sdb_reopenDBLocal(SDBPrivate *sdb_p, sqlite3 **sqlDB)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ sqlite3 *newDB;
|
|
|
285d04 |
+ int sqlerr;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* open a new database */
|
|
|
285d04 |
+ sqlerr = sdb_openDB(sdb_p->sqlDBName, &newDB, SDB_RDONLY);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ return sqlerr;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* if we are in a transaction, we may not be holding the monitor.
|
|
|
285d04 |
+ * grab it before we update the transaction database. This is
|
|
|
285d04 |
+ * safe since are using monitors. */
|
|
|
285d04 |
+ PR_EnterMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ /* update our view of the database */
|
|
|
285d04 |
+ if (sdb_p->sqlReadDB == *sqlDB) {
|
|
|
285d04 |
+ sdb_p->sqlReadDB = newDB;
|
|
|
285d04 |
+ } else if (sdb_p->sqlXactDB == *sqlDB) {
|
|
|
285d04 |
+ sdb_p->sqlXactDB = newDB;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ PR_ExitMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* close the old one */
|
|
|
285d04 |
+ sqlite3_close(*sqlDB);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ *sqlDB = newDB;
|
|
|
285d04 |
+ return SQLITE_OK;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+struct SDBFindStr {
|
|
|
285d04 |
+ sqlite3 *sqlDB;
|
|
|
285d04 |
+ sqlite3_stmt *findstmt;
|
|
|
285d04 |
+};
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char FIND_OBJECTS_CMD[] = "SELECT ALL id FROM %s WHERE %s;";
|
|
|
285d04 |
+static const char FIND_OBJECTS_ALL_CMD[] = "SELECT ALL id FROM %s;";
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_FindObjectsInit(SDB *sdb, const CK_ATTRIBUTE *template, CK_ULONG count,
|
|
|
285d04 |
+ SDBFind **find)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ const char *table;
|
|
|
285d04 |
+ char *newStr, *findStr = NULL;
|
|
|
285d04 |
+ sqlite3_stmt *findstmt = NULL;
|
|
|
285d04 |
+ char *join = "";
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ unsigned int i;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ *find = NULL;
|
|
|
285d04 |
+ error = sdb_openDBLocal(sdb_p, &sqlDB, &table);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ findStr = sqlite3_mprintf("");
|
|
|
285d04 |
+ for (i = 0; findStr && i < count; i++) {
|
|
|
285d04 |
+ newStr = sqlite3_mprintf("%s%sa%x=$DATA%d", findStr, join,
|
|
|
285d04 |
+ template[i].type, i);
|
|
|
285d04 |
+ join = " AND ";
|
|
|
285d04 |
+ sqlite3_free(findStr);
|
|
|
285d04 |
+ findStr = newStr;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (findStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (count == 0) {
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(FIND_OBJECTS_ALL_CMD, table);
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(FIND_OBJECTS_CMD, table, findStr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlite3_free(findStr);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, newStr, -1, &findstmt, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ for (i = 0; sqlerr == SQLITE_OK && i < count; i++) {
|
|
|
285d04 |
+ const void *blobData = template[i].pValue;
|
|
|
285d04 |
+ unsigned int blobSize = template[i].ulValueLen;
|
|
|
285d04 |
+ if (blobSize == 0) {
|
|
|
285d04 |
+ blobSize = SQLITE_EXPLICIT_NULL_LEN;
|
|
|
285d04 |
+ blobData = SQLITE_EXPLICIT_NULL;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_blob(findstmt, i + 1, blobData, blobSize,
|
|
|
285d04 |
+ SQLITE_TRANSIENT);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr == SQLITE_OK) {
|
|
|
285d04 |
+ *find = PORT_New(SDBFind);
|
|
|
285d04 |
+ if (*find == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ (*find)->findstmt = findstmt;
|
|
|
285d04 |
+ (*find)->sqlDB = sqlDB;
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ if (findstmt) {
|
|
|
285d04 |
+ sqlite3_reset(findstmt);
|
|
|
285d04 |
+ sqlite3_finalize(findstmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_FindObjects(SDB *sdb, SDBFind *sdbFind, CK_OBJECT_HANDLE *object,
|
|
|
285d04 |
+ CK_ULONG arraySize, CK_ULONG *count)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = sdbFind->findstmt;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ *count = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (arraySize == 0) {
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr == SQLITE_ROW) {
|
|
|
285d04 |
+ /* only care about the id */
|
|
|
285d04 |
+ *object++ = sqlite3_column_int(stmt, 0);
|
|
|
285d04 |
+ arraySize--;
|
|
|
285d04 |
+ (*count)++;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry) && (arraySize > 0));
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* we only have some of the objects, there is probably more,
|
|
|
285d04 |
+ * set the sqlerr to an OK value so we return CKR_OK */
|
|
|
285d04 |
+ if (sqlerr == SQLITE_ROW && arraySize == 0) {
|
|
|
285d04 |
+ sqlerr = SQLITE_DONE;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_FindObjectsFinal(SDB *sdb, SDBFind *sdbFind)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = sdbFind->findstmt;
|
|
|
285d04 |
+ sqlite3 *sqlDB = sdbFind->sqlDB;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlerr = sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ PORT_Free(sdbFind);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static CK_RV
|
|
|
285d04 |
+sdb_GetValidAttributeValueNoLock(SDB *sdb, CK_OBJECT_HANDLE object_id,
|
|
|
285d04 |
+ CK_ATTRIBUTE *template, CK_ULONG count)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ const char *table = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ int found = 0;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+ unsigned int i;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (count == 0) {
|
|
|
285d04 |
+ error = CKR_OBJECT_HANDLE_INVALID;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* open a new db if necessary */
|
|
|
285d04 |
+ error = sdb_openDBLocal(sdb_p, &sqlDB, &table);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ char *columns = NULL;
|
|
|
285d04 |
+ for (i = 0; i < count; i++) {
|
|
|
285d04 |
+ char *newColumns;
|
|
|
285d04 |
+ if (columns) {
|
|
|
285d04 |
+ newColumns = sqlite3_mprintf("%s, a%x", columns, template[i].type);
|
|
|
285d04 |
+ sqlite3_free(columns);
|
|
|
285d04 |
+ columns = NULL;
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ newColumns = sqlite3_mprintf("a%x", template[i].type);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (!newColumns) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ columns = newColumns;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ PORT_Assert(columns);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ char *statement = sqlite3_mprintf("SELECT DISTINCT %s FROM %s where id=$ID LIMIT 1;",
|
|
|
285d04 |
+ columns, table);
|
|
|
285d04 |
+ sqlite3_free(columns);
|
|
|
285d04 |
+ columns = NULL;
|
|
|
285d04 |
+ if (!statement) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, statement, -1, &stmt, NULL);
|
|
|
285d04 |
+ sqlite3_free(statement);
|
|
|
285d04 |
+ statement = NULL;
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ // NB: indices in sqlite3_bind_int are 1-indexed
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_int(stmt, 1, object_id);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr == SQLITE_ROW) {
|
|
|
285d04 |
+ PORT_Assert(!found);
|
|
|
285d04 |
+ for (i = 0; i < count; i++) {
|
|
|
285d04 |
+ unsigned int blobSize;
|
|
|
285d04 |
+ const char *blobData;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ // NB: indices in sqlite_column_{bytes,blob} are 0-indexed
|
|
|
285d04 |
+ blobSize = sqlite3_column_bytes(stmt, i);
|
|
|
285d04 |
+ blobData = sqlite3_column_blob(stmt, i);
|
|
|
285d04 |
+ if (blobData == NULL) {
|
|
|
285d04 |
+ /* PKCS 11 requires that get attributes process all the
|
|
|
285d04 |
+ * attributes in the template, marking the attributes with
|
|
|
285d04 |
+ * issues with -1. Mark the error but continue */
|
|
|
285d04 |
+ template[i].ulValueLen = -1;
|
|
|
285d04 |
+ error = CKR_ATTRIBUTE_TYPE_INVALID;
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* If the blob equals our explicit NULL value, then the
|
|
|
285d04 |
+ * attribute is a NULL. */
|
|
|
285d04 |
+ if ((blobSize == SQLITE_EXPLICIT_NULL_LEN) &&
|
|
|
285d04 |
+ (PORT_Memcmp(blobData, SQLITE_EXPLICIT_NULL,
|
|
|
285d04 |
+ SQLITE_EXPLICIT_NULL_LEN) == 0)) {
|
|
|
285d04 |
+ blobSize = 0;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (template[i].pValue) {
|
|
|
285d04 |
+ if (template[i].ulValueLen < blobSize) {
|
|
|
285d04 |
+ /* like CKR_ATTRIBUTE_TYPE_INVALID, continue processing */
|
|
|
285d04 |
+ template[i].ulValueLen = -1;
|
|
|
285d04 |
+ error = CKR_BUFFER_TOO_SMALL;
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ PORT_Memcpy(template[i].pValue, blobData, blobSize);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ template[i].ulValueLen = blobSize;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ found = 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ stmt = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ /* fix up the error if necessary */
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ if (!found && error == CKR_OK) {
|
|
|
285d04 |
+ error = CKR_OBJECT_HANDLE_INVALID;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* if we had to open a new database, free it now */
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* NOTE: requires sdb_p->schemaAttrs to be sorted asc. */
|
|
|
285d04 |
+inline static PRBool
|
|
|
285d04 |
+sdb_attributeExists(SDB *sdb, CK_ATTRIBUTE_TYPE attr)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ int first = 0;
|
|
|
285d04 |
+ int last = (int)sdb_p->numSchemaAttrs - 1;
|
|
|
285d04 |
+ while (last >= first) {
|
|
|
285d04 |
+ int mid = first + (last - first) / 2;
|
|
|
285d04 |
+ if (sdb_p->schemaAttrs[mid] == attr) {
|
|
|
285d04 |
+ return PR_TRUE;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (attr > sdb_p->schemaAttrs[mid]) {
|
|
|
285d04 |
+ first = mid + 1;
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ last = mid - 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return PR_FALSE;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_GetAttributeValue(SDB *sdb, CK_OBJECT_HANDLE object_id,
|
|
|
285d04 |
+ CK_ATTRIBUTE *template, CK_ULONG count)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ CK_RV crv = CKR_OK;
|
|
|
285d04 |
+ unsigned int tmplIdx;
|
|
|
285d04 |
+ unsigned int resIdx = 0;
|
|
|
285d04 |
+ unsigned int validCount = 0;
|
|
|
285d04 |
+ unsigned int i;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (count == 0) {
|
|
|
285d04 |
+ return crv;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ CK_ATTRIBUTE *validTemplate;
|
|
|
285d04 |
+ PRBool invalidExists = PR_FALSE;
|
|
|
285d04 |
+ for (tmplIdx = 0; tmplIdx < count; tmplIdx++) {
|
|
|
285d04 |
+ if (!sdb_attributeExists(sdb, template[tmplIdx].type)) {
|
|
|
285d04 |
+ template[tmplIdx].ulValueLen = -1;
|
|
|
285d04 |
+ crv = CKR_ATTRIBUTE_TYPE_INVALID;
|
|
|
285d04 |
+ invalidExists = PR_TRUE;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (!invalidExists) {
|
|
|
285d04 |
+ validTemplate = template;
|
|
|
285d04 |
+ validCount = count;
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ /* Create a new template containing only the valid subset of
|
|
|
285d04 |
+ * input |template|, and query with that. */
|
|
|
285d04 |
+ validCount = tmplIdx;
|
|
|
285d04 |
+ validTemplate = malloc(sizeof(CK_ATTRIBUTE) * count);
|
|
|
285d04 |
+ if (!validTemplate) {
|
|
|
285d04 |
+ return CKR_HOST_MEMORY;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* Copy in what we already know is valid. */
|
|
|
285d04 |
+ for (i = 0; i < validCount; i++) {
|
|
|
285d04 |
+ validTemplate[i] = template[i];
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* tmplIdx was left at the index of the first invalid
|
|
|
285d04 |
+ * attribute, which has been handled. We only need to
|
|
|
285d04 |
+ * deal with the remainder. */
|
|
|
285d04 |
+ tmplIdx++;
|
|
|
285d04 |
+ for (; tmplIdx < count; tmplIdx++) {
|
|
|
285d04 |
+ if (sdb_attributeExists(sdb, template[tmplIdx].type)) {
|
|
|
285d04 |
+ validTemplate[validCount++] = template[tmplIdx];
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ template[tmplIdx].ulValueLen = -1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (validCount) {
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ CK_RV crv2 = sdb_GetValidAttributeValueNoLock(sdb, object_id, validTemplate, validCount);
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* If an invalid attribute was removed above, let
|
|
|
285d04 |
+ * the caller know. Any other error from the actual
|
|
|
285d04 |
+ * query should propogate. */
|
|
|
285d04 |
+ crv = (crv2 == CKR_OK) ? crv : crv2;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (invalidExists) {
|
|
|
285d04 |
+ /* Copy out valid lengths. */
|
|
|
285d04 |
+ tmplIdx = 0;
|
|
|
285d04 |
+ for (resIdx = 0; resIdx < validCount; resIdx++) {
|
|
|
285d04 |
+ for (; tmplIdx < count; tmplIdx++) {
|
|
|
285d04 |
+ if (template[tmplIdx].type != validTemplate[resIdx].type) {
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ template[tmplIdx].ulValueLen = validTemplate[resIdx].ulValueLen;
|
|
|
285d04 |
+ tmplIdx++;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ free(validTemplate);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return crv;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char SET_ATTRIBUTE_CMD[] = "UPDATE %s SET %s WHERE id=$ID;";
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_SetAttributeValue(SDB *sdb, CK_OBJECT_HANDLE object_id,
|
|
|
285d04 |
+ const CK_ATTRIBUTE *template, CK_ULONG count)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ char *setStr = NULL;
|
|
|
285d04 |
+ char *newStr = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ unsigned int i;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if ((sdb->sdb_flags & SDB_RDONLY) != 0) {
|
|
|
285d04 |
+ return CKR_TOKEN_WRITE_PROTECTED;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (count == 0) {
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ setStr = sqlite3_mprintf("");
|
|
|
285d04 |
+ for (i = 0; setStr && i < count; i++) {
|
|
|
285d04 |
+ if (i == 0) {
|
|
|
285d04 |
+ sqlite3_free(setStr);
|
|
|
285d04 |
+ setStr = sqlite3_mprintf("a%x=$VALUE%d",
|
|
|
285d04 |
+ template[i].type, i);
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ newStr = sqlite3_mprintf("%s,a%x=$VALUE%d", setStr,
|
|
|
285d04 |
+ template[i].type, i);
|
|
|
285d04 |
+ sqlite3_free(setStr);
|
|
|
285d04 |
+ setStr = newStr;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ newStr = NULL;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (setStr == NULL) {
|
|
|
285d04 |
+ return CKR_HOST_MEMORY;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(SET_ATTRIBUTE_CMD, sdb_p->table, setStr);
|
|
|
285d04 |
+ sqlite3_free(setStr);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return CKR_HOST_MEMORY;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ error = sdb_openDBLocal(sdb_p, &sqlDB, NULL);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, newStr, -1, &stmt, NULL);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ for (i = 0; i < count; i++) {
|
|
|
285d04 |
+ if (template[i].ulValueLen != 0) {
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_blob(stmt, i + 1, template[i].pValue,
|
|
|
285d04 |
+ template[i].ulValueLen, SQLITE_STATIC);
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_blob(stmt, i + 1, SQLITE_EXPLICIT_NULL,
|
|
|
285d04 |
+ SQLITE_EXPLICIT_NULL_LEN, SQLITE_STATIC);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_int(stmt, i + 1, object_id);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ if (newStr) {
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * check to see if a candidate object handle already exists.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static PRBool
|
|
|
285d04 |
+sdb_objectExists(SDB *sdb, CK_OBJECT_HANDLE candidate)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ CK_RV crv;
|
|
|
285d04 |
+ CK_ATTRIBUTE template = { CKA_LABEL, NULL, 0 };
|
|
|
285d04 |
+
|
|
|
285d04 |
+ crv = sdb_GetValidAttributeValueNoLock(sdb, candidate, &template, 1);
|
|
|
285d04 |
+ if (crv == CKR_OBJECT_HANDLE_INVALID) {
|
|
|
285d04 |
+ return PR_FALSE;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return PR_TRUE;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * if we're here, we are in a transaction, so it's safe
|
|
|
285d04 |
+ * to examine the current state of the database
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static CK_OBJECT_HANDLE
|
|
|
285d04 |
+sdb_getObjectId(SDB *sdb)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ CK_OBJECT_HANDLE candidate;
|
|
|
285d04 |
+ static CK_OBJECT_HANDLE next_obj = CK_INVALID_HANDLE;
|
|
|
285d04 |
+ int count;
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * get an initial object handle to use
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ if (next_obj == CK_INVALID_HANDLE) {
|
|
|
285d04 |
+ PRTime time;
|
|
|
285d04 |
+ time = PR_Now();
|
|
|
285d04 |
+
|
|
|
285d04 |
+ next_obj = (CK_OBJECT_HANDLE)(time & 0x3fffffffL);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ candidate = next_obj++;
|
|
|
285d04 |
+ /* detect that we've looped through all the handles... */
|
|
|
285d04 |
+ for (count = 0; count < 0x40000000; count++, candidate = next_obj++) {
|
|
|
285d04 |
+ /* mask off excess bits */
|
|
|
285d04 |
+ candidate &= 0x3fffffff;
|
|
|
285d04 |
+ /* if we hit zero, go to the next entry */
|
|
|
285d04 |
+ if (candidate == CK_INVALID_HANDLE) {
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* make sure we aren't already using */
|
|
|
285d04 |
+ if (!sdb_objectExists(sdb, candidate)) {
|
|
|
285d04 |
+ /* this one is free */
|
|
|
285d04 |
+ return candidate;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* no handle is free, fail */
|
|
|
285d04 |
+ return CK_INVALID_HANDLE;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_GetNewObjectID(SDB *sdb, CK_OBJECT_HANDLE *object)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ CK_OBJECT_HANDLE id;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ id = sdb_getObjectId(sdb);
|
|
|
285d04 |
+ if (id == CK_INVALID_HANDLE) {
|
|
|
285d04 |
+ return CKR_DEVICE_MEMORY; /* basically we ran out of resources */
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ *object = id;
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char CREATE_CMD[] = "INSERT INTO %s (id%s) VALUES($ID%s);";
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_CreateObject(SDB *sdb, CK_OBJECT_HANDLE *object_id,
|
|
|
285d04 |
+ const CK_ATTRIBUTE *template, CK_ULONG count)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ char *columnStr = NULL;
|
|
|
285d04 |
+ char *valueStr = NULL;
|
|
|
285d04 |
+ char *newStr = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ CK_OBJECT_HANDLE this_object = CK_INVALID_HANDLE;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+ unsigned int i;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if ((sdb->sdb_flags & SDB_RDONLY) != 0) {
|
|
|
285d04 |
+ return CKR_TOKEN_WRITE_PROTECTED;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ if ((*object_id != CK_INVALID_HANDLE) &&
|
|
|
285d04 |
+ !sdb_objectExists(sdb, *object_id)) {
|
|
|
285d04 |
+ this_object = *object_id;
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ this_object = sdb_getObjectId(sdb);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (this_object == CK_INVALID_HANDLE) {
|
|
|
285d04 |
+ UNLOCK_SQLITE();
|
|
|
285d04 |
+ return CKR_HOST_MEMORY;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ columnStr = sqlite3_mprintf("");
|
|
|
285d04 |
+ valueStr = sqlite3_mprintf("");
|
|
|
285d04 |
+ *object_id = this_object;
|
|
|
285d04 |
+ for (i = 0; columnStr && valueStr && i < count; i++) {
|
|
|
285d04 |
+ newStr = sqlite3_mprintf("%s,a%x", columnStr, template[i].type);
|
|
|
285d04 |
+ sqlite3_free(columnStr);
|
|
|
285d04 |
+ columnStr = newStr;
|
|
|
285d04 |
+ newStr = sqlite3_mprintf("%s,$VALUE%d", valueStr, i);
|
|
|
285d04 |
+ sqlite3_free(valueStr);
|
|
|
285d04 |
+ valueStr = newStr;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ newStr = NULL;
|
|
|
285d04 |
+ if ((columnStr == NULL) || (valueStr == NULL)) {
|
|
|
285d04 |
+ if (columnStr) {
|
|
|
285d04 |
+ sqlite3_free(columnStr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (valueStr) {
|
|
|
285d04 |
+ sqlite3_free(valueStr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return CKR_HOST_MEMORY;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_CMD, sdb_p->table, columnStr, valueStr);
|
|
|
285d04 |
+ sqlite3_free(columnStr);
|
|
|
285d04 |
+ sqlite3_free(valueStr);
|
|
|
285d04 |
+ error = sdb_openDBLocal(sdb_p, &sqlDB, NULL);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, newStr, -1, &stmt, NULL);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_int(stmt, 1, *object_id);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ for (i = 0; i < count; i++) {
|
|
|
285d04 |
+ if (template[i].ulValueLen) {
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_blob(stmt, i + 2, template[i].pValue,
|
|
|
285d04 |
+ template[i].ulValueLen, SQLITE_STATIC);
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_blob(stmt, i + 2, SQLITE_EXPLICIT_NULL,
|
|
|
285d04 |
+ SQLITE_EXPLICIT_NULL_LEN, SQLITE_STATIC);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ if (newStr) {
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * Generic destroy that can destroy metadata or objects
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static const char DESTROY_CMD[] = "DELETE FROM %s WHERE (id=$ID);";
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_destroyAnyObject(SDB *sdb, const char *table,
|
|
|
285d04 |
+ CK_OBJECT_HANDLE object_id, const char *string_id)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ char *newStr = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if ((sdb->sdb_flags & SDB_RDONLY) != 0) {
|
|
|
285d04 |
+ return CKR_TOKEN_WRITE_PROTECTED;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ error = sdb_openDBLocal(sdb_p, &sqlDB, NULL);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(DESTROY_CMD, table);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, newStr, -1, &stmt, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ if (string_id == NULL) {
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_int(stmt, 1, object_id);
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_text(stmt, 1, string_id,
|
|
|
285d04 |
+ PORT_Strlen(string_id), SQLITE_STATIC);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_DestroyObject(SDB *sdb, CK_OBJECT_HANDLE object_id)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ return sdb_destroyAnyObject(sdb, sdb_p->table, object_id, NULL);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_DestroyMetaData(SDB *sdb, const char *id)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ return sdb_destroyAnyObject(sdb, "metaData", 0, id);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char BEGIN_CMD[] = "BEGIN IMMEDIATE TRANSACTION;";
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * start a transaction.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * We need to open a new database, then store that new database into
|
|
|
285d04 |
+ * the private data structure. We open the database first, then use locks
|
|
|
285d04 |
+ * to protect storing the data to prevent deadlocks.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_Begin(SDB *sdb)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if ((sdb->sdb_flags & SDB_RDONLY) != 0) {
|
|
|
285d04 |
+ return CKR_TOKEN_WRITE_PROTECTED;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* get a new version that we will use for the entire transaction */
|
|
|
285d04 |
+ sqlerr = sdb_openDB(sdb_p->sqlDBName, &sqlDB, SDB_RDWR);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, BEGIN_CMD, -1, &stmt, NULL);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* don't retry BEGIN transaction*/
|
|
|
285d04 |
+ retry = 0;
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* we are starting a new transaction,
|
|
|
285d04 |
+ * and if we succeeded, then save this database for the rest of
|
|
|
285d04 |
+ * our transaction */
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ /* we hold a 'BEGIN TRANSACTION' and a sdb_p->lock. At this point
|
|
|
285d04 |
+ * sdb_p->sqlXactDB MUST be null */
|
|
|
285d04 |
+ PR_EnterMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ PORT_Assert(sdb_p->sqlXactDB == NULL);
|
|
|
285d04 |
+ sdb_p->sqlXactDB = sqlDB;
|
|
|
285d04 |
+ sdb_p->sqlXactThread = PR_GetCurrentThread();
|
|
|
285d04 |
+ PR_ExitMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ } else {
|
|
|
285d04 |
+ /* we failed to start our transaction,
|
|
|
285d04 |
+ * free any databases we opened. */
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sqlite3_close(sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * Complete a transaction. Basically undo everything we did in begin.
|
|
|
285d04 |
+ * There are 2 flavors Abort and Commit. Basically the only differerence between
|
|
|
285d04 |
+ * these 2 are what the database will show. (no change in to former, change in
|
|
|
285d04 |
+ * the latter).
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static CK_RV
|
|
|
285d04 |
+sdb_complete(SDB *sdb, const char *cmd)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if ((sdb->sdb_flags & SDB_RDONLY) != 0) {
|
|
|
285d04 |
+ return CKR_TOKEN_WRITE_PROTECTED;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* We must have a transation database, or we shouldn't have arrived here */
|
|
|
285d04 |
+ PR_EnterMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ PORT_Assert(sdb_p->sqlXactDB);
|
|
|
285d04 |
+ if (sdb_p->sqlXactDB == NULL) {
|
|
|
285d04 |
+ PR_ExitMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ return CKR_GENERAL_ERROR; /* shouldn't happen */
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ PORT_Assert(sdb_p->sqlXactThread == PR_GetCurrentThread());
|
|
|
285d04 |
+ if (sdb_p->sqlXactThread != PR_GetCurrentThread()) {
|
|
|
285d04 |
+ PR_ExitMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ return CKR_GENERAL_ERROR; /* shouldn't happen */
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlDB = sdb_p->sqlXactDB;
|
|
|
285d04 |
+ sdb_p->sqlXactDB = NULL; /* no one else can get to this DB,
|
|
|
285d04 |
+ * safe to unlock */
|
|
|
285d04 |
+ sdb_p->sqlXactThread = NULL;
|
|
|
285d04 |
+ PR_ExitMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, cmd, -1, &stmt, NULL);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Pending BEGIN TRANSACTIONS Can move forward at this point. */
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* we we have a cached DB image, update it as well */
|
|
|
285d04 |
+ if (sdb_p->cacheTable) {
|
|
|
285d04 |
+ PR_EnterMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ sdb_updateCache(sdb_p);
|
|
|
285d04 |
+ PR_ExitMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* We just finished a transaction.
|
|
|
285d04 |
+ * Free the database, and remove it from the list */
|
|
|
285d04 |
+ sqlite3_close(sqlDB);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char COMMIT_CMD[] = "COMMIT TRANSACTION;";
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_Commit(SDB *sdb)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ CK_RV crv;
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ crv = sdb_complete(sdb, COMMIT_CMD);
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return crv;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char ROLLBACK_CMD[] = "ROLLBACK TRANSACTION;";
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_Abort(SDB *sdb)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ CK_RV crv;
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ crv = sdb_complete(sdb, ROLLBACK_CMD);
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return crv;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static int tableExists(sqlite3 *sqlDB, const char *tableName);
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char GET_PW_CMD[] = "SELECT ALL * FROM metaData WHERE id=$ID;";
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_GetMetaData(SDB *sdb, const char *id, SECItem *item1, SECItem *item2)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = sdb_p->sqlXactDB;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ int found = 0;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ error = sdb_openDBLocal(sdb_p, &sqlDB, NULL);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* handle 'test' versions of the sqlite db */
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, GET_PW_CMD, -1, &stmt, NULL);
|
|
|
285d04 |
+ /* Sigh, if we created a new table since we opened the database,
|
|
|
285d04 |
+ * the database handle will not see the new table, we need to close this
|
|
|
285d04 |
+ * database and reopen it. This is safe because we are holding the lock
|
|
|
285d04 |
+ * still. */
|
|
|
285d04 |
+ if (sqlerr == SQLITE_SCHEMA) {
|
|
|
285d04 |
+ sqlerr = sdb_reopenDBLocal(sdb_p, &sqlDB);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, GET_PW_CMD, -1, &stmt, NULL);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_text(stmt, 1, id, PORT_Strlen(id), SQLITE_STATIC);
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr == SQLITE_ROW) {
|
|
|
285d04 |
+ const char *blobData;
|
|
|
285d04 |
+ unsigned int len = item1->len;
|
|
|
285d04 |
+ item1->len = sqlite3_column_bytes(stmt, 1);
|
|
|
285d04 |
+ if (item1->len > len) {
|
|
|
285d04 |
+ error = CKR_BUFFER_TOO_SMALL;
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ blobData = sqlite3_column_blob(stmt, 1);
|
|
|
285d04 |
+ PORT_Memcpy(item1->data, blobData, item1->len);
|
|
|
285d04 |
+ if (item2) {
|
|
|
285d04 |
+ len = item2->len;
|
|
|
285d04 |
+ item2->len = sqlite3_column_bytes(stmt, 2);
|
|
|
285d04 |
+ if (item2->len > len) {
|
|
|
285d04 |
+ error = CKR_BUFFER_TOO_SMALL;
|
|
|
285d04 |
+ continue;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ blobData = sqlite3_column_blob(stmt, 2);
|
|
|
285d04 |
+ PORT_Memcpy(item2->data, blobData, item2->len);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ found = 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ /* fix up the error if necessary */
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ if (!found && error == CKR_OK) {
|
|
|
285d04 |
+ error = CKR_OBJECT_HANDLE_INVALID;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char PW_CREATE_TABLE_CMD[] =
|
|
|
285d04 |
+ "CREATE TABLE metaData (id PRIMARY KEY UNIQUE ON CONFLICT REPLACE, item1, item2);";
|
|
|
285d04 |
+static const char PW_CREATE_CMD[] =
|
|
|
285d04 |
+ "INSERT INTO metaData (id,item1,item2) VALUES($ID,$ITEM1,$ITEM2);";
|
|
|
285d04 |
+static const char MD_CREATE_CMD[] =
|
|
|
285d04 |
+ "INSERT INTO metaData (id,item1) VALUES($ID,$ITEM1);";
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_PutMetaData(SDB *sdb, const char *id, const SECItem *item1,
|
|
|
285d04 |
+ const SECItem *item2)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = sdb_p->sqlXactDB;
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+ const char *cmd = PW_CREATE_CMD;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if ((sdb->sdb_flags & SDB_RDONLY) != 0) {
|
|
|
285d04 |
+ return CKR_TOKEN_WRITE_PROTECTED;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ error = sdb_openDBLocal(sdb_p, &sqlDB, NULL);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (!tableExists(sqlDB, "metaData")) {
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, PW_CREATE_TABLE_CMD, NULL, 0, NULL);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (item2 == NULL) {
|
|
|
285d04 |
+ cmd = MD_CREATE_CMD;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, cmd, -1, &stmt, NULL);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_text(stmt, 1, id, PORT_Strlen(id), SQLITE_STATIC);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_blob(stmt, 2, item1->data, item1->len, SQLITE_STATIC);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ if (item2) {
|
|
|
285d04 |
+ sqlerr = sqlite3_bind_blob(stmt, 3, item2->data,
|
|
|
285d04 |
+ item2->len, SQLITE_STATIC);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ /* fix up the error if necessary */
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (stmt) {
|
|
|
285d04 |
+ sqlite3_reset(stmt);
|
|
|
285d04 |
+ sqlite3_finalize(stmt);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char RESET_CMD[] = "DELETE FROM %s;";
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_Reset(SDB *sdb)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ char *newStr;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* only Key databases can be reset */
|
|
|
285d04 |
+ if (sdb_p->type != SDB_KEY) {
|
|
|
285d04 |
+ return CKR_OBJECT_HANDLE_INVALID;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ LOCK_SQLITE()
|
|
|
285d04 |
+ error = sdb_openDBLocal(sdb_p, &sqlDB, NULL);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (tableExists(sqlDB, sdb_p->table)) {
|
|
|
285d04 |
+ /* delete the contents of the key table */
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(RESET_CMD, sdb_p->table);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK)
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* delete the password entry table */
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, "DROP TABLE IF EXISTS metaData;",
|
|
|
285d04 |
+ NULL, 0, NULL);
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ /* fix up the error if necessary */
|
|
|
285d04 |
+ if (error == CKR_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sdb_closeDBLocal(sdb_p, sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ UNLOCK_SQLITE()
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_Close(SDB *sdb)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ SDBPrivate *sdb_p = sdb->private;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ sdbDataType type = sdb_p->type;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sqlerr = sqlite3_close(sdb_p->sqlReadDB);
|
|
|
285d04 |
+ PORT_Free(sdb_p->sqlDBName);
|
|
|
285d04 |
+ if (sdb_p->cacheTable) {
|
|
|
285d04 |
+ sqlite3_free(sdb_p->cacheTable);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sdb_p->dbMon) {
|
|
|
285d04 |
+ PR_DestroyMonitor(sdb_p->dbMon);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ free(sdb_p->schemaAttrs);
|
|
|
285d04 |
+ free(sdb_p);
|
|
|
285d04 |
+ free(sdb);
|
|
|
285d04 |
+ return sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * functions to support open
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+
|
|
|
285d04 |
+static const char CHECK_TABLE_CMD[] = "SELECT ALL * FROM %s LIMIT 0;";
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* return 1 if sqlDB contains table 'tableName */
|
|
|
285d04 |
+static int
|
|
|
285d04 |
+tableExists(sqlite3 *sqlDB, const char *tableName)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ char *cmd = sqlite3_mprintf(CHECK_TABLE_CMD, tableName);
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (cmd == NULL) {
|
|
|
285d04 |
+ return 0;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, cmd, NULL, 0, 0);
|
|
|
285d04 |
+ sqlite3_free(cmd);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return (sqlerr == SQLITE_OK) ? 1 : 0;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+void
|
|
|
285d04 |
+sdb_SetForkState(PRBool forked)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ /* XXXright now this is a no-op. The global fork state in the softokn3
|
|
|
285d04 |
+ * shared library is already taken care of at the PKCS#11 level.
|
|
|
285d04 |
+ * If and when we add fork state to the sqlite shared library and extern
|
|
|
285d04 |
+ * interface, we will need to set it and reset it from here */
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+static int
|
|
|
285d04 |
+sdb_attributeComparator(const void *a, const void *b)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ if (*(CK_ATTRIBUTE_TYPE *)a < *(CK_ATTRIBUTE_TYPE *)b) {
|
|
|
285d04 |
+ return -1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (*(CK_ATTRIBUTE_TYPE *)a > *(CK_ATTRIBUTE_TYPE *)b) {
|
|
|
285d04 |
+ return 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ return 0;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/*
|
|
|
285d04 |
+ * initialize a single database
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+static const char INIT_CMD[] =
|
|
|
285d04 |
+ "CREATE TABLE %s (id PRIMARY KEY UNIQUE ON CONFLICT ABORT%s)";
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+sdb_init(char *dbname, char *table, sdbDataType type, int *inUpdate,
|
|
|
285d04 |
+ int *newInit, int inFlags, PRUint32 accessOps, SDB **pSdb)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ int i;
|
|
|
285d04 |
+ char *initStr = NULL;
|
|
|
285d04 |
+ char *newStr;
|
|
|
285d04 |
+ char *queryStr = NULL;
|
|
|
285d04 |
+ int inTransaction = 0;
|
|
|
285d04 |
+ SDB *sdb = NULL;
|
|
|
285d04 |
+ SDBPrivate *sdb_p = NULL;
|
|
|
285d04 |
+ sqlite3 *sqlDB = NULL;
|
|
|
285d04 |
+ int sqlerr = SQLITE_OK;
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ char *cacheTable = NULL;
|
|
|
285d04 |
+ PRIntervalTime now = 0;
|
|
|
285d04 |
+ char *env;
|
|
|
285d04 |
+ PRBool enableCache = PR_FALSE;
|
|
|
285d04 |
+ PRBool checkFSType = PR_FALSE;
|
|
|
285d04 |
+ PRBool measureSpeed = PR_FALSE;
|
|
|
285d04 |
+ PRBool create;
|
|
|
285d04 |
+ int flags = inFlags & 0x7;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ *pSdb = NULL;
|
|
|
285d04 |
+ *inUpdate = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* sqlite3 doesn't have a flag to specify that we want to
|
|
|
285d04 |
+ * open the database read only. If the db doesn't exist,
|
|
|
285d04 |
+ * sqlite3 will always create it.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ LOCK_SQLITE();
|
|
|
285d04 |
+ create = (_NSSUTIL_Access(dbname, PR_ACCESS_EXISTS) != PR_SUCCESS);
|
|
|
285d04 |
+ if ((flags == SDB_RDONLY) && create) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, SQLITE_CANTOPEN);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sdb_openDB(dbname, &sqlDB, flags);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * SQL created the file, but it doesn't set appropriate modes for
|
|
|
285d04 |
+ * a database.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * NO NSPR call for chmod? :(
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ if (create && sdb_chmod(dbname, 0600) != 0) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, SQLITE_CANTOPEN);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (flags != SDB_RDONLY) {
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, BEGIN_CMD, NULL, 0, NULL);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ inTransaction = 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (!tableExists(sqlDB, table)) {
|
|
|
285d04 |
+ *newInit = 1;
|
|
|
285d04 |
+ if (flags != SDB_CREATE) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, SQLITE_CANTOPEN);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ initStr = sqlite3_mprintf("");
|
|
|
285d04 |
+ for (i = 0; initStr && i < known_attributes_size; i++) {
|
|
|
285d04 |
+ newStr = sqlite3_mprintf("%s, a%x", initStr, known_attributes[i]);
|
|
|
285d04 |
+ sqlite3_free(initStr);
|
|
|
285d04 |
+ initStr = newStr;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (initStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(INIT_CMD, table, initStr);
|
|
|
285d04 |
+ sqlite3_free(initStr);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_ISSUER_INDEX_CMD, table);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_SUBJECT_INDEX_CMD, table);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_LABEL_INDEX_CMD, table);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ newStr = sqlite3_mprintf(CREATE_ID_INDEX_CMD, table);
|
|
|
285d04 |
+ if (newStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, newStr, NULL, 0, NULL);
|
|
|
285d04 |
+ sqlite3_free(newStr);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(type, sqlerr);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * detect the case where we have created the database, but have
|
|
|
285d04 |
+ * not yet updated it.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * We only check the Key database because only the key database has
|
|
|
285d04 |
+ * a metaData table. The metaData table is created when a password
|
|
|
285d04 |
+ * is set, or in the case of update, when a password is supplied.
|
|
|
285d04 |
+ * If no key database exists, then the update would have happened immediately
|
|
|
285d04 |
+ * on noticing that the cert database didn't exist (see newInit set above).
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ if (type == SDB_KEY && !tableExists(sqlDB, "metaData")) {
|
|
|
285d04 |
+ *newInit = 1;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* access to network filesystems are significantly slower than local ones
|
|
|
285d04 |
+ * for database operations. In those cases we need to create a cached copy
|
|
|
285d04 |
+ * of the database in a temporary location on the local disk. SQLITE
|
|
|
285d04 |
+ * already provides a way to create a temporary table and initialize it,
|
|
|
285d04 |
+ * so we use it for the cache (see sdb_buildCache for how it's done).*/
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * we decide whether or not to use the cache based on the following input.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * NSS_SDB_USE_CACHE environment variable is set to anything other than
|
|
|
285d04 |
+ * "yes" or "no" (for instance, "auto"): NSS will measure the performance
|
|
|
285d04 |
+ * of access to the temp database versus the access to the user's
|
|
|
285d04 |
+ * passed-in database location. If the temp database location is
|
|
|
285d04 |
+ * "significantly" faster we will use the cache.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * NSS_SDB_USE_CACHE environment variable is nonexistent or set to "no":
|
|
|
285d04 |
+ * cache will not be used.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * NSS_SDB_USE_CACHE environment variable is set to "yes": cache will
|
|
|
285d04 |
+ * always be used.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * It is expected that most applications will not need this feature, and
|
|
|
285d04 |
+ * thus it is disabled by default.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+
|
|
|
285d04 |
+ env = PR_GetEnvSecure("NSS_SDB_USE_CACHE");
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Variables enableCache, checkFSType, measureSpeed are PR_FALSE by default,
|
|
|
285d04 |
+ * which is the expected behavior for NSS_SDB_USE_CACHE="no".
|
|
|
285d04 |
+ * We don't need to check for "no" here. */
|
|
|
285d04 |
+ if (!env) {
|
|
|
285d04 |
+ /* By default, with no variable set, we avoid expensive measuring for
|
|
|
285d04 |
+ * most FS types. We start with inexpensive FS type checking, and
|
|
|
285d04 |
+ * might perform measuring for some types. */
|
|
|
285d04 |
+ checkFSType = PR_TRUE;
|
|
|
285d04 |
+ } else if (PORT_Strcasecmp(env, "yes") == 0) {
|
|
|
285d04 |
+ enableCache = PR_TRUE;
|
|
|
285d04 |
+ } else if (PORT_Strcasecmp(env, "no") != 0) { /* not "no" => "auto" */
|
|
|
285d04 |
+ measureSpeed = PR_TRUE;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (checkFSType) {
|
|
|
285d04 |
+#if defined(LINUX) && !defined(ANDROID)
|
|
|
285d04 |
+ struct statfs statfs_s;
|
|
|
285d04 |
+ if (statfs(dbname, &statfs_s) == 0) {
|
|
|
285d04 |
+ switch (statfs_s.f_type) {
|
|
|
285d04 |
+ case SMB_SUPER_MAGIC:
|
|
|
285d04 |
+ case 0xff534d42: /* CIFS_MAGIC_NUMBER */
|
|
|
285d04 |
+ case NFS_SUPER_MAGIC:
|
|
|
285d04 |
+ /* We assume these are slow. */
|
|
|
285d04 |
+ enableCache = PR_TRUE;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ case CODA_SUPER_MAGIC:
|
|
|
285d04 |
+ case 0x65735546: /* FUSE_SUPER_MAGIC */
|
|
|
285d04 |
+ case NCP_SUPER_MAGIC:
|
|
|
285d04 |
+ /* It's uncertain if this FS is fast or slow.
|
|
|
285d04 |
+ * It seems reasonable to perform slow measuring for users
|
|
|
285d04 |
+ * with questionable FS speed. */
|
|
|
285d04 |
+ measureSpeed = PR_TRUE;
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ case AFS_SUPER_MAGIC: /* Already implements caching. */
|
|
|
285d04 |
+ default:
|
|
|
285d04 |
+ break;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (measureSpeed) {
|
|
|
285d04 |
+ char *tempDir = NULL;
|
|
|
285d04 |
+ PRUint32 tempOps = 0;
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * Use PR_Access to determine how expensive it
|
|
|
285d04 |
+ * is to check for the existance of a local file compared to the same
|
|
|
285d04 |
+ * check in the temp directory. If the temp directory is faster, cache
|
|
|
285d04 |
+ * the database there. */
|
|
|
285d04 |
+ tempDir = sdb_getTempDir(sqlDB);
|
|
|
285d04 |
+ if (tempDir) {
|
|
|
285d04 |
+ tempOps = sdb_measureAccess(tempDir);
|
|
|
285d04 |
+ PORT_Free(tempDir);
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* There is a cost to continually copying the database.
|
|
|
285d04 |
+ * Account for that cost with the arbitrary factor of 10 */
|
|
|
285d04 |
+ enableCache = (PRBool)(tempOps > accessOps * 10);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (enableCache) {
|
|
|
285d04 |
+ /* try to set the temp store to memory.*/
|
|
|
285d04 |
+ sqlite3_exec(sqlDB, "PRAGMA temp_store=MEMORY", NULL, 0, NULL);
|
|
|
285d04 |
+ /* Failure to set the temp store to memory is not fatal,
|
|
|
285d04 |
+ * ignore the error */
|
|
|
285d04 |
+
|
|
|
285d04 |
+ cacheTable = sqlite3_mprintf("%sCache", table);
|
|
|
285d04 |
+ if (cacheTable == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* build the cache table */
|
|
|
285d04 |
+ error = sdb_buildCache(sqlDB, type, cacheTable, table);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* initialize the last cache build time */
|
|
|
285d04 |
+ now = PR_IntervalNow();
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sdb = (SDB *)malloc(sizeof(SDB));
|
|
|
285d04 |
+ if (!sdb) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sdb_p = (SDBPrivate *)malloc(sizeof(SDBPrivate));
|
|
|
285d04 |
+ if (!sdb_p) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Cache the attributes that are held in the table, so we can later check
|
|
|
285d04 |
+ * that queried attributes actually exist. We don't assume the schema
|
|
|
285d04 |
+ * to be exactly |known_attributes|, as it may change over time. */
|
|
|
285d04 |
+ sdb_p->schemaAttrs = NULL;
|
|
|
285d04 |
+ if (!PORT_Strcmp("nssPublic", table) ||
|
|
|
285d04 |
+ !PORT_Strcmp("nssPrivate", table)) {
|
|
|
285d04 |
+ sqlite3_stmt *stmt = NULL;
|
|
|
285d04 |
+ int retry = 0;
|
|
|
285d04 |
+ unsigned int backedAttrs = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Can't bind parameters to a PRAGMA. */
|
|
|
285d04 |
+ queryStr = sqlite3_mprintf("PRAGMA table_info(%s);", table);
|
|
|
285d04 |
+ if (queryStr == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_prepare_v2(sqlDB, queryStr, -1, &stmt, NULL);
|
|
|
285d04 |
+ sqlite3_free(queryStr);
|
|
|
285d04 |
+ queryStr = NULL;
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ unsigned int schemaAttrsCapacity = known_attributes_size;
|
|
|
285d04 |
+ sdb_p->schemaAttrs = malloc(schemaAttrsCapacity * sizeof(CK_ATTRIBUTE_TYPE));
|
|
|
285d04 |
+ if (!sdb_p->schemaAttrs) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ do {
|
|
|
285d04 |
+ sqlerr = sqlite3_step(stmt);
|
|
|
285d04 |
+ if (sqlerr == SQLITE_BUSY) {
|
|
|
285d04 |
+ PR_Sleep(SDB_BUSY_RETRY_TIME);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlerr == SQLITE_ROW) {
|
|
|
285d04 |
+ if (backedAttrs == schemaAttrsCapacity) {
|
|
|
285d04 |
+ schemaAttrsCapacity += known_attributes_size;
|
|
|
285d04 |
+ sdb_p->schemaAttrs = realloc(sdb_p->schemaAttrs,
|
|
|
285d04 |
+ schemaAttrsCapacity * sizeof(CK_ATTRIBUTE_TYPE));
|
|
|
285d04 |
+ if (!sdb_p->schemaAttrs) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ /* Record the ULONG attribute value. */
|
|
|
285d04 |
+ char *val = (char *)sqlite3_column_text(stmt, 1);
|
|
|
285d04 |
+ if (val && val[0] == 'a') {
|
|
|
285d04 |
+ CK_ATTRIBUTE_TYPE attr = strtoul(&val[1], NULL, 16);
|
|
|
285d04 |
+ sdb_p->schemaAttrs[backedAttrs++] = attr;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ } while (!sdb_done(sqlerr, &retry));
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (sqlerr != SQLITE_DONE) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_reset(stmt);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ sqlerr = sqlite3_finalize(stmt);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sdb_p->numSchemaAttrs = backedAttrs;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* Sort these once so we can shortcut invalid attribute searches. */
|
|
|
285d04 |
+ qsort(sdb_p->schemaAttrs, sdb_p->numSchemaAttrs,
|
|
|
285d04 |
+ sizeof(CK_ATTRIBUTE_TYPE), sdb_attributeComparator);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* invariant fields */
|
|
|
285d04 |
+ sdb_p->sqlDBName = PORT_Strdup(dbname);
|
|
|
285d04 |
+ sdb_p->type = type;
|
|
|
285d04 |
+ sdb_p->table = table;
|
|
|
285d04 |
+ sdb_p->cacheTable = cacheTable;
|
|
|
285d04 |
+ sdb_p->lastUpdateTime = now;
|
|
|
285d04 |
+ /* set the cache delay time. This is how long we will wait before we
|
|
|
285d04 |
+ * decide the existing cache is stale. Currently set to 10 sec */
|
|
|
285d04 |
+ sdb_p->updateInterval = PR_SecondsToInterval(10);
|
|
|
285d04 |
+ sdb_p->dbMon = PR_NewMonitor();
|
|
|
285d04 |
+ /* these fields are protected by the lock */
|
|
|
285d04 |
+ sdb_p->sqlXactDB = NULL;
|
|
|
285d04 |
+ sdb_p->sqlXactThread = NULL;
|
|
|
285d04 |
+ sdb->private = sdb_p;
|
|
|
285d04 |
+ sdb->version = 1;
|
|
|
285d04 |
+ sdb->sdb_flags = inFlags | SDB_HAS_META;
|
|
|
285d04 |
+ sdb->app_private = NULL;
|
|
|
285d04 |
+ sdb->sdb_FindObjectsInit = sdb_FindObjectsInit;
|
|
|
285d04 |
+ sdb->sdb_FindObjects = sdb_FindObjects;
|
|
|
285d04 |
+ sdb->sdb_FindObjectsFinal = sdb_FindObjectsFinal;
|
|
|
285d04 |
+ sdb->sdb_GetAttributeValue = sdb_GetAttributeValue;
|
|
|
285d04 |
+ sdb->sdb_SetAttributeValue = sdb_SetAttributeValue;
|
|
|
285d04 |
+ sdb->sdb_CreateObject = sdb_CreateObject;
|
|
|
285d04 |
+ sdb->sdb_DestroyObject = sdb_DestroyObject;
|
|
|
285d04 |
+ sdb->sdb_GetMetaData = sdb_GetMetaData;
|
|
|
285d04 |
+ sdb->sdb_PutMetaData = sdb_PutMetaData;
|
|
|
285d04 |
+ sdb->sdb_DestroyMetaData = sdb_DestroyMetaData;
|
|
|
285d04 |
+ sdb->sdb_Begin = sdb_Begin;
|
|
|
285d04 |
+ sdb->sdb_Commit = sdb_Commit;
|
|
|
285d04 |
+ sdb->sdb_Abort = sdb_Abort;
|
|
|
285d04 |
+ sdb->sdb_Reset = sdb_Reset;
|
|
|
285d04 |
+ sdb->sdb_Close = sdb_Close;
|
|
|
285d04 |
+ sdb->sdb_SetForkState = sdb_SetForkState;
|
|
|
285d04 |
+ sdb->sdb_GetNewObjectID = sdb_GetNewObjectID;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (inTransaction) {
|
|
|
285d04 |
+ sqlerr = sqlite3_exec(sqlDB, COMMIT_CMD, NULL, 0, NULL);
|
|
|
285d04 |
+ if (sqlerr != SQLITE_OK) {
|
|
|
285d04 |
+ error = sdb_mapSQLError(sdb_p->type, sqlerr);
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ inTransaction = 0;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ sdb_p->sqlReadDB = sqlDB;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ *pSdb = sdb;
|
|
|
285d04 |
+ UNLOCK_SQLITE();
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ /* lots of stuff to do */
|
|
|
285d04 |
+ if (inTransaction) {
|
|
|
285d04 |
+ sqlite3_exec(sqlDB, ROLLBACK_CMD, NULL, 0, NULL);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sdb) {
|
|
|
285d04 |
+ free(sdb);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sdb_p) {
|
|
|
285d04 |
+ if (sdb_p->schemaAttrs) {
|
|
|
285d04 |
+ free(sdb_p->schemaAttrs);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ free(sdb_p);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (sqlDB) {
|
|
|
285d04 |
+ sqlite3_close(sqlDB);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ UNLOCK_SQLITE();
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+/* sdbopen */
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+s_open(const char *directory, const char *certPrefix, const char *keyPrefix,
|
|
|
285d04 |
+ int cert_version, int key_version, int flags,
|
|
|
285d04 |
+ SDB **certdb, SDB **keydb, int *newInit)
|
|
|
285d04 |
+{
|
|
|
285d04 |
+ char *cert = sdb_BuildFileName(directory, certPrefix,
|
|
|
285d04 |
+ "cert", cert_version);
|
|
|
285d04 |
+ char *key = sdb_BuildFileName(directory, keyPrefix,
|
|
|
285d04 |
+ "key", key_version);
|
|
|
285d04 |
+ CK_RV error = CKR_OK;
|
|
|
285d04 |
+ int inUpdate;
|
|
|
285d04 |
+ PRUint32 accessOps;
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (certdb)
|
|
|
285d04 |
+ *certdb = NULL;
|
|
|
285d04 |
+ if (keydb)
|
|
|
285d04 |
+ *keydb = NULL;
|
|
|
285d04 |
+ *newInit = 0;
|
|
|
285d04 |
+
|
|
|
285d04 |
+#ifdef SQLITE_UNSAFE_THREADS
|
|
|
285d04 |
+ if (sqlite_lock == NULL) {
|
|
|
285d04 |
+ sqlite_lock = PR_NewLock();
|
|
|
285d04 |
+ if (sqlite_lock == NULL) {
|
|
|
285d04 |
+ error = CKR_HOST_MEMORY;
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /* how long does it take to test for a non-existant file in our working
|
|
|
285d04 |
+ * directory? Allows us to test if we may be on a network file system */
|
|
|
285d04 |
+ accessOps = 1;
|
|
|
285d04 |
+ {
|
|
|
285d04 |
+ char *env;
|
|
|
285d04 |
+ env = PR_GetEnvSecure("NSS_SDB_USE_CACHE");
|
|
|
285d04 |
+ /* If the environment variable is undefined or set to yes or no,
|
|
|
285d04 |
+ * sdb_init() will ignore the value of accessOps, and we can skip the
|
|
|
285d04 |
+ * measuring.*/
|
|
|
285d04 |
+ if (env && PORT_Strcasecmp(env, "no") != 0 &&
|
|
|
285d04 |
+ PORT_Strcasecmp(env, "yes") != 0) {
|
|
|
285d04 |
+ accessOps = sdb_measureAccess(directory);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * open the cert data base
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ if (certdb) {
|
|
|
285d04 |
+ /* initialize Certificate database */
|
|
|
285d04 |
+ error = sdb_init(cert, "nssPublic", SDB_CERT, &inUpdate,
|
|
|
285d04 |
+ newInit, flags, accessOps, certdb);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ /*
|
|
|
285d04 |
+ * open the key data base:
|
|
|
285d04 |
+ * NOTE:if we want to implement a single database, we open
|
|
|
285d04 |
+ * the same database file as the certificate here.
|
|
|
285d04 |
+ *
|
|
|
285d04 |
+ * cert an key db's have different tables, so they will not
|
|
|
285d04 |
+ * conflict.
|
|
|
285d04 |
+ */
|
|
|
285d04 |
+ if (keydb) {
|
|
|
285d04 |
+ /* initialize the Key database */
|
|
|
285d04 |
+ error = sdb_init(key, "nssPrivate", SDB_KEY, &inUpdate,
|
|
|
285d04 |
+ newInit, flags, accessOps, keydb);
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ goto loser;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+loser:
|
|
|
285d04 |
+ if (cert) {
|
|
|
285d04 |
+ sqlite3_free(cert);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (key) {
|
|
|
285d04 |
+ sqlite3_free(key);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ if (error != CKR_OK) {
|
|
|
285d04 |
+ /* currently redundant, but could be necessary if more code is added
|
|
|
285d04 |
+ * just before loser */
|
|
|
285d04 |
+ if (keydb && *keydb) {
|
|
|
285d04 |
+ sdb_Close(*keydb);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ if (certdb && *certdb) {
|
|
|
285d04 |
+ sdb_Close(*certdb);
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+
|
|
|
285d04 |
+ return error;
|
|
|
285d04 |
+}
|
|
|
285d04 |
+
|
|
|
285d04 |
+CK_RV
|
|
|
285d04 |
+s_shutdown()
|
|
|
285d04 |
+{
|
|
|
285d04 |
+#ifdef SQLITE_UNSAFE_THREADS
|
|
|
285d04 |
+ if (sqlite_lock) {
|
|
|
285d04 |
+ PR_DestroyLock(sqlite_lock);
|
|
|
285d04 |
+ sqlite_lock = NULL;
|
|
|
285d04 |
+ }
|
|
|
285d04 |
+#endif
|
|
|
285d04 |
+ return CKR_OK;
|
|
|
285d04 |
+}
|
|
|
285d04 |
diff --git a/cmd/manifest.mn b/cmd/manifest.mn
|
|
|
285d04 |
--- a/cmd/manifest.mn
|
|
|
285d04 |
+++ b/cmd/manifest.mn
|
|
|
285d04 |
@@ -36,16 +36,17 @@ NSS_SRCDIRS = \
|
|
|
285d04 |
addbuiltin \
|
|
|
285d04 |
atob \
|
|
|
285d04 |
btoa \
|
|
|
285d04 |
certutil \
|
|
|
285d04 |
chktest \
|
|
|
285d04 |
crlutil \
|
|
|
285d04 |
crmftest \
|
|
|
285d04 |
dbtest \
|
|
|
285d04 |
+ dbtool \
|
|
|
285d04 |
derdump \
|
|
|
285d04 |
digest \
|
|
|
285d04 |
httpserv \
|
|
|
285d04 |
listsuites \
|
|
|
285d04 |
makepqg \
|
|
|
285d04 |
multinit \
|
|
|
285d04 |
nss-policy-check \
|
|
|
285d04 |
ocspclnt \
|