1 // Copyright (c) 2009 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 "base/crypto/signature_verifier.h"
11 #include "base/logging.h"
12 #include "base/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
, SECOID_AlgorithmIDTemplate
,
59 if (rv
!= SECSuccess
) {
60 SECKEY_DestroyPublicKey(public_key
);
61 PORT_FreeArena(arena
, PR_TRUE
);
67 sig
.data
= const_cast<uint8
*>(signature
);
68 sig
.len
= signature_len
;
69 SECOidTag hash_alg_tag
;
70 vfy_context_
= VFY_CreateContextWithAlgorithmID(public_key
, &sig
,
71 &sig_alg_id
, &hash_alg_tag
,
73 SECKEY_DestroyPublicKey(public_key
); // Done with public_key.
74 PORT_FreeArena(arena
, PR_TRUE
); // Done with sig_alg_id.
76 // A corrupted RSA signature could be detected without the data, so
77 // VFY_CreateContextWithAlgorithmID may fail with SEC_ERROR_BAD_SIGNATURE
82 rv
= VFY_Begin(vfy_context_
);
83 if (rv
!= SECSuccess
) {
90 void SignatureVerifier::VerifyUpdate(const uint8
* data_part
,
92 SECStatus rv
= VFY_Update(vfy_context_
, data_part
, data_part_len
);
93 DCHECK(rv
== SECSuccess
);
96 bool SignatureVerifier::VerifyFinal() {
97 SECStatus rv
= VFY_End(vfy_context_
);
100 // If signature verification fails, the error code is
101 // SEC_ERROR_BAD_SIGNATURE (-8182).
102 return (rv
== SECSuccess
);
105 void SignatureVerifier::Reset() {
107 VFY_DestroyContext(vfy_context_
, PR_TRUE
);