using System; using System.Linq; using System.Reflection; using System.Security.Cryptography.X509Certificates; using System.Xml.Linq; using dk.nsi.seal.dgwstypes; using dk.nsi.seal.Model; using dk.nsi.seal.pki; using System.Xml.Serialization; using System.Xml; using System.Xml.Schema; using dk.nsi.seal.Constants; using dk.nsi.seal.Model.Constants; using dk.nsi.seal.Model.ModelBuilders; namespace dk.nsi.seal { public abstract class IdCard : IXmlSerializable { protected static string CREATED; public XElement Xassertion { get; set; } public const string IDCARDTYPE_SYSTEM = "system"; public const string IDCARDTYPE_USER = "user"; private const int IDCARD_BEGIN_TIME_BUFFER_IN_MINUTES = 5; private const int MAX_IDCARD_LIFE_IN_HOURS = 24; protected string LastDomOperation { get; set; } protected bool NeedsSignature { get; set; } protected static string RE_ASSIGNED = "NodeReAssignedToNewDocument"; protected static string SIGNED = "SignatureCreated"; public string AlternativeIdentifier { get; protected set; } public string CertHash { get; protected set; } public DateTime CreatedDate { get; protected set; } public DateTime ExpiryDate { get; protected set; } public string IdCardId { get; protected set; } public string Issuer { get; protected set; } public string Password { get; protected set; } public string Username { get; protected set; } public X509Certificate2 SignedByCertificate => Xassertion == null ? null : SealUtilities.GetAssertionSignature(Xassertion); public string Version { get; protected set; } public AuthenticationLevel AuthenticationLevel { get; protected set; } public bool IsValidInTime { get { DateTime now = DateTime.Now; return now.Equals(CreatedDate) || (now > CreatedDate && now < ExpiryDate); } } public static string SealLibraryVersion => "SealNet-" + Assembly.GetExecutingAssembly().GetName().Version.ToString(3); /// /// Empty constructor ONLY for Serialization/Deserialization /// protected IdCard() { } protected IdCard(IdCard toCopy, string issuer, string certHash, string alternativeIdentifier, AuthenticationLevel authenticationLevel) { AlternativeIdentifier = alternativeIdentifier; AuthenticationLevel = authenticationLevel; CertHash = certHash; CreatedDate = DateTime.Now; ExpiryDate = toCopy.ExpiryDate; Issuer = SealLibraryVersion + " " + issuer; Password = toCopy.Password; Username = toCopy.Username; Version = toCopy.Version; IdCardId = Guid.NewGuid().ToString("D"); } protected IdCard(string version, XElement xAssertion, string cardId, AuthenticationLevel authLevel, string certHash, string issuer, DateTime creationDate, DateTime expiryDate, string alternativeIdentifier, string username, string password) { ModelUtilities.ValidateNotNull(cardId, "IDCard ID cannot be 'null'"); ModelUtilities.ValidateNotEmpty(issuer, "'Issuer' cannot be null or empty"); ModelUtilities.ValidateNotNull(authLevel, "'AuthenticationLevel' cannot be null"); this.Version = version; this.CreatedDate = creationDate; this.ExpiryDate = expiryDate; this.Issuer = SealLibraryVersion + " " + issuer; this.AuthenticationLevel = authLevel; if (AuthenticationLevel.MocesTrustedUser.Equals(authLevel) || AuthenticationLevel.VocesTrustedSystem.Equals(authLevel)) { this.CertHash = certHash; } this.Xassertion = xAssertion; this.AlternativeIdentifier = alternativeIdentifier; if (AuthenticationLevel.UsernamePasswordAuthentication.Equals(authLevel)) { ModelUtilities.ValidateNotEmpty(username, "'username' cannot be null or empty for authenticationlevel 2"); ModelUtilities.ValidateNotEmpty(password, "'password' cannot be null or empty for authenticationlevel 2"); this.Username = username; this.Password = password; } // This is an invariant! When the IDCard is created from deserialization, // the ID card is already signed => needsSignature=false NeedsSignature = (xAssertion == null); IdCardId = cardId; } protected IdCard(string version, AuthenticationLevel authLevel, string issuer, string certHash, string alternativeIdentifier, string username, string password) { ModelUtilities.ValidateNotEmpty(issuer, "'Issuer' cannot be null or empty"); ModelUtilities.ValidateNotNull(authLevel, "'AuthenticationLevel' cannot be null"); Version = version; CreatedDate = DateTime.Now .ToUniversalTime() .AddMinutes(-IDCARD_BEGIN_TIME_BUFFER_IN_MINUTES) .ToLocalTime(); ExpiryDate = CreatedDate .ToUniversalTime() .AddHours(MAX_IDCARD_LIFE_IN_HOURS) .ToLocalTime(); Issuer = SealLibraryVersion + " " + issuer; AuthenticationLevel = authLevel; if (AuthenticationLevel.MocesTrustedUser.Equals(authLevel) || AuthenticationLevel.VocesTrustedSystem.Equals(authLevel)) { CertHash = certHash ?? ""; } AlternativeIdentifier = alternativeIdentifier; if (AuthenticationLevel.UsernamePasswordAuthentication.Equals(authLevel)) { ModelUtilities.ValidateNotEmpty(username, "'username' cannot be null or empty for authenticationlevel 2"); ModelUtilities.ValidateNotEmpty(password, "'password' cannot be null or empty for authenticationlevel 2"); Username = username; Password = password; } NeedsSignature = (Xassertion == null); IdCardId = Guid.NewGuid().ToString("D"); } protected IdCard(string version, XElement domElement, string cardId, AuthenticationLevel authenticationLevel, string issuer, SystemInfo systemInfo, string certHash, string alternativeIdentifier, string userName, string password) { ModelUtilities.ValidateNotEmpty(issuer, "'Issuer' cannot be null or empty"); ModelUtilities.ValidateNotNull(authenticationLevel, "'AuthenticationLevel' cannot be null"); Version = version; CreatedDate = DateTime.Now .ToUniversalTime() .AddMinutes(-IDCARD_BEGIN_TIME_BUFFER_IN_MINUTES) .ToLocalTime(); ExpiryDate = CreatedDate .ToUniversalTime() .AddHours(MAX_IDCARD_LIFE_IN_HOURS) .ToLocalTime(); Issuer = SealLibraryVersion + " " + issuer; AuthenticationLevel = authenticationLevel; if (AuthenticationLevel.MocesTrustedUser.Equals(authenticationLevel) || AuthenticationLevel.VocesTrustedSystem.Equals(authenticationLevel)) { CertHash = certHash ?? ""; } AlternativeIdentifier = alternativeIdentifier; if (AuthenticationLevel.UsernamePasswordAuthentication.Equals(authenticationLevel)) { ModelUtilities.ValidateNotEmpty(userName, "'username' cannot be null or empty for authenticationlevel 2"); ModelUtilities.ValidateNotEmpty(password, "'password' cannot be null or empty for authenticationlevel 2"); Username = userName; Password = password; } Xassertion = domElement; NeedsSignature = Xassertion == null; IdCardId = cardId; } public T Sign(ISignatureProvider provider) where T : class { if (NeedsSignature) { //Validate IdCard before starting to generate Assertion new IdCardValidator().ValidateIdCard(this); //Generate unsigned assertion here based on IdCard, and sign it Xassertion = provider.Sign(GenerateAssertion()); LastDomOperation = SIGNED; NeedsSignature = false; } if (Xassertion == null) { Xassertion = SerializerUtil.Serialize(GenerateAssertion()).Root; } return SerializerUtil.Deserialize(Xassertion, nameof(Assertion)); } public T GetAssertion() where T : class => GetAssertion(nameof(Assertion)); public T GetAssertion(string rootId) where T : class { if (Xassertion == null) { Xassertion = SerializerUtil.Serialize(GenerateAssertion()).Root; } return SerializerUtil.Deserialize(Xassertion, rootId); } protected abstract Assertion GenerateAssertion(); public void ValidateSignature() => InternalValidateSignature(null, Configuration.CheckTrust); public void ValidateSignatureAndTrust(Federation.Federation federation) => InternalValidateSignature(federation, Configuration.CheckTrust); [Obsolete("Use ValidateSignature instead.")] public void ValidateSignatureAndTrust() => InternalValidateSignature(null, Configuration.CheckTrust); public void InternalValidateSignature(Federation.Federation federation, bool checkTrust) { if (AuthenticationLevel.Level < AuthenticationLevel.VocesTrustedSystem.Level) { throw new ModelException("AuthenticationLevel does not support signature"); } if (Xassertion == null) { throw new ModelException("Assertion not initialized"); } if (!SealUtilities.CheckAssertionSignature(Xassertion)) { throw new ModelException("IDCard is not signed!"); } //Check that Signature is in credentialVault and that no certificate in chain is revoked if (checkTrust && !SignatureUtil.Validate(Xassertion, federation, checkTrust, Configuration.CheckCrl, Configuration.CheckDate)) { throw new ModelException("Signature on IdCard could not be validated"); } } public XmlSchema GetSchema() => null; public virtual void ReadXml(XmlReader reader) { reader.MoveToContent(); reader.ReadStartElement(); { LastDomOperation = reader.GetAttribute("LastDomOperation"); string needsSignatureAttribute = reader.GetAttribute("NeedsSignature"); if (!string.IsNullOrEmpty(needsSignatureAttribute)) NeedsSignature = bool.Parse(needsSignatureAttribute); AlternativeIdentifier = reader.GetAttribute("AlternativeIdentifier"); CertHash = reader.GetAttribute("CertHash"); IdCardId = reader.GetAttribute("IdCardId"); Issuer = reader.GetAttribute("Issuer"); Password = reader.GetAttribute("Password"); Username = reader.GetAttribute("Username"); Version = reader.GetAttribute("Version"); var authenticationLevelAttribute = reader.GetAttribute("AuthenticationLevel"); if (!string.IsNullOrEmpty(authenticationLevelAttribute)) AuthenticationLevel = AuthenticationLevel.GetEnumeratedValue(int.Parse(authenticationLevelAttribute)); while (reader.Read()) { if (reader.IsStartElement()) { if (reader.Name == "CreatedDate") { reader.ReadStartElement(); this.CreatedDate = (DateTime)new XmlSerializer(typeof(DateTime)).Deserialize(reader); reader.ReadEndElement(); } if (reader.Name == "ExpiryDate") { reader.ReadStartElement(); this.ExpiryDate = (DateTime)new XmlSerializer(typeof(DateTime)).Deserialize(reader); reader.ReadEndElement(); } if (reader.Name == "Xassertion") { reader.ReadStartElement(); this.Xassertion = XElement.Parse(reader.ReadOuterXml(), LoadOptions.PreserveWhitespace); } } } } } public virtual void WriteXml(XmlWriter writer) { writer.WriteStartElement("UserIdCard"); { writer.WriteAttributeString("LastDomOperation", LastDomOperation); writer.WriteAttributeString("NeedsSignature", NeedsSignature.ToString()); writer.WriteAttributeString("AlternativeIdentifier", AlternativeIdentifier); writer.WriteAttributeString("CertHash", CertHash); writer.WriteAttributeString("IdCardId", IdCardId); writer.WriteAttributeString("Issuer", Issuer); writer.WriteAttributeString("Password", Password); writer.WriteAttributeString("Username", Username); writer.WriteAttributeString("Version", Version); writer.WriteAttributeString("AuthenticationLevel", AuthenticationLevel.Level.ToString()); writer.WriteStartElement("CreatedDate"); new XmlSerializer(typeof(DateTime)).Serialize(writer, this.CreatedDate); writer.WriteEndElement(); writer.WriteStartElement("ExpiryDate"); new XmlSerializer(typeof(DateTime)).Serialize(writer, this.ExpiryDate); writer.WriteEndElement(); if (Xassertion != null) { writer.WriteStartElement("Xassertion"); Xassertion.WriteTo(writer); writer.WriteEndElement(); } } writer.WriteEndElement(); } public static IdCard From(XElement idCardElement) { IdCard result; string itSystemName = null, ocesCertHash = null, id = null, version = null, cpr = null, givenName = null, surName = null, email = null, occupation = null, userRole = null, authorizationCode = null, careProviderId = null, careProviderIdType = null, careProviderName = null, authLevel = null, alternativeIdentifier = null, username = null, password = null; bool hasIdCardData = false, hasSystemLog = false, hasUserLog = false; DateTime createdDate = new DateTime(), expiryDate = new DateTime(); var timeConstraints = idCardElement.Descendants("{" + SamlTags.Conditions.Ns + "}" + SamlTags.Conditions.TagName); var conditionsAttributes = timeConstraints.Attributes(); try { foreach (var attribute in conditionsAttributes) { var attributeValue = attribute.Value; var attributeName = attribute.Name; if (SamlAttributes.NotOnOrAfter == attributeName) { expiryDate = DateTime.Parse(attributeValue); } else if (SamlAttributes.NotBefore == attributeName) { createdDate = DateTime.Parse(attributeValue); } } } catch (Exception e) { throw new ModelBuildException("SAML:Conditions could not be parsed", e); } var subjectNameIdNode = idCardElement.Descendants("{" + SamlTags.NameID.Ns + "}" + SamlTags.NameID.TagName).FirstOrDefault(); var nameIdFormatNode = subjectNameIdNode.Attribute((SamlAttributes.Format)); if (nameIdFormatNode.Value.Equals(SubjectIdentifierTypeValues.Other)) { alternativeIdentifier = subjectNameIdNode.Value; } var issuerNode = idCardElement.Descendants("{" + SamlTags.Issuer.Ns + "}" + SamlTags.Issuer.TagName) .FirstOrDefault(); var issuer = issuerNode.Value.Replace("_x0020_", " "); var attributeStatementNodeList = idCardElement.Descendants("{" + SamlTags.AttributeStatement.Ns + "}" + SamlTags.AttributeStatement.TagName); bool? isUserIDCard = null; foreach (var attributeStatement in attributeStatementNodeList) { var map = attributeStatement.Attributes(); foreach (var attribute in map) { var attributeValue = attribute.Value; if (IdValues.SystemLog.Equals(attributeValue)) { var samlAttributeNodes = attributeStatement.Descendants("{" + SamlTags.Attribute.Ns + "}" + SamlTags.Attribute.TagName); foreach (var samlAttribute in samlAttributeNodes) { var attributeName = samlAttribute.Attribute("Name").Value; var attributeNameValue = GetAttributeNameValue(samlAttribute, attributeName); if (MedComAttributes.ItSystemName.Equals(attributeName)) { itSystemName = attributeNameValue; } else if (MedComAttributes.CareProviderId.Equals(attributeName)) { careProviderId = attributeNameValue; var nameFormatAttribute = samlAttribute.Attribute(SamlAttributes.NameFormat); if (nameFormatAttribute == null) { throw new ModelBuildException( "DGWS violation: 'medcom:CareProviderID' SAML attribute must contain a 'NameFormat' attribute!"); } careProviderIdType = nameFormatAttribute.Value; } else if (MedComAttributes.CareProviderName.Equals(attributeName)) { careProviderName = attributeNameValue; } } hasSystemLog = true; } else if (IdValues.IdCardData.Equals(attributeValue)) { var samlAttributeNodes = attributeStatement.Descendants("{" + SamlTags.Attribute.Ns + "}" + SamlTags.Attribute.TagName); foreach (var samlAttribute in samlAttributeNodes) { var attributeName = samlAttribute.Attribute("Name").Value; var attributeNameValue = GetAttributeNameValue(samlAttribute, attributeName); if (SosiAttributes.OcesCertHash.Equals(attributeName)) { ocesCertHash = attributeNameValue; } else if (SosiAttributes.IDCardID.Equals(attributeName)) { id = attributeNameValue; } else if (SosiAttributes.IDCardVersion.Equals(attributeName)) { version = attributeNameValue; } else if (SosiAttributes.IDCardType.Equals(attributeName)) { if (IDCARDTYPE_USER.Equals(attributeNameValue)) isUserIDCard = true; else if (IDCARDTYPE_SYSTEM.Equals(attributeNameValue)) isUserIDCard = false; } else if (SosiAttributes.AuthenticationLevel.Equals(attributeName)) { authLevel = attributeNameValue; } } hasIdCardData = true; } else if (IdValues.UserLog.Equals(attributeValue)) { var samlAttributeNodes = attributeStatement.Descendants("{" + SamlTags.Attribute.Ns + "}" + SamlTags.Attribute.TagName); foreach (var samlAttribute in samlAttributeNodes) { var attributeName = samlAttribute.Attribute("Name").Value; var attributeNameValue = GetAttributeNameValue(samlAttribute, attributeName); if (MedComAttributes.UserCivilRegistrationNumber.Equals(attributeName)) { cpr = attributeNameValue; } else if (MedComAttributes.UserGivenName.Equals(attributeName)) { givenName = attributeNameValue; } else if (MedComAttributes.UserSurname.Equals(attributeName)) { surName = attributeNameValue; } else if (MedComAttributes.UserEmailAddress.Equals(attributeName)) { email = attributeNameValue; } else if (MedComAttributes.UserOccupation.Equals(attributeName)) { occupation = attributeNameValue; } else if (MedComAttributes.UserRole.Equals(attributeName)) { userRole = attributeNameValue; } else if (MedComAttributes.UserAuthorizationCode.Equals(attributeName)) { authorizationCode = attributeNameValue; } } hasUserLog = true; } } } Enum.TryParse(careProviderIdType.Replace(":", ""), true, out SubjectIdentifierType careProviderIdEnum); CareProvider careProvider = new CareProvider(careProviderIdEnum, careProviderId, careProviderName); SystemInfo systemInfo = new SystemInfo(careProvider, itSystemName); // All IDCard types must have a IDCardData element if (!hasIdCardData) throw new ModelBuildException("IDCardData element missing for IDCard"); // All IDCard types must have a SystemLog element if (!hasSystemLog) throw new ModelBuildException("SystemLog element missing for IDCard"); if (isUserIDCard == null) throw new ModelBuildException("ID Card type not found or invalid"); if (isUserIDCard.Value) { if (!hasUserLog) throw new ModelBuildException("UserLog element missing for UserIDCard"); UserInfo userInfo = new UserInfo(cpr, givenName, surName, email, occupation, userRole, authorizationCode); result = new UserIdCard(version, idCardElement, id, AuthenticationLevel.GetEnumeratedValue(int.Parse(authLevel)), ocesCertHash, issuer, systemInfo, userInfo, createdDate, expiryDate, alternativeIdentifier, username, password); } else { if (hasUserLog) throw new ModelBuildException("IDCard type is 'system', but also has a UserLog element (??)"); result = new SystemIdCard(version, idCardElement, id, AuthenticationLevel.GetEnumeratedValue(int.Parse(authLevel)), ocesCertHash, issuer, systemInfo, createdDate, expiryDate, alternativeIdentifier, username, password); } result.Issuer = issuer; return result; } public static IdCard From(XDocument doc) { var assertions = doc.Elements(NameSpaces.saml + SamlTags.Assertion); foreach (var assertion in assertions) { foreach (var xAttribute in assertion.Attributes()) { if (xAttribute.Name.LocalName.ToLower().EndsWith("id") && xAttribute.Value.Equals(IdValues.IdCard)) { return From(assertion); } } } return null; } private static string GetAttributeNameValue(XElement samlAttribute, string attributeName) { var elmAttributeValue = samlAttribute .DescendantsAndSelf("{" + SamlTags.AttributeValue.Ns + "}" + SamlTags.AttributeValue.TagName) .FirstOrDefault(); if (elmAttributeValue == null) throw new ModelBuildException("Missing 'saml:AttributeValue' element for 'saml:Attribute' element '" + attributeName + "'"); return elmAttributeValue.Value; } } }