"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/lib/dns/dst_internal.h" (4 Sep 2020, 7655 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dst_internal.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  *
   11  * Portions Copyright (C) Network Associates, Inc.
   12  *
   13  * Permission to use, copy, modify, and/or distribute this software for any
   14  * purpose with or without fee is hereby granted, provided that the above
   15  * copyright notice and this permission notice appear in all copies.
   16  *
   17  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
   18  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
   19  * WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE
   20  * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   22  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
   23  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   24  */
   25 
   26 #pragma once
   27 
   28 #include <inttypes.h>
   29 #include <stdbool.h>
   30 
   31 #include <isc/buffer.h>
   32 #include <isc/hmac.h>
   33 #include <isc/lang.h>
   34 #include <isc/magic.h>
   35 #include <isc/md.h>
   36 #include <isc/refcount.h>
   37 #include <isc/region.h>
   38 #include <isc/stdtime.h>
   39 #include <isc/types.h>
   40 
   41 #if USE_PKCS11
   42 #include <pk11/pk11.h>
   43 #include <pk11/site.h>
   44 #endif /* USE_PKCS11 */
   45 
   46 #include <openssl/dh.h>
   47 #include <openssl/err.h>
   48 #include <openssl/evp.h>
   49 #include <openssl/objects.h>
   50 #include <openssl/rsa.h>
   51 
   52 #include <dns/time.h>
   53 
   54 #include <dst/dst.h>
   55 
   56 ISC_LANG_BEGINDECLS
   57 
   58 #define KEY_MAGIC ISC_MAGIC('D', 'S', 'T', 'K')
   59 #define CTX_MAGIC ISC_MAGIC('D', 'S', 'T', 'C')
   60 
   61 #define VALID_KEY(x) ISC_MAGIC_VALID(x, KEY_MAGIC)
   62 #define VALID_CTX(x) ISC_MAGIC_VALID(x, CTX_MAGIC)
   63 
   64 /***
   65  *** Types
   66  ***/
   67 
   68 typedef struct dst_func dst_func_t;
   69 
   70 typedef struct dst_hmac_key dst_hmac_key_t;
   71 
   72 /*%
   73  * Indicate whether a DST context will be used for signing
   74  * or for verification
   75  */
   76 typedef enum { DO_SIGN, DO_VERIFY } dst_use_t;
   77 
   78 /*% DST Key Structure */
   79 struct dst_key {
   80     unsigned int magic;
   81     isc_refcount_t refs;
   82     dns_name_t *key_name;       /*%< name of the key */
   83     unsigned int key_size;      /*%< size of the key in bits */
   84     unsigned int key_proto;     /*%< protocols this key is used for
   85                      * */
   86     unsigned int key_alg;       /*%< algorithm of the key */
   87     uint32_t key_flags;     /*%< flags of the public key */
   88     uint16_t key_id;        /*%< identifier of the key */
   89     uint16_t key_rid;       /*%< identifier of the key when
   90                      *   revoked */
   91     uint16_t key_bits;      /*%< hmac digest bits */
   92     dns_rdataclass_t key_class; /*%< class of the key record */
   93     dns_ttl_t key_ttl;      /*%< default/initial dnskey ttl */
   94     isc_mem_t *mctx;        /*%< memory context */
   95     char *engine;           /*%< engine name (HSM) */
   96     char *label;            /*%< engine label (HSM) */
   97     union {
   98         void *generic;
   99         gss_ctx_id_t gssctx;
  100         DH *dh;
  101 #if USE_OPENSSL
  102         EVP_PKEY *pkey;
  103 #endif /* if USE_OPENSSL */
  104 #if USE_PKCS11
  105         pk11_object_t *pkey;
  106 #endif /* if USE_PKCS11 */
  107         dst_hmac_key_t *hmac_key;
  108     } keydata; /*%< pointer to key in crypto pkg fmt */
  109 
  110     isc_stdtime_t times[DST_MAX_TIMES + 1]; /*%< timing metadata */
  111     bool timeset[DST_MAX_TIMES + 1];    /*%< data set? */
  112 
  113     uint32_t nums[DST_MAX_NUMERIC + 1]; /*%< numeric metadata
  114                          * */
  115     bool numset[DST_MAX_NUMERIC + 1];   /*%< data set? */
  116 
  117     bool bools[DST_MAX_BOOLEAN + 1];   /*%< boolean metadata
  118                         * */
  119     bool boolset[DST_MAX_BOOLEAN + 1]; /*%< data set? */
  120 
  121     dst_key_state_t keystates[DST_MAX_KEYSTATES + 1]; /*%< key states
  122                                * */
  123     bool keystateset[DST_MAX_KEYSTATES + 1];      /*%< data
  124                                * set? */
  125 
  126     bool inactive; /*%< private key not present as it is
  127             * inactive */
  128     bool external; /*%< external key */
  129 
  130     int fmt_major; /*%< private key format, major version
  131             * */
  132     int fmt_minor; /*%< private key format, minor version
  133             * */
  134 
  135     dst_func_t *func;        /*%< crypto package specific functions */
  136     isc_buffer_t *key_tkeytoken; /*%< TKEY token data */
  137 };
  138 
  139 struct dst_context {
  140     unsigned int magic;
  141     dst_use_t use;
  142     dst_key_t *key;
  143     isc_mem_t *mctx;
  144     isc_logcategory_t *category;
  145     union {
  146         void *generic;
  147         dst_gssapi_signverifyctx_t *gssctx;
  148         isc_hmac_t *hmac_ctx;
  149         EVP_MD_CTX *evp_md_ctx;
  150 #if USE_PKCS11
  151         pk11_context_t *pk11_ctx;
  152 #endif /* if USE_PKCS11 */
  153     } ctxdata;
  154 };
  155 
  156 struct dst_func {
  157     /*
  158      * Context functions
  159      */
  160     isc_result_t (*createctx)(dst_key_t *key, dst_context_t *dctx);
  161     isc_result_t (*createctx2)(dst_key_t *key, int maxbits,
  162                    dst_context_t *dctx);
  163     void (*destroyctx)(dst_context_t *dctx);
  164     isc_result_t (*adddata)(dst_context_t *dctx, const isc_region_t *data);
  165 
  166     /*
  167      * Key operations
  168      */
  169     isc_result_t (*sign)(dst_context_t *dctx, isc_buffer_t *sig);
  170     isc_result_t (*verify)(dst_context_t *dctx, const isc_region_t *sig);
  171     isc_result_t (*verify2)(dst_context_t *dctx, int maxbits,
  172                 const isc_region_t *sig);
  173     isc_result_t (*computesecret)(const dst_key_t *pub,
  174                       const dst_key_t *priv,
  175                       isc_buffer_t *secret);
  176     bool (*compare)(const dst_key_t *key1, const dst_key_t *key2);
  177     bool (*paramcompare)(const dst_key_t *key1, const dst_key_t *key2);
  178     isc_result_t (*generate)(dst_key_t *key, int parms,
  179                  void (*callback)(int));
  180     bool (*isprivate)(const dst_key_t *key);
  181     void (*destroy)(dst_key_t *key);
  182 
  183     /* conversion functions */
  184     isc_result_t (*todns)(const dst_key_t *key, isc_buffer_t *data);
  185     isc_result_t (*fromdns)(dst_key_t *key, isc_buffer_t *data);
  186     isc_result_t (*tofile)(const dst_key_t *key, const char *directory);
  187     isc_result_t (*parse)(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub);
  188 
  189     /* cleanup */
  190     void (*cleanup)(void);
  191 
  192     isc_result_t (*fromlabel)(dst_key_t *key, const char *engine,
  193                   const char *label, const char *pin);
  194     isc_result_t (*dump)(dst_key_t *key, isc_mem_t *mctx, char **buffer,
  195                  int *length);
  196     isc_result_t (*restore)(dst_key_t *key, const char *keystr);
  197 };
  198 
  199 /*%
  200  * Initializers
  201  */
  202 isc_result_t
  203 dst__openssl_init(isc_mem_t *, const char *engine);
  204 #define dst__pkcs11_init pk11_initialize
  205 
  206 isc_result_t
  207 dst__hmacmd5_init(struct dst_func **funcp);
  208 isc_result_t
  209 dst__hmacsha1_init(struct dst_func **funcp);
  210 isc_result_t
  211 dst__hmacsha224_init(struct dst_func **funcp);
  212 isc_result_t
  213 dst__hmacsha256_init(struct dst_func **funcp);
  214 isc_result_t
  215 dst__hmacsha384_init(struct dst_func **funcp);
  216 isc_result_t
  217 dst__hmacsha512_init(struct dst_func **funcp);
  218 isc_result_t
  219 dst__openssldh_init(struct dst_func **funcp);
  220 #if USE_OPENSSL
  221 isc_result_t
  222 dst__opensslrsa_init(struct dst_func **funcp, unsigned char algorithm);
  223 isc_result_t
  224 dst__opensslecdsa_init(struct dst_func **funcp);
  225 #if HAVE_OPENSSL_ED25519 || HAVE_OPENSSL_ED448
  226 isc_result_t
  227 dst__openssleddsa_init(struct dst_func **funcp);
  228 #endif /* HAVE_OPENSSL_ED25519 || HAVE_OPENSSL_ED448 */
  229 #endif /* USE_OPENSSL */
  230 #if USE_PKCS11
  231 isc_result_t
  232 dst__pkcs11rsa_init(struct dst_func **funcp);
  233 isc_result_t
  234 dst__pkcs11dsa_init(struct dst_func **funcp);
  235 isc_result_t
  236 dst__pkcs11ecdsa_init(struct dst_func **funcp);
  237 isc_result_t
  238 dst__pkcs11eddsa_init(struct dst_func **funcp);
  239 #endif /* USE_PKCS11 */
  240 #if HAVE_GSSAPI
  241 isc_result_t
  242 dst__gssapi_init(struct dst_func **funcp);
  243 #endif /* HAVE_GSSAPI*/
  244 
  245 /*%
  246  * Destructors
  247  */
  248 void
  249 dst__openssl_destroy(void);
  250 #define dst__pkcs11_destroy pk11_finalize
  251 
  252 /*%
  253  * Memory allocators using the DST memory pool.
  254  */
  255 void *
  256 dst__mem_alloc(size_t size);
  257 void
  258 dst__mem_free(void *ptr);
  259 void *
  260 dst__mem_realloc(void *ptr, size_t size);
  261 
  262 ISC_LANG_ENDDECLS
  263 
  264 /*! \file */