1 /* $NetBSD: bigkey.c,v 1.8 2023/01/25 21:43:27 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 19 #include <isc/buffer.h> 20 #include <isc/mem.h> 21 #include <isc/platform.h> 22 #include <isc/print.h> 23 #include <isc/region.h> 24 #include <isc/stdio.h> 25 #include <isc/string.h> 26 #include <isc/util.h> 27 28 #define DST_KEY_INTERNAL 29 30 #include <openssl/bn.h> 31 #include <openssl/err.h> 32 #include <openssl/evp.h> 33 #include <openssl/objects.h> 34 #include <openssl/rsa.h> 35 36 #include <dns/dnssec.h> 37 #include <dns/fixedname.h> 38 #include <dns/keyvalues.h> 39 #include <dns/log.h> 40 #include <dns/name.h> 41 #include <dns/rdataclass.h> 42 #include <dns/result.h> 43 #include <dns/secalg.h> 44 45 #include <dst/dst.h> 46 #include <dst/result.h> 47 48 dst_key_t *key; 49 dns_fixedname_t fname; 50 dns_name_t *name; 51 unsigned int bits = 2048U; 52 isc_mem_t *mctx; 53 isc_log_t *log_; 54 isc_logconfig_t *logconfig; 55 int level = ISC_LOG_WARNING; 56 isc_logdestination_t destination; 57 char filename[255]; 58 isc_result_t result; 59 isc_buffer_t buf; 60 RSA *rsa; 61 BIGNUM *e; 62 EVP_PKEY *pkey; 63 64 #define CHECK(op, msg) \ 65 do { \ 66 result = (op); \ 67 if (result != ISC_R_SUCCESS) { \ 68 fprintf(stderr, \ 69 "fatal error: %s returns %s at file %s line " \ 70 "%d\n", \ 71 msg, isc_result_totext(result), __FILE__, \ 72 __LINE__); \ 73 exit(1); \ 74 } \ 75 } while (0) 76 77 int 78 main(int argc, char **argv) { 79 UNUSED(argc); 80 UNUSED(argv); 81 82 #if !USE_PKCS11 83 84 rsa = RSA_new(); 85 e = BN_new(); 86 pkey = EVP_PKEY_new(); 87 88 if ((rsa == NULL) || (e == NULL) || (pkey == NULL) || 89 !EVP_PKEY_set1_RSA(pkey, rsa)) 90 { 91 fprintf(stderr, "fatal error: basic OpenSSL failure\n"); 92 exit(1); 93 } 94 95 /* e = 0x1000000000001 */ 96 BN_set_bit(e, 0); 97 BN_set_bit(e, 48); 98 99 if (RSA_generate_key_ex(rsa, bits, e, NULL)) { 100 BN_free(e); 101 RSA_free(rsa); 102 } else { 103 fprintf(stderr, 104 "fatal error: RSA_generate_key_ex() fails " 105 "at file %s line %d\n", 106 __FILE__, __LINE__); 107 exit(1); 108 } 109 110 dns_result_register(); 111 112 isc_mem_create(&mctx); 113 CHECK(dst_lib_init(mctx, NULL), "dst_lib_init()"); 114 isc_log_create(mctx, &log_, &logconfig); 115 isc_log_setcontext(log_); 116 dns_log_init(log_); 117 dns_log_setcontext(log_); 118 isc_log_settag(logconfig, "bigkey"); 119 120 destination.file.stream = stderr; 121 destination.file.name = NULL; 122 destination.file.versions = ISC_LOG_ROLLNEVER; 123 destination.file.maximum_size = 0; 124 isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC, level, 125 &destination, 126 ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL); 127 128 CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL), "isc_log_" 129 "usechannel(" 130 ")"); 131 name = dns_fixedname_initname(&fname); 132 isc_buffer_constinit(&buf, "example.", strlen("example.")); 133 isc_buffer_add(&buf, strlen("example.")); 134 CHECK(dns_name_fromtext(name, &buf, dns_rootname, 0, NULL), "dns_name_" 135 "fromtext(" 136 "\"example." 137 "\")"); 138 139 CHECK(dst_key_buildinternal(name, DNS_KEYALG_RSASHA256, bits, 140 DNS_KEYOWNER_ZONE, DNS_KEYPROTO_DNSSEC, 141 dns_rdataclass_in, pkey, mctx, &key), 142 "dst_key_buildinternal(...)"); 143 144 CHECK(dst_key_tofile(key, DST_TYPE_PRIVATE | DST_TYPE_PUBLIC, NULL), 145 "dst_key_tofile()"); 146 isc_buffer_init(&buf, filename, sizeof(filename) - 1); 147 isc_buffer_clear(&buf); 148 CHECK(dst_key_buildfilename(key, 0, NULL, &buf), "dst_key_" 149 "buildfilename()"); 150 printf("%s\n", filename); 151 dst_key_free(&key); 152 153 isc_log_destroy(&log_); 154 isc_log_setcontext(NULL); 155 dns_log_setcontext(NULL); 156 dst_lib_destroy(); 157 isc_mem_destroy(&mctx); 158 return (0); 159 #else /* !USE_PKCS11 */ 160 return (1); 161 #endif /* !USE_PKC11 */ 162 } 163 164 /*! \file */ 165