static void create_dnskeys(dnssec_keystore_t *keystore, dnssec_key_algorithm_t algorithm, const char *id, dnssec_key_t **p11_key_ptr, dnssec_key_t **soft_key_ptr) { int r; // construct PKCS #11 privkey-pubkey key pair dnssec_key_t *p11_key = NULL; r = dnssec_key_new(&p11_key); ok(r == DNSSEC_EOK && p11_key != NULL, MSG_PKCS11 " dnssec_key_new()"); r = dnssec_key_set_algorithm(p11_key, algorithm); ok(r == DNSSEC_EOK, MSG_PKCS11 " dnssec_set_key_algorithm()"); r = dnssec_key_import_keystore(p11_key, keystore, id); ok(r == DNSSEC_EOK, MSG_PKCS11 " dnssec_key_import_keystore()"); // construct software public key dnssec_key_t *soft_key = NULL; r = dnssec_key_new(&soft_key); ok(r == DNSSEC_EOK && soft_key != NULL, MSG_SOFTWARE " dnssec_key_new()"); dnssec_binary_t rdata = { 0 }; dnssec_key_get_rdata(p11_key, &rdata); r = dnssec_key_set_rdata(soft_key, &rdata); ok(r == DNSSEC_EOK, MSG_SOFTWARE " dnssec_key_set_rdata()"); *p11_key_ptr = p11_key; *soft_key_ptr = soft_key; }
/*! * Create DNSKEY from parameters. */ static int create_dnskey(const uint8_t *dname, key_params_t *params, dnssec_key_t **key_ptr) { assert(dname); assert(params); assert(key_ptr); int result = key_params_check(params); if (result != DNSSEC_EOK) { return result; } // create key dnssec_key_t *key = NULL; result = dnssec_key_new(&key); if (result != DNSSEC_EOK) { return result; } // set key parameters result = dnssec_key_set_dname(key, dname); if (result != DNSSEC_EOK) { dnssec_key_free(key); return result; } dnssec_key_set_algorithm(key, params->algorithm); uint16_t flags = dnskey_flags(params->is_ksk); dnssec_key_set_flags(key, flags); result = dnssec_key_set_pubkey(key, ¶ms->public_key); if (result != DNSSEC_EOK) { dnssec_key_free(key); return result; } // validate key ID const char *key_id = dnssec_key_get_id(key); if (!key_id) { dnssec_key_free(key); return DNSSEC_INVALID_PUBLIC_KEY; } if (!dnssec_keyid_equal(params->id, key_id)) { dnssec_key_free(key); return DNSSEC_INVALID_KEY_ID; } *key_ptr = key; return DNSSEC_EOK; }
/*! * Generate new key with parameters from KASP policy and add it into zone. */ int generate_key(dnssec_event_ctx_t *ctx, bool ksk, dnssec_kasp_key_t **key_ptr) { assert(ctx); assert(ctx->zone); assert(ctx->keystore); assert(ctx->policy); dnssec_key_algorithm_t algorithm = ctx->policy->algorithm; unsigned size = ksk ? ctx->policy->ksk_size : ctx->policy->zsk_size; // generate key in the keystore char *id = NULL; int r = dnssec_keystore_generate_key(ctx->keystore, algorithm, size, &id); if (r != DNSSEC_EOK) { return r; } // create KASP key dnssec_key_t *dnskey = NULL; r = dnssec_key_new(&dnskey); if (r != DNSSEC_EOK) { free(id); return r; } r = dnssec_key_set_dname(dnskey, ctx->zone->dname); if (r != DNSSEC_EOK) { dnssec_key_free(dnskey); free(id); return r; } dnssec_key_set_flags(dnskey, dnskey_flags(ksk)); dnssec_key_set_algorithm(dnskey, algorithm); r = dnssec_key_import_keystore(dnskey, ctx->keystore, id); if (r != DNSSEC_EOK) { dnssec_key_free(dnskey); free(id); return r; } dnssec_kasp_key_t *key = calloc(1, sizeof(*key)); if (!key) { dnssec_key_free(dnskey); free(id); return DNSSEC_ENOMEM; } key->id = id; key->key = dnskey; key->timing.created = ctx->now; // add into KASP zone dnssec_list_t *keys = dnssec_kasp_zone_get_keys(ctx->zone); dnssec_list_append(keys, key); if (key_ptr) { *key_ptr = key; } return DNSSEC_EOK; }