blob: de34f9d33734eddd63df49aa3f93a83ea21fdd1b [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 Thompson4069ce92013-07-10 19:41:55 -070013
Jeff Thompsonf0fea002013-07-30 17:22:42 -070014static ndn_Error encodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070015{
16 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070017 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Signature)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070018 return error;
19
20 // TODO: Check if digestAlgorithm is the same as the default, and skip it, otherwise encode it as UDATA.
21
Jeff Thompson94ddc272013-08-08 14:17:38 -070022 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
23 (encoder, ndn_BinaryXml_DTag_Witness, signature->witness, signature->witnessLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070024 return error;
25
26 // Require a signature.
Jeff Thompson94ddc272013-08-08 14:17:38 -070027 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
28 (encoder, ndn_BinaryXml_DTag_SignatureBits, signature->signature, signature->signatureLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070029 return error;
30
Jeff Thompson94ddc272013-08-08 14:17:38 -070031 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070032 return error;
33
Jeff Thompsonadaf9232013-08-08 14:30:29 -070034 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070035}
36
Jeff Thompsonf0fea002013-07-30 17:22:42 -070037static ndn_Error decodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070038{
39 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070040 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Signature)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070041 return error;
42
43 /* TODO: digestAlgorithm as UDATA */ signature->digestAlgorithm = 0; signature->digestAlgorithmLength = 0;
44
Jeff Thompson94ddc272013-08-08 14:17:38 -070045 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
46 (decoder, ndn_BinaryXml_DTag_Witness, 0, &signature->witness, &signature->witnessLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070047 return error;
48
49 // Require a signature.
Jeff Thompson94ddc272013-08-08 14:17:38 -070050 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
51 (decoder, ndn_BinaryXml_DTag_SignatureBits, 0, &signature->signature, &signature->signatureLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070052 return error;
53
Jeff Thompson94ddc272013-08-08 14:17:38 -070054 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070055 return error;
56
Jeff Thompsonadaf9232013-08-08 14:30:29 -070057 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070058}
59
Jeff Thompsonf4585af2013-09-11 14:56:59 -070060static ndn_Error encodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070061{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070062 if (metaInfo->type < 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070063 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070064
65 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070066 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_SignedInfo)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070067 return error;
68
69 // This will skip encoding if there is no publisherPublicKeyDigest.
Jeff Thompsonf4585af2013-09-11 14:56:59 -070070 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&signature->publisherPublicKeyDigest, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070071 return error;
72
Jeff Thompson94ddc272013-08-08 14:17:38 -070073 if ((error = ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -070074 (encoder, ndn_BinaryXml_DTag_Timestamp, metaInfo->timestampMilliseconds)))
Jeff Thompson2bcece32013-07-11 18:10:19 -070075 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070076
Jeff Thompsonfec716d2013-09-11 13:54:36 -070077 if (!(metaInfo->type < 0 || metaInfo->type == ndn_ContentType_DATA)) {
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070078 // Not the default of DATA, so we need to encode the type.
Jeff Thompson7ed3e272013-08-16 19:15:30 -070079 unsigned char *typeBytes;
80 unsigned int typeBytesLength = 3;
Jeff Thompsonfec716d2013-09-11 13:54:36 -070081 if (metaInfo->type == ndn_ContentType_ENCR)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070082 typeBytes = "\x10\xD0\x91";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070083 else if (metaInfo->type == ndn_ContentType_GONE)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070084 typeBytes = "\x18\xE3\x44";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070085 else if (metaInfo->type == ndn_ContentType_KEY)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070086 typeBytes = "\x28\x46\x3F";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070087 else if (metaInfo->type == ndn_ContentType_LINK)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070088 typeBytes = "\x2C\x83\x4A";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070089 else if (metaInfo->type == ndn_ContentType_NACK)
Jeff Thompson7ed3e272013-08-16 19:15:30 -070090 typeBytes = "\x34\x00\x8A";
91 else
92 return NDN_ERROR_unrecognized_ndn_ContentType;
93
94 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
95 (encoder, ndn_BinaryXml_DTag_Type, typeBytes, typeBytesLength)))
96 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070097 }
98
Jeff Thompson94ddc272013-08-08 14:17:38 -070099 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700100 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, metaInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700101 return error;
102
Jeff Thompson94ddc272013-08-08 14:17:38 -0700103 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
Jeff Thompson145e2252013-09-12 12:51:35 -0700104 (encoder, ndn_BinaryXml_DTag_FinalBlockID, metaInfo->finalBlockID.value, metaInfo->finalBlockID.valueLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700105 return error;
106
107 // This will skip encoding if there is no key locator.
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700108 if ((error = ndn_encodeBinaryXmlKeyLocator(&signature->keyLocator, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700109 return error;
110
Jeff Thompson94ddc272013-08-08 14:17:38 -0700111 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700112 return error;
113
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700114 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700115}
116
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700117static ndn_Error decodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700118{
119 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700120 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700121 return error;
122
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700123 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signature->publisherPublicKeyDigest, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700124 return error;
125
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700126 if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700127 (decoder, ndn_BinaryXml_DTag_Timestamp, &metaInfo->timestampMilliseconds))
Jeff Thompson210b92f2013-07-11 15:16:03 -0700128 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700129
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700130 unsigned char *typeBytes;
131 unsigned int typeBytesLength;
132 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
133 (decoder, ndn_BinaryXml_DTag_Type, 0, &typeBytes, &typeBytesLength)))
134 return error;
135 if (typeBytesLength == 0)
136 // The default Type is DATA.
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700137 metaInfo->type = ndn_ContentType_DATA;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700138 else if (typeBytesLength == 3) {
139 // All the recognized content types are 3 bytes.
140 if (ndn_memcmp(typeBytes, "\x0C\x04\xC0", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700141 metaInfo->type = ndn_ContentType_DATA;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700142 else if (ndn_memcmp(typeBytes, "\x10\xD0\x91", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700143 metaInfo->type = ndn_ContentType_ENCR;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700144 else if (ndn_memcmp(typeBytes, "\x18\xE3\x44", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700145 metaInfo->type = ndn_ContentType_GONE;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700146 else if (ndn_memcmp(typeBytes, "\x28\x46\x3F", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700147 metaInfo->type = ndn_ContentType_KEY;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700148 else if (ndn_memcmp(typeBytes, "\x2C\x83\x4A", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700149 metaInfo->type = ndn_ContentType_LINK;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700150 else if (ndn_memcmp(typeBytes, "\x34\x00\x8A", typeBytesLength) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700151 metaInfo->type = ndn_ContentType_NACK;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700152 else
153 return NDN_ERROR_unrecognized_ndn_ContentType;
154 }
155 else
156 return NDN_ERROR_unrecognized_ndn_ContentType;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700157
Jeff Thompson94ddc272013-08-08 14:17:38 -0700158 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700159 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &metaInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700160 return error;
161
Jeff Thompson94ddc272013-08-08 14:17:38 -0700162 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson145e2252013-09-12 12:51:35 -0700163 (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &metaInfo->finalBlockID.value, &metaInfo->finalBlockID.valueLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700164 return error;
165
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700166 if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&signature->keyLocator, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700167 return error;
168
Jeff Thompson94ddc272013-08-08 14:17:38 -0700169 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700170 return error;
171
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700172 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700173}
174
Jeff Thompsonaa020712013-08-08 21:20:06 -0700175ndn_Error ndn_encodeBinaryXmlData
Jeff Thompson9c661702013-09-13 14:35:44 -0700176 (struct ndn_Data *data, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700177{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700178 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700179 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700180 return error;
181
Jeff Thompson94ddc272013-08-08 14:17:38 -0700182 if ((error = encodeSignature(&data->signature, encoder)))
Jeff Thompsonae8a6f82013-08-16 17:12:49 -0700183 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700184
Jeff Thompson9c661702013-09-13 14:35:44 -0700185 *signedPortionBeginOffset = encoder->offset;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700186
Jeff Thompson94ddc272013-08-08 14:17:38 -0700187 if ((error = ndn_encodeBinaryXmlName(&data->name, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700188 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700189
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700190 if ((error = encodeSignedInfo(&data->signature, &data->metaInfo, encoder)))
Jeff Thompsonae8a6f82013-08-16 17:12:49 -0700191 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700192
Jeff Thompson94ddc272013-08-08 14:17:38 -0700193 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
194 (encoder, ndn_BinaryXml_DTag_Content, data->content, data->contentLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700195 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700196
Jeff Thompson9c661702013-09-13 14:35:44 -0700197 *signedPortionEndOffset = encoder->offset;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700198
Jeff Thompson94ddc272013-08-08 14:17:38 -0700199 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700200 return error;
201
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700202 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700203}
204
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700205ndn_Error ndn_decodeBinaryXmlData
Jeff Thompson9c661702013-09-13 14:35:44 -0700206 (struct ndn_Data *data, unsigned int *signedPortionBeginOffset, unsigned int *signedPortionEndOffset, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700207{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700208 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700209 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700210 return error;
211
212 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700213 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700214 return error;
215 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700216 if ((error = decodeSignature(&data->signature, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700217 return error;
218 }
219 else
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700220 ndn_Signature_initialize(&data->signature, data->signature.keyLocator.keyName.components, data->signature.keyLocator.keyName.maxComponents);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700221
Jeff Thompson9c661702013-09-13 14:35:44 -0700222 *signedPortionBeginOffset = decoder->offset;
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700223
Jeff Thompson94ddc272013-08-08 14:17:38 -0700224 if ((error = ndn_decodeBinaryXmlName(&data->name, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700225 return error;
226
Jeff Thompson94ddc272013-08-08 14:17:38 -0700227 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700228 return error;
229 if (gotExpectedTag) {
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700230 if ((error = decodeSignedInfo(&data->signature, &data->metaInfo, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700231 return error;
232 }
233 else
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700234 ndn_MetaInfo_initialize(&data->metaInfo);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700235
236 // Require a Content element, but set allowNull to allow a missing BLOB.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700237 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
238 (decoder, ndn_BinaryXml_DTag_Content, 1, &data->content, &data->contentLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700239 return error;
240
Jeff Thompson9c661702013-09-13 14:35:44 -0700241 *signedPortionEndOffset = decoder->offset;
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700242
Jeff Thompson94ddc272013-08-08 14:17:38 -0700243 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700244 return error;
245
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700246 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700247}