blob: 5bc6d1af57580b9ca88bffc6f659d433fa54e7b0 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu64c3fb42014-02-26 17:30:04 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Yu64c3fb42014-02-26 17:30:04 -080020 */
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
29namespace ndn {
30
31class SecuredBag
32{
33public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 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 Yu64c3fb42014-02-26 17:30:04 -080043
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070044 SecuredBag()
Yingdi Yu64c3fb42014-02-26 17:30:04 -080045 : m_wire(tlv::security::IdentityPackage)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070046 {
47 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080048
49 SecuredBag(const IdentityCertificate& cert,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070050 ConstBufferPtr key)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080051 : 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070061 virtual
Yingdi Yu64c3fb42014-02-26 17:30:04 -080062 ~SecuredBag()
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070063 {
64 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070065
Yingdi Yu64c3fb42014-02-26 17:30:04 -080066 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067 wireDecode(const Block& wire)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080068 {
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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070091
Yingdi Yu64c3fb42014-02-26 17:30:04 -080092 ConstBufferPtr
93 getKey() const
94 {
95 return m_key;
96 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097
Yingdi Yu64c3fb42014-02-26 17:30:04 -080098private:
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