blob: 29b20656c87aae7bdb446009100794a716c27ce1 [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
72 // TODO: Implement timeStamp
73
74 if (signedInfo->type != ndn_ContentType_DATA) {
75 // Not the default of DATA, so we need to encode the type.
76 // TODO: Implement converting the type from an int and encoding.
77 }
78
79 if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
80 (encoder, ndn_BinaryXML_DTag_FreshnessSeconds, signedInfo->freshnessSeconds))
81 return error;
82
83 if (error = ndn_BinaryXMLEncoder_writeOptionalBlobDTagElement
84 (encoder, ndn_BinaryXML_DTag_FinalBlockID, signedInfo->finalBlockID, signedInfo->finalBlockIDLength))
85 return error;
86
87 // This will skip encoding if there is no key locator.
88 if (error = ndn_encodeBinaryXMLKeyLocator(&signedInfo->keyLocator, encoder))
89 return error;
90
91 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
92 return error;
93
94 return 0;
95}
96
97static ndn_Error decodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXMLDecoder *decoder)
98{
99 ndn_Error error;
100 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_SignedInfo))
101 return error;
102
103 if (error = ndn_decodeOptionalBinaryXMLPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, decoder))
104 return error;
105
Jeff Thompson210b92f2013-07-11 15:16:03 -0700106 if (error= ndn_BinaryXMLDecoder_readOptionalTimeMillisecondsDTagElement
107 (decoder, ndn_BinaryXML_DTag_Timestamp, &signedInfo->timestampMilliseconds))
108 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700109
110 // TODO: Implement reading the type and converting to an int.
111 signedInfo->type = ndn_ContentType_DATA;
112
113 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
114 (decoder, ndn_BinaryXML_DTag_FreshnessSeconds, &signedInfo->freshnessSeconds))
115 return error;
116
117 if (error = ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
118 (decoder, ndn_BinaryXML_DTag_FinalBlockID, 0, &signedInfo->finalBlockID, &signedInfo->finalBlockIDLength))
119 return error;
120
121 if (error = ndn_decodeOptionalBinaryXMLKeyLocator(&signedInfo->keyLocator, decoder))
122 return error;
123
124 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
125 return error;
126
127 return 0;
128}
129
Jeff Thompson4069ce92013-07-10 19:41:55 -0700130ndn_Error ndn_encodeBinaryXMLContentObject(struct ndn_ContentObject *contentObject, struct ndn_BinaryXMLEncoder *encoder)
131{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700132 ndn_Error error;
133 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_ContentObject))
134 return error;
135
136 if (error = encodeSignature(&contentObject->signature, encoder))
137 return 0;
138
139 if (error = ndn_encodeBinaryXMLName(&contentObject->name, encoder))
140 return error;
141
142 if (error = encodeSignedInfo(&contentObject->signedInfo, encoder))
143 return 0;
144
145 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
146 (encoder, ndn_BinaryXML_DTag_Content, contentObject->content, contentObject->contentLength))
147 return error;
148
149 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
150 return error;
151
Jeff Thompson4069ce92013-07-10 19:41:55 -0700152 return 0;
153}
154
155ndn_Error ndn_decodeBinaryXMLContentObject(struct ndn_ContentObject *contentObject, struct ndn_BinaryXMLDecoder *decoder)
156{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700157 ndn_Error error;
158 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_ContentObject))
159 return error;
160
161 int gotExpectedTag;
162 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Signature, &gotExpectedTag))
163 return error;
164 if (gotExpectedTag) {
165 if (error = decodeSignature(&contentObject->signature, decoder))
166 return error;
167 }
168 else
169 ndn_Signature_init(&contentObject->signature);
170
171 if (error = ndn_decodeBinaryXMLName(&contentObject->name, decoder))
172 return error;
173
174 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_SignedInfo, &gotExpectedTag))
175 return error;
176 if (gotExpectedTag) {
177 if (error = decodeSignedInfo(&contentObject->signedInfo, decoder))
178 return error;
179 }
180 else
181 ndn_SignedInfo_init(&contentObject->signedInfo);
182
183 // Require a Content element, but set allowNull to allow a missing BLOB.
184 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
185 (decoder, ndn_BinaryXML_DTag_Content, 1, &contentObject->content, &contentObject->contentLength))
186 return error;
187
188 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
189 return error;
190
Jeff Thompson4069ce92013-07-10 19:41:55 -0700191 return 0;
192}