File: curvedns-keygen.c

package info (click to toggle)
curvedns 0.87-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,964 kB
  • sloc: asm: 19,669; ansic: 11,907; cpp: 1,066; sh: 954; makefile: 57
file content (164 lines) | stat: -rw-r--r-- 4,913 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* 
 * Copyright 2010 CurveDNS Project. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this list of
 *      conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY CurveDNS Project ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CurveDNS Project OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of CurveDNS Project.
 * 
 */

/*
 * $Id$ 
 * $Author$
 * $Date$
 * $Revision$
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "sodium.h"
#include "debug.h"
#include "misc.h"

extern int global_urandom_fd;

char hexpublic[65], hexprivate[65];
uint8_t public[32], private[32], dnspublic[55];

// Implicitly called by crypto_box_keypair, urandom fd is file descriptor of /dev/urandom
// Opening etc. is handled by misc_crypto_random_init()
void randombytes(unsigned char *x, unsigned long long xlen) {
	int i;

	while (xlen > 0) {
		if (xlen < 1048576) i = xlen; else i = 1048576;

		i = read(global_urandom_fd, x, i);
		if (i < 1) {
			sleep(1);
			continue;
		}

		x += i;
		xlen -= i;
	}
}

int curvedns_env(char *path, char *name) {
	char fullname[256], fullpath[256];
	FILE *f;
	struct stat st;

	if (strlen(name) > 200) {
		fprintf(stderr, "Authoritative name server name too long.\n");
		return 1;
	}
	if (snprintf(fullname, sizeof(fullname), "%s.%s", dnspublic, name) < 0) return 1;

	if (snprintf(fullpath, sizeof(fullpath), "%s/env", path) < 0) return 1;
	if (stat(fullpath, &st) < 0) {
		if (errno != ENOENT) return 1;
		mkdir(fullpath, 0700);
	} else {
		if (!S_ISDIR(st.st_mode)) {
			fprintf(stderr, "%s is not a directory, manually remove this first\n", fullpath);
			return 1;
		}
	}

	if (snprintf(fullpath, sizeof(fullpath), "%s/env/CURVEDNS_PRIVATE_KEY", path) < 0) return 1;
	if (stat(fullpath, &st) == 0) {
		fprintf(stderr, "A private key file already exists, manually remove that first.\n");
		return 1;
	}
	f = fopen(fullpath, "w");
	if (!f) {
		fprintf(stderr, "Unable to open %s for writing.\n", fullpath);
		return 1;
	}
	fprintf(f, "%s\n", hexprivate);
	fclose(f);
	if (chmod(fullpath, 0400) != 0) return 1;

	printf("Authoritative name server name:\n%s\n", fullname);
	printf("DNS public key:\n%s\n", dnspublic);
	printf("Hex public key:\n%s\n", hexpublic);
	printf("Hex secret key:\n%s\n", hexprivate);
	printf("\n");
	printf("The private key was written to %s, so it can be used inside the CurveDNS environment.\n", fullpath);

	return 0;
}

int main(int argc, char *argv[]) {
	unsigned dnspublic_len = sizeof(dnspublic) - 3;

	if (!misc_crypto_random_init()) {
		debug_log(DEBUG_FATAL, "unable to ensure randomness\n");
		return 1;
	}
	
	// Generate the actual keypair:
	crypto_box_curve25519xsalsa20poly1305_keypair(public, private);

	// The DNSCurve (base32)-encoding of the PUBLIC key:
	memcpy(dnspublic, "uz5", 3);
	if (!misc_base32_encode(dnspublic + 3, &dnspublic_len, public, 32)) {
		perror("base32_encode");
		return 1;
	}
	
	// The hex encoding of the PUBLIC key:
	if (!misc_hex_encode(public, 32, hexpublic, 64)) {
		perror("hex_encode");
		return 1;
	}
	
	// The hex encoding of the PRIVATE key:
	if (!misc_hex_encode(private, 32, hexprivate, 64)) {
		perror("hex_encode");
		return 1;
	}
	
	dnspublic[54] = 0;
	hexpublic[64] = 0;
	hexprivate[64] = 0;
	
	if (argc == 1) {
		printf("DNS public key:\t%s\n", dnspublic);
		printf("Hex public key:\t%s\n", hexpublic);
		printf("Hex secret key:\t%s\n", hexprivate);
	} else if (argc != 3) {
		fprintf(stderr, "Usage: %s <path of CurveDNS installation> <authoritative name server name>\n", argv[0]);
		return 1;
	} else {
		return curvedns_env(argv[1], argv[2]);
	}

	return 0;
}