blob: 18df6592b891dda91a5d1c295206929c0c16064c [file] [log] [blame]
Yingdi Yu0b60e7a2015-07-16 21:05:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "back-end.hpp"
23#include "key-handle.hpp"
24#include "tpm.hpp"
25#include "../transform.hpp"
26#include "../../encoding/buffer-stream.hpp"
27#include "../../util/random.hpp"
28#include "../pib/key.hpp"
29
30namespace ndn {
31namespace security {
32namespace tpm {
33
34BackEnd::~BackEnd() = default;
35
36bool
37BackEnd::hasKey(const Name& keyName) const
38{
39 return doHasKey(keyName);
40}
41
42unique_ptr<KeyHandle>
43BackEnd::getKeyHandle(const Name& keyName) const
44{
45 return doGetKeyHandle(keyName);
46}
47
48unique_ptr<KeyHandle>
49BackEnd::createKey(const Name& identity, const KeyParams& params)
50{
51 // key name checking
52 switch (params.getKeyIdType()) {
53 case KeyIdType::USER_SPECIFIED: { // keyId is pre-set.
54 Name keyName = v2::constructKeyName(identity, params.getKeyId());
55 if (hasKey(keyName)) {
56 BOOST_THROW_EXCEPTION(Tpm::Error("Key `" + keyName.toUri() + "` already exists"));
57 }
58 break;
59 }
60 case KeyIdType::SHA256: {
61 // KeyName will be assigned in setKeyName after key is generated
62 break;
63 }
64 case KeyIdType::RANDOM: {
65 Name keyName;
66 name::Component keyId;
67 do {
68 keyId = name::Component::fromNumber(random::generateSecureWord64());
69 keyName = v2::constructKeyName(identity, params.getKeyId());
70 } while (hasKey(keyName));
71
72 const_cast<KeyParams&>(params).setKeyId(keyId);
73 break;
74 }
75 default: {
76 BOOST_THROW_EXCEPTION(Error("Unsupported key id type"));
77 }
78 }
79
80 return doCreateKey(identity, params);
81}
82
83void
84BackEnd::deleteKey(const Name& keyName)
85{
86 doDeleteKey(keyName);
87}
88
89ConstBufferPtr
90BackEnd::exportKey(const Name& keyName, const char* pw, size_t pwLen)
91{
92 if (!hasKey(keyName)) {
93 BOOST_THROW_EXCEPTION(Error("Key `" + keyName.toUri() + "` does not exist"));
94 }
95 return doExportKey(keyName, pw, pwLen);
96}
97
98void
99BackEnd::importKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen)
100{
101 if (hasKey(keyName)) {
102 BOOST_THROW_EXCEPTION(Error("Key `" + keyName.toUri() + "` already exists"));
103 }
104 doImportKey(keyName, pkcs8, pkcs8Len, pw, pwLen);
105}
106
107void
108BackEnd::setKeyName(KeyHandle& keyHandle, const Name& identity, const KeyParams& params)
109{
110 name::Component keyId;
111 switch (params.getKeyIdType()) {
112 case KeyIdType::USER_SPECIFIED:
113 keyId = params.getKeyId();
114 break;
115 case KeyIdType::SHA256: {
116 using namespace transform;
117
118 OBufferStream os;
119 bufferSource(*keyHandle.derivePublicKey()) >> digestFilter() >> streamSink(os);
120 keyId = name::Component(os.buf());
121 break;
122 }
123 case KeyIdType::RANDOM: {
124 BOOST_ASSERT(!params.getKeyId().empty());
125 keyId = params.getKeyId();
126 break;
127 }
128 default: {
129 BOOST_ASSERT(false);
130 }
131 }
132
133 keyHandle.setKeyName(v2::constructKeyName(identity, keyId));
134}
135
136} // namespace tpm
137} // namespace security
138} // namespace ndn