blob: bc559116d504dacfc5af5f910746bed044879bc1 [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 Thompson3c166032013-11-20 15:12:19 -080011#include "binary-xml-name.h"
Jeff Thompson9915bdd2013-07-11 11:27:09 -070012
Jeff Thompson7329a132013-08-16 15:57:37 -070013static ndn_Error decodeKeyNameData(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
14{
15 int gotExpectedTag;
16 ndn_Error error;
17 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, &gotExpectedTag)))
18 return error;
19 if (gotExpectedTag) {
20 keyLocator->keyNameType = ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST;
21 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070022 (decoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, 0, &keyLocator->keyData)))
Jeff Thompson7329a132013-08-16 15:57:37 -070023 return error;
24 }
25 else {
26 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, &gotExpectedTag)))
27 return error;
28 if (gotExpectedTag) {
29 keyLocator->keyNameType = ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST;
30 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070031 (decoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, 0, &keyLocator->keyData)))
Jeff Thompson7329a132013-08-16 15:57:37 -070032 return error;
33 }
34 else {
35 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, &gotExpectedTag)))
36 return error;
37 if (gotExpectedTag) {
38 keyLocator->keyNameType = ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST;
39 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070040 (decoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, 0, &keyLocator->keyData)))
Jeff Thompson7329a132013-08-16 15:57:37 -070041 return error;
42 }
43 else {
44 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, &gotExpectedTag)))
45 return error;
46 if (gotExpectedTag) {
47 keyLocator->keyNameType = ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST;
48 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070049 (decoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, 0, &keyLocator->keyData)))
Jeff Thompson7329a132013-08-16 15:57:37 -070050 return error;
51 }
Jeff Thompson1cf72e92013-08-23 20:38:39 -070052 else {
53 // Key name data is omitted.
54 keyLocator->keyNameType = -1;
Jeff Thompson93034532013-10-08 11:52:43 -070055 keyLocator->keyData.length = 0;
Jeff Thompson1cf72e92013-08-23 20:38:39 -070056 }
Jeff Thompson7329a132013-08-16 15:57:37 -070057 }
58 }
59 }
60
61 return NDN_ERROR_success;
62}
63
Jeff Thompsonf0fea002013-07-30 17:22:42 -070064ndn_Error ndn_encodeBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -070065{
Jeff Thompson860b0ae2013-09-12 13:23:31 -070066 if ((int)keyLocator->type < 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070067 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -070068
69 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070070 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_KeyLocator)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070071 return error;
72
73 if (keyLocator->type == ndn_KeyLocatorType_KEY) {
Jeff Thompson93034532013-10-08 11:52:43 -070074 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Key, &keyLocator->keyData)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -070075 return error;
76 }
77 else if (keyLocator->type == ndn_KeyLocatorType_CERTIFICATE) {
Jeff Thompson93034532013-10-08 11:52:43 -070078 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Certificate, &keyLocator->keyData)))
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 Thompson93034532013-10-08 11:52:43 -070087 if ((int)keyLocator->keyNameType >= 0 && keyLocator->keyData.length > 0) {
Jeff Thompson1cf72e92013-08-23 20:38:39 -070088 if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_PUBLIC_KEY_DIGEST) {
89 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070090 (encoder, ndn_BinaryXml_DTag_PublisherPublicKeyDigest, &keyLocator->keyData)))
Jeff Thompson1cf72e92013-08-23 20:38:39 -070091 return error;
92 }
93 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_CERTIFICATE_DIGEST) {
94 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070095 (encoder, ndn_BinaryXml_DTag_PublisherCertificateDigest, &keyLocator->keyData)))
Jeff Thompson1cf72e92013-08-23 20:38:39 -070096 return error;
97 }
98 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_ISSUER_KEY_DIGEST) {
99 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700100 (encoder, ndn_BinaryXml_DTag_PublisherIssuerKeyDigest, &keyLocator->keyData)))
Jeff Thompson1cf72e92013-08-23 20:38:39 -0700101 return error;
102 }
103 else if (keyLocator->keyNameType == ndn_KeyNameType_PUBLISHER_ISSUER_CERTIFICATE_DIGEST) {
104 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700105 (encoder, ndn_BinaryXml_DTag_PublisherIssuerCertificateDigest, &keyLocator->keyData)))
Jeff Thompson1cf72e92013-08-23 20:38:39 -0700106 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 Thompson93034532013-10-08 11:52:43 -0700136 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Key, 0, &keyLocator->keyData)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700137 return error;
138 }
139 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700140 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Certificate, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700141 return error;
142 if (gotExpectedTag) {
143 keyLocator->type = ndn_KeyLocatorType_CERTIFICATE;
144
Jeff Thompson94ddc272013-08-08 14:17:38 -0700145 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700146 (decoder, ndn_BinaryXml_DTag_Certificate, 0, &keyLocator->keyData)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700147 return error;
148 }
149 else {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700150 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyName, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700151 return error;
152 if (gotExpectedTag) {
Jeff Thompson7329a132013-08-16 15:57:37 -0700153 keyLocator->type = ndn_KeyLocatorType_KEYNAME;
Jeff Thompson46c06512013-07-14 23:26:09 -0700154
Jeff Thompson7329a132013-08-16 15:57:37 -0700155 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_KeyName)))
Jeff Thompson46c06512013-07-14 23:26:09 -0700156 return error;
Jeff Thompson7329a132013-08-16 15:57:37 -0700157 if ((error = ndn_decodeBinaryXmlName(&keyLocator->keyName, decoder)))
158 return error;
159 if ((error = decodeKeyNameData(keyLocator, decoder)))
160 return error;
161 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
162 return error;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700163 }
164 else
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700165 return NDN_ERROR_decodeBinaryXmlKeyLocator_unrecognized_key_locator_type;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700166 }
167 }
168
Jeff Thompson94ddc272013-08-08 14:17:38 -0700169 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700170 return error;
171
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700172 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700173}
174
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700175ndn_Error ndn_decodeOptionalBinaryXmlKeyLocator(struct ndn_KeyLocator *keyLocator, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700176{
177 int gotExpectedTag;
178 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700179 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_KeyLocator, &gotExpectedTag)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700180 return error;
181 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700182 if ((error = ndn_decodeBinaryXmlKeyLocator(keyLocator, decoder)))
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700183 return error;
184 }
185 else
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700186 ndn_KeyLocator_initialize(keyLocator, keyLocator->keyName.components, keyLocator->keyName.maxComponents);
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700187
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700188 return NDN_ERROR_success;
Jeff Thompson9915bdd2013-07-11 11:27:09 -0700189}