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"
9 #include "base/crypto/cssm_init.h"
10 #include "base/logging.h"
14 SignatureVerifier::SignatureVerifier() : sig_handle_(0) {
18 SignatureVerifier::~SignatureVerifier() {
22 bool SignatureVerifier::VerifyInit(const uint8
* signature_algorithm
,
23 int signature_algorithm_len
,
24 const uint8
* signature
,
26 const uint8
* public_key_info
,
27 int public_key_info_len
) {
28 signature_
.assign(signature
, signature
+ signature_len
);
29 public_key_info_
.assign(public_key_info
,
30 public_key_info
+ public_key_info_len
);
32 CSSM_ALGORITHMS key_alg
= CSSM_ALGID_RSA
; // TODO(wtc): hardcoded.
34 memset(&public_key_
, 0, sizeof(public_key_
));
35 public_key_
.KeyData
.Data
= const_cast<uint8
*>(&public_key_info_
[0]);
36 public_key_
.KeyData
.Length
= public_key_info_
.size();
37 public_key_
.KeyHeader
.HeaderVersion
= CSSM_KEYHEADER_VERSION
;
38 public_key_
.KeyHeader
.BlobType
= CSSM_KEYBLOB_RAW
;
39 public_key_
.KeyHeader
.Format
= CSSM_KEYBLOB_RAW_FORMAT_X509
;
40 public_key_
.KeyHeader
.AlgorithmId
= key_alg
;
41 public_key_
.KeyHeader
.KeyClass
= CSSM_KEYCLASS_PUBLIC_KEY
;
42 public_key_
.KeyHeader
.KeyAttr
= CSSM_KEYATTR_EXTRACTABLE
;
43 public_key_
.KeyHeader
.KeyUsage
= CSSM_KEYUSE_VERIFY
;
44 CSSM_KEY_SIZE key_size
;
46 crtn
= CSSM_QueryKeySizeInBits(GetSharedCSPHandle(), NULL
,
47 &public_key_
, &key_size
);
49 NOTREACHED() << "CSSM_QueryKeySizeInBits failed: " << crtn
;
52 public_key_
.KeyHeader
.LogicalKeySizeInBits
= key_size
.LogicalKeySizeInBits
;
54 // TODO(wtc): decode signature_algorithm...
55 CSSM_ALGORITHMS sig_alg
= CSSM_ALGID_SHA1WithRSA
;
57 crtn
= CSSM_CSP_CreateSignatureContext(GetSharedCSPHandle(), sig_alg
, NULL
,
58 &public_key_
, &sig_handle_
);
63 crtn
= CSSM_VerifyDataInit(sig_handle_
);
71 void SignatureVerifier::VerifyUpdate(const uint8
* data_part
,
74 data
.Data
= const_cast<uint8
*>(data_part
);
75 data
.Length
= data_part_len
;
76 CSSM_RETURN crtn
= CSSM_VerifyDataUpdate(sig_handle_
, &data
, 1);
77 DCHECK(crtn
== CSSM_OK
);
80 bool SignatureVerifier::VerifyFinal() {
82 sig
.Data
= const_cast<uint8
*>(&signature_
[0]);
83 sig
.Length
= signature_
.size();
84 CSSM_RETURN crtn
= CSSM_VerifyDataFinal(sig_handle_
, &sig
);
87 // crtn is CSSMERR_CSP_VERIFY_FAILED if signature verification fails.
88 return (crtn
== CSSM_OK
);
91 void SignatureVerifier::Reset() {
94 crtn
= CSSM_DeleteContext(sig_handle_
);
95 DCHECK(crtn
== CSSM_OK
);
100 // Can't call CSSM_FreeKey on public_key_ because we constructed
101 // public_key_ manually.