static void Main(string[] args) { var TagDictionary = Constants.MakeDictionary(Constants.JoseTags); var ExamplePayloadBytes = UTF8Encoding.UTF8.GetBytes(ExamplePayload); var ExampleHeaderBytes = UTF8Encoding.UTF8.GetBytes(ExampleHeader); var Key = new Key(new JSONReader(ExampleKey)); var Header = new Header(new JSONReader(ExampleHeader)); //var Signed = Sign(ExampleHeaderBytes, ExamplePayloadBytes, Key); // Sign and serialize using JSON var JWS = new JoseWebSignature(Header, ExamplePayload); Sign(JWS, Key, ExampleHeader); byte[] JWSinJSON = JWS.ToJson(); Console.WriteLine("In JSON: {0} bytes", JWSinJSON.Length); Console.WriteLine(" Protected {0} / Payload {1} / Signature {2}", JWS.Protected.Length, JWS.Payload.Length, JWS.Signature.Length); // Sign and serialize using JSON-B SignB(JWS, Key); byte[] JWSinJSONB = JWS.ToJsonB(); Console.WriteLine("In JSON-B: {0} bytes", JWSinJSONB.Length); Console.WriteLine(" Protected {0} / Payload {1} / Signature {2}", JWS.Protected.Length, JWS.Payload.Length, JWS.Signature.Length); // Sign and serialize using JSON-C SignC(JWS, Key, TagDictionary); byte[] JWSinJSONC = JWS.ToJsonC(TagDictionary); Console.WriteLine("In JSON-C: {0} bytes", JWSinJSONC.Length); Console.WriteLine(" Protected {0} / Payload {1} / Signature {2}", JWS.Protected.Length, JWS.Payload.Length, JWS.Signature.Length); Dump("JSON-B", JWSinJSONB); Dump("JSON-C", JWSinJSONC); }
static void Sign(JoseWebSignature Signature, Key Key, string PHeader) { Signature.ProtectHeader(); var MAC = new HMACSHA256(Key.k); string Base64Protected = BaseConvert.ToBase64urlString( UTF8Encoding.UTF8.GetBytes(PHeader), false); byte[] ByteBase64Protected = UTF8Encoding.UTF8.GetBytes(Base64Protected); string Base64Payload = BaseConvert.ToBase64urlString( Signature.Payload, false); byte[] ByteBase64Payload = UTF8Encoding.UTF8.GetBytes(Base64Payload); var Stream = new MemoryStream(); Stream.Write(ByteBase64Protected, 0, ByteBase64Protected.Length); Stream.WriteByte((byte)'.'); Stream.Write(ByteBase64Payload, 0, ByteBase64Payload.Length); Stream.Position = 0; //var ByteStream = Stream.ToArray(); //for (int i = 0; i < ByteStream.Length; i++) { // Console.Write("{0}, ", ByteStream[i]); // } Signature.Signature = MAC.ComputeHash(Stream); }
static void SignB(JoseWebSignature Signature, Key Key) { Signature.ProtectHeaderB(); var Stream = new MemoryStream(); Stream.Write(Signature.Protected, 0, Signature.Protected.Length); Stream.WriteByte((byte)'.'); Stream.Write(Signature.Payload, 0, Signature.Payload.Length); var MAC = new HMACSHA256(Key.k); Signature.Signature = MAC.ComputeHash(Stream); }
static void SignC(JoseWebSignature Signature, Key Key, Dictionary<string, int> Dict) { Signature.ProtectHeaderC(Dict); var Stream = new MemoryStream(); Stream.Write(Signature.Protected, 0, Signature.Protected.Length); Stream.WriteByte((byte)'.'); Stream.Write(Signature.Payload, 0, Signature.Payload.Length); var MAC = new HMACSHA256(Key.k); Signature.Signature = MAC.ComputeHash(Stream); }
/// <summary> /// Construct an instance from the specified tagged JSONReader stream. /// </summary> public static void Deserialize(JSONReader JSONReader, out JSONObject Out) { JSONReader.StartObject (); if (JSONReader.EOR) { Out = null; return; } string token = JSONReader.ReadToken (); Out = null; switch (token) { case "JoseWebSignature" : { var Result = new JoseWebSignature (); Result.Deserialize (JSONReader); Out = Result; break; } case "JoseWebEncryption" : { var Result = new JoseWebEncryption (); Result.Deserialize (JSONReader); Out = Result; break; } case "Signed" : { var Result = new Signed (); Result.Deserialize (JSONReader); Out = Result; break; } case "Encrypted" : { var Result = new Encrypted (); Result.Deserialize (JSONReader); Out = Result; break; } case "KeyData" : { var Result = new KeyData (); Result.Deserialize (JSONReader); Out = Result; break; } case "Header" : { var Result = new Header (); Result.Deserialize (JSONReader); Out = Result; break; } case "Key" : { var Result = new Key (); Result.Deserialize (JSONReader); Out = Result; break; } case "Recipient" : { var Result = new Recipient (); Result.Deserialize (JSONReader); Out = Result; break; } case "PublicKeyRSA" : { var Result = new PublicKeyRSA (); Result.Deserialize (JSONReader); Out = Result; break; } case "PrivateKeyRSA" : { var Result = new PrivateKeyRSA (); Result.Deserialize (JSONReader); Out = Result; break; } default : { throw new Exception ("Not supported"); } } JSONReader.EndObject (); }
/// <summary> /// Deserialize a tagged stream /// </summary> /// <param name="JSONReader"></param> public static new Key FromTagged (JSONReader JSONReader) { Key Out = null; JSONReader.StartObject (); if (JSONReader.EOR) { return null; } string token = JSONReader.ReadToken (); switch (token) { case "Key" : { var Result = new Key (); Result.Deserialize (JSONReader); Out = Result; break; } case "PublicKeyRSA" : { var Result = new PublicKeyRSA (); Result.Deserialize (JSONReader); Out = Result; break; } case "PrivateKeyRSA" : { var Result = new PrivateKeyRSA (); Result.Deserialize (JSONReader); Out = Result; break; } default : { //Ignore the unknown data //throw new Exception ("Not supported"); break; } } JSONReader.EndObject (); return Out; }
/// <summary> /// Having read a tag, process the corresponding value data. /// </summary> /// <param name="JSONReader">The input stream</param> /// <param name="Tag">The tag</param> public override void DeserializeToken (JSONReader JSONReader, string Tag) { switch (Tag) { case "UDF" : { UDF = JSONReader.ReadString (); break; } case "X509Certificate" : { X509Certificate = JSONReader.ReadBinary (); break; } case "X509Chain" : { // Have a sequence of values bool _Going = JSONReader.StartArray (); X509Chain = new List <byte[]> (); while (_Going) { byte[] _Item = JSONReader.ReadBinary (); X509Chain.Add (_Item); _Going = JSONReader.NextArray (); } break; } case "X509CSR" : { X509CSR = JSONReader.ReadBinary (); break; } case "PublicParameters" : { PublicParameters = Key.FromTagged (JSONReader) ; // A tagged structure break; } case "PrivateParameters" : { PrivateParameters = Key.FromTagged (JSONReader) ; // A tagged structure break; } default : { break; } } // check up that all the required elements are present }
/// <summary> /// Having read a tag, process the corresponding value data. /// </summary> /// <param name="JSONReader">The input stream</param> /// <param name="Tag">The tag</param> public override void DeserializeToken (JSONReader JSONReader, string Tag) { switch (Tag) { case "PrivateKeys" : { // Have a sequence of values bool _Going = JSONReader.StartArray (); PrivateKeys = new List <Key> (); while (_Going) { // an untagged structure. var _Item = new Key (JSONReader); PrivateKeys.Add (_Item); _Going = JSONReader.NextArray (); } break; } default : { break; } } // check up that all the required elements are present }
/// <summary> /// Having read a tag, process the corresponding value data. /// </summary> /// <param name="JSONReader">The input stream</param> /// <param name="Tag">The tag</param> public override void DeserializeToken (JSONReader JSONReader, string Tag) { switch (Tag) { case "DeviceSignatureKey" : { // An untagged structure DeviceSignatureKey = new Key (JSONReader); break; } case "DeviceAuthenticationKey" : { // An untagged structure DeviceAuthenticationKey = new Key (JSONReader); break; } case "DeviceEncryptiontionKey" : { // An untagged structure DeviceEncryptiontionKey = new Key (JSONReader); break; } default : { break; } } // check up that all the required elements are present }