Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[84adc4]: testbed / src / create_manifest.c Maximize Restore History

Download this file

create_manifest.c    252 lines (220 with data), 6.8 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#include "cryptlib.h"
#include "../asn/certificate.h"
#include "../asn/roa.h"
#include <keyfile.h>
#include <casn.h>
#include <asn.h>
#include <time.h>
#include "create_object.h"
#include "obj_err.h"
//#include "create_utils.h"
#include <sys/types.h>
#include <dirent.h>
#include "cryptlib.h"
/**
*
* This file is designed to provide functions to create a manifest
* from a template.
*
**/
void print_table(struct object_field *table);
int write_EEcert(void* my_var, void* value);
int write_EEkey(void* my_var, void* value);
char *man_template = "../templates/M.man";
/**
* Write the unique identifier for this manifest number
*
*
**/
int write_manNum(void* man, void* value)
{
//first I cast the generic parameters, then I grab the manifest part of the
// CMS object, then I parse the value parameter into a long and then I write
// it into the struct.
struct ROA* roa = man;
struct Manifest *manp = &roa->content.signedData.encapContentInfo.eContent.manifest;
long manNum = strtol(((char*)value), NULL, 0);
write_casn_num( &manp->manifestNumber, manNum);
return SUCCESS;
}
/*
* Write out the thisUpdate date
*/
int write_thisUpdate(void* man, void* value)
{
//Same idea here. Cast the generic parameters, grab the manifest part of the
// CMS object, clear out the old value and then put in the new one.
struct ROA* roa = man;
if (value == NULL)
return -1;
struct Manifest *manp = &roa->content.signedData.encapContentInfo.eContent.manifest;
clear_casn(&manp->thisUpdate);
if(write_casn(&manp->thisUpdate,(uchar *)value,strlen(value)) < 0)
return -1;
return SUCCESS;
}
/*
* Write out the thisUpdate date
*/
int write_nextUpdate(void* man, void* value)
{
// Same as the thisUpdate function above.
struct ROA* roa = man;
if (value == NULL)
return -1;
struct Manifest *manp = &roa->content.signedData.encapContentInfo.eContent.manifest;
clear_casn(&manp->nextUpdate);
if(write_casn(&manp->nextUpdate,(uchar *)value,strlen(value)) < 0)
return -1;
return SUCCESS;
}
/**
* write_MAN_casn_fileList
* params: two generic pointers, this function should know how handle them
*
* This function fills in the manifest file list with file names and hashes
*
**/
int write_fileList(void* man, void* value)
{
//cast the parameter into the correct struct
struct ROA* roa = man;
char* filesAndHashes;
char* buf;
char* token = ",";
//Get the manifest pointer
struct Manifest *manp = &roa->content.signedData.encapContentInfo.eContent.manifest;
struct FileAndHash* fahp;
if(value == NULL)
return -1;
//copy the value into a local copy of the buffer
filesAndHashes = calloc(strlen((char*)value) + 1, sizeof(char));
memcpy(filesAndHashes, (char*)value, strlen( (char*)value));
buf = NULL;
//Clear the original fileList from the template
clear_casn(&manp->fileList.self);
//tokenize the ',' separated list.
buf=strtok(filesAndHashes,token);
int num = 0;
while(buf != NULL)
{
// this is for safety. while using the strtok function
// it's best to work on a copy of the tokenized item
char* testBuf = calloc(strlen(buf) + 1, sizeof(char));
memcpy(testBuf, buf,strlen(buf));
int fileNameLen;
char* hash = NULL;
//Set up two char lengths
hash = strchr(testBuf,'%');
fileNameLen = (char*)hash - (char*)testBuf;
hash++;
//Build the hash bits
uchar* hashBits = NULL;
hashBits = calloc( (strlen(hash)/2)+1, sizeof(char));
//get a null terminated filename to copy into the casn
char* fileName = calloc(fileNameLen+1,sizeof(char));
memcpy(fileName,testBuf,fileNameLen);
//Use a function in create_utils.c to read the hex value from
// the string.
if( read_hex_val(hash, strlen(hash), &hashBits[1]) > 0 )
{
if(fileName != NULL && hashBits != NULL)
{
//Use the ASN library functions to insert a new file in the fileList
// structure
if (!(fahp = (struct FileAndHash *)inject_casn(&manp->fileList.self, num)))
warn(3, "fileList");
write_casn(&fahp->file, (uchar *)fileName, strlen(fileName));
write_casn(&fahp->hash, hashBits, (strlen(hash)/2));
}
num++;
}
buf=strtok(NULL,",");
free(testBuf);
free(fileName);
}
return SUCCESS;
}
// This is the generic table that describes all input
// options to a manifest
struct object_field manifest_field_table[] =
{
{"outputfilename", TEXT, NULL, REQUIRED, NULL}, // output filename for the manifest
{"manNum",INTEGER, 0, OPTIONAL, write_manNum}, // sequence number
{"thisUpdate", TEXT, NULL,OPTIONAL, write_thisUpdate},
{"nextUpdate", TEXT, NULL,OPTIONAL, write_nextUpdate},
{"fileList", TEXT, NULL,OPTIONAL, write_fileList},
{"EECertLocation",TEXT, NULL,OPTIONAL, write_EEcert},
{"EEKeyLocation",TEXT, NULL,OPTIONAL, write_EEkey},
{NULL,TEXT,NULL,OPTIONAL,NULL}
};
/**
* Accessor for the above manifest table.
*
*/
struct object_field *get_man_field_table()
{
return manifest_field_table;
}
/**
* create_manifest is the main function call to start the process of
* creating a manifest file
*
* Params: type -
* table - the filled in object_field that we should fill the new file with
* Returns:
**/
int create_manifest(struct object_field *table)
{
struct ROA roa;
struct FileListInManifest *f;
//struct CMSAlgorithmIdentifier *algidp;
ROA(&roa, 0);
if (!templateFile) {
templateFile = man_template;
}
// Read the manifest template into this manifest
if (get_casn_file(&roa.self, (char*)templateFile, 0) < 0)
{
warn(FILE_OPEN_ERR, (char*)templateFile);
return(FILE_OPEN_ERR);
}
// Remove existing manifest file list.
f = &roa.content.signedData.encapContentInfo.eContent.manifest.fileList;
eject_all_casn(&f->self);
//Setup the outerlying CMS structure
int i = 0;
//Populate the manifest information for everything that has a function pointer stored in it
for(i=0; table[i].name != NULL; i++)
{
if(table[i].func != NULL)
{
if (table[i].value != NULL)
{
//This is useful to allow us to have a loop over all
// of the manifest table fields. This is an extremely
// generic call to a function pointer in the table
if(table[i].func(&roa,table[i].value) != SUCCESS)
{
return -1;
}
}
}
}
//write the new file out
char* fileName = table[0].value;
if (put_casn_file(&roa.self, fileName, 0) < SUCCESS)
{
printf("fail\n");
return -1;
}
return SUCCESS;
}