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