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