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
38 #include <openssl/rsa.h>
39 #include <openssl/pem.h>
42 * Should be run as root. Creates /etc/hammer2/rsa.{pub,prv} using
46 cmd_rsainit(const char *dir_path)
56 * Create the directory if necessary
58 if (stat(dir_path, &st) < 0) {
59 str1 = strdup(dir_path);
62 while ((str2 = strchr(str2 + 1, '/')) != NULL) {
70 asprintf(&str1, "%s/rsa.prv", dir_path);
71 asprintf(&str2, "%s/rsa.pub", dir_path);
73 if (stat(str1, &st) < 0) {
74 old_umask = umask(077);
75 asprintf(&cmd, "openssl genrsa -out %s 2048", str1);
82 "hammer2 rsainit: private key gen failed\n");
87 printf("hammer2 rsainit: created %s\n", str1);
90 printf("hammer2 rsainit: Using existing private key in %s\n",
93 if (stat(str2, &st) < 0) {
94 asprintf(&cmd, "openssl rsa -in %s -out %s -pubout",
100 "hammer2 rsainit: public key gen failed\n");
105 printf("hammer2 rsainit: created %s\n", str2);
107 printf("hammer2 rsainit: both keys already exist\n");
116 cmd_rsaenc(const char **keyfiles, int nkeys)
118 RSA **keys = calloc(nkeys, sizeof(RSA *));
119 int *ispub = calloc(nkeys, sizeof(int));
125 unsigned char *data_in;
126 unsigned char *data_out;
128 for (i = 0; i < nkeys; ++i) {
132 sfx = strrchr(keyfiles[i], '.');
133 if (sfx && strcmp(sfx, ".pub") == 0) {
134 fp = fopen(keyfiles[i], "r");
136 fprintf(stderr, "hammer2 rsaenc: unable to "
137 "open %s\n", keyfiles[i]);
141 keys[i] = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
144 if (keys[i] == NULL) {
145 fprintf(stderr, "hammer2 rsaenc: unable to "
146 "parse public key from %s\n",
151 } else if (sfx && strcmp(sfx, ".prv") == 0) {
152 fp = fopen(keyfiles[i], "r");
154 fprintf(stderr, "hammer2 rsaenc: unable to "
155 "open %s\n", keyfiles[i]);
159 keys[i] = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
161 if (keys[i] == NULL) {
162 fprintf(stderr, "hammer2 rsaenc: unable to "
163 "parse private key from %s\n",
169 fprintf(stderr, "hammer2: rsaenc: key files must end "
170 "in .pub or .prv\n");
175 blksize = RSA_size(keys[i]);
177 assert(blksize == RSA_size(keys[i]));
179 fprintf(stderr, "blksize %d\n", blksize);
184 data_in = malloc(blksize);
185 data_out = malloc(blksize);
187 while ((n = read(0, data_in + off, blksize - off)) > 0) {
189 if (off == blksize) {
190 for (i = 0; i < nkeys; ++i) {
192 RSA_public_encrypt(blksize,
197 RSA_private_encrypt(blksize,
202 bcopy(data_out, data_in, blksize);
204 if (write(1, data_out, blksize) != blksize) {
212 if (off && ecode == 0) {
214 bzero(data_in + off, blksize - off);
215 for (i = 0; i < nkeys; ++i) {
217 RSA_public_encrypt(blksize,
222 RSA_private_encrypt(blksize,
227 bcopy(data_out, data_in, blksize);
229 if (write(1, data_out, blksize) != blksize) {
241 for (i = 0; i < nkeys; ++i) {
251 cmd_rsadec(const char **keyfiles, int nkeys)
253 RSA **keys = calloc(nkeys, sizeof(RSA *));
254 int *ispub = calloc(nkeys, sizeof(int));
260 unsigned char *data_in;
261 unsigned char *data_out;
263 for (i = 0; i < nkeys; ++i) {
267 sfx = strrchr(keyfiles[i], '.');
268 if (sfx && strcmp(sfx, ".pub") == 0) {
269 fp = fopen(keyfiles[i], "r");
271 fprintf(stderr, "hammer2 rsaenc: unable to "
272 "open %s\n", keyfiles[i]);
276 keys[i] = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
279 if (keys[i] == NULL) {
280 fprintf(stderr, "hammer2 rsaenc: unable to "
281 "parse public key from %s\n",
286 } else if (sfx && strcmp(sfx, ".prv") == 0) {
287 fp = fopen(keyfiles[i], "r");
289 fprintf(stderr, "hammer2 rsaenc: unable to "
290 "open %s\n", keyfiles[i]);
294 keys[i] = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
296 if (keys[i] == NULL) {
297 fprintf(stderr, "hammer2 rsaenc: unable to "
298 "parse private key from %s\n",
304 fprintf(stderr, "hammer2: rsaenc: key files must end "
305 "in .pub or .prv\n");
310 blksize = RSA_size(keys[i]);
312 assert(blksize == RSA_size(keys[i]));
318 data_in = malloc(blksize);
319 data_out = malloc(blksize);
321 while ((n = read(0, data_in + off, blksize - off)) > 0) {
323 if (off == blksize) {
324 for (i = 0; i < nkeys; ++i) {
326 RSA_public_decrypt(blksize,
331 RSA_private_decrypt(blksize,
336 bcopy(data_out, data_in, blksize);
338 if (write(1, data_out, blksize) != blksize) {
348 bzero(data_in + off, blksize - off);
349 for (i = 0; i < nkeys; ++i) {
351 RSA_public_decrypt(blksize,
356 RSA_private_decrypt(blksize,
361 bcopy(data_out, data_in, blksize);
363 if (write(1, data_out, blksize) != blksize) {
375 for (i = 0; i < nkeys; ++i) {