1999-07-12 20:08:42 +00:00
|
|
|
/*
|
2018-03-15 18:32:45 -07:00
|
|
|
* Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10: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 http://mozilla.org/MPL/2.0/.
|
2007-06-18 23:47:57 +00:00
|
|
|
*
|
2018-02-23 09:53:12 +01:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*
|
|
|
|
* Portions Copyright (C) Network Associates, Inc.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
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.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* 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.
|
1999-07-12 20:08:42 +00:00
|
|
|
*/
|
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-06-12 11:26:04 +02:00
|
|
|
#include <config.h>
|
2000-04-29 02:02:38 +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
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
#include <isc/lang.h>
|
|
|
|
#include <isc/buffer.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>
|
1999-07-12 20:08:42 +00:00
|
|
|
#include <isc/region.h>
|
2001-11-07 23:03:54 +00:00
|
|
|
#include <isc/types.h>
|
2010-12-09 00:54:34 +00:00
|
|
|
#include <isc/refcount.h>
|
2009-07-19 04:18:05 +00:00
|
|
|
#include <isc/stdtime.h>
|
2006-12-04 01:54:53 +00:00
|
|
|
#include <isc/hmacmd5.h>
|
|
|
|
#include <isc/hmacsha.h>
|
1999-07-12 20:08:42 +00:00
|
|
|
|
2018-06-12 11:26:04 +02:00
|
|
|
#if USE_PKCS11
|
|
|
|
#include <pk11/pk11.h>
|
2016-08-19 08:02:51 +10:00
|
|
|
#include <pk11/site.h>
|
2018-06-12 11:26:04 +02:00
|
|
|
#endif /* USE_PKCS11 */
|
2016-08-19 08:02:51 +10:00
|
|
|
|
2009-07-19 04:18:05 +00:00
|
|
|
#include <dns/time.h>
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
#include <dst/dst.h>
|
|
|
|
|
2006-12-04 01:54:53 +00:00
|
|
|
#include <openssl/dh.h>
|
|
|
|
#include <openssl/err.h>
|
2008-03-31 14:42:51 +00:00
|
|
|
#include <openssl/evp.h>
|
2006-12-04 01:54:53 +00:00
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
ISC_LANG_BEGINDECLS
|
|
|
|
|
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)
|
|
|
|
|
2016-09-07 14:12:11 +10:00
|
|
|
LIBDNS_EXTERNAL_DATA extern isc_mem_t *dst__memory_pool;
|
2001-11-07 23:03:54 +00:00
|
|
|
|
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
|
|
|
|
2009-06-10 00:27:22 +00:00
|
|
|
typedef struct dst_hmacmd5_key dst_hmacmd5_key_t;
|
2006-12-04 01:54:53 +00:00
|
|
|
typedef struct dst_hmacsha1_key dst_hmacsha1_key_t;
|
|
|
|
typedef struct dst_hmacsha224_key dst_hmacsha224_key_t;
|
|
|
|
typedef struct dst_hmacsha256_key dst_hmacsha256_key_t;
|
|
|
|
typedef struct dst_hmacsha384_key dst_hmacsha384_key_t;
|
|
|
|
typedef struct dst_hmacsha512_key dst_hmacsha512_key_t;
|
|
|
|
|
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;
|
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 */
|
2008-03-31 14:42:51 +00:00
|
|
|
char *engine; /*%< engine name (HSM) */
|
|
|
|
char *label; /*%< engine label (HSM) */
|
2006-12-04 01:54:53 +00:00
|
|
|
union {
|
|
|
|
void *generic;
|
|
|
|
gss_ctx_id_t gssctx;
|
|
|
|
DH *dh;
|
2018-06-12 11:26:04 +02:00
|
|
|
#if USE_OPENSSL
|
2008-03-31 14:42:51 +00:00
|
|
|
EVP_PKEY *pkey;
|
2018-06-12 11:26:04 +02:00
|
|
|
#endif
|
|
|
|
#if USE_PKCS11
|
2014-02-11 21:20:28 -08:00
|
|
|
pk11_object_t *pkey;
|
2006-12-04 01:54:53 +00:00
|
|
|
#endif
|
|
|
|
dst_hmacmd5_key_t *hmacmd5;
|
|
|
|
dst_hmacsha1_key_t *hmacsha1;
|
|
|
|
dst_hmacsha224_key_t *hmacsha224;
|
|
|
|
dst_hmacsha256_key_t *hmacsha256;
|
|
|
|
dst_hmacsha384_key_t *hmacsha384;
|
|
|
|
dst_hmacsha512_key_t *hmacsha512;
|
2008-04-01 23:47:10 +00:00
|
|
|
|
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 */
|
2018-04-17 08:29:14 -07:00
|
|
|
bool timeset[DST_MAX_TIMES + 1]; /*%< data set? */
|
2009-10-09 06:09:21 +00:00
|
|
|
isc_stdtime_t nums[DST_MAX_NUMERIC + 1]; /*%< numeric metadata */
|
2018-04-17 08:29:14 -07:00
|
|
|
bool numset[DST_MAX_NUMERIC + 1]; /*%< data set? */
|
|
|
|
bool inactive; /*%< private key not present as it is
|
2013-08-15 10:48:05 +10:00
|
|
|
inactive */
|
2018-04-17 08:29:14 -07:00
|
|
|
bool external; /*%< external key */
|
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;
|
2012-10-24 12:58:16 -07:00
|
|
|
isc_logcategory_t *category;
|
2006-12-04 01:54:53 +00:00
|
|
|
union {
|
|
|
|
void *generic;
|
|
|
|
dst_gssapi_signverifyctx_t *gssctx;
|
|
|
|
isc_hmacmd5_t *hmacmd5ctx;
|
|
|
|
isc_hmacsha1_t *hmacsha1ctx;
|
|
|
|
isc_hmacsha224_t *hmacsha224ctx;
|
|
|
|
isc_hmacsha256_t *hmacsha256ctx;
|
|
|
|
isc_hmacsha384_t *hmacsha384ctx;
|
|
|
|
isc_hmacsha512_t *hmacsha512ctx;
|
2008-03-31 14:42:51 +00:00
|
|
|
EVP_MD_CTX *evp_md_ctx;
|
2018-06-12 11:26:04 +02:00
|
|
|
#if USE_PKCS11
|
2014-02-11 21:20:28 -08:00
|
|
|
pk11_context_t *pk11_ctx;
|
2008-03-31 14:42:51 +00:00
|
|
|
#endif
|
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);
|
2014-01-14 15:40:56 -08:00
|
|
|
isc_result_t (*createctx2)(dst_key_t *key, int maxbits,
|
|
|
|
dst_context_t *dctx);
|
2000-06-02 18:57:51 +00:00
|
|
|
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);
|
|
|
|
isc_result_t (*verify)(dst_context_t *dctx, const isc_region_t *sig);
|
2012-06-15 11:54:58 +10:00
|
|
|
isc_result_t (*verify2)(dst_context_t *dctx, int maxbits,
|
2012-06-14 15:44:20 +10:00
|
|
|
const isc_region_t *sig);
|
2000-03-06 20:06:01 +00:00
|
|
|
isc_result_t (*computesecret)(const dst_key_t *pub,
|
1999-09-27 16:55:45 +00:00
|
|
|
const dst_key_t *priv,
|
|
|
|
isc_buffer_t *secret);
|
2018-04-17 08:29:14 -07:00
|
|
|
bool (*compare)(const dst_key_t *key1, const dst_key_t *key2);
|
|
|
|
bool (*paramcompare)(const dst_key_t *key1,
|
1999-09-27 16:55:45 +00:00
|
|
|
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
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
void (*cleanup)(void);
|
2008-03-31 14:42:51 +00:00
|
|
|
|
|
|
|
isc_result_t (*fromlabel)(dst_key_t *key, const char *engine,
|
|
|
|
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
|
|
|
|
*/
|
2009-10-05 17:30:49 +00:00
|
|
|
isc_result_t dst__openssl_init(const char *engine);
|
2014-03-12 20:52:01 -07:00
|
|
|
#define dst__pkcs11_init pk11_initialize
|
2000-06-09 22:32:20 +00:00
|
|
|
|
2000-06-06 21:58:16 +00:00
|
|
|
isc_result_t dst__hmacmd5_init(struct dst_func **funcp);
|
2006-01-27 02:35:15 +00:00
|
|
|
isc_result_t dst__hmacsha1_init(struct dst_func **funcp);
|
|
|
|
isc_result_t dst__hmacsha224_init(struct dst_func **funcp);
|
|
|
|
isc_result_t dst__hmacsha256_init(struct dst_func **funcp);
|
|
|
|
isc_result_t dst__hmacsha384_init(struct dst_func **funcp);
|
|
|
|
isc_result_t dst__hmacsha512_init(struct dst_func **funcp);
|
2018-07-19 13:46:01 -04:00
|
|
|
isc_result_t dst__openssldh_init(struct dst_func **funcp);
|
2018-06-12 11:26:04 +02:00
|
|
|
#if USE_OPENSSL
|
2009-10-27 22:25:37 +00:00
|
|
|
isc_result_t dst__opensslrsa_init(struct dst_func **funcp,
|
|
|
|
unsigned char algorithm);
|
2014-01-14 15:40:56 -08:00
|
|
|
isc_result_t dst__opensslecdsa_init(struct dst_func **funcp);
|
2018-07-19 11:29:05 -04:00
|
|
|
#if HAVE_OPENSSL_ED25519 || HAVE_OPENSSL_ED448
|
2017-07-31 15:26:00 +02:00
|
|
|
isc_result_t dst__openssleddsa_init(struct dst_func **funcp);
|
2018-07-19 11:29:05 -04:00
|
|
|
#endif /* HAVE_OPENSSL_ED25519 || HAVE_OPENSSL_ED448 */
|
2018-06-12 11:26:04 +02:00
|
|
|
#endif /* USE_OPENSSL */
|
|
|
|
#if USE_PKCS11
|
|
|
|
isc_result_t dst__pkcs11rsa_init(struct dst_func **funcp);
|
|
|
|
isc_result_t dst__pkcs11dsa_init(struct dst_func **funcp);
|
2014-01-14 15:40:56 -08:00
|
|
|
isc_result_t dst__pkcs11ecdsa_init(struct dst_func **funcp);
|
2017-07-31 15:26:00 +02:00
|
|
|
#if defined(HAVE_PKCS11_ED25519) || defined(HAVE_PKCS11_ED448)
|
|
|
|
isc_result_t dst__pkcs11eddsa_init(struct dst_func **funcp);
|
|
|
|
#endif
|
2018-06-12 11:26:04 +02:00
|
|
|
#endif /* USE_PKCS11 */
|
|
|
|
#ifdef GSSAPI
|
|
|
|
isc_result_t dst__gssapi_init(struct dst_func **funcp);
|
|
|
|
#endif /* GSSAPI */
|
2000-06-06 21:58:16 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2000-06-06 21:58:16 +00:00
|
|
|
* Destructors
|
|
|
|
*/
|
2000-06-09 23:31:55 +00:00
|
|
|
void dst__openssl_destroy(void);
|
2014-03-12 20:52:01 -07:00
|
|
|
#define dst__pkcs11_destroy pk11_finalize
|
2000-06-09 23:31:55 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2000-05-02 03:54:17 +00:00
|
|
|
* Memory allocators using the DST memory pool.
|
|
|
|
*/
|
2000-06-09 22:32:20 +00:00
|
|
|
void * dst__mem_alloc(size_t size);
|
|
|
|
void dst__mem_free(void *ptr);
|
|
|
|
void * dst__mem_realloc(void *ptr, size_t size);
|
|
|
|
|
1999-07-12 20:08:42 +00:00
|
|
|
ISC_LANG_ENDDECLS
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|