Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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. | |
| 4 | |
| 5 #include "crypto/cup.h" | |
| 6 | |
| 7 #include <base64.h> | |
| 8 #include <keyhi.h> | |
| 9 #include <pk11pub.h> | |
| 10 #include <seccomon.h> | |
| 11 | |
| 12 #include "base/logging.h" | |
| 13 #include "crypto/nss_util.h" | |
| 14 #include "crypto/scoped_nss_types.h" | |
| 15 | |
| 16 using base::StringPiece; | |
| 17 | |
| 18 namespace crypto { | |
| 19 | |
| 20 typedef scoped_ptr_malloc< | |
| 21 CERTSubjectPublicKeyInfo, | |
| 22 NSSDestroyer<CERTSubjectPublicKeyInfo, SECKEY_DestroySubjectPublicKeyInfo> > | |
| 23 ScopedCERTSubjectPublicKeyInfo; | |
| 24 | |
| 25 size_t ClientUpdateProtocol::LoadPublicKey(const StringPiece& public_key) { | |
| 26 EnsureNSSInit(); | |
| 27 | |
| 28 // The binary blob |public_key| is expected to be a DER-encoded ASN.1-encoded | |
|
wtc
2013/05/30 21:38:56
ASN.1-encoded => ASN.1
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 29 // Subject Public Key Info. | |
| 30 std::vector<unsigned char> pkey(public_key.begin(), public_key.end()); | |
| 31 SECItem spki_item; | |
| 32 spki_item.type = siBuffer; | |
| 33 spki_item.data = &pkey[0]; | |
|
wtc
2013/05/30 21:38:56
You can set spki_item.data to (unsigned char*)publ
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 34 spki_item.len = static_cast<unsigned int>(public_key.size()); | |
| 35 | |
| 36 ScopedCERTSubjectPublicKeyInfo spki( | |
| 37 SECKEY_DecodeDERSubjectPublicKeyInfo(&spki_item)); | |
| 38 if (!spki.get()) | |
| 39 return 0; | |
| 40 | |
| 41 public_key_.reset(SECKEY_ExtractPublicKey(spki.get())); | |
| 42 if (!public_key_.get()) | |
| 43 return 0; | |
| 44 | |
| 45 return SECKEY_PublicKeyStrength(public_key_.get()); | |
| 46 } | |
| 47 | |
| 48 bool ClientUpdateProtocol::EncryptKeySource( | |
| 49 const std::vector<uint8>& key_source) { | |
| 50 // WARNING: This call bypasses the usual PKCS padding and does direct RSA | |
|
wtc
2013/05/30 21:38:56
PKCS => PKCS #1
(There are other PKCS standards.
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 51 // exponentiation. This not secure without taking measures to ensure that | |
|
wtc
2013/05/30 21:38:56
This not secure => This is not secure
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 52 // the contents of r are suitable. This is done to remain compatible with | |
| 53 // the implementation on the Google Update servers; don't copy-paste this | |
| 54 // code arbitrarily and expect it to work and/or remain secure! | |
| 55 SECKEYPublicKey* pkv = public_key_.get(); | |
|
wtc
2013/05/30 21:38:56
The Style Guide recommends against variable names
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 56 if (!pkv) | |
| 57 return false; | |
| 58 | |
| 59 size_t keysize = SECKEY_PublicKeyStrength(pkv); | |
| 60 if (key_source.size() != keysize) | |
| 61 return false; | |
| 62 | |
| 63 // Make a copy of the key source, since for some reason, the NSS function | |
| 64 // takes the data in a non-const pointer. | |
|
wtc
2013/05/30 21:38:56
It *should* be safe to use a const_cast here as we
Ryan Myers (chromium)
2013/05/30 21:55:11
Done.
| |
| 65 std::vector<uint8> key_source_copy(key_source); | |
| 66 | |
| 67 encrypted_key_source_.resize(keysize); | |
| 68 return SECSuccess == PK11_PubEncryptRaw(pkv, | |
| 69 &encrypted_key_source_[0], | |
| 70 &key_source_copy[0], | |
| 71 key_source_copy.size(), | |
| 72 NULL); | |
| 73 } | |
| 74 | |
| 75 } // namespace crypto | |
| 76 | |
| OLD | NEW |