|
@@ -17,6 +17,7 @@ using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using Org.BouncyCastle.Math;
|
|
|
using Org.BouncyCastle.OpenSsl;
|
|
|
+using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
|
|
namespace CAUtilLib
|
|
|
{
|
|
@@ -50,7 +51,7 @@ namespace CAUtilLib
|
|
|
AsymmetricCipherKeyPair issuerKey,
|
|
|
AsymmetricKeyParameter subjectPublic,
|
|
|
BigInteger? issuerSerialNumber = null,
|
|
|
- bool isCertificateAuthority = false)
|
|
|
+ List<AddExtensionData> extensionDatas = null)
|
|
|
{
|
|
|
var selfserilaNumber = BigInteger.ProbablePrime(120, secureRandom);
|
|
|
if (issuerSerialNumber is null)
|
|
@@ -60,9 +61,11 @@ namespace CAUtilLib
|
|
|
|
|
|
ISignatureFactory signatureFactory;
|
|
|
signatureFactory = new Asn1SignatureFactory(
|
|
|
- PkcsObjectIdentifiers.Sha512WithRsaEncryption.ToString(),
|
|
|
+ PkcsObjectIdentifiers.Sha256WithRsaEncryption.ToString(),
|
|
|
issuerKey.Private);
|
|
|
- signatureFactory = new Asn1SignatureFactory(PkcsObjectIdentifiers.Sha512WithRsaEncryption.ToString(), issuerKey.Private);
|
|
|
+ //signatureFactory = new Asn1SignatureFactory(
|
|
|
+ // PkcsObjectIdentifiers.Sha512WithRsaEncryption.ToString(),
|
|
|
+ // issuerKey.Private);
|
|
|
|
|
|
var certGenerator = new X509V3CertificateGenerator();
|
|
|
certGenerator.SetIssuerDN(issuer);
|
|
@@ -84,24 +87,32 @@ namespace CAUtilLib
|
|
|
)
|
|
|
);
|
|
|
|
|
|
- if (isCertificateAuthority)
|
|
|
+ if (extensionDatas != null)
|
|
|
{
|
|
|
- certGenerator.AddExtension(X509Extensions.BasicConstraints.Id, true,
|
|
|
- new BasicConstraints(0));
|
|
|
- //certGenerator.AddExtension(X509Extensions.KeyUsage.Id, true,
|
|
|
- // new KeyUsage(KeyUsage.KeyCertSign));
|
|
|
+ foreach (var data in extensionDatas)
|
|
|
+ {
|
|
|
+ certGenerator.AddExtension(data.OID, data.Critical, data.ExtensionValue);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ //if (isCertificateAuthority)
|
|
|
+ //{
|
|
|
+ // certGenerator.AddExtension(X509Extensions.BasicConstraints.Id, true,
|
|
|
+ // new BasicConstraints(0));
|
|
|
+ // certGenerator.AddExtension(X509Extensions.KeyUsage.Id, true,
|
|
|
+ // new KeyUsage(KeyUsage.KeyCertSign));
|
|
|
+ //}
|
|
|
+
|
|
|
return certGenerator.Generate(signatureFactory);
|
|
|
}
|
|
|
|
|
|
public static bool ValidateCertificateChain(List<X509Certificate> certificates)
|
|
|
{
|
|
|
- for (int index = certificates.Count - 1; index > 0; index--)
|
|
|
+ for (int index = 0; index < certificates.Count - 1; index++)
|
|
|
{
|
|
|
- var signedCertificate = certificates[index];
|
|
|
- var sourceCertificate = certificates[index - 1];
|
|
|
- if (!ValidateCert(signedCertificate, sourceCertificate.GetPublicKey()))
|
|
|
+ var validateCertificate = certificates[index];
|
|
|
+ var sourceCertificate = certificates[index + 1];
|
|
|
+ if (!ValidateCert(validateCertificate, sourceCertificate.GetPublicKey()))
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
@@ -116,11 +127,11 @@ namespace CAUtilLib
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- for (int index = certChain.Count - 1; index > -1; index--)
|
|
|
+ for (int index = 0; index < certChain.Count; index++)
|
|
|
{
|
|
|
- var signedCertificate = cert;
|
|
|
+ var validateCertificate = cert;
|
|
|
var sourceCertificate = certChain[index];
|
|
|
- if (ValidateCert(signedCertificate, sourceCertificate.GetPublicKey()))
|
|
|
+ if (ValidateCert(validateCertificate, sourceCertificate.GetPublicKey()))
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
@@ -153,21 +164,78 @@ namespace CAUtilLib
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static X509Certificate GenerateSelfSignedCertificate(AsymmetricCipherKeyPair kp, string commonName = "Zerova")
|
|
|
+ public static X509Certificate GenerateSelfSignedRootCertificate(
|
|
|
+ X509Name subject,
|
|
|
+ AsymmetricCipherKeyPair kp
|
|
|
+ )
|
|
|
{
|
|
|
- //var certName = new X509Name("CN=" + commomName);
|
|
|
- var certName = CreateX509Name(commonName: commonName);
|
|
|
- return GenerateCertificate(certName, certName, kp, kp.Public, isCertificateAuthority: true);
|
|
|
+ List<AddExtensionData> datas = new List<AddExtensionData>() {
|
|
|
+ new AddExtensionData(){
|
|
|
+ OID = X509Extensions.BasicConstraints.Id,
|
|
|
+ Critical = true,
|
|
|
+ ExtensionValue = new BasicConstraints(true)
|
|
|
+ },
|
|
|
+ new AddExtensionData()
|
|
|
+ {
|
|
|
+ OID= X509Extensions.KeyUsage.Id,
|
|
|
+ Critical = true,
|
|
|
+ ExtensionValue = new KeyUsage(KeyUsage.DigitalSignature | KeyUsage.KeyCertSign | KeyUsage.CrlSign)
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return GenerateCertificate(subject, subject, kp, kp.Public, extensionDatas: datas);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static X509Certificate GenerateIntermediateCertificate(
|
|
|
+ Pkcs10CertificationRequest csr,
|
|
|
+ X509Certificate rca,
|
|
|
+ AsymmetricCipherKeyPair issuerKey,
|
|
|
+ int pathLengthConstraint
|
|
|
+ )
|
|
|
+ {
|
|
|
+ var csrInfo = csr.GetCertificationRequestInfo();
|
|
|
+
|
|
|
+ List<AddExtensionData> datas = new List<AddExtensionData>() {
|
|
|
+ new AddExtensionData(){
|
|
|
+ OID = X509Extensions.BasicConstraints.Id,
|
|
|
+ Critical = true,
|
|
|
+ ExtensionValue = new BasicConstraints(pathLengthConstraint)
|
|
|
+ },
|
|
|
+ new AddExtensionData()
|
|
|
+ {
|
|
|
+ OID= X509Extensions.KeyUsage.Id,
|
|
|
+ Critical = true,
|
|
|
+ ExtensionValue = new KeyUsage(KeyUsage.DigitalSignature | KeyUsage.KeyCertSign | KeyUsage.CrlSign)
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return GenerateCertificate(rca.SubjectDN, csrInfo.Subject, issuerKey, csr.GetPublicKey(), extensionDatas: datas);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static X509Certificate GenerateEndCertificate(
|
|
|
+ Pkcs10CertificationRequest csr,
|
|
|
+ X509Certificate rca,
|
|
|
+ AsymmetricCipherKeyPair issuerKey
|
|
|
+ )
|
|
|
+ {
|
|
|
+ var csrInfo = csr.GetCertificationRequestInfo();
|
|
|
+
|
|
|
+ List<AddExtensionData> datas = new List<AddExtensionData>() {
|
|
|
+ new AddExtensionData()
|
|
|
+ {
|
|
|
+ OID= X509Extensions.KeyUsage.Id,
|
|
|
+ Critical = true,
|
|
|
+ ExtensionValue = new KeyUsage(KeyUsage.DigitalSignature | KeyUsage.NonRepudiation | KeyUsage.KeyEncipherment)
|
|
|
+ }
|
|
|
+ };
|
|
|
+ return GenerateCertificate(rca.SubjectDN, csrInfo.Subject, issuerKey, csr.GetPublicKey(), extensionDatas: datas);
|
|
|
}
|
|
|
|
|
|
public static X509Certificate SignCertificate(
|
|
|
Pkcs10CertificationRequest csr,
|
|
|
X509Certificate rca,
|
|
|
- AsymmetricCipherKeyPair issuerKey,
|
|
|
- bool isCertificateAuthority = false)
|
|
|
+ AsymmetricCipherKeyPair issuerKey)
|
|
|
{
|
|
|
var csrInfo = csr.GetCertificationRequestInfo();
|
|
|
- return GenerateCertificate(rca.SubjectDN, csrInfo.Subject, issuerKey, csr.GetPublicKey(), issuerSerialNumber: rca.SerialNumber, isCertificateAuthority: isCertificateAuthority);
|
|
|
+ return GenerateCertificate(rca.SubjectDN, csrInfo.Subject, issuerKey, csr.GetPublicKey(), issuerSerialNumber: rca.SerialNumber);
|
|
|
}
|
|
|
|
|
|
public static Pkcs10CertificationRequest CreateCertificateReq(AsymmetricCipherKeyPair kp, string commonName, string organizationName)
|
|
@@ -175,7 +243,7 @@ namespace CAUtilLib
|
|
|
var subject = CreateX509Name(commonName: commonName, organizationName: organizationName);
|
|
|
|
|
|
var csr = new Pkcs10CertificationRequest(
|
|
|
- PkcsObjectIdentifiers.Sha512WithRsaEncryption.ToString(),
|
|
|
+ PkcsObjectIdentifiers.Sha256WithRsaEncryption.ToString(),
|
|
|
subject,
|
|
|
kp.Public,
|
|
|
null,
|