def setDefaultKeyOfIdentity(self, identityName, keyName): """ Set the key with keyName as the default key for the identity with name identityName. :param Name identityName: The name of the identity. This copies the name. :param Name keyName: The name of the key. This copies the name. :raises Pib.Error: If the key does not exist. """ if not self.hasKey(keyName): raise Pib.Error("Key `" + keyName.toUri() + "` does not exist") try: cursor = self._database.cursor() cursor.execute( "UPDATE keys SET is_default=1 WHERE key_name=?", (sqlite3.Binary(bytearray(keyName.wireEncode().buf())), )) self._database.commit() cursor.close() except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))
def hasKey(self, keyName): """ Check for the existence of a key with keyName. :param Name keyName: The name of the key. :return: True if the key exists, otherwise False. Return False if the identity does not exist. :rtype: bool """ try: cursor = self._database.cursor() cursor.execute( "SELECT id FROM keys WHERE key_name=?", (sqlite3.Binary(bytearray(keyName.wireEncode().buf())), )) row = cursor.fetchone() result = (row != None) cursor.close() return result except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))
def hasCertificate(self, certificateName): """ Check for the existence of a certificate with name certificateName. :param Name certificateName: The name of the certificate. :return: True if the certificate exists, otherwise False. :rtype: bool """ try: cursor = self._database.cursor() cursor.execute( "SELECT id FROM certificates WHERE certificate_name=?", (sqlite3.Binary(bytearray( certificateName.wireEncode().buf())), )) row = cursor.fetchone() result = (row != None) cursor.close() return result except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))
def _hasDefaultCertificateOfKey(self, keyName): """ Check if there is a default certificate for the key with keyName. :param Name keyName: The key Name. :return: True if there is a default certificate. :rtype: bool """ try: cursor = self._database.cursor() cursor.execute( "SELECT certificate_data " + "FROM certificates JOIN keys ON certificates.key_id=keys.id " + "WHERE certificates.is_default=1 AND keys.key_name=?", (sqlite3.Binary(bytearray(keyName.wireEncode().buf())), )) row = cursor.fetchone() result = (row != None) cursor.close() return result except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))
def _hasDefaultKeyOfIdentity(self, identityName): """ Check if there is a default key for the identity with identityName. :param Name identityName: The identity Name. :return: True if there is a default key. :rtype: bool """ try: cursor = self._database.cursor() cursor.execute( "SELECT key_name " + "FROM keys JOIN identities ON keys.identity_id=identities.id " + "WHERE identities.identity=? AND keys.is_default=1", (sqlite3.Binary(bytearray(identityName.wireEncode().buf())), )) row = cursor.fetchone() result = (row != None) cursor.close() return result except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))
def addIdentity(self, identityName): """ Add the identity. If the identity already exists, do nothing. If no default identity has been set, set the added identity as the default. :param Name identityName: The name of the identity to add. This copies the name. """ if not self.hasIdentity(identityName): try: cursor = self._database.cursor() cursor.execute( "INSERT INTO identities (identity) values (?)", (sqlite3.Binary(bytearray( identityName.wireEncode().buf())), )) self._database.commit() cursor.close() except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex)) if not self._hasDefaultIdentity(): self.setDefaultIdentity(identityName)
def getIdentities(self): """ Get the names of all the identities. :return: The a fresh set of identity names. The Name objects are fresh copies. :rtype: set of Name """ identities = set() try: cursor = self._database.cursor() cursor.execute("SELECT identity FROM identities") rows = cursor.fetchall() for (encoding, ) in rows: name = Name() name.wireDecode(bytearray(encoding)) identities.add(name) cursor.close() except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex)) return identities
def getDefaultKeyOfIdentity(self, identityName): """ Get the name of the default key for the identity with name identityName. :param Name identityName: The name of the identity. :return: The name of the default key, as a fresh copy. :rtype: Name :raises Pib.Error: If there is no default key or if the identity does not exist. """ if not self.hasIdentity(identityName): raise Pib.Error("Identity `" + identityName.toUri() + "` does not exist") encoding = None try: cursor = self._database.cursor() cursor.execute( "SELECT key_name " + "FROM keys JOIN identities ON keys.identity_id=identities.id " + "WHERE identities.identity=? AND keys.is_default=1", (sqlite3.Binary(bytearray(identityName.wireEncode().buf())), )) row = cursor.fetchone() if row != None: (encoding, ) = row cursor.close() except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex)) if encoding != None: name = Name() name.wireDecode(bytearray(encoding)) return name else: raise Pib.Error("No default key for identity `" + identityName.toUri() + "`")
def setTpmLocator(self, tpmLocator): """ Set the corresponding TPM information to tpmLocator. This method does not reset the contents of the PIB. :param str tpmLocator: The TPM locator string. """ try: if self.getTpmLocator() == "": # The tpmLocator does not exist. Insert it directly. cursor = self._database.cursor() cursor.execute("INSERT INTO tpmInfo (tpm_locator) values (?)", (tpmLocator, )) self._database.commit() cursor.close() else: # Update the existing tpmLocator. cursor = self._database.cursor() cursor.execute("UPDATE tpmInfo SET tpm_locator=?", (tpmLocator, )) self._database.commit() cursor.close() except Exception as ex: raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))