blob: 5ae3db050dbc51210665101ddf5f06db56e0299d [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
21 (decoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
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
30 (decoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
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
39 (decoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
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
48 (decoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
49 return error;
50 }
Jeff Thompson1cf72e92013-08-23 20:38:39 -070051 else {
52 // Key name data is omitted.
53 keyLocator->keyNameType = -1;
54 keyLocator->keyDataLength = 0;
55 }
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 Thompson94ddc272013-08-08 14:17:38 -070073 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -070074 (encoder, ndn_BinaryXml_DTag_Key, keyLocator->keyData, keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070075 return error;
76 }
77 else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070078 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -070079 (encoder, ndn_BinaryXml_DTag_Certificate, keyLocator->keyData, keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070080 return error;
81 }
82 else if (keyLocator->type == ndn_KeyLocatorType_KEYNAME) {
Jeff Thompson7329a132013-08-16 15:57:37 -070083 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyName)))
84 return error;
85 if ((error = ndn_encodeBinaryXmlName(&keyLocator->keyName, encoder)))
86 return error;
87
Jeff Thompson1cf72e92013-08-23 20:38:39 -070088 if ((int)keyLocator->keyNameType >= 0 && keyLocator->keyDataLength > 0) {
89 if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST) {
90 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
91 (encoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, keyLocator->keyData, keyLocator->keyDataLength)))
92 return error;
93 }
94 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST) {
95 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
96 (encoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, keyLocator->keyData, keyLocator->keyDataLength)))
97 return error;
98 }
99 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST) {
100 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
101 (encoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, keyLocator->keyData, keyLocator->keyDataLength)))
102 return error;
103 }
104 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST) {
105 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
106 (encoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, keyLocator->keyData, keyLocator->keyDataLength)))
107 return error;
108 }
109 else
110 return NDN_ERROR_unrecognized_ndn_KeyNameType;
Jeff Thompson7329a132013-08-16 15:57:37 -0700111 }
Jeff Thompson1cf72e92013-08-23 20:38:39 -0700112
Jeff Thompson7329a132013-08-16 15:57:37 -0700113 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
114 return error;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700115 }
116 else
117 return NDN_ERROR_unrecognized_ndn_KeyLocatorType;
118
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700119 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700120 return error;
121
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700122 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700123}
124
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700125ndn_Error ndn_decodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700126{
127 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700128 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyLocator)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700129 return error;
130
131 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700132 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Key, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700133 return error;
134 if (gotExpectedTag) {
135 keyLocator->type = ndn_KeyLocatorType_KEY;
136
Jeff Thompson94ddc272013-08-08 14:17:38 -0700137 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -0700138 (decoder, ndn_BinaryXml_DTag_Key, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700139 return error;
140 }
141 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700142 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Certificate, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700143 return error;
144 if (gotExpectedTag) {
145 keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
146
Jeff Thompson94ddc272013-08-08 14:17:38 -0700147 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson63d02692013-08-16 12:09:07 -0700148 (decoder, ndn_BinaryXml_DTag_Certificate, 0, &keyLocator->keyData, &keyLocator->keyDataLength)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700149 return error;
150 }
151 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700152 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyName, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700153 return error;
154 if (gotExpectedTag) {
Jeff Thompson7329a132013-08-16 15:57:37 -0700155 keyLocator->type = ndn_KeyLocatorType_KEYNAME;
Jeff Thompson46c06512013-07-14 23:26:09 -0700156
Jeff Thompson7329a132013-08-16 15:57:37 -0700157 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyName)))
Jeff Thompson46c06512013-07-14 23:26:09 -0700158 return error;
Jeff Thompson7329a132013-08-16 15:57:37 -0700159 if ((error = ndn_decodeBinaryXmlName(&keyLocator->keyName, decoder)))
160 return error;
161 if ((error = decodeKeyNameData(keyLocator, decoder)))
162 return error;
163 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
164 return error;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700165 }
166 else
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700167 return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_locator_type;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700168 }
169 }
170
Jeff Thompson94ddc272013-08-08 14:17:38 -0700171 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700172 return error;
173
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700174 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700175}
176
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700177ndn_Error ndn_decodeOptionalBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700178{
179 int gotExpectedTag;
180 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700181 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyLocator, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700182 return error;
183 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700184 if ((error = ndn_decodeBinaryXmlKeyLocator(keyLocator, decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700185 return error;
186 }
187 else
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700188 ndn_KeyLocator_initialize(keyLocator, keyLocator->keyName.components, keyLocator->keyName.maxComponents);
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700189
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700190 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700191}