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"
9 #include "base/logging.h"
10 #include "crypto/cssm_init.h"
14 SignatureVerifier::SignatureVerifier() : sig_handle_(0) {
15 memset(&public_key_
, 0, sizeof(public_key_
));
19 SignatureVerifier::~SignatureVerifier() {
23 bool SignatureVerifier::VerifyInit(const uint8
* signature_algorithm
,
24 int signature_algorithm_len
,
25 const uint8
* signature
,
27 const uint8
* public_key_info
,
28 int public_key_info_len
) {
29 signature_
.assign(signature
, signature
+ signature_len
);
30 public_key_info_
.assign(public_key_info
,
31 public_key_info
+ public_key_info_len
);
33 CSSM_ALGORITHMS key_alg
= CSSM_ALGID_RSA
; // TODO(wtc): hardcoded.
35 memset(&public_key_
, 0, sizeof(public_key_
));
36 public_key_
.KeyData
.Data
= const_cast<uint8
*>(&public_key_info_
[0]);
37 public_key_
.KeyData
.Length
= public_key_info_
.size();
38 public_key_
.KeyHeader
.HeaderVersion
= CSSM_KEYHEADER_VERSION
;
39 public_key_
.KeyHeader
.BlobType
= CSSM_KEYBLOB_RAW
;
40 public_key_
.KeyHeader
.Format
= CSSM_KEYBLOB_RAW_FORMAT_X509
;
41 public_key_
.KeyHeader
.AlgorithmId
= key_alg
;
42 public_key_
.KeyHeader
.KeyClass
= CSSM_KEYCLASS_PUBLIC_KEY
;
43 public_key_
.KeyHeader
.KeyAttr
= CSSM_KEYATTR_EXTRACTABLE
;
44 public_key_
.KeyHeader
.KeyUsage
= CSSM_KEYUSE_VERIFY
;
45 CSSM_KEY_SIZE key_size
;
47 crtn
= CSSM_QueryKeySizeInBits(GetSharedCSPHandle(), NULL
,
48 &public_key_
, &key_size
);
50 NOTREACHED() << "CSSM_QueryKeySizeInBits failed: " << crtn
;
53 public_key_
.KeyHeader
.LogicalKeySizeInBits
= key_size
.LogicalKeySizeInBits
;
55 // TODO(wtc): decode signature_algorithm...
56 CSSM_ALGORITHMS sig_alg
= CSSM_ALGID_SHA1WithRSA
;
58 crtn
= CSSM_CSP_CreateSignatureContext(GetSharedCSPHandle(), sig_alg
, NULL
,
59 &public_key_
, &sig_handle_
);
64 crtn
= CSSM_VerifyDataInit(sig_handle_
);
72 void SignatureVerifier::VerifyUpdate(const uint8
* data_part
,
75 data
.Data
= const_cast<uint8
*>(data_part
);
76 data
.Length
= data_part_len
;
77 CSSM_RETURN crtn
= CSSM_VerifyDataUpdate(sig_handle_
, &data
, 1);
78 DCHECK_EQ(CSSM_OK
, crtn
);
81 bool SignatureVerifier::VerifyFinal() {
83 sig
.Data
= const_cast<uint8
*>(&signature_
[0]);
84 sig
.Length
= signature_
.size();
85 CSSM_RETURN crtn
= CSSM_VerifyDataFinal(sig_handle_
, &sig
);
88 // crtn is CSSMERR_CSP_VERIFY_FAILED if signature verification fails.
89 return (crtn
== CSSM_OK
);
92 void SignatureVerifier::Reset() {
95 crtn
= CSSM_DeleteContext(sig_handle_
);
96 DCHECK_EQ(CSSM_OK
, crtn
);
101 // Can't call CSSM_FreeKey on public_key_ because we constructed
102 // public_key_ manually.
105 } // namespace crypto