common.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California
4  *
5  * NAC library is free software: you can redistribute it and/or modify it under the
6  * terms of the GNU Lesser General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later version.
8  *
9  * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12  *
13  * You should have received copies of the GNU General Public License and GNU Lesser
14  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * See AUTHORS.md for complete list of NAC library authors and contributors.
18  */
19 
20 #include "common.hpp"
21 
22 namespace ndn::nac {
23 
24 Name
25 convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure)
26 {
27  // * <access-namespace>/KEK/<key-id> =>> <access-namespace>/KDK/<key-id>
28  if (kekName.size() < 2 || kekName.get(-2) != KEK) {
29  onFailure(ErrorCode::KekInvalidName, "Invalid KEK name [" + kekName.toUri() + "]");
30  return {};
31  }
32 
33  return kekName.getPrefix(-2).append(KDK).append(kekName.get(-1));
34 }
35 
36 std::tuple<Name, Name, Name>
37 extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure)
38 {
39  // <full-ck-name-with-id> | /ENCRYPTED-BY/<kek-prefix>/NAC/KEK/<key-id>
40 
41  if (ckDataName.size() < ckName.size() + 1 ||
42  ckDataName.getPrefix(ckName.size()) != ckName ||
43  ckDataName.get(ckName.size()) != ENCRYPTED_BY) {
44  onFailure(ErrorCode::CkInvalidName, "Invalid CK name [" + ckDataName.toUri() + "]");
45  return {};
46  }
47 
48  auto kekName = ckDataName.getSubName(ckName.size() + 1);
49  return {convertKekNameToKdkPrefix(kekName, onFailure),
50  kekName.getPrefix(-2),
51  kekName.getPrefix(-2).append("KEY").append(kekName.get(-1))};
52 }
53 
54 } // namespace ndn::nac
const name::Component KDK
Definition: common.hpp:85
std::tuple< Name, Name, Name > extractKdkInfoFromCkName(const Name &ckDataName, const Name &ckName, const ErrorCallback &onFailure)
Extract KDK information from name of CK data packet name.
Definition: common.cpp:37
Name convertKekNameToKdkPrefix(const Name &kekName, const ErrorCallback &onFailure)
Convert KEK name to KDK prefix:
Definition: common.cpp:25
std::function< void(const ErrorCode &, const std::string &)> ErrorCallback
Definition: common.hpp:117
const name::Component ENCRYPTED_BY
Definition: common.hpp:82
const name::Component KEK
Definition: common.hpp:84