blob: 9672b3fc5e298312281a447b08a9ad1e33642166 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001/*
2 * @author: ucla-cs
3 * This class represents Signature Objects
4 */
5
6
Jeff Thompsone85ff1d2012-09-29 21:21:57 -07007var Signature = function Signature(_witness,_signature,_digestAlgorithm) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07008
Jeff Thompsone85ff1d2012-09-29 21:21:57 -07009 this.Witness = _witness;//byte [] _witness;
10 this.signature = _signature;//byte [] _signature;
11 this.digestAlgorithm = _digestAlgorithm//String _digestAlgorithm;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070012};
13
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070014var generateSignature = function(contentName,content,signedinfo){
15
16 var enc = new BinaryXMLEncoder();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070017 contentName.to_ccnb(enc);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070018 var hex1 = toHex(enc.getReducedOstream());
19
20 var enc = new BinaryXMLEncoder();
Jeff Thompson86aea882012-09-29 17:32:48 -070021 content.to_ccnb(enc);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070022 var hex2 = toHex(enc.getReducedOstream());
23
24 var enc = new BinaryXMLEncoder();
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070025 signedinfo.to_ccnb(enc);
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070026 var hex3 = toHex(enc.getReducedOstream());
27
28 var hex = hex1+hex2+hex3;
29
30 //globalKeyManager.sig
31
32};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070033
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070034Signature.prototype.from_ccnb =function( decoder) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070035 decoder.readStartElement(this.getElementLabel());
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070036
37 if(LOG>4)console.log('STARTED DECODING SIGNATURE ');
38
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070039 if (decoder.peekStartElement(CCNProtocolDTags.DigestAlgorithm)) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070040
41 if(LOG>4)console.log('DIGIEST ALGORITHM FOUND');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070042 this.digestAlgorithm = decoder.readUTF8Element(CCNProtocolDTags.DigestAlgorithm);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070043 }
44 if (decoder.peekStartElement(CCNProtocolDTags.Witness)) {
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070045 if(LOG>4)console.log('WITNESS FOUND FOUND');
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070046 this.Witness = decoder.readBinaryElement(CCNProtocolDTags.Witness);
47 }
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070048
49 //FORCE TO READ A SIGNATURE
50
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070051 //if(LOG>4)console.log('SIGNATURE FOUND ');
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070052 this.signature = decoder.readBinaryElement(CCNProtocolDTags.SignatureBits);
Meki Cherkaoui8f173612012-06-06 01:05:40 -070053 if(LOG>4)console.log('READ SIGNATURE ');
Meki Cherkaouif3d8f692012-05-18 15:44:28 -070054
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070055 decoder.readEndElement();
56
57};
58
59
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070060Signature.prototype.to_ccnb= function( encoder){
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070061
62 if (!this.validate()) {
63 throw new Exception("Cannot encode: field values missing.");
64 }
65
66 encoder.writeStartElement(this.getElementLabel());
67
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070068 if ((null != this.digestAlgorithm) && (!this.digestAlgorithm.equals(CCNDigestHelper.DEFAULT_DIGEST_ALGORITHM))) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070069 encoder.writeElement(CCNProtocolDTags.DigestAlgorithm, OIDLookup.getDigestOID(this.DigestAlgorithm));
70 }
71
72 if (null != this.Witness) {
73 // needs to handle null witness
74 encoder.writeElement(CCNProtocolDTags.Witness, this.Witness);
75 }
76
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070077 encoder.writeElement(CCNProtocolDTags.SignatureBits, this.signature);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070078
79 encoder.writeEndElement();
80};
81
82Signature.prototype.getElementLabel = function() { return CCNProtocolDTags.Signature; };
83
84
85Signature.prototype.validate = function() {
Jeff Thompsone85ff1d2012-09-29 21:21:57 -070086 return null != this.signature;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070087};
88