Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 4 | * Derived from ContentObject.js by Meki Cheraoui. |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 8 | #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 Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 12 | #include "binary-xml-data.h" |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 13 | #include "binary-xml-key.h" |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 15 | static ndn_Error encodeSignature(struct ndn_Signature *signature, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 16 | { |
| 17 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 18 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Signature))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 19 | return error; |
| 20 | |
| 21 | // TODO: Check if digestAlgorithm is the same as the default, and skip it, otherwise encode it as UDATA. |
| 22 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 23 | if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement(encoder, ndn_BinaryXml_DTag_Witness, &signature->witness))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 24 | return error; |
| 25 | |
| 26 | // Require a signature. |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 27 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_SignatureBits, &signature->signature))) |
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 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 42 | /* TODO: digestAlgorithm as UDATA */ signature->digestAlgorithm.value = 0; signature->digestAlgorithm.length = 0; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 43 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 44 | if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 45 | (decoder, ndn_BinaryXml_DTag_Witness, 0, &signature->witness))) |
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 |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 50 | (decoder, ndn_BinaryXml_DTag_SignatureBits, 0, &signature->signature))) |
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 | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 59 | static ndn_Error encodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 60 | { |
Jeff Thompson | 62f6e7a | 2013-11-20 15:20:18 -0800 | [diff] [blame] | 61 | if ((int)metaInfo->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 | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 69 | if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&signature->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 |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 73 | (encoder, ndn_BinaryXml_DTag_Timestamp, metaInfo->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 | |
Jeff Thompson | 62f6e7a | 2013-11-20 15:20:18 -0800 | [diff] [blame] | 76 | if (!((int)metaInfo->type < 0 || metaInfo->type == ndn_ContentType_DATA)) { |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 77 | // Not the default of DATA, so we need to encode the type. |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 78 | struct ndn_Blob typeBytes; |
| 79 | typeBytes.length = 3; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 80 | if (metaInfo->type == ndn_ContentType_ENCR) |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 81 | typeBytes.value = (uint8_t *)"\x10\xD0\x91"; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 82 | else if (metaInfo->type == ndn_ContentType_GONE) |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 83 | typeBytes.value = (uint8_t *)"\x18\xE3\x44"; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 84 | else if (metaInfo->type == ndn_ContentType_KEY) |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 85 | typeBytes.value = (uint8_t *)"\x28\x46\x3F"; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 86 | else if (metaInfo->type == ndn_ContentType_LINK) |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 87 | typeBytes.value = (uint8_t *)"\x2C\x83\x4A"; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 88 | else if (metaInfo->type == ndn_ContentType_NACK) |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 89 | typeBytes.value = (uint8_t *)"\x34\x00\x8A"; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 90 | else |
| 91 | return NDN_ERROR_unrecognized_ndn_ContentType; |
| 92 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 93 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Type, &typeBytes))) |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 94 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 97 | if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 98 | (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, metaInfo->freshnessSeconds))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 99 | return error; |
| 100 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 101 | if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 102 | (encoder, ndn_BinaryXml_DTag_FinalBlockID, &metaInfo->finalBlockID.value))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 103 | return error; |
| 104 | |
| 105 | // This will skip encoding if there is no key locator. |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 106 | if ((error = ndn_encodeBinaryXmlKeyLocator(&signature->keyLocator, encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 107 | return error; |
| 108 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 109 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 110 | return error; |
| 111 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 112 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 115 | static ndn_Error decodeSignedInfo(struct ndn_Signature *signature, struct ndn_MetaInfo *metaInfo, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 116 | { |
| 117 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 118 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_SignedInfo))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 119 | return error; |
| 120 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 121 | if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&signature->publisherPublicKeyDigest, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 122 | return error; |
| 123 | |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 124 | if ((error = ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement |
| 125 | (decoder, ndn_BinaryXml_DTag_Timestamp, &metaInfo->timestampMilliseconds))) |
Jeff Thompson | 210b92f | 2013-07-11 15:16:03 -0700 | [diff] [blame] | 126 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 127 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 128 | struct ndn_Blob typeBytes; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 129 | if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 130 | (decoder, ndn_BinaryXml_DTag_Type, 0, &typeBytes))) |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 131 | return error; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 132 | if (typeBytes.length == 0) |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 133 | // The default Type is DATA. |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 134 | metaInfo->type = ndn_ContentType_DATA; |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 135 | else if (typeBytes.length == 3) { |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 136 | // All the recognized content types are 3 bytes. |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 137 | if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x0C\x04\xC0", typeBytes.length) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 138 | metaInfo->type = ndn_ContentType_DATA; |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 139 | else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x10\xD0\x91", typeBytes.length) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 140 | metaInfo->type = ndn_ContentType_ENCR; |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 141 | else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x18\xE3\x44", typeBytes.length) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 142 | metaInfo->type = ndn_ContentType_GONE; |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 143 | else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x28\x46\x3F", typeBytes.length) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 144 | metaInfo->type = ndn_ContentType_KEY; |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 145 | else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x2C\x83\x4A", typeBytes.length) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 146 | metaInfo->type = ndn_ContentType_LINK; |
Jeff Thompson | e2cfa35 | 2013-11-20 15:13:40 -0800 | [diff] [blame] | 147 | else if (ndn_memcmp(typeBytes.value, (uint8_t *)"\x34\x00\x8A", typeBytes.length) == 0) |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 148 | metaInfo->type = ndn_ContentType_NACK; |
Jeff Thompson | 7ed3e27 | 2013-08-16 19:15:30 -0700 | [diff] [blame] | 149 | else |
| 150 | return NDN_ERROR_unrecognized_ndn_ContentType; |
| 151 | } |
| 152 | else |
| 153 | return NDN_ERROR_unrecognized_ndn_ContentType; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 154 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 155 | if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 156 | (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &metaInfo->freshnessSeconds))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 157 | return error; |
| 158 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 159 | if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 160 | (decoder, ndn_BinaryXml_DTag_FinalBlockID, 0, &metaInfo->finalBlockID.value))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 161 | return error; |
| 162 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 163 | if ((error = ndn_decodeOptionalBinaryXmlKeyLocator(&signature->keyLocator, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 164 | return error; |
| 165 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 166 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 167 | return error; |
| 168 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 169 | return NDN_ERROR_success; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 172 | ndn_Error ndn_encodeBinaryXmlData |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 173 | (struct ndn_Data *data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 174 | { |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 175 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 176 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ContentObject))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 177 | return error; |
| 178 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 179 | if ((error = encodeSignature(&data->signature, encoder))) |
Jeff Thompson | ae8a6f8 | 2013-08-16 17:12:49 -0700 | [diff] [blame] | 180 | return error; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 181 | |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 182 | *signedPortionBeginOffset = encoder->offset; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 183 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 184 | if ((error = ndn_encodeBinaryXmlName(&data->name, encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 185 | return error; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 186 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 187 | if ((error = encodeSignedInfo(&data->signature, &data->metaInfo, encoder))) |
Jeff Thompson | ae8a6f8 | 2013-08-16 17:12:49 -0700 | [diff] [blame] | 188 | return error; |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 189 | |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 190 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Content, &data->content))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 191 | return error; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 192 | |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 193 | *signedPortionEndOffset = encoder->offset; |
Jeff Thompson | aa02071 | 2013-08-08 21:20:06 -0700 | [diff] [blame] | 194 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 195 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 196 | return error; |
| 197 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 198 | return NDN_ERROR_success; |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame] | 201 | ndn_Error ndn_decodeBinaryXmlData |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 202 | (struct ndn_Data *data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 203 | { |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 204 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 205 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ContentObject))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 206 | return error; |
| 207 | |
| 208 | int gotExpectedTag; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 209 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Signature, &gotExpectedTag))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 210 | return error; |
| 211 | if (gotExpectedTag) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 212 | if ((error = decodeSignature(&data->signature, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 213 | return error; |
| 214 | } |
| 215 | else |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 216 | ndn_Signature_initialize(&data->signature, data->signature.keyLocator.keyName.components, data->signature.keyLocator.keyName.maxComponents); |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 217 | |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 218 | *signedPortionBeginOffset = decoder->offset; |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame] | 219 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 220 | if ((error = ndn_decodeBinaryXmlName(&data->name, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 221 | return error; |
| 222 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 223 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_SignedInfo, &gotExpectedTag))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 224 | return error; |
| 225 | if (gotExpectedTag) { |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 226 | if ((error = decodeSignedInfo(&data->signature, &data->metaInfo, decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 227 | return error; |
| 228 | } |
| 229 | else |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 230 | ndn_MetaInfo_initialize(&data->metaInfo); |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 231 | |
| 232 | // Require a Content element, but set allowNull to allow a missing BLOB. |
Jeff Thompson | 9303453 | 2013-10-08 11:52:43 -0700 | [diff] [blame] | 233 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Content, 1, &data->content))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 234 | return error; |
| 235 | |
Jeff Thompson | 9c66170 | 2013-09-13 14:35:44 -0700 | [diff] [blame] | 236 | *signedPortionEndOffset = decoder->offset; |
Jeff Thompson | 2d4698a | 2013-08-12 11:28:34 -0700 | [diff] [blame] | 237 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 238 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | de6fc6a | 2013-07-11 12:16:37 -0700 | [diff] [blame] | 239 | return error; |
| 240 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 241 | return NDN_ERROR_success; |
Jeff Thompson | 4069ce9 | 2013-07-10 19:41:55 -0700 | [diff] [blame] | 242 | } |