blob: a1e33cbd593df61ac207b4ddc25adfc32b575dae [file] [log] [blame]
Yingdi Yu64c3fb42014-02-26 17:30:04 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Yu64c3fb42014-02-26 17:30:04 -080011 */
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
20namespace ndn {
21
22class SecuredBag
23{
24public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070025 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 Yu64c3fb42014-02-26 17:30:04 -080034
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070035 SecuredBag()
Yingdi Yu64c3fb42014-02-26 17:30:04 -080036 : m_wire(tlv::security::IdentityPackage)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070037 {
38 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080039
40 SecuredBag(const IdentityCertificate& cert,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070041 ConstBufferPtr key)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080042 : 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 virtual
Yingdi Yu64c3fb42014-02-26 17:30:04 -080053 ~SecuredBag()
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070054 {
55 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056
Yingdi Yu64c3fb42014-02-26 17:30:04 -080057 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 wireDecode(const Block& wire)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080059 {
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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070082
Yingdi Yu64c3fb42014-02-26 17:30:04 -080083 ConstBufferPtr
84 getKey() const
85 {
86 return m_key;
87 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088
Yingdi Yu64c3fb42014-02-26 17:30:04 -080089private:
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