| 1 |
/* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 3 |
* |
| 4 |
* The contents of this file are subject to the Mozilla Public License Version |
| 5 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 6 |
* the License. You may obtain a copy of the License at |
| 7 |
* http://www.mozilla.org/MPL/ |
| 8 |
* |
| 9 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 10 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 |
* for the specific language governing rights and limitations under the |
| 12 |
* License. |
| 13 |
* |
| 14 |
* The Original Code is the Netscape security libraries. |
| 15 |
* |
| 16 |
* The Initial Developer of the Original Code is |
| 17 |
* Netscape Communications Corporation. |
| 18 |
* Portions created by the Initial Developer are Copyright (C) 1994-2000 |
| 19 |
* the Initial Developer. All Rights Reserved. |
| 20 |
* |
| 21 |
* Contributor(s): |
| 22 |
* Rob Crittenden ([email protected]) |
| 23 |
* |
| 24 |
* Alternatively, the contents of this file may be used under the terms of |
| 25 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 26 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 27 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 28 |
* of those above. If you wish to allow use of your version of this file only |
| 29 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 30 |
* use your version of this file under the terms of the MPL, indicate your |
| 31 |
* decision by deleting the provisions above and replace them with the notice |
| 32 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
| 33 |
* the provisions above, a recipient may use your version of this file under |
| 34 |
* the terms of any one of the MPL, the GPL or the LGPL. |
| 35 |
* |
| 36 |
* ***** END LICENSE BLOCK ***** */ |
| 37 |
|
| 38 |
/* cribbed from secutil.c */ |
| 39 |
|
| 40 |
#include "ckpem.h" |
| 41 |
|
| 42 |
#include <base64.h> |
| 43 |
#include <cryptohi.h> |
| 44 |
#include <nspr.h> |
| 45 |
#include <nssb64.h> |
| 46 |
#include <nssbase.h> |
| 47 |
#include <secerr.h> |
| 48 |
#include <secitem.h> |
| 49 |
#include <secpkcs7.h> |
| 50 |
|
| 51 |
#include <stdarg.h> |
| 52 |
|
| 53 |
static int put_object(SECItem *der, SECItem ***derlist, int *count) |
| 54 |
{ |
| 55 |
if (*derlist) { |
| 56 |
*derlist = NSS_ZRealloc(*derlist, (*count + 1) * sizeof(SECItem *)); |
| 57 |
} else { |
| 58 |
*count = 0; |
| 59 |
*derlist = NSS_ZNEWARRAY(NULL, SECItem*, 1); |
| 60 |
} |
| 61 |
if (! (*derlist)) { |
| 62 |
return CKR_HOST_MEMORY; |
| 63 |
} |
| 64 |
(*derlist)[ (*count) ] = (der); |
| 65 |
(*count)++; |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
static SECStatus FileToItem(SECItem * dst, PRFileDesc * src) |
| 70 |
{ |
| 71 |
static const PRInt32 chunk = 65536; |
| 72 |
PRInt32 bytesReadTotal = 0; |
| 73 |
for (;;) { |
| 74 |
PRInt32 bytesReadNow; |
| 75 |
/* round up to next chunk size. note bytesReadTotal will usually be |
| 76 |
one byte short of a full chunk (hence we add one). further, in |
| 77 |
the event of an incomplete read on the prior pass (including due |
| 78 |
to EOF) we will avoid moving to a larger size buffer. */ |
| 79 |
PRInt32 newSize = (bytesReadTotal + chunk + 1) / chunk * chunk; |
| 80 |
if (newSize < chunk) |
| 81 |
/* int overflow */ |
| 82 |
break; |
| 83 |
|
| 84 |
if (SECITEM_ReallocItemV2(NULL, dst, newSize) != SECSuccess) |
| 85 |
/* out of memory */ |
| 86 |
break; |
| 87 |
|
| 88 |
/* subtract 1 to leave room for null terminator */ |
| 89 |
bytesReadNow = PR_Read(src, dst->data + bytesReadTotal, |
| 90 |
newSize - bytesReadTotal - 1); |
| 91 |
|
| 92 |
if (bytesReadNow < 0) |
| 93 |
/* read error */ |
| 94 |
break; |
| 95 |
|
| 96 |
if (bytesReadNow == 0) { |
| 97 |
/* EOF */ |
| 98 |
dst->data[bytesReadTotal] = '\0'; |
| 99 |
dst->len = bytesReadTotal; |
| 100 |
return SECSuccess; |
| 101 |
} |
| 102 |
|
| 103 |
bytesReadTotal += bytesReadNow; |
| 104 |
} |
| 105 |
|
| 106 |
free(dst->data); |
| 107 |
return SECFailure; |
| 108 |
} |
| 109 |
|
| 110 |
static SECStatus ConvertAsciiToZAllocItem(SECItem *der, const char *ascii) |
| 111 |
{ |
| 112 |
SECStatus rv = SECFailure; |
| 113 |
SECItem tmp; |
| 114 |
|
| 115 |
if (!der) |
| 116 |
return rv; |
| 117 |
|
| 118 |
der->data = tmp.data = NULL; |
| 119 |
der->len = tmp.len = 0; |
| 120 |
if (!NSSBase64_DecodeBuffer(NULL, &tmp, ascii, strlen(ascii))) |
| 121 |
return rv; |
| 122 |
|
| 123 |
der->data = NSS_ZAlloc(NULL, tmp.len); |
| 124 |
if (der->data) { |
| 125 |
rv = SECSuccess; |
| 126 |
memcpy(der->data, tmp.data, tmp.len); |
| 127 |
der->len = tmp.len; |
| 128 |
} |
| 129 |
SECITEM_FreeItem(&tmp, PR_FALSE); |
| 130 |
return rv; |
| 131 |
} |
| 132 |
|
| 133 |
/* returns count of objects read, or -1 on error */ |
| 134 |
int ReadDERFromFile(SECItem *** derlist, char *filename, int *cipher, |
| 135 |
char **ivstring, PRBool certsonly) |
| 136 |
{ |
| 137 |
SECStatus rv; |
| 138 |
PRFileDesc *inFile; |
| 139 |
int count = 0; |
| 140 |
SECItem *der = NULL; |
| 141 |
int error; |
| 142 |
SECItem filedata; |
| 143 |
char *c, *iv; |
| 144 |
|
| 145 |
memset(&filedata, 0, sizeof filedata); |
| 146 |
|
| 147 |
inFile = PR_Open(filename, PR_RDONLY, 0); |
| 148 |
if (!inFile) |
| 149 |
return -1; |
| 150 |
|
| 151 |
/* First convert ascii to binary */ |
| 152 |
char *asc, *body, *bufferEnd; |
| 153 |
|
| 154 |
/* Read in ascii data */ |
| 155 |
rv = FileToItem(&filedata, inFile); |
| 156 |
if (rv != SECSuccess) { |
| 157 |
PR_Close(inFile); |
| 158 |
return -1; |
| 159 |
} |
| 160 |
asc = (char *) filedata.data; |
| 161 |
if (!asc) { |
| 162 |
PR_Close(inFile); |
| 163 |
return -1; |
| 164 |
} |
| 165 |
bufferEnd = asc + filedata.len; |
| 166 |
|
| 167 |
/* check for headers and trailers and remove them */ |
| 168 |
if (strstr(asc, "-----BEGIN") != NULL) { |
| 169 |
int key = 0; |
| 170 |
while ((asc) && (asc < bufferEnd) && ((body = strstr(asc, "-----BEGIN")) != NULL)) { |
| 171 |
key = 0; |
| 172 |
if ((strncmp(body, "-----BEGIN RSA PRIVATE KEY", 25) == 0) || |
| 173 |
(strncmp(body, "-----BEGIN PRIVATE KEY", 21) == 0)) { |
| 174 |
key = 1; |
| 175 |
c = body; |
| 176 |
body = strchr(body, '\n'); |
| 177 |
if (NULL == body) |
| 178 |
goto loser; |
| 179 |
body++; |
| 180 |
if (strncmp(body, "Proc-Type: 4,ENCRYPTED", 22) == 0) { |
| 181 |
body = strchr(body, '\n'); |
| 182 |
if (NULL == body) |
| 183 |
goto loser; |
| 184 |
body++; |
| 185 |
if (strncmp(body, "DEK-Info: ", 10) == 0) { |
| 186 |
body += 10; |
| 187 |
c = body; |
| 188 |
body = strchr(body, ','); |
| 189 |
if (body == NULL) |
| 190 |
goto loser; |
| 191 |
*body = '\0'; |
| 192 |
if (!strcasecmp(c, "DES-EDE3-CBC")) |
| 193 |
*cipher = NSS_DES_EDE3_CBC; |
| 194 |
else if (!strcasecmp(c, "DES-CBC")) |
| 195 |
*cipher = NSS_DES_CBC; |
| 196 |
else { |
| 197 |
*cipher = -1; |
| 198 |
goto loser; |
| 199 |
} |
| 200 |
body++; |
| 201 |
iv = body; |
| 202 |
body = strchr(body, '\n'); |
| 203 |
if (body == NULL) |
| 204 |
goto loser; |
| 205 |
*body = '\0'; |
| 206 |
body++; |
| 207 |
*ivstring = PORT_Strdup(iv); |
| 208 |
} |
| 209 |
} else { /* Else the private key is not encrypted */ |
| 210 |
*cipher = 0; |
| 211 |
body = c; |
| 212 |
} |
| 213 |
} |
| 214 |
der = NSS_ZAlloc(NULL, sizeof(SECItem)); |
| 215 |
if (der == NULL) |
| 216 |
goto loser; |
| 217 |
|
| 218 |
char *trailer = NULL; |
| 219 |
asc = body; |
| 220 |
body = strchr(body, '\n'); |
| 221 |
if (!body) |
| 222 |
body = strchr(asc, '\r'); /* maybe this is a MAC file */ |
| 223 |
if (body) { |
| 224 |
trailer = strstr(++body, "-----END"); |
| 225 |
} |
| 226 |
if (trailer != NULL) { |
| 227 |
asc = trailer + 1; |
| 228 |
*trailer = '\0'; |
| 229 |
} else { |
| 230 |
goto loser; |
| 231 |
} |
| 232 |
|
| 233 |
/* Convert to binary */ |
| 234 |
rv = ConvertAsciiToZAllocItem(der, body); |
| 235 |
if (rv) { |
| 236 |
goto loser; |
| 237 |
} |
| 238 |
if ((certsonly && !key) || (!certsonly && key)) { |
| 239 |
error = put_object(der, derlist, &count); |
| 240 |
if (error) { |
| 241 |
goto loser; |
| 242 |
} |
| 243 |
der = NULL; |
| 244 |
} else { |
| 245 |
NSS_ZFreeIf(der->data); |
| 246 |
NSS_ZFreeIf(der); |
| 247 |
der = NULL; |
| 248 |
} |
| 249 |
} /* while */ |
| 250 |
} else { /* No headers and footers, translate the blob */ |
| 251 |
der = NSS_ZAlloc(NULL, sizeof(SECItem)); |
| 252 |
if (der == NULL) |
| 253 |
goto loser; |
| 254 |
|
| 255 |
rv = ConvertAsciiToZAllocItem(der, asc); |
| 256 |
if (rv) { |
| 257 |
goto loser; |
| 258 |
} |
| 259 |
/* NOTE: This code path has never been tested. */ |
| 260 |
error = put_object(der, derlist, &count); |
| 261 |
if (error) { |
| 262 |
goto loser; |
| 263 |
} |
| 264 |
der = NULL; |
| 265 |
} |
| 266 |
|
| 267 |
free(filedata.data); |
| 268 |
PR_Close(inFile); |
| 269 |
return count; |
| 270 |
|
| 271 |
loser: |
| 272 |
free(filedata.data); |
| 273 |
if (der) { |
| 274 |
NSS_ZFreeIf(der->data); |
| 275 |
NSS_ZFreeIf(der); |
| 276 |
} |
| 277 |
PR_Close(inFile); |
| 278 |
return -1; |
| 279 |
} |
| 280 |
|
| 281 |
#ifdef DEBUG |
| 282 |
#define LOGGING_BUFFER_SIZE 400 |
| 283 |
#define PEM_DEFAULT_LOG_FILE "/tmp/pkcs11.log" |
| 284 |
static const char *pemLogModuleName = "PEM"; |
| 285 |
static PRLogModuleInfo* pemLogModule = NULL; |
| 286 |
#endif |
| 287 |
|
| 288 |
void open_nss_pem_log() |
| 289 |
{ |
| 290 |
#ifdef DEBUG |
| 291 |
const char *nsprLogFile = PR_GetEnv("NSPR_LOG_FILE"); |
| 292 |
|
| 293 |
if (!pemLogModule) { |
| 294 |
pemLogModule = PR_NewLogModule(pemLogModuleName); |
| 295 |
} |
| 296 |
|
| 297 |
(void) PR_SetLogFile(nsprLogFile ? nsprLogFile : PEM_DEFAULT_LOG_FILE); |
| 298 |
/* If false, the log file will remain what it was before */ |
| 299 |
#endif |
| 300 |
} |
| 301 |
|
| 302 |
void plog(const char *fmt, ...) |
| 303 |
{ |
| 304 |
#ifdef DEBUG |
| 305 |
char buf[LOGGING_BUFFER_SIZE]; |
| 306 |
va_list ap; |
| 307 |
|
| 308 |
va_start(ap, fmt); |
| 309 |
PR_vsnprintf(buf, sizeof(buf), fmt, ap); |
| 310 |
va_end(ap); |
| 311 |
PR_LOG(pemLogModule, PR_LOG_DEBUG, ("%s", buf)); |
| 312 |
#endif |
| 313 |
} |