/// <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 "EmailAddress" : {
					EmailAddress = JSONReader.ReadString ();
					break;
					}
				case "ReplyToAddress" : {
					ReplyToAddress = JSONReader.ReadString ();
					break;
					}
				case "DisplayName" : {
					DisplayName = JSONReader.ReadString ();
					break;
					}
				case "AccountName" : {
					AccountName = JSONReader.ReadString ();
					break;
					}
				case "Inbound" : {
					// Have a sequence of values
					bool _Going = JSONReader.StartArray ();
					Inbound = new List <Connection> ();
					while (_Going) {
						// an untagged structure.
						var _Item = new Connection (JSONReader);
						Inbound.Add (_Item);
						_Going = JSONReader.NextArray ();
						}
					break;
					}
				case "Outbound" : {
					// Have a sequence of values
					bool _Going = JSONReader.StartArray ();
					Outbound = new List <Connection> ();
					while (_Going) {
						// an untagged structure.
						var _Item = new Connection (JSONReader);
						Outbound.Add (_Item);
						_Going = JSONReader.NextArray ();
						}
					break;
					}
				case "Sign" : {
					// Have a sequence of values
					bool _Going = JSONReader.StartArray ();
					Sign = new List <PublicKey> ();
					while (_Going) {
						// an untagged structure.
						var _Item = new PublicKey (JSONReader);
						Sign.Add (_Item);
						_Going = JSONReader.NextArray ();
						}
					break;
					}
				case "Encrypt" : {
					// Have a sequence of values
					bool _Going = JSONReader.StartArray ();
					Encrypt = new List <PublicKey> ();
					while (_Going) {
						// an untagged structure.
						var _Item = new PublicKey (JSONReader);
						Encrypt.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 "UDF" : {
					UDF = JSONReader.ReadString ();
					break;
					}
				case "UserKeys" : {
					// Have a sequence of values
					bool _Going = JSONReader.StartArray ();
					UserKeys = new List <PublicKey> ();
					while (_Going) {
						// an untagged structure.
						var _Item = new PublicKey (JSONReader);
						UserKeys.Add (_Item);
						_Going = JSONReader.NextArray ();
						}
					break;
					}
				default : {
					base.DeserializeToken(JSONReader, Tag);
					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 "EncryptionPGP" : {
					// An untagged structure
					EncryptionPGP = new PublicKey (JSONReader);
 
					break;
					}
				case "EncryptionSMIME" : {
					// An untagged structure
					EncryptionSMIME = new PublicKey (JSONReader);
 
					break;
					}
				default : {
					base.DeserializeToken(JSONReader, Tag);
					break;
					}
				}
			// check up that all the required elements are present
			}
        /// <summary>
        /// Generate S/MIME key pairs and register in the correct Windows stores.
        /// </summary>
        /// <returns>The generated key pair data.</returns>
        public virtual bool GenerateSMIME () {

            // Much of this should probably be turned into convenience methods on the PublicKey class.

            var RootKey = PublicKey.Generate(KeyType.ASK,
                                CryptoCatalog.Default.AlgorithmSignature);

            //var NewCert = new Certificate(RootKey.KeyPair, 
            //    Application.PersonalMaster | Application.CA |
            //    Application.CodeSigning | Application.TimeStamping |
            //    Application.ServerAuth | Application.ClientAuth,
            //    EmailAddress, EmailAddress);
            //NewCert.TBSCertificate.SetValidity(20);

            //NewCert.Sign(Signer.Certificate);



            RootKey.SignCertificate(Application.PersonalMaster | Application.CA, 
                    EmailAddress, RootKey);
            var RootKeyCertificate = RootKey.Certificate.Data;

            var SignKey = PublicKey.Generate(KeyType.ASK,
                                CryptoCatalog.Default.AlgorithmSignature);
            SignKey.SignCertificate(Application.EmailSignature |
                        Application.DataSignature, EmailAddress, RootKey);
            SignKey.X509Chain = new List<byte[]>();
            SignKey.X509Chain.Add(RootKeyCertificate);


            var EncryptKey = PublicKey.Generate(KeyType.AEK, 
                                CryptoCatalog.Default.AlgorithmExchange);
            EncryptKey.SignCertificate(Application.EmailEncryption |
                        Application.DataEncryption, EmailAddress, RootKey);
            EncryptKey.X509Chain = new List<byte[]>();
            EncryptKey.X509Chain.Add(RootKeyCertificate);


            //var SigningCSR = new CertificationRequest(SignKey.Certificate);
            //SignKey.X509CSR = SigningCSR.DER();

            //var EncryptionCSR = new CertificationRequest(EncryptKey.Certificate);
            //EncryptKey.X509CSR = EncryptionCSR.DER();

            CertificateStore.RegisterTrustedRoot(RootKey.Certificate);
            CertificateStore.Register(SignKey.Certificate);
            CertificateStore.Register(EncryptKey.Certificate);

            CertificateSign = SignKey;
            CertificateEncrypt = EncryptKey;

            if (Encrypt == null) {
                Encrypt = new List<PublicKey>();
                }
            if (Sign == null) {
                Sign = new List<PublicKey>();
                }

            Encrypt.Add(EncryptKey);
            Sign.Add(SignKey);


            return false;
            }
		/// <summary>
        /// Construct an instance from the specified tagged JSONReader stream.
        /// </summary>
        /// <param name="JSONReader">Input stream</param>
        /// <param name="Out">The created object</param>
        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 "PublicKey" : {
					var Result = new PublicKey ();
					Result.Deserialize (JSONReader);
					Out = Result;
					break;
					}


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


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


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


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


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


				case "Profile" : {
					Out = null;
					throw new Exception ("Can't create abstract type");
					}


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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

				default : {
					throw new Exception ("Not supported");
					}
				}	
			JSONReader.EndObject ();
            }
        /// <summary>
        /// Deserialize a tagged stream
        /// </summary>
        /// <param name="JSONReader">The input stream</param>
        /// <returns>The created object.</returns>		
        public static new PublicKey  FromTagged (JSONReader JSONReader) {
			PublicKey Out = null;

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

			string token = JSONReader.ReadToken ();

			switch (token) {

				case "PublicKey" : {
					var Result = new PublicKey ();
					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 "Description" : {
					Description = JSONReader.ReadString ();
					break;
					}
				case "DeviceSignatureKey" : {
					// An untagged structure
					DeviceSignatureKey = new PublicKey (JSONReader);
 
					break;
					}
				case "DeviceAuthenticationKey" : {
					// An untagged structure
					DeviceAuthenticationKey = new PublicKey (JSONReader);
 
					break;
					}
				case "DeviceEncryptiontionKey" : {
					// An untagged structure
					DeviceEncryptiontionKey = new PublicKey (JSONReader);
 
					break;
					}
				default : {
					base.DeserializeToken(JSONReader, Tag);
					break;
					}
				}
			// check up that all the required elements are present
			}