blob: 827225aab1c87f126f61448ca687e1f27dfa3861 [file] [log] [blame]
Yingdi Yu64c3fb42014-02-26 17:30:04 -08001/* -*- 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
14namespace ndn {
15
16class SecuredBag
17{
18public:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070019 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 Yu64c3fb42014-02-26 17:30:04 -080028
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070029 SecuredBag()
Yingdi Yu64c3fb42014-02-26 17:30:04 -080030 : m_wire(tlv::security::IdentityPackage)
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070031 {
32 }
Yingdi Yu64c3fb42014-02-26 17:30:04 -080033
34 SecuredBag(const IdentityCertificate& cert,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070035 ConstBufferPtr key)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080036 : 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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070046 virtual
Yingdi Yu64c3fb42014-02-26 17:30:04 -080047 ~SecuredBag()
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070048 {
49 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050
Yingdi Yu64c3fb42014-02-26 17:30:04 -080051 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052 wireDecode(const Block& wire)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080053 {
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 Afanasyevfdbfc6d2014-04-14 15:12:11 -070076
Yingdi Yu64c3fb42014-02-26 17:30:04 -080077 ConstBufferPtr
78 getKey() const
79 {
80 return m_key;
81 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070082
Yingdi Yu64c3fb42014-02-26 17:30:04 -080083private:
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