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
|
/**
* orkeygen.c
* Key generation utility.
*
* Matej Pfajfar <mp292@cam.ac.uk>
*/
/*
* Changes :
* $Log$
* Revision 1.1 2002/06/26 22:45:50 arma
* Initial revision
*
* Revision 1.1 2002/01/04 07:19:27 badbytes
* Key generation utility.
*
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/err.h>
#include <openssl/pem.h>
int main(int argc, char *argv[])
{
char *file_pr = argv[1];
char *file_pu = argv[2];
FILE *f_pr = NULL;
FILE *f_pu = NULL;
RSA *rsa_key = NULL;
int retval = 0;
if (argc < 3)
{
printf("Need two files, for private and public key in that order.\n");
exit(1);
}
/* generate the key */
rsa_key = RSA_generate_key(1024,65535,NULL,NULL);
if (!rsa_key) /* error has occured */
{
printf("%s",ERR_reason_error_string(ERR_get_error()));
exit(1);
}
else /* keys generated */
{
retval = RSA_check_key(rsa_key);
if (retval == 1)
{
printf("Generated key seems to be valid.\n");
/* open the output files */
f_pr = fopen(file_pr,"w");
if (!f_pr)
{
perror("fopen");
RSA_free(rsa_key);
exit(1);
}
f_pu = fopen(file_pu,"w");
if (!f_pu)
{
perror("fopen");
RSA_free(rsa_key);
exit(1);
}
/* write the private key */
retval = PEM_write_RSAPrivateKey(f_pr,rsa_key,NULL,NULL,0,0,NULL);
if (retval == 0)
{
printf("%s",ERR_reason_error_string(ERR_get_error()));
fclose(f_pr);
fclose(f_pu);
RSA_free(rsa_key);
exit(1);
}
/* write the public key */
retval = PEM_write_RSAPublicKey(f_pu,rsa_key);
if (retval == 0)
{
printf("%s",ERR_reason_error_string(ERR_get_error()));
fclose(f_pr);
fclose(f_pu);
RSA_free(rsa_key);
exit(1);
}
printf("Keys written to files %s (public) and %s (private).\n",file_pu,file_pr);
}
else if (retval == 0)
{
printf("Generated key seems to be invalid. Exiting.\n");
RSA_free(rsa_key);
exit(1);
}
else if (retval == -1)
{
printf("%s",ERR_reason_error_string(ERR_get_error()));
RSA_free(rsa_key);
exit(1);
}
}
RSA_free(rsa_key);
fclose(f_pu);
fclose(f_pr);
exit(0);
}
|