summaryrefslogtreecommitdiffstats
path: root/pki/base/native-tools/src/tkstool/tkstool.h
blob: 3b0407227f5d57ff215eebbbfae1784d2bfd46fc (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
/* --- 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 headers  **/
/************************/

#ifdef HAVE_CONFIG_H
#ifndef AUTOTOOLS_CONFIG_H
#define AUTOTOOLS_CONFIG_H

/* Eliminate warnings when using Autotools */
#undef PACKAGE_BUGREPORT
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION

#include <config.h>
#endif /* AUTOTOOLS_CONFIG_H */
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <string.h>

#if defined(WIN32)
#include "fcntl.h"
#include "io.h"
#endif

#if defined(XP_UNIX)
#include <unistd.h>
#include <sys/time.h>
#include <termios.h>
#endif

#if defined(XP_WIN) || defined (XP_PC)
#include <time.h>
#include <conio.h>
#endif

#include "secutil.h"
#include "nspr.h"
#include "prtypes.h"
#include "prtime.h"
#include "prlong.h"
#include "pk11func.h"
#include "secasn1.h"
#include "cert.h"
#include "cryptohi.h"
#include "secoid.h"
#include "certdb.h"
#include "nss.h"


/****************/
/**  #defines  **/
/****************/

#define TKSTOOL_MAJOR_VERSION_NUMBER           1
#define TKSTOOL_MINOR_VERSION_NUMBER           0
#define TKSTOOL_VERSION_SUFFIX                 ""

#define DEFAULT_KEY_BITS                       1024
#define NUM_KEYSTROKES                         120
#define RAND_BUF_LENGTH                        60
#define DES_LENGTH                             8
#define KEYSTROKES_TO_PROCEED                  8
#define KCV_LENGTH                             4
#define CTRL_C                                 3

#define FIRST_SESSION_KEY_SHARE                "first"
#define FIRST_SESSION_KEY_SHARE_LENGTH         16
#define SECOND_SESSION_KEY_SHARE               "second"
#define SECOND_SESSION_KEY_SHARE_LENGTH        16
#define THIRD_SESSION_KEY_SHARE                "third"
#define THIRD_SESSION_KEY_SHARE_LENGTH         16
#define HEX_SESSION_KEY_BUF_LENGTH             32
#define HEX_SESSION_KEY_KCV_BUF_LENGTH         8

#define MASTER_KEY_LENGTH                      16

#define WRAPPED_KEY_LENGTH                     16
#define HEX_WRAPPED_KEY_LENGTH                 32
#define HEX_WRAPPED_KEY_KCV_LENGTH             8

#if defined(PAD_DES2_KEY_LENGTH)
#define PADDED_FIRST_SESSION_KEY_SHARE_LENGTH  24
#define PADDED_SECOND_SESSION_KEY_SHARE_LENGTH 24
#define PADDED_THIRD_SESSION_KEY_SHARE_LENGTH  24
#endif

#define FIRST_SYMMETRIC_KEY                    "first"
#define SECOND_SYMMETRIC_KEY                   "second"
#define THIRD_SYMMETRIC_KEY                    "third"
#define MASTER_KEY                             "master"
#define RESIDENT_KEY                           "resident"
#define SESSION_KEY                            "session"
#define SYMMETRIC_KEY                          "symmetric"
#define TRANSPORT_KEY                          "transport"
#define UNWRAPPED_KEY                          "unwrapped"
#define WRAPPED_KEY                            "wrapped"

#define CONTINUATION_MESSAGE                   "Press enter to continue " \
                                               "(or ^C to break):  "

#define PROCEED_MESSAGE                        "Type the word \"proceed\" "   \
                                               "and press enter to continue " \
                                               "(or ^C to break):  "


/**************************************/
/**  external function declarations  **/
/**************************************/

#if defined(__sun) && !defined(SVR4)
extern int fclose( FILE* );
extern int fprintf( FILE *, char *, ... );
extern int isatty( int );
extern char *sys_errlist[];
#define strerror( errno ) sys_errlist[errno]
#endif


/***************************/
/**  function prototypes  **/
/***************************/

/************/
/* delete.c */
/************/

SECStatus
TKS_DeleteKeys( char *progName,
                PK11SlotInfo *slot,
                char *keyname,
                secuPWData *pwdata );


/**********/
/* file.c */
/**********/

SECStatus
TKS_ReadInputFileIntoSECItem( char    *input,
                              char    *hexInternalKeyKCV,
                              int      hexInternalKeyKCVLength,
                              char    *keyname,
                              SECItem *wrappedKey );

SECStatus
TKS_WriteSECItemIntoOutputFile( SECItem *wrappedKey,
                                char    *keyname,
                                char    *hexInternalKeyKCV,
                                int      hexInternalKeyKCVLength,
                                char    *output );


/**********/
/* find.c */
/**********/

SECStatus
TKS_FindSymKey( PK11SlotInfo *slot,
                char *keyname,
                void *pwdata );


/**********/
/* help.c */
/**********/

void
TKS_Usage( char *progName );

void
TKS_PrintHelp( char *progName );


/*********/
/* key.c */
/*********/

SECStatus
TKS_ComputeAndDisplayKCV( PRUint8    *newKey,
                          PRIntn      newKeyLen,
                          PRUint8    *KCV,
                          PRIntn      KCVLen,
                          PK11SymKey *symKey,
                          char       *keyName,
                          char       *keyType,
                          PRBool      displayKCV,
                          PRUint8    *expectedHexKCV );

SECStatus
TKS_GenerateSessionKeyShare( char    *sessionKeyShareName,
                             SECItem *sessionKeyShare );

SECStatus
TKS_InputSessionKeyShare( char    *sessionKeyShareName,
                          SECItem *sessionKeyShare );

PK11SymKey *
TKS_ImportSymmetricKey( char              *symmetricKeyName,
                        PK11SlotInfo      *slot,
                        CK_MECHANISM_TYPE  mechanism,
                        CK_ATTRIBUTE_TYPE  operation,
                        SECItem           *sessionKeyShare,
                        secuPWData        *pwdata );

PK11SymKey *
TKS_DeriveSymmetricKey( char              *symmetricKeyName,
                        PK11SymKey        *symKey,
                        CK_MECHANISM_TYPE  derive,
                        SECItem           *sessionKeyShare,
                        CK_MECHANISM_TYPE  target,
                        CK_ATTRIBUTE_TYPE  operation,
                        int                keysize );

SECStatus
TKS_StoreSymmetricKeyAndNameIt( char              *symmetricKeyName,
                                char              *keyname,
                                PK11SlotInfo      *slot,
                                CK_ATTRIBUTE_TYPE  operation,
                                CK_FLAGS           flags,
                                PK11SymKey        *symKey );


/**********/
/* list.c */
/**********/

SECStatus
TKS_ListKeys( char *progName,
              PK11SlotInfo *slot,
              char *keyname,
              int index, 
              PRBool dopriv,
              secuPWData *pwdata );


/*************/
/* modules.c */
/*************/

SECStatus
TKS_ListSecModules( void );


/************/
/* random.c */
/************/

void
TKS_FileForRNG( char *noise );

SECStatus
TKS_SeedRNG( char *noise );


/**************/
/* retrieve.c */
/**************/

PK11SymKey *
TKS_RetrieveSymKey( PK11SlotInfo *slot,
                    char *keyname,
                    void *pwdata );


/**********/
/* util.c */
/**********/

PR_IMPLEMENT( void )
TKS_ClearScreen();

PR_IMPLEMENT( void )
TKS_WaitForUser();

PR_IMPLEMENT( void )
TKS_TypeProceedToContinue();

PR_IMPLEMENT( void )
TKS_AdjustOddParity( PRUint8 *key );

PR_IMPLEMENT( void )
TKS_StringToHex( PRUint8 *key,
                 PRIntn len,
                 PRUint8 *hex_key,
                 PRIntn hex_len );

PR_IMPLEMENT( PRBool )
TKS_ConvertStringOfHexCharactersIntoBitStream( char* input,
                                               PRIntn input_bytes,
                                               PRUint8* output );


/*************/
/* version.c */
/*************/

void
TKS_Version( char *progName );