blob: 42e446366165ec615e3314eaa61770cc3f8849f9 [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 }
50 else
51 return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_name_type;
52 }
53 }
54 }
55
56 return NDN_ERROR_success;
57}
58
Jeff Thompsonf0fea002013-07-30 17:22:42 -070059ndn_Error ndn_encodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -070060{
61 if (keyLocator->type < 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070062 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -070063
64 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070065 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyLocator)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070066 return error;
67
68 if (keyLocator->type == ndn_KeyLocatorType_KEY) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070069 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -070070 (encoder, ndn_BinaryXml_DTag_Key, keyLocator->keyData, keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070071 return error;
72 }
73 else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070074 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -070075 (encoder, ndn_BinaryXml_DTag_Certificate, keyLocator->keyData, keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070076 return error;
77 }
78 else if (keyLocator->type == ndn_KeyLocatorType_KEYNAME) {
Jeff Thompson7329a132013-08-16 15:57:37 -070079 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 Thompson9915bdd2013-07-11 11:27:09 -0700109 }
110 else
111 return NDN_ERROR_unrecognized_ndn_KeyLocatorType;
112
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700113 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700114 return error;
115
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700116 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700117}
118
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700119ndn_Error ndn_decodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700120{
121 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700122 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyLocator)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700123 return error;
124
125 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700126 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Key, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700127 return error;
128 if (gotExpectedTag) {
129 keyLocator->type = ndn_KeyLocatorType_KEY;
130
Jeff Thompson94ddc272013-08-08 14:17:38 -0700131 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -0700132 (decoder, ndn_BinaryXml_DTag_Key, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700133 return error;
134 }
135 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700136 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Certificate, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700137 return error;
138 if (gotExpectedTag) {
139 keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
140
Jeff Thompson94ddc272013-08-08 14:17:38 -0700141 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -0700142 (decoder, ndn_BinaryXml_DTag_Certificate, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700143 return error;
144 }
145 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700146 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyName, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700147 return error;
148 if (gotExpectedTag) {
Jeff Thompson7329a132013-08-16 15:57:37 -0700149 keyLocator->type = ndn_KeyLocatorType_KEYNAME;
Jeff Thompson46c06512013-07-14 23:26:09 -0700150
Jeff Thompson7329a132013-08-16 15:57:37 -0700151 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyName)))
Jeff Thompson46c06512013-07-14 23:26:09 -0700152 return error;
Jeff Thompson7329a132013-08-16 15:57:37 -0700153 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 Thompson9915bdd2013-07-11 11:27:09 -0700159 }
160 else
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700161 return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_locator_type;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700162 }
163 }
164
Jeff Thompson94ddc272013-08-08 14:17:38 -0700165 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700166 return error;
167
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700168 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700169}
170
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700171ndn_Error ndn_decodeOptionalBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700172{
173 int gotExpectedTag;
174 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700175 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyLocator, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700176 return error;
177 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700178 if ((error = ndn_decodeBinaryXmlKeyLocator(keyLocator, decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700179 return error;
180 }
181 else
Jeff Thompson7329a132013-08-16 15:57:37 -0700182 ndn_KeyLocator_init(keyLocator, keyLocator->keyName.components, keyLocator->keyName.maxComponents);
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700183
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700184 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700185}