Пример #1
0
        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);

            }
Пример #2
0
        /// <summary>
        /// Create from a current personal profile.
        /// </summary>
        /// <param name="Data">The current profile to sign</param>
        /// <exception cref="Exception">The administration key could not be found.</exception>
        public SignedPersonalProfile(PersonalProfile Data) {

            // Locate the administration key for this device.
            var AdminKey = Data.GetAdministrationKey();

            Throw.If(AdminKey == null, "Device not authorized for administration.");

            // Make sure all the data is properly registered
            Data.Package();
            Identifier = Data.Identifier;

            //Goedel.Debug.Trace.WriteLine("Data to be signed {0}", Data.ToString());

            SignedData = new JoseWebSignature(Data.GetBytes(), AdminKey);
            _Signed = Data;
            }
Пример #3
0
 /// <summary>
 /// Construct a signed profile from the specified profile.
 /// </summary>
 /// <param name="Data">The profile to sign.</param>
 public SignedMasterProfile(MasterProfile Data) {
     Identifier = Data.Identifier;  // pass through
     SignedData = new JoseWebSignature(Data.GetBytes(), Data.MasterSignatureKey.KeyPair);
     _Signed = Data;
     }
Пример #4
0
 /// <summary>
 /// Sign the device profile.
 /// </summary>
 /// <param name="Data">The device profile to sign.</param>
 public void Sign(DeviceProfile Data) {
     Identifier = Data.Identifier;  // pass through
     var PrivateKey = Data.DeviceSignatureKey.PrivateKey;
     SignedData = new JoseWebSignature(Data.GetBytes(), PrivateKey);
     _Signed = Data;
     }
Пример #5
0
        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);

            }
Пример #6
0
        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);
            }
Пример #7
0
        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>
        /// Sign a connection request.
        /// </summary>
        /// <param name="ConnectionRequest">Connection request to sign.</param>
        public SignedConnectionRequest (ConnectionRequest ConnectionRequest) {
            var SignedDeviceProfile = ConnectionRequest.Device;
            var DeviceProfile = SignedDeviceProfile.Data;

            Identifier = DeviceProfile.UDF;

            var PrivateKey = DeviceProfile.DeviceSignatureKey.PrivateKey;
            SignedData = new JoseWebSignature(ConnectionRequest.GetBytes(), PrivateKey);
            }
        /// <summary>
        /// Create a signed connection result.
        /// </summary>
        /// <param name="ConnectionResult">Data to sign.</param>
        /// <param name="AdminKey">Signing key.</param>
        public SignedConnectionResult(ConnectionResult ConnectionResult,
                    KeyPair AdminKey) {
            var SignedDeviceProfile = ConnectionResult.Device;
            var DeviceProfile = SignedDeviceProfile.Data;

            Identifier = DeviceProfile.UDF;
            SignedData = new JoseWebSignature(ConnectionResult.GetBytes(), AdminKey);
            }
Пример #10
0
		/// <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 ();
            }
Пример #11
0
        /// <summary>
        /// Deserialize a tagged stream
        /// </summary>
        /// <param name="JSONReader"></param>
        public static new JoseWebSignature  FromTagged (JSONReader JSONReader) {
			JoseWebSignature Out = null;

			JSONReader.StartObject ();
            if (JSONReader.EOR) {
                return null;
                }

			string token = JSONReader.ReadToken ();

			switch (token) {

				case "JoseWebSignature" : {
					var Result = new JoseWebSignature ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}

				default : {
					//Ignore the unknown data
                    //throw new Exception ("Not supported");
                    break;
					}
				}
			JSONReader.EndObject ();

			return Out;
			}
Пример #12
0
        /// <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 "SignedData" : {
					// An untagged structure
					SignedData = new JoseWebSignature (JSONReader);
 
					break;
					}
				default : {
					base.DeserializeToken(JSONReader, Tag);
					break;
					}
				}
			// check up that all the required elements are present
			}