Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 1 | /** |
| 2 | * @author: Jeff Thompson |
| 3 | * Derived from Key.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.h" |
| 8 | #include "binary-xml-structure-decoder.h" |
| 9 | #include "binary-xml-key.h" |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 11 | static ndn_Error decodeKeyNameData(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder) |
| 12 | { |
| 13 | int gotExpectedTag; |
| 14 | ndn_Error error; |
| 15 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, &gotExpectedTag))) |
| 16 | return error; |
| 17 | if (gotExpectedTag) { |
| 18 | keyLocator->keyNameType = ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST; |
| 19 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 20 | (decoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, 0, &keyLocator->keyData, &keyLocator->keyDataLength))) |
| 21 | return error; |
| 22 | } |
| 23 | else { |
| 24 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, &gotExpectedTag))) |
| 25 | return error; |
| 26 | if (gotExpectedTag) { |
| 27 | keyLocator->keyNameType = ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST; |
| 28 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 29 | (decoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, 0, &keyLocator->keyData, &keyLocator->keyDataLength))) |
| 30 | return error; |
| 31 | } |
| 32 | else { |
| 33 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, &gotExpectedTag))) |
| 34 | return error; |
| 35 | if (gotExpectedTag) { |
| 36 | keyLocator->keyNameType = ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST; |
| 37 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 38 | (decoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, 0, &keyLocator->keyData, &keyLocator->keyDataLength))) |
| 39 | return error; |
| 40 | } |
| 41 | else { |
| 42 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, &gotExpectedTag))) |
| 43 | return error; |
| 44 | if (gotExpectedTag) { |
| 45 | keyLocator->keyNameType = ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST; |
| 46 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
| 47 | (decoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, 0, &keyLocator->keyData, &keyLocator->keyDataLength))) |
| 48 | return error; |
| 49 | } |
Jeff Thompson | 1cf72e9 | 2013-08-23 20:38:39 -0700 | [diff] [blame^] | 50 | else { |
| 51 | // Key name data is omitted. |
| 52 | keyLocator->keyNameType = -1; |
| 53 | keyLocator->keyDataLength = 0; |
| 54 | } |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return NDN_ERROR_success; |
| 60 | } |
| 61 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 62 | ndn_Error ndn_encodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlEncoder *encoder) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 63 | { |
| 64 | if (keyLocator->type < 0) |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 65 | return NDN_ERROR_success; |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 66 | |
| 67 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 68 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyLocator))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 69 | return error; |
| 70 | |
| 71 | if (keyLocator->type == ndn_KeyLocatorType_KEY) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 72 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
Jeff Thompson | 63d0269 | 2013-08-16 12:09:07 -0700 | [diff] [blame] | 73 | (encoder, ndn_BinaryXml_DTag_Key, keyLocator->keyData, keyLocator->keyDataLength))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 74 | return error; |
| 75 | } |
| 76 | else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 77 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
Jeff Thompson | 63d0269 | 2013-08-16 12:09:07 -0700 | [diff] [blame] | 78 | (encoder, ndn_BinaryXml_DTag_Certificate, keyLocator->keyData, keyLocator->keyDataLength))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 79 | return error; |
| 80 | } |
| 81 | else if (keyLocator->type == ndn_KeyLocatorType_KEYNAME) { |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 82 | if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyName))) |
| 83 | return error; |
| 84 | if ((error = ndn_encodeBinaryXmlName(&keyLocator->keyName, encoder))) |
| 85 | return error; |
| 86 | |
Jeff Thompson | 1cf72e9 | 2013-08-23 20:38:39 -0700 | [diff] [blame^] | 87 | if ((int)keyLocator->keyNameType >= 0 && keyLocator->keyDataLength > 0) { |
| 88 | if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST) { |
| 89 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 90 | (encoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, keyLocator->keyData, keyLocator->keyDataLength))) |
| 91 | return error; |
| 92 | } |
| 93 | else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST) { |
| 94 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 95 | (encoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, keyLocator->keyData, keyLocator->keyDataLength))) |
| 96 | return error; |
| 97 | } |
| 98 | else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST) { |
| 99 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 100 | (encoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, keyLocator->keyData, keyLocator->keyDataLength))) |
| 101 | return error; |
| 102 | } |
| 103 | else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST) { |
| 104 | if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement |
| 105 | (encoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, keyLocator->keyData, keyLocator->keyDataLength))) |
| 106 | return error; |
| 107 | } |
| 108 | else |
| 109 | return NDN_ERROR_unrecognized_ndn_KeyNameType; |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 110 | } |
Jeff Thompson | 1cf72e9 | 2013-08-23 20:38:39 -0700 | [diff] [blame^] | 111 | |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 112 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
| 113 | return error; |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 114 | } |
| 115 | else |
| 116 | return NDN_ERROR_unrecognized_ndn_KeyLocatorType; |
| 117 | |
Jeff Thompson | 2d27e2f | 2013-08-09 12:55:00 -0700 | [diff] [blame] | 118 | if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 119 | return error; |
| 120 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 121 | return NDN_ERROR_success; |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 124 | ndn_Error ndn_decodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 125 | { |
| 126 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 127 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyLocator))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 128 | return error; |
| 129 | |
| 130 | int gotExpectedTag; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 131 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Key, &gotExpectedTag))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 132 | return error; |
| 133 | if (gotExpectedTag) { |
| 134 | keyLocator->type = ndn_KeyLocatorType_KEY; |
| 135 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 136 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
Jeff Thompson | 63d0269 | 2013-08-16 12:09:07 -0700 | [diff] [blame] | 137 | (decoder, ndn_BinaryXml_DTag_Key, 0, &keyLocator->keyData, &keyLocator->keyDataLength))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 138 | return error; |
| 139 | } |
| 140 | else { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 141 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Certificate, &gotExpectedTag))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 142 | return error; |
| 143 | if (gotExpectedTag) { |
| 144 | keyLocator->type = ndn_KeyLocatorType_CERTIFICATE; |
| 145 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 146 | if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement |
Jeff Thompson | 63d0269 | 2013-08-16 12:09:07 -0700 | [diff] [blame] | 147 | (decoder, ndn_BinaryXml_DTag_Certificate, 0, &keyLocator->keyData, &keyLocator->keyDataLength))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 148 | return error; |
| 149 | } |
| 150 | else { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 151 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyName, &gotExpectedTag))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 152 | return error; |
| 153 | if (gotExpectedTag) { |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 154 | keyLocator->type = ndn_KeyLocatorType_KEYNAME; |
Jeff Thompson | 46c0651 | 2013-07-14 23:26:09 -0700 | [diff] [blame] | 155 | |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 156 | if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyName))) |
Jeff Thompson | 46c0651 | 2013-07-14 23:26:09 -0700 | [diff] [blame] | 157 | return error; |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 158 | if ((error = ndn_decodeBinaryXmlName(&keyLocator->keyName, decoder))) |
| 159 | return error; |
| 160 | if ((error = decodeKeyNameData(keyLocator, decoder))) |
| 161 | return error; |
| 162 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
| 163 | return error; |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 164 | } |
| 165 | else |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 166 | return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_locator_type; |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 170 | if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 171 | return error; |
| 172 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 173 | return NDN_ERROR_success; |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Jeff Thompson | f0fea00 | 2013-07-30 17:22:42 -0700 | [diff] [blame] | 176 | ndn_Error ndn_decodeOptionalBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 177 | { |
| 178 | int gotExpectedTag; |
| 179 | ndn_Error error; |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 180 | if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyLocator, &gotExpectedTag))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 181 | return error; |
| 182 | if (gotExpectedTag) { |
Jeff Thompson | 94ddc27 | 2013-08-08 14:17:38 -0700 | [diff] [blame] | 183 | if ((error = ndn_decodeBinaryXmlKeyLocator(keyLocator, decoder))) |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 184 | return error; |
| 185 | } |
| 186 | else |
Jeff Thompson | 7329a13 | 2013-08-16 15:57:37 -0700 | [diff] [blame] | 187 | ndn_KeyLocator_init(keyLocator, keyLocator->keyName.components, keyLocator->keyName.maxComponents); |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 188 | |
Jeff Thompson | adaf923 | 2013-08-08 14:30:29 -0700 | [diff] [blame] | 189 | return NDN_ERROR_success; |
Jeff Thompson | 9915bdd | 2013-07-11 11:27:09 -0700 | [diff] [blame] | 190 | } |