This source file includes following definitions.
- DerSignData
#include "crypto/third_party/nss/chromium-nss.h"
#include <vector>
#include <cryptohi.h>
#include <pk11pub.h>
#include <secerr.h>
#include <sechash.h>
#include "base/basictypes.h"
#include "base/logging.h"
#include "build/build_config.h"
SECStatus DerSignData(PLArenaPool *arena,
SECItem *result,
SECItem *input,
SECKEYPrivateKey *key,
SECOidTag algo_id) {
if (key->keyType != ecKey) {
return SEC_DerSignData(arena, result, input->data, input->len, key,
algo_id);
}
HASH_HashType hash_type;
switch (algo_id) {
case SEC_OID_ANSIX962_ECDSA_SHA1_SIGNATURE:
hash_type = HASH_AlgSHA1;
break;
#ifdef SHA224_LENGTH
case SEC_OID_ANSIX962_ECDSA_SHA224_SIGNATURE:
hash_type = HASH_AlgSHA224;
break;
#endif
case SEC_OID_ANSIX962_ECDSA_SHA256_SIGNATURE:
hash_type = HASH_AlgSHA256;
break;
case SEC_OID_ANSIX962_ECDSA_SHA384_SIGNATURE:
hash_type = HASH_AlgSHA384;
break;
case SEC_OID_ANSIX962_ECDSA_SHA512_SIGNATURE:
hash_type = HASH_AlgSHA512;
break;
default:
PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
return SECFailure;
}
std::vector<uint8> hash_data(HASH_ResultLen(hash_type));
SECStatus rv = HASH_HashBuf(
hash_type, &hash_data[0], input->data, input->len);
if (rv != SECSuccess)
return rv;
SECItem hash = {siBuffer, &hash_data[0],
static_cast<unsigned int>(hash_data.size())};
int signature_len = PK11_SignatureLen(key);
std::vector<uint8> signature_data(signature_len);
SECItem sig = {siBuffer, &signature_data[0],
static_cast<unsigned int>(signature_len)};
rv = PK11_Sign(key, &sig, &hash);
if (rv != SECSuccess)
return rv;
CERTSignedData sd;
PORT_Memset(&sd, 0, sizeof(sd));
sd.data.data = (unsigned char*) input->data;
sd.data.len = input->len;
rv = SECOID_SetAlgorithmID(arena, &sd.signatureAlgorithm, algo_id, 0);
if (rv != SECSuccess)
return rv;
rv = DSAU_EncodeDerSigWithLen(&sd.signature, &sig, sig.len);
if (rv != SECSuccess)
return rv;
sd.signature.len <<= 3;
void* encode_result = SEC_ASN1EncodeItem(
arena, result, &sd, SEC_ASN1_GET(CERT_SignedDataTemplate));
PORT_Free(sd.signature.data);
return encode_result ? SECSuccess : SECFailure;
}