| OLD | NEW |
| 1 | 1 |
| 2 /* This Source Code Form is subject to the terms of the Mozilla Public | 2 /* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 * License, v. 2.0. If a copy of the MPL was not distributed with this | 3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 /* | 5 /* |
| 6 * This file PKCS #12 fuctions that should really be moved to the | 6 * This file PKCS #12 fuctions that should really be moved to the |
| 7 * PKCS #12 directory, however we can't do that in a point release | 7 * PKCS #12 directory, however we can't do that in a point release |
| 8 * because that will break binary compatibility, so we keep them here for now. | 8 * because that will break binary compatibility, so we keep them here for now. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "seccomon.h" | 11 #include "seccomon.h" |
| 12 #include "secmod.h" | 12 #include "secmod.h" |
| 13 #include "secmodi.h" | 13 #include "secmodi.h" |
| 14 #include "pkcs11.h" | 14 #include "pkcs11.h" |
| 15 #include "pk11func.h" | 15 #include "pk11func.h" |
| 16 #include "secitem.h" | 16 #include "secitem.h" |
| 17 #include "key.h" | 17 #include "key.h" |
| 18 #include "secoid.h" | 18 #include "secoid.h" |
| 19 #include "secasn1.h" | 19 #include "secasn1.h" |
| 20 #include "secerr.h" | 20 #include "secerr.h" |
| 21 #include "prerror.h" |
| 21 | 22 |
| 22 | 23 |
| 23 | 24 |
| 24 /* These data structures should move to a common .h file shared between the | 25 /* These data structures should move to a common .h file shared between the |
| 25 * wrappers and the pkcs 12 code. */ | 26 * wrappers and the pkcs 12 code. */ |
| 26 | 27 |
| 27 /* | 28 /* |
| 28 ** RSA Raw Private Key structures | 29 ** RSA Raw Private Key structures |
| 29 */ | 30 */ |
| 30 | 31 |
| (...skipping 478 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 SECStatus | 510 SECStatus |
| 510 PK11_ImportPrivateKeyInfo(PK11SlotInfo *slot, SECKEYPrivateKeyInfo *pki, | 511 PK11_ImportPrivateKeyInfo(PK11SlotInfo *slot, SECKEYPrivateKeyInfo *pki, |
| 511 SECItem *nickname, SECItem *publicValue, PRBool isPerm, | 512 SECItem *nickname, SECItem *publicValue, PRBool isPerm, |
| 512 PRBool isPrivate, unsigned int keyUsage, void *wincx) | 513 PRBool isPrivate, unsigned int keyUsage, void *wincx) |
| 513 { | 514 { |
| 514 return PK11_ImportPrivateKeyInfoAndReturnKey(slot, pki, nickname, | 515 return PK11_ImportPrivateKeyInfoAndReturnKey(slot, pki, nickname, |
| 515 publicValue, isPerm, isPrivate, keyUsage, NULL, wincx); | 516 publicValue, isPerm, isPrivate, keyUsage, NULL, wincx); |
| 516 | 517 |
| 517 } | 518 } |
| 518 | 519 |
| 520 SECItem * |
| 521 PK11_ExportDERPrivateKeyInfo(SECKEYPrivateKey *pk, void *wincx) |
| 522 { |
| 523 SECKEYPrivateKeyInfo *pki = PK11_ExportPrivKeyInfo(pk, wincx); |
| 524 SECItem *derPKI; |
| 525 |
| 526 if (!pki) { |
| 527 return NULL; |
| 528 } |
| 529 derPKI = SEC_ASN1EncodeItem(NULL, NULL, pki, |
| 530 SECKEY_PrivateKeyInfoTemplate); |
| 531 SECKEY_DestroyPrivateKeyInfo(pki, PR_TRUE); |
| 532 return derPKI; |
| 533 } |
| 534 |
| 535 static PRBool |
| 536 ReadAttribute(SECKEYPrivateKey *key, CK_ATTRIBUTE_TYPE type, |
| 537 PLArenaPool *arena, SECItem *output) |
| 538 { |
| 539 SECStatus rv = PK11_ReadAttribute(key->pkcs11Slot, key->pkcs11ID, type, |
| 540 arena, output); |
| 541 return rv == SECSuccess; |
| 542 } |
| 543 |
| 544 /* |
| 545 * The caller is responsible for freeing the return value by passing it to |
| 546 * SECKEY_DestroyPrivateKeyInfo(..., PR_TRUE). |
| 547 */ |
| 548 SECKEYPrivateKeyInfo * |
| 549 PK11_ExportPrivKeyInfo(SECKEYPrivateKey *pk, void *wincx) |
| 550 { |
| 551 /* PrivateKeyInfo version (always zero) */ |
| 552 const unsigned char pkiVersion = 0; |
| 553 /* RSAPrivateKey version (always zero) */ |
| 554 const unsigned char rsaVersion = 0; |
| 555 PLArenaPool *arena = NULL; |
| 556 SECKEYRawPrivateKey rawKey; |
| 557 SECKEYPrivateKeyInfo *pki; |
| 558 SECItem *encoded; |
| 559 SECStatus rv; |
| 560 |
| 561 if (pk->keyType != rsaKey) { |
| 562 PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); |
| 563 goto loser; |
| 564 } |
| 565 |
| 566 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
| 567 if (!arena) { |
| 568 goto loser; |
| 569 } |
| 570 memset(&rawKey, 0, sizeof(rawKey)); |
| 571 rawKey.keyType = pk->keyType; |
| 572 rawKey.u.rsa.version.type = siUnsignedInteger; |
| 573 rawKey.u.rsa.version.data = (unsigned char *)PORT_ArenaAlloc(arena, 1); |
| 574 if (!rawKey.u.rsa.version.data) { |
| 575 goto loser; |
| 576 } |
| 577 rawKey.u.rsa.version.data[0] = rsaVersion; |
| 578 rawKey.u.rsa.version.len = 1; |
| 579 |
| 580 /* Read the component attributes of the private key */ |
| 581 prepare_rsa_priv_key_export_for_asn1(&rawKey); |
| 582 if (!ReadAttribute(pk, CKA_MODULUS, arena, &rawKey.u.rsa.modulus) || |
| 583 !ReadAttribute(pk, CKA_PUBLIC_EXPONENT, arena, |
| 584 &rawKey.u.rsa.publicExponent) || |
| 585 !ReadAttribute(pk, CKA_PRIVATE_EXPONENT, arena, |
| 586 &rawKey.u.rsa.privateExponent) || |
| 587 !ReadAttribute(pk, CKA_PRIME_1, arena, &rawKey.u.rsa.prime1) || |
| 588 !ReadAttribute(pk, CKA_PRIME_2, arena, &rawKey.u.rsa.prime2) || |
| 589 !ReadAttribute(pk, CKA_EXPONENT_1, arena, |
| 590 &rawKey.u.rsa.exponent1) || |
| 591 !ReadAttribute(pk, CKA_EXPONENT_2, arena, |
| 592 &rawKey.u.rsa.exponent2) || |
| 593 !ReadAttribute(pk, CKA_COEFFICIENT, arena, |
| 594 &rawKey.u.rsa.coefficient)) { |
| 595 goto loser; |
| 596 } |
| 597 |
| 598 pki = PORT_ArenaZNew(arena, SECKEYPrivateKeyInfo); |
| 599 if (!pki) { |
| 600 goto loser; |
| 601 } |
| 602 encoded = SEC_ASN1EncodeItem(arena, &pki->privateKey, &rawKey, |
| 603 SECKEY_RSAPrivateKeyExportTemplate); |
| 604 if (!encoded) { |
| 605 goto loser; |
| 606 } |
| 607 rv = SECOID_SetAlgorithmID(arena, &pki->algorithm, |
| 608 SEC_OID_PKCS1_RSA_ENCRYPTION, NULL); |
| 609 if (rv != SECSuccess) { |
| 610 goto loser; |
| 611 } |
| 612 pki->version.type = siUnsignedInteger; |
| 613 pki->version.data = (unsigned char *)PORT_ArenaAlloc(arena, 1); |
| 614 if (!pki->version.data) { |
| 615 goto loser; |
| 616 } |
| 617 pki->version.data[0] = pkiVersion; |
| 618 pki->version.len = 1; |
| 619 pki->arena = arena; |
| 620 |
| 621 return pki; |
| 622 |
| 623 loser: |
| 624 if (arena) { |
| 625 PORT_FreeArena(arena, PR_TRUE); |
| 626 } |
| 627 return NULL; |
| 628 } |
| OLD | NEW |