blob: c9104fff9098aae5571042cc7065f843334ec59e [file] [log] [blame]
Jeff Thompson4069ce92013-07-10 19:41:55 -07001/**
2 * @author: Jeff Thompson
3 * Derived from ContentObject.js by Meki Cheraoui.
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "BinaryXMLEncoder.h"
8#include "BinaryXMLDecoder.h"
9#include "BinaryXMLName.h"
10#include "BinaryXMLPublisherPublicKeyDigest.h"
11#include "BinaryXMLContentObject.h"
12
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070013static ndn_Error encodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXMLEncoder *encoder)
14{
15 ndn_Error error;
16 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Signature))
17 return error;
18
19 // TODO: Check if digestAlgorithm is the same as the default, and skip it, otherwise encode it as UDATA.
20
21 if (error = ndn_BinaryXMLEncoder_writeOptionalBlobDTagElement
22 (encoder, ndn_BinaryXML_DTag_Witness, signature->witness, signature->witnessLength))
23 return error;
24
25 // Require a signature.
26 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
27 (encoder, ndn_BinaryXML_DTag_SignatureBits, signature->signature, signature->signatureLength))
28 return error;
29
30 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
31 return error;
32
33 return 0;
34}
35
36static ndn_Error decodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXMLDecoder *decoder)
37{
38 ndn_Error error;
39 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Signature))
40 return error;
41
42 /* TODO: digestAlgorithm as UDATA */ signature->digestAlgorithm = 0; signature->digestAlgorithmLength = 0;
43
44 if (error = ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
45 (decoder, ndn_BinaryXML_DTag_Witness, 0, &signature->witness, &signature->witnessLength))
46 return error;
47
48 // Require a signature.
49 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
50 (decoder, ndn_BinaryXML_DTag_SignatureBits, 0, &signature->signature, &signature->signatureLength))
51 return error;
52
53 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
54 return error;
55
56 return 0;
57}
58
59static ndn_Error encodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXMLEncoder *encoder)
60{
61 if (signedInfo->type < 0)
62 return;
63
64 ndn_Error error;
65 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_SignedInfo))
66 return error;
67
68 // This will skip encoding if there is no publisherPublicKeyDigest.
69 if (error = ndn_encodeBinaryXMLPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, encoder))
70 return error;
71
Jeff Thompson2bcece32013-07-11 18:10:19 -070072 if (error = ndn_BinaryXMLEncoder_writeOptionalTimeMillisecondsDTagElement
73 (encoder, ndn_BinaryXML_DTag_Timestamp, signedInfo->timestampMilliseconds))
74 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070075
76 if (signedInfo->type != ndn_ContentType_DATA) {
77 // Not the default of DATA, so we need to encode the type.
78 // TODO: Implement converting the type from an int and encoding.
79 }
80
81 if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
82 (encoder, ndn_BinaryXML_DTag_FreshnessSeconds, signedInfo->freshnessSeconds))
83 return error;
84
85 if (error = ndn_BinaryXMLEncoder_writeOptionalBlobDTagElement
86 (encoder, ndn_BinaryXML_DTag_FinalBlockID, signedInfo->finalBlockID, signedInfo->finalBlockIDLength))
87 return error;
88
89 // This will skip encoding if there is no key locator.
90 if (error = ndn_encodeBinaryXMLKeyLocator(&signedInfo->keyLocator, encoder))
91 return error;
92
93 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
94 return error;
95
96 return 0;
97}
98
99static ndn_Error decodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXMLDecoder *decoder)
100{
101 ndn_Error error;
102 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_SignedInfo))
103 return error;
104
105 if (error = ndn_decodeOptionalBinaryXMLPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, decoder))
106 return error;
107
Jeff Thompson210b92f2013-07-11 15:16:03 -0700108 if (error= ndn_BinaryXMLDecoder_readOptionalTimeMillisecondsDTagElement
109 (decoder, ndn_BinaryXML_DTag_Timestamp, &signedInfo->timestampMilliseconds))
110 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700111
112 // TODO: Implement reading the type and converting to an int.
113 signedInfo->type = ndn_ContentType_DATA;
114
115 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
116 (decoder, ndn_BinaryXML_DTag_FreshnessSeconds, &signedInfo->freshnessSeconds))
117 return error;
118
119 if (error = ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
120 (decoder, ndn_BinaryXML_DTag_FinalBlockID, 0, &signedInfo->finalBlockID, &signedInfo->finalBlockIDLength))
121 return error;
122
123 if (error = ndn_decodeOptionalBinaryXMLKeyLocator(&signedInfo->keyLocator, decoder))
124 return error;
125
126 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
127 return error;
128
129 return 0;
130}
131
Jeff Thompson4069ce92013-07-10 19:41:55 -0700132ndn_Error ndn_encodeBinaryXMLContentObject(struct ndn_ContentObject *contentObject, struct ndn_BinaryXMLEncoder *encoder)
133{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700134 ndn_Error error;
135 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_ContentObject))
136 return error;
137
138 if (error = encodeSignature(&contentObject->signature, encoder))
139 return 0;
140
141 if (error = ndn_encodeBinaryXMLName(&contentObject->name, encoder))
142 return error;
143
144 if (error = encodeSignedInfo(&contentObject->signedInfo, encoder))
145 return 0;
146
147 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
148 (encoder, ndn_BinaryXML_DTag_Content, contentObject->content, contentObject->contentLength))
149 return error;
150
151 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
152 return error;
153
Jeff Thompson4069ce92013-07-10 19:41:55 -0700154 return 0;
155}
156
157ndn_Error ndn_decodeBinaryXMLContentObject(struct ndn_ContentObject *contentObject, struct ndn_BinaryXMLDecoder *decoder)
158{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700159 ndn_Error error;
160 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_ContentObject))
161 return error;
162
163 int gotExpectedTag;
164 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Signature, &gotExpectedTag))
165 return error;
166 if (gotExpectedTag) {
167 if (error = decodeSignature(&contentObject->signature, decoder))
168 return error;
169 }
170 else
171 ndn_Signature_init(&contentObject->signature);
172
173 if (error = ndn_decodeBinaryXMLName(&contentObject->name, decoder))
174 return error;
175
176 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_SignedInfo, &gotExpectedTag))
177 return error;
178 if (gotExpectedTag) {
179 if (error = decodeSignedInfo(&contentObject->signedInfo, decoder))
180 return error;
181 }
182 else
183 ndn_SignedInfo_init(&contentObject->signedInfo);
184
185 // Require a Content element, but set allowNull to allow a missing BLOB.
186 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
187 (decoder, ndn_BinaryXML_DTag_Content, 1, &contentObject->content, &contentObject->contentLength))
188 return error;
189
190 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
191 return error;
192
Jeff Thompson4069ce92013-07-10 19:41:55 -0700193 return 0;
194}