servlib/security/KeyDB.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  * Copyright 2007 BBN Technologies Corporation
       
     3  *
       
     4  * Licensed under the Apache License, Version 2.0 (the "License"); you
       
     5  * may not use this file except in compliance with the License. You
       
     6  * may obtain a copy of the License at
       
     7  *
       
     8  *     http://www.apache.org/licenses/LICENSE-2.0
       
     9  *
       
    10  * Unless required by applicable law or agreed to in writing, software
       
    11  * distributed under the License is distributed on an "AS IS" BASIS,
       
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
       
    13  * implied.
       
    14  */
       
    15 
       
    16 /*
       
    17  * $Id$
       
    18  */
       
    19 
       
    20 #ifndef _KEYDB_H_
       
    21 #define _KEYDB_H_
       
    22 
       
    23 #ifdef BSP_ENABLED
       
    24 
       
    25 #include <list>
       
    26 #include <string>
       
    27 #include <oasys/compat/inttypes.h>
       
    28 #include <oasys/util/Singleton.h>
       
    29 #include <oasys/util/StringBuffer.h>
       
    30 
       
    31 namespace dtn {
       
    32 
       
    33 class KeyDB : public oasys::Singleton<KeyDB, false> {
       
    34 public:
       
    35 
       
    36     /**
       
    37      * Nested class used to provide storage for key bytes.
       
    38      */
       
    39     class Entry;
       
    40 
       
    41     /**
       
    42      * Constructor (called at startup).
       
    43      */
       
    44     KeyDB();
       
    45 
       
    46     /**
       
    47      * Destructor (called at shutdown).
       
    48      */
       
    49     ~KeyDB();
       
    50 
       
    51     /**
       
    52      * Startup-time initializer.
       
    53      */
       
    54     static void init();
       
    55 
       
    56     /**
       
    57      * Set the key for a given host and ciphersuite type.  This will
       
    58      * overwrite any existing entry for the same host/ciphersuite.
       
    59      *
       
    60      * XXX Eventually we'd probably want to index by KeyID too, which
       
    61      * is how we'd distinguish between multiple keys for the same
       
    62      * host.
       
    63      */
       
    64     static void set_key(Entry& entry);
       
    65 
       
    66     /**
       
    67      * Find the key for a given host and ciphersuite type.
       
    68      *
       
    69      * @return A pointer to the matching Entry, or NULL if no
       
    70      * match is found.
       
    71      */
       
    72     static const Entry* find_key(const char* host, u_int16_t cs_num);
       
    73     
       
    74     /**
       
    75      * Delete the key entry for the given host and ciphersuite type.
       
    76      */
       
    77     static void del_key(const char* host, u_int16_t cs_num);
       
    78 
       
    79     /**
       
    80      * Delete all key entries (including wildcard entries).
       
    81      */
       
    82     static void flush_keys();
       
    83 
       
    84     /**
       
    85      * Dump the contents of the KeyDB in human-readable form.
       
    86      */
       
    87     static void dump(oasys::StringBuffer* buf);
       
    88 
       
    89     /**
       
    90      * Dump a human-readable header for the output of dump().
       
    91      */
       
    92     static void dump_header(oasys::StringBuffer* buf);
       
    93 
       
    94     /**
       
    95      * Validate the specified ciphersuite number (see if it
       
    96      * corresponds to a registered ciphersuite).
       
    97      */
       
    98     static bool validate_cs_num(u_int16_t cs_num);
       
    99 
       
   100     /**
       
   101      * Validate that the specified key length matches what's expected
       
   102      * for the specified ciphersuite.  Stores the expected length back
       
   103      * into the key_len argument if it's wrong.
       
   104      */
       
   105     static bool validate_key_len(u_int16_t cs_num, size_t* key_len);
       
   106 
       
   107 private:
       
   108 
       
   109     typedef std::list<Entry> EntryList;
       
   110 
       
   111     EntryList keys_;
       
   112 
       
   113 };
       
   114 
       
   115 class KeyDB::Entry {
       
   116 
       
   117 public:
       
   118 
       
   119     /**
       
   120      * Constructor.
       
   121      *
       
   122      * @param host     Name of host for whom this key should be used.
       
   123      * @param cs_num   Ciphersuite number this key is to be used with.
       
   124      * @param key      Key data.
       
   125      * @param key_len  Length of key data (in bytes).
       
   126      */
       
   127     Entry(const char* host, u_int16_t cs_num, const u_char* key,
       
   128           size_t key_len);
       
   129 
       
   130     /**
       
   131      * Default constructor.
       
   132      */
       
   133     Entry();
       
   134 
       
   135     /**
       
   136      * Copy constructor.
       
   137      */
       
   138     Entry(const Entry& other);
       
   139 
       
   140     /**
       
   141      * Destructor.
       
   142      */
       
   143     ~Entry();
       
   144 
       
   145     /**
       
   146      * Assignment operator.
       
   147      */
       
   148     void operator=(const Entry& other);
       
   149 
       
   150     /**
       
   151      * Determine if two entries have the same host and ciphersuite
       
   152      * number.
       
   153      */
       
   154     bool match(const Entry& other) const;
       
   155 
       
   156     /**
       
   157      * Determine if this entry matches the given host and ciphersuite
       
   158      * number.
       
   159      */
       
   160     bool match(const char* host, u_int16_t cs_num) const;
       
   161 
       
   162     /**
       
   163      * Same as match(), but also matches wildcard entries (where host
       
   164      * is "*").
       
   165      */
       
   166     bool match_wildcard(const char* host, u_int16_t cs_num) const;
       
   167 
       
   168     /// @{ Non-mutating accessors.
       
   169     const std::string& host()     const { return host_; }
       
   170     u_int16_t          cs_num()   const { return cs_num_; }
       
   171     const u_char*      key()      const { return key_; }
       
   172     size_t             key_len()  const { return key_len_; }
       
   173     /// @}
       
   174 
       
   175     /**
       
   176      * Dump this entry to a StringBuffer in human-readable form.
       
   177      */
       
   178     void dump(oasys::StringBuffer* buf) const;
       
   179 
       
   180     /**
       
   181      * Dump a human-readable header for the output of dump().
       
   182      */
       
   183     static void dump_header(oasys::StringBuffer* buf);
       
   184 
       
   185 private:
       
   186     std::string host_;
       
   187     u_int16_t   cs_num_;
       
   188     u_char*     key_;
       
   189     size_t      key_len_;
       
   190 };
       
   191 
       
   192 } // namespace dtn
       
   193 
       
   194 #endif  /* BSP_ENABLED */
       
   195 
       
   196 #endif  /* _KEYDB_H_ */