blob: a757d916868134f09b7905bbedc1232d19abd3bb [file] [log] [blame]
Wentao Shangbd63e462012-12-03 16:19:33 -08001/**
Jeff Thompson146d7de2012-11-17 16:15:28 -08002 * @author: Meki Cheraoui
Jeff Thompson745026e2012-10-13 12:49:20 -07003 * See COPYING for copyright and distribution information.
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07004 * This class represents Publisher and PublisherType Objects
5 */
6
Jeff Thompson2b14c7e2013-07-29 15:09:56 -07007/**
8 * @constructor
9 */
10var PublisherType = function PublisherType(tag){
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070011 this.KEY =(NDNProtocolDTags.PublisherPublicKeyDigest);
12 this.CERTIFICATE= (NDNProtocolDTags.PublisherCertificateDigest);
13 this.ISSUER_KEY= (NDNProtocolDTags.PublisherIssuerKeyDigest);
14 this.ISSUER_CERTIFICATE =(NDNProtocolDTags.PublisherIssuerCertificateDigest);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070015
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070016 this.Tag = tag;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070017};
18
19var isTypeTagVal = function(tagVal) {
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070020 if ((tagVal == NDNProtocolDTags.PublisherPublicKeyDigest) ||
21 (tagVal == NDNProtocolDTags.PublisherCertificateDigest) ||
22 (tagVal == NDNProtocolDTags.PublisherIssuerKeyDigest) ||
23 (tagVal == NDNProtocolDTags.PublisherIssuerCertificateDigest)) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070024 return true;
25 }
26 return false;
27};
28
Jeff Thompson2b14c7e2013-07-29 15:09:56 -070029/**
30 * @constructor
31 */
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070032var PublisherID = function PublisherID() {
33
34 this.PUBLISHER_ID_DIGEST_ALGORITHM = "SHA-256";
35 this.PUBLISHER_ID_LEN = 256/8;
36
37 //TODO, implement publisherID creation and key creation
38
39 //TODO implement generatePublicKeyDigest
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070040 this.publisherID =null;//= generatePublicKeyDigest(key);//ByteArray
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070041
42 //TODO implement generate key
43 //CryptoUtil.generateKeyID(PUBLISHER_ID_DIGEST_ALGORITHM, key);
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070044 this.publisherType = null;//isIssuer ? PublisherType.ISSUER_KEY : PublisherType.KEY;//publisher Type
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070045
46};
47
48
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070049PublisherID.prototype.from_ndnb = function(decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070050
51 // We have a choice here of one of 4 binary element types.
52 var nextTag = decoder.peekStartElementAsLong();
53
54 if (null == nextTag) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070055 throw new Error("Cannot parse publisher ID.");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070056 }
57
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070058 this.publisherType = new PublisherType(nextTag);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070059
60 if (!isTypeTagVal(nextTag)) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070061 throw new Error("Invalid publisher ID, got unexpected type: " + nextTag);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070062 }
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070063 this.publisherID = decoder.readBinaryElement(nextTag);
64 if (null == this.publisherID) {
Jeff Thompsoncab74c32012-10-21 13:27:28 -070065 throw new ContentDecodingException(new Error("Cannot parse publisher ID of type : " + nextTag + "."));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070066 }
67};
68
Alexander Afanasyev03d3f742013-08-14 17:47:28 -070069PublisherID.prototype.to_ndnb = function(encoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070070 if (!this.validate()) {
Jeff Thompson34a2ec02012-09-29 21:47:05 -070071 throw new Error("Cannot encode " + this.getClass().getName() + ": field values missing.");
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070072 }
73
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070074 encoder.writeElement(this.getElementLabel(), this.publisherID);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070075};
76
Jeff Thompson86aea882012-09-29 17:32:48 -070077PublisherID.peek = function(/* XMLDecoder */ decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070078
79 //Long
Jeff Thompson48ff28a2013-02-18 22:53:29 -080080 var nextTag = decoder.peekStartElementAsLong();
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070081
82 if (null == nextTag) {
83 // on end element
84 return false;
85 }
86 return (isTypeTagVal(nextTag));
87 };
88
89PublisherID.prototype.getElementLabel = function() {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070090 return this.publisherType.Tag;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070091};
92
93PublisherID.prototype.validate = function(){
94 return ((null != id() && (null != type())));
95};
96
97
98