]> andersk Git - moira.git/blame - reg_svr/genkey.c
Command line printer manipulation client, and build goo.
[moira.git] / reg_svr / genkey.c
CommitLineData
b50f996d 1/* $Id$
2 *
3 * Utility program to generate a public/private key pair
4 *
5 * Copyright (C) 1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 *
9 */
10
11#include <mit-copyright.h>
12#include <moira.h>
13#include "reg_svr.h"
14
15#include <sys/param.h>
16
17#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include <com_err.h>
23
24/* RSARef includes */
25#include "global.h"
26#include "rsaref.h"
27
28RCSID("$Header$");
29
30void printhex(FILE *out, unsigned char *buf, int len);
31
32int main(int argc, char **argv)
33{
34 R_RSA_PRIVATE_KEY private;
35 R_RSA_PUBLIC_KEY public;
36 R_RSA_PROTO_KEY proto;
37 R_RANDOM_STRUCT random;
38 FILE *in, *out;
39 int needed;
40 unsigned char *buf;
41 char pubname[MAXPATHLEN], oldname[MAXPATHLEN];
42 char *whoami = argv[0], *inname = argv[1], *outname = argv[2];
43
44 if (strchr(whoami, '/'))
45 whoami = strrchr(whoami, '/');
46
47 if (argc != 3)
48 {
49 com_err(NULL, 0, "Usage: %s infile outfile\n"
50 "where infile is a file of random data", whoami);
51 exit(1);
52 }
53 in = fopen(inname, "r");
54 if (!in)
55 {
56 com_err(whoami, errno, "trying to open %s", inname);
57 exit(1);
58 }
59
60 R_RandomInit(&random);
61 R_GetRandomBytesNeeded(&needed, &random);
62 buf = malloc(needed);
63 if (fread(buf, needed, 1, in) != 1)
64 {
65 com_err(whoami, 0, "Not enough random input data: need %d bytes\n",
66 needed);
67 exit(1);
68 }
69 R_RandomUpdate(&random, buf, needed);
70
71 proto.bits = 1024;
72 proto.useFermat4 = 1;
73
74 if (R_GeneratePEMKeys(&public, &private, &proto, &random))
75 {
76 com_err(whoami, 0, "Couldn't generate key");
77 exit(1);
78 }
79
80 sprintf(oldname, "%s.old", outname);
81 rename(outname, oldname);
82 out = fopen(outname, "w");
83 if (!out)
84 {
85 com_err(whoami, errno, "opening %s", outname);
86 exit(1);
87 }
88 if (fwrite(&private, sizeof(private), 1, out) != 1)
89 {
90 com_err(whoami, errno, "writing %s", outname);
91 exit(1);
92 }
93 fclose(out);
94
95 sprintf(pubname, "%s.pub", outname);
96 sprintf(oldname, "%s.old", pubname);
97 rename(pubname, oldname);
98 out = fopen(pubname, "w");
99 if (!out)
100 {
101 com_err(whoami, errno, "opening %s", pubname);
102 exit(1);
103 }
104 if (fwrite(&public, sizeof(public), 1, out) != 1)
105 {
106 com_err(whoami, errno, "writing %s", pubname);
107 exit(1);
108 }
109 fclose(out);
110
111 sprintf(pubname, "%s.pub.txt", outname);
112 out = fopen(pubname, "w");
113 if (!out)
114 {
115 com_err(whoami, errno, "opening %s", pubname);
116 exit(1);
117 }
118 printhex(out, public.modulus, MAX_RSA_MODULUS_LEN);
119 fclose(out);
120
121 exit(0);
122}
123
124char hexd[] = { '0', '1', '2', '3', '4', '5', '6', '7',
125 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
126
127void printhex(FILE *out, unsigned char *buf, int len)
128{
129 while (len--)
130 {
131 fprintf(out, "%c%c", hexd[*buf>>4], hexd[*buf%0x10]);
132 buf++;
133 }
134}
This page took 0.074828 seconds and 5 git commands to generate.