certificate_db.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #ifndef SQUID_SSL_CERTIFICATE_DB_H
10 #define SQUID_SSL_CERTIFICATE_DB_H
11 
12 #include "base/Here.h"
13 #include "ssl/gadgets.h"
14 
15 #include <string>
16 
17 namespace Ssl
18 {
20 class Lock
21 {
22 public:
23  explicit Lock(std::string const &filename);
24  ~Lock();
25  void lock();
26  void unlock();
27  bool locked() const;
28  const char *name() const { return filename.c_str(); }
29 private:
30  std::string filename;
31 #if _SQUID_WINDOWS_
32  HANDLE hFile;
33 #else
34  int fd;
35 #endif
36 };
37 
39 class Locker
40 {
41 public:
43  Locker(Lock &, const SourceLocation &);
45  ~Locker();
46 private:
47  bool weLocked;
49 
52 };
53 
64 {
65 public:
67  enum Columns {
68  cnlKey = 0, //< The key to use for storing/retrieving entries from DB.
73  cnlNumber
74  };
75 
77  class Row
78  {
79  public:
81  Row();
83  Row(char **row, size_t width);
85  ~Row();
86  void setValue(size_t number, char const * value);
87  char ** getRow();
88  void reset();
89  private:
90  char **row;
91  size_t width;
92  };
93 
94  CertificateDb(std::string const & db_path, size_t aMax_db_size, size_t aFs_block_size);
96  bool find(std::string const & key, const Security::CertPointer &expectedOrig, Security::CertPointer & cert, Security::PrivateKeyPointer & pkey);
98  bool purgeCert(std::string const & key);
100  bool addCertAndPrivateKey(std::string const & useKey, const Security::CertPointer & cert, const Security::PrivateKeyPointer & pkey, const Security::CertPointer &orig);
101 
103  static void Create(std::string const & db_path);
105  static void Check(std::string const & db_path, size_t max_db_size, size_t fs_block_size);
106 private:
107  void load();
108  void save();
109  size_t size();
111  void addSize(std::string const & filename);
113  void subSize(std::string const & filename);
114  size_t readSize();
115  void writeSize(size_t db_size);
116  size_t getFileSize(std::string const & filename);
117  size_t rebuildSize();
119  bool pure_find(std::string const & key, const Security::CertPointer & expectedOrig, Security::CertPointer & cert, Security::PrivateKeyPointer & pkey);
120 
121  void deleteRow(const char **row, int rowIndex);
122  bool deleteInvalidCertificate();
123  bool deleteOldestCertificate();
124  bool deleteByKey(std::string const & key);
125  bool hasRows() const;
126 
128  static bool WriteEntry(const std::string &filename, const Security::CertPointer & cert, const Security::PrivateKeyPointer & pkey, const Security::CertPointer &orig);
129 
131  static bool ReadEntry(std::string filename, Security::CertPointer & cert, Security::PrivateKeyPointer & pkey, Security::CertPointer &orig);
132 
134  static void sq_TXT_DB_delete(TXT_DB *db, const char **row);
136  static void sq_TXT_DB_delete_row(TXT_DB *db, int idx);
137 
139  static unsigned long index_serial_hash(const char **a);
141  static int index_serial_cmp(const char **a, const char **b);
143  static unsigned long index_name_hash(const char **a);
145  static int index_name_cmp(const char **a, const char **b);
146 
149 #if SQUID_USE_SSLLHASH_HACK
150  static unsigned long index_serial_hash_LHASH_HASH(const void *a) {
151  return index_serial_hash((const char **)a);
152  }
153  static int index_serial_cmp_LHASH_COMP(const void *arg1, const void *arg2) {
154  return index_serial_cmp((const char **)arg1, (const char **)arg2);
155  }
156  static unsigned long index_name_hash_LHASH_HASH(const void *a) {
157  return index_name_hash((const char **)a);
158  }
159  static int index_name_cmp_LHASH_COMP(const void *arg1, const void *arg2) {
160  return index_name_cmp((const char **)arg1, (const char **)arg2);
161  }
162 #else
164  static IMPLEMENT_LHASH_COMP_FN(index_serial_cmp,const char **)
166  static IMPLEMENT_LHASH_COMP_FN(index_name_cmp,const char **)
167 #endif
168 
169  static const std::string db_file;
170  static const std::string cert_dir;
171  static const std::string size_file;
173  static const size_t min_db_size;
174 
175  const std::string db_path;
176  const std::string db_full;
177  const std::string cert_full;
178  const std::string size_full;
179 
181  const size_t max_db_size;
182  const size_t fs_block_size;
183  mutable Lock dbLock;
184 };
185 
186 } // namespace Ssl
187 #endif // SQUID_SSL_CERTIFICATE_DB_H
188 
a source code location that is cheap to create, copy, and store
Definition: Here.h:30
A wrapper for OpenSSL database row of TXT_DB database.
void setValue(size_t number, char const *value)
Set cell's value in row.
size_t width
Number of cells in the row.
Row()
Create row wrapper.
void reset()
Abandon row and don't free memory.
char ** getRow()
Raw row.
bool deleteByKey(std::string const &key)
Delete using key.
const size_t fs_block_size
File system block size.
static bool WriteEntry(const std::string &filename, const Security::CertPointer &cert, const Security::PrivateKeyPointer &pkey, const Security::CertPointer &orig)
stores the db entry into a file
static int index_serial_cmp(const char **a, const char **b)
Callback compare function for serials. Used to create TXT_DB index of serials.
static void Create(std::string const &db_path)
Create and initialize a database under the db_path.
void deleteRow(const char **row, int rowIndex)
Delete a row from TXT_DB.
const std::string size_full
Full path of the file to store the db size.
static IMPLEMENT_LHASH_HASH_FN(index_serial_hash, const char **) static IMPLEMENT_LHASH_COMP_FN(index_serial_cmp
TXT_DB_Pointer db
Database with certificates info.
bool pure_find(std::string const &key, const Security::CertPointer &expectedOrig, Security::CertPointer &cert, Security::PrivateKeyPointer &pkey)
Only find certificate in current db and return it.
void writeSize(size_t db_size)
Write size to file size_file.
static unsigned long index_serial_hash(const char **a)
Callback hash function for serials. Used to create TXT_DB index of serials.
bool purgeCert(std::string const &key)
Delete a certificate from database.
static const std::string cert_dir
Base name of the directory to store the certs.
const std::string cert_full
Full path of the directory to store the certs.
void subSize(std::string const &filename)
Decrease db size by the given file size and update size_file.
bool deleteOldestCertificate()
Delete oldest certificate.
static unsigned long index_name_hash(const char **a)
Callback hash function for names. Used to create TXT_DB index of names..
void addSize(std::string const &filename)
Increase db size by the given file size and update size_file.
static bool ReadEntry(std::string filename, Security::CertPointer &cert, Security::PrivateKeyPointer &pkey, Security::CertPointer &orig)
loads a db entry from the file
size_t readSize()
Read size from file size_file.
bool hasRows() const
Whether the TXT_DB has stored items.
static int index_name_cmp(const char **a, const char **b)
Callback compare function for names. Used to create TXT_DB index of names..
const size_t max_db_size
Max size of db.
void save()
Save db to disk.
static void Check(std::string const &db_path, size_t max_db_size, size_t fs_block_size)
Check the database stored under the db_path.
Columns
Names of db columns.
CertificateDb(std::string const &db_path, size_t aMax_db_size, size_t aFs_block_size)
bool addCertAndPrivateKey(std::string const &useKey, const Security::CertPointer &cert, const Security::PrivateKeyPointer &pkey, const Security::CertPointer &orig)
Save certificate to disk.
static const std::string size_file
bool find(std::string const &key, const Security::CertPointer &expectedOrig, Security::CertPointer &cert, Security::PrivateKeyPointer &pkey)
finds matching generated certificate and its private key
const std::string db_full
Full path of the database index file.
static const char **static const char **static const std::string db_file
Base name of the database index file.
const std::string db_path
The database directory.
bool deleteInvalidCertificate()
Delete invalid certificate.
static void sq_TXT_DB_delete(TXT_DB *db, const char **row)
Removes the first matching row from TXT_DB. Ignores failures.
static const size_t min_db_size
Min size of disk db. If real size < min_db_size the db will be disabled.
Lock dbLock
protects the database file
static void sq_TXT_DB_delete_row(TXT_DB *db, int idx)
Remove the row on position idx from TXT_DB. Ignores failures.
size_t getFileSize(std::string const &filename)
get file size on disk.
void load()
Load db from disk.
maintains an exclusive blocking file-based lock
const char * name() const
Lock(std::string const &filename)
creates an unlocked lock
int fd
Linux file descriptor.
~Lock()
releases the lock if it is locked
bool locked() const
whether our lock is locked
void lock()
locks the lock, may block
void unlock()
unlocks locked lock or throws
std::string filename
an exception-safe way to obtain and release a lock
~Locker()
unlocks the lock if it was locked by us
Locker(Lock &, const SourceLocation &)
locks the lock if the lock was unlocked
const SourceLocation caller
where the lock was needed (currently not reported anywhere)
Lock & lock
the lock we are operating on
bool weLocked
whether we locked the lock
Definition: Xaction.cc:48
std::unique_ptr< TXT_DB, HardFun< void, TXT_DB *, &TXT_DB_free > > TXT_DB_Pointer
Definition: gadgets.h:57
number
Definition: testStatHist.cc:16

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors