Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * See COPYING for copyright and distribution information. |
| 4 | */ |
| 5 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 6 | #ifndef NDN_DATA_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 7 | #define NDN_DATA_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 8 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 9 | #include "common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 10 | #include "name.hpp" |
| 11 | #include "publisher-public-key-digest.hpp" |
| 12 | #include "key.hpp" |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 13 | #include "c/data.h" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 14 | |
| 15 | namespace ndn { |
| 16 | |
| 17 | class Signature { |
| 18 | public: |
| 19 | /** |
| 20 | * Set the signatureStruct to point to the values in this signature object, without copying any memory. |
| 21 | * WARNING: The resulting pointers in signatureStruct are invalid after a further use of this object which could reallocate memory. |
| 22 | * @param signatureStruct a C ndn_Signature struct where the name components array is already allocated. |
| 23 | */ |
| 24 | void get(struct ndn_Signature &signatureStruct) const; |
| 25 | |
| 26 | /** |
| 27 | * Clear this signature, and set the values by copying from the ndn_Signature struct. |
| 28 | * @param signatureStruct a C ndn_Signature struct |
| 29 | */ |
| 30 | void set(const struct ndn_Signature &signatureStruct); |
| 31 | |
Jeff Thompson | 237fbc0 | 2013-08-08 20:33:21 -0700 | [diff] [blame] | 32 | const std::vector<unsigned char> &getDigestAlgorithm() const { return digestAlgorithm_; } |
| 33 | std::vector<unsigned char> &getDigestAlgorithm() { return digestAlgorithm_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 34 | |
Jeff Thompson | 237fbc0 | 2013-08-08 20:33:21 -0700 | [diff] [blame] | 35 | const std::vector<unsigned char> &getWitness() const { return witness_; } |
| 36 | std::vector<unsigned char> &getWitness() { return witness_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 37 | |
Jeff Thompson | 237fbc0 | 2013-08-08 20:33:21 -0700 | [diff] [blame] | 38 | const std::vector<unsigned char> &getSignature() const { return signature_; } |
| 39 | std::vector<unsigned char> &getSignature() { return signature_; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 40 | |
| 41 | void setDigestAlgorithm(const std::vector<unsigned char> &digestAlgorithm) { digestAlgorithm_ = digestAlgorithm; } |
| 42 | void setDigestAlgorithm(const unsigned char *digestAlgorithm, unsigned int digestAlgorithmLength) |
| 43 | { |
| 44 | setVector(digestAlgorithm_, digestAlgorithm, digestAlgorithmLength); |
| 45 | } |
| 46 | |
| 47 | void setWitness(const std::vector<unsigned char> &witness) { witness_ = witness; } |
| 48 | void setWitness(const unsigned char *witness, unsigned int witnessLength) |
| 49 | { |
| 50 | setVector(witness_, witness, witnessLength); |
| 51 | } |
| 52 | |
| 53 | void setSignature(const std::vector<unsigned char> &signature) { signature_ = signature; } |
| 54 | void setSignature(const unsigned char *signature, unsigned int signatureLength) |
| 55 | { |
| 56 | setVector(signature_, signature, signatureLength); |
| 57 | } |
Jeff Thompson | f842d21 | 2013-08-12 11:05:28 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Clear the digest algorithm, witness and signature fields. |
| 61 | */ |
| 62 | void clear() |
| 63 | { |
| 64 | digestAlgorithm_.clear(); |
| 65 | witness_.clear(); |
| 66 | signature_.clear(); |
| 67 | } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 68 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 69 | private: |
| 70 | std::vector<unsigned char> digestAlgorithm_; /**< if empty, the default is 2.16.840.1.101.3.4.2.1 (sha-256) */ |
| 71 | std::vector<unsigned char> witness_; |
| 72 | std::vector<unsigned char> signature_; |
| 73 | }; |
| 74 | |
| 75 | class SignedInfo { |
| 76 | public: |
| 77 | SignedInfo() |
| 78 | { |
| 79 | type_ = ndn_ContentType_DATA; |
| 80 | freshnessSeconds_ = -1; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set the signedInfoStruct to point to the values in this signed info object, without copying any memory. |
| 85 | * WARNING: The resulting pointers in signedInfoStruct are invalid after a further use of this object which could reallocate memory. |
| 86 | * @param signedInfoStruct a C ndn_SignedInfo struct where the name components array is already allocated. |
| 87 | */ |
| 88 | void get(struct ndn_SignedInfo &signedInfoStruct) const; |
| 89 | |
| 90 | /** |
| 91 | * Clear this signed info, and set the values by copying from the ndn_SignedInfo struct. |
| 92 | * @param signedInfoStruct a C ndn_SignedInfo struct |
| 93 | */ |
| 94 | void set(const struct ndn_SignedInfo &signedInfoStruct); |
| 95 | |
| 96 | const PublisherPublicKeyDigest &getPublisherPublicKeyDigest() const { return publisherPublicKeyDigest_; } |
Jeff Thompson | ac1b0ac | 2013-08-08 20:09:39 -0700 | [diff] [blame] | 97 | PublisherPublicKeyDigest &getPublisherPublicKeyDigest() { return publisherPublicKeyDigest_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 98 | |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 99 | double getTimestampMilliseconds() const { return timestampMilliseconds_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 100 | |
| 101 | int getType() const { return type_; } |
| 102 | |
| 103 | int getFreshnessSeconds() const { return freshnessSeconds_; } |
| 104 | |
| 105 | const std::vector<unsigned char> getFinalBlockID() const { return finalBlockID_; } |
Jeff Thompson | ac1b0ac | 2013-08-08 20:09:39 -0700 | [diff] [blame] | 106 | std::vector<unsigned char> getFinalBlockID() { return finalBlockID_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 107 | |
| 108 | const KeyLocator &getKeyLocator() const { return keyLocator_; } |
Jeff Thompson | ac1b0ac | 2013-08-08 20:09:39 -0700 | [diff] [blame] | 109 | KeyLocator &getKeyLocator() { return keyLocator_; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 110 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 111 | void setPublisherPublicKeyDigest(const PublisherPublicKeyDigest &publisherPublicKeyDigest) { publisherPublicKeyDigest_ = publisherPublicKeyDigest; } |
| 112 | |
| 113 | void setTimestampMilliseconds(double timestampMilliseconds) { timestampMilliseconds_ = timestampMilliseconds; } |
| 114 | |
| 115 | void setType(int type) { type_ = type; } |
| 116 | |
| 117 | void setFreshnessSeconds(int freshnessSeconds) { freshnessSeconds_ = freshnessSeconds; } |
| 118 | |
| 119 | void setFinalBlockID(const std::vector<unsigned char> &finalBlockID) { finalBlockID_ = finalBlockID; } |
| 120 | void setFinalBlockID(const unsigned char *finalBlockID, unsigned int finalBlockIdLength) |
| 121 | { |
| 122 | setVector(finalBlockID_, finalBlockID, finalBlockIdLength); |
| 123 | } |
| 124 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 125 | void setKeyLocator(const KeyLocator &keyLocator) { keyLocator_ = keyLocator; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 126 | |
| 127 | private: |
| 128 | PublisherPublicKeyDigest publisherPublicKeyDigest_; |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 129 | double timestampMilliseconds_; /**< milliseconds since 1/1/1970. -1 for none */ |
| 130 | int type_; /**< default is ndn_ContentType_DATA. -1 for none */ |
| 131 | int freshnessSeconds_; /**< -1 for none */ |
| 132 | std::vector<unsigned char> finalBlockID_; /** size 0 for none */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 133 | KeyLocator keyLocator_; |
| 134 | }; |
| 135 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 136 | class Data { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 137 | public: |
Jeff Thompson | f5dbd27 | 2013-08-08 16:49:55 -0700 | [diff] [blame] | 138 | Data() |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | Data(const Name &name) |
| 143 | : name_(name) |
| 144 | { |
| 145 | } |
| 146 | |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 147 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode(WireFormat &wireFormat) const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 148 | { |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 149 | return wireFormat.encodeData(*this); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 150 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 151 | ptr_lib::shared_ptr<std::vector<unsigned char> > wireEncode() const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 152 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 153 | return wireEncode(*WireFormat::getDefaultWireFormat()); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 154 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 155 | void wireDecode(const unsigned char *input, unsigned int inputLength, WireFormat &wireFormat) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 156 | { |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 157 | wireFormat.decodeData(*this, input, inputLength); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 158 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 159 | void wireDecode(const unsigned char *input, unsigned int inputLength) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 160 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 161 | wireDecode(input, inputLength, *WireFormat::getDefaultWireFormat()); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 162 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 163 | void wireDecode(const std::vector<unsigned char> &input, WireFormat &wireFormat) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 164 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 165 | wireDecode(&input[0], input.size(), wireFormat); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 166 | } |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 167 | void wireDecode(const std::vector<unsigned char> &input) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 168 | { |
Jeff Thompson | 67e9e0a | 2013-08-02 19:16:19 -0700 | [diff] [blame] | 169 | wireDecode(&input[0], input.size()); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 173 | * Set the dataStruct to point to the values in this interest, without copying any memory. |
| 174 | * WARNING: The resulting pointers in dataStruct are invalid after a further use of this object which could reallocate memory. |
| 175 | * @param dataStruct a C ndn_Data struct where the name components array is already allocated. |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 176 | */ |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 177 | void get(struct ndn_Data &dataStruct) const; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 180 | * Clear this data object, and set the values by copying from the ndn_Data struct. |
| 181 | * @param dataStruct a C ndn_Data struct |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 182 | */ |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 183 | void set(const struct ndn_Data &dataStruct); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 184 | |
| 185 | const Signature &getSignature() const { return signature_; } |
Jeff Thompson | 237fbc0 | 2013-08-08 20:33:21 -0700 | [diff] [blame] | 186 | Signature &getSignature() { return signature_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 187 | |
| 188 | const Name &getName() const { return name_; } |
Jeff Thompson | 237fbc0 | 2013-08-08 20:33:21 -0700 | [diff] [blame] | 189 | Name &getName() { return name_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 190 | |
| 191 | const SignedInfo &getSignedInfo() const { return signedInfo_; } |
Jeff Thompson | 237fbc0 | 2013-08-08 20:33:21 -0700 | [diff] [blame] | 192 | SignedInfo &getSignedInfo() { return signedInfo_; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 193 | |
| 194 | const std::vector<unsigned char> getContent() const { return content_; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 195 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 196 | void setSignature(const Signature &signature) { signature_ = signature; } |
| 197 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 198 | void setName(const Name &name) { name_ = name; } |
| 199 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 200 | void setSignedInfo(const SignedInfo &signedInfo) { signedInfo_ = signedInfo; } |
| 201 | |
| 202 | void setContent(const std::vector<unsigned char> &content) { content_ = content; } |
| 203 | void setContent(const unsigned char *content, unsigned int contentLength) |
| 204 | { |
| 205 | setVector(content_, content, contentLength); |
| 206 | } |
Jeff Thompson | ac1b0ac | 2013-08-08 20:09:39 -0700 | [diff] [blame] | 207 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 208 | private: |
| 209 | Signature signature_; |
| 210 | Name name_; |
| 211 | SignedInfo signedInfo_; |
| 212 | std::vector<unsigned char> content_; |
| 213 | }; |
| 214 | |
| 215 | } |
| 216 | |
| 217 | #endif |