blob: 2925ee6d11c21aa5f53397241d1a9aa2354dd0be [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 Thompsonf4585af2013-09-11 14:56:59 -070059static ndn_Error encodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070060{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070061 if (metaInfo->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 Thompsonf4585af2013-09-11 14:56:59 -070069 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&signature->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
Jeff Thompsonfec716d2013-09-11 13:54:36 -070073 (encoder, ndn_BinaryXml_DTag_Timestamp, metaInfo->timestampMilliseconds)))
Jeff Thompson2bcece32013-07-11 18:10:19 -070074 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070075
Jeff Thompsonfec716d2013-09-11 13:54:36 -070076 if (!(metaInfo->type < 0 || metaInfo->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;
Jeff Thompsonfec716d2013-09-11 13:54:36 -070080 if (metaInfo->type == ndn_ContentType_ENCR)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070081 typeBytes = "\x10\xD0\x91";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070082 else if (metaInfo->type == ndn_ContentType_GONE)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070083 typeBytes = "\x18\xE3\x44";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070084 else if (metaInfo->type == ndn_ContentType_KEY)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070085 typeBytes = "\x28\x46\x3F";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070086 else if (metaInfo->type == ndn_ContentType_LINK)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070087 typeBytes = "\x2C\x83\x4A";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070088 else if (metaInfo->type == ndn_ContentType_NACK)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070089 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
Jeff Thompsonfec716d2013-09-11 13:54:36 -070099 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, metaInfo->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
Jeff Thompson145e2252013-09-12 12:51:35 -0700103 (encoder, ndn_BinaryXml_DTag_FinalBlockID, metaInfo->finalBlockID.value, metaInfo->finalBlockID.valueLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700104 return error;
105
106 // This will skip encoding if there is no key locator.
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700107 if ((error = ndn_encodeBinaryXmlKeyLocator(&signature->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 Thompsonf4585af2013-09-11 14:56:59 -0700116static ndn_Error decodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, 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 Thompsonf4585af2013-09-11 14:56:59 -0700122 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signature->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
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700126 (decoder, ndn_BinaryXml_DTag_Timestamp, &metaInfo->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.
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700136 metaInfo->type = ndn_ContentType_DATA;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700137 else if (typeBytesLength == 3) {
138 // All the recognized content types are 3 bytes.
139 if (ndn_memcmp(typeBytes, "\x0C\x04\xC0", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700140 metaInfo->type = ndn_ContentType_DATA;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700141 else if (ndn_memcmp(typeBytes, "\x10\xD0\x91", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700142 metaInfo->type = ndn_ContentType_ENCR;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700143 else if (ndn_memcmp(typeBytes, "\x18\xE3\x44", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700144 metaInfo->type = ndn_ContentType_GONE;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700145 else if (ndn_memcmp(typeBytes, "\x28\x46\x3F", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700146 metaInfo->type = ndn_ContentType_KEY;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700147 else if (ndn_memcmp(typeBytes, "\x2C\x83\x4A", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700148 metaInfo->type = ndn_ContentType_LINK;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700149 else if (ndn_memcmp(typeBytes, "\x34\x00\x8A", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700150 metaInfo->type = ndn_ContentType_NACK;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700151 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
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700158 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &metaInfo->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
Jeff Thompson145e2252013-09-12 12:51:35 -0700162 (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &metaInfo->finalBlockID.value, &metaInfo->finalBlockID.valueLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700163 return error;
164
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700165 if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&signature->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 Thompsonf4585af2013-09-11 14:56:59 -0700189 if ((error = encodeSignedInfo(&data->signature, &data->metaInfo, 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 Thompsonf4585af2013-09-11 14:56:59 -0700219 ndn_Signature_initialize(&data->signature, data->signature.keyLocator.keyName.components, data->signature.keyLocator.keyName.maxComponents);
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 Thompsonf4585af2013-09-11 14:56:59 -0700229 if ((error = decodeSignedInfo(&data->signature, &data->metaInfo, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700230 return error;
231 }
232 else
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700233 ndn_MetaInfo_initialize(&data->metaInfo);
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}