certsgen.c
5.11 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <stdlib.h>
#include <sha1.h>
#include <PR/bbmetadata.h>
#include <PR/bbcert.h>
#include <openssl/rand.h>
#include <stdlib.h>
#include <bb_nn.h>
#include <util.h>
#include <bbtoolsapi.h>
#define MAX_KEY_SIZE 4096
int main(int argc, char **argv){
RSA *prsa;
unsigned char subjectname[64];
unsigned char issuername[64];
unsigned long certdata[SIZE_RSA_CERTBLOB_WORDS];
char *temp_string;
unsigned char tmp[256];
int i, num_bits, num_bytes, chains;
unsigned long number;
FILE *certptr;
FILE *rsawriteptr;
FILE *rsareadptr;
unsigned char rootfilename[256];
unsigned char keyfilename[256];
unsigned char signerfilename[256];
unsigned char signerfileext[256];
unsigned char keyfileext[256];
FILE *cmdfileptr;
int num_chains;
certptr = fopen("cert.sys", "w");
if(certptr == 0){
fprintf(stderr,"cannot open file to write \n");
}
/* input file name is root key data, use it */
if(argc ==2){
cmdfileptr = fopen(argv[1], "r");
if(cmdfileptr ==0){
fprintf(stderr,"Cannot open cmd file\n");
exit(1);
}
}
else{
printf("have to specify cmd file name \n");
exit(1);
}
fscanf(cmdfileptr,"%s", rootfilename);
prsa = RSA_new();
rsareadptr = fopen(rootfilename, "r");
if(rsareadptr ==0){
fprintf(stderr,"Cannot open root key data file %s\n", rootfilename);
}
readRsaData(rsareadptr, prsa);
if (validate(prsa, 4096) != 0){
fprintf(stderr,"Key validation failed\n");
}
else{
;
#ifdef DEBUG
printf("Key validation OK\n");
#endif
}
fclose(rsareadptr);
free(prsa);
/* read in number of chains */
fscanf(cmdfileptr, "%d\n", &num_chains);
/* write out number of certs in cert.sys
*/
number = htonl((num_chains*2));
fwrite((void *)&number, 4, 1, certptr);
/* generate the two certs for chain and save and validate for each line*/
for(chains =0; chains< num_chains; chains++){
prsa = RSA_new();
num_bits = 2048;
num_bytes = num_bits/8;
prsa = RSA_generate_key(num_bits, 3, NULL, NULL);
/* assign the names */
/* null data first */
fscanf(cmdfileptr, "%s", keyfilename);
sprintf(tmp, "%s.key", keyfilename);
rsawriteptr = fopen(tmp, "w");
for(i = 0; i < 64; i++){
issuername[i] = 0;
subjectname[i] = 0;
}
fscanf(cmdfileptr, "%s", signerfilename);
sprintf(tmp, "Root-%s", signerfilename);
for(i = 0; i< strlen(tmp); i++){
issuername[i] = tmp[i];
}
sprintf(tmp, "%s", keyfilename);
for(i =0; i < strlen(tmp); i++){
subjectname[i] = tmp[i];
}
/* save */
sprintf(tmp, "Root-%s-%s",signerfilename,keyfilename);
saveRsaDataNamed(rsawriteptr, prsa, tmp);
fclose(rsawriteptr);
/* validate */
if (validate(prsa, 2048) != 0){
fprintf(stderr,"Key validation failed\n");
}
else{
;
#ifdef DEBUG
printf("Key validation OK\n");
#endif
}
free(prsa);
/* create cert */
/* read signer name */
#ifdef DEBUG
for( i =0; i< 64; i++){
printf("issuername = %02x\n", issuername[i]);
}
for( i =0; i< 64; i++){
printf("subjectname = %02x\n", subjectname[i]);
}
#endif
/* sign it
*/
/* compute keys for CA for signing CP cert RSA 2048 bits */
prsa = RSA_new();
num_bits = 2048;
num_bytes = num_bits/8;
prsa = RSA_generate_key(num_bits, 3, NULL, NULL);
/* write out data */
sprintf(tmp, "%s.key", signerfilename);
rsawriteptr = fopen(tmp, "w");
saveRsaDataNamed(rsawriteptr, prsa, issuername);
fclose(rsawriteptr);
/* validate */
if (validate(prsa, 2048) != 0){
fprintf(stderr,"Key validation failed\n");
}
else{
;
#ifdef DEBUG
printf("Key validation OK\n");
#endif
}
sprintf(keyfileext, "%s.key", keyfilename);
sprintf(signerfileext, "%s.key", signerfilename);
/* Generate cert data */
/*
generateCertFromKeyData(keyfileext, signerfileext, subjectname, issuername, certdata);
*/
generateCertFromKeyData(keyfileext, signerfileext, 0, 0, certdata);
fwrite((void *)&certdata, 4, SIZE_RSA_CERTBLOB_WORDS, certptr);
/* create custom cert of ca */
for(i = 0; i < 64; i++){
issuername[i] = 0;
subjectname[i] = 0;
}
temp_string = "Root";
for(i = 0; i< strlen(temp_string); i++){
issuername[i] = temp_string[i];
}
sprintf(tmp, "%s", signerfilename);
for(i =0; i < strlen(tmp); i++){
subjectname[i] = tmp[i];
}
/*
generateCertFromKeyData(signerfileext, rootfilename, subjectname, issuername, certdata);
*/
generateCertFromKeyData(signerfileext, rootfilename, 0, 0, certdata);
fwrite((void *)&certdata, 4, SIZE_RSA_CERTBLOB_WORDS, certptr);
free(prsa);
}
fclose(certptr);
fclose(cmdfileptr);
return 0;
}