Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #ifndef NDN_SECURITY_SECURED_BAG_HPP |
| 23 | #define NDN_SECURITY_SECURED_BAG_HPP |
| 24 | |
| 25 | #include "../common.hpp" |
| 26 | #include "identity-certificate.hpp" |
| 27 | #include "../encoding/tlv-security.hpp" |
| 28 | |
| 29 | namespace ndn { |
| 30 | |
| 31 | class SecuredBag |
| 32 | { |
| 33 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 34 | class Error : public std::runtime_error |
| 35 | { |
| 36 | public: |
| 37 | explicit |
| 38 | Error(const std::string& what) |
| 39 | : std::runtime_error(what) |
| 40 | { |
| 41 | } |
| 42 | }; |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 44 | SecuredBag() |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 45 | : m_wire(tlv::security::IdentityPackage) |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 46 | { |
| 47 | } |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 48 | |
| 49 | SecuredBag(const IdentityCertificate& cert, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 50 | ConstBufferPtr key) |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 51 | : m_cert(cert) |
| 52 | , m_key(key) |
| 53 | , m_wire(tlv::security::IdentityPackage) |
| 54 | { |
| 55 | Block wireKey(tlv::security::KeyPackage, m_key); |
| 56 | Block wireCert(tlv::security::CertificatePackage, cert.wireEncode()); |
| 57 | m_wire.push_back(wireCert); |
| 58 | m_wire.push_back(wireKey); |
| 59 | } |
| 60 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 61 | virtual |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 62 | ~SecuredBag() |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 63 | { |
| 64 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 65 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 66 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 67 | wireDecode(const Block& wire) |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 68 | { |
| 69 | m_wire = wire; |
| 70 | m_wire.parse(); |
| 71 | |
| 72 | m_cert.wireDecode(m_wire.get(tlv::security::CertificatePackage).blockFromValue()); |
| 73 | |
| 74 | Block wireKey = m_wire.get(tlv::security::KeyPackage); |
| 75 | shared_ptr<Buffer> key = make_shared<Buffer>(wireKey.value(), wireKey.value_size()); |
| 76 | m_key = key; |
| 77 | } |
| 78 | |
| 79 | inline const Block& |
| 80 | wireEncode() const |
| 81 | { |
| 82 | m_wire.encode(); |
| 83 | return m_wire; |
| 84 | } |
| 85 | |
| 86 | const IdentityCertificate& |
| 87 | getCertificate() const |
| 88 | { |
| 89 | return m_cert; |
| 90 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 92 | ConstBufferPtr |
| 93 | getKey() const |
| 94 | { |
| 95 | return m_key; |
| 96 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 97 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 98 | private: |
| 99 | IdentityCertificate m_cert; |
| 100 | ConstBufferPtr m_key; |
| 101 | |
| 102 | mutable Block m_wire; |
| 103 | }; |
| 104 | |
| 105 | } // namespace ndn |
| 106 | |
| 107 | #endif //NDN_SECURITY_IDENTITY_CERTIFICATE_HPP |