blob: f2075e62d17de41e9003398ee195c0a0bb729991 [file] [log] [blame]
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -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.
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080011 */
12
13#ifndef NDN_SIGNATURE_HPP
14#define NDN_SIGNATURE_HPP
15
16namespace ndn {
17
18/**
19 * A Signature is storage for the signature-related information (info and value) in a Data packet.
20 */
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070021class Signature
22{
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080023public:
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070024 class Error : public Tlv::Error
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070025 {
26 public:
27 explicit
28 Error(const std::string& what)
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -070029 : Tlv::Error(what)
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070030 {
31 }
32 };
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080033
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080034 enum {
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -080035 Sha256 = 0,
36 Sha256WithRsa = 1
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080037 };
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070038
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080039 Signature()
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070040 : m_type(-1)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080041 {
42 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070043
44 Signature(const Block& info, const Block& value = Block())
45 : m_value(value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080046 {
Alexander Afanasyev31b2adf2014-01-03 15:31:16 -080047 setInfo(info);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080048 }
49
50 operator bool() const
51 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070052 return m_type != -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080053 }
54
55 uint32_t
56 getType() const
57 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070058 return m_type;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080059 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070060
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080061 const Block&
62 getInfo() const
63 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070064 m_info.encode(); // will do nothing if wire already exists
65 return m_info;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080066 }
67
68 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070069 setInfo(const Block& info)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080070 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070071 m_info = info;
72 if (m_info.hasWire() || m_info.hasValue())
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080073 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070074 m_info.parse();
75 const Block& signatureType = m_info.get(Tlv::SignatureType);
76 m_type = readNonNegativeInteger(signatureType);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080077 }
78 else
79 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070080 m_type = -1;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080081 }
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070082 }
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080083
84 const Block&
85 getValue() const
86 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070087 m_value.encode(); // will do nothing if wire already exists
88 return m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080089 }
90
91 void
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070092 setValue(const Block& value)
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080093 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -070094 m_value = value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -080095 }
96
97 void
98 reset()
99 {
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700100 m_type = -1;
101 m_info.reset();
102 m_value.reset();
103 }
104
105public: // EqualityComparable concept
106 bool
107 operator==(const Signature& other) const
108 {
109 return getInfo() == other.getInfo() &&
110 getValue() == other.getValue();
111 }
112
113 bool
114 operator!=(const Signature& other) const
115 {
116 return !(*this == other);
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800117 }
118
Alexander Afanasyevfa13f8e2014-01-03 15:19:07 -0800119protected:
Alexander Afanasyevff2d08f2014-04-07 18:28:25 -0700120 int32_t m_type;
121
122 mutable Block m_info;
123 mutable Block m_value;
Alexander Afanasyev2ba8f662014-01-05 22:53:18 -0800124};
125
126} // namespace ndn
127
128#endif // NDN_SIGNATURE_HPP