blob: 31354d4d1c92cb8c4dcca8f0d4534d76c1d7c2ad [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
Jeff Thompson53412192013-08-06 13:35:50 -07007#include "binary-xml-encoder.h"
8#include "binary-xml-decoder.h"
9#include "binary-xml-name.h"
10#include "binary-xml-publisher-public-key-digest.h"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070011#include "binary-xml-data.h"
Jeff Thompson4069ce92013-07-10 19:41:55 -070012
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 Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -070030 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070031 return error;
32
Jeff Thompsonadaf9232013-08-08 14:30:29 -070033 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070034}
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 Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -070053 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070054 return error;
55
Jeff Thompsonadaf9232013-08-08 14:30:29 -070056 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070057}
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)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070062 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070063
64 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -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 Thompson94ddc272013-08-08 14:17:38 -070069 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070070 return error;
71
Jeff Thompson94ddc272013-08-08 14:17:38 -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
Jeff Thompson7ed3e272013-08-16 19:15:30 -070076 if (!(signedInfo->type < 0 || signedInfo->type == ndn_ContentType_DATA)) {
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070077 // Not the default of DATA, so we need to encode the type.
Jeff Thompson7ed3e272013-08-16 19:15:30 -070078 unsigned char *typeBytes;
79 unsigned int typeBytesLength = 3;
80 if (signedInfo->type == ndn_ContentType_ENCR)
81 typeBytes = "\x10\xD0\x91";
82 else if (signedInfo->type == ndn_ContentType_GONE)
83 typeBytes = "\x18\xE3\x44";
84 else if (signedInfo->type == ndn_ContentType_KEY)
85 typeBytes = "\x28\x46\x3F";
86 else if (signedInfo->type == ndn_ContentType_LINK)
87 typeBytes = "\x2C\x83\x4A";
88 else if (signedInfo->type == ndn_ContentType_NACK)
89 typeBytes = "\x34\x00\x8A";
90 else
91 return NDN_ERROR_unrecognized_ndn_ContentType;
92
93 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
94 (encoder, ndn_BinaryXml_DTag_Type, typeBytes, typeBytesLength)))
95 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070096 }
97
Jeff Thompson94ddc272013-08-08 14:17:38 -070098 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
99 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, signedInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700100 return error;
101
Jeff Thompson94ddc272013-08-08 14:17:38 -0700102 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
103 (encoder, ndn_BinaryXml_DTag_FinalBlockID, signedInfo->finalBlockID, signedInfo->finalBlockIDLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700104 return error;
105
106 // This will skip encoding if there is no key locator.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700107 if ((error = ndn_encodeBinaryXmlKeyLocator(&signedInfo->keyLocator, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700108 return error;
109
Jeff Thompson94ddc272013-08-08 14:17:38 -0700110 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700111 return error;
112
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700113 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700114}
115
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700116static ndn_Error decodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700117{
118 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700119 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700120 return error;
121
Jeff Thompson94ddc272013-08-08 14:17:38 -0700122 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700123 return error;
124
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700125 if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
126 (decoder, ndn_BinaryXml_DTag_Timestamp, &signedInfo->timestampMilliseconds))
Jeff Thompson210b92f2013-07-11 15:16:03 -0700127 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700128
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700129 unsigned char *typeBytes;
130 unsigned int typeBytesLength;
131 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
132 (decoder, ndn_BinaryXml_DTag_Type, 0, &typeBytes, &typeBytesLength)))
133 return error;
134 if (typeBytesLength == 0)
135 // The default Type is DATA.
136 signedInfo->type = ndn_ContentType_DATA;
137 else if (typeBytesLength == 3) {
138 // All the recognized content types are 3 bytes.
139 if (ndn_memcmp(typeBytes, "\x0C\x04\xC0", typeBytesLength) == 0)
140 signedInfo->type = ndn_ContentType_DATA;
141 else if (ndn_memcmp(typeBytes, "\x10\xD0\x91", typeBytesLength) == 0)
142 signedInfo->type = ndn_ContentType_ENCR;
143 else if (ndn_memcmp(typeBytes, "\x18\xE3\x44", typeBytesLength) == 0)
144 signedInfo->type = ndn_ContentType_GONE;
145 else if (ndn_memcmp(typeBytes, "\x28\x46\x3F", typeBytesLength) == 0)
146 signedInfo->type = ndn_ContentType_KEY;
147 else if (ndn_memcmp(typeBytes, "\x2C\x83\x4A", typeBytesLength) == 0)
148 signedInfo->type = ndn_ContentType_LINK;
149 else if (ndn_memcmp(typeBytes, "\x34\x00\x8A", typeBytesLength) == 0)
150 signedInfo->type = ndn_ContentType_NACK;
151 else
152 return NDN_ERROR_unrecognized_ndn_ContentType;
153 }
154 else
155 return NDN_ERROR_unrecognized_ndn_ContentType;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700156
Jeff Thompson94ddc272013-08-08 14:17:38 -0700157 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
158 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &signedInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700159 return error;
160
Jeff Thompson94ddc272013-08-08 14:17:38 -0700161 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
162 (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &signedInfo->finalBlockID, &signedInfo->finalBlockIDLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700163 return error;
164
Jeff Thompson94ddc272013-08-08 14:17:38 -0700165 if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&signedInfo->keyLocator, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700166 return error;
167
Jeff Thompson94ddc272013-08-08 14:17:38 -0700168 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700169 return error;
170
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700171 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700172}
173
Jeff Thompsonaa020712013-08-08 21:20:06 -0700174ndn_Error ndn_encodeBinaryXmlData
175 (struct ndn_Data *data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700176{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700177 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700178 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700179 return error;
180
Jeff Thompson94ddc272013-08-08 14:17:38 -0700181 if ((error = encodeSignature(&data->signature, encoder)))
Jeff Thompsonae8a6f82013-08-16 17:12:49 -0700182 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700183
184 *signedFieldsBeginOffset = encoder->offset;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700185
Jeff Thompson94ddc272013-08-08 14:17:38 -0700186 if ((error = ndn_encodeBinaryXmlName(&data->name, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700187 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700188
Jeff Thompson94ddc272013-08-08 14:17:38 -0700189 if ((error = encodeSignedInfo(&data->signedInfo, encoder)))
Jeff Thompsonae8a6f82013-08-16 17:12:49 -0700190 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700191
Jeff Thompson94ddc272013-08-08 14:17:38 -0700192 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
193 (encoder, ndn_BinaryXml_DTag_Content, data->content, data->contentLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700194 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700195
196 *signedFieldsEndOffset = encoder->offset;
197
Jeff Thompson94ddc272013-08-08 14:17:38 -0700198 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700199 return error;
200
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700201 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700202}
203
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700204ndn_Error ndn_decodeBinaryXmlData
205 (struct ndn_Data *data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700206{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700207 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700208 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700209 return error;
210
211 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700212 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700213 return error;
214 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700215 if ((error = decodeSignature(&data->signature, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700216 return error;
217 }
218 else
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700219 ndn_Signature_initialize(&data->signature);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700220
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700221 *signedFieldsBeginOffset = decoder->offset;
222
Jeff Thompson94ddc272013-08-08 14:17:38 -0700223 if ((error = ndn_decodeBinaryXmlName(&data->name, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700224 return error;
225
Jeff Thompson94ddc272013-08-08 14:17:38 -0700226 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700227 return error;
228 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700229 if ((error = decodeSignedInfo(&data->signedInfo, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700230 return error;
231 }
232 else
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700233 ndn_SignedInfo_initialize(&data->signedInfo, data->signedInfo.keyLocator.keyName.components, data->signedInfo.keyLocator.keyName.maxComponents);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700234
235 // Require a Content element, but set allowNull to allow a missing BLOB.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700236 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
237 (decoder, ndn_BinaryXml_DTag_Content, 1, &data->content, &data->contentLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700238 return error;
239
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700240 *signedFieldsEndOffset = decoder->offset;
241
Jeff Thompson94ddc272013-08-08 14:17:38 -0700242 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700243 return error;
244
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700245 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700246}