Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NDN_SECURITY_SECURED_BAG_HPP |
| 8 | #define NDN_SECURITY_SECURED_BAG_HPP |
| 9 | |
| 10 | #include "../common.hpp" |
| 11 | #include "identity-certificate.hpp" |
| 12 | #include "../encoding/tlv-security.hpp" |
| 13 | |
| 14 | namespace ndn { |
| 15 | |
| 16 | class SecuredBag |
| 17 | { |
| 18 | public: |
| 19 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 20 | |
| 21 | SecuredBag() |
| 22 | : m_wire(tlv::security::IdentityPackage) |
| 23 | {} |
| 24 | |
| 25 | SecuredBag(const IdentityCertificate& cert, |
| 26 | ConstBufferPtr key) |
| 27 | : m_cert(cert) |
| 28 | , m_key(key) |
| 29 | , m_wire(tlv::security::IdentityPackage) |
| 30 | { |
| 31 | Block wireKey(tlv::security::KeyPackage, m_key); |
| 32 | Block wireCert(tlv::security::CertificatePackage, cert.wireEncode()); |
| 33 | m_wire.push_back(wireCert); |
| 34 | m_wire.push_back(wireKey); |
| 35 | } |
| 36 | |
| 37 | virtual |
| 38 | ~SecuredBag() |
| 39 | {} |
| 40 | |
| 41 | void |
| 42 | wireDecode(const Block &wire) |
| 43 | { |
| 44 | m_wire = wire; |
| 45 | m_wire.parse(); |
| 46 | |
| 47 | m_cert.wireDecode(m_wire.get(tlv::security::CertificatePackage).blockFromValue()); |
| 48 | |
| 49 | Block wireKey = m_wire.get(tlv::security::KeyPackage); |
| 50 | shared_ptr<Buffer> key = make_shared<Buffer>(wireKey.value(), wireKey.value_size()); |
| 51 | m_key = key; |
| 52 | } |
| 53 | |
| 54 | inline const Block& |
| 55 | wireEncode() const |
| 56 | { |
| 57 | m_wire.encode(); |
| 58 | return m_wire; |
| 59 | } |
| 60 | |
| 61 | const IdentityCertificate& |
| 62 | getCertificate() const |
| 63 | { |
| 64 | return m_cert; |
| 65 | } |
| 66 | |
| 67 | ConstBufferPtr |
| 68 | getKey() const |
| 69 | { |
| 70 | return m_key; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | IdentityCertificate m_cert; |
| 75 | ConstBufferPtr m_key; |
| 76 | |
| 77 | mutable Block m_wire; |
| 78 | }; |
| 79 | |
| 80 | } // namespace ndn |
| 81 | |
| 82 | #endif //NDN_SECURITY_IDENTITY_CERTIFICATE_HPP |