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