blob: d95889cf380dc1c74afe522bc54da126da64e337 [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 Thompsonf0fea002013-07-30 17:22:42 -070013static ndn_Error encodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070014{
15 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070016 if (error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Signature))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070017 return error;
18
19 // TODO: Check if digestAlgorithm is the same as the default, and skip it, otherwise encode it as UDATA.
20
Jeff Thompsonf0fea002013-07-30 17:22:42 -070021 if (error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
22 (encoder, ndn_BinaryXml_DTag_Witness, signature->witness, signature->witnessLength))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070023 return error;
24
25 // Require a signature.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070026 if (error = ndn_BinaryXmlEncoder_writeBlobDTagElement
27 (encoder, ndn_BinaryXml_DTag_SignatureBits, signature->signature, signature->signatureLength))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070028 return error;
29
Jeff Thompsonf0fea002013-07-30 17:22:42 -070030 if (error = ndn_BinaryXmlEncoder_writeElementClose(encoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070031 return error;
32
33 return 0;
34}
35
Jeff Thompsonf0fea002013-07-30 17:22:42 -070036static ndn_Error decodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070037{
38 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070039 if (error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Signature))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070040 return error;
41
42 /* TODO: digestAlgorithm as UDATA */ signature->digestAlgorithm = 0; signature->digestAlgorithmLength = 0;
43
Jeff Thompsonf0fea002013-07-30 17:22:42 -070044 if (error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
45 (decoder, ndn_BinaryXml_DTag_Witness, 0, &signature->witness, &signature->witnessLength))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070046 return error;
47
48 // Require a signature.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070049 if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement
50 (decoder, ndn_BinaryXml_DTag_SignatureBits, 0, &signature->signature, &signature->signatureLength))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070051 return error;
52
Jeff Thompsonf0fea002013-07-30 17:22:42 -070053 if (error = ndn_BinaryXmlDecoder_readElementClose(decoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070054 return error;
55
56 return 0;
57}
58
Jeff Thompsonf0fea002013-07-30 17:22:42 -070059static ndn_Error encodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070060{
61 if (signedInfo->type < 0)
62 return;
63
64 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -070065 if (error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_SignedInfo))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070066 return error;
67
68 // This will skip encoding if there is no publisherPublicKeyDigest.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070069 if (error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, encoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070070 return error;
71
Jeff Thompsonf0fea002013-07-30 17:22:42 -070072 if (error = ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
73 (encoder, ndn_BinaryXml_DTag_Timestamp, signedInfo->timestampMilliseconds))
Jeff Thompson2bcece32013-07-11 18:10:19 -070074 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
Jeff Thompsonf0fea002013-07-30 17:22:42 -070081 if (error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
82 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, signedInfo->freshnessSeconds))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070083 return error;
84
Jeff Thompsonf0fea002013-07-30 17:22:42 -070085 if (error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
86 (encoder, ndn_BinaryXml_DTag_FinalBlockID, signedInfo->finalBlockID, signedInfo->finalBlockIDLength))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070087 return error;
88
89 // This will skip encoding if there is no key locator.
Jeff Thompsonf0fea002013-07-30 17:22:42 -070090 if (error = ndn_encodeBinaryXmlKeyLocator(&signedInfo->keyLocator, encoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070091 return error;
92
Jeff Thompsonf0fea002013-07-30 17:22:42 -070093 if (error = ndn_BinaryXmlEncoder_writeElementClose(encoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070094 return error;
95
96 return 0;
97}
98
Jeff Thompsonf0fea002013-07-30 17:22:42 -070099static ndn_Error decodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700100{
101 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700102 if (error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700103 return error;
104
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700105 if (error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, decoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700106 return error;
107
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700108 if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
109 (decoder, ndn_BinaryXml_DTag_Timestamp, &signedInfo->timestampMilliseconds))
Jeff Thompson210b92f2013-07-11 15:16:03 -0700110 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
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700115 if (error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
116 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &signedInfo->freshnessSeconds))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700117 return error;
118
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700119 if (error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
120 (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &signedInfo->finalBlockID, &signedInfo->finalBlockIDLength))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700121 return error;
122
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700123 if (error = ndn_decodeOptionalBinaryXmlKeyLocator(&signedInfo->keyLocator, decoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700124 return error;
125
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700126 if (error = ndn_BinaryXmlDecoder_readElementClose(decoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700127 return error;
128
129 return 0;
130}
131
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700132ndn_Error ndn_encodeBinaryXmlContentObject(struct ndn_ContentObject *contentObject, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700133{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700134 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700135 if (error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700136 return error;
137
138 if (error = encodeSignature(&contentObject->signature, encoder))
139 return 0;
140
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700141 if (error = ndn_encodeBinaryXmlName(&contentObject->name, encoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700142 return error;
143
144 if (error = encodeSignedInfo(&contentObject->signedInfo, encoder))
145 return 0;
146
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700147 if (error = ndn_BinaryXmlEncoder_writeBlobDTagElement
148 (encoder, ndn_BinaryXml_DTag_Content, contentObject->content, contentObject->contentLength))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700149 return error;
150
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700151 if (error = ndn_BinaryXmlEncoder_writeElementClose(encoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700152 return error;
153
Jeff Thompson4069ce92013-07-10 19:41:55 -0700154 return 0;
155}
156
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700157ndn_Error ndn_decodeBinaryXmlContentObject(struct ndn_ContentObject *contentObject, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700158{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700159 ndn_Error error;
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700160 if (error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700161 return error;
162
163 int gotExpectedTag;
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700164 if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700165 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
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700173 if (error = ndn_decodeBinaryXmlName(&contentObject->name, decoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700174 return error;
175
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700176 if (error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700177 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.
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700186 if (error = ndn_BinaryXmlDecoder_readBinaryDTagElement
187 (decoder, ndn_BinaryXml_DTag_Content, 1, &contentObject->content, &contentObject->contentLength))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700188 return error;
189
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700190 if (error = ndn_BinaryXmlDecoder_readElementClose(decoder))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700191 return error;
192
Jeff Thompson4069ce92013-07-10 19:41:55 -0700193 return 0;
194}