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