1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "net/quic/test_tools/crypto_test_utils.h"
6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/stl_util.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_util.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "crypto/ec_private_key.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "crypto/ec_signature_creator.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "net/quic/crypto/channel_id.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "net/quic/crypto/channel_id_chromium.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using base::StringPiece;
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using std::string;
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace net {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace test {
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class TestChannelIDSource : public ChannelIDSource {
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~TestChannelIDSource() {
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    STLDeleteValues(&hostname_to_key_);
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // ChannelIDSource implementation.
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual QuicAsyncStatus GetChannelIDKey(
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const string& hostname,
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      scoped_ptr<ChannelIDKey>* channel_id_key,
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      ChannelIDSourceCallback* /*callback*/) OVERRIDE {
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    channel_id_key->reset(new ChannelIDKeyChromium(HostnameToKey(hostname)));
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return QUIC_SUCCESS;
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) private:
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  typedef std::map<string, crypto::ECPrivateKey*> HostnameToKeyMap;
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  crypto::ECPrivateKey* HostnameToKey(const string& hostname) {
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    HostnameToKeyMap::const_iterator it = hostname_to_key_.find(hostname);
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (it != hostname_to_key_.end()) {
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      return it->second->Copy();
447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    crypto::ECPrivateKey* keypair = crypto::ECPrivateKey::Create();
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    if (!keypair) {
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return NULL;
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    }
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    hostname_to_key_[hostname] = keypair;
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return keypair->Copy();
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  }
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  HostnameToKeyMap hostname_to_key_;
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// static
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)ChannelIDSource* CryptoTestUtils::ChannelIDSourceForTesting() {
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return new TestChannelIDSource();
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace test
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace net
65