blob: d8013cf4002ef938c15811dd6e22964a1f7ab38e [file] [log] [blame]
Jeff Thompson4069ce92013-07-10 19:41:55 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson4069ce92013-07-10 19:41:55 -07004 * Derived from ContentObject.js by Meki Cheraoui.
5 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson53412192013-08-06 13:35:50 -07008#include "binary-xml-encoder.h"
9#include "binary-xml-decoder.h"
10#include "binary-xml-name.h"
11#include "binary-xml-publisher-public-key-digest.h"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070012#include "binary-xml-data.h"
Jeff Thompsone2cfa352013-11-20 15:13:40 -080013#include "binary-xml-key.h"
Jeff Thompson4069ce92013-07-10 19:41:55 -070014
Jeff Thompsonf0fea002013-07-30 17:22:42 -070015static ndn_Error encodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070016{
17 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070018 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Signature)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070019 return error;
20
21 // TODO: Check if digestAlgorithm is the same as the default, and skip it, otherwise encode it as UDATA.
22
Jeff Thompson93034532013-10-08 11:52:43 -070023 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement(encoder, ndn_BinaryXml_DTag_Witness, &signature->witness)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070024 return error;
25
26 // Require a signature.
Jeff Thompson93034532013-10-08 11:52:43 -070027 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_SignatureBits, &signature->signature)))
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
Jeff Thompson93034532013-10-08 11:52:43 -070042 /* TODO: digestAlgorithm as UDATA */ signature->digestAlgorithm.value = 0; signature->digestAlgorithm.length = 0;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070043
Jeff Thompson94ddc272013-08-08 14:17:38 -070044 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070045 (decoder, ndn_BinaryXml_DTag_Witness, 0, &signature->witness)))
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
Jeff Thompson93034532013-10-08 11:52:43 -070050 (decoder, ndn_BinaryXml_DTag_SignatureBits, 0, &signature->signature)))
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 Thompson62f6e7a2013-11-20 15:20:18 -080061 if ((int)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 Thompson62f6e7a2013-11-20 15:20:18 -080076 if (!((int)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 Thompson93034532013-10-08 11:52:43 -070078 struct ndn_Blob typeBytes;
79 typeBytes.length = 3;
Jeff Thompsonfec716d2013-09-11 13:54:36 -070080 if (metaInfo->type == ndn_ContentType_ENCR)
Jeff Thompsone2cfa352013-11-20 15:13:40 -080081 typeBytes.value = (uint8_t *)"\x10\xD0\x91";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070082 else if (metaInfo->type == ndn_ContentType_GONE)
Jeff Thompsone2cfa352013-11-20 15:13:40 -080083 typeBytes.value = (uint8_t *)"\x18\xE3\x44";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070084 else if (metaInfo->type == ndn_ContentType_KEY)
Jeff Thompsone2cfa352013-11-20 15:13:40 -080085 typeBytes.value = (uint8_t *)"\x28\x46\x3F";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070086 else if (metaInfo->type == ndn_ContentType_LINK)
Jeff Thompsone2cfa352013-11-20 15:13:40 -080087 typeBytes.value = (uint8_t *)"\x2C\x83\x4A";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070088 else if (metaInfo->type == ndn_ContentType_NACK)
Jeff Thompsone2cfa352013-11-20 15:13:40 -080089 typeBytes.value = (uint8_t *)"\x34\x00\x8A";
Jeff Thompson7ed3e272013-08-16 19:15:30 -070090 else
91 return NDN_ERROR_unrecognized_ndn_ContentType;
92
Jeff Thompson93034532013-10-08 11:52:43 -070093 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Type, &typeBytes)))
Jeff Thompson7ed3e272013-08-16 19:15:30 -070094 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070095 }
96
Jeff Thompson94ddc272013-08-08 14:17:38 -070097 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -070098 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, metaInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070099 return error;
100
Jeff Thompson94ddc272013-08-08 14:17:38 -0700101 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700102 (encoder, ndn_BinaryXml_DTag_FinalBlockID, &metaInfo->finalBlockID.value)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700103 return error;
104
105 // This will skip encoding if there is no key locator.
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700106 if ((error = ndn_encodeBinaryXmlKeyLocator(&signature->keyLocator, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700107 return error;
108
Jeff Thompson94ddc272013-08-08 14:17:38 -0700109 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700110 return error;
111
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700112 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700113}
114
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700115static ndn_Error decodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700116{
117 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700118 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700119 return error;
120
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700121 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signature->publisherPublicKeyDigest, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700122 return error;
123
Jeff Thompsone2cfa352013-11-20 15:13:40 -0800124 if ((error = ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
125 (decoder, ndn_BinaryXml_DTag_Timestamp, &metaInfo->timestampMilliseconds)))
Jeff Thompson210b92f2013-07-11 15:16:03 -0700126 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700127
Jeff Thompson93034532013-10-08 11:52:43 -0700128 struct ndn_Blob typeBytes;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700129 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700130 (decoder, ndn_BinaryXml_DTag_Type, 0, &typeBytes)))
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700131 return error;
Jeff Thompson93034532013-10-08 11:52:43 -0700132 if (typeBytes.length == 0)
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700133 // The default Type is DATA.
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700134 metaInfo->type = ndn_ContentType_DATA;
Jeff Thompson93034532013-10-08 11:52:43 -0700135 else if (typeBytes.length == 3) {
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700136 // All the recognized content types are 3 bytes.
Jeff Thompsone2cfa352013-11-20 15:13:40 -0800137 if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x0C\x04\xC0", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700138 metaInfo->type = ndn_ContentType_DATA;
Jeff Thompsone2cfa352013-11-20 15:13:40 -0800139 else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x10\xD0\x91", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700140 metaInfo->type = ndn_ContentType_ENCR;
Jeff Thompsone2cfa352013-11-20 15:13:40 -0800141 else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x18\xE3\x44", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700142 metaInfo->type = ndn_ContentType_GONE;
Jeff Thompsone2cfa352013-11-20 15:13:40 -0800143 else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x28\x46\x3F", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700144 metaInfo->type = ndn_ContentType_KEY;
Jeff Thompsone2cfa352013-11-20 15:13:40 -0800145 else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x2C\x83\x4A", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700146 metaInfo->type = ndn_ContentType_LINK;
Jeff Thompsone2cfa352013-11-20 15:13:40 -0800147 else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x34\x00\x8A", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700148 metaInfo->type = ndn_ContentType_NACK;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700149 else
150 return NDN_ERROR_unrecognized_ndn_ContentType;
151 }
152 else
153 return NDN_ERROR_unrecognized_ndn_ContentType;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700154
Jeff Thompson94ddc272013-08-08 14:17:38 -0700155 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700156 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &metaInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700157 return error;
158
Jeff Thompson94ddc272013-08-08 14:17:38 -0700159 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700160 (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &metaInfo->finalBlockID.value)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700161 return error;
162
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700163 if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&signature->keyLocator, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700164 return error;
165
Jeff Thompson94ddc272013-08-08 14:17:38 -0700166 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700167 return error;
168
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700169 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700170}
171
Jeff Thompsonaa020712013-08-08 21:20:06 -0700172ndn_Error ndn_encodeBinaryXmlData
Jeff Thompson97223af2013-09-24 17:01:27 -0700173 (struct ndn_Data *data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700174{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700175 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700176 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700177 return error;
178
Jeff Thompson94ddc272013-08-08 14:17:38 -0700179 if ((error = encodeSignature(&data->signature, encoder)))
Jeff Thompsonae8a6f82013-08-16 17:12:49 -0700180 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700181
Jeff Thompson9c661702013-09-13 14:35:44 -0700182 *signedPortionBeginOffset = encoder->offset;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700183
Jeff Thompson94ddc272013-08-08 14:17:38 -0700184 if ((error = ndn_encodeBinaryXmlName(&data->name, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700185 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700186
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700187 if ((error = encodeSignedInfo(&data->signature, &data->metaInfo, encoder)))
Jeff Thompsonae8a6f82013-08-16 17:12:49 -0700188 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700189
Jeff Thompson93034532013-10-08 11:52:43 -0700190 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Content, &data->content)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700191 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700192
Jeff Thompson9c661702013-09-13 14:35:44 -0700193 *signedPortionEndOffset = encoder->offset;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700194
Jeff Thompson94ddc272013-08-08 14:17:38 -0700195 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700196 return error;
197
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700198 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700199}
200
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700201ndn_Error ndn_decodeBinaryXmlData
Jeff Thompson97223af2013-09-24 17:01:27 -0700202 (struct ndn_Data *data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700203{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700204 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700205 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700206 return error;
207
208 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700209 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700210 return error;
211 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700212 if ((error = decodeSignature(&data->signature, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700213 return error;
214 }
215 else
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700216 ndn_Signature_initialize(&data->signature, data->signature.keyLocator.keyName.components, data->signature.keyLocator.keyName.maxComponents);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700217
Jeff Thompson9c661702013-09-13 14:35:44 -0700218 *signedPortionBeginOffset = decoder->offset;
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700219
Jeff Thompson94ddc272013-08-08 14:17:38 -0700220 if ((error = ndn_decodeBinaryXmlName(&data->name, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700221 return error;
222
Jeff Thompson94ddc272013-08-08 14:17:38 -0700223 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700224 return error;
225 if (gotExpectedTag) {
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700226 if ((error = decodeSignedInfo(&data->signature, &data->metaInfo, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700227 return error;
228 }
229 else
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700230 ndn_MetaInfo_initialize(&data->metaInfo);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700231
232 // Require a Content element, but set allowNull to allow a missing BLOB.
Jeff Thompson93034532013-10-08 11:52:43 -0700233 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Content, 1, &data->content)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700234 return error;
235
Jeff Thompson9c661702013-09-13 14:35:44 -0700236 *signedPortionEndOffset = decoder->offset;
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700237
Jeff Thompson94ddc272013-08-08 14:17:38 -0700238 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700239 return error;
240
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700241 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700242}