blob: 0f28ff5d24119f94a1122b56ea1dd23c5bd23a81 [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 Thompson93034532013-10-08 11:52:43 -070022 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement(encoder, ndn_BinaryXml_DTag_Witness, &signature->witness)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070023 return error;
24
25 // Require a signature.
Jeff Thompson93034532013-10-08 11:52:43 -070026 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_SignatureBits, &signature->signature)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070027 return error;
28
Jeff Thompson94ddc272013-08-08 14:17:38 -070029 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070030 return error;
31
Jeff Thompsonadaf9232013-08-08 14:30:29 -070032 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070033}
34
Jeff Thompsonf0fea002013-07-30 17:22:42 -070035static ndn_Error decodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070036{
37 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070038 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Signature)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070039 return error;
40
Jeff Thompson93034532013-10-08 11:52:43 -070041 /* TODO: digestAlgorithm as UDATA */ signature->digestAlgorithm.value = 0; signature->digestAlgorithm.length = 0;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070042
Jeff Thompson94ddc272013-08-08 14:17:38 -070043 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070044 (decoder, ndn_BinaryXml_DTag_Witness, 0, &signature->witness)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070045 return error;
46
47 // Require a signature.
Jeff Thompson94ddc272013-08-08 14:17:38 -070048 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070049 (decoder, ndn_BinaryXml_DTag_SignatureBits, 0, &signature->signature)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070050 return error;
51
Jeff Thompson94ddc272013-08-08 14:17:38 -070052 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070053 return error;
54
Jeff Thompsonadaf9232013-08-08 14:30:29 -070055 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070056}
57
Jeff Thompsonf4585af2013-09-11 14:56:59 -070058static ndn_Error encodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070059{
Jeff Thompsonfec716d2013-09-11 13:54:36 -070060 if (metaInfo->type < 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070061 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070062
63 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070064 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_SignedInfo)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070065 return error;
66
67 // This will skip encoding if there is no publisherPublicKeyDigest.
Jeff Thompsonf4585af2013-09-11 14:56:59 -070068 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&signature->publisherPublicKeyDigest, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070069 return error;
70
Jeff Thompson94ddc272013-08-08 14:17:38 -070071 if ((error = ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -070072 (encoder, ndn_BinaryXml_DTag_Timestamp, metaInfo->timestampMilliseconds)))
Jeff Thompson2bcece32013-07-11 18:10:19 -070073 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070074
Jeff Thompsonfec716d2013-09-11 13:54:36 -070075 if (!(metaInfo->type < 0 || metaInfo->type == ndn_ContentType_DATA)) {
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070076 // Not the default of DATA, so we need to encode the type.
Jeff Thompson93034532013-10-08 11:52:43 -070077 struct ndn_Blob typeBytes;
78 typeBytes.length = 3;
Jeff Thompsonfec716d2013-09-11 13:54:36 -070079 if (metaInfo->type == ndn_ContentType_ENCR)
Jeff Thompson93034532013-10-08 11:52:43 -070080 typeBytes.value = "\x10\xD0\x91";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070081 else if (metaInfo->type == ndn_ContentType_GONE)
Jeff Thompson93034532013-10-08 11:52:43 -070082 typeBytes.value = "\x18\xE3\x44";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070083 else if (metaInfo->type == ndn_ContentType_KEY)
Jeff Thompson93034532013-10-08 11:52:43 -070084 typeBytes.value = "\x28\x46\x3F";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070085 else if (metaInfo->type == ndn_ContentType_LINK)
Jeff Thompson93034532013-10-08 11:52:43 -070086 typeBytes.value = "\x2C\x83\x4A";
Jeff Thompsonfec716d2013-09-11 13:54:36 -070087 else if (metaInfo->type == ndn_ContentType_NACK)
Jeff Thompson93034532013-10-08 11:52:43 -070088 typeBytes.value = "\x34\x00\x8A";
Jeff Thompson7ed3e272013-08-16 19:15:30 -070089 else
90 return NDN_ERROR_unrecognized_ndn_ContentType;
91
Jeff Thompson93034532013-10-08 11:52:43 -070092 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Type, &typeBytes)))
Jeff Thompson7ed3e272013-08-16 19:15:30 -070093 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070094 }
95
Jeff Thompson94ddc272013-08-08 14:17:38 -070096 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -070097 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, metaInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070098 return error;
99
Jeff Thompson94ddc272013-08-08 14:17:38 -0700100 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700101 (encoder, ndn_BinaryXml_DTag_FinalBlockID, &metaInfo->finalBlockID.value)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700102 return error;
103
104 // This will skip encoding if there is no key locator.
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700105 if ((error = ndn_encodeBinaryXmlKeyLocator(&signature->keyLocator, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700106 return error;
107
Jeff Thompson94ddc272013-08-08 14:17:38 -0700108 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700109 return error;
110
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700111 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700112}
113
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700114static ndn_Error decodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700115{
116 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700117 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700118 return error;
119
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700120 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signature->publisherPublicKeyDigest, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700121 return error;
122
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700123 if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700124 (decoder, ndn_BinaryXml_DTag_Timestamp, &metaInfo->timestampMilliseconds))
Jeff Thompson210b92f2013-07-11 15:16:03 -0700125 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700126
Jeff Thompson93034532013-10-08 11:52:43 -0700127 struct ndn_Blob typeBytes;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700128 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700129 (decoder, ndn_BinaryXml_DTag_Type, 0, &typeBytes)))
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700130 return error;
Jeff Thompson93034532013-10-08 11:52:43 -0700131 if (typeBytes.length == 0)
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700132 // The default Type is DATA.
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700133 metaInfo->type = ndn_ContentType_DATA;
Jeff Thompson93034532013-10-08 11:52:43 -0700134 else if (typeBytes.length == 3) {
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700135 // All the recognized content types are 3 bytes.
Jeff Thompson93034532013-10-08 11:52:43 -0700136 if (ndn_memcmp(typeBytes.value, "\x0C\x04\xC0", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700137 metaInfo->type = ndn_ContentType_DATA;
Jeff Thompson93034532013-10-08 11:52:43 -0700138 else if (ndn_memcmp(typeBytes.value, "\x10\xD0\x91", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700139 metaInfo->type = ndn_ContentType_ENCR;
Jeff Thompson93034532013-10-08 11:52:43 -0700140 else if (ndn_memcmp(typeBytes.value, "\x18\xE3\x44", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700141 metaInfo->type = ndn_ContentType_GONE;
Jeff Thompson93034532013-10-08 11:52:43 -0700142 else if (ndn_memcmp(typeBytes.value, "\x28\x46\x3F", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700143 metaInfo->type = ndn_ContentType_KEY;
Jeff Thompson93034532013-10-08 11:52:43 -0700144 else if (ndn_memcmp(typeBytes.value, "\x2C\x83\x4A", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700145 metaInfo->type = ndn_ContentType_LINK;
Jeff Thompson93034532013-10-08 11:52:43 -0700146 else if (ndn_memcmp(typeBytes.value, "\x34\x00\x8A", typeBytes.length) == 0)
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700147 metaInfo->type = ndn_ContentType_NACK;
Jeff Thompson7ed3e272013-08-16 19:15:30 -0700148 else
149 return NDN_ERROR_unrecognized_ndn_ContentType;
150 }
151 else
152 return NDN_ERROR_unrecognized_ndn_ContentType;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700153
Jeff Thompson94ddc272013-08-08 14:17:38 -0700154 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
Jeff Thompsonfec716d2013-09-11 13:54:36 -0700155 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &metaInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700156 return error;
157
Jeff Thompson94ddc272013-08-08 14:17:38 -0700158 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700159 (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &metaInfo->finalBlockID.value)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700160 return error;
161
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700162 if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&signature->keyLocator, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700163 return error;
164
Jeff Thompson94ddc272013-08-08 14:17:38 -0700165 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700166 return error;
167
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700168 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700169}
170
Jeff Thompsonaa020712013-08-08 21:20:06 -0700171ndn_Error ndn_encodeBinaryXmlData
Jeff Thompson97223af2013-09-24 17:01:27 -0700172 (struct ndn_Data *data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700173{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700174 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700175 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700176 return error;
177
Jeff Thompson94ddc272013-08-08 14:17:38 -0700178 if ((error = encodeSignature(&data->signature, encoder)))
Jeff Thompsonae8a6f82013-08-16 17:12:49 -0700179 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700180
Jeff Thompson9c661702013-09-13 14:35:44 -0700181 *signedPortionBeginOffset = encoder->offset;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700182
Jeff Thompson94ddc272013-08-08 14:17:38 -0700183 if ((error = ndn_encodeBinaryXmlName(&data->name, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700184 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700185
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700186 if ((error = encodeSignedInfo(&data->signature, &data->metaInfo, encoder)))
Jeff Thompsonae8a6f82013-08-16 17:12:49 -0700187 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700188
Jeff Thompson93034532013-10-08 11:52:43 -0700189 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Content, &data->content)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700190 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700191
Jeff Thompson9c661702013-09-13 14:35:44 -0700192 *signedPortionEndOffset = encoder->offset;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700193
Jeff Thompson94ddc272013-08-08 14:17:38 -0700194 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700195 return error;
196
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700197 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700198}
199
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700200ndn_Error ndn_decodeBinaryXmlData
Jeff Thompson97223af2013-09-24 17:01:27 -0700201 (struct ndn_Data *data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700202{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700203 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700204 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700205 return error;
206
207 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700208 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700209 return error;
210 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700211 if ((error = decodeSignature(&data->signature, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700212 return error;
213 }
214 else
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700215 ndn_Signature_initialize(&data->signature, data->signature.keyLocator.keyName.components, data->signature.keyLocator.keyName.maxComponents);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700216
Jeff Thompson9c661702013-09-13 14:35:44 -0700217 *signedPortionBeginOffset = decoder->offset;
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700218
Jeff Thompson94ddc272013-08-08 14:17:38 -0700219 if ((error = ndn_decodeBinaryXmlName(&data->name, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700220 return error;
221
Jeff Thompson94ddc272013-08-08 14:17:38 -0700222 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700223 return error;
224 if (gotExpectedTag) {
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700225 if ((error = decodeSignedInfo(&data->signature, &data->metaInfo, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700226 return error;
227 }
228 else
Jeff Thompsonf4585af2013-09-11 14:56:59 -0700229 ndn_MetaInfo_initialize(&data->metaInfo);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700230
231 // Require a Content element, but set allowNull to allow a missing BLOB.
Jeff Thompson93034532013-10-08 11:52:43 -0700232 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Content, 1, &data->content)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700233 return error;
234
Jeff Thompson9c661702013-09-13 14:35:44 -0700235 *signedPortionEndOffset = decoder->offset;
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700236
Jeff Thompson94ddc272013-08-08 14:17:38 -0700237 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700238 return error;
239
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700240 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700241}