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