| 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 |
#include "ckpem.h" |
| 39 |
|
| 40 |
#include <base64.h> |
| 41 |
#include <blapi.h> |
| 42 |
#include <pk11pub.h> |
| 43 |
#include <secmodt.h> |
| 44 |
|
| 45 |
/* |
| 46 |
* psession.c |
| 47 |
* |
| 48 |
* This file implements the NSSCKMDSession object for the |
| 49 |
* "PEM objects" cryptoki module. |
| 50 |
*/ |
| 51 |
|
| 52 |
NSS_EXTERN_DATA const SEC_ASN1Template pem_RSAPrivateKeyTemplate[]; |
| 53 |
|
| 54 |
void prepare_low_rsa_priv_key_for_asn1(NSSLOWKEYPrivateKey * key); |
| 55 |
void pem_DestroyPrivateKey(NSSLOWKEYPrivateKey * privk); |
| 56 |
|
| 57 |
/* |
| 58 |
* Convert a hex string into bytes. |
| 59 |
*/ |
| 60 |
static unsigned char *convert_iv(char *src, int num) |
| 61 |
{ |
| 62 |
int i; |
| 63 |
char conv[3]; |
| 64 |
unsigned char *c; |
| 65 |
|
| 66 |
c = (unsigned char *) NSS_ZAlloc(NULL, (num) + 1); |
| 67 |
if (c == NULL) |
| 68 |
return NULL; |
| 69 |
|
| 70 |
conv[2] = '\0'; |
| 71 |
memset(c, 0, num); |
| 72 |
for (i = 0; i < num; i++) { |
| 73 |
conv[0] = src[(i * 2)]; |
| 74 |
conv[1] = src[(i * 2) + 1]; |
| 75 |
c[i] = strtol(conv, NULL, 16); |
| 76 |
} |
| 77 |
return c; |
| 78 |
} |
| 79 |
|
| 80 |
/* |
| 81 |
* The key is a 24-bit hash. The first 16 bits are the MD5 hash of the |
| 82 |
* password and the IV (salt). This has is then re-hashed with the |
| 83 |
* password and IV again and the first 8 bytes of that are the remaining |
| 84 |
* bytes of the 24-bit key. |
| 85 |
*/ |
| 86 |
static int |
| 87 |
make_key(const unsigned char *salt, const unsigned char *data, int len, |
| 88 |
unsigned char *key) |
| 89 |
{ |
| 90 |
int nkey = 0; |
| 91 |
MD5Context *ctx; |
| 92 |
unsigned int digestLen; |
| 93 |
int count, i; |
| 94 |
unsigned char H[25]; |
| 95 |
|
| 96 |
/* obtain MD5 hash object */ |
| 97 |
const SECHashObject *hobj = HASH_GetRawHashObject(HASH_AlgMD5); |
| 98 |
if (!hobj) |
| 99 |
return -1; |
| 100 |
|
| 101 |
/* initialize MD5 context */ |
| 102 |
ctx = hobj->create(); |
| 103 |
if (!ctx) |
| 104 |
return -1; |
| 105 |
|
| 106 |
nkey = 24; |
| 107 |
count = 0; |
| 108 |
|
| 109 |
while (nkey > 0) { |
| 110 |
hobj->begin(ctx); |
| 111 |
if (count) |
| 112 |
hobj->update(ctx, H, digestLen); |
| 113 |
hobj->update(ctx, data, len); |
| 114 |
hobj->update(ctx, salt, 8U); |
| 115 |
hobj->end(ctx, H, &digestLen, sizeof(H)); |
| 116 |
|
| 117 |
i = 0; |
| 118 |
while (nkey && (i != digestLen)) { |
| 119 |
*(key++) = H[i]; |
| 120 |
nkey--; |
| 121 |
i++; |
| 122 |
} |
| 123 |
count++; |
| 124 |
} |
| 125 |
|
| 126 |
/* destroy MD5 context */ |
| 127 |
hobj->destroy(ctx, /* freeit */PR_TRUE); |
| 128 |
|
| 129 |
return 24; |
| 130 |
} |
| 131 |
|
| 132 |
static NSSCKMDFindObjects * |
| 133 |
pem_mdSession_FindObjectsInit |
| 134 |
( |
| 135 |
NSSCKMDSession * mdSession, |
| 136 |
NSSCKFWSession * fwSession, |
| 137 |
NSSCKMDToken * mdToken, |
| 138 |
NSSCKFWToken * fwToken, |
| 139 |
NSSCKMDInstance * mdInstance, |
| 140 |
NSSCKFWInstance * fwInstance, |
| 141 |
CK_ATTRIBUTE_PTR pTemplate, |
| 142 |
CK_ULONG ulAttributeCount, |
| 143 |
CK_RV * pError |
| 144 |
) |
| 145 |
{ |
| 146 |
plog("mdSession_FindObjectsInit\n"); |
| 147 |
return pem_FindObjectsInit(fwSession, pTemplate, ulAttributeCount, |
| 148 |
pError); |
| 149 |
} |
| 150 |
|
| 151 |
static NSSCKMDObject * |
| 152 |
pem_mdSession_CreateObject |
| 153 |
( |
| 154 |
NSSCKMDSession * mdSession, |
| 155 |
NSSCKFWSession * fwSession, |
| 156 |
NSSCKMDToken * mdToken, |
| 157 |
NSSCKFWToken * fwToken, |
| 158 |
NSSCKMDInstance * mdInstance, |
| 159 |
NSSCKFWInstance * fwInstance, |
| 160 |
NSSArena * arena, |
| 161 |
CK_ATTRIBUTE_PTR pTemplate, |
| 162 |
CK_ULONG ulAttributeCount, |
| 163 |
CK_RV * pError |
| 164 |
) |
| 165 |
{ |
| 166 |
plog("mdSession_CreateObject\n"); |
| 167 |
return pem_CreateObject(fwInstance, fwSession, mdToken, pTemplate, |
| 168 |
ulAttributeCount, pError); |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
* increase refCount of internal object(s) |
| 173 |
*/ |
| 174 |
NSSCKMDObject * |
| 175 |
pem_mdSession_CopyObject |
| 176 |
( |
| 177 |
NSSCKMDSession * mdSession, |
| 178 |
NSSCKFWSession * fwSession, |
| 179 |
NSSCKMDToken * mdToken, |
| 180 |
NSSCKFWToken * fwToken, |
| 181 |
NSSCKMDInstance * mdInstance, |
| 182 |
NSSCKFWInstance * fwInstance, |
| 183 |
NSSCKMDObject * mdOldObject, |
| 184 |
NSSCKFWObject * fwOldObject, |
| 185 |
NSSArena * arena, |
| 186 |
CK_ATTRIBUTE_PTR pTemplate, |
| 187 |
CK_ULONG ulAttributeCount, |
| 188 |
CK_RV * pError |
| 189 |
) |
| 190 |
{ |
| 191 |
NSSCKMDObject *rvmdObject = NULL; |
| 192 |
pemInternalObject *io = (pemInternalObject *) mdOldObject->etc; |
| 193 |
|
| 194 |
/* make a new mdObject */ |
| 195 |
rvmdObject = NSS_ZNEW(arena, NSSCKMDObject); |
| 196 |
if ((NSSCKMDObject *) NULL == rvmdObject) { |
| 197 |
*pError = CKR_HOST_MEMORY; |
| 198 |
return (NSSCKMDObject *) NULL; |
| 199 |
} |
| 200 |
|
| 201 |
if (NULL == io->list) { |
| 202 |
io->refCount ++; |
| 203 |
} else { |
| 204 |
/* go through list of objects */ |
| 205 |
pemObjectListItem *item = io->list; |
| 206 |
while (item) { |
| 207 |
item->io->refCount ++; |
| 208 |
item = item->next; |
| 209 |
} |
| 210 |
} |
| 211 |
/* struct (shallow) copy the old one */ |
| 212 |
*rvmdObject = *mdOldObject; |
| 213 |
|
| 214 |
return rvmdObject; |
| 215 |
} |
| 216 |
|
| 217 |
CK_RV |
| 218 |
pem_mdSession_Login |
| 219 |
( |
| 220 |
NSSCKMDSession * mdSession, |
| 221 |
NSSCKFWSession * fwSession, |
| 222 |
NSSCKMDToken * mdToken, |
| 223 |
NSSCKFWToken * fwToken, |
| 224 |
NSSCKMDInstance * mdInstance, |
| 225 |
NSSCKFWInstance * fwInstance, |
| 226 |
CK_USER_TYPE userType, |
| 227 |
NSSItem * pin, |
| 228 |
CK_STATE oldState, |
| 229 |
CK_STATE newState |
| 230 |
) |
| 231 |
{ |
| 232 |
NSSCKFWSlot *fwSlot; |
| 233 |
CK_SLOT_ID slotID; |
| 234 |
pemInternalObject *io = NULL; |
| 235 |
unsigned char *iv = 0; |
| 236 |
unsigned char mykey[32]; |
| 237 |
unsigned char *output = NULL; |
| 238 |
DESContext *cx = NULL; |
| 239 |
SECStatus rv; |
| 240 |
unsigned int len = 0; |
| 241 |
NSSLOWKEYPrivateKey *lpk = NULL; |
| 242 |
PLArenaPool *arena; |
| 243 |
SECItem plain; |
| 244 |
pemInternalObject *curObj; |
| 245 |
|
| 246 |
fwSlot = NSSCKFWToken_GetFWSlot(fwToken); |
| 247 |
slotID = NSSCKFWSlot_GetSlotID(fwSlot); |
| 248 |
|
| 249 |
arena = PORT_NewArena(2048); |
| 250 |
if (!arena) { |
| 251 |
return CKR_HOST_MEMORY; |
| 252 |
} |
| 253 |
|
| 254 |
plog("pem_mdSession_Login '%s'\n", (char *) pin->data); |
| 255 |
|
| 256 |
token_needsLogin[slotID - 1] = PR_FALSE; |
| 257 |
|
| 258 |
/* Find the right key object */ |
| 259 |
list_for_each_entry_reverse(curObj, &pem_objs, gl_list) { |
| 260 |
if ((slotID == curObj->slotID) && (curObj->type == pemBareKey)) { |
| 261 |
io = curObj; |
| 262 |
break; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (NULL == io) { |
| 267 |
rv = CKR_SLOT_ID_INVALID; |
| 268 |
goto loser; |
| 269 |
} |
| 270 |
|
| 271 |
/* Convert the IV from hex into an array of bytes */ |
| 272 |
if (NULL == io->u.key.ivstring) { |
| 273 |
rv = CKR_PIN_INCORRECT; |
| 274 |
goto loser; |
| 275 |
} |
| 276 |
iv = convert_iv(io->u.key.ivstring, 8); |
| 277 |
|
| 278 |
/* Convert the PIN and IV into a DES key */ |
| 279 |
make_key(iv, pin->data, pin->size, mykey); |
| 280 |
|
| 281 |
output = |
| 282 |
(unsigned char *) NSS_ZAlloc(NULL, |
| 283 |
(io->u.key.key.privateKey->len + 1)); |
| 284 |
if (!output) { |
| 285 |
rv = CKR_HOST_MEMORY; |
| 286 |
goto loser; |
| 287 |
} |
| 288 |
|
| 289 |
cx = DES_CreateContext((const unsigned char *) mykey, iv, |
| 290 |
io->u.key.cipher, PR_FALSE); |
| 291 |
if (!cx) { |
| 292 |
rv = CKR_HOST_MEMORY; |
| 293 |
goto loser; |
| 294 |
} |
| 295 |
|
| 296 |
rv = DES_Decrypt(cx, output, &len, io->u.key.key.privateKey->len, |
| 297 |
io->u.key.key.privateKey->data, |
| 298 |
io->u.key.key.privateKey->len); |
| 299 |
DES_DestroyContext(cx, PR_TRUE); |
| 300 |
|
| 301 |
NSS_ZFreeIf(iv); |
| 302 |
iv = NULL; |
| 303 |
if (rv != SECSuccess) { |
| 304 |
rv = CKR_PIN_INCORRECT; |
| 305 |
goto loser; |
| 306 |
} |
| 307 |
|
| 308 |
lpk = (NSSLOWKEYPrivateKey *) NSS_ZAlloc(NULL, |
| 309 |
sizeof (NSSLOWKEYPrivateKey)); |
| 310 |
if (lpk == NULL) { |
| 311 |
rv = CKR_HOST_MEMORY; |
| 312 |
goto loser; |
| 313 |
} |
| 314 |
|
| 315 |
lpk->arena = arena; |
| 316 |
lpk->keyType = NSSLOWKEYRSAKey; |
| 317 |
prepare_low_rsa_priv_key_for_asn1(lpk); |
| 318 |
|
| 319 |
|
| 320 |
/* Decode the resulting blob and see if it is a decodable DER that fits |
| 321 |
* our private key template. If so we declare success and move on. If not |
| 322 |
* then we return an error. |
| 323 |
*/ |
| 324 |
memset(&plain, 0, sizeof(plain)); |
| 325 |
plain.data = output; |
| 326 |
plain.len = len - output[len - 1]; |
| 327 |
rv = SEC_QuickDERDecodeItem(arena, lpk, pem_RSAPrivateKeyTemplate, |
| 328 |
&plain); |
| 329 |
pem_DestroyPrivateKey(lpk); |
| 330 |
arena = NULL; |
| 331 |
if (rv != SECSuccess) |
| 332 |
goto loser; |
| 333 |
|
| 334 |
NSS_ZFreeIf(io->u.key.key.privateKey->data); |
| 335 |
io->u.key.key.privateKey->len = len - output[len - 1]; |
| 336 |
io->u.key.key.privateKey->data = |
| 337 |
(void *) NSS_ZAlloc(NULL, io->u.key.key.privateKey->len); |
| 338 |
memcpy(io->u.key.key.privateKey->data, output, len - output[len - 1]); |
| 339 |
|
| 340 |
rv = CKR_OK; |
| 341 |
|
| 342 |
loser: |
| 343 |
if (arena) |
| 344 |
PORT_FreeArena(arena, PR_FALSE); |
| 345 |
NSS_ZFreeIf(iv); |
| 346 |
NSS_ZFreeIf(output); |
| 347 |
|
| 348 |
return rv; |
| 349 |
} |
| 350 |
|
| 351 |
NSS_IMPLEMENT NSSCKMDSession * |
| 352 |
pem_CreateSession |
| 353 |
( |
| 354 |
NSSCKFWSession * fwSession, |
| 355 |
CK_RV * pError |
| 356 |
) |
| 357 |
{ |
| 358 |
NSSArena *arena; |
| 359 |
NSSCKMDSession *rv; |
| 360 |
|
| 361 |
plog("pem_CreateSession returning new session\n"); |
| 362 |
arena = NSSCKFWSession_GetArena(fwSession, pError); |
| 363 |
if ((NSSArena *) NULL == arena) { |
| 364 |
return (NSSCKMDSession *) NULL; |
| 365 |
} |
| 366 |
|
| 367 |
rv = NSS_ZNEW(arena, NSSCKMDSession); |
| 368 |
if ((NSSCKMDSession *) NULL == rv) { |
| 369 |
*pError = CKR_HOST_MEMORY; |
| 370 |
return (NSSCKMDSession *) NULL; |
| 371 |
} |
| 372 |
|
| 373 |
/* |
| 374 |
* rv was zeroed when allocated, so we only |
| 375 |
* need to set the non-zero members. |
| 376 |
*/ |
| 377 |
|
| 378 |
rv->etc = (void *) fwSession; |
| 379 |
/* rv->Close */ |
| 380 |
/* rv->GetDeviceError */ |
| 381 |
rv->Login = pem_mdSession_Login; |
| 382 |
/* rv->Logout */ |
| 383 |
/* rv->InitPIN */ |
| 384 |
/* rv->SetPIN */ |
| 385 |
/* rv->GetOperationStateLen */ |
| 386 |
/* rv->GetOperationState */ |
| 387 |
/* rv->SetOperationState */ |
| 388 |
rv->CreateObject = pem_mdSession_CreateObject; |
| 389 |
rv->CopyObject = pem_mdSession_CopyObject; |
| 390 |
rv->FindObjectsInit = pem_mdSession_FindObjectsInit; |
| 391 |
/* rv->SeedRandom */ |
| 392 |
/* rv->GetRandom */ |
| 393 |
/* rv->null */ |
| 394 |
|
| 395 |
return rv; |
| 396 |
} |