blob: 1f61e51efdace7cb786bf487c7158c84115508cb [file] [log] [blame]
Jeff Thompson9915bdd2013-07-11 11:27:09 -07001/**
2 * @author: Jeff Thompson
3 * Derived from Key.js by Meki Cheraoui.
4 * See COPYING for copyright and distribution information.
5 */
6
Jeff Thompson53412192013-08-06 13:35:50 -07007#include "binary-xml.h"
8#include "binary-xml-structure-decoder.h"
9#include "binary-xml-key.h"
Jeff Thompson9915bdd2013-07-11 11:27:09 -070010
Jeff Thompson7329a132013-08-16 15:57:37 -070011static 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 Thompson1cf72e92013-08-23 20:38:39 -070050 else {
51 // Key name data is omitted.
52 keyLocator->keyNameType = -1;
53 keyLocator->keyDataLength = 0;
54 }
Jeff Thompson7329a132013-08-16 15:57:37 -070055 }
56 }
57 }
58
59 return NDN_ERROR_success;
60}
61
Jeff Thompsonf0fea002013-07-30 17:22:42 -070062ndn_Error ndn_encodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -070063{
64 if (keyLocator->type < 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070065 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -070066
67 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070068 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyLocator)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070069 return error;
70
71 if (keyLocator->type == ndn_KeyLocatorType_KEY) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070072 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -070073 (encoder, ndn_BinaryXml_DTag_Key, keyLocator->keyData, keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070074 return error;
75 }
76 else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070077 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -070078 (encoder, ndn_BinaryXml_DTag_Certificate, keyLocator->keyData, keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070079 return error;
80 }
81 else if (keyLocator->type == ndn_KeyLocatorType_KEYNAME) {
Jeff Thompson7329a132013-08-16 15:57:37 -070082 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 Thompson1cf72e92013-08-23 20:38:39 -070087 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 Thompson7329a132013-08-16 15:57:37 -0700110 }
Jeff Thompson1cf72e92013-08-23 20:38:39 -0700111
Jeff Thompson7329a132013-08-16 15:57:37 -0700112 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
113 return error;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700114 }
115 else
116 return NDN_ERROR_unrecognized_ndn_KeyLocatorType;
117
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700118 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700119 return error;
120
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700121 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700122}
123
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700124ndn_Error ndn_decodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700125{
126 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700127 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyLocator)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700128 return error;
129
130 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700131 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Key, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700132 return error;
133 if (gotExpectedTag) {
134 keyLocator->type = ndn_KeyLocatorType_KEY;
135
Jeff Thompson94ddc272013-08-08 14:17:38 -0700136 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -0700137 (decoder, ndn_BinaryXml_DTag_Key, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700138 return error;
139 }
140 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700141 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Certificate, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700142 return error;
143 if (gotExpectedTag) {
144 keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
145
Jeff Thompson94ddc272013-08-08 14:17:38 -0700146 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -0700147 (decoder, ndn_BinaryXml_DTag_Certificate, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700148 return error;
149 }
150 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700151 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyName, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700152 return error;
153 if (gotExpectedTag) {
Jeff Thompson7329a132013-08-16 15:57:37 -0700154 keyLocator->type = ndn_KeyLocatorType_KEYNAME;
Jeff Thompson46c06512013-07-14 23:26:09 -0700155
Jeff Thompson7329a132013-08-16 15:57:37 -0700156 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyName)))
Jeff Thompson46c06512013-07-14 23:26:09 -0700157 return error;
Jeff Thompson7329a132013-08-16 15:57:37 -0700158 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 Thompson9915bdd2013-07-11 11:27:09 -0700164 }
165 else
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700166 return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_locator_type;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700167 }
168 }
169
Jeff Thompson94ddc272013-08-08 14:17:38 -0700170 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700171 return error;
172
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700173 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700174}
175
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700176ndn_Error ndn_decodeOptionalBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700177{
178 int gotExpectedTag;
179 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700180 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyLocator, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700181 return error;
182 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700183 if ((error = ndn_decodeBinaryXmlKeyLocator(keyLocator, decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700184 return error;
185 }
186 else
Jeff Thompson7329a132013-08-16 15:57:37 -0700187 ndn_KeyLocator_init(keyLocator, keyLocator->keyName.components, keyLocator->keyName.maxComponents);
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700188
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700189 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700190}