Beispiel #1
0
bool
Db::insertLeafData(const Leaf& leaf, const Data& data)
{
  if (leaf.getDataSeqNo() != m_nextLeafSeqNo)
    return false;

  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_db,
                     "INSERT INTO leaves (dataSeqNo, dataName, signerSeqNo, timestamp, isCert, cert)\
                      VALUES (?, ?, ?, ?, 1, ?)",
                     -1, &statement, nullptr);

  sqlite3_bind_int(statement, 1, leaf.getDataSeqNo());
  sqlite3_bind_block(statement, 2, leaf.getDataName().wireEncode(), SQLITE_TRANSIENT);
  sqlite3_bind_int(statement, 3, leaf.getSignerSeqNo());
  sqlite3_bind_int(statement, 4, leaf.getTimestamp());
  sqlite3_bind_block(statement, 5, data.wireEncode(), SQLITE_TRANSIENT);

  int result = sqlite3_step(statement);
  sqlite3_finalize(statement);

  if (result == SQLITE_OK || result == SQLITE_DONE) {
    m_nextLeafSeqNo++;
    return true;
  }

  return false;
}
Beispiel #2
0
void
PibDb::deleteCertificate(const Name& certificateName)
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "DELETE FROM certificates WHERE certificate_name=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, certificateName.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_step(statement);
  sqlite3_finalize(statement);
}
Beispiel #3
0
void
PibDb::setDefaultCertNameOfKey(const Name& certificateName)
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "UPDATE certificates SET is_default=1 WHERE certificate_name=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, certificateName.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_step(statement);
  sqlite3_finalize(statement);
}
Beispiel #4
0
void
PibDb::setDefaultKeyNameOfIdentity(const Name& keyName)
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "UPDATE keys SET is_default=1 WHERE key_name=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, keyName.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_step(statement);
  sqlite3_finalize(statement);
}
Beispiel #5
0
void
PibDb::deleteKey(const Name& keyName)
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "DELETE FROM keys WHERE key_name=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, keyName.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_step(statement);
  sqlite3_finalize(statement);
}
Beispiel #6
0
void
PibDb::setDefaultIdentity(const Name& identity)
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "UPDATE identities SET is_default=1 WHERE identity=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, identity.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_step(statement);
  sqlite3_finalize(statement);
}
Beispiel #7
0
void
PibDb::deleteIdentity(const Name& identity)
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "DELETE FROM identities WHERE identity=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, identity.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_step(statement);
  sqlite3_finalize(statement);
}
Beispiel #8
0
int64_t
PibDb::addIdentity(const Name& identity)
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "INSERT INTO identities (identity) values (?)",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, identity.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_step(statement);
  sqlite3_finalize(statement);

  return sqlite3_last_insert_rowid(m_database);
}
Beispiel #9
0
int64_t
PibDb::addCertificate(const IdentityCertificate& certificate)
{
  const Name& certName = certificate.getName();
  const Name& keyName = certificate.getPublicKeyName();

  if (!hasKey(keyName))
    addKey(keyName, certificate.getPublicKeyInfo());

  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "INSERT INTO certificates \
                      (key_id, certificate_name, certificate_data) \
                      values ((SELECT id FROM keys WHERE key_name=?), ?, ?)",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, keyName.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_bind_block(statement, 2, certName.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_bind_block(statement, 3, certificate.wireEncode(), SQLITE_STATIC);
  sqlite3_step(statement);
  sqlite3_finalize(statement);

  return sqlite3_last_insert_rowid(m_database);
}
Beispiel #10
0
int64_t
PibDb::addKey(const Name& keyName, const PublicKey& key)
{
  if (keyName.empty())
    return 0;

  Name&& identity = keyName.getPrefix(-1);
  if (!hasIdentity(identity))
    addIdentity(identity);

  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "INSERT INTO keys (identity_id, key_name, key_type, key_bits) \
                      values ((SELECT id FROM identities WHERE identity=?), ?, ?, ?)",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, identity.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_bind_block(statement, 2, keyName.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_bind_int(statement, 3, key.getKeyType());
  sqlite3_bind_blob(statement, 4, key.get().buf(), key.get().size(), SQLITE_STATIC);
  sqlite3_step(statement);
  sqlite3_finalize(statement);

  return sqlite3_last_insert_rowid(m_database);
}
Beispiel #11
0
bool
PibDb::hasIdentity(const Name& identity) const
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "SELECT id FROM identities WHERE identity=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, identity.wireEncode(), SQLITE_TRANSIENT);
  int result = sqlite3_step(statement);
  sqlite3_finalize(statement);

  if (result == SQLITE_ROW)
    return true;
  else
    return false;
}
Beispiel #12
0
bool
PibDb::hasCertificate(const Name& certificateName) const
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "SELECT id FROM certificates WHERE certificate_name=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, certificateName.wireEncode(), SQLITE_TRANSIENT);
  int result = sqlite3_step(statement);
  sqlite3_finalize(statement);

  if (result == SQLITE_ROW)
    return true;
  else
    return false;
}
Beispiel #13
0
shared_ptr<PublicKey>
PibDb::getKey(const Name& keyName) const
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "SELECT key_bits FROM keys WHERE key_name=?"
                     , -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, keyName.wireEncode(), SQLITE_TRANSIENT);

  shared_ptr<PublicKey> key;
  if (sqlite3_step(statement) == SQLITE_ROW) {
      key = make_shared<PublicKey>(static_cast<const uint8_t*>(sqlite3_column_blob(statement, 0)),
                                   sqlite3_column_bytes(statement, 0));
  }
  sqlite3_finalize(statement);
  return key;
}
Beispiel #14
0
shared_ptr<IdentityCertificate>
PibDb::getCertificate(const Name& certificateName) const
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "SELECT certificate_data FROM certificates WHERE certificate_name=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, certificateName.wireEncode(), SQLITE_TRANSIENT);

  shared_ptr<IdentityCertificate> certificate;
  if (sqlite3_step(statement) == SQLITE_ROW) {
    certificate = make_shared<IdentityCertificate>();
    certificate->wireDecode(sqlite3_column_block(statement, 0));
  }

  sqlite3_finalize(statement);
  return certificate;
}
Beispiel #15
0
Name
PibDb::getDefaultKeyNameOfIdentity(const Name& identity) const
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "SELECT key_name FROM keys JOIN identities ON keys.identity_id=identities.id\
                      WHERE identities.identity=? AND keys.is_default=1",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, identity.wireEncode(), SQLITE_TRANSIENT);

  Name keyName = NON_EXISTING_KEY;
  if (sqlite3_step(statement) == SQLITE_ROW && sqlite3_column_bytes(statement, 0) != 0) {
    keyName = Name(sqlite3_column_block(statement, 0));
  }

  sqlite3_finalize(statement);
  return keyName;
}
Beispiel #16
0
Name
PibDb::getDefaultCertNameOfKey(const Name& keyName) const
{
  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "SELECT certificate_name\
                      FROM certificates JOIN keys ON certificates.key_id=keys.id\
                      WHERE keys.key_name=? AND certificates.is_default=1",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, keyName.wireEncode(), SQLITE_TRANSIENT);

  Name certName = NON_EXISTING_CERTIFICATE;
  if (sqlite3_step(statement) == SQLITE_ROW && sqlite3_column_bytes(statement, 0) != 0) {
    certName = Name(sqlite3_column_block(statement, 0));
  }
  sqlite3_finalize(statement);
  return certName;
}
Beispiel #17
0
vector<Name>
PibDb::listKeyNamesOfIdentity(const Name& identity) const
{
  vector<Name> keyNames;

  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "SELECT key_name FROM keys JOIN identities ON keys.identity_id=identities.id\
                      WHERE identities.identity=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, identity.wireEncode(), SQLITE_TRANSIENT);

  keyNames.clear();
  while (sqlite3_step(statement) == SQLITE_ROW) {
    Name keyName(sqlite3_column_block(statement, 0));
    keyNames.push_back(keyName);
  }

  sqlite3_finalize(statement);
  return keyNames;
}
Beispiel #18
0
vector<Name>
PibDb::listCertNamesOfKey(const Name& keyName) const
{
  vector<Name> certNames;

  sqlite3_stmt* statement;
  sqlite3_prepare_v2(m_database,
                     "SELECT certificate_name\
                      FROM certificates JOIN keys ON certificates.key_id=keys.id\
                      WHERE keys.key_name=?",
                     -1, &statement, nullptr);
  sqlite3_bind_block(statement, 1, keyName.wireEncode(), SQLITE_TRANSIENT);

  certNames.clear();
  while (sqlite3_step(statement) == SQLITE_ROW) {
    Name name(sqlite3_column_block(statement, 0));
    certNames.push_back(name);
  }
  sqlite3_finalize(statement);

  return certNames;
}
Beispiel #19
0
void
PibDb::updateMgmtCertificate(const IdentityCertificate& certificate)
{
  const Name& keyName = certificate.getPublicKeyName();

  // Name of mgmt key should be "/localhost/pib/[UserName]/mgmt/[KeyID]"
  if (keyName.size() != 5 ||
      keyName.compare(0, 2, LOCALHOST_PIB) ||
      keyName.get(3) != MGMT_LABEL)
    throw Error("PibDb::updateMgmtCertificate: certificate does not follow the naming convention");

  string owner = keyName.get(2).toUri();
  sqlite3_stmt* statement;
  if (!m_owner.empty()) {
    if (m_owner != owner)
      throw Error("PibDb::updateMgmtCertificate: owner name does not match");
    else {
      sqlite3_prepare_v2(m_database,
                         "UPDATE mgmt SET local_management_cert=? WHERE owner=?",
                         -1, &statement, nullptr);
    }
  }
  else {
    sqlite3_prepare_v2(m_database,
                       "INSERT INTO mgmt (local_management_cert, owner) VALUES (?, ?)",
                       -1, &statement, nullptr);
  }

  sqlite3_bind_block(statement, 1, certificate.wireEncode(), SQLITE_TRANSIENT);
  sqlite3_bind_string(statement, 2, owner, SQLITE_TRANSIENT);
  sqlite3_step(statement);
  sqlite3_finalize(statement);

  m_owner = owner;

  mgmtCertificateChanged();
}