2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * Should be run as root. Creates /etc/hammer2/rsa.{pub,prv} using
43 cmd_rsainit(const char *dir_path)
53 * Create the directory if necessary
55 if (stat(dir_path, &st) < 0) {
56 str1 = strdup(dir_path);
59 while ((str2 = strchr(str2 + 1, '/')) != NULL) {
67 asprintf(&str1, "%s/rsa.prv", dir_path);
68 asprintf(&str2, "%s/rsa.pub", dir_path);
70 if (stat(str1, &st) < 0) {
71 old_umask = umask(077);
72 asprintf(&cmd, "openssl genrsa -out %s 2048", str1);
79 "hammer2 rsainit: private key gen failed\n");
84 printf("hammer2 rsainit: created %s\n", str1);
87 printf("hammer2 rsainit: Using existing private key in %s\n",
90 if (stat(str2, &st) < 0) {
91 asprintf(&cmd, "openssl rsa -in %s -out %s -pubout",
97 "hammer2 rsainit: public key gen failed\n");
102 printf("hammer2 rsainit: created %s\n", str2);
104 printf("hammer2 rsainit: both keys already exist\n");
113 cmd_rsaenc(const char **keyfiles, int nkeys)
115 RSA **keys = calloc(nkeys, sizeof(RSA *));
116 int *ispub = calloc(nkeys, sizeof(int));
122 unsigned char *data_in;
123 unsigned char *data_out;
125 for (i = 0; i < nkeys; ++i) {
129 sfx = strrchr(keyfiles[i], '.');
130 if (sfx && strcmp(sfx, ".pub") == 0) {
131 fp = fopen(keyfiles[i], "r");
133 fprintf(stderr, "hammer2 rsaenc: unable to "
134 "open %s\n", keyfiles[i]);
138 keys[i] = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
141 if (keys[i] == NULL) {
142 fprintf(stderr, "hammer2 rsaenc: unable to "
143 "parse public key from %s\n",
148 } else if (sfx && strcmp(sfx, ".prv") == 0) {
149 fp = fopen(keyfiles[i], "r");
151 fprintf(stderr, "hammer2 rsaenc: unable to "
152 "open %s\n", keyfiles[i]);
156 keys[i] = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
158 if (keys[i] == NULL) {
159 fprintf(stderr, "hammer2 rsaenc: unable to "
160 "parse private key from %s\n",
166 fprintf(stderr, "hammer2: rsaenc: key files must end "
167 "in .pub or .prv\n");
172 blksize = RSA_size(keys[i]);
174 assert(blksize == RSA_size(keys[i]));
176 fprintf(stderr, "blksize %d\n", blksize);
181 data_in = malloc(blksize);
182 data_out = malloc(blksize);
184 while ((n = read(0, data_in + off, blksize - off)) > 0) {
186 if (off == blksize) {
187 for (i = 0; i < nkeys; ++i) {
189 RSA_public_encrypt(blksize,
194 RSA_private_encrypt(blksize,
199 bcopy(data_out, data_in, blksize);
201 if (write(1, data_out, blksize) != blksize) {
209 if (off && ecode == 0) {
211 bzero(data_in + off, blksize - off);
212 for (i = 0; i < nkeys; ++i) {
214 RSA_public_encrypt(blksize,
219 RSA_private_encrypt(blksize,
224 bcopy(data_out, data_in, blksize);
226 if (write(1, data_out, blksize) != blksize) {
238 for (i = 0; i < nkeys; ++i) {
248 cmd_rsadec(const char **keyfiles, int nkeys)
250 RSA **keys = calloc(nkeys, sizeof(RSA *));
251 int *ispub = calloc(nkeys, sizeof(int));
257 unsigned char *data_in;
258 unsigned char *data_out;
260 for (i = 0; i < nkeys; ++i) {
264 sfx = strrchr(keyfiles[i], '.');
265 if (sfx && strcmp(sfx, ".pub") == 0) {
266 fp = fopen(keyfiles[i], "r");
268 fprintf(stderr, "hammer2 rsaenc: unable to "
269 "open %s\n", keyfiles[i]);
273 keys[i] = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
276 if (keys[i] == NULL) {
277 fprintf(stderr, "hammer2 rsaenc: unable to "
278 "parse public key from %s\n",
283 } else if (sfx && strcmp(sfx, ".prv") == 0) {
284 fp = fopen(keyfiles[i], "r");
286 fprintf(stderr, "hammer2 rsaenc: unable to "
287 "open %s\n", keyfiles[i]);
291 keys[i] = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
293 if (keys[i] == NULL) {
294 fprintf(stderr, "hammer2 rsaenc: unable to "
295 "parse private key from %s\n",
301 fprintf(stderr, "hammer2: rsaenc: key files must end "
302 "in .pub or .prv\n");
307 blksize = RSA_size(keys[i]);
309 assert(blksize == RSA_size(keys[i]));
315 data_in = malloc(blksize);
316 data_out = malloc(blksize);
318 while ((n = read(0, data_in + off, blksize - off)) > 0) {
320 if (off == blksize) {
321 for (i = 0; i < nkeys; ++i) {
323 RSA_public_decrypt(blksize,
328 RSA_private_decrypt(blksize,
333 bcopy(data_out, data_in, blksize);
335 if (write(1, data_out, blksize) != blksize) {
345 bzero(data_in + off, blksize - off);
346 for (i = 0; i < nkeys; ++i) {
348 RSA_public_decrypt(blksize,
353 RSA_private_decrypt(blksize,
358 bcopy(data_out, data_in, blksize);
360 if (write(1, data_out, blksize) != blksize) {
372 for (i = 0; i < nkeys; ++i) {