internal static IPrivateKeyProvider Create( IPublicKeyDataProvider publicKeyDataProvider, TimeSpan keyLifetime, TimeSpan keyRotationPeriod, IDateTimeProvider dateTimeProvider = null ) { if( keyLifetime < keyRotationPeriod ) { throw new ArgumentException( "Private key lifetime must exceed the rotation period", "keyLifetime" ); } dateTimeProvider = dateTimeProvider ?? new DateTimeProvider(); ID2LSecurityTokenFactory d2lSecurityTokenFactory = new D2LSecurityTokenFactory( dateTimeProvider, keyLifetime ); IPrivateKeyProvider privateKeyProvider = new RsaPrivateKeyProvider( d2lSecurityTokenFactory ); privateKeyProvider = new SavingPrivateKeyProvider( privateKeyProvider, PublicKeyDataProviderFactory.CreateInternal( publicKeyDataProvider ) ); privateKeyProvider = new RotatingPrivateKeyProvider( privateKeyProvider, dateTimeProvider, keyRotationPeriod ); return privateKeyProvider; }
public async Task FromJson_GeneratedKeyRoundTrips() { IPrivateKeyProvider privateKeyProvider = new RsaPrivateKeyProvider( new D2LSecurityTokenFactory( new DateTimeProvider(), TimeSpan.FromHours( 1 ) ) ); D2LSecurityToken token = await privateKeyProvider.GetSigningCredentialsAsync().SafeAsync(); JsonWebKey expectedKey = token.ToJsonWebKey(); string expectedJson = JsonConvert.SerializeObject( expectedKey.ToJwkDto() ); JsonWebKey actualKey = JsonWebKey.FromJson( expectedJson ); string actualJson = JsonConvert.SerializeObject( actualKey.ToJwkDto() ); Assert.AreEqual( expectedKey.Id, actualKey.Id ); Assert.AreEqual( (long)expectedKey.ExpiresAt.Value.TimeSinceUnixEpoch().TotalSeconds, (long)actualKey.ExpiresAt.Value.TimeSinceUnixEpoch().TotalSeconds ); Assert.AreEqual( expectedJson, actualJson ); }
public static XmlElement GenerateSignature(XmlDocument licenseDocument, IPrivateCryptoKey privateKey) { using (var privateKeyProvider = new RsaPrivateKeyProvider()) { var reference = new Reference { Uri = string.Empty }; reference.AddTransform(new XmlDsigEnvelopedSignatureTransform()); var signedXml = new SignedXml(licenseDocument) { SigningKey = privateKeyProvider.Recreate(privateKey) }; signedXml.AddReference(reference); signedXml.ComputeSignature(); return(signedXml.GetXml()); } }
public async Task FromJson_GeneratedKeyRoundTrips() { IPrivateKeyProvider privateKeyProvider = new RsaPrivateKeyProvider( new D2LSecurityTokenFactory( DateTimeProvider.Instance, TimeSpan.FromHours(1) ) ); D2LSecurityToken token = await privateKeyProvider.GetSigningCredentialsAsync().ConfigureAwait(false); JsonWebKey expectedKey = token.ToJsonWebKey(); string expectedJson = JsonConvert.SerializeObject(expectedKey.ToJwkDto()); JsonWebKey actualKey = JsonWebKey.FromJson(expectedJson); string actualJson = JsonConvert.SerializeObject(actualKey.ToJwkDto()); Assert.AreEqual(expectedKey.Id, actualKey.Id); Assert.AreEqual(expectedKey.ExpiresAt.Value.ToUnixTimeSeconds(), actualKey.ExpiresAt.Value.ToUnixTimeSeconds()); Assert.AreEqual(expectedJson, actualJson); }
public static void Main(string[] args) { var dataDirectory = @"..\..\..\..\LicenseData".ResolveBaseDirectory(); var publicKeyPath = @"..\..\..\..\LicenseData\PublicKey.xml".ResolveBaseDirectory(); var licensePath = @"..\..\..\..\LicenseData\License.xml".ResolveBaseDirectory(); if (!Directory.Exists(dataDirectory)) { Directory.CreateDirectory(dataDirectory); } var licenseCriteria = new LicenseCriteria { ExpirationDate = DateTimeOffset.UtcNow.LastDayOfMonth().EndOfDay(), IssueDate = DateTimeOffset.UtcNow, Id = Guid.NewGuid(), MetaData = new Dictionary <string, string> { { "LicensedCores", "16" } }, Type = "Subscription" }; var privateKey = new RsaPrivateKeyProvider().Create(); //var serverLicense = new ServerLicenseGenerator().Generate(privateKey, licenseCriteria); var serverLicense = new ServerLicenseGenerator().Generate(privateKey, licenseCriteria, "This is my password"); var clientLicense = serverLicense.ToClientLicense(); // In a real implementation, you would embed the public key into the assembly, via a resource file File.WriteAllText(publicKeyPath, privateKey.ExtractPublicKey().Contents); // In a real implementation you would implement ILicenseRepository File.WriteAllText(licensePath, clientLicense.Content.InnerXml); Console.WriteLine(Messsages.LicenseGenerated, dataDirectory); Console.WriteLine(Messsages.PressAnyKey); Console.ReadKey(); }
public static XmlElement GenerateSignature(XmlDocument licenseDocument, IPrivateCryptoKey privateKey) { using (var privateKeyProvider = new RsaPrivateKeyProvider()) { var reference = new Reference { Uri = string.Empty }; reference.AddTransform(new XmlDsigEnvelopedSignatureTransform()); reference.AddTransform(new XmlDsigExcC14NTransform()); reference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256"; var signedXml = new SignedXml(licenseDocument) { SigningKey = privateKeyProvider.Recreate(privateKey) }; signedXml.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"; signedXml.AddReference(reference); signedXml.ComputeSignature(); return(signedXml.GetXml()); } }