Wentao Shang | bd63e46 | 2012-12-03 16:19:33 -0800 | [diff] [blame] | 1 | /** |
Jeff Thompson | 146d7de | 2012-11-17 16:15:28 -0800 | [diff] [blame] | 2 | * @author: Meki Cheraoui |
Jeff Thompson | 745026e | 2012-10-13 12:49:20 -0700 | [diff] [blame] | 3 | * See COPYING for copyright and distribution information. |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 4 | * This class represents Publisher and PublisherType Objects |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | var PublisherType = function PublisherType(_tag){ |
| 9 | this.KEY =(CCNProtocolDTags.PublisherPublicKeyDigest); |
| 10 | this.CERTIFICATE= (CCNProtocolDTags.PublisherCertificateDigest); |
| 11 | this.ISSUER_KEY= (CCNProtocolDTags.PublisherIssuerKeyDigest); |
| 12 | this.ISSUER_CERTIFICATE =(CCNProtocolDTags.PublisherIssuerCertificateDigest); |
| 13 | |
| 14 | this.Tag = _tag; |
| 15 | }; |
| 16 | |
| 17 | var isTypeTagVal = function(tagVal) { |
| 18 | if ((tagVal == CCNProtocolDTags.PublisherPublicKeyDigest) || |
| 19 | (tagVal == CCNProtocolDTags.PublisherCertificateDigest) || |
| 20 | (tagVal == CCNProtocolDTags.PublisherIssuerKeyDigest) || |
| 21 | (tagVal == CCNProtocolDTags.PublisherIssuerCertificateDigest)) { |
| 22 | return true; |
| 23 | } |
| 24 | return false; |
| 25 | }; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | var PublisherID = function PublisherID() { |
| 31 | |
| 32 | this.PUBLISHER_ID_DIGEST_ALGORITHM = "SHA-256"; |
| 33 | this.PUBLISHER_ID_LEN = 256/8; |
| 34 | |
| 35 | //TODO, implement publisherID creation and key creation |
| 36 | |
| 37 | //TODO implement generatePublicKeyDigest |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 38 | this.publisherID =null;//= generatePublicKeyDigest(key);//ByteArray |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 39 | |
| 40 | //TODO implement generate key |
| 41 | //CryptoUtil.generateKeyID(PUBLISHER_ID_DIGEST_ALGORITHM, key); |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 42 | this.publisherType = null;//isIssuer ? PublisherType.ISSUER_KEY : PublisherType.KEY;//publisher Type |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 43 | |
| 44 | }; |
| 45 | |
| 46 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 47 | PublisherID.prototype.from_ccnb = function(decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 48 | |
| 49 | // We have a choice here of one of 4 binary element types. |
| 50 | var nextTag = decoder.peekStartElementAsLong(); |
| 51 | |
| 52 | if (null == nextTag) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 53 | throw new Error("Cannot parse publisher ID."); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 56 | this.publisherType = new PublisherType(nextTag); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 57 | |
| 58 | if (!isTypeTagVal(nextTag)) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 59 | throw new Error("Invalid publisher ID, got unexpected type: " + nextTag); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 60 | } |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 61 | this.publisherID = decoder.readBinaryElement(nextTag); |
| 62 | if (null == this.publisherID) { |
Jeff Thompson | cab74c3 | 2012-10-21 13:27:28 -0700 | [diff] [blame] | 63 | throw new ContentDecodingException(new Error("Cannot parse publisher ID of type : " + nextTag + ".")); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 64 | } |
| 65 | }; |
| 66 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 67 | PublisherID.prototype.to_ccnb = function(encoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 68 | if (!this.validate()) { |
Jeff Thompson | 34a2ec0 | 2012-09-29 21:47:05 -0700 | [diff] [blame] | 69 | throw new Error("Cannot encode " + this.getClass().getName() + ": field values missing."); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 72 | encoder.writeElement(this.getElementLabel(), this.publisherID); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Jeff Thompson | 86aea88 | 2012-09-29 17:32:48 -0700 | [diff] [blame] | 75 | PublisherID.peek = function(/* XMLDecoder */ decoder) { |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 76 | |
| 77 | //Long |
Jeff Thompson | 48ff28a | 2013-02-18 22:53:29 -0800 | [diff] [blame] | 78 | var nextTag = decoder.peekStartElementAsLong(); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 79 | |
| 80 | if (null == nextTag) { |
| 81 | // on end element |
| 82 | return false; |
| 83 | } |
| 84 | return (isTypeTagVal(nextTag)); |
| 85 | }; |
| 86 | |
| 87 | PublisherID.prototype.getElementLabel = function() { |
Jeff Thompson | e85ff1d | 2012-09-29 21:21:57 -0700 | [diff] [blame] | 88 | return this.publisherType.Tag; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | PublisherID.prototype.validate = function(){ |
| 92 | return ((null != id() && (null != type()))); |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | |