1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crypto/signature_verifier.h"
11 #include "base/logging.h"
12 #include "crypto/nss_util.h"
16 SignatureVerifier::SignatureVerifier() : vfy_context_(NULL
) {
20 SignatureVerifier::~SignatureVerifier() {
24 bool SignatureVerifier::VerifyInit(const uint8
* signature_algorithm
,
25 int signature_algorithm_len
,
26 const uint8
* signature
,
28 const uint8
* public_key_info
,
29 int public_key_info_len
) {
30 signature_
.assign(signature
, signature
+ signature_len
);
32 CERTSubjectPublicKeyInfo
* spki
= NULL
;
34 spki_der
.type
= siBuffer
;
35 spki_der
.data
= const_cast<uint8
*>(public_key_info
);
36 spki_der
.len
= public_key_info_len
;
37 spki
= SECKEY_DecodeDERSubjectPublicKeyInfo(&spki_der
);
40 SECKEYPublicKey
* public_key
= SECKEY_ExtractPublicKey(spki
);
41 SECKEY_DestroySubjectPublicKeyInfo(spki
); // Done with spki.
45 PLArenaPool
* arena
= PORT_NewArena(DER_DEFAULT_CHUNKSIZE
);
47 SECKEY_DestroyPublicKey(public_key
);
52 sig_alg_der
.type
= siBuffer
;
53 sig_alg_der
.data
= const_cast<uint8
*>(signature_algorithm
);
54 sig_alg_der
.len
= signature_algorithm_len
;
55 SECAlgorithmID sig_alg_id
;
57 rv
= SEC_QuickDERDecodeItem(arena
, &sig_alg_id
,
58 SEC_ASN1_GET(SECOID_AlgorithmIDTemplate
),
60 if (rv
!= SECSuccess
) {
61 SECKEY_DestroyPublicKey(public_key
);
62 PORT_FreeArena(arena
, PR_TRUE
);
68 sig
.data
= const_cast<uint8
*>(signature
);
69 sig
.len
= signature_len
;
70 SECOidTag hash_alg_tag
;
71 vfy_context_
= VFY_CreateContextWithAlgorithmID(public_key
, &sig
,
72 &sig_alg_id
, &hash_alg_tag
,
74 SECKEY_DestroyPublicKey(public_key
); // Done with public_key.
75 PORT_FreeArena(arena
, PR_TRUE
); // Done with sig_alg_id.
77 // A corrupted RSA signature could be detected without the data, so
78 // VFY_CreateContextWithAlgorithmID may fail with SEC_ERROR_BAD_SIGNATURE
83 rv
= VFY_Begin(vfy_context_
);
84 if (rv
!= SECSuccess
) {
91 void SignatureVerifier::VerifyUpdate(const uint8
* data_part
,
93 SECStatus rv
= VFY_Update(vfy_context_
, data_part
, data_part_len
);
94 DCHECK_EQ(SECSuccess
, rv
);
97 bool SignatureVerifier::VerifyFinal() {
98 SECStatus rv
= VFY_End(vfy_context_
);
101 // If signature verification fails, the error code is
102 // SEC_ERROR_BAD_SIGNATURE (-8182).
103 return (rv
== SECSuccess
);
106 void SignatureVerifier::Reset() {
108 VFY_DestroyContext(vfy_context_
, PR_TRUE
);
114 } // namespace crypto