blob: 584898142b468332db0b8092e3befc772b96ad0b [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)
31 {}
32
33 SecuredBag(const IdentityCertificate& cert,
34 ConstBufferPtr key)
35 : m_cert(cert)
36 , m_key(key)
37 , m_wire(tlv::security::IdentityPackage)
38 {
39 Block wireKey(tlv::security::KeyPackage, m_key);
40 Block wireCert(tlv::security::CertificatePackage, cert.wireEncode());
41 m_wire.push_back(wireCert);
42 m_wire.push_back(wireKey);
43 }
44
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070045 virtual
Yingdi Yu64c3fb42014-02-26 17:30:04 -080046 ~SecuredBag()
47 {}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070048
Yingdi Yu64c3fb42014-02-26 17:30:04 -080049 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070050 wireDecode(const Block& wire)
Yingdi Yu64c3fb42014-02-26 17:30:04 -080051 {
52 m_wire = wire;
53 m_wire.parse();
54
55 m_cert.wireDecode(m_wire.get(tlv::security::CertificatePackage).blockFromValue());
56
57 Block wireKey = m_wire.get(tlv::security::KeyPackage);
58 shared_ptr<Buffer> key = make_shared<Buffer>(wireKey.value(), wireKey.value_size());
59 m_key = key;
60 }
61
62 inline const Block&
63 wireEncode() const
64 {
65 m_wire.encode();
66 return m_wire;
67 }
68
69 const IdentityCertificate&
70 getCertificate() const
71 {
72 return m_cert;
73 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070074
Yingdi Yu64c3fb42014-02-26 17:30:04 -080075 ConstBufferPtr
76 getKey() const
77 {
78 return m_key;
79 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070080
Yingdi Yu64c3fb42014-02-26 17:30:04 -080081private:
82 IdentityCertificate m_cert;
83 ConstBufferPtr m_key;
84
85 mutable Block m_wire;
86};
87
88} // namespace ndn
89
90#endif //NDN_SECURITY_IDENTITY_CERTIFICATE_HPP