Alexander Afanasyev | 2b4e885 | 2018-06-13 20:32:27 -0400 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, 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 { |
| 23 | namespace nac { |
| 24 | |
| 25 | Name |
| 26 | convertKekNameToKdkPrefix(const Name& kekName, const ErrorCallback& onFailure) |
| 27 | { |
| 28 | // * <access-namespace>/KEK/<key-id> =>> <access-namespace>/KDK/<key-id> |
| 29 | if (kekName.size() < 2 || kekName.get(-2) != KEK) { |
| 30 | onFailure(ErrorCode::KekInvalidName, "Invalid KEK name [" + kekName.toUri() + "]"); |
| 31 | return {}; |
| 32 | } |
| 33 | |
| 34 | return kekName.getPrefix(-2).append(KDK).append(kekName.get(-1)); |
| 35 | } |
| 36 | |
| 37 | std::tuple<Name, Name, Name> |
| 38 | extractKdkInfoFromCkName(const Name& ckDataName, const Name& ckName, const ErrorCallback& onFailure) |
| 39 | { |
| 40 | // <full-ck-name-with-id> | /ENCRYPTED-BY/<kek-prefix>/NAC/KEK/<key-id> |
| 41 | |
| 42 | if (ckDataName.size() < ckName.size() + 1 || |
| 43 | ckDataName.getPrefix(ckName.size()) != ckName || |
| 44 | ckDataName.get(ckName.size()) != ENCRYPTED_BY) { |
| 45 | onFailure(ErrorCode::CkInvalidName, "Invalid CK name [" + ckDataName.toUri() + "]"); |
| 46 | return {}; |
| 47 | } |
| 48 | |
| 49 | auto kekName = ckDataName.getSubName(ckName.size() + 1); |
| 50 | return std::make_tuple(convertKekNameToKdkPrefix(kekName, onFailure), |
| 51 | kekName.getPrefix(-2), |
| 52 | kekName.getPrefix(-2).append("KEY").append(kekName.get(-1))); |
| 53 | } |
| 54 | |
| 55 | } // namespace nac |
| 56 | } // namespace ndn |