Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * @author: ucla-cs |
| 3 | * This class represents ContentObject Objects |
| 4 | */ |
| 5 | var ContentObject = function ContentObject(_Name,_SignedInfo,_Content,_Signature){ |
| 6 | |
| 7 | |
| 8 | if (typeof _Name === 'string'){ |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 9 | this.Name = new ContentName(_Name); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 10 | } |
| 11 | else{ |
| 12 | //TODO Check the class of _Name |
| 13 | this.Name = _Name; |
| 14 | } |
| 15 | this.SignedInfo = _SignedInfo; |
| 16 | this.Content=_Content; |
| 17 | this.Signature = _Signature; |
| 18 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 19 | |
| 20 | this.StartSIG = null; |
| 21 | this.EndSIG = null; |
| 22 | |
| 23 | this.StartSignedInfo = null; |
| 24 | this.EndContent = null; |
| 25 | |
| 26 | this.rawSignatureData = null; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 29 | ContentObject.prototype.sign = function(){ |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 30 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 31 | var n1 = this.encodeObject(this.Name); |
| 32 | var n2 = this.encodeObject(this.SignedInfo); |
| 33 | var n3 = this.encodeContent(); |
| 34 | |
| 35 | var n = n1.concat(n2,n3); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 36 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 37 | if(LOG>2)console.log('Signature Data is (binary) '+n); |
| 38 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 39 | if(LOG>2)console.log('Signature Data is (RawString)'); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 40 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 41 | if(LOG>2)console.log( DataUtils.toString(n) ); |
| 42 | |
| 43 | var sig = DataUtils.toString(n); |
| 44 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 45 | |
| 46 | var rsa = new RSAKey(); |
| 47 | |
| 48 | rsa.readPrivateKeyFromPEMString(globalKeyManager.privateKey); |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 49 | |
| 50 | //var hSig = rsa.signString(sig, "sha256"); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 51 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 52 | var hSig = rsa.signByteArrayWithSHA256(n); |
| 53 | |
| 54 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 55 | if(LOG>2)console.log('SIGNATURE SAVED IS'); |
| 56 | |
| 57 | if(LOG>2)console.log(hSig); |
| 58 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 59 | if(LOG>2)console.log( DataUtils.toNumbers(hSig.trim())); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 60 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 61 | this.Signature.Signature = DataUtils.toNumbers(hSig.trim()); |
| 62 | |
| 63 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | ContentObject.prototype.encodeObject = function encodeObject(obj){ |
| 67 | var enc = new BinaryXMLEncoder(); |
| 68 | |
| 69 | obj.encode(enc); |
| 70 | |
| 71 | var num = enc.getReducedOstream(); |
| 72 | |
| 73 | return num; |
| 74 | |
| 75 | |
| 76 | }; |
| 77 | |
| 78 | ContentObject.prototype.encodeContent = function encodeContent(obj){ |
| 79 | var enc = new BinaryXMLEncoder(); |
| 80 | |
| 81 | enc.writeElement(CCNProtocolDTags.Content, this.Content); |
| 82 | |
| 83 | var num = enc.getReducedOstream(); |
| 84 | |
| 85 | return num; |
| 86 | |
| 87 | |
| 88 | }; |
| 89 | |
| 90 | ContentObject.prototype.saveRawData = function(bytes){ |
| 91 | |
| 92 | var sigBits = bytes.slice(this.StartSIG, this.EndSIG ); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 93 | |
| 94 | this.rawSignatureData = sigBits; |
| 95 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 96 | |
| 97 | ContentObject.prototype.decode = function(/*XMLDecoder*/ decoder) { |
| 98 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 99 | // TODO VALIDATE THAT ALL FIELDS EXCEPT SIGNATURE ARE PRESENT |
| 100 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 101 | decoder.readStartElement(this.getElementLabel()); |
| 102 | |
Meki Cherkaoui | 8f17361 | 2012-06-06 01:05:40 -0700 | [diff] [blame^] | 103 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 104 | if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){ |
| 105 | this.Signature = new Signature(); |
| 106 | this.Signature.decode(decoder); |
| 107 | } |
| 108 | |
| 109 | //this.EndSIG = decoder.offset; |
| 110 | |
| 111 | this.StartSIG = decoder.offset; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 112 | |
| 113 | this.Name = new ContentName(); |
| 114 | this.Name.decode(decoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 115 | |
| 116 | //this.StartSignedInfo = decoder.offset; |
| 117 | |
| 118 | |
| 119 | if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){ |
| 120 | this.SignedInfo = new SignedInfo(); |
| 121 | this.SignedInfo.decode(decoder); |
| 122 | } |
| 123 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 124 | this.Content = decoder.readBinaryElement(CCNProtocolDTags.Content); |
| 125 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 126 | |
| 127 | //this.EndContent = decoder.offset; |
| 128 | this.EndSIG = decoder.offset; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 129 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 130 | |
| 131 | decoder.readEndElement(); |
| 132 | |
| 133 | this.saveRawData(decoder.istream); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | ContentObject.prototype.encode = function(/*XMLEncoder*/ encoder) { |
| 137 | |
| 138 | //TODO verify Name, SignedInfo and Signature is present |
| 139 | |
| 140 | |
| 141 | encoder.writeStartElement(this.getElementLabel()); |
| 142 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 143 | |
| 144 | |
| 145 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 146 | if(null!=this.Signature) this.Signature.encode(encoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 147 | |
| 148 | |
| 149 | this.StartSIG = encoder.offset; |
| 150 | |
| 151 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 152 | if(null!=this.Name) this.Name.encode(encoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 153 | |
| 154 | //this.EndSIG = encoder.offset; |
| 155 | //this.StartSignedInfo = encoder.offset; |
| 156 | |
| 157 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 158 | if(null!=this.SignedInfo) this.SignedInfo.encode(encoder); |
| 159 | |
| 160 | encoder.writeElement(CCNProtocolDTags.Content, this.Content); |
| 161 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 162 | |
| 163 | this.EndSIG = encoder.offset; |
| 164 | |
| 165 | //this.EndContent = encoder.offset; |
| 166 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 167 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 168 | encoder.writeEndElement(); |
| 169 | |
| 170 | this.saveRawData(encoder.ostream); |
| 171 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;}; |