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