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