| 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 |
/* |
| 41 |
* prsa.c |
| 42 |
* |
| 43 |
* This file implements the NSSCKMDMechnaism and NSSCKMDCryptoOperation objects |
| 44 |
* for the RSA operation. |
| 45 |
*/ |
| 46 |
|
| 47 |
#include <nssckmdt.h> |
| 48 |
#include <secdert.h> |
| 49 |
#include <secoid.h> |
| 50 |
|
| 51 |
#define SSL3_SHAMD5_HASH_SIZE 36 /* LEN_MD5 (16) + LEN_SHA1 (20) */ |
| 52 |
|
| 53 |
#ifdef HAVE_LOWKEYTI_H |
| 54 |
# include <lowkeyti.h> |
| 55 |
#else |
| 56 |
/* |
| 57 |
* we are dealing with older NSS that does not include the patch |
| 58 |
* for https://bugzilla.mozilla.org/show_bug.cgi?id=1263179 yet |
| 59 |
*/ |
| 60 |
struct NSSLOWKEYAttributeStr { |
| 61 |
SECItem attrType; |
| 62 |
SECItem *attrValue; |
| 63 |
}; |
| 64 |
typedef struct NSSLOWKEYAttributeStr NSSLOWKEYAttribute; |
| 65 |
struct NSSLOWKEYPrivateKeyInfoStr { |
| 66 |
PLArenaPool *arena; |
| 67 |
SECItem version; |
| 68 |
SECAlgorithmID algorithm; |
| 69 |
SECItem privateKey; |
| 70 |
NSSLOWKEYAttribute **attributes; |
| 71 |
}; |
| 72 |
typedef struct NSSLOWKEYPrivateKeyInfoStr NSSLOWKEYPrivateKeyInfo; |
| 73 |
#endif |
| 74 |
|
| 75 |
const SEC_ASN1Template pem_RSAPrivateKeyTemplate[] = { |
| 76 |
{SEC_ASN1_SEQUENCE, 0, NULL, sizeof(NSSLOWKEYPrivateKey)} , |
| 77 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.version)}, |
| 78 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.modulus)}, |
| 79 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.publicExponent)}, |
| 80 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.privateExponent)}, |
| 81 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.prime1)}, |
| 82 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.prime2)}, |
| 83 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.exponent1)}, |
| 84 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.exponent2)}, |
| 85 |
{SEC_ASN1_INTEGER, offsetof(NSSLOWKEYPrivateKey, u.rsa.coefficient)}, |
| 86 |
{0} |
| 87 |
}; |
| 88 |
|
| 89 |
static const SEC_ASN1Template pem_AttributeTemplate[] = { |
| 90 |
{ SEC_ASN1_SEQUENCE, |
| 91 |
0, NULL, sizeof(NSSLOWKEYAttribute) }, |
| 92 |
{ SEC_ASN1_OBJECT_ID, offsetof(NSSLOWKEYAttribute, attrType) }, |
| 93 |
{ SEC_ASN1_SET_OF | SEC_ASN1_XTRN, offsetof(NSSLOWKEYAttribute, attrValue), |
| 94 |
SEC_ASN1_SUB(SEC_AnyTemplate) }, |
| 95 |
{ 0 } |
| 96 |
}; |
| 97 |
|
| 98 |
static const SEC_ASN1Template pem_SetOfAttributeTemplate[] = { |
| 99 |
{ SEC_ASN1_SET_OF, 0, pem_AttributeTemplate }, |
| 100 |
}; |
| 101 |
|
| 102 |
const SEC_ASN1Template pem_PrivateKeyInfoTemplate[] = { |
| 103 |
{ SEC_ASN1_SEQUENCE, |
| 104 |
0, NULL, sizeof(NSSLOWKEYPrivateKeyInfo) }, |
| 105 |
{ SEC_ASN1_INTEGER, |
| 106 |
offsetof(NSSLOWKEYPrivateKeyInfo,version) }, |
| 107 |
{ SEC_ASN1_INLINE | SEC_ASN1_XTRN, |
| 108 |
offsetof(NSSLOWKEYPrivateKeyInfo,algorithm), |
| 109 |
SEC_ASN1_SUB(SECOID_AlgorithmIDTemplate) }, |
| 110 |
{ SEC_ASN1_OCTET_STRING, |
| 111 |
offsetof(NSSLOWKEYPrivateKeyInfo,privateKey) }, |
| 112 |
{ SEC_ASN1_OPTIONAL | SEC_ASN1_CONSTRUCTED | SEC_ASN1_CONTEXT_SPECIFIC | 0, |
| 113 |
offsetof(NSSLOWKEYPrivateKeyInfo, attributes), |
| 114 |
pem_SetOfAttributeTemplate }, |
| 115 |
{ 0 } |
| 116 |
}; |
| 117 |
|
| 118 |
/* Declarations */ |
| 119 |
SECStatus pem_RSA_Sign(NSSLOWKEYPrivateKey * key, unsigned char *output, |
| 120 |
unsigned int *outputLen, unsigned int maxOutputLen, |
| 121 |
unsigned char *input, unsigned int inputLen); |
| 122 |
SECStatus pem_RSA_DecryptBlock(NSSLOWKEYPrivateKey * key, |
| 123 |
unsigned char *output, unsigned int *outputLen, |
| 124 |
unsigned int maxOutputLen, unsigned char *input, |
| 125 |
unsigned int inputLen); |
| 126 |
|
| 127 |
void prepare_low_rsa_priv_key_for_asn1(NSSLOWKEYPrivateKey * key) |
| 128 |
{ |
| 129 |
key->u.rsa.modulus.type = siUnsignedInteger; |
| 130 |
key->u.rsa.publicExponent.type = siUnsignedInteger; |
| 131 |
key->u.rsa.privateExponent.type = siUnsignedInteger; |
| 132 |
key->u.rsa.prime1.type = siUnsignedInteger; |
| 133 |
key->u.rsa.prime2.type = siUnsignedInteger; |
| 134 |
key->u.rsa.exponent1.type = siUnsignedInteger; |
| 135 |
key->u.rsa.exponent2.type = siUnsignedInteger; |
| 136 |
key->u.rsa.coefficient.type = siUnsignedInteger; |
| 137 |
} |
| 138 |
|
| 139 |
unsigned int |
| 140 |
pem_PrivateModulusLen(NSSLOWKEYPrivateKey * privk) |
| 141 |
{ |
| 142 |
|
| 143 |
unsigned char b0; |
| 144 |
|
| 145 |
switch (privk->keyType) { |
| 146 |
case NSSLOWKEYRSAKey: |
| 147 |
b0 = privk->u.rsa.modulus.data[0]; |
| 148 |
return b0 ? privk->u.rsa.modulus.len : privk->u.rsa.modulus.len - |
| 149 |
1; |
| 150 |
default: |
| 151 |
break; |
| 152 |
} |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
struct SFTKHashSignInfoStr { |
| 157 |
SECOidTag hashOid; |
| 158 |
NSSLOWKEYPrivateKey *key; |
| 159 |
}; |
| 160 |
typedef struct SFTKHashSignInfoStr SFTKHashSignInfo; |
| 161 |
|
| 162 |
void |
| 163 |
pem_DestroyPrivateKey(NSSLOWKEYPrivateKey * privk) |
| 164 |
{ |
| 165 |
if (privk && privk->arena) { |
| 166 |
PORT_FreeArena(privk->arena, PR_TRUE); |
| 167 |
} |
| 168 |
NSS_ZFreeIf(privk); |
| 169 |
} |
| 170 |
|
| 171 |
/* decode and parse the rawkey into the lpk structure */ |
| 172 |
static NSSLOWKEYPrivateKey * |
| 173 |
pem_getPrivateKey(PLArenaPool *arena, SECItem *rawkey, CK_RV * pError, NSSItem *modulus) |
| 174 |
{ |
| 175 |
NSSLOWKEYPrivateKey *lpk = NULL; |
| 176 |
SECStatus rv = SECFailure; |
| 177 |
NSSLOWKEYPrivateKeyInfo *pki = NULL; |
| 178 |
SECItem *keysrc = NULL; |
| 179 |
|
| 180 |
/* make sure SECOID is initialized - not sure why we have to do this outside of nss_Init */ |
| 181 |
if (SECSuccess != (rv = SECOID_Init())) { |
| 182 |
*pError = CKR_GENERAL_ERROR; |
| 183 |
return NULL; /* wha???? */ |
| 184 |
} |
| 185 |
|
| 186 |
pki = (NSSLOWKEYPrivateKeyInfo*)PORT_ArenaZAlloc(arena, |
| 187 |
sizeof(NSSLOWKEYPrivateKeyInfo)); |
| 188 |
if(!pki) { |
| 189 |
*pError = CKR_HOST_MEMORY; |
| 190 |
goto done; |
| 191 |
} |
| 192 |
|
| 193 |
/* let's first see if this is a "raw" RSA private key or an RSA private key in PKCS#8 format */ |
| 194 |
rv = SEC_ASN1DecodeItem(arena, pki, pem_PrivateKeyInfoTemplate, rawkey); |
| 195 |
if (rv != SECSuccess) { |
| 196 |
/* not PKCS#8 - assume it's a "raw" RSA private key */ |
| 197 |
plog("Failed to decode key, assuming raw RSA private key\n"); |
| 198 |
keysrc = rawkey; |
| 199 |
} else if (SECOID_GetAlgorithmTag(&pki->algorithm) == SEC_OID_PKCS1_RSA_ENCRYPTION) { |
| 200 |
keysrc = &pki->privateKey; |
| 201 |
} else { /* unsupported */ |
| 202 |
*pError = CKR_FUNCTION_NOT_SUPPORTED; |
| 203 |
goto done; |
| 204 |
} |
| 205 |
|
| 206 |
lpk = (NSSLOWKEYPrivateKey *) NSS_ZAlloc(NULL, |
| 207 |
sizeof(NSSLOWKEYPrivateKey)); |
| 208 |
if (lpk == NULL) { |
| 209 |
*pError = CKR_HOST_MEMORY; |
| 210 |
goto done; |
| 211 |
} |
| 212 |
|
| 213 |
lpk->arena = arena; |
| 214 |
lpk->keyType = NSSLOWKEYRSAKey; |
| 215 |
prepare_low_rsa_priv_key_for_asn1(lpk); |
| 216 |
|
| 217 |
/* I don't know what this is supposed to accomplish. We free the old |
| 218 |
modulus data and set it again, making a copy of the new data. |
| 219 |
But we just allocated a new empty key structure above with |
| 220 |
NSS_ZAlloc. So lpk->u.rsa.modulus.data is NULL and |
| 221 |
lpk->u.rsa.modulus.len. If the intention is to free the old |
| 222 |
modulus data, why not just set it to NULL after freeing? Why |
| 223 |
go through this unnecessary and confusing copying code? |
| 224 |
*/ |
| 225 |
if (modulus) { |
| 226 |
NSS_ZFreeIf(modulus->data); |
| 227 |
modulus->data = (void *) NSS_ZAlloc(NULL, lpk->u.rsa.modulus.len); |
| 228 |
modulus->size = lpk->u.rsa.modulus.len; |
| 229 |
memcpy(modulus->data, |
| 230 |
lpk->u.rsa.modulus.data, |
| 231 |
lpk->u.rsa.modulus.len); |
| 232 |
} |
| 233 |
|
| 234 |
/* decode the private key and any algorithm parameters */ |
| 235 |
rv = SEC_QuickDERDecodeItem(arena, lpk, pem_RSAPrivateKeyTemplate, |
| 236 |
keysrc); |
| 237 |
|
| 238 |
if (rv != SECSuccess) { |
| 239 |
plog("SEC_QuickDERDecodeItem failed\n"); |
| 240 |
*pError = CKR_KEY_TYPE_INCONSISTENT; |
| 241 |
/* do not use pem_DestroyPrivateKey() to avoid double free of arena */ |
| 242 |
NSS_ZFreeIf(lpk); |
| 243 |
return NULL; |
| 244 |
} |
| 245 |
|
| 246 |
done: |
| 247 |
return lpk; |
| 248 |
} |
| 249 |
|
| 250 |
CK_RV |
| 251 |
pem_PopulateModulusExponent(pemInternalObject * io) |
| 252 |
{ |
| 253 |
CK_RV error = CKR_OK; |
| 254 |
const NSSItem *classItem; |
| 255 |
const NSSItem *keyType; |
| 256 |
NSSLOWKEYPrivateKey *lpk = NULL; |
| 257 |
PLArenaPool *arena; |
| 258 |
|
| 259 |
classItem = pem_FetchAttribute(io, CKA_CLASS, &error); |
| 260 |
if (error != CKR_OK) |
| 261 |
return error; |
| 262 |
|
| 263 |
keyType = pem_FetchAttribute(io, CKA_KEY_TYPE, &error); |
| 264 |
if (error != CKR_OK) |
| 265 |
return error; |
| 266 |
|
| 267 |
/* make sure we have the right objects */ |
| 268 |
if (((const NSSItem *) NULL == classItem) || |
| 269 |
(sizeof(CK_OBJECT_CLASS) != classItem->size) || |
| 270 |
(CKO_PRIVATE_KEY != *(CK_OBJECT_CLASS *) classItem->data) || |
| 271 |
((const NSSItem *) NULL == keyType) || |
| 272 |
(sizeof(CK_KEY_TYPE) != keyType->size) || |
| 273 |
(CKK_RSA != *(CK_KEY_TYPE *) keyType->data)) { |
| 274 |
return CKR_KEY_TYPE_INCONSISTENT; |
| 275 |
} |
| 276 |
|
| 277 |
arena = PORT_NewArena(2048); |
| 278 |
if (!arena) { |
| 279 |
return CKR_HOST_MEMORY; |
| 280 |
} |
| 281 |
|
| 282 |
lpk = pem_getPrivateKey(arena, io->u.key.key.privateKey, &error, NULL); |
| 283 |
if (lpk == NULL) { |
| 284 |
plog("pem_PopulateModulusExponent: pem_getPrivateKey returned NULL, error 0x%08x\n", error); |
| 285 |
PORT_FreeArena(arena, PR_FALSE); |
| 286 |
return (error ? error : CKR_KEY_TYPE_INCONSISTENT); |
| 287 |
} |
| 288 |
|
| 289 |
NSS_ZFreeIf(io->u.key.key.modulus.data); |
| 290 |
io->u.key.key.modulus.data = |
| 291 |
(void *) NSS_ZAlloc(NULL, lpk->u.rsa.modulus.len); |
| 292 |
io->u.key.key.modulus.size = lpk->u.rsa.modulus.len; |
| 293 |
memcpy(io->u.key.key.modulus.data, |
| 294 |
lpk->u.rsa.modulus.data, |
| 295 |
lpk->u.rsa.modulus.len); |
| 296 |
|
| 297 |
NSS_ZFreeIf(io->u.key.key.exponent.data); |
| 298 |
io->u.key.key.exponent.data = |
| 299 |
(void *) NSS_ZAlloc(NULL, lpk->u.rsa.publicExponent.len); |
| 300 |
io->u.key.key.exponent.size = lpk->u.rsa.publicExponent.len; |
| 301 |
memcpy(io->u.key.key.exponent.data, |
| 302 |
lpk->u.rsa.publicExponent.data, |
| 303 |
lpk->u.rsa.publicExponent.len); |
| 304 |
|
| 305 |
NSS_ZFreeIf(io->u.key.key.privateExponent.data); |
| 306 |
io->u.key.key.privateExponent.data = |
| 307 |
(void *) NSS_ZAlloc(NULL, lpk->u.rsa.privateExponent.len); |
| 308 |
io->u.key.key.privateExponent.size = lpk->u.rsa.privateExponent.len; |
| 309 |
memcpy(io->u.key.key.privateExponent.data, |
| 310 |
lpk->u.rsa.privateExponent.data, |
| 311 |
lpk->u.rsa.privateExponent.len); |
| 312 |
|
| 313 |
NSS_ZFreeIf(io->u.key.key.prime1.data); |
| 314 |
io->u.key.key.prime1.data = |
| 315 |
(void *) NSS_ZAlloc(NULL, lpk->u.rsa.prime1.len); |
| 316 |
io->u.key.key.prime1.size = lpk->u.rsa.prime1.len; |
| 317 |
memcpy(io->u.key.key.prime1.data, |
| 318 |
lpk->u.rsa.prime1.data, |
| 319 |
lpk->u.rsa.prime1.len); |
| 320 |
|
| 321 |
NSS_ZFreeIf(io->u.key.key.prime2.data); |
| 322 |
io->u.key.key.prime2.data = |
| 323 |
(void *) NSS_ZAlloc(NULL, lpk->u.rsa.prime2.len); |
| 324 |
io->u.key.key.prime2.size = lpk->u.rsa.prime2.len; |
| 325 |
memcpy(io->u.key.key.prime2.data, |
| 326 |
lpk->u.rsa.prime2.data, |
| 327 |
lpk->u.rsa.prime2.len); |
| 328 |
|
| 329 |
NSS_ZFreeIf(io->u.key.key.exponent1.data); |
| 330 |
io->u.key.key.exponent1.data = |
| 331 |
(void *) NSS_ZAlloc(NULL, lpk->u.rsa.exponent1.len); |
| 332 |
io->u.key.key.exponent1.size = lpk->u.rsa.exponent1.len; |
| 333 |
memcpy(io->u.key.key.exponent1.data, |
| 334 |
lpk->u.rsa.exponent1.data, |
| 335 |
lpk->u.rsa.exponent1.len); |
| 336 |
|
| 337 |
NSS_ZFreeIf(io->u.key.key.exponent2.data); |
| 338 |
io->u.key.key.exponent2.data = |
| 339 |
(void *) NSS_ZAlloc(NULL, lpk->u.rsa.exponent2.len); |
| 340 |
io->u.key.key.exponent2.size = lpk->u.rsa.exponent2.len; |
| 341 |
memcpy(io->u.key.key.exponent2.data, |
| 342 |
lpk->u.rsa.exponent2.data, |
| 343 |
lpk->u.rsa.exponent2.len); |
| 344 |
|
| 345 |
NSS_ZFreeIf(io->u.key.key.coefficient.data); |
| 346 |
io->u.key.key.coefficient.data = |
| 347 |
(void *) NSS_ZAlloc(NULL, lpk->u.rsa.coefficient.len); |
| 348 |
io->u.key.key.coefficient.size = lpk->u.rsa.coefficient.len; |
| 349 |
memcpy(io->u.key.key.coefficient.data, |
| 350 |
lpk->u.rsa.coefficient.data, |
| 351 |
lpk->u.rsa.coefficient.len); |
| 352 |
|
| 353 |
pem_DestroyPrivateKey(lpk); |
| 354 |
return CKR_OK; |
| 355 |
} |
| 356 |
|
| 357 |
typedef struct pemInternalCryptoOperationRSAPrivStr |
| 358 |
pemInternalCryptoOperationRSAPriv; |
| 359 |
struct pemInternalCryptoOperationRSAPrivStr |
| 360 |
{ |
| 361 |
NSSCKMDCryptoOperation mdOperation; |
| 362 |
NSSCKMDMechanism *mdMechanism; |
| 363 |
pemInternalObject *iKey; |
| 364 |
NSSLOWKEYPrivateKey *lpk; |
| 365 |
NSSItem buffer; |
| 366 |
}; |
| 367 |
|
| 368 |
/* |
| 369 |
* pem_mdCryptoOperationRSAPriv_Create |
| 370 |
*/ |
| 371 |
static NSSCKMDCryptoOperation * |
| 372 |
pem_mdCryptoOperationRSAPriv_Create |
| 373 |
( |
| 374 |
const NSSCKMDCryptoOperation * proto, |
| 375 |
NSSCKMDMechanism * mdMechanism, |
| 376 |
NSSCKMDObject * mdKey, |
| 377 |
CK_RV * pError |
| 378 |
) |
| 379 |
{ |
| 380 |
pemInternalObject *iKey = (pemInternalObject *) mdKey->etc; |
| 381 |
const NSSItem *classItem; |
| 382 |
const NSSItem *keyType; |
| 383 |
pemInternalCryptoOperationRSAPriv *iOperation; |
| 384 |
NSSLOWKEYPrivateKey *lpk = NULL; |
| 385 |
PLArenaPool *arena; |
| 386 |
|
| 387 |
classItem = pem_FetchAttribute(iKey, CKA_CLASS, pError); |
| 388 |
if (*pError != CKR_OK) |
| 389 |
return (NSSCKMDCryptoOperation *) NULL; |
| 390 |
|
| 391 |
keyType = pem_FetchAttribute(iKey, CKA_KEY_TYPE, pError); |
| 392 |
if (*pError != CKR_OK) |
| 393 |
return (NSSCKMDCryptoOperation *) NULL; |
| 394 |
|
| 395 |
/* make sure we have the right objects */ |
| 396 |
if (((const NSSItem *) NULL == classItem) || |
| 397 |
(sizeof(CK_OBJECT_CLASS) != classItem->size) || |
| 398 |
(CKO_PRIVATE_KEY != *(CK_OBJECT_CLASS *) classItem->data) || |
| 399 |
((const NSSItem *) NULL == keyType) || |
| 400 |
(sizeof(CK_KEY_TYPE) != keyType->size) || |
| 401 |
(CKK_RSA != *(CK_KEY_TYPE *) keyType->data)) { |
| 402 |
*pError = CKR_KEY_TYPE_INCONSISTENT; |
| 403 |
return (NSSCKMDCryptoOperation *) NULL; |
| 404 |
} |
| 405 |
|
| 406 |
arena = PORT_NewArena(2048); |
| 407 |
if (!arena) { |
| 408 |
*pError = CKR_HOST_MEMORY; |
| 409 |
return (NSSCKMDCryptoOperation *) NULL; |
| 410 |
} |
| 411 |
|
| 412 |
lpk = pem_getPrivateKey(arena, iKey->u.key.key.privateKey, pError, &iKey->u.key.key.modulus); |
| 413 |
if (lpk == NULL) { |
| 414 |
plog("pem_mdCryptoOperationRSAPriv_Create: pem_getPrivateKey returned NULL, pError 0x%08x\n", *pError); |
| 415 |
PORT_FreeArena(arena, PR_FALSE); |
| 416 |
return (NSSCKMDCryptoOperation *) NULL; |
| 417 |
} |
| 418 |
|
| 419 |
iOperation = NSS_ZNEW(NULL, pemInternalCryptoOperationRSAPriv); |
| 420 |
if ((pemInternalCryptoOperationRSAPriv *) NULL == iOperation) { |
| 421 |
*pError = CKR_HOST_MEMORY; |
| 422 |
return (NSSCKMDCryptoOperation *) NULL; |
| 423 |
} |
| 424 |
iOperation->mdMechanism = mdMechanism; |
| 425 |
iOperation->iKey = iKey; |
| 426 |
iOperation->lpk = lpk; |
| 427 |
|
| 428 |
memcpy(&iOperation->mdOperation, proto, sizeof iOperation->mdOperation); |
| 429 |
iOperation->mdOperation.etc = iOperation; |
| 430 |
|
| 431 |
return &iOperation->mdOperation; |
| 432 |
} |
| 433 |
|
| 434 |
static void |
| 435 |
pem_mdCryptoOperationRSAPriv_Destroy |
| 436 |
( |
| 437 |
NSSCKMDCryptoOperation * mdOperation, |
| 438 |
NSSCKFWCryptoOperation * fwOperation, |
| 439 |
NSSCKMDInstance * mdInstance, |
| 440 |
NSSCKFWInstance * fwInstance |
| 441 |
) |
| 442 |
{ |
| 443 |
pemInternalCryptoOperationRSAPriv *iOperation = |
| 444 |
(pemInternalCryptoOperationRSAPriv *) mdOperation->etc; |
| 445 |
|
| 446 |
NSS_ZFreeIf(iOperation->buffer.data); |
| 447 |
iOperation->buffer.data = NULL; |
| 448 |
|
| 449 |
pem_DestroyPrivateKey(iOperation->lpk); |
| 450 |
iOperation->lpk = NULL; |
| 451 |
NSS_ZFreeIf(iOperation); |
| 452 |
} |
| 453 |
|
| 454 |
static CK_ULONG |
| 455 |
pem_mdCryptoOperationRSA_GetFinalLength |
| 456 |
( |
| 457 |
NSSCKMDCryptoOperation * mdOperation, |
| 458 |
NSSCKFWCryptoOperation * fwOperation, |
| 459 |
NSSCKMDSession * mdSession, |
| 460 |
NSSCKFWSession * fwSession, |
| 461 |
NSSCKMDToken * mdToken, |
| 462 |
NSSCKFWToken * fwToken, |
| 463 |
NSSCKMDInstance * mdInstance, |
| 464 |
NSSCKFWInstance * fwInstance, |
| 465 |
CK_RV * pError |
| 466 |
) |
| 467 |
{ |
| 468 |
pemInternalCryptoOperationRSAPriv *iOperation = |
| 469 |
(pemInternalCryptoOperationRSAPriv *) mdOperation->etc; |
| 470 |
const NSSItem *modulus = |
| 471 |
pem_FetchAttribute(iOperation->iKey, CKA_MODULUS, pError); |
| 472 |
|
| 473 |
if (NULL == modulus) { |
| 474 |
*pError = CKR_FUNCTION_FAILED; |
| 475 |
return 0; |
| 476 |
} |
| 477 |
|
| 478 |
return modulus->size; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
/* |
| 483 |
* pem_mdCryptoOperationRSADecrypt_GetOperationLength |
| 484 |
* we won't know the length until we actually decrypt the |
| 485 |
* input block. Since we go to all the work to decrypt the |
| 486 |
* the block, we'll save if for when the block is asked for |
| 487 |
*/ |
| 488 |
static CK_ULONG |
| 489 |
pem_mdCryptoOperationRSADecrypt_GetOperationLength |
| 490 |
( |
| 491 |
NSSCKMDCryptoOperation * mdOperation, |
| 492 |
NSSCKFWCryptoOperation * fwOperation, |
| 493 |
NSSCKMDSession * mdSession, |
| 494 |
NSSCKFWSession * fwSession, |
| 495 |
NSSCKMDToken * mdToken, |
| 496 |
NSSCKFWToken * fwToken, |
| 497 |
NSSCKMDInstance * mdInstance, |
| 498 |
NSSCKFWInstance * fwInstance, |
| 499 |
const NSSItem * input, |
| 500 |
CK_RV * pError |
| 501 |
) |
| 502 |
{ |
| 503 |
pemInternalCryptoOperationRSAPriv *iOperation = |
| 504 |
(pemInternalCryptoOperationRSAPriv *) mdOperation->etc; |
| 505 |
SECStatus rv; |
| 506 |
|
| 507 |
/* FIXME: Just because Microsoft is broken doesn't mean I have to be. |
| 508 |
* but this is faster to do for now */ |
| 509 |
|
| 510 |
/* Microsoft's Decrypt operation works in place. Since we don't want |
| 511 |
* to trash our input buffer, we make a copy of it */ |
| 512 |
iOperation->buffer.data = NSS_ZAlloc(NULL, input->size); |
| 513 |
if (NULL == iOperation->buffer.data) { |
| 514 |
*pError = CKR_HOST_MEMORY; |
| 515 |
return 0; |
| 516 |
} |
| 517 |
/* copy data and initialize size of the NSSItem */ |
| 518 |
memcpy(iOperation->buffer.data, input->data, input->size); |
| 519 |
iOperation->buffer.size = input->size; |
| 520 |
|
| 521 |
rv = pem_RSA_DecryptBlock(iOperation->lpk, iOperation->buffer.data, |
| 522 |
&iOperation->buffer.size, |
| 523 |
iOperation->buffer.size, input->data, |
| 524 |
input->size); |
| 525 |
|
| 526 |
if (rv != SECSuccess) { |
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
return iOperation->buffer.size; |
| 531 |
} |
| 532 |
|
| 533 |
/* |
| 534 |
* pem_mdCryptoOperationRSADecrypt_UpdateFinal |
| 535 |
* |
| 536 |
* NOTE: pem_mdCryptoOperationRSADecrypt_GetOperationLength is presumed to |
| 537 |
* have been called previously. |
| 538 |
*/ |
| 539 |
static CK_RV |
| 540 |
pem_mdCryptoOperationRSADecrypt_UpdateFinal |
| 541 |
( |
| 542 |
NSSCKMDCryptoOperation * mdOperation, |
| 543 |
NSSCKFWCryptoOperation * fwOperation, |
| 544 |
NSSCKMDSession * mdSession, |
| 545 |
NSSCKFWSession * fwSession, |
| 546 |
NSSCKMDToken * mdToken, |
| 547 |
NSSCKFWToken * fwToken, |
| 548 |
NSSCKMDInstance * mdInstance, |
| 549 |
NSSCKFWInstance * fwInstance, |
| 550 |
const NSSItem * input, |
| 551 |
NSSItem * output |
| 552 |
) |
| 553 |
{ |
| 554 |
pemInternalCryptoOperationRSAPriv *iOperation = |
| 555 |
(pemInternalCryptoOperationRSAPriv *) mdOperation->etc; |
| 556 |
NSSItem *buffer = &iOperation->buffer; |
| 557 |
|
| 558 |
if (NULL == buffer->data) { |
| 559 |
return CKR_GENERAL_ERROR; |
| 560 |
} |
| 561 |
memcpy(output->data, buffer->data, buffer->size); |
| 562 |
output->size = buffer->size; |
| 563 |
return CKR_OK; |
| 564 |
} |
| 565 |
|
| 566 |
/* |
| 567 |
* pem_mdCryptoOperationRSASign_UpdateFinal |
| 568 |
* |
| 569 |
*/ |
| 570 |
static CK_RV |
| 571 |
pem_mdCryptoOperationRSASign_UpdateFinal |
| 572 |
( |
| 573 |
NSSCKMDCryptoOperation * mdOperation, |
| 574 |
NSSCKFWCryptoOperation * fwOperation, |
| 575 |
NSSCKMDSession * mdSession, |
| 576 |
NSSCKFWSession * fwSession, |
| 577 |
NSSCKMDToken * mdToken, |
| 578 |
NSSCKFWToken * fwToken, |
| 579 |
NSSCKMDInstance * mdInstance, |
| 580 |
NSSCKFWInstance * fwInstance, |
| 581 |
const NSSItem * input, |
| 582 |
NSSItem * output |
| 583 |
) |
| 584 |
{ |
| 585 |
pemInternalCryptoOperationRSAPriv *iOperation = |
| 586 |
(pemInternalCryptoOperationRSAPriv *) mdOperation->etc; |
| 587 |
CK_RV error = CKR_OK; |
| 588 |
SECStatus rv = SECSuccess; |
| 589 |
|
| 590 |
rv = pem_RSA_Sign(iOperation->lpk, output->data, &output->size, |
| 591 |
output->size, input->data, input->size); |
| 592 |
|
| 593 |
if (rv != SECSuccess) { |
| 594 |
error = CKR_GENERAL_ERROR; |
| 595 |
} |
| 596 |
|
| 597 |
return error; |
| 598 |
} |
| 599 |
|
| 600 |
NSS_IMPLEMENT_DATA const NSSCKMDCryptoOperation |
| 601 |
pem_mdCryptoOperationRSADecrypt_proto = { |
| 602 |
NULL, /* etc */ |
| 603 |
pem_mdCryptoOperationRSAPriv_Destroy, |
| 604 |
NULL, /* GetFinalLengh - not needed for one shot Decrypt/Encrypt */ |
| 605 |
pem_mdCryptoOperationRSADecrypt_GetOperationLength, |
| 606 |
NULL, /* Final - not needed for one shot operation */ |
| 607 |
NULL, /* Update - not needed for one shot operation */ |
| 608 |
NULL, /* DigestUpdate - not needed for one shot operation */ |
| 609 |
pem_mdCryptoOperationRSADecrypt_UpdateFinal, |
| 610 |
NULL, /* UpdateCombo - not needed for one shot operation */ |
| 611 |
NULL, /* DigestKey - not needed for one shot operation */ |
| 612 |
(void *) NULL /* null terminator */ |
| 613 |
}; |
| 614 |
|
| 615 |
NSS_IMPLEMENT_DATA const NSSCKMDCryptoOperation |
| 616 |
pem_mdCryptoOperationRSASign_proto = { |
| 617 |
NULL, /* etc */ |
| 618 |
pem_mdCryptoOperationRSAPriv_Destroy, |
| 619 |
pem_mdCryptoOperationRSA_GetFinalLength, |
| 620 |
NULL, /* GetOperationLengh - not needed for one shot Sign/Verify */ |
| 621 |
NULL, /* Final - not needed for one shot operation */ |
| 622 |
NULL, /* Update - not needed for one shot operation */ |
| 623 |
NULL, /* DigestUpdate - not needed for one shot operation */ |
| 624 |
pem_mdCryptoOperationRSASign_UpdateFinal, |
| 625 |
NULL, /* UpdateCombo - not needed for one shot operation */ |
| 626 |
NULL, /* DigestKey - not needed for one shot operation */ |
| 627 |
(void *) NULL /* null terminator */ |
| 628 |
}; |
| 629 |
|
| 630 |
/********** NSSCKMDMechansim functions ***********************/ |
| 631 |
/* |
| 632 |
* pem_mdMechanismRSA_Destroy |
| 633 |
*/ |
| 634 |
static void |
| 635 |
pem_mdMechanismRSA_Destroy |
| 636 |
( |
| 637 |
NSSCKMDMechanism * mdMechanism, |
| 638 |
NSSCKFWMechanism * fwMechanism, |
| 639 |
NSSCKMDInstance * mdInstance, |
| 640 |
NSSCKFWInstance * fwInstance |
| 641 |
) |
| 642 |
{ |
| 643 |
NSS_ZFreeIf(fwMechanism); |
| 644 |
} |
| 645 |
|
| 646 |
/* |
| 647 |
* pem_mdMechanismRSA_GetMinKeySize |
| 648 |
*/ |
| 649 |
static CK_ULONG |
| 650 |
pem_mdMechanismRSA_GetMinKeySize |
| 651 |
( |
| 652 |
NSSCKMDMechanism * mdMechanism, |
| 653 |
NSSCKFWMechanism * fwMechanism, |
| 654 |
NSSCKMDToken * mdToken, |
| 655 |
NSSCKFWToken * fwToken, |
| 656 |
NSSCKMDInstance * mdInstance, |
| 657 |
NSSCKFWInstance * fwInstance, |
| 658 |
CK_RV * pError |
| 659 |
) |
| 660 |
{ |
| 661 |
return 384; |
| 662 |
} |
| 663 |
|
| 664 |
/* |
| 665 |
* pem_mdMechanismRSA_GetMaxKeySize |
| 666 |
*/ |
| 667 |
static CK_ULONG |
| 668 |
pem_mdMechanismRSA_GetMaxKeySize |
| 669 |
( |
| 670 |
NSSCKMDMechanism * mdMechanism, |
| 671 |
NSSCKFWMechanism * fwMechanism, |
| 672 |
NSSCKMDToken * mdToken, |
| 673 |
NSSCKFWToken * fwToken, |
| 674 |
NSSCKMDInstance * mdInstance, |
| 675 |
NSSCKFWInstance * fwInstance, |
| 676 |
CK_RV * pError |
| 677 |
) |
| 678 |
{ |
| 679 |
return 16384; |
| 680 |
} |
| 681 |
|
| 682 |
/* |
| 683 |
* pem_mdMechanismRSA_DecryptInit |
| 684 |
*/ |
| 685 |
static NSSCKMDCryptoOperation * |
| 686 |
pem_mdMechanismRSA_DecryptInit |
| 687 |
( |
| 688 |
NSSCKMDMechanism * mdMechanism, |
| 689 |
NSSCKFWMechanism * fwMechanism, |
| 690 |
CK_MECHANISM * pMechanism, |
| 691 |
NSSCKMDSession * mdSession, |
| 692 |
NSSCKFWSession * fwSession, |
| 693 |
NSSCKMDToken * mdToken, |
| 694 |
NSSCKFWToken * fwToken, |
| 695 |
NSSCKMDInstance * mdInstance, |
| 696 |
NSSCKFWInstance * fwInstance, |
| 697 |
NSSCKMDObject * mdKey, |
| 698 |
NSSCKFWObject * fwKey, |
| 699 |
CK_RV * pError |
| 700 |
) |
| 701 |
{ |
| 702 |
return pem_mdCryptoOperationRSAPriv_Create |
| 703 |
(&pem_mdCryptoOperationRSADecrypt_proto, mdMechanism, mdKey, |
| 704 |
pError); |
| 705 |
} |
| 706 |
|
| 707 |
/* |
| 708 |
* pem_mdMechanismRSA_SignInit |
| 709 |
*/ |
| 710 |
static NSSCKMDCryptoOperation * |
| 711 |
pem_mdMechanismRSA_SignInit |
| 712 |
( |
| 713 |
NSSCKMDMechanism * mdMechanism, |
| 714 |
NSSCKFWMechanism * fwMechanism, |
| 715 |
CK_MECHANISM * pMechanism, |
| 716 |
NSSCKMDSession * mdSession, |
| 717 |
NSSCKFWSession * fwSession, |
| 718 |
NSSCKMDToken * mdToken, |
| 719 |
NSSCKFWToken * fwToken, |
| 720 |
NSSCKMDInstance * mdInstance, |
| 721 |
NSSCKFWInstance * fwInstance, |
| 722 |
NSSCKMDObject * mdKey, |
| 723 |
NSSCKFWObject * fwKey, |
| 724 |
CK_RV * pError |
| 725 |
) |
| 726 |
{ |
| 727 |
return pem_mdCryptoOperationRSAPriv_Create |
| 728 |
(&pem_mdCryptoOperationRSASign_proto, mdMechanism, mdKey, pError); |
| 729 |
} |
| 730 |
|
| 731 |
NSS_IMPLEMENT_DATA const NSSCKMDMechanism |
| 732 |
pem_mdMechanismRSA = { |
| 733 |
(void *) NULL, /* etc */ |
| 734 |
pem_mdMechanismRSA_Destroy, |
| 735 |
pem_mdMechanismRSA_GetMinKeySize, |
| 736 |
pem_mdMechanismRSA_GetMaxKeySize, |
| 737 |
NULL, /* GetInHardware - default false */ |
| 738 |
NULL, /* EncryptInit - default errs */ |
| 739 |
pem_mdMechanismRSA_DecryptInit, |
| 740 |
NULL, /* DigestInit - default errs */ |
| 741 |
pem_mdMechanismRSA_SignInit, |
| 742 |
NULL, /* VerifyInit - default errs */ |
| 743 |
pem_mdMechanismRSA_SignInit, /* SignRecoverInit */ |
| 744 |
NULL, /* VerifyRecoverInit - default errs */ |
| 745 |
NULL, /* GenerateKey - default errs */ |
| 746 |
NULL, /* GenerateKeyPair - default errs */ |
| 747 |
NULL, /* GetWrapKeyLength - default errs */ |
| 748 |
NULL, /* WrapKey - default errs */ |
| 749 |
NULL, /* UnwrapKey - default errs */ |
| 750 |
NULL, /* DeriveKey - default errs */ |
| 751 |
(void *) NULL /* null terminator */ |
| 752 |
}; |