15185a700Sflorian /* 25185a700Sflorian * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 35185a700Sflorian * 45185a700Sflorian * Permission to use, copy, modify, and/or distribute this software for any 55185a700Sflorian * purpose with or without fee is hereby granted, provided that the above 65185a700Sflorian * copyright notice and this permission notice appear in all copies. 75185a700Sflorian * 85185a700Sflorian * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 95185a700Sflorian * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 105185a700Sflorian * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 115185a700Sflorian * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 125185a700Sflorian * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 135185a700Sflorian * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 145185a700Sflorian * PERFORMANCE OF THIS SOFTWARE. 155185a700Sflorian */ 165185a700Sflorian 17*1fb015a8Sflorian /* $Id: dst.h,v 1.11 2020/09/14 08:40:43 florian Exp $ */ 185185a700Sflorian 195185a700Sflorian #ifndef DST_DST_H 205185a700Sflorian #define DST_DST_H 1 215185a700Sflorian 225185a700Sflorian /*! \file dst/dst.h */ 235185a700Sflorian 245185a700Sflorian #include <dns/types.h> 255185a700Sflorian #include <dns/log.h> 265185a700Sflorian #include <dns/name.h> 275185a700Sflorian #include <dns/secalg.h> 285185a700Sflorian #include <dns/ds.h> 295185a700Sflorian 305185a700Sflorian /*** 315185a700Sflorian *** Types 325185a700Sflorian ***/ 335185a700Sflorian 345185a700Sflorian /*% 355185a700Sflorian * The dst_key structure is opaque. Applications should use the accessor 365185a700Sflorian * functions provided to retrieve key attributes. If an application needs 375185a700Sflorian * to set attributes, new accessor functions will be written. 385185a700Sflorian */ 395185a700Sflorian 405185a700Sflorian typedef struct dst_key dst_key_t; 415185a700Sflorian typedef struct dst_context dst_context_t; 425185a700Sflorian 435185a700Sflorian /* DST algorithm codes */ 445185a700Sflorian #define DST_ALG_UNKNOWN 0 455185a700Sflorian #define DST_ALG_RSAMD5 1 465185a700Sflorian #define DST_ALG_RSA DST_ALG_RSAMD5 /*%< backwards compatibility */ 475185a700Sflorian #define DST_ALG_HMACSHA1 161 /* XXXMPA */ 485185a700Sflorian #define DST_ALG_HMACSHA224 162 /* XXXMPA */ 495185a700Sflorian #define DST_ALG_HMACSHA256 163 /* XXXMPA */ 505185a700Sflorian #define DST_ALG_HMACSHA384 164 /* XXXMPA */ 515185a700Sflorian #define DST_ALG_HMACSHA512 165 /* XXXMPA */ 525185a700Sflorian #define DST_MAX_ALGS 255 535185a700Sflorian 545185a700Sflorian /*% A buffer of this size is large enough to hold any key */ 555185a700Sflorian #define DST_KEY_MAXSIZE 1280 565185a700Sflorian 575185a700Sflorian /*% 585185a700Sflorian * A buffer of this size is large enough to hold the textual representation 595185a700Sflorian * of any key 605185a700Sflorian */ 615185a700Sflorian #define DST_KEY_MAXTEXTSIZE 2048 625185a700Sflorian 635185a700Sflorian /*% 'Type' for dst_read_key() */ 645185a700Sflorian #define DST_TYPE_KEY 0x1000000 /* KEY key */ 655185a700Sflorian #define DST_TYPE_PRIVATE 0x2000000 665185a700Sflorian #define DST_TYPE_PUBLIC 0x4000000 675185a700Sflorian 685185a700Sflorian /* Key timing metadata definitions */ 695185a700Sflorian #define DST_TIME_CREATED 0 705185a700Sflorian #define DST_TIME_PUBLISH 1 715185a700Sflorian #define DST_TIME_ACTIVATE 2 725185a700Sflorian #define DST_TIME_REVOKE 3 735185a700Sflorian #define DST_TIME_INACTIVE 4 745185a700Sflorian #define DST_TIME_DELETE 5 755185a700Sflorian #define DST_TIME_DSPUBLISH 6 765185a700Sflorian #define DST_MAX_TIMES 6 775185a700Sflorian 785185a700Sflorian /* Numeric metadata definitions */ 795185a700Sflorian #define DST_NUM_PREDECESSOR 0 805185a700Sflorian #define DST_NUM_SUCCESSOR 1 815185a700Sflorian #define DST_NUM_MAXTTL 2 825185a700Sflorian #define DST_NUM_ROLLPERIOD 3 835185a700Sflorian #define DST_MAX_NUMERIC 3 845185a700Sflorian 855185a700Sflorian /* 865185a700Sflorian * Current format version number of the private key parser. 875185a700Sflorian * 885185a700Sflorian * When parsing a key file with the same major number but a higher minor 895185a700Sflorian * number, the key parser will ignore any fields it does not recognize. 905185a700Sflorian * Thus, DST_MINOR_VERSION should be incremented whenever new 915185a700Sflorian * fields are added to the private key file (such as new metadata). 925185a700Sflorian * 935185a700Sflorian * When rewriting these keys, those fields will be dropped, and the 945185a700Sflorian * format version set back to the current one.. 955185a700Sflorian * 965185a700Sflorian * When a key is seen with a higher major number, the key parser will 975185a700Sflorian * reject it as invalid. Thus, DST_MAJOR_VERSION should be incremented 985185a700Sflorian * and DST_MINOR_VERSION set to zero whenever there is a format change 995185a700Sflorian * which is not backward compatible to previous versions of the dst_key 1005185a700Sflorian * parser, such as change in the syntax of an existing field, the removal 1015185a700Sflorian * of a currently mandatory field, or a new field added which would 1025185a700Sflorian * alter the functioning of the key if it were absent. 1035185a700Sflorian */ 1045185a700Sflorian #define DST_MAJOR_VERSION 1 1055185a700Sflorian #define DST_MINOR_VERSION 3 1065185a700Sflorian 1075185a700Sflorian /*** 1085185a700Sflorian *** Functions 1095185a700Sflorian ***/ 1105185a700Sflorian 1115185a700Sflorian isc_result_t 1125185a700Sflorian dst_lib_init(void); 1135185a700Sflorian /*%< 1145185a700Sflorian * Initializes the DST subsystem. 1155185a700Sflorian * 1165185a700Sflorian * Requires: 1175185a700Sflorian * 1185185a700Sflorian * Returns: 1195185a700Sflorian * \li ISC_R_SUCCESS 1205185a700Sflorian * \li ISC_R_NOMEMORY 1215185a700Sflorian * \li DST_R_NOENGINE 1225185a700Sflorian * 1235185a700Sflorian * Ensures: 1245185a700Sflorian * \li DST is properly initialized. 1255185a700Sflorian */ 1265185a700Sflorian 1275185a700Sflorian void 1285185a700Sflorian dst_lib_destroy(void); 1295185a700Sflorian /*%< 1305185a700Sflorian * Releases all resources allocated by DST. 1315185a700Sflorian */ 1325185a700Sflorian 133*1fb015a8Sflorian int 1345185a700Sflorian dst_algorithm_supported(unsigned int alg); 1355185a700Sflorian /*%< 1365185a700Sflorian * Checks that a given algorithm is supported by DST. 1375185a700Sflorian * 1385185a700Sflorian * Returns: 139*1fb015a8Sflorian * \li 1 140*1fb015a8Sflorian * \li 0 1415185a700Sflorian */ 1425185a700Sflorian 1435185a700Sflorian isc_result_t 1445185a700Sflorian dst_context_create3(dst_key_t *key, 145*1fb015a8Sflorian isc_logcategory_t *category, int useforsigning, 1465185a700Sflorian dst_context_t **dctxp); 1475185a700Sflorian 1485185a700Sflorian /*%< 1495185a700Sflorian * Creates a context to be used for a sign or verify operation. 1505185a700Sflorian * 1515185a700Sflorian * Requires: 1525185a700Sflorian * \li "key" is a valid key. 1535185a700Sflorian * \li dctxp != NULL && *dctxp == NULL 1545185a700Sflorian * 1555185a700Sflorian * Returns: 1565185a700Sflorian * \li ISC_R_SUCCESS 1575185a700Sflorian * \li ISC_R_NOMEMORY 1585185a700Sflorian * 1595185a700Sflorian * Ensures: 1605185a700Sflorian * \li *dctxp will contain a usable context. 1615185a700Sflorian */ 1625185a700Sflorian 1635185a700Sflorian void 1645185a700Sflorian dst_context_destroy(dst_context_t **dctxp); 1655185a700Sflorian /*%< 1665185a700Sflorian * Destroys all memory associated with a context. 1675185a700Sflorian * 1685185a700Sflorian * Requires: 1695185a700Sflorian * \li *dctxp != NULL && *dctxp == NULL 1705185a700Sflorian * 1715185a700Sflorian * Ensures: 1725185a700Sflorian * \li *dctxp == NULL 1735185a700Sflorian */ 1745185a700Sflorian 1755185a700Sflorian isc_result_t 1765185a700Sflorian dst_context_adddata(dst_context_t *dctx, const isc_region_t *data); 1775185a700Sflorian /*%< 1785185a700Sflorian * Incrementally adds data to the context to be used in a sign or verify 1795185a700Sflorian * operation. 1805185a700Sflorian * 1815185a700Sflorian * Requires: 1825185a700Sflorian * \li "dctx" is a valid context 1835185a700Sflorian * \li "data" is a valid region 1845185a700Sflorian * 1855185a700Sflorian * Returns: 1865185a700Sflorian * \li ISC_R_SUCCESS 1875185a700Sflorian * \li DST_R_SIGNFAILURE 1885185a700Sflorian * \li all other errors indicate failure 1895185a700Sflorian */ 1905185a700Sflorian 1915185a700Sflorian isc_result_t 1925185a700Sflorian dst_context_sign(dst_context_t *dctx, isc_buffer_t *sig); 1935185a700Sflorian /*%< 1945185a700Sflorian * Computes a signature using the data and key stored in the context. 1955185a700Sflorian * 1965185a700Sflorian * Requires: 1975185a700Sflorian * \li "dctx" is a valid context. 1985185a700Sflorian * \li "sig" is a valid buffer. 1995185a700Sflorian * 2005185a700Sflorian * Returns: 2015185a700Sflorian * \li ISC_R_SUCCESS 2025185a700Sflorian * \li DST_R_VERIFYFAILURE 2035185a700Sflorian * \li all other errors indicate failure 2045185a700Sflorian * 2055185a700Sflorian * Ensures: 2065185a700Sflorian * \li "sig" will contain the signature 2075185a700Sflorian */ 2085185a700Sflorian 2095185a700Sflorian isc_result_t 2105185a700Sflorian dst_context_verify(dst_context_t *dctx, isc_region_t *sig); 2115185a700Sflorian 2125185a700Sflorian isc_result_t 2135185a700Sflorian dst_key_todns(const dst_key_t *key, isc_buffer_t *target); 2145185a700Sflorian /*%< 2155185a700Sflorian * Converts a DST key into a DNS KEY record. 2165185a700Sflorian * 2175185a700Sflorian * Requires: 2185185a700Sflorian * \li "key" is a valid key. 2195185a700Sflorian * \li "target" is a valid buffer. There must be at least 4 bytes unused. 2205185a700Sflorian * 2215185a700Sflorian * Returns: 2225185a700Sflorian * \li ISC_R_SUCCESS 2235185a700Sflorian * \li any other result indicates failure 2245185a700Sflorian * 2255185a700Sflorian * Ensures: 2265185a700Sflorian * \li If successful, the used pointer in 'target' is advanced by at least 4. 2275185a700Sflorian */ 2285185a700Sflorian 2295185a700Sflorian isc_result_t 23027db9c2cSflorian dst_key_frombuffer(unsigned int alg, unsigned int flags, unsigned int protocol, 2315185a700Sflorian isc_buffer_t *source, dst_key_t **keyp); 2325185a700Sflorian /*%< 2335185a700Sflorian * Converts a buffer containing DNS KEY RDATA into a DST key. 2345185a700Sflorian * 2355185a700Sflorian * Requires: 2365185a700Sflorian *\li "name" is a valid absolute dns name. 2375185a700Sflorian *\li "alg" is a supported key algorithm. 2385185a700Sflorian *\li "source" is a valid buffer. 2395185a700Sflorian *\li "keyp" is not NULL and "*keyp" is NULL. 2405185a700Sflorian * 2415185a700Sflorian * Returns: 2425185a700Sflorian *\li ISC_R_SUCCESS 2435185a700Sflorian * \li any other result indicates failure 2445185a700Sflorian * 2455185a700Sflorian * Ensures: 2465185a700Sflorian *\li If successful, *keyp will contain a valid key, and the consumed 2475185a700Sflorian * pointer in source will be advanced. 2485185a700Sflorian */ 2495185a700Sflorian 2505185a700Sflorian void 2515185a700Sflorian dst_key_attach(dst_key_t *source, dst_key_t **target); 2525185a700Sflorian /* 2535185a700Sflorian * Attach to a existing key increasing the reference count. 2545185a700Sflorian * 2555185a700Sflorian * Requires: 2565185a700Sflorian *\li 'source' to be a valid key. 2575185a700Sflorian *\li 'target' to be non-NULL and '*target' to be NULL. 2585185a700Sflorian */ 2595185a700Sflorian 2605185a700Sflorian void 2615185a700Sflorian dst_key_free(dst_key_t **keyp); 2625185a700Sflorian /*%< 2635185a700Sflorian * Decrement the key's reference counter and, when it reaches zero, 2645185a700Sflorian * release all memory associated with the key. 2655185a700Sflorian * 2665185a700Sflorian * Requires: 2675185a700Sflorian *\li "keyp" is not NULL and "*keyp" is a valid key. 2685185a700Sflorian *\li reference counter greater than zero. 2695185a700Sflorian * 2705185a700Sflorian * Ensures: 2715185a700Sflorian *\li All memory associated with "*keyp" will be freed. 2725185a700Sflorian *\li *keyp == NULL 2735185a700Sflorian */ 2745185a700Sflorian 2755185a700Sflorian /*%< 2765185a700Sflorian * Accessor functions to obtain key fields. 2775185a700Sflorian * 2785185a700Sflorian * Require: 2795185a700Sflorian *\li "key" is a valid key. 2805185a700Sflorian */ 2815185a700Sflorian unsigned int 2825185a700Sflorian dst_key_size(const dst_key_t *key); 2835185a700Sflorian 2845185a700Sflorian unsigned int 2855185a700Sflorian dst_key_alg(const dst_key_t *key); 2865185a700Sflorian 2875185a700Sflorian isc_result_t 2885185a700Sflorian dst_key_sigsize(const dst_key_t *key, unsigned int *n); 2895185a700Sflorian /*%< 2905185a700Sflorian * Computes the size of a signature generated by the given key. 2915185a700Sflorian * 2925185a700Sflorian * Requires: 2935185a700Sflorian *\li "key" is a valid key. 2945185a700Sflorian *\li "n" is not NULL 2955185a700Sflorian * 2965185a700Sflorian * Returns: 2975185a700Sflorian *\li #ISC_R_SUCCESS 2985185a700Sflorian *\li DST_R_UNSUPPORTEDALG 2995185a700Sflorian * 3005185a700Sflorian * Ensures: 3015185a700Sflorian *\li "n" stores the size of a generated signature 3025185a700Sflorian */ 3035185a700Sflorian 3045185a700Sflorian uint16_t 3055185a700Sflorian dst_region_computeid(const isc_region_t *source, unsigned int alg); 3065185a700Sflorian /*%< 3075185a700Sflorian * Computes the (revoked) key id of the key stored in the provided 3085185a700Sflorian * region with the given algorithm. 3095185a700Sflorian * 3105185a700Sflorian * Requires: 3115185a700Sflorian *\li "source" contains a valid, non-NULL region. 3125185a700Sflorian * 3135185a700Sflorian * Returns: 3145185a700Sflorian *\li the key id 3155185a700Sflorian */ 3165185a700Sflorian 3175185a700Sflorian uint16_t 3185185a700Sflorian dst_key_getbits(const dst_key_t *key); 3195185a700Sflorian /*%< 3205185a700Sflorian * Get the number of digest bits required (0 == MAX). 3215185a700Sflorian * 3225185a700Sflorian * Requires: 3235185a700Sflorian * "key" is a valid key. 3245185a700Sflorian */ 3255185a700Sflorian 3265185a700Sflorian void 3275185a700Sflorian dst_key_setbits(dst_key_t *key, uint16_t bits); 3285185a700Sflorian /*%< 3295185a700Sflorian * Set the number of digest bits required (0 == MAX). 3305185a700Sflorian * 3315185a700Sflorian * Requires: 3325185a700Sflorian * "key" is a valid key. 3335185a700Sflorian */ 3345185a700Sflorian 3355185a700Sflorian #endif /* DST_DST_H */ 336