Index: crypto/cup_nss.cc |
=================================================================== |
--- crypto/cup_nss.cc (revision 0) |
+++ crypto/cup_nss.cc (revision 0) |
@@ -0,0 +1,76 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "crypto/cup.h" |
+ |
+#include <base64.h> |
+#include <keyhi.h> |
+#include <pk11pub.h> |
+#include <seccomon.h> |
+ |
+#include "base/logging.h" |
+#include "crypto/nss_util.h" |
+#include "crypto/scoped_nss_types.h" |
+ |
+using base::StringPiece; |
+ |
+namespace crypto { |
+ |
+typedef scoped_ptr_malloc< |
+ CERTSubjectPublicKeyInfo, |
+ NSSDestroyer<CERTSubjectPublicKeyInfo, SECKEY_DestroySubjectPublicKeyInfo> > |
+ ScopedCERTSubjectPublicKeyInfo; |
+ |
+size_t ClientUpdateProtocol::LoadPublicKey(const StringPiece& public_key) { |
+ EnsureNSSInit(); |
+ |
+ // 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.
|
+ // Subject Public Key Info. |
+ std::vector<unsigned char> pkey(public_key.begin(), public_key.end()); |
+ SECItem spki_item; |
+ spki_item.type = siBuffer; |
+ 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.
|
+ spki_item.len = static_cast<unsigned int>(public_key.size()); |
+ |
+ ScopedCERTSubjectPublicKeyInfo spki( |
+ SECKEY_DecodeDERSubjectPublicKeyInfo(&spki_item)); |
+ if (!spki.get()) |
+ return 0; |
+ |
+ public_key_.reset(SECKEY_ExtractPublicKey(spki.get())); |
+ if (!public_key_.get()) |
+ return 0; |
+ |
+ return SECKEY_PublicKeyStrength(public_key_.get()); |
+} |
+ |
+bool ClientUpdateProtocol::EncryptKeySource( |
+ const std::vector<uint8>& key_source) { |
+ // 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.
|
+ // 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.
|
+ // the contents of r are suitable. This is done to remain compatible with |
+ // the implementation on the Google Update servers; don't copy-paste this |
+ // code arbitrarily and expect it to work and/or remain secure! |
+ 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.
|
+ if (!pkv) |
+ return false; |
+ |
+ size_t keysize = SECKEY_PublicKeyStrength(pkv); |
+ if (key_source.size() != keysize) |
+ return false; |
+ |
+ // Make a copy of the key source, since for some reason, the NSS function |
+ // 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.
|
+ std::vector<uint8> key_source_copy(key_source); |
+ |
+ encrypted_key_source_.resize(keysize); |
+ return SECSuccess == PK11_PubEncryptRaw(pkv, |
+ &encrypted_key_source_[0], |
+ &key_source_copy[0], |
+ key_source_copy.size(), |
+ NULL); |
+} |
+ |
+} // namespace crypto |
+ |