1 
2 
3 /********************************************************************************************
4 *   KEY PAIR WITH PROOF
5 ********************************************************************************************/
6 
7 int
save_KEY_PAIR_WITH_PROOF(FILE * file,KEY_PAIR_WITH_PROOF_internal * key_pair_with_proof)8 save_KEY_PAIR_WITH_PROOF(FILE *file,
9                          KEY_PAIR_WITH_PROOF_internal *key_pair_with_proof)
10 {
11         save_DAA_PK_internal( file,  key_pair_with_proof->pk);
12         save_DAA_PRIVATE_KEY( file, key_pair_with_proof->private_key);
13         save_DAA_PK_PROOF_internal( file, key_pair_with_proof->proof);
14 
15         return 0;
16 }
17 
18 KEY_PAIR_WITH_PROOF_internal *
load_KEY_PAIR_WITH_PROOF(FILE * file)19 load_KEY_PAIR_WITH_PROOF(FILE *file)
20 {
21         KEY_PAIR_WITH_PROOF_internal *key_pair_with_proof =
22                 (KEY_PAIR_WITH_PROOF_internal *)malloc(sizeof(KEY_PAIR_WITH_PROOF_internal));
23 
24         key_pair_with_proof->pk = load_DAA_PK_internal(file);
25         key_pair_with_proof->private_key = load_DAA_PRIVATE_KEY(file);
26         key_pair_with_proof->proof = load_DAA_PK_PROOF_internal(file);
27 
28         return key_pair_with_proof;
29 }
30 
31 int
save_DAA_PK_internal(FILE * file,const TSS_DAA_PK_internal * pk_internal)32 save_DAA_PK_internal(FILE *file, const TSS_DAA_PK_internal *pk_internal)
33 {
34         char *buffer;
35 
36         LogDebug("-> save_DAA_PK_internal");
37 
38         BI_SAVE( pk_internal->modulus, file);
39         BI_SAVE( pk_internal->capitalS, file);
40         BI_SAVE( pk_internal->capitalZ, file);
41         BI_SAVE( pk_internal->capitalR0, file);
42         BI_SAVE( pk_internal->capitalR1, file);
43         BI_SAVE( pk_internal->gamma, file);
44         BI_SAVE( pk_internal->capitalGamma, file);
45         BI_SAVE( pk_internal->rho, file);
46         BI_SAVE_ARRAY( pk_internal->capitalRReceiver, file);
47         BI_SAVE_ARRAY( pk_internal->capitalRIssuer, file);
48         fprintf( file, "%d\n", pk_internal->issuerBaseNameLength);
49         buffer = (char *)malloc( pk_internal->issuerBaseNameLength + 1);
50         memcpy( buffer, pk_internal->issuerBaseName, pk_internal->issuerBaseNameLength);
51         buffer[ pk_internal->issuerBaseNameLength] = 0;
52         fprintf( file, "%s\n", buffer);
53         free( buffer);
54 
55         LogDebug("<- save_DAA_PK_internal");
56 
57         return 0;
58 }
59 
60 TSS_DAA_PK_internal *
load_DAA_PK_internal(FILE * file)61 load_DAA_PK_internal(FILE *file)
62 {
63         TSS_DAA_PK_internal *pk_internal =
64                 (TSS_DAA_PK_internal *)malloc(sizeof(TSS_DAA_PK_internal));
65         char *read_buffer;
66 
67         pk_internal->modulus = bi_new_ptr();
68         BI_LOAD( pk_internal->modulus, file);
69         pk_internal->capitalS = bi_new_ptr();
70         BI_LOAD( pk_internal->capitalS, file);
71         pk_internal->capitalZ = bi_new_ptr();
72         BI_LOAD( pk_internal->capitalZ, file);
73         pk_internal->capitalR0 = bi_new_ptr();
74         BI_LOAD( pk_internal->capitalR0, file);
75         pk_internal->capitalR1 = bi_new_ptr();
76         BI_LOAD( pk_internal->capitalR1, file);
77         pk_internal->gamma = bi_new_ptr();
78         BI_LOAD( pk_internal->gamma, file);
79         pk_internal->capitalGamma = bi_new_ptr();
80         BI_LOAD( pk_internal->capitalGamma, file);
81         pk_internal->rho = bi_new_ptr();
82         BI_LOAD( pk_internal->rho, file);
83         pk_internal->capitalRReceiver = ALLOC_BI_ARRAY();
84         BI_LOAD_ARRAY( pk_internal->capitalRReceiver, file);
85         pk_internal->capitalRIssuer = ALLOC_BI_ARRAY();
86         BI_LOAD_ARRAY( pk_internal->capitalRIssuer, file);
87         pk_internal->capitalY = ALLOC_BI_ARRAY();
88         populate_capitalY( pk_internal);
89         pk_internal->issuerBaseNameLength = read_int( file);
90         read_buffer = read_str( file);
91         pk_internal->issuerBaseName = malloc( pk_internal->issuerBaseNameLength);
92         memcpy( pk_internal->issuerBaseName, read_buffer, pk_internal->issuerBaseNameLength);
93         compute_capitalSprime( pk_internal);
94         return pk_internal;
95 }
96 
97 int
save_DAA_PK_PROOF_internal(FILE * file,TSS_DAA_PK_PROOF_internal * proof)98 save_DAA_PK_PROOF_internal(FILE *file, TSS_DAA_PK_PROOF_internal *proof)
99 {
100         int i;
101 
102 #ifdef DAA_DEBUG
103         printf("save_DAA_PK_PROOF_internal");
104 #endif
105         fprintf(file, "%d # %s.length\n", proof->length_challenge, "challenge");
106         fprintf(file, "%s\n", dump_byte_array( proof->length_challenge,
107                 proof->challenge));
108         fprintf(file, "%d # %s.length\n", proof->length_response, "response");
109         for (i = 0; i < proof->length_response; i++) {
110                 BI_SAVE_ARRAY( proof->response[i], file);
111         }
112 
113         return 0;
114 }
115 
116 /* load <proof> using <filename> */
117 /* allocation of: */
118 /*              proof->challenge (BYTE*) */
119 /*              response (bi_array_ptr) */
120 TSS_DAA_PK_PROOF_internal *
load_DAA_PK_PROOF_internal(FILE * file)121 load_DAA_PK_PROOF_internal(FILE *file)
122 {
123         TSS_DAA_PK_PROOF_internal *proof =
124                 (TSS_DAA_PK_PROOF_internal *)malloc(sizeof(TSS_DAA_PK_PROOF_internal));
125         char *read_buffer;
126         int  i;
127 
128 #ifdef DAA_DEBUG
129         printf("load_DAA_PK_PROOF_internal");
130 #endif
131         proof->length_challenge = read_int( file);
132         read_buffer = read_str( file);
133         proof->challenge = retrieve_byte_array( &(proof->length_challenge),read_buffer);
134         proof->length_response = read_int( file);
135         proof->response = (bi_array_ptr *)malloc( sizeof(bi_array_ptr) * proof->length_response);
136         for (i = 0; i < proof->length_response; i++) {
137                 proof->response[i] = ALLOC_BI_ARRAY();
138                 BI_LOAD_ARRAY( proof->response[i], file);
139         }
140         return proof;
141 }
142 
143 TSS_DAA_CRED_ISSUER *
load_TSS_DAA_CRED_ISSUER(FILE * file)144 load_TSS_DAA_CRED_ISSUER(FILE *file)
145 {
146         TSS_DAA_CRED_ISSUER *credential =
147                 (TSS_DAA_CRED_ISSUER *)malloc(sizeof(TSS_DAA_CRED_ISSUER));
148         char *read_buffer;
149         int  i, len;
150 
151         init_tss_version( credential);
152         credential->capitalALength = read_int( file);
153         read_buffer = read_str( file);
154         credential->capitalA = retrieve_byte_array( &(credential->capitalALength),
155                                                 read_buffer);
156         credential->eLength = read_int( file);
157         read_buffer = read_str( file);
158         credential->e = retrieve_byte_array( &(credential->eLength),read_buffer);
159         credential->vPrimePrimeLength = read_int( file);
160         read_buffer = read_str( file);
161         credential->vPrimePrime = retrieve_byte_array(&(credential->vPrimePrimeLength),
162                                                         read_buffer);
163         // attributes issuer
164         credential->attributesIssuerLength = read_int( file);
165         credential->attributesIssuer = malloc(credential->attributesIssuerLength*sizeof(BYTE*));
166         for( i=0; i < (int)credential->attributesIssuerLength; i++) {
167                 credential->attributesIssuer[i] = retrieve_byte_array( &len, read_buffer);
168         }
169         credential->cPrimeLength = read_int( file);
170         read_buffer = read_str( file);
171         credential->cPrime = retrieve_byte_array( &(credential->cPrimeLength),read_buffer);
172         credential->sELength = read_int( file);
173         read_buffer = read_str( file);
174         credential->sE = retrieve_byte_array( &(credential->sELength),read_buffer);
175         return credential;
176 }
177 
178 int
save_TSS_DAA_CRED_ISSUER(FILE * file,TSS_DAA_CRED_ISSUER * credential)179 save_TSS_DAA_CRED_ISSUER(FILE *file, TSS_DAA_CRED_ISSUER *credential)
180 {
181         int i;
182 
183         fprintf(file, "%d # %s.length\n", credential->capitalALength, "capitalA");
184         fprintf(file, "%s\n", dump_byte_array( credential->capitalALength,
185                                                 credential->capitalA));
186         fprintf(file, "%d # %s.length\n", credential->eLength, "e");
187         fprintf(file, "%s\n", dump_byte_array( credential->eLength,
188                                                 credential->e));
189         fprintf(file, "%d # %s.length\n", credential->vPrimePrimeLength, "vPrimePrime");
190         fprintf(file, "%s\n", dump_byte_array( credential->vPrimePrimeLength,
191                                                 credential->vPrimePrime));
192         fprintf(file, "%d # %s\n", credential->attributesIssuerLength, "attributesIssuerLength");
193         for( i=0; i < (int)credential->attributesIssuerLength; i++) {
194                 fprintf(file, "%s\n", dump_byte_array( DAA_PARAM_SIZE_F_I / 8,
195                                                         credential->attributesIssuer[i]));
196 
197         }
198         fprintf(file, "%d # %s.length\n", credential->cPrimeLength, "cPrime");
199         fprintf(file, "%s\n", dump_byte_array( credential->cPrimeLength,
200                                                 credential->cPrime));
201         fprintf(file, "%d # %s.length\n", credential->sELength, "sE");
202         fprintf(file, "%s\n", dump_byte_array( credential->sELength,
203                                                 credential->sE));
204         return 0;
205 }
206 
207 TSS_DAA_CREDENTIAL *
load_TSS_DAA_CREDENTIAL(FILE * file)208 load_TSS_DAA_CREDENTIAL(FILE *file)
209 {
210         TSS_DAA_CREDENTIAL *credential =
211                 (TSS_DAA_CREDENTIAL *)malloc(sizeof(TSS_DAA_CREDENTIAL));
212         char *read_buffer;
213         int  i, len;
214         TSS_DAA_PK_internal *pk_internal;
215         TSS_DAA_PK *pk;
216 
217         init_tss_version( credential);
218         credential->capitalALength = read_int( file);
219         read_buffer = read_str( file);
220         credential->capitalA = retrieve_byte_array( &(credential->capitalALength),
221                                                 read_buffer);
222         credential->exponentLength = read_int( file);
223         read_buffer = read_str( file);
224         credential->exponent = retrieve_byte_array( &(credential->exponentLength),
225                                                 read_buffer);
226         credential->vBar0Length = read_int( file);
227         read_buffer = read_str( file);
228         credential->vBar0 = retrieve_byte_array(&(credential->vBar0Length),
229                                                 read_buffer);
230         credential->vBar1Length = read_int( file);
231         read_buffer = read_str( file);
232         credential->vBar1 = retrieve_byte_array(&(credential->vBar1Length),
233                                                 read_buffer);
234         // attributes issuer
235         credential->attributesLength = read_int( file);
236         printf("attributesLength=%d\n", credential->attributesLength);
237         credential->attributes = malloc(credential->attributesLength * sizeof( BYTE *));
238         for( i=0; i < (int)credential->attributesLength; i++) {
239                 read_buffer = read_str( file);
240                 credential->attributes[i] = retrieve_byte_array( &len, read_buffer);
241                 if( len != DAA_PARAM_SIZE_F_I / 8) {
242                         LogError("Error when parsing attributes");
243                         LogError("\tattribute length:%d", len);
244                         LogError("\texpected length:%d", DAA_PARAM_SIZE_F_I / 8);
245                         return NULL;
246                 }
247         }
248         pk_internal = load_DAA_PK_internal( file);
249         pk = i_2_e_TSS_DAA_PK( pk_internal, &normal_malloc, (TSS_HOBJECT)NULL);
250         memcpy( &(credential->issuerPK), pk, sizeof(TSS_DAA_PK));
251         free( pk);
252         free_TSS_DAA_PK_internal( pk_internal);
253         credential->tpmSpecificEncLength = read_int( file);
254         read_buffer = read_str( file);
255         credential->tpmSpecificEnc = retrieve_byte_array( &(credential->tpmSpecificEncLength),
256                                                         read_buffer);
257         credential->daaCounter = read_int( file);
258         return credential;
259 }
260 
261 int
save_TSS_DAA_CREDENTIAL(FILE * file,TSS_DAA_CREDENTIAL * credential)262 save_TSS_DAA_CREDENTIAL(FILE *file,
263                         TSS_DAA_CREDENTIAL *credential)
264 {
265         int i;
266         TSS_DAA_PK_internal *pk_internal;
267 
268         fprintf(file, "%d # %s.length\n", credential->capitalALength, "capitalA");
269         fprintf(file, "%s\n", dump_byte_array( credential->capitalALength,
270                                                 credential->capitalA));
271         fprintf(file, "%d # %s.length\n", credential->exponentLength, "exponent");
272         fprintf(file, "%s\n", dump_byte_array( credential->exponentLength,
273                                                 credential->exponent));
274         fprintf(file, "%d # %s.length\n", credential->vBar0Length, "vBar0");
275         fprintf(file, "%s\n", dump_byte_array( credential->vBar0Length,
276                                                 credential->vBar0));
277         fprintf(file, "%d # %s.length\n", credential->vBar1Length, "vBar1");
278         fprintf(file, "%s\n", dump_byte_array( credential->vBar1Length,
279                                                 credential->vBar1));
280         fprintf(file, "%d # %s\n", credential->attributesLength, "attributesLength");
281         for( i=0; i < (int)credential->attributesLength; i++) {
282                 fprintf(file, "%s\n", dump_byte_array( DAA_PARAM_SIZE_F_I / 8,
283                                                         credential->attributes[i]));
284         }
285         pk_internal = e_2_i_TSS_DAA_PK( &(credential->issuerPK) );
286         save_DAA_PK_internal( file, pk_internal);
287         free_TSS_DAA_PK_internal( pk_internal);
288         fprintf(file, "%d # %s.length\n", credential->tpmSpecificEncLength, "tpmSpecificEnc");
289         fprintf(file, "%s\n", dump_byte_array( credential->tpmSpecificEncLength,
290                                                 credential->tpmSpecificEnc));
291         fprintf(file, "%d # daaCounter\n", credential->daaCounter);
292         return 0;
293 }
294 
295