Code Monkey home page Code Monkey logo

lethek / fluentcertificates Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 319 KB

FluentCertificates is a library using the Immutable Fluent Builder pattern for easily creating, finding and exporting certificates. Makes it simple to generate your own certificate chains, or just stand-alone self-signed certificates.

License: MIT License

C# 97.45% Batchfile 0.09% PowerShell 1.35% Shell 1.10%
ca cert certificate crt fluent fluent-api pem pfx x509

fluentcertificates's Introduction

๐Ÿ“– FluentCertificates Overview

โš ๏ธ Note: while version numbers are v0.x.y, this software is under initial development and there'll be breaking-changes in its API from version to version.

NuGet Build & Publish GitHub license

FluentCertificates is a library using the Immutable Fluent Builder pattern for easily creating, finding and exporting certificates. Makes it simple to generate your own certificate chains, or just stand-alone self-signed certificates.

This project is published in several NuGet packages:

Unfortunately documentation is incomplete. You may find more examples within the project's unit tests.

CertificateBuilder examples

CertificateBuilder requires the FluentCertificates.Builder package and is found under the FluentCertificates namespace.

The absolute minimum needed to create a certificate (although it may not be a very useful one):

using var cert = new CertificateBuilder().Create();

Create a CertificateSigningRequest for signing, exporting and passing to a 3rd party CA:

//A public & private keypair must be created first, outside of the CertificateBuilder, otherwise you'd have no way to retrieve the private-key used for the new CertificateSigningRequest object
using var keys = RSA.Create();

//Creating a CertificateSigningRequest
var csr = new CertificateBuilder()
    .SetUsage(CertificateUsage.Server)
    .SetSubject(b => b.SetCommonName("*.fake.domain"))
    .SetDnsNames("*.fake.domain", "fake.domain")
    .SetKeyPair(keys)
    .CreateCertificateSigningRequest();

//The CertificateRequest object is accessible here:
var certRequest = csr.CertificateRequest;

//CSR can be exported to a string
Console.WriteLine(csr.ToPemString());

//Or to a file or StringWriter instance
csr.ExportAsPem("csr.pem");

Build a self-signed web server certificate:

//Using a fluent style
using var cert = new CertificateBuilder()
    .SetUsage(CertificateUsage.Server)
    .SetFriendlyName("Example self-signed web-server certificate")
    .SetSubject(b => b.SetCommonName("*.fake.domain"))
    .SetDnsNames("*.fake.domain", "fake.domain")
    .SetNotAfter(DateTimeOffset.UtcNow.AddMonths(1))
    .Create();

//And just to demonstrate using object initializers (I'll use fluent style from now on though)
using var builder = new CertificateBuilder() {
    Usage = CertificateUsage.Server,
    FriendlyName = "Example self-signed web-server certificate",
    Subject = new X500NameBuilder().SetCommonName("*.fake.domain"),
    DnsNames = new[] { "*.fake.domain", "fake.domain" },
    NotAfter = DateTimeOffset.UtcNow.AddMonths(1)
};
var cert = builder.Create();

Build a CA (certificate authority):

//A CA's expiry date must be later than that of any certificates it will issue
using var issuer = new CertificateBuilder()
    .SetUsage(CertificateUsage.CA)
    .SetFriendlyName("Example root CA")
    .SetSubject(b => b.SetCommonName("Example root CA"))
    .SetNotAfter(DateTimeOffset.UtcNow.AddYears(100))
    .Create();

Build a client-auth certificate signed by a CA:

//Note: the 'issuer' certificate used must have a private-key attached in order to sign this new certificate
using var cert = new CertificateBuilder()
    .SetUsage(CertificateUsage.Client)
    .SetFriendlyName("Example client-auth certificate")
    .SetSubject(b => b.SetCommonName("User: Michael"))
    .SetNotAfter(DateTimeOffset.UtcNow.AddYears(1))
    .SetIssuer(issuer)
    .Create();

Advanced: Build a certificate with customized extensions:

using var cert = new CertificateBuilder()
    .SetFriendlyName("Example certificate with customized extensions")
    .SetSubject(b => b.SetCommonName("Example certificate with customized extensions"))
    .AddExtension(new X509BasicConstraintsExtension(false, false, 0, true))
    .AddExtension(new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DataEncipherment, true))
    .AddExtension(new X509EnhancedKeyUsageExtension(new OidCollection { new(KeyPurposeID.AnyExtendedKeyUsage.Id) }, false))
    .SetIssuer(issuer)
    .Create();

CertificateFinder examples

CertificateFinder requires the FluentCertificates.Finder package and is found under the FluentCertificates namespace.

TODO: document this


X500NameBuilder examples

X500NameBuilder requires the FluentCertificates.Builder package and is found under the FluentCertificates namespace.

TODO: document this; see unit tests for more examples


X509Certificate2 extension-methods

These extension methods require the FluentCertificates.Builder package and are found under the FluentCertificates namespace.

TODO: document these; see unit tests for more examples

Extension-Method Description
BuildChain
ExportAsCert
ExportAsPkcs12
ExportAsPkcs7
ExportAsPem
ToPemString
ToBase64String
GetPrivateKey
GetSignatureData
GetToBeSignedData
IsValidNow
IsValid
IsSelfSigned
IsIssuedBy

X509Chain extension-methods

These extension methods require the FluentCertificates.Builder package and are found under the FluentCertificates namespace.

TODO: document these

Extension-Method Description
ToCollection
ToEnumerable
ExportAsPkcs7
ExportAsPkcs12
ExportAsPem
ToPemString

X509Certificate2Collection extension-methods

These extension methods require the FluentCertificates.Builder package and are found under the FluentCertificates namespace.

TODO: document these

Extension-Method Description
ToEnumerable
ExportAsPkcs7
ExportAsPkcs12
ExportAsPem
ToPemString

IEnumerable<X509Certificate2> extension-methods

These extension methods require the FluentCertificates.Builder package and are found under the FluentCertificates namespace.

TODO: document these

Extension-Method Description
ToCollection
FilterPrivateKeys
ExportAsPkcs7
ExportAsPkcs12
ExportAsPem
ToPemString

AsymmetricAlgorithm extension-methods

These extension methods require the FluentCertificates.Builder package and are found under the FluentCertificates namespace.

TODO: document these

Extension-Method Description
ToPrivateKeyPemString
ToPublicKeyPemString
ExportAsPrivateKeyPem
ExportAsPublicKeyPem

CertificateRequest extension-methods

These extension methods require the FluentCertificates.Builder package and are found under the FluentCertificates namespace.

Extension-Method Description
ToPemString() Exports the CertificateRequest to a PEM string.
ExportAsPem(string path) Exports the CertificateRequest to the specified PEM file.
ExportAsPem(TextWriter writer) Exports the CertificateRequest in PEM format to the given TextWriter.
ConvertToBouncyCastle() Converts the CertificateRequest to a BouncyCastle Pkcs10CertificationRequest

X509Extension extension-methods

These extension methods require the FluentCertificates.Builder package and are found under the FluentCertificates namespace.

Extension-Method Description
dnExtension.ConvertToBouncyCastle() Converts a DotNet X509Extension to a BouncyCastle X509Extension.
bcExtension.ConvertToDotNet(string oid) Converts a BouncyCastle X509Extension to a DotNet X509Extension. A DotNet X509Extension includes an OID, but a BouncyCastle one doesn't, therefore one must be supplied in the parameters here.
bcExtension.ConvertToDotNet(DerObjectIdentifier oid) Converts a BouncyCastle X509Extension to a DotNet X509Extension. A DotNet X509Extension includes an OID, but a BouncyCastle one doesn't, therefore one must be supplied in the parameters here.

fluentcertificates's People

Contributors

dependabot[bot] avatar lethek avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fluentcertificates's Issues

Add method ToCertificateRequest() method to the CertificateBuilder

ToCertificateRequest should generate a .NET CertificateRequest object based on the builder's parameters. Write extension-methods for the CertificateRequest class for exporting as PEM etc.

The ToCsr method should be made obsolete.

The goal is to minimize the library's reliance on the BouncyCastle and use built-in .NET classes wherever possible without sacrificing flexibility.

[BUG]Calling .ToCertificateRequest().CreateSigningRequest() on a CertificateBuilder used to work but now throws an exception

Describe the bug
Calling the ToCertificateRequest() method on a CertificateBuilder used to create a CertificateRequest instance that could be called with the parameterless CreateSigningRequest() method overload. However, this now throws an exception:

System.InvalidOperationException: This method cannot be used since no signing key was provided via a constructor, use an overload accepting an X509SignatureGenerator instead.
   at System.Security.Cryptography.X509Certificates.CertificateRequest.CreateSigningRequest()

Expected behaviour
To be able to create a Certificate Signing Request simply without advanced knowledge about signature generators etc.

Code snippet

using var keys = RSA.Create();
var req = new CertificateBuilder().SetKeyPair(keys).ToCertificateRequest();
var csr = req.CreateSigningRequest(); //This line used to not, but now throws an InvalidOperationException

Environment

  • FluentCertificates 0.7.1
  • .NET 6.0

The bug has the severity

  • Major: The defect affects major functionality or major data. It has a workaround but is not obvious and is difficult.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.