1999-07-12 20:08:42 +00:00
|
|
|
/*
|
2014-01-16 15:19:24 +11:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2014-01-16 15:19:24 +11:00
|
|
|
* SPDX-License-Identifier: MPL-2.0 AND ISC
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
1999-07-12 20:08:42 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
1999-07-12 20:08:42 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2018-02-23 09:53:12 +01:00
|
|
|
*/
|
2021-06-03 08:37:05 +02:00
|
|
|
|
2018-02-23 09:53:12 +01:00
|
|
|
/*
|
1999-07-12 20:08:42 +00:00
|
|
|
* Portions Copyright (C) Network Associates, Inc.
|
|
|
|
*
|
2007-08-28 07:20:43 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
1999-07-12 20:08:42 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
|
|
|
|
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
|
|
|
|
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2000-04-29 02:02:38 +00:00
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#pragma once
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2021-05-20 16:26:53 +02:00
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
#include <isc/buffer.h>
|
2018-03-20 17:20:50 +00:00
|
|
|
#include <isc/hmac.h>
|
2001-08-28 03:58:29 +00:00
|
|
|
#include <isc/magic.h>
|
2018-06-01 09:31:59 +02:00
|
|
|
#include <isc/md.h>
|
2010-12-09 00:54:34 +00:00
|
|
|
#include <isc/refcount.h>
|
1999-07-12 20:08:42 +00:00
|
|
|
#include <isc/region.h>
|
2009-07-19 04:18:05 +00:00
|
|
|
#include <isc/stdtime.h>
|
2018-03-20 17:20:50 +00:00
|
|
|
#include <isc/types.h>
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <dns/time.h>
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
|
|
|
|
2009-06-10 00:27:22 +00:00
|
|
|
#define KEY_MAGIC ISC_MAGIC('D', 'S', 'T', 'K')
|
|
|
|
#define CTX_MAGIC ISC_MAGIC('D', 'S', 'T', 'C')
|
2001-01-17 01:08:08 +00:00
|
|
|
|
|
|
|
#define VALID_KEY(x) ISC_MAGIC_VALID(x, KEY_MAGIC)
|
|
|
|
#define VALID_CTX(x) ISC_MAGIC_VALID(x, CTX_MAGIC)
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
/***
|
|
|
|
*** Types
|
|
|
|
***/
|
|
|
|
|
2000-06-02 23:36:14 +00:00
|
|
|
typedef struct dst_func dst_func_t;
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2018-03-20 17:20:50 +00:00
|
|
|
typedef struct dst_hmac_key dst_hmac_key_t;
|
2006-12-04 01:54:53 +00:00
|
|
|
|
2014-01-14 15:40:56 -08:00
|
|
|
/*%
|
|
|
|
* Indicate whether a DST context will be used for signing
|
|
|
|
* or for verification
|
|
|
|
*/
|
|
|
|
typedef enum { DO_SIGN, DO_VERIFY } dst_use_t;
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*% DST Key Structure */
|
1999-07-12 20:08:42 +00:00
|
|
|
struct dst_key {
|
|
|
|
unsigned int magic;
|
2010-12-09 00:54:34 +00:00
|
|
|
isc_refcount_t refs;
|
2021-06-25 10:51:21 +02:00
|
|
|
isc_mutex_t mdlock; /*%< lock for read/write metadata */
|
2005-04-27 04:57:32 +00:00
|
|
|
dns_name_t *key_name; /*%< name of the key */
|
|
|
|
unsigned int key_size; /*%< size of the key in bits */
|
|
|
|
unsigned int key_proto; /*%< protocols this key is used for
|
|
|
|
* */
|
|
|
|
unsigned int key_alg; /*%< algorithm of the key */
|
2018-03-28 14:19:37 +02:00
|
|
|
uint32_t key_flags; /*%< flags of the public key */
|
|
|
|
uint16_t key_id; /*%< identifier of the key */
|
|
|
|
uint16_t key_rid; /*%< identifier of the key when
|
2011-10-20 21:20:02 +00:00
|
|
|
* revoked */
|
2018-03-28 14:19:37 +02:00
|
|
|
uint16_t key_bits; /*%< hmac digest bits */
|
2005-04-27 04:57:32 +00:00
|
|
|
dns_rdataclass_t key_class; /*%< class of the key record */
|
2011-03-17 01:40:40 +00:00
|
|
|
dns_ttl_t key_ttl; /*%< default/initial dnskey ttl */
|
2005-04-27 04:57:32 +00:00
|
|
|
isc_mem_t *mctx; /*%< memory context */
|
2022-03-09 11:27:03 +01:00
|
|
|
char *directory; /*%< key directory */
|
2024-08-05 11:40:42 +02:00
|
|
|
char *label; /*%< HSM label */
|
2006-12-04 01:54:53 +00:00
|
|
|
union {
|
|
|
|
void *generic;
|
2021-02-11 14:40:59 +01:00
|
|
|
dns_gss_ctx_id_t gssctx;
|
2018-03-20 17:20:50 +00:00
|
|
|
dst_hmac_key_t *hmac_key;
|
2022-12-26 16:29:56 +02:00
|
|
|
struct {
|
|
|
|
EVP_PKEY *pub;
|
|
|
|
EVP_PKEY *priv;
|
|
|
|
} pkeypair;
|
2006-12-04 01:54:53 +00:00
|
|
|
} keydata; /*%< pointer to key in crypto pkg fmt */
|
2009-07-19 04:18:05 +00:00
|
|
|
|
2009-10-09 06:09:21 +00:00
|
|
|
isc_stdtime_t times[DST_MAX_TIMES + 1]; /*%< timing metadata */
|
2019-09-11 16:29:33 +02:00
|
|
|
bool timeset[DST_MAX_TIMES + 1]; /*%< data set? */
|
2019-09-12 11:57:55 +02:00
|
|
|
|
2019-09-11 13:46:02 +02:00
|
|
|
uint32_t nums[DST_MAX_NUMERIC + 1]; /*%< numeric metadata
|
|
|
|
* */
|
2019-09-11 16:29:33 +02:00
|
|
|
bool numset[DST_MAX_NUMERIC + 1]; /*%< data set? */
|
2019-09-12 11:57:55 +02:00
|
|
|
|
2019-09-11 16:29:33 +02:00
|
|
|
bool bools[DST_MAX_BOOLEAN + 1]; /*%< boolean metadata
|
|
|
|
* */
|
|
|
|
bool boolset[DST_MAX_BOOLEAN + 1]; /*%< data set? */
|
|
|
|
|
2019-09-12 11:57:55 +02:00
|
|
|
dst_key_state_t keystates[DST_MAX_KEYSTATES + 1]; /*%< key states
|
|
|
|
* */
|
|
|
|
bool keystateset[DST_MAX_KEYSTATES + 1]; /*%< data
|
|
|
|
* set? */
|
|
|
|
|
2020-12-03 10:19:38 +01:00
|
|
|
bool kasp; /*%< key has kasp state */
|
2019-09-11 16:29:33 +02:00
|
|
|
bool inactive; /*%< private key not present as it is
|
|
|
|
* inactive */
|
2018-04-17 08:29:14 -07:00
|
|
|
bool external; /*%< external key */
|
2022-05-03 12:28:31 +02:00
|
|
|
bool modified; /*%< set to true if key file metadata has changed */
|
2009-09-02 06:29:01 +00:00
|
|
|
|
|
|
|
int fmt_major; /*%< private key format, major version
|
|
|
|
* */
|
|
|
|
int fmt_minor; /*%< private key format, minor version
|
|
|
|
* */
|
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
dst_func_t *func; /*%< crypto package specific functions */
|
2010-12-18 01:56:23 +00:00
|
|
|
isc_buffer_t *key_tkeytoken; /*%< TKEY token data */
|
1999-07-12 20:08:42 +00:00
|
|
|
};
|
|
|
|
|
2000-06-02 18:57:51 +00:00
|
|
|
struct dst_context {
|
|
|
|
unsigned int magic;
|
2014-01-14 15:40:56 -08:00
|
|
|
dst_use_t use;
|
2000-06-02 18:57:51 +00:00
|
|
|
dst_key_t *key;
|
|
|
|
isc_mem_t *mctx;
|
2024-08-14 13:25:50 +02:00
|
|
|
isc_logcategory_t category;
|
2006-12-04 01:54:53 +00:00
|
|
|
union {
|
|
|
|
void *generic;
|
|
|
|
dst_gssapi_signverifyctx_t *gssctx;
|
2018-03-20 17:20:50 +00:00
|
|
|
isc_hmac_t *hmac_ctx;
|
2008-03-31 14:42:51 +00:00
|
|
|
EVP_MD_CTX *evp_md_ctx;
|
2006-12-04 01:54:53 +00:00
|
|
|
} ctxdata;
|
2000-06-02 18:57:51 +00:00
|
|
|
};
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
struct dst_func {
|
2000-06-02 18:57:51 +00:00
|
|
|
/*
|
|
|
|
* Context functions
|
|
|
|
*/
|
|
|
|
isc_result_t (*createctx)(dst_key_t *key, dst_context_t *dctx);
|
|
|
|
void (*destroyctx)(dst_context_t *dctx);
|
|
|
|
isc_result_t (*adddata)(dst_context_t *dctx, const isc_region_t *data);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Key operations
|
|
|
|
*/
|
|
|
|
isc_result_t (*sign)(dst_context_t *dctx, isc_buffer_t *sig);
|
2025-04-28 17:22:18 +02:00
|
|
|
isc_result_t (*verify)(dst_context_t *dctx, const isc_region_t *sig);
|
2018-04-17 08:29:14 -07:00
|
|
|
bool (*compare)(const dst_key_t *key1, const dst_key_t *key2);
|
2009-10-24 09:46:19 +00:00
|
|
|
isc_result_t (*generate)(dst_key_t *key, int parms,
|
|
|
|
void (*callback)(int));
|
2018-04-17 08:29:14 -07:00
|
|
|
bool (*isprivate)(const dst_key_t *key);
|
2000-06-02 18:57:51 +00:00
|
|
|
void (*destroy)(dst_key_t *key);
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
/* conversion functions */
|
2000-06-02 18:57:51 +00:00
|
|
|
isc_result_t (*todns)(const dst_key_t *key, isc_buffer_t *data);
|
|
|
|
isc_result_t (*fromdns)(dst_key_t *key, isc_buffer_t *data);
|
2000-06-06 21:58:16 +00:00
|
|
|
isc_result_t (*tofile)(const dst_key_t *key, const char *directory);
|
2009-09-03 04:09:58 +00:00
|
|
|
isc_result_t (*parse)(dst_key_t *key, isc_lex_t *lexer, dst_key_t *pub);
|
2001-11-06 20:47:59 +00:00
|
|
|
|
2024-08-05 11:40:42 +02:00
|
|
|
isc_result_t (*fromlabel)(dst_key_t *key, const char *label,
|
|
|
|
const char *pin);
|
2011-01-10 05:32:04 +00:00
|
|
|
isc_result_t (*dump)(dst_key_t *key, isc_mem_t *mctx, char **buffer,
|
|
|
|
int *length);
|
|
|
|
isc_result_t (*restore)(dst_key_t *key, const char *keystr);
|
1999-07-12 20:08:42 +00:00
|
|
|
};
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2000-06-02 23:36:14 +00:00
|
|
|
* Initializers
|
|
|
|
*/
|
2024-08-05 11:40:42 +02:00
|
|
|
void
|
2000-06-06 21:58:16 +00:00
|
|
|
dst__hmacmd5_init(struct dst_func **funcp);
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2006-01-27 02:35:15 +00:00
|
|
|
dst__hmacsha1_init(struct dst_func **funcp);
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2006-01-27 02:35:15 +00:00
|
|
|
dst__hmacsha224_init(struct dst_func **funcp);
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2006-01-27 02:35:15 +00:00
|
|
|
dst__hmacsha256_init(struct dst_func **funcp);
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2006-01-27 02:35:15 +00:00
|
|
|
dst__hmacsha384_init(struct dst_func **funcp);
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2006-01-27 02:35:15 +00:00
|
|
|
dst__hmacsha512_init(struct dst_func **funcp);
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2025-04-01 00:12:52 +11:00
|
|
|
dst__opensslrsa_init(struct dst_func **funcp, unsigned short algorithm);
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2014-01-14 15:40:56 -08:00
|
|
|
dst__opensslecdsa_init(struct dst_func **funcp);
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2022-08-15 15:36:03 +10:00
|
|
|
dst__openssleddsa_init(struct dst_func **funcp, unsigned char algorithm);
|
2018-08-07 16:46:53 +02:00
|
|
|
#if HAVE_GSSAPI
|
2024-08-05 12:14:26 +02:00
|
|
|
void
|
2018-06-12 11:26:04 +02:00
|
|
|
dst__gssapi_init(struct dst_func **funcp);
|
2018-08-07 16:46:53 +02:00
|
|
|
#endif /* HAVE_GSSAPI*/
|
2000-06-06 21:58:16 +00:00
|
|
|
|
2023-03-30 18:00:17 +02:00
|
|
|
/*%
|
|
|
|
* Secure private file handling
|
|
|
|
*/
|
|
|
|
FILE *
|
|
|
|
dst_key_open(char *tmpname, mode_t mode);
|
|
|
|
isc_result_t
|
|
|
|
dst_key_close(char *tmpname, FILE *fp, char *filename);
|
|
|
|
isc_result_t
|
|
|
|
dst_key_cleanup(char *tmpname, FILE *fp);
|
|
|
|
|
2025-02-04 13:17:31 +01:00
|
|
|
/*%
|
|
|
|
* Library constructor and destructor.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
dst__lib_initialize(void);
|
|
|
|
void
|
|
|
|
dst__lib_shutdown(void);
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|