Skip to content

Commit

Permalink
Allow output of YubiKey attestion data to eventlog (#50)
Browse files Browse the repository at this point in the history
* Update EWT 4201 parameter ordering
Update EWT 4204 parameter order and added YubiKey data.
Added unit test

* Added test to create full configuration for YubiKeyPolicy
  • Loading branch information
virot authored Jan 19, 2025
1 parent 973a7c2 commit 9d868aa
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 30 deletions.
43 changes: 43 additions & 0 deletions TameMyCerts.Tests/YubikeyValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,5 +776,48 @@ public void Validate_Slot_incorrect_with_0x_10022()

output.WriteLine(policy.SaveToString());
}
[Fact]
public void Validate_YubiKeyObject_to_Human_readable_10023()
{
CertificateDatabaseRow dbRow = new CertificateDatabaseRow(_yubikey_valid_5_4_3_Once_Never_UsbAKeychain_9a_Normal_RSA_2048_CSR, CertCli.CR_IN_PKCS10, null, 10023);
var result = new CertificateRequestValidationResult(dbRow);
result = _YKvalidator.ExtractAttestion(result, _policy, dbRow, out var yubikeyInfo);

PrintResult(result);

output.WriteLine(yubikeyInfo.SaveToString());

Assert.Contains("19661687", yubikeyInfo.SaveToString());


}

[Fact]
public void Validate_all_configuration_options_10024()
{
CertificateDatabaseRow dbRow = new CertificateDatabaseRow(_yubikey_valid_5_4_3_Once_Never_UsbAKeychain_9a_Normal_RSA_2048_CSR, CertCli.CR_IN_PKCS10, null, 10024);
var policy = _policy;
policy.YubikeyPolicy[0].KeyAlgorithmFamilies = new List<KeyAlgorithmFamily> { KeyAlgorithmFamily.RSA, KeyAlgorithmFamily.ECC};
policy.YubikeyPolicy[0].MinimumFirmwareString = "0.0.0";
policy.YubikeyPolicy[0].MaximumFirmwareString = "9.9.9";
policy.YubikeyPolicy[0].Formfactor = new List<YubikeyFormFactor> { YubikeyFormFactor.UsbAKeychain, YubikeyFormFactor.UsbCKeychain, YubikeyFormFactor.UsbANano, YubikeyFormFactor.UsbCNano, YubikeyFormFactor.UsbCLightning, YubikeyFormFactor.UsbABiometricKeychain, YubikeyFormFactor.UsbCBiometricKeychain };
policy.YubikeyPolicy[0].Edition = new List<YubikeyEdition> { YubikeyEdition.FIPS, YubikeyEdition.Normal, YubikeyEdition.CSPN };
policy.YubikeyPolicy[0].Slot = new List<string> { "9a", "9c", "9d", "9e" };
policy.YubikeyPolicy[0].TouchPolicies = new List<YubikeyTouchPolicy> { YubikeyTouchPolicy.Always, YubikeyTouchPolicy.Never, YubikeyTouchPolicy.Cached };
policy.YubikeyPolicy[0].PinPolicies = new List<YubikeyPinPolicy> { YubikeyPinPolicy.Once, YubikeyPinPolicy.Never, YubikeyPinPolicy.Always, YubikeyPinPolicy.MatchOnce, YubikeyPinPolicy.MatchAlways };
policy.YubikeyPolicy[0].Action = YubikeyPolicyAction.Allow;

var result = new CertificateRequestValidationResult(dbRow);
result = _YKvalidator.ExtractAttestion(result, _policy, dbRow, out var yubikeyInfo);

PrintResult(result);

output.WriteLine(policy.SaveToString());

Assert.Contains("UsbANano", policy.SaveToString());


}

}
}
8 changes: 4 additions & 4 deletions TameMyCerts/EWTLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ public void TMC_94_XML_Parsing_error(string filename, string error)

#region Yubico Validator events 4201-4399
[Event(4201, Level = EventLevel.Warning, Channel = EventChannel.Operational, Task = Tasks.YubikeyValidator, Keywords = EventKeywords.None)]
public void YKVal_4201_Denied_by_Policy(string denyingPolicy, int requestID)
public void YKVal_4201_Denied_by_Policy(int requestID, string denyingPolicy)
{
if (IsEnabled())
{
WriteEvent(4201, denyingPolicy, requestID);
WriteEvent(4201, requestID, denyingPolicy);
}
}
[Event(4202, Level = EventLevel.Warning, Channel = EventChannel.Operational, Task = Tasks.YubikeyValidator, Keywords = EventKeywords.None)]
Expand All @@ -144,11 +144,11 @@ public void YKVal_4203_Denied_due_to_no_matching_policy_default_deny(int request
}
}
[Event(4204, Level = EventLevel.Verbose, Channel = EventChannel.Operational, Task = Tasks.YubikeyValidator, Keywords = EventKeywords.None)]
public void YKVal_4204_Matching_policy(string policy, int requestID)
public void YKVal_4204_Matching_policy(int requestID, string policy, string yubiKey)
{
if (IsEnabled())
{
WriteEvent(4204, policy, requestID);
WriteEvent(4204, requestID, policy, yubiKey);
}
}
[Event(4205, Level = EventLevel.Error, Channel = EventChannel.Operational, Task = Tasks.YubikeyValidator, Keywords = EventKeywords.None)]
Expand Down
8 changes: 5 additions & 3 deletions TameMyCerts/LocalizedStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions TameMyCerts/LocalizedStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@
<value>The certificate request has been denied due to incorrect certificate request.</value>
</data>
<data name="event_YKVal_4201_Denied_by_Policy" xml:space="preserve">
<value>The request {1} was rejected due to policy: \r\n{0}</value>
<value>The request {0} was rejected due to policy: \r\n{1}</value>
</data>
<data name="event_YKVal_4202_Denied_by_Policy" xml:space="preserve">
<value>The request {0} was rejected as the Yubikey Attestion failed for the embedded attestion.</value>
Expand All @@ -387,8 +387,10 @@
<value>The request {0} was denied due to not meeting any Grant policy in the Yubikey Validator.</value>
</data>
<data name="event_YKVal_4204_Matching_policy" xml:space="preserve">
<value>The request {1} is matching policy:
{0}</value>
<value>The request {0} is matching policy:
{1}
YubiKey attestion data:
{2}</value>
</data>
<data name="event_YKVal_4205_Failed_to_extract_Yubikey_Attestion" xml:space="preserve">
<value>The request {0} contained an embedded Yubikey Attestion, but it failed to be extracted.</value>
Expand Down
90 changes: 72 additions & 18 deletions TameMyCerts/Models/YubikeyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
using TameMyCerts.Enums;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace TameMyCerts.Models
{
internal class YubikeyObject
// Must be public due to XML serialization, otherwise 0x80131509 / System.InvalidOperationException
[XmlRoot(ElementName = "YubiKeyObject")]
public class YubikeyObject
{
private const StringComparison COMPARISON = StringComparison.InvariantCultureIgnoreCase;

private static X509Certificate2 YubikeyValidationCA = new X509Certificate2(new byte[] { 0x30, 0x82, 0x3, 0x17, 0x30, 0x82, 0x1, 0xFF, 0xA0, 0x3, 0x2, 0x1, 0x2, 0x2, 0x3, 0x4, 0x6, 0x47, 0x30, 0xD, 0x6, 0x9, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0xD, 0x1, 0x1, 0xB, 0x5, 0x0, 0x30, 0x2B, 0x31, 0x29, 0x30, 0x27, 0x6, 0x3, 0x55, 0x4, 0x3, 0xC, 0x20, 0x59, 0x75, 0x62, 0x69, 0x63, 0x6F, 0x20, 0x50, 0x49, 0x56, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x20, 0x43, 0x41, 0x20, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6C, 0x20, 0x32, 0x36, 0x33, 0x37, 0x35, 0x31, 0x30, 0x20, 0x17, 0xD, 0x31, 0x36, 0x30, 0x33, 0x31, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x18, 0xF, 0x32, 0x30, 0x35, 0x32, 0x30, 0x34, 0x31, 0x37, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x2B, 0x31, 0x29, 0x30, 0x27, 0x6, 0x3, 0x55, 0x4, 0x3, 0xC, 0x20, 0x59, 0x75, 0x62, 0x69, 0x63, 0x6F, 0x20, 0x50, 0x49, 0x56, 0x20, 0x52, 0x6F, 0x6F, 0x74, 0x20, 0x43, 0x41, 0x20, 0x53, 0x65, 0x72, 0x69, 0x61, 0x6C, 0x20, 0x32, 0x36, 0x33, 0x37, 0x35, 0x31, 0x30, 0x82, 0x1, 0x22, 0x30, 0xD, 0x6, 0x9, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0xD, 0x1, 0x1, 0x1, 0x5, 0x0, 0x3, 0x82, 0x1, 0xF, 0x0, 0x30, 0x82, 0x1, 0xA, 0x2,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0x7A, 0x52, 0xD0, 0xF, 0x81, 0xA7, 0x7D, 0x2F, 0x97, 0xD, 0xBE, 0xE6, 0xBF, 0x21, 0x64, 0x66, 0x9B, 0xE0, 0xD, 0xCB, 0x73, 0xB6, 0x2C, 0x7F, 0xBE, 0x3F, 0x29, 0x7C, 0x49, 0x11, 0x33, 0x53, 0xCA, 0x27, 0x6C, 0x1B, 0x23, 0x32, 0xF, 0x50, 0xE, 0x24, 0x9F, 0xE6, 0x82, 0x4B, 0x2A, 0xF7, 0x7F, 0x45, 0xE9, 0xFE, 0xCC, 0x66, 0x3B });

[XmlIgnore]
public Dictionary<string, string> Attributes { get; } =
new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

Expand Down Expand Up @@ -102,7 +106,7 @@ public YubikeyObject(byte[] publicKey, X509Certificate2 AttestationCertificate,
#endregion
#region FormFactor
byte FormFactor = AttestationCertificate.Extensions.Cast<X509Extension>().FirstOrDefault(x => x.Oid.Value == YubikeyX509Extensions.FORMFACTOR)?.RawData[0] ?? 0;
this.FormFactor = (YubikeyFormFactor)FormFactor;
this.FormFactor = (YubikeyFormFactor)(FormFactor & 0x0F); // Mask out the upper 4 bits, Those are used for CSPN and FIPS
#endregion
#region Firmware Version
// Update the Firmware Version
Expand All @@ -125,7 +129,7 @@ public YubikeyObject(byte[] publicKey, X509Certificate2 AttestationCertificate,
}
#endregion

#region
#region FIPS / CSPN
// Check for the FIPS extension
if (IntermediateCertificate.Extensions.Cast<X509Extension>().Where(x => x.Oid.Value == YubikeyX509Extensions.FIPS_CERTIFIED).Any())
{
Expand Down Expand Up @@ -157,21 +161,71 @@ public YubikeyObject(byte[] publicKey, X509Certificate2 AttestationCertificate,
Attributes.Add("SerialNumber", this.SerialNumber);
}


public YubikeyTouchPolicy TouchPolicy { get; }
public YubikeyPinPolicy PinPolicy { get; }
public YubikeyFormFactor FormFactor { get; }
public string Slot { get; } = "";
public string SerialNumber { get; } = "";
public Version FirmwareVersion { get; } = new Version(0, 0, 0);
public KeyAlgorithmFamily keyAlgorithm { get; }
public int KeyLength { get; }
public YubikeyEdition Edition { get; } = YubikeyEdition.Normal;
[XmlElement(ElementName = "TouchPolicy")]
public YubikeyTouchPolicy TouchPolicy { get; set; }
[XmlElement(ElementName = "PinPolicy")]
public YubikeyPinPolicy PinPolicy { get; set; }
[XmlElement(ElementName = "FormFactor")]
public YubikeyFormFactor FormFactor { get; set; }
[XmlElement(ElementName = "Slot")]
public string Slot { get; set; } = "";
[XmlElement(ElementName = "Serial")]
public string SerialNumber { get; set; } = "";
[XmlElement(ElementName = "FirmwareVersion")]
public string FirmwareVersionString { get { return this.FirmwareVersion.ToString(); } set { throw new Exception("String cannot be set."); } }
[XmlIgnore]
public Version FirmwareVersion { get; set; } = new Version(0, 0, 0);
[XmlElement(ElementName = "KeyAlgorithm")]
public KeyAlgorithmFamily keyAlgorithm { get; set; }
[XmlElement(ElementName = "KeyLength")]
public int KeyLength { get; set; }
[XmlElement(ElementName = "Edition")]
public YubikeyEdition Edition { get; set; } = YubikeyEdition.Normal;
[XmlIgnore]
public X509Certificate2 AttestionCertificate { get; }
[XmlIgnore]
public X509Certificate2 IntermediateCertificate { get; }

[XmlElement(ElementName = "Validated")]
public bool? Validated { get; } = false;
private static string attestionSlotPattern = @"CN=YubiKey PIV Attestation (?<slot>[0-9A-Fa-f]{2})";

[XmlIgnore]
public static string attestionSlotPattern = @"CN=YubiKey PIV Attestation (?<slot>[0-9A-Fa-f]{2})";

public static string ConvertToHumanReadableXml(string inputString)
{
var xmlWriterSettings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true,
NewLineOnAttributes = true
};

var stringBuilder = new StringBuilder();

var xElement = XElement.Parse(inputString);

using (var xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings))
{
xElement.Save(xmlWriter);
}

return stringBuilder.ToString();
}

public string SaveToString()
{
var xmlSerializer = new XmlSerializer(typeof(YubikeyObject));

using (var stringWriter = new StringWriter())
{
using (var xmlWriter = XmlWriter.Create(stringWriter))
{
xmlSerializer.Serialize(xmlWriter, this);
var xmlData = stringWriter.ToString();

return ConvertToHumanReadableXml(xmlData);
}
}
}

}
}
4 changes: 2 additions & 2 deletions TameMyCerts/Validators/YubikeyValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ public CertificateRequestValidationResult VerifyRequest(CertificateRequestValida
{
if (ykP.Action == YubikeyPolicyAction.Deny)
{
ETWLogger.Log.YKVal_4201_Denied_by_Policy(ykP.SaveToString(), requestID);
ETWLogger.Log.YKVal_4201_Denied_by_Policy(requestID, ykP.SaveToString());
result.SetFailureStatus(WinError.CERTSRV_E_TEMPLATE_DENIED, string.Format(
LocalizedStrings.YKVal_Policy_Matches_with_Reject, ykP.SaveToString()));
return result ;
}
ETWLogger.Log.YKVal_4204_Matching_policy(ykP.SaveToString(), requestID);
ETWLogger.Log.YKVal_4204_Matching_policy(requestID, ykP.SaveToString(), yubikey.SaveToString());
foundMatch = true;

// Store the AttestionData and Intermediate Certificate in the certificate, if requested
Expand Down

0 comments on commit 9d868aa

Please sign in to comment.