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(){ |
| 30 | var n1 = this.encodeObject(this.Name); |
| 31 | var n2 = this.encodeObject(this.SignedInfo); |
| 32 | var n3 = this.encodeContent(); |
| 33 | |
| 34 | var n = n1.concat(n2,n3); |
| 35 | if(LOG>2)console.log('Signature Data is (binary) '+n); |
| 36 | |
| 37 | if(LOG>2)console.log('Signature Data is (RawString) '+ toString(n) ); |
| 38 | if(LOG>2)console.log(toString(n) ); |
| 39 | |
| 40 | |
| 41 | var rsa = new RSAKey(); |
| 42 | |
| 43 | rsa.readPrivateKeyFromPEMString(globalKeyManager.privateKey); |
| 44 | |
| 45 | var hSig = rsa.signString(toString(n), "sha256"); |
| 46 | |
| 47 | if(LOG>2)console.log('SIGNATURE SAVED IS'); |
| 48 | |
| 49 | if(LOG>2)console.log(hSig); |
| 50 | |
| 51 | if(LOG>2)console.log(toNumbers(hSig.trim())); |
| 52 | |
| 53 | this.Signature.Signature = toNumbers(hSig.trim()); |
| 54 | }; |
| 55 | |
| 56 | ContentObject.prototype.encodeObject = function encodeObject(obj){ |
| 57 | var enc = new BinaryXMLEncoder(); |
| 58 | |
| 59 | obj.encode(enc); |
| 60 | |
| 61 | var num = enc.getReducedOstream(); |
| 62 | |
| 63 | return num; |
| 64 | |
| 65 | |
| 66 | }; |
| 67 | |
| 68 | ContentObject.prototype.encodeContent = function encodeContent(obj){ |
| 69 | var enc = new BinaryXMLEncoder(); |
| 70 | |
| 71 | enc.writeElement(CCNProtocolDTags.Content, this.Content); |
| 72 | |
| 73 | var num = enc.getReducedOstream(); |
| 74 | |
| 75 | return num; |
| 76 | |
| 77 | |
| 78 | }; |
| 79 | |
| 80 | ContentObject.prototype.saveRawData = function(bytes){ |
| 81 | |
| 82 | var sigBits = bytes.slice(this.StartSIG, this.EndSIG ); |
| 83 | |
| 84 | //var sigIngoPlusContentBits = bytes.slice(this.StartSignedInfo, this.EndContent ); |
| 85 | |
| 86 | //var concat = sigBits.concat(sigIngoPlusContentBits); |
| 87 | |
| 88 | //this.rawSignatureData = concat; |
| 89 | |
| 90 | this.rawSignatureData = sigBits; |
| 91 | }; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 92 | |
| 93 | ContentObject.prototype.decode = function(/*XMLDecoder*/ decoder) { |
| 94 | |
| 95 | decoder.readStartElement(this.getElementLabel()); |
| 96 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 97 | |
| 98 | if( decoder.peekStartElement(CCNProtocolDTags.Signature) ){ |
| 99 | this.Signature = new Signature(); |
| 100 | this.Signature.decode(decoder); |
| 101 | } |
| 102 | |
| 103 | //this.EndSIG = decoder.offset; |
| 104 | |
| 105 | this.StartSIG = decoder.offset; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 106 | |
| 107 | this.Name = new ContentName(); |
| 108 | this.Name.decode(decoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 109 | |
| 110 | //this.StartSignedInfo = decoder.offset; |
| 111 | |
| 112 | |
| 113 | if( decoder.peekStartElement(CCNProtocolDTags.SignedInfo) ){ |
| 114 | this.SignedInfo = new SignedInfo(); |
| 115 | this.SignedInfo.decode(decoder); |
| 116 | } |
| 117 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 118 | this.Content = decoder.readBinaryElement(CCNProtocolDTags.Content); |
| 119 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 120 | |
| 121 | //this.EndContent = decoder.offset; |
| 122 | this.EndSIG = decoder.offset; |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 123 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 124 | |
| 125 | decoder.readEndElement(); |
| 126 | |
| 127 | this.saveRawData(decoder.istream); |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | ContentObject.prototype.encode = function(/*XMLEncoder*/ encoder) { |
| 131 | |
| 132 | //TODO verify Name, SignedInfo and Signature is present |
| 133 | |
| 134 | |
| 135 | encoder.writeStartElement(this.getElementLabel()); |
| 136 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 137 | |
| 138 | |
| 139 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 140 | if(null!=this.Signature) this.Signature.encode(encoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 141 | |
| 142 | |
| 143 | this.StartSIG = encoder.offset; |
| 144 | |
| 145 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 146 | if(null!=this.Name) this.Name.encode(encoder); |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 147 | |
| 148 | //this.EndSIG = encoder.offset; |
| 149 | //this.StartSignedInfo = encoder.offset; |
| 150 | |
| 151 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 152 | if(null!=this.SignedInfo) this.SignedInfo.encode(encoder); |
| 153 | |
| 154 | encoder.writeElement(CCNProtocolDTags.Content, this.Content); |
| 155 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 156 | |
| 157 | this.EndSIG = encoder.offset; |
| 158 | |
| 159 | //this.EndContent = encoder.offset; |
| 160 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 161 | |
Meki Cherkaoui | f3d8f69 | 2012-05-18 15:44:28 -0700 | [diff] [blame] | 162 | encoder.writeEndElement(); |
| 163 | |
| 164 | this.saveRawData(encoder.ostream); |
| 165 | |
Meki Cherkaoui | f441d3a | 2012-04-22 15:17:52 -0700 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | ContentObject.prototype.getElementLabel= function(){return CCNProtocolDTags.ContentObject;}; |