summaryrefslogtreecommitdiffstats
path: root/base/native-tools/src/tkstool/file.c
blob: 6e95f03fa04b1e424af6b4ffefec0c64438cee1d (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/* --- BEGIN COPYRIGHT BLOCK ---
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Copyright (C) 2007 Red Hat, Inc.
 * All rights reserved.
 * --- END COPYRIGHT BLOCK ---
 */

#include "tkstool.h"

SECStatus
TKS_ReadInputFileIntoSECItem( char    *input,
                              char    *hexInternalKeyKCV,
                              int      hexInternalKeyKCVLength,
                              char    *wrappedKeyName,
                              SECItem *wrappedKey )
{
    char        buf[1];
    PRFileDesc *fd               = NULL;
    PRInt32     c                = 0;
    PRInt32     k                = 0;
    PRInt32     count            = 0;
    PRIntn      firstCount       = 0;
    PRIntn      secondCount      = 0;
    PRIntn      thirdCount       = 0;
    PRIntn      i                = 0;
    SECItem     hexWrappedKey    = { siBuffer,
                                     NULL,
                                     0 };
    SECStatus   status           = SECFailure;

    /* Create a clean new hex display buffer for this wrapped key */
    hexWrappedKey.type = ( SECItemType ) siBuffer;
    hexWrappedKey.len  = ( ( wrappedKey->len * 2 ) + 1 );
    hexWrappedKey.data = ( unsigned char * )
                         PORT_ZAlloc( hexWrappedKey.len );
    if( hexWrappedKey.data == NULL ) {
        status = SECFailure;
        goto destroyHexWrappedKey;
    }

    /* open the input file read-only */
    fd = PR_OpenFile( input, PR_RDONLY, 0666 );
    if( !fd ) {
        status = SECFailure;
        goto destroyHexWrappedKey;
    }

    /* read in the wrapped key */
    while( c < HEX_WRAPPED_KEY_LENGTH ) {
        /* read in the next byte */
        count = PR_Read( fd, buf, 1 );

        /* check for EOF */
        if( count > 0 ) {
            /* save acceptable hex characters    */
            /* silently throw anything else away */
            switch( *buf ) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    /* acceptable character; save it as typed */
                    hexWrappedKey.data[c] = buf[0];
                    break;
                case 'A':
                case 'a':
                    /* acceptable character; save uppercase version */
                    hexWrappedKey.data[c] = 'A';
                    break;
                case 'B':
                case 'b':
                    /* acceptable character; save uppercase version */
                    hexWrappedKey.data[c] = 'B';
                    break;
                case 'C':
                case 'c':
                    /* acceptable character; save uppercase version */
                    hexWrappedKey.data[c] = 'C';
                    break;
                case 'D':
                case 'd':
                    /* acceptable character; save uppercase version */
                    hexWrappedKey.data[c] = 'D';
                    break;
                case 'E':
                case 'e':
                    /* acceptable character; save uppercase version */
                    hexWrappedKey.data[c] = 'E';
                    break;
                case 'F':
                case 'f':
                    /* acceptable character; save uppercase version */
                    hexWrappedKey.data[c] = 'F';
                    break;
                default:
                    /* unacceptable character; don't save it */
                    continue;
            }

            /* increment the number of wrapped key bytes read */
            c++;
        }
    }

    /* insure that the wrapped key was completely obtained */
    if( c != HEX_WRAPPED_KEY_LENGTH ) {
        status = SECFailure;
        goto destroyHexWrappedKey;
    }

    /* Convert these wrapped key hex digits */
    /* into the data portion of a SECItem   */
    TKS_ConvertStringOfHexCharactersIntoBitStream( ( char * ) hexWrappedKey.data,
                                                   ( hexWrappedKey.len - 1 ),
                                                   wrappedKey->data );

    /* read in the wrapped key KCV */
    while( k < HEX_WRAPPED_KEY_KCV_LENGTH ) {
        count = PR_Read( fd, buf, 1 );

        if( count > 0 ) {
            /* save acceptable hex characters; silently */
            /* throw anything else away                 */
            switch( *buf ) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    /* acceptable character; save it as typed */
                    hexInternalKeyKCV[k] = buf[0];
                    break;
                case 'A':
                case 'a':
                    /* acceptable character; save uppercase version */
                    hexInternalKeyKCV[k] = 'A';
                    break;
                case 'B':
                case 'b':
                    /* acceptable character; save uppercase version */
                    hexInternalKeyKCV[k] = 'B';
                    break;
                case 'C':
                case 'c':
                    /* acceptable character; save uppercase version */
                    hexInternalKeyKCV[k] = 'C';
                    break;
                case 'D':
                case 'd':
                    /* acceptable character; save uppercase version */
                    hexInternalKeyKCV[k] = 'D';
                    break;
                case 'E':
                case 'e':
                    /* acceptable character; save uppercase version */
                    hexInternalKeyKCV[k] = 'E';
                    break;
                case 'F':
                case 'f':
                    /* acceptable character; save uppercase version */
                    hexInternalKeyKCV[k] = 'F';
                    break;
                default:
                    /* unacceptable character; don't save it */
                    continue;
            }

            /* increment the number of key KCV bytes read */
            k++;
        }
    }

    /* insure that the wrapped key KCV was completely obtained */
    if( k != HEX_WRAPPED_KEY_KCV_LENGTH ) {
        status = SECFailure;
        goto destroyHexWrappedKey;
    }

    /* For convenience, display the read-in wrapped key */
    /* and its associated KCV to the user.              */
    if( hexWrappedKey.data != NULL ) {
        /* Display this final wrapped key */
        if( ( hexWrappedKey.len - 1 ) !=
            HEX_WRAPPED_KEY_LENGTH ) {
            /* invalid key length */
            PR_fprintf( PR_STDERR,
                        "ERROR:  Invalid data length of %d bytes!\n\n\n",
                        hexWrappedKey.len );
            status = SECFailure;
            goto destroyHexWrappedKey;
        } else {
            /* Print wrapped data blob */
            PR_fprintf( PR_STDOUT,
                        "\n    wrapped data:    " );

            /* Print first DES_LENGTH bytes */
            if( wrappedKey->len == ( 3 * DES_LENGTH ) ) {
                firstCount = ( ( hexWrappedKey.len - 1 ) / 3 );
            } else {
                firstCount = ( ( hexWrappedKey.len - 1 ) / 2 );
            }
            for( i = 0; i < firstCount; i += 4 ) {
                PR_fprintf( PR_STDOUT,
                            "%c%c%c%c ",
                            hexWrappedKey.data[i],
                            hexWrappedKey.data[i + 1],
                            hexWrappedKey.data[i + 2],
                            hexWrappedKey.data[i + 3] );
            }

            /* Print appropriate padding length */
            PR_fprintf( PR_STDOUT, "\n                     " );

            /* Print second DES_LENGTH bytes */
            secondCount = firstCount * 2;
            for( i = firstCount; i < secondCount; i += 4 ) {
                PR_fprintf( PR_STDOUT,
                            "%c%c%c%c ",
                            hexWrappedKey.data[i],
                            hexWrappedKey.data[i + 1],
                            hexWrappedKey.data[i + 2],
                            hexWrappedKey.data[i + 3] );
            }

            /* print out last 8 bytes of triple-DES keys */
            if( wrappedKey->len == ( 3 * DES_LENGTH ) ) {
                /* Print appropriate padding length */
                PR_fprintf( PR_STDOUT, "\n                     " );

                /* Print third DES_LENGTH bytes */
                thirdCount = hexWrappedKey.len;
                for( i = secondCount; i < thirdCount; i += 4 ) {
                    PR_fprintf( PR_STDOUT,
                                "%c%c%c%c ",
                                hexWrappedKey.data[i],
                                hexWrappedKey.data[i + 1],
                                hexWrappedKey.data[i + 2],
                                hexWrappedKey.data[i + 3] );
                }
            }

            /* Print appropriate vertical spacing */
            PR_fprintf( PR_STDOUT, "\n\n\n" );
        }
    }
  
    if( hexInternalKeyKCV != NULL ) {
        /* Display this final wrapped key's KCV */
        if( ( hexInternalKeyKCVLength - 1 ) !=
            HEX_WRAPPED_KEY_KCV_LENGTH ) {
            /* invalid key length */
            PR_fprintf( PR_STDERR,
                        "ERROR:  Invalid key KCV length "
                        "of %d bytes!\n\n\n",
                        hexInternalKeyKCVLength );
            status = SECFailure;
            goto destroyHexWrappedKey;
        } else {
            PR_fprintf( PR_STDOUT,
                        "    master key KCV:  "
                        "%c%c%c%c %c%c%c%c\n    (pre-computed KCV of the "
                        "master key residing inside the wrapped data)\n\n\n",
                        hexInternalKeyKCV[0],
                        hexInternalKeyKCV[1],
                        hexInternalKeyKCV[2],
                        hexInternalKeyKCV[3],
                        hexInternalKeyKCV[4],
                        hexInternalKeyKCV[5],
                        hexInternalKeyKCV[6],
                        hexInternalKeyKCV[7] );
        }
    }

    /* close the input file */
    PR_Close( fd );

    status = SECSuccess;

destroyHexWrappedKey:
    /* Destroy the hex wrapped key */
    if( hexWrappedKey.data != NULL ) {
        PORT_ZFree( ( unsigned char * )
                    hexWrappedKey.data,
                    hexWrappedKey.len );
        hexWrappedKey.data = NULL;
        hexWrappedKey.len  = 0;
    }

    return status;
}


SECStatus
TKS_WriteSECItemIntoOutputFile( SECItem *wrappedKey,
                                char    *wrappedKeyName,
                                char    *hexInternalKeyKCV,
                                int      hexInternalKeyKCVLength,
                                char    *output )
{
    PRFileDesc *fd               = NULL;
    PRInt32     count            = 0;
    PRInt32     r                = 0;
    PRIntn      firstCount       = 0;
    PRIntn      secondCount      = 0;
    PRIntn      thirdCount       = 0;
    PRIntn      i                = 0;
    SECItem     hexWrappedKey    = { siBuffer,
                                     NULL,
                                     0 };
    SECStatus   status           = SECFailure;

    /* Create a clean new hex display buffer for this wrapped key */
    hexWrappedKey.type = ( SECItemType ) siBuffer;
    hexWrappedKey.len  = ( ( wrappedKey->len * 2 ) + 1 );
    hexWrappedKey.data = ( unsigned char * )
                         PORT_ZAlloc( hexWrappedKey.len );
    if( hexWrappedKey.data == NULL ) {
        status = SECFailure;
        goto destroyHexWrappedKey;
    }

    /* Convert this wrapped key into hex digits */
    TKS_StringToHex( ( PRUint8 * ) wrappedKey->data,
                     ( PRIntn )    wrappedKey->len,
                     ( PRUint8 * ) hexWrappedKey.data,
                     ( PRIntn )    hexWrappedKey.len );

    /* For convenience, display this wrapped key to the user. */
    if( hexWrappedKey.data != NULL ) {
        /* Display this final wrapped key */
        if( ( hexWrappedKey.len - 1 ) !=
            HEX_WRAPPED_KEY_LENGTH ) {
            /* invalid key length */
            PR_fprintf( PR_STDERR,
                        "ERROR:  Invalid data length of %d bytes!\n\n\n",
                        hexWrappedKey.len );
            status = SECFailure;
            goto destroyHexWrappedKey;
        } else {
            /* Print wrapped data blob */
            PR_fprintf( PR_STDOUT,
                        "    wrapped data:    " );

            /* Print first DES_LENGTH bytes */
            if( wrappedKey->len == ( 3 * DES_LENGTH ) ) {
                firstCount = ( ( hexWrappedKey.len - 1 ) / 3 );
            } else {
                firstCount = ( ( hexWrappedKey.len - 1 ) / 2 );
            }
            for( i = 0; i < firstCount; i += 4 ) {
                PR_fprintf( PR_STDOUT,
                            "%c%c%c%c ",
                            hexWrappedKey.data[i],
                            hexWrappedKey.data[i + 1],
                            hexWrappedKey.data[i + 2],
                            hexWrappedKey.data[i + 3] );
            }

            /* Print appropriate padding length */
            PR_fprintf( PR_STDOUT, "\n                     " );

            /* Print second DES_LENGTH bytes */
            secondCount = firstCount * 2;
            for( i = firstCount; i < secondCount; i += 4 ) {
                PR_fprintf( PR_STDOUT,
                            "%c%c%c%c ",
                            hexWrappedKey.data[i],
                            hexWrappedKey.data[i + 1],
                            hexWrappedKey.data[i + 2],
                            hexWrappedKey.data[i + 3] );
            }

            /* print out last 8 bytes of triple-DES keys */
            if( wrappedKey->len == ( 3 * DES_LENGTH ) ) {
                /* Print appropriate padding length */
                PR_fprintf( PR_STDOUT, "\n                     " );

                /* Print third DES_LENGTH bytes */
                thirdCount = hexWrappedKey.len;
                for( i = secondCount; i < thirdCount; i += 4 ) {
                    PR_fprintf( PR_STDOUT,
                                "%c%c%c%c ",
                                hexWrappedKey.data[i],
                                hexWrappedKey.data[i + 1],
                                hexWrappedKey.data[i + 2],
                                hexWrappedKey.data[i + 3] );
                }
            }

            /* Print appropriate vertical spacing */
            PR_fprintf( PR_STDOUT, "\n\n\n" );
        }
    }

    /* For convenience, display this wrapped key's */
    /* master key KCV to the user.                 */
    if( ( hexInternalKeyKCV != NULL ) &&
        ( hexInternalKeyKCVLength == HEX_WRAPPED_KEY_KCV_LENGTH ) ) {
            /* display this wrapped key's computed KCV value (in hex) */
            PR_fprintf( PR_STDOUT,
                        "    master key KCV:  "
                        "%c%c%c%c %c%c%c%c\n    (computed KCV of the "
                        "master key residing inside the wrapped data)\n\n\n",
                        hexInternalKeyKCV[0],
                        hexInternalKeyKCV[1],
                        hexInternalKeyKCV[2],
                        hexInternalKeyKCV[3],
                        hexInternalKeyKCV[4],
                        hexInternalKeyKCV[5],
                        hexInternalKeyKCV[6],
                        hexInternalKeyKCV[7] );
    }

    /* open the output file read-write */
    fd = PR_OpenFile( output, ( PR_RDWR | PR_CREATE_FILE ), 0666 );
    if( !fd ) {
        status = SECFailure;
        goto destroyHexWrappedKey;
    }

    /* write out the wrapped key (in hex) to the output file */
    while( count < HEX_WRAPPED_KEY_LENGTH ) {
        /* write out 4 bytes */
        r = PR_Write( fd, &( hexWrappedKey.data[count] ), 4 );
        if( r != 4 ) {
            status = SECFailure;
            goto destroyHexWrappedKey;
        }

        /* increment the byte count by 4 */
        count += 4;

        if( count >= HEX_WRAPPED_KEY_LENGTH ) {
            r = PR_Write( fd, "\n", 1 );
            if( r != 1 ) {
                status = SECFailure;
                goto destroyHexWrappedKey;
            }
        } else {
            r = PR_Write( fd, " ", 1 );
            if( r != 1 ) {
                status = SECFailure;
                goto destroyHexWrappedKey;
            }
        }
    }

    /* reinitialize count */
    count = 0;

    /* write out the master key KCV (in hex) to the output file */
    while( count < HEX_WRAPPED_KEY_KCV_LENGTH ) {
        /* write out 4 bytes */
        r = PR_Write( fd, &( hexInternalKeyKCV[count] ), 4 );
        if( r != 4 ) {
            status = SECFailure;
            goto destroyHexWrappedKey;
        }

        /* increment the byte count by 4 */
        count += 4;

        if( count >= HEX_WRAPPED_KEY_KCV_LENGTH ) {
            r = PR_Write( fd, "\n", 1 );
            if( r != 1 ) {
                status = SECFailure;
                goto destroyHexWrappedKey;
            }
        } else {
            r = PR_Write( fd, " ", 1 );
            if( r != 1 ) {
                status = SECFailure;
                goto destroyHexWrappedKey;
            }
        }
    }

    /* close the output file */
    PR_Close( fd );

    status = SECSuccess;

destroyHexWrappedKey:
    /* Destroy the hex wrapped key */
    if( hexWrappedKey.data != NULL ) {
        PORT_ZFree( ( unsigned char * )
                    hexWrappedKey.data,
                    hexWrappedKey.len );
        hexWrappedKey.data = NULL;
        hexWrappedKey.len  = 0;
    }

    return status;
}

SECStatus
TKS_WriteSharedKeyIntoOutputFile( char *output )
{
    SECStatus   status           = SECSuccess;
    PRFileDesc *fd               = NULL;
    PRInt32     r                = 0;

    fd = PR_Open( output, ( PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE ), 0600 );
    if( !fd ) {
        status = SECFailure;
        goto done;
    }

    r = PR_Write( fd, "<SharedKey></SharedKey>\n", 24 );
    if( r < 0 ) {
        status = SECFailure;
        goto close;
    }

close:
    PR_Close( fd );

done:
    return status;
}