blob: ef5053298cd3ef2e814870bff8f704eec3c38c68 [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
106 // TODO: Implement timeStamp
107
108 // TODO: Implement reading the type and converting to an int.
109 signedInfo->type = ndn_ContentType_DATA;
110
111 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
112 (decoder, ndn_BinaryXML_DTag_FreshnessSeconds, &signedInfo->freshnessSeconds))
113 return error;
114
115 if (error = ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
116 (decoder, ndn_BinaryXML_DTag_FinalBlockID, 0, &signedInfo->finalBlockID, &signedInfo->finalBlockIDLength))
117 return error;
118
119 if (error = ndn_decodeOptionalBinaryXMLKeyLocator(&signedInfo->keyLocator, decoder))
120 return error;
121
122 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
123 return error;
124
125 return 0;
126}
127
Jeff Thompson4069ce92013-07-10 19:41:55 -0700128ndn_Error ndn_encodeBinaryXMLContentObject(struct ndn_ContentObject *contentObject, struct ndn_BinaryXMLEncoder *encoder)
129{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700130 ndn_Error error;
131 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_ContentObject))
132 return error;
133
134 if (error = encodeSignature(&contentObject->signature, encoder))
135 return 0;
136
137 if (error = ndn_encodeBinaryXMLName(&contentObject->name, encoder))
138 return error;
139
140 if (error = encodeSignedInfo(&contentObject->signedInfo, encoder))
141 return 0;
142
143 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
144 (encoder, ndn_BinaryXML_DTag_Content, contentObject->content, contentObject->contentLength))
145 return error;
146
147 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
148 return error;
149
Jeff Thompson4069ce92013-07-10 19:41:55 -0700150 return 0;
151}
152
153ndn_Error ndn_decodeBinaryXMLContentObject(struct ndn_ContentObject *contentObject, struct ndn_BinaryXMLDecoder *decoder)
154{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700155 ndn_Error error;
156 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_ContentObject))
157 return error;
158
159 int gotExpectedTag;
160 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Signature, &gotExpectedTag))
161 return error;
162 if (gotExpectedTag) {
163 if (error = decodeSignature(&contentObject->signature, decoder))
164 return error;
165 }
166 else
167 ndn_Signature_init(&contentObject->signature);
168
169 if (error = ndn_decodeBinaryXMLName(&contentObject->name, decoder))
170 return error;
171
172 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_SignedInfo, &gotExpectedTag))
173 return error;
174 if (gotExpectedTag) {
175 if (error = decodeSignedInfo(&contentObject->signedInfo, decoder))
176 return error;
177 }
178 else
179 ndn_SignedInfo_init(&contentObject->signedInfo);
180
181 // Require a Content element, but set allowNull to allow a missing BLOB.
182 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
183 (decoder, ndn_BinaryXML_DTag_Content, 1, &contentObject->content, &contentObject->contentLength))
184 return error;
185
186 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
187 return error;
188
Jeff Thompson4069ce92013-07-10 19:41:55 -0700189 return 0;
190}