~ubuntu-branches/ubuntu/quantal/nss/quantal-updates

« back to all changes in this revision

Viewing changes to mozilla/security/nss/cmd/pwdecrypt/pwdecrypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2010-03-25 13:46:06 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20100325134606-bl6liuok2w9l7snv
Tags: 3.12.6-0ubuntu1
* New upstream release 3.12.6 RTM (NSS_3_12_6_RTM)
  - fixes CVE-2009-3555 aka US-CERT VU#120541
* Adjust patches to changed upstream code base
  - update debian/patches/38_kbsd.patch
  - update debian/patches/38_mips64_build.patch
  - update debian/patches/85_security_load.patch
* Remove patches that are merged upstream
  - delete debian/patches/91_nonexec_stack.patch
  - update debian/patches/series
* Bump nspr dependency to 4.8
  - update debian/control
* Add new symbols for 3.12.6
  - update debian/libnss3-1d.symbols

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
/*
38
38
 * Test program for SDR (Secret Decoder Ring) functions.
39
39
 *
40
 
 * $Id: pwdecrypt.c,v 1.5 2008/08/08 23:47:58 julien.pierre.boogz%sun.com Exp $
 
40
 * $Id: pwdecrypt.c,v 1.7 2009/08/03 07:07:13 nelson%bolyard.com Exp $
41
41
 */
42
42
 
43
43
#include "nspr.h"
116
116
 * base64 table only used to identify the end of a base64 string 
117
117
 */
118
118
static unsigned char b64[256] = {
119
 
/*   0: */        0,      0,      0,      0,      0,      0,      0,      0,
120
 
/*   8: */        0,      0,      0,      0,      0,      0,      0,      0,
121
 
/*  16: */        0,      0,      0,      0,      0,      0,      0,      0,
122
 
/*  24: */        0,      0,      0,      0,      0,      0,      0,      0,
123
 
/*  32: */        0,      0,      0,      0,      0,      0,      0,      0,
124
 
/*  40: */        0,      0,      0,      1,      0,      0,      0,      1,
125
 
/*  48: */        1,      1,      1,      1,      1,      1,      1,      1,
126
 
/*  56: */        1,      1,      0,      0,      0,      0,      0,      0,
127
 
/*  64: */        0,      1,      1,      1,      1,      1,      1,      1,
128
 
/*  72: */        1,      1,      1,      1,      1,      1,      1,      1,
129
 
/*  80: */        1,      1,      1,      1,      1,      1,      1,      1,
130
 
/*  88: */        1,      1,      1,      0,      0,      0,      0,      0,
131
 
/*  96: */        0,      1,      1,      1,      1,      1,      1,      1,
132
 
/* 104: */        1,      1,      1,      1,      1,      1,      1,      1,
133
 
/* 112: */        1,      1,      1,      1,      1,      1,      1,      1,
134
 
/* 120: */        1,      1,      1,      0,      0,      0,      0,      0,
135
 
/* 128: */        0,      0,      0,      0,      0,      0,      0,      0
 
119
/*  00: */      0,      0,      0,      0,      0,      0,      0,      0,
 
120
/*  08: */      0,      0,      0,      0,      0,      0,      0,      0,
 
121
/*  10: */      0,      0,      0,      0,      0,      0,      0,      0,
 
122
/*  18: */      0,      0,      0,      0,      0,      0,      0,      0,
 
123
/*  20: */      0,      0,      0,      0,      0,      0,      0,      0,
 
124
/*  28: */      0,      0,      0,      1,      0,      0,      0,      1,
 
125
/*  30: */      1,      1,      1,      1,      1,      1,      1,      1,
 
126
/*  38: */      1,      1,      0,      0,      0,      0,      0,      0,
 
127
/*  40: */      0,      1,      1,      1,      1,      1,      1,      1,
 
128
/*  48: */      1,      1,      1,      1,      1,      1,      1,      1,
 
129
/*  50: */      1,      1,      1,      1,      1,      1,      1,      1,
 
130
/*  58: */      1,      1,      1,      0,      0,      0,      0,      0,
 
131
/*  60: */      0,      1,      1,      1,      1,      1,      1,      1,
 
132
/*  68: */      1,      1,      1,      1,      1,      1,      1,      1,
 
133
/*  70: */      1,      1,      1,      1,      1,      1,      1,      1,
 
134
/*  78: */      1,      1,      1,      0,      0,      0,      0,      0,
136
135
};
137
136
 
138
137
enum {
140
139
   true = 1
141
140
} bool;
142
141
 
143
 
int
144
 
isatobchar(int c) { return b64[c] != 0; }
145
 
 
146
 
 
147
 
#define MAX_STRING 256
148
 
int
149
 
getData(FILE *inFile,char **inString) {
150
 
    int len = 0;
151
 
    int space = MAX_STRING;
152
 
    int oneequal = false;
153
 
    int c;
154
 
    char *string = (char *) malloc(space);
155
 
 
156
 
    string[len++]='M';
157
 
 
158
 
    while ((c = getc(inFile)) != EOF) {
159
 
        if (len >= space) {
160
 
            char *newString;
161
 
 
162
 
            space *= 2;
163
 
            newString = (char *)realloc(string,space);
164
 
            if (newString == NULL) {
165
 
                ungetc(c,inFile);
166
 
                break;
167
 
            }
168
 
            string = newString;
169
 
        }
170
 
        string[len++] = c;
171
 
        if (!isatobchar(c)) {
172
 
           if (c == '=') {
173
 
                if (oneequal) {
174
 
                    break;
175
 
                }
176
 
                oneequal = true;
177
 
                continue;
178
 
           } else {
179
 
               ungetc(c,inFile);
180
 
               len--;
181
 
               break;
182
 
           }
183
 
        }
184
 
        if (oneequal) {
185
 
           ungetc(c,inFile);
186
 
           len--;
187
 
           break;
188
 
        }
189
 
    }
190
 
    if (len >= space) {
191
 
        space += 2;
192
 
        string = (char *)realloc(string,space);
193
 
    }
194
 
    string[len++] = 0;
195
 
    *inString = string;
 
142
#define isatobchar(c) (b64[c])
 
143
 
 
144
#define MAX_STRING 8192
 
145
 
 
146
int
 
147
isBase64(char *inString) 
 
148
{
 
149
    unsigned int i;
 
150
    unsigned char c;
 
151
 
 
152
    for (i = 0; (c = inString[i]) != 0 && isatobchar(c); ++i) 
 
153
        ;
 
154
    if (c == '=') {
 
155
        while ((c = inString[++i]) == '=')
 
156
            ; /* skip trailing '=' characters */
 
157
    }
 
158
    if (c && c != '\n' && c != '\r')
 
159
        return false;
 
160
    if (i == 0 || i % 4)
 
161
        return false;
196
162
    return true;
197
163
}
198
164
 
 
165
void
 
166
doDecrypt(char * dataString, FILE *outFile, FILE *logFile, secuPWData *pwdata)
 
167
{
 
168
    int        strLen = strlen(dataString);
 
169
    SECItem   *decoded = NSSBase64_DecodeBuffer(NULL, NULL, dataString, strLen);
 
170
    SECStatus  rv;
 
171
    int        err;
 
172
    unsigned int i;
 
173
    SECItem    result = { siBuffer, NULL, 0 };
 
174
 
 
175
    if ((decoded == NULL) || (decoded->len == 0)) {
 
176
        if (logFile) {
 
177
            err = PORT_GetError();
 
178
            fprintf(logFile,"Base 64 decode failed on <%s>\n", dataString);
 
179
            fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err));
 
180
        }
 
181
        fputs(dataString, outFile);
 
182
        if (decoded)
 
183
            SECITEM_FreeItem(decoded, PR_TRUE);
 
184
        return;
 
185
    }
 
186
 
 
187
    rv = PK11SDR_Decrypt(decoded, &result, pwdata);
 
188
    SECITEM_ZfreeItem(decoded, PR_TRUE);
 
189
    if (rv == SECSuccess) {
 
190
        /* result buffer has no extra space for a NULL */
 
191
        fprintf(outFile, "Decrypted: \"%.*s\"\n", result.len, result.data);
 
192
        SECITEM_ZfreeItem(&result, PR_FALSE);
 
193
        return;
 
194
    }
 
195
    /* Encryption failed. output raw input. */
 
196
    if (logFile) {
 
197
        err = PORT_GetError();
 
198
        fprintf(logFile,"SDR decrypt failed on <%s>\n", dataString);
 
199
        fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err));
 
200
    }
 
201
    fputs(dataString,outFile);
 
202
}
 
203
 
 
204
void
 
205
doDecode(char * dataString, FILE *outFile, FILE *logFile)
 
206
{
 
207
    int        strLen = strlen(dataString + 1);
 
208
    SECItem   *decoded;
 
209
 
 
210
    decoded = NSSBase64_DecodeBuffer(NULL, NULL, dataString + 1, strLen);
 
211
    if ((decoded == NULL) || (decoded->len == 0)) {
 
212
        if (logFile) {
 
213
            int err = PORT_GetError();
 
214
            fprintf(logFile,"Base 64 decode failed on <%s>\n", dataString + 1);
 
215
            fprintf(logFile," Error %d: %s\n", err, SECU_Strerror(err));
 
216
        }
 
217
        fputs(dataString, outFile);
 
218
        if (decoded)
 
219
            SECITEM_FreeItem(decoded, PR_TRUE);
 
220
        return;
 
221
    }
 
222
    fprintf(outFile, "Decoded: \"%.*s\"\n", decoded->len, decoded->data);
 
223
    SECITEM_ZfreeItem(decoded, PR_TRUE);
 
224
}
 
225
 
 
226
char dataString[MAX_STRING + 1];
 
227
 
199
228
int
200
229
main (int argc, char **argv)
201
230
{
210
239
    FILE        *outFile = stdout;
211
240
    FILE        *logFile = NULL;
212
241
    PLOptStatus optstatus;
213
 
    SECItem     result;
214
 
    int         c;
215
242
    secuPWData  pwdata = { PW_NONE, NULL };
216
243
 
217
 
    result.data = 0;
218
244
 
219
245
    program_name = PL_strrchr(argv[0], '/');
220
246
    program_name = program_name ? (program_name + 1) : argv[0];
270
296
    }
271
297
 
272
298
    if (input_file) {
273
 
      inFile = fopen(input_file,"r");
274
 
      if (inFile == NULL) {
275
 
        perror(input_file);
276
 
        return 1;
277
 
      }
278
 
      PR_Free(input_file);
 
299
        inFile = fopen(input_file,"r");
 
300
        if (inFile == NULL) {
 
301
            perror(input_file);
 
302
            return 1;
 
303
        }
 
304
        PR_Free(input_file);
279
305
    }
280
306
    if (output_file) {
281
 
      outFile = fopen(output_file,"w+");
282
 
      if (outFile == NULL) {
283
 
        perror(output_file);
284
 
        return 1;
285
 
      }
286
 
      PR_Free(output_file);
 
307
        outFile = fopen(output_file,"w+");
 
308
        if (outFile == NULL) {
 
309
            perror(output_file);
 
310
            return 1;
 
311
        }
 
312
        PR_Free(output_file);
287
313
    }
288
314
    if (log_file) {
289
 
      logFile = fopen(log_file,"w+");
290
 
      if (logFile == NULL) {
291
 
        perror(log_file);
292
 
        return 1;
293
 
      }
294
 
      PR_Free(log_file);
 
315
        if (log_file[0] == '-')
 
316
            logFile = stderr;
 
317
        else
 
318
            logFile = fopen(log_file,"w+");
 
319
        if (logFile == NULL) {
 
320
            perror(log_file);
 
321
            return 1;
 
322
        }
 
323
        PR_Free(log_file);
295
324
    }
296
325
 
297
326
    /*
308
337
    /* Get the encrypted result, either from the input file
309
338
     * or from encrypting the plaintext value
310
339
     */
311
 
 
312
 
    while ((c = getc(inFile)) != EOF) {
313
 
        if (c == 'M') {
314
 
           char *dataString = NULL;
315
 
           SECItem *inText;
316
 
 
317
 
           rv = getData(inFile, &dataString);
318
 
           if (!rv) {
319
 
                fputs(dataString,outFile);
320
 
                free(dataString);
321
 
                continue;
322
 
           }
323
 
           inText = NSSBase64_DecodeBuffer(NULL, NULL, dataString,
324
 
                                                        strlen(dataString));
325
 
           if ((inText == NULL) || (inText->len == 0)) {
326
 
                if (logFile) {
327
 
                    fprintf(logFile,"Base 64 decode failed on <%s>\n",
328
 
                                                                dataString);
329
 
                    fprintf(logFile," Error %x: %s\n",PORT_GetError(),
330
 
                        SECU_Strerror(PORT_GetError()));
331
 
                }
332
 
                fputs(dataString,outFile);
333
 
                free(dataString);
334
 
                continue;
335
 
           }
336
 
           result.data = NULL;
337
 
           result.len  = 0;
338
 
           rv = PK11SDR_Decrypt(inText, &result, &pwdata);
339
 
           SECITEM_FreeItem(inText, PR_TRUE);
340
 
           if (rv != SECSuccess) {
341
 
                if (logFile) {
342
 
                    fprintf(logFile,"SDR decrypt failed on <%s>\n",
343
 
                                                                dataString);
344
 
                    fprintf(logFile," Error %x: %s\n",PORT_GetError(),
345
 
                        SECU_Strerror(PORT_GetError()));
346
 
                }
347
 
                fputs(dataString,outFile);
348
 
                free(dataString);
349
 
                SECITEM_ZfreeItem(&result, PR_FALSE);
350
 
                continue;
351
 
           }
352
 
           /* result buffer has no extra space for a NULL */
353
 
           fprintf(outFile, "%.*s", result.len, result.data);
354
 
           SECITEM_ZfreeItem(&result, PR_FALSE);
355
 
         } else {
356
 
           putc(c,outFile);
357
 
         }
 
340
    while (fgets(dataString, sizeof dataString, inFile)) {
 
341
        unsigned char c = dataString[0];
 
342
 
 
343
        if (c == 'M' && isBase64(dataString)) {
 
344
            doDecrypt(dataString, outFile, logFile, &pwdata);
 
345
        } else if (c == '~' && isBase64(dataString + 1)) {
 
346
            doDecode(dataString, outFile, logFile);
 
347
        } else {
 
348
            fputs(dataString, outFile);
 
349
        }
358
350
    }
 
351
    if (pwdata.data)
 
352
        PR_Free(pwdata.data);
359
353
 
360
354
    fclose(outFile);
361
355
    fclose(inFile);
362
 
    if (logFile) {
 
356
    if (logFile && logFile != stderr) {
363
357
        fclose(logFile);
364
358
    }
365
359
 
366
360
    if (NSS_Shutdown() != SECSuccess) {
367
361
        SECU_PrintError (program_name, "NSS_Shutdown failed");
368
 
       exit(1);
 
362
        exit(1);
369
363
    }
370
364
 
371
365
prdone: