blob: d9cce61480c65e33af31d2476f2731e4bf050138 [file] [log] [blame]
Jeff Thompson9915bdd2013-07-11 11:27:09 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson9915bdd2013-07-11 11:27:09 -07004 * Derived from Key.js by Meki Cheraoui.
5 * See COPYING for copyright and distribution information.
6 */
7
Jeff Thompson53412192013-08-06 13:35:50 -07008#include "binary-xml.h"
9#include "binary-xml-structure-decoder.h"
10#include "binary-xml-key.h"
Jeff Thompson9915bdd2013-07-11 11:27:09 -070011
Jeff Thompson7329a132013-08-16 15:57:37 -070012static 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 Thompson93034532013-10-08 11:52:43 -070021 (decoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, 0, &keyLocator->keyData)))
Jeff Thompson7329a132013-08-16 15:57:37 -070022 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 Thompson93034532013-10-08 11:52:43 -070030 (decoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, 0, &keyLocator->keyData)))
Jeff Thompson7329a132013-08-16 15:57:37 -070031 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 Thompson93034532013-10-08 11:52:43 -070039 (decoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, 0, &keyLocator->keyData)))
Jeff Thompson7329a132013-08-16 15:57:37 -070040 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 Thompson93034532013-10-08 11:52:43 -070048 (decoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, 0, &keyLocator->keyData)))
Jeff Thompson7329a132013-08-16 15:57:37 -070049 return error;
50 }
Jeff Thompson1cf72e92013-08-23 20:38:39 -070051 else {
52 // Key name data is omitted.
53 keyLocator->keyNameType = -1;
Jeff Thompson93034532013-10-08 11:52:43 -070054 keyLocator->keyData.length = 0;
Jeff Thompson1cf72e92013-08-23 20:38:39 -070055 }
Jeff Thompson7329a132013-08-16 15:57:37 -070056 }
57 }
58 }
59
60 return NDN_ERROR_success;
61}
62
Jeff Thompsonf0fea002013-07-30 17:22:42 -070063ndn_Error ndn_encodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -070064{
Jeff Thompson860b0ae2013-09-12 13:23:31 -070065 if ((int)keyLocator->type < 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070066 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -070067
68 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070069 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyLocator)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070070 return error;
71
72 if (keyLocator->type == ndn_KeyLocatorType_KEY) {
Jeff Thompson93034532013-10-08 11:52:43 -070073 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Key, &keyLocator->keyData)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070074 return error;
75 }
76 else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
Jeff Thompson93034532013-10-08 11:52:43 -070077 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Certificate, &keyLocator->keyData)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070078 return error;
79 }
80 else if (keyLocator->type == ndn_KeyLocatorType_KEYNAME) {
Jeff Thompson7329a132013-08-16 15:57:37 -070081 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 Thompson93034532013-10-08 11:52:43 -070086 if ((int)keyLocator->keyNameType >= 0 && keyLocator->keyData.length > 0) {
Jeff Thompson1cf72e92013-08-23 20:38:39 -070087 if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST) {
88 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070089 (encoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, &keyLocator->keyData)))
Jeff Thompson1cf72e92013-08-23 20:38:39 -070090 return error;
91 }
92 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST) {
93 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070094 (encoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, &keyLocator->keyData)))
Jeff Thompson1cf72e92013-08-23 20:38:39 -070095 return error;
96 }
97 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST) {
98 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070099 (encoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, &keyLocator->keyData)))
Jeff Thompson1cf72e92013-08-23 20:38:39 -0700100 return error;
101 }
102 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST) {
103 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700104 (encoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, &keyLocator->keyData)))
Jeff Thompson1cf72e92013-08-23 20:38:39 -0700105 return error;
106 }
107 else
108 return NDN_ERROR_unrecognized_ndn_KeyNameType;
Jeff Thompson7329a132013-08-16 15:57:37 -0700109 }
Jeff Thompson1cf72e92013-08-23 20:38:39 -0700110
Jeff Thompson7329a132013-08-16 15:57:37 -0700111 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
112 return error;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700113 }
114 else
115 return NDN_ERROR_unrecognized_ndn_KeyLocatorType;
116
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700117 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700118 return error;
119
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700120 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700121}
122
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700123ndn_Error ndn_decodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700124{
125 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700126 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyLocator)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700127 return error;
128
129 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700130 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Key, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700131 return error;
132 if (gotExpectedTag) {
133 keyLocator->type = ndn_KeyLocatorType_KEY;
134
Jeff Thompson93034532013-10-08 11:52:43 -0700135 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Key, 0, &keyLocator->keyData)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700136 return error;
137 }
138 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700139 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Certificate, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700140 return error;
141 if (gotExpectedTag) {
142 keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
143
Jeff Thompson94ddc272013-08-08 14:17:38 -0700144 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700145 (decoder, ndn_BinaryXml_DTag_Certificate, 0, &keyLocator->keyData)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700146 return error;
147 }
148 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700149 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyName, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700150 return error;
151 if (gotExpectedTag) {
Jeff Thompson7329a132013-08-16 15:57:37 -0700152 keyLocator->type = ndn_KeyLocatorType_KEYNAME;
Jeff Thompson46c06512013-07-14 23:26:09 -0700153
Jeff Thompson7329a132013-08-16 15:57:37 -0700154 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyName)))
Jeff Thompson46c06512013-07-14 23:26:09 -0700155 return error;
Jeff Thompson7329a132013-08-16 15:57:37 -0700156 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 Thompson9915bdd2013-07-11 11:27:09 -0700162 }
163 else
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700164 return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_locator_type;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700165 }
166 }
167
Jeff Thompson94ddc272013-08-08 14:17:38 -0700168 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700169 return error;
170
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700171 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700172}
173
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700174ndn_Error ndn_decodeOptionalBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700175{
176 int gotExpectedTag;
177 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700178 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyLocator, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700179 return error;
180 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700181 if ((error = ndn_decodeBinaryXmlKeyLocator(keyLocator, decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700182 return error;
183 }
184 else
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700185 ndn_KeyLocator_initialize(keyLocator, keyLocator->keyName.components, keyLocator->keyName.maxComponents);
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700186
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700187 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700188}