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: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 19 | class Error : public std::runtime_error |
| 20 | { |
| 21 | public: |
| 22 | explicit |
| 23 | Error(const std::string& what) |
| 24 | : std::runtime_error(what) |
| 25 | { |
| 26 | } |
| 27 | }; |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 29 | SecuredBag() |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 30 | : m_wire(tlv::security::IdentityPackage) |
| 31 | {} |
| 32 | |
| 33 | SecuredBag(const IdentityCertificate& cert, |
| 34 | ConstBufferPtr key) |
| 35 | : m_cert(cert) |
| 36 | , m_key(key) |
| 37 | , m_wire(tlv::security::IdentityPackage) |
| 38 | { |
| 39 | Block wireKey(tlv::security::KeyPackage, m_key); |
| 40 | Block wireCert(tlv::security::CertificatePackage, cert.wireEncode()); |
| 41 | m_wire.push_back(wireCert); |
| 42 | m_wire.push_back(wireKey); |
| 43 | } |
| 44 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 45 | virtual |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 46 | ~SecuredBag() |
| 47 | {} |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 49 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 50 | wireDecode(const Block& wire) |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 51 | { |
| 52 | m_wire = wire; |
| 53 | m_wire.parse(); |
| 54 | |
| 55 | m_cert.wireDecode(m_wire.get(tlv::security::CertificatePackage).blockFromValue()); |
| 56 | |
| 57 | Block wireKey = m_wire.get(tlv::security::KeyPackage); |
| 58 | shared_ptr<Buffer> key = make_shared<Buffer>(wireKey.value(), wireKey.value_size()); |
| 59 | m_key = key; |
| 60 | } |
| 61 | |
| 62 | inline const Block& |
| 63 | wireEncode() const |
| 64 | { |
| 65 | m_wire.encode(); |
| 66 | return m_wire; |
| 67 | } |
| 68 | |
| 69 | const IdentityCertificate& |
| 70 | getCertificate() const |
| 71 | { |
| 72 | return m_cert; |
| 73 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 74 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 75 | ConstBufferPtr |
| 76 | getKey() const |
| 77 | { |
| 78 | return m_key; |
| 79 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 80 | |
Yingdi Yu | 64c3fb4 | 2014-02-26 17:30:04 -0800 | [diff] [blame] | 81 | private: |
| 82 | IdentityCertificate m_cert; |
| 83 | ConstBufferPtr m_key; |
| 84 | |
| 85 | mutable Block m_wire; |
| 86 | }; |
| 87 | |
| 88 | } // namespace ndn |
| 89 | |
| 90 | #endif //NDN_SECURITY_IDENTITY_CERTIFICATE_HPP |