Exemplo n.º 1
0
    def setDefaultCertificateOfKey(self, keyName, certificateName):
        """
        Set the cert with name certificateName as the default for the key with
        keyName.

        :param Name keyName: The name of the key.
        :param Name certificateName: The name of the certificate. This copies
          the name.
        :raises Pib.Error: If the certificate with name certificateName does not
          exist.
        """
        if not self.hasCertificate(certificateName):
            raise Pib.Error("Certificate `" + certificateName.toUri() +
                            "` does not exist")

        try:
            cursor = self._database.cursor()
            cursor.execute(
                "UPDATE certificates SET is_default=1 WHERE certificate_name=?",
                (sqlite3.Binary(bytearray(
                    certificateName.wireEncode().buf())), ))
            self._database.commit()
            cursor.close()
        except Exception as ex:
            raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))
Exemplo n.º 2
0
    def getDefaultCertificateOfKey(self, keyName):
        """
        Get the default certificate for the key with eyName.

        :param Name keyName: The name of the key.
        :return: A copy of the default certificate.
        :rtype: CertificateV2
        :raises Pib.Error: If the default certificate does not exist.
        """
        encoding = None
        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()

            if row != None:
                (encoding, ) = row
            cursor.close()
        except Exception as ex:
            raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))

        if encoding != None:
            certificate = CertificateV2()
            certificate.wireDecode(bytearray(encoding))
            return certificate
        else:
            raise Pib.Error("No default certificate for key `" +
                            keyName.toUri() + "`")
Exemplo n.º 3
0
    def getCertificate(self, certificateName):
        """
        Get the certificate with name certificateName.

        :param Name certificateName: The name of the certificate.
        :return: A copy of the certificate.
        :rtype: CertificateV2
        :raises Pib.Error: If the certificate does not exist.
        """
        encoding = None
        try:
            cursor = self._database.cursor()
            cursor.execute(
                "SELECT certificate_data FROM certificates WHERE certificate_name=?",
                (sqlite3.Binary(bytearray(
                    certificateName.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:
            certificate = CertificateV2()
            certificate.wireDecode(bytearray(encoding))
            return certificate
        else:
            raise Pib.Error("Certificate `" + certificateName.toUri() +
                            "` does not exit")
Exemplo n.º 4
0
    def getKeyBits(self, keyName):
        """
        Get the key bits of a key with name keyName.

        :param Name keyName: The name of the key.
        :return: The key bits.
        :rtype: Blob
        :raises Pib.Error: If the key does not exist.
        """
        key = None
        try:
            cursor = self._database.cursor()
            cursor.execute(
                "SELECT key_bits FROM keys WHERE key_name=?",
                (sqlite3.Binary(bytearray(keyName.wireEncode().buf())), ))
            row = cursor.fetchone()

            if row != None:
                (key, ) = row
            cursor.close()
        except Exception as ex:
            raise PibImpl.Error("PibSqlite3: SQLite error: " + str(ex))

        if key != None:
            return Blob(bytearray(key), False)
        else:
            raise Pib.Error("Key `" + keyName.toUri() + "` does not exist")
Exemplo n.º 5
0
    def getDefaultIdentity(self):
        """
        Get the default identity.

        :return: The name of the default identity, as a fresh copy.
        :rtype: Name
        :raises Pib.Error: For no default identity.
        """
        encoding = None
        try:
            cursor = self._database.cursor()
            cursor.execute(
                "SELECT identity FROM identities WHERE is_default=1")
            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 identity")
Exemplo n.º 6
0
    def getDefaultIdentity(self):
        """
        Get the default identity.

        :return: The name of the default identity, as a fresh copy.
        :rtype: Name
        :raises Pib.Error: For no default identity.
        """
        if self._defaultIdentityName != None:
            # Copy the name.
            return Name(self._defaultIdentityName)

        raise Pib.Error("No default identity")
Exemplo n.º 7
0
    def getCertificate(self, certificateName):
        """
        Get the certificate with name certificateName.

        :param Name certificateName: The name of the certificate.
        :return: A copy of the certificate.
        :rtype: CertificateV2
        :raises Pib.Error: If the certificate does not exist.
        """
        if not self.hasCertificate(certificateName):
            raise Pib.Error(
              "Certificate `" + certificateName.toUri() +  "` does not exist")

        return CertificateV2(self._certificates[certificateName])
Exemplo n.º 8
0
    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() + "` not found")

        # Copy the Names.
        self._defaultKeyNames[Name(identityName)] = Name(keyName)
Exemplo n.º 9
0
    def getKeyBits(self, keyName):
        """
        Get the key bits of a key with name keyName.

        :param Name keyName: The name of the key.
        :return: The key bits.
        :rtype: Blob
        :raises Pib.Error: If the key does not exist.
        """
        if not self.hasKey(keyName):
            raise Pib.Error("Key `" + keyName.toUri() + "` not found")

        key = self._keys[keyName]
        return key
Exemplo n.º 10
0
    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() + "`")
Exemplo n.º 11
0
    def getDefaultCertificateOfKey(self, keyName):
        """
        Get the default certificate for the key with eyName.

        :param Name keyName: The name of the key.
        :return: A copy of the default certificate.
        :rtype: CertificateV2
        :raises Pib.Error: If the default certificate does not exist.
        """
        try:
            certificateName = self._defaultCertificateNames[keyName]
        except KeyError:
            raise Pib.Error(
              "No default certificate for key `" + keyName.toUri() + "`")

        certificate = self._certificates[certificateName]
        return CertificateV2(certificate)
Exemplo n.º 12
0
    def setDefaultCertificateOfKey(self, keyName, certificateName):
        """
        Set the cert with name certificateName as the default for the key with
        keyName.

        :param Name keyName: The name of the key.
        :param Name certificateName: The name of the certificate. This copies
          the name.
        :raises Pib.Error: If the certificate with name certificateName does not
          exist.
        """
        if not self.hasCertificate(certificateName):
          raise Pib.Error(
            "Certificate `" + certificateName.toUri() +  "` does not exist")

        # Copy the Names.
        self._defaultCertificateNames[Name(keyName)] = Name(certificateName)
Exemplo n.º 13
0
    def __init__(self, identityName, pibImpl, needInit):
        self._defaultKey = None

        # Copy the Name.
        self._identityName = Name(identityName)
        self._keys = PibKeyContainer(identityName, pibImpl)
        self._pibImpl = pibImpl

        if pibImpl == None:
            raise ValueError("The pibImpl is None")

        if needInit:
            self._pibImpl.addIdentity(self._identityName)
        else:
            if not self._pibImpl.hasIdentity(self._identityName):
                raise Pib.Error("Identity " + self._identityName.toUri() +
                                " does not exist")
Exemplo n.º 14
0
    def __init__(self, keyName, arg2, arg3=None):
        self._defaultCertificate = None

        if isinstance(arg2, PibImpl):
            # PibKeyImpl(keyName, pibImpl)
            pibImpl = arg2

            self._identityName = PibKey.extractIdentityFromKeyName(keyName)
            self._keyName = Name(keyName)
            self._certificates = PibCertificateContainer(keyName, pibImpl)
            self._pibImpl = pibImpl

            if pibImpl == None:
                raise ValueError("The pibImpl is None")

            self._keyEncoding = self._pibImpl.getKeyBits(self._keyName)

            try:
                publicKey = PublicKey(self._keyEncoding)
            except:
                # We don't expect this since we just fetched the encoding.
                raise Pib.Error("Error decoding public key")

            self._keyType = publicKey.getKeyType()
        else:
            # PibKeyImpl(keyName, keyEncoding, pibImpl)
            keyEncoding = arg2
            pibImpl = arg3

            self._identityName = PibKey.extractIdentityFromKeyName(keyName)
            self._keyName = Name(keyName)
            self._keyEncoding = Blob(keyEncoding, True)
            self._certificates = PibCertificateContainer(keyName, pibImpl)
            self._pibImpl = pibImpl

            if pibImpl == None:
                raise ValueError("The pibImpl is None")

            try:
                publicKey = PublicKey(self._keyEncoding)
                self._keyType = publicKey.getKeyType()
            except:
                raise ValueError("Invalid key encoding")

            self._pibImpl.addKey(self._identityName, self._keyName,
                                 keyEncoding)
Exemplo n.º 15
0
    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.
        """
        try:
            defaultKey = self._defaultKeyNames[identityName]
        except KeyError:
            raise Pib.Error(
              "No default key for identity `" + identityName.toUri() + "`")

        # Copy the name.
        return Name(defaultKey)
Exemplo n.º 16
0
    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))