public virtual bool Match( object obj) { X509Certificate c = obj as X509Certificate; if (c == null) { return(false); } if (!MatchExtension(authorityKeyIdentifier, c, X509Extensions.AuthorityKeyIdentifier)) { return(false); } if (basicConstraints != -1) { int bc = c.GetBasicConstraints(); if (basicConstraints == -2) { if (bc != -1) { return(false); } } else { if (bc < basicConstraints) { return(false); } } } if (certificate != null && !certificate.Equals(c)) { return(false); } if (certificateValid != null && !c.IsValid(certificateValid.Value)) { return(false); } if (extendedKeyUsage != null) { IList eku = c.GetExtendedKeyUsage(); // Note: if no extended key usage set, all key purposes are implicitly allowed if (eku != null) { foreach (DerObjectIdentifier oid in extendedKeyUsage) { if (!eku.Contains(oid.Id)) { return(false); } } } } if (issuer != null && !issuer.Equivalent(c.IssuerDN, !ignoreX509NameOrdering)) { return(false); } if (keyUsage != null) { bool[] ku = c.GetKeyUsage(); // Note: if no key usage set, all key purposes are implicitly allowed if (ku != null) { for (int i = 0; i < 9; ++i) { if (keyUsage[i] && !ku[i]) { return(false); } } } } if (policy != null) { Asn1OctetString extVal = c.GetExtensionValue(X509Extensions.CertificatePolicies); if (extVal == null) { return(false); } Asn1Sequence certPolicies = Asn1Sequence.GetInstance( X509ExtensionUtilities.FromExtensionValue(extVal)); if (policy.Count < 1 && certPolicies.Count < 1) { return(false); } bool found = false; foreach (PolicyInformation pi in certPolicies) { if (policy.Contains(pi.PolicyIdentifier)) { found = true; break; } } if (!found) { return(false); } } if (privateKeyValid != null) { Asn1OctetString extVal = c.GetExtensionValue(X509Extensions.PrivateKeyUsagePeriod); if (extVal == null) { return(false); } PrivateKeyUsagePeriod pkup = PrivateKeyUsagePeriod.GetInstance( X509ExtensionUtilities.FromExtensionValue(extVal)); DateTime dt = privateKeyValid.Value; DateTime notAfter = pkup.NotAfter.ToDateTime(); DateTime notBefore = pkup.NotBefore.ToDateTime(); if (dt.CompareTo(notAfter) > 0 || dt.CompareTo(notBefore) < 0) { return(false); } } if (serialNumber != null && !serialNumber.Equals(c.SerialNumber)) { return(false); } if (subject != null && !subject.Equivalent(c.SubjectDN, !ignoreX509NameOrdering)) { return(false); } if (!MatchExtension(subjectKeyIdentifier, c, X509Extensions.SubjectKeyIdentifier)) { return(false); } if (subjectPublicKey != null && !subjectPublicKey.Equals(GetSubjectPublicKey(c))) { return(false); } if (subjectPublicKeyAlgID != null && !subjectPublicKeyAlgID.Equals(GetSubjectPublicKey(c).AlgorithmID)) { return(false); } return(true); }
static void Main(string[] args) { PolicyInformation[] certPolicies = new PolicyInformation[2]; certPolicies[0] = new PolicyInformation(new DerObjectIdentifier("2.16.840.1.101.2.1.11.5")); certPolicies[1] = new PolicyInformation(new DerObjectIdentifier("2.16.840.1.101.2.1.11.18")); var randomGenerator = new CryptoApiRandomGenerator(); var random = new SecureRandom(randomGenerator); var certificateGenerator = new X509V3CertificateGenerator(); //serial var serialNumber = BigIntegers.CreateRandomInRange( BigInteger.One, BigInteger.ValueOf(Int64.MaxValue), random); certificateGenerator.SetSerialNumber(serialNumber); // sig alg const string signatureAlgorithm = "SHA1WithRSA"; certificateGenerator.SetSignatureAlgorithm(signatureAlgorithm); // Subjects // Time x = new Time(); var subjectDN = new X509Name("CN=localhost, O=Arsslensoft, C=TN,surname=Idadi,givenname=Arsslen, uniqueidentifier=15002060,businesscategory=Production,initials=Hello, gender=male, placeofbirth=El Manar, pseudonym=Arsslinko, postaladdress=2076, countryofcitizenship=TN, countryofresidence=TN,telephonenumber=53299093"); var issuerDN = subjectDN; certificateGenerator.SetIssuerDN(issuerDN); certificateGenerator.SetSubjectDN(subjectDN); // Validity var notBefore = DateTime.UtcNow.Date.Subtract(new TimeSpan(5, 0, 0)); var notAfter = notBefore.AddYears(2); certificateGenerator.SetNotBefore(notBefore); certificateGenerator.SetNotAfter(notAfter); // PKEY const int strength = 512; var keyGenerationParameters = new KeyGenerationParameters(random, strength); // var x= new Al.Security.Crypto.Generators.DsaKeyPairGenerator(); // X9ECParameters ecP = NistNamedCurves.GetByName("B-571"); // ECDomainParameters ecSpec = new ECDomainParameters(ecP.Curve, ecP.G, ecP.N, ecP.H, ecP.GetSeed()); // ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator("ECDSA"); // //ECPA par = new DsaParametersGenerator(); // //par.Init(2048, 100, random); // //ECKeyGenerationParameters pa = new ECKeyGenerationParameters(random, par.GenerateParameters()); //// var keyPairGenerator = new DHKeyPairGenerator(); // //DsaParametersGenerator par = new DsaParametersGenerator(); // //par.Init(2048, 100, random); // //DsaKeyGenerationParameters pa = new DsaKeyGenerationParameters(random, par.GenerateParameters()); // // keyPairGenerator.Init(pa); // keyPairGenerator.Init(new ECKeyGenerationParameters(ecSpec, new SecureRandom())); //var keyPairGenerator = new DsaKeyPairGenerator(); //DsaParametersGenerator par = new DsaParametersGenerator(); //par.Init(1024, 100, random); //DsaKeyGenerationParameters pa = new DsaKeyGenerationParameters(random, par.GenerateParameters()); //keyPairGenerator.Init(pa); // KeyPair = keyPairGenerator.GenerateKeyPair(); var keyPairGenerator = new RsaKeyPairGenerator(); keyPairGenerator.Init(keyGenerationParameters); StreamReader str = new StreamReader("D:\\test.key"); PemReader pem = new PemReader(str); AsymmetricCipherKeyPair keypair = (AsymmetricCipherKeyPair)pem.ReadObject(); var subjectKeyPair = keypair; str.Close(); certificateGenerator.SetPublicKey(subjectKeyPair.Public); // ext X509Extensions certificateGenerator.AddExtension(X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifierStructure(subjectKeyPair.Public)); certificateGenerator.AddExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(subjectKeyPair.Public)); certificateGenerator.AddExtension(X509Extensions.BasicConstraints, false, new BasicConstraints(false)); // key usage certificateGenerator.AddExtension( X509Extensions.KeyUsage, true, new KeyUsage(KeyUsage.KeyAgreement | KeyUsage.DataEncipherment | KeyUsage.DigitalSignature)); // extended key usage var usages = new[] { KeyPurposeID.IdKPServerAuth, KeyPurposeID.IdKPClientAuth }; ExtendedKeyUsage extendedKeyUsage = new ExtendedKeyUsage(usages); certificateGenerator.AddExtension(X509Extensions.ExtendedKeyUsage, false, extendedKeyUsage); // Test Policy DerSequence seq = CreatePolicyInformationsSequence("http://www.arsslensoft.com", "Arsslensoft", "1.3.6.1.4.1.23823.1.1.1", "Test Notice"); // certificateGenerator.AddExtension(X509Extensions.CertificatePolicies, false, new DerSequence(certPolicies)); // Authority access List <GeneralSubtree> ees = new List <GeneralSubtree>(); ees.Add(new GeneralSubtree(new GeneralName(GeneralName.UniformResourceIdentifier, "http://www.google.com"))); certificateGenerator.AddExtension(X509Extensions.NameConstraints, true, new NameConstraints(null, ees)); certificateGenerator.AddExtension(X509Extensions.NetscapeComment, true, new DerVisibleString("NS COMMENT")); certificateGenerator.AddExtension(X509Extensions.NetscapeBaseUrl, true, new DerIA5String("http://www.google.com")); certificateGenerator.AddExtension(X509Extensions.InhibitAnyPolicy, true, new DerInteger(12)); // Policy constraints byte inhibit = 12; byte explicitc = 12; // certificateGenerator.AddExtension(X509Extensions.PolicyConstraints, false, new DerOctetSequence(new byte[] { 128, 1, explicitc, 129, 1, inhibit })); certificateGenerator.AddExtension(X509Extensions.NetscapeCertUsage, false, new KeyUsage(KeyUsage.KeyAgreement)); certificateGenerator.AddExtension(X509Extensions.AuthorityInfoAccess, false, CreateAuthorityAccessInformationSequence("http://www.arsslensoft.com", null)); // Subhect Issuer Alternative name GeneralName altName = new GeneralName(GeneralName.DnsName, "localhost"); GeneralNames subjectAltName = new GeneralNames(altName); certificateGenerator.AddExtension(X509Extensions.IssuerAlternativeName, false, subjectAltName); certificateGenerator.AddExtension(X509Extensions.SubjectAlternativeName, false, subjectAltName); // certificateGenerator.AddExtension(new DerObjectIdentifier("2.16.840.1.11730.29.53"), false, subjectAltName); // GeneralNames s; //CRL Distribution Points DistributionPointName distPointOne = new DistributionPointName(new GeneralNames( new GeneralName(GeneralName.UniformResourceIdentifier, "http://crl.somewebsite.com/master.crl"))); GeneralNames gns = new GeneralNames(new GeneralName[] { new GeneralName(GeneralName.UniformResourceIdentifier, "ldap://crl.somewebsite.com/cn%3dSecureCA%2cou%3dPKI%2co%3dCyberdyne%2cc%3dUS?certificaterevocationlist;binary"), new GeneralName(GeneralName.Rfc822Name, "Arslen") }); DistributionPointName distPointTwo = new DistributionPointName(gns); DistributionPoint[] distPoints = new DistributionPoint[2]; distPoints[0] = new DistributionPoint(distPointOne, null, null); distPoints[1] = new DistributionPoint(distPointTwo, null, gns); IssuingDistributionPoint iss = new IssuingDistributionPoint(distPointOne, false, true, null, false, false); certificateGenerator.AddExtension(X509Extensions.IssuingDistributionPoint, false, iss); certificateGenerator.AddExtension(X509Extensions.CrlDistributionPoints, false, new CrlDistPoint(distPoints)); // Biometric Asn1EncodableVector v = new Asn1EncodableVector(); BiometricData bdat = new BiometricData(new TypeOfBiometricData(TypeOfBiometricData.HandwrittenSignature), new AlgorithmIdentifier(new DerObjectIdentifier("1.3.14.3.2.26")), new DerOctetString(new byte[] { 169, 74, 143, 229, 204, 177, 155, 166, 28, 76, 8, 115, 211, 145, 233, 135, 152, 47, 187, 211 }), new DerIA5String("http://www.google.com")); v.Add(bdat); v.Add(new BiometricData(new TypeOfBiometricData(TypeOfBiometricData.HandwrittenSignature), new AlgorithmIdentifier(new DerObjectIdentifier("1.3.14.3.2.26")), new DerOctetString(new byte[] { 169, 74, 143, 229, 204, 177, 155, 166, 28, 76, 8, 115, 211, 145, 233, 135, 152, 47, 187, 211 }), new DerIA5String("http://www.google.co"))); certificateGenerator.AddExtension(X509Extensions.BiometricInfo, false, new DerSequenceOf(v)); QCStatement st = new QCStatement(Rfc3739QCObjectIdentifiers.IdQcs); certificateGenerator.AddExtension(X509Extensions.QCStatements, false, st); //Al.Security.Pkcs.Pkcs10CertificationRequest c = new Al.Security.Pkcs.Pkcs10CertificationRequest( //certificateGenerator.AddExtension(X509Extensions.ReasonCode, false, ce); // test done certificateGenerator.AddExtension(X509Extensions.SubjectInfoAccess, false, CreateAuthorityAccessInformationSequence("http://www.arsslensoft.com", null)); //// 2 //TargetInformation ti = new Al.Security.Asn1.X509.TargetInformation(new Target[] { new Target(Target.Choice.Name, new GeneralName(GeneralName.UniformResourceIdentifier, "http://www.go.com")) }); //certificateGenerator.AddExtension(X509Extensions.TargetInformation, false, new DerSequence(ti)); // 3 PrivateKeyUsagePeriod kup = new PrivateKeyUsagePeriod(DateTime.Now, DateTime.Now.AddYears(2)); certificateGenerator.AddExtension(X509Extensions.PrivateKeyUsagePeriod, false, new DerSequence(kup)); //generate var issuerKeyPair = subjectKeyPair; var certificate = certificateGenerator.Generate(issuerKeyPair.Private, random); StreamWriter wstr = new StreamWriter(Path.ChangeExtension("D:\\test.crt", ".pem"), false); PemWriter pemWriter = new PemWriter(wstr); pemWriter.WriteObject(certificate); pemWriter.WriteObject(issuerKeyPair.Private); wstr.Flush(); wstr.Close(); // System.Security.Cryptography.X509Certificates.X509Certificate x509_ = DotNetUtilities.ToX509Certificate(certificate.CertificateStructure); //File.WriteAllBytes(@"D:\\test.crt", x509_.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Pkcs12)); }
public virtual bool Match(object obj) { X509Certificate x509Certificate = obj as X509Certificate; if (x509Certificate == null) { return(false); } if (!X509CertStoreSelector.MatchExtension(this.authorityKeyIdentifier, x509Certificate, X509Extensions.AuthorityKeyIdentifier)) { return(false); } if (this.basicConstraints != -1) { int num = x509Certificate.GetBasicConstraints(); if (this.basicConstraints == -2) { if (num != -1) { return(false); } } else if (num < this.basicConstraints) { return(false); } } if (this.certificate != null && !this.certificate.Equals(x509Certificate)) { return(false); } if (this.certificateValid != null && !x509Certificate.IsValid(this.certificateValid.Value)) { return(false); } if (this.extendedKeyUsage != null) { IList list = x509Certificate.GetExtendedKeyUsage(); if (list != null) { foreach (DerObjectIdentifier derObjectIdentifier in this.extendedKeyUsage) { if (!list.Contains(derObjectIdentifier.Id)) { return(false); } } } } if (this.issuer != null && !this.issuer.Equivalent(x509Certificate.IssuerDN, true)) { return(false); } if (this.keyUsage != null) { bool[] array = x509Certificate.GetKeyUsage(); if (array != null) { for (int i = 0; i < 9; i++) { if (this.keyUsage[i] && !array[i]) { return(false); } } } } if (this.policy != null) { Asn1OctetString extensionValue = x509Certificate.GetExtensionValue(X509Extensions.CertificatePolicies); if (extensionValue == null) { return(false); } Asn1Sequence instance = Asn1Sequence.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue)); if (this.policy.Count < 1 && instance.Count < 1) { return(false); } bool flag = false; foreach (PolicyInformation policyInformation in instance) { if (this.policy.Contains(policyInformation.PolicyIdentifier)) { flag = true; break; } } if (!flag) { return(false); } } if (this.privateKeyValid != null) { Asn1OctetString extensionValue2 = x509Certificate.GetExtensionValue(X509Extensions.PrivateKeyUsagePeriod); if (extensionValue2 == null) { return(false); } PrivateKeyUsagePeriod instance2 = PrivateKeyUsagePeriod.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue2)); DateTime value = this.privateKeyValid.Value; DateTime value2 = instance2.NotAfter.ToDateTime(); DateTime value3 = instance2.NotBefore.ToDateTime(); if (value.CompareTo(value2) > 0 || value.CompareTo(value3) < 0) { return(false); } } return((this.serialNumber == null || this.serialNumber.Equals(x509Certificate.SerialNumber)) && (this.subject == null || this.subject.Equivalent(x509Certificate.SubjectDN, true)) && X509CertStoreSelector.MatchExtension(this.subjectKeyIdentifier, x509Certificate, X509Extensions.SubjectKeyIdentifier) && (this.subjectPublicKey == null || this.subjectPublicKey.Equals(X509CertStoreSelector.GetSubjectPublicKey(x509Certificate))) && (this.subjectPublicKeyAlgID == null || this.subjectPublicKeyAlgID.Equals(X509CertStoreSelector.GetSubjectPublicKey(x509Certificate).AlgorithmID))); }
public virtual bool Match(object obj) { X509Certificate x509Certificate = obj as X509Certificate; if (x509Certificate == null) { return(false); } if (!MatchExtension(authorityKeyIdentifier, x509Certificate, X509Extensions.AuthorityKeyIdentifier)) { return(false); } if (basicConstraints != -1) { int num = x509Certificate.GetBasicConstraints(); if (basicConstraints == -2) { if (num != -1) { return(false); } } else if (num < basicConstraints) { return(false); } } if (certificate != null && !certificate.Equals(x509Certificate)) { return(false); } if (certificateValid != null && !x509Certificate.IsValid(certificateValid.Value)) { return(false); } if (extendedKeyUsage != null) { IList list = x509Certificate.GetExtendedKeyUsage(); if (list != null) { foreach (DerObjectIdentifier item in extendedKeyUsage) { if (!list.Contains(item.Id)) { return(false); } } } } if (issuer != null && !issuer.Equivalent(x509Certificate.IssuerDN, inOrder: true)) { return(false); } if (keyUsage != null) { bool[] array = x509Certificate.GetKeyUsage(); if (array != null) { for (int i = 0; i < 9; i++) { if (keyUsage[i] && !array[i]) { return(false); } } } } if (policy != null) { Asn1OctetString extensionValue = x509Certificate.GetExtensionValue(X509Extensions.CertificatePolicies); if (extensionValue == null) { return(false); } Asn1Sequence instance = Asn1Sequence.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue)); if (policy.Count < 1 && instance.Count < 1) { return(false); } bool flag = false; foreach (PolicyInformation item2 in instance) { if (policy.Contains(item2.PolicyIdentifier)) { flag = true; break; } } if (!flag) { return(false); } } if (privateKeyValid != null) { Asn1OctetString extensionValue2 = x509Certificate.GetExtensionValue(X509Extensions.PrivateKeyUsagePeriod); if (extensionValue2 == null) { return(false); } PrivateKeyUsagePeriod instance2 = PrivateKeyUsagePeriod.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue2)); DateTime value = privateKeyValid.Value; DateTime dateTime = instance2.NotAfter.ToDateTime(); DateTime dateTime2 = instance2.NotBefore.ToDateTime(); if (value.CompareTo((object)dateTime) > 0 || value.CompareTo((object)dateTime2) < 0) { return(false); } } if (serialNumber != null && !serialNumber.Equals(x509Certificate.SerialNumber)) { return(false); } if (subject != null && !subject.Equivalent(x509Certificate.SubjectDN, inOrder: true)) { return(false); } if (!MatchExtension(subjectKeyIdentifier, x509Certificate, X509Extensions.SubjectKeyIdentifier)) { return(false); } if (subjectPublicKey != null && !subjectPublicKey.Equals(GetSubjectPublicKey(x509Certificate))) { return(false); } if (subjectPublicKeyAlgID != null && !subjectPublicKeyAlgID.Equals(GetSubjectPublicKey(x509Certificate).AlgorithmID)) { return(false); } return(true); }
public virtual bool Match(object obj) { X509Certificate x509Certificate = obj as X509Certificate; if (x509Certificate == null) { return(false); } if (!MatchExtension(authorityKeyIdentifier, x509Certificate, X509Extensions.AuthorityKeyIdentifier)) { return(false); } if (basicConstraints != -1) { int num = x509Certificate.GetBasicConstraints(); if (basicConstraints == -2) { if (num != -1) { return(false); } } else if (num < basicConstraints) { return(false); } } if (certificate != null && !certificate.Equals(x509Certificate)) { return(false); } if (certificateValid != null && !x509Certificate.IsValid(certificateValid.Value)) { return(false); } if (extendedKeyUsage != null) { global::System.Collections.IList list = x509Certificate.GetExtendedKeyUsage(); if (list != null) { { global::System.Collections.IEnumerator enumerator = ((global::System.Collections.IEnumerable)extendedKeyUsage).GetEnumerator(); try { while (enumerator.MoveNext()) { DerObjectIdentifier derObjectIdentifier = (DerObjectIdentifier)enumerator.get_Current(); if (!list.Contains((object)derObjectIdentifier.Id)) { return(false); } } } finally { global::System.IDisposable disposable = enumerator as global::System.IDisposable; if (disposable != null) { disposable.Dispose(); } } } } } if (issuer != null && !issuer.Equivalent(x509Certificate.IssuerDN, inOrder: true)) { return(false); } if (keyUsage != null) { bool[] array = x509Certificate.GetKeyUsage(); if (array != null) { for (int i = 0; i < 9; i++) { if (keyUsage[i] && !array[i]) { return(false); } } } } if (policy != null) { Asn1OctetString extensionValue = x509Certificate.GetExtensionValue(X509Extensions.CertificatePolicies); if (extensionValue == null) { return(false); } Asn1Sequence instance = Asn1Sequence.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue)); if (((global::System.Collections.ICollection)policy).get_Count() < 1 && instance.Count < 1) { return(false); } bool flag = false; { global::System.Collections.IEnumerator enumerator = instance.GetEnumerator(); try { while (enumerator.MoveNext()) { PolicyInformation policyInformation = (PolicyInformation)enumerator.get_Current(); if (policy.Contains(policyInformation.PolicyIdentifier)) { flag = true; break; } } } finally { global::System.IDisposable disposable2 = enumerator as global::System.IDisposable; if (disposable2 != null) { disposable2.Dispose(); } } } if (!flag) { return(false); } } if (privateKeyValid != null) { Asn1OctetString extensionValue2 = x509Certificate.GetExtensionValue(X509Extensions.PrivateKeyUsagePeriod); if (extensionValue2 == null) { return(false); } PrivateKeyUsagePeriod instance2 = PrivateKeyUsagePeriod.GetInstance(X509ExtensionUtilities.FromExtensionValue(extensionValue2)); global::System.DateTime value = privateKeyValid.Value; global::System.DateTime dateTime = instance2.NotAfter.ToDateTime(); global::System.DateTime dateTime2 = instance2.NotBefore.ToDateTime(); if (value.CompareTo((object)dateTime) > 0 || value.CompareTo((object)dateTime2) < 0) { return(false); } } if (serialNumber != null && !serialNumber.Equals(x509Certificate.SerialNumber)) { return(false); } if (subject != null && !subject.Equivalent(x509Certificate.SubjectDN, inOrder: true)) { return(false); } if (!MatchExtension(subjectKeyIdentifier, x509Certificate, X509Extensions.SubjectKeyIdentifier)) { return(false); } if (subjectPublicKey != null && !subjectPublicKey.Equals(GetSubjectPublicKey(x509Certificate))) { return(false); } if (subjectPublicKeyAlgID != null && !subjectPublicKeyAlgID.Equals(GetSubjectPublicKey(x509Certificate).AlgorithmID)) { return(false); } return(true); }