blob: 45488b185c2e6bc35701650fc985875eeec71f4a [file] [log] [blame]
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventobde084f2022-04-17 00:21:35 -04002/*
3 * Copyright (c) 2014-2022, Regents of the University of California
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -04004 *
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
Davide Pesaventobde084f2022-04-17 00:21:35 -040022namespace ndn::nac {
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040023
24Name
25convertKekNameToKdkPrefix(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
36std::tuple<Name, Name, Name>
37extractKdkInfoFromCkName(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);
Davide Pesaventobde084f2022-04-17 00:21:35 -040049 return {convertKekNameToKdkPrefix(kekName, onFailure),
50 kekName.getPrefix(-2),
51 kekName.getPrefix(-2).append("KEY").append(kekName.get(-1))};
Alexander Afanasyev2b4e8852018-06-13 20:32:27 -040052}
53
Davide Pesaventobde084f2022-04-17 00:21:35 -040054} // namespace ndn::nac