Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * Derived from ContentObject.js by Meki Cheraoui. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 7 | #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 Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 11 | #include "binary-xml-data.h" |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 13 | static ndn_Error encodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 14 | { |
| 15 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 16 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Signature))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 17 | return error; |
| 18 | |
| 19 | // TODO: Check if digestAlgorithm is the same as the default, and skip it, otherwise encode it as UDATA. |
| 20 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 21 | if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement |
| 22 | (encoder, ndn_BinaryXml_DTag_Witness, signature->witness, signature->witnessLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 23 | return error; |
| 24 | |
| 25 | // Require a signature. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 26 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 27 | (encoder, ndn_BinaryXml_DTag_SignatureBits, signature->signature, signature->signatureLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 28 | return error; |
| 29 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 30 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 31 | return error; |
| 32 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 33 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 36 | static ndn_Error decodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 37 | { |
| 38 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 39 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Signature))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 40 | return error; |
| 41 | |
| 42 | /* TODO: digestAlgorithm as UDATA */ signature->digestAlgorithm = 0; signature->digestAlgorithmLength = 0; |
| 43 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 44 | if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
| 45 | (decoder, ndn_BinaryXml_DTag_Witness, 0, &signature->witness, &signature->witnessLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 46 | return error; |
| 47 | |
| 48 | // Require a signature. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 49 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 50 | (decoder, ndn_BinaryXml_DTag_SignatureBits, 0, &signature->signature, &signature->signatureLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 51 | return error; |
| 52 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 53 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 54 | return error; |
| 55 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 56 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 59 | static ndn_Error encodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 60 | { |
| 61 | if (signedInfo->type < 0) |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 62 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 63 | |
| 64 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 65 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_SignedInfo))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 66 | return error; |
| 67 | |
| 68 | // This will skip encoding if there is no publisherPublicKeyDigest. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 69 | if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 70 | return error; |
| 71 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 72 | if ((error = ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement |
| 73 | (encoder, ndn_BinaryXml_DTag_Timestamp, signedInfo->timestampMilliseconds))) |
Jeff Thompson | 2bcece3 | 2013-07-11 18:10:19 -0700 | [diff] [blame] | 74 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 75 | |
| 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 Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 81 | if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement |
| 82 | (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, signedInfo->freshnessSeconds))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 83 | return error; |
| 84 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 85 | if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement |
| 86 | (encoder, ndn_BinaryXml_DTag_FinalBlockID, signedInfo->finalBlockID, signedInfo->finalBlockIDLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 87 | return error; |
| 88 | |
| 89 | // This will skip encoding if there is no key locator. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 90 | if ((error = ndn_encodeBinaryXmlKeyLocator(&signedInfo->keyLocator, encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 91 | return error; |
| 92 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 93 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 94 | return error; |
| 95 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 96 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 99 | static ndn_Error decodeSignedInfo(struct ndn_SignedInfo *signedInfo, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 100 | { |
| 101 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 102 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 103 | return error; |
| 104 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 105 | if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signedInfo->publisherPublicKeyDigest, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 106 | return error; |
| 107 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 108 | if (error= ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement |
| 109 | (decoder, ndn_BinaryXml_DTag_Timestamp, &signedInfo->timestampMilliseconds)) |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 110 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 111 | |
| 112 | // TODO: Implement reading the type and converting to an int. |
| 113 | signedInfo->type = ndn_ContentType_DATA; |
| 114 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 115 | if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement |
| 116 | (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &signedInfo->freshnessSeconds))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 117 | return error; |
| 118 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 119 | if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
| 120 | (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &signedInfo->finalBlockID, &signedInfo->finalBlockIDLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 121 | return error; |
| 122 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 123 | if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&signedInfo->keyLocator, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 124 | return error; |
| 125 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 126 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 127 | return error; |
| 128 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 129 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 132 | ndn_Error ndn_encodeBinaryXmlData |
| 133 | (struct ndn_Data *data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 134 | { |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 135 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 136 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 137 | return error; |
| 138 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 139 | if ((error = encodeSignature(&data->signature, encoder))) |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 140 | return NDN_ERROR_success; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 141 | |
| 142 | *signedFieldsBeginOffset = encoder->offset; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 143 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 144 | if ((error = ndn_encodeBinaryXmlName(&data->name, encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 145 | return error; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 146 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 147 | if ((error = encodeSignedInfo(&data->signedInfo, encoder))) |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 148 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 149 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 150 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 151 | (encoder, ndn_BinaryXml_DTag_Content, data->content, data->contentLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 152 | return error; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 153 | |
| 154 | *signedFieldsEndOffset = encoder->offset; |
| 155 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 156 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 157 | return error; |
| 158 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 159 | return NDN_ERROR_success; |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame^] | 162 | ndn_Error ndn_decodeBinaryXmlData |
| 163 | (struct ndn_Data *data, unsigned int *signedFieldsBeginOffset, unsigned int *signedFieldsEndOffset, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 164 | { |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 165 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 166 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 167 | return error; |
| 168 | |
| 169 | int gotExpectedTag; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 170 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 171 | return error; |
| 172 | if (gotExpectedTag) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 173 | if ((error = decodeSignature(&data->signature, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 174 | return error; |
| 175 | } |
| 176 | else |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 177 | ndn_Signature_init(&data->signature); |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 178 | |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame^] | 179 | *signedFieldsBeginOffset = decoder->offset; |
| 180 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 181 | if ((error = ndn_decodeBinaryXmlName(&data->name, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 182 | return error; |
| 183 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 184 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 185 | return error; |
| 186 | if (gotExpectedTag) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 187 | if ((error = decodeSignedInfo(&data->signedInfo, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 188 | return error; |
| 189 | } |
| 190 | else |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 191 | ndn_SignedInfo_init(&data->signedInfo); |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 192 | |
| 193 | // Require a Content element, but set allowNull to allow a missing BLOB. |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 194 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 195 | (decoder, ndn_BinaryXml_DTag_Content, 1, &data->content, &data->contentLength))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 196 | return error; |
| 197 | |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame^] | 198 | *signedFieldsEndOffset = decoder->offset; |
| 199 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 200 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 201 | return error; |
| 202 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 203 | return NDN_ERROR_success; |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 204 | } |