Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Signature stuff. | |
| 3 * | |
| 4 * ***** BEGIN LICENSE BLOCK ***** | |
| 5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
| 6 * | |
| 7 * The contents of this file are subject to the Mozilla Public License Version | |
| 8 * 1.1 (the "License"); you may not use this file except in compliance with | |
| 9 * the License. You may obtain a copy of the License at | |
| 10 * http://www.mozilla.org/MPL/ | |
| 11 * | |
| 12 * Software distributed under the License is distributed on an "AS IS" basis, | |
| 13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
| 14 * for the specific language governing rights and limitations under the | |
| 15 * License. | |
| 16 * | |
| 17 * The Original Code is the Netscape security libraries. | |
| 18 * | |
| 19 * The Initial Developer of the Original Code is | |
| 20 * Netscape Communications Corporation. | |
| 21 * Portions created by the Initial Developer are Copyright (C) 1994-2000 | |
| 22 * the Initial Developer. All Rights Reserved. | |
| 23 * | |
| 24 * Contributor(s): | |
| 25 * Dr Vipul Gupta <vipul.gupta@sun.com>, Sun Microsystems Laboratories | |
| 26 * | |
| 27 * Alternatively, the contents of this file may be used under the terms of | |
| 28 * either the GNU General Public License Version 2 or later (the "GPL"), or | |
| 29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
| 30 * in which case the provisions of the GPL or the LGPL are applicable instead | |
| 31 * of those above. If you wish to allow use of your version of this file only | |
| 32 * under the terms of either the GPL or the LGPL, and not to allow others to | |
| 33 * use your version of this file under the terms of the MPL, indicate your | |
| 34 * decision by deleting the provisions above and replace them with the notice | |
| 35 * and other provisions required by the GPL or the LGPL. If you do not delete | |
| 36 * the provisions above, a recipient may use your version of this file under | |
| 37 * the terms of any one of the MPL, the GPL or the LGPL. | |
| 38 * | |
| 39 * ***** END LICENSE BLOCK ***** */ | |
| 40 | |
| 41 #include "crypto/third_party/nss/chromium-nss.h" | |
| 42 | |
| 43 #include <vector> | |
| 44 | |
| 45 #include <cryptohi.h> | |
| 46 #include <pk11pub.h> | |
| 47 #include <sechash.h> | |
| 48 | |
| 49 #include "base/basictypes.h" | |
| 50 #include "base/logging.h" | |
| 51 #include "build/build_config.h" | |
| 52 | |
| 53 SECStatus DerSignData(PLArenaPool *arena, | |
| 54 SECItem *result, | |
| 55 SECItem *input, | |
| 56 SECKEYPrivateKey *key, | |
| 57 SECOidTag algo_id) { | |
| 58 #if defined(OS_WIN) || defined(OS_MACOSX) | |
| 59 return SEC_DerSignData(arena, result, input->data, input->len, key, algo_id); | |
| 60 #else | |
| 61 if (key->keyType != ecKey) | |
|
wtc
2011/11/15 03:10:57
Add curly braces around the return statement becau
mattm
2011/11/15 05:42:31
Done.
| |
| 62 return SEC_DerSignData(arena, result, input->data, input->len, key, | |
| 63 algo_id); | |
| 64 | |
| 65 // NSS has a private function sec_DecodeSigAlg it uses to figure out the | |
| 66 // correct hash from the algorithm id. | |
| 67 HASH_HashType hash_type; | |
| 68 switch (algo_id) { | |
| 69 case SEC_OID_ANSIX962_ECDSA_SHA1_SIGNATURE: | |
| 70 hash_type = HASH_AlgSHA1; | |
| 71 break; | |
| 72 #ifdef SHA224_LENGTH | |
| 73 case SEC_OID_ANSIX962_ECDSA_SHA224_SIGNATURE: | |
| 74 hash_type = HASH_AlgSHA224; | |
| 75 break; | |
| 76 #endif | |
| 77 case SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE: | |
| 78 hash_type = HASH_AlgSHA256; | |
| 79 break; | |
| 80 case SEC_OID_ANSIX962_ECDSA_SHA384_SIGNATURE: | |
| 81 hash_type = HASH_AlgSHA384; | |
| 82 break; | |
| 83 case SEC_OID_ANSIX962_ECDSA_SHA512_SIGNATURE: | |
| 84 hash_type = HASH_AlgSHA512; | |
| 85 break; | |
| 86 default: | |
| 87 return SECFailure; | |
|
wtc
2011/11/15 03:10:57
Please call PORT_SetError() before returning SECFa
mattm
2011/11/15 05:42:31
Done.
| |
| 88 } | |
| 89 | |
| 90 // Hash the input. | |
| 91 std::vector<uint8> hash_data(HASH_ResultLen(hash_type)); | |
| 92 SECStatus rv = HASH_HashBuf( | |
| 93 hash_type, hash_data.data(), input->data, input->len); | |
| 94 if (rv != SECSuccess) | |
| 95 return rv; | |
| 96 SECItem hash = {siBuffer, hash_data.data(), hash_data.size()}; | |
| 97 | |
| 98 // Compute signature of hash. | |
| 99 int signature_len = PK11_SignatureLen(key); | |
| 100 std::vector<uint8> signature_data(signature_len); | |
| 101 SECItem sig = {siBuffer, signature_data.data(), signature_len}; | |
| 102 rv = PK11_Sign(key, &sig, &hash); | |
|
wtc
2011/11/15 03:10:57
I suggest we use this code for all signature algor
mattm
2011/11/15 05:42:31
Hm, I could, but I think handling them all would m
wtc
2011/11/15 06:15:01
Not doing the other part (use PK11_Sign for all al
mattm
2011/11/16 01:41:13
right. Should I revert that?
| |
| 103 if (rv != SECSuccess) | |
| 104 return rv; | |
| 105 | |
| 106 CERTSignedData sd; | |
| 107 PORT_Memset(&sd, 0, sizeof(sd)); | |
| 108 // Fill in tbsCertificate. | |
| 109 sd.data.data = (unsigned char*) input->data; | |
| 110 sd.data.len = input->len; | |
| 111 | |
| 112 // Fill in signatureAlgorithm. | |
| 113 rv = SECOID_SetAlgorithmID(arena, &sd.signatureAlgorithm, algo_id, 0); | |
| 114 if (rv != SECSuccess) | |
| 115 return rv; | |
| 116 | |
| 117 // Fill in signatureValue. | |
| 118 rv = DSAU_EncodeDerSigWithLen(&sd.signature, &sig, sig.len); | |
| 119 if (rv != SECSuccess) | |
| 120 return rv; | |
| 121 sd.signature.len <<= 3; // Convert to bit string. | |
| 122 | |
| 123 // DER encode the signed data object. | |
| 124 void* encode_result = SEC_ASN1EncodeItem( | |
| 125 arena, result, &sd, SEC_ASN1_GET(CERT_SignedDataTemplate)); | |
| 126 | |
| 127 PORT_Free(sd.signature.data); | |
| 128 | |
| 129 return encode_result ? SECSuccess : SECFailure; | |
| 130 #endif | |
| 131 } | |
| OLD | NEW |