blob: 564e6f4ed32b56d831ef4ccd15acb8b3f23bc954 [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
76 if (signedInfo->type != ndn_ContentType_DATA) {
77 // Not the default of DATA, so we need to encode the type.
78 // TODO: Implement converting the type from an int and encoding.
79 }
80
Jeff Thompson94ddc272013-08-08 14:17:38 -070081 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
82 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, signedInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070083 return error;
84
Jeff Thompson94ddc272013-08-08 14:17:38 -070085 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement
86 (encoder, ndn_BinaryXml_DTag_FinalBlockID, signedInfo->finalBlockID, signedInfo->finalBlockIDLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070087 return error;
88
89 // This will skip encoding if there is no key locator.
Jeff Thompson94ddc272013-08-08 14:17:38 -070090 if ((error = ndn_encodeBinaryXmlKeyLocator(&signedInfo->keyLocator, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070091 return error;
92
Jeff Thompson94ddc272013-08-08 14:17:38 -070093 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070094 return error;
95
Jeff Thompsonadaf9232013-08-08 14:30:29 -070096 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -070097}
98
Jeff Thompsonf0fea002013-07-30 17:22:42 -070099static ndn_Error decodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700100{
101 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700102 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700103 return error;
104
Jeff Thompson94ddc272013-08-08 14:17:38 -0700105 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700106 return error;
107
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700108 if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
109 (decoder, ndn_BinaryXml_DTag_Timestamp, &signedInfo->timestampMilliseconds))
Jeff Thompson210b92f2013-07-11 15:16:03 -0700110 return error;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700111
112 // TODO: Implement reading the type and converting to an int.
113 signedInfo->type = ndn_ContentType_DATA;
114
Jeff Thompson94ddc272013-08-08 14:17:38 -0700115 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
116 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &signedInfo->freshnessSeconds)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700117 return error;
118
Jeff Thompson94ddc272013-08-08 14:17:38 -0700119 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
120 (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &signedInfo->finalBlockID, &signedInfo->finalBlockIDLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700121 return error;
122
Jeff Thompson94ddc272013-08-08 14:17:38 -0700123 if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&signedInfo->keyLocator, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700124 return error;
125
Jeff Thompson94ddc272013-08-08 14:17:38 -0700126 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700127 return error;
128
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700129 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700130}
131
Jeff Thompsonaa020712013-08-08 21:20:06 -0700132ndn_Error ndn_encodeBinaryXmlData
133 (struct ndn_Data *data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700134{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700135 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700136 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700137 return error;
138
Jeff Thompson94ddc272013-08-08 14:17:38 -0700139 if ((error = encodeSignature(&data->signature, encoder)))
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700140 return NDN_ERROR_success;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700141
142 *signedFieldsBeginOffset = encoder->offset;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700143
Jeff Thompson94ddc272013-08-08 14:17:38 -0700144 if ((error = ndn_encodeBinaryXmlName(&data->name, encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700145 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700146
Jeff Thompson94ddc272013-08-08 14:17:38 -0700147 if ((error = encodeSignedInfo(&data->signedInfo, encoder)))
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700148 return NDN_ERROR_success;
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700149
Jeff Thompson94ddc272013-08-08 14:17:38 -0700150 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
151 (encoder, ndn_BinaryXml_DTag_Content, data->content, data->contentLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700152 return error;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700153
154 *signedFieldsEndOffset = encoder->offset;
155
Jeff Thompson94ddc272013-08-08 14:17:38 -0700156 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700157 return error;
158
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700159 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700160}
161
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700162ndn_Error ndn_decodeBinaryXmlData
163 (struct ndn_Data *data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson4069ce92013-07-10 19:41:55 -0700164{
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700165 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700166 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700167 return error;
168
169 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700170 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700171 return error;
172 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700173 if ((error = decodeSignature(&data->signature, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700174 return error;
175 }
176 else
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700177 ndn_Signature_init(&data->signature);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700178
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700179 *signedFieldsBeginOffset = decoder->offset;
180
Jeff Thompson94ddc272013-08-08 14:17:38 -0700181 if ((error = ndn_decodeBinaryXmlName(&data->name, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700182 return error;
183
Jeff Thompson94ddc272013-08-08 14:17:38 -0700184 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700185 return error;
186 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700187 if ((error = decodeSignedInfo(&data->signedInfo, decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700188 return error;
189 }
190 else
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700191 ndn_SignedInfo_init(&data->signedInfo);
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700192
193 // Require a Content element, but set allowNull to allow a missing BLOB.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700194 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
195 (decoder, ndn_BinaryXml_DTag_Content, 1, &data->content, &data->contentLength)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700196 return error;
197
Jeff Thompson2d4698a2013-08-12 11:28:34 -0700198 *signedFieldsEndOffset = decoder->offset;
199
Jeff Thompson94ddc272013-08-08 14:17:38 -0700200 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonde6fc6a2013-07-11 12:16:37 -0700201 return error;
202
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700203 return NDN_ERROR_success;
Jeff Thompson4069ce92013-07-10 19:41:55 -0700204}