-1

I am programming certificate generation, but I am experiancing something like assembly namespace conflict between BouncyCastle and ITextSharp-LGPL-4.1.6.

So I tried to add an alias to the BouncyCastle library that I am using, and tried explicit conversion, but nothing worked.

return (BouncyCastleCrypto::Org.BouncyCastle.X509.X509Certificate)cert.Generate(pair.Private);

And the result is that I get an error:

CS0029 Cannot implicitly convert type 'Org.BouncyCastle.X509.X509Certificate [C:\XXX\packages\BouncyCastle.1.8.4\lib\BouncyCastle.Crypto.dll]' to 'Org.BouncyCastle.X509.X509Certificate [C:\XXX\packages\iTextSharp-LGPL.4.1.6\lib\iTextSharp.dll]' Project C:\xxx\src\MyCode.cs

I have no idea what to do now, I am out of options.

EDIT1:

Question is not about accessing the type itself, its about calling the method of the type.

I have this part of code:

BouncyCastleCrypto::Org.BouncyCastle.X509.X509V3CertificateGenerator cert = new BouncyCastleCrypto::Org.BouncyCastle.X509.X509V3CertificateGenerator();

And then I have this part of code:

return cert.Generate(pair.Private);

Which gives me the error. How am I supposed to clarify which assembly I am using when this is just method call?

EDIT2:

Okay so I am pasting whole file here :

extern alias BouncyCastleCrypto;
using BouncyCastleCrypto.Org.BouncyCastle.Crypto.Operators;
using BouncyCastleCrypto.Org.BouncyCastle.Asn1.Pkcs;
using BouncyCastleCrypto.Org.BouncyCastle.Asn1.X509;
using BouncyCastleCrypto.Org.BouncyCastle.Crypto;
using BouncyCastleCrypto.Org.BouncyCastle.Crypto.Generators;
using BouncyCastleCrypto.Org.BouncyCastle.Crypto.Prng;
using BouncyCastleCrypto.Org.BouncyCastle.OpenSsl;
using BouncyCastleCrypto.Org.BouncyCastle.Pkcs;
using BouncyCastleCrypto.Org.BouncyCastle.Security;
using XXXX.ViewModels;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using BouncyCastleCrypto.Org.BouncyCastle.X509;
using BouncyCastleCrypto.Org.BouncyCastle.Math;

namespace XXXX.Helpers
{
  public static class PKIHelper
  {


public static Org.BouncyCastle.X509.X509Certificate GenCert(CertInfo info)
{
  RsaKeyPairGenerator _rsa = new RsaKeyPairGenerator();
  SecureRandom _random = new SecureRandom();

  _rsa.Init(new KeyGenerationParameters(_random, info.rsa_strength));
  AsymmetricCipherKeyPair _pair = _rsa.GenerateKeyPair();

  X509Name _cert_name = new X509Name("CN=" + info.name);
  BigInteger _serialnumber = BigInteger.ProbablePrime(120, new Random());

  BouncyCastleCrypto::Org.BouncyCastle.X509.X509V3CertificateGenerator _cert = new BouncyCastleCrypto::Org.BouncyCastle.X509.X509V3CertificateGenerator();
  _cert.SetSerialNumber(_serialnumber);
  _cert.SetSubjectDN(_cert_name);
  _cert.SetIssuerDN(_cert_name);
  _cert.SetNotBefore(info.begin_date);
  _cert.SetNotAfter(info.expire_date);
  _cert.SetSignatureAlgorithm("SHA1withRSA");
  _cert.SetPublicKey(_pair.Public);

  _cert.AddExtension(X509Extensions.ExtendedKeyUsage.Id, false,
      new AuthorityKeyIdentifier(
          SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(_pair.Public),
          new GeneralNames(new GeneralName(_cert_name)), _serialnumber));
  _cert.AddExtension(X509Extensions.ExtendedKeyUsage.Id, false,
      new ExtendedKeyUsage(new[] { KeyPurposeID.IdKPServerAuth }));


  return _cert.Generate(_pair.Private); // here's error
}

  }
}
Friedl Crafts
  • 47
  • 1
  • 7
  • 1
    Possible duplicate of [Type exists in 2 assemblies](https://stackoverflow.com/questions/9194495/type-exists-in-2-assemblies) – devNull Jan 31 '19 at 13:05
  • Please check this URL https://stackoverflow.com/questions/13798879/converting-x509certificate2-certificate-into-bouncycastle-x509certificate Might be helpful – Jabir E Jan 31 '19 at 13:08
  • Nope, not a duplicate, check the edit @devNull – Friedl Crafts Jan 31 '19 at 13:45
  • *Question is not about accessing the type itself, its about calling the method of the type* it's the same thing – Liam Jan 31 '19 at 13:47
  • Then I simply dont know how to do that, cause I've tried to give a full qualified name with the extern alias and its just not working. – Friedl Crafts Jan 31 '19 at 13:50
  • @FriedlCrafts Did you try the steps posted in the answer in the link I sent? I just replicated your error and was able to resolve it using those steps. Make sure to specify `extern alias BouncyCastleCrypto;` in the file where you're trying to access `X509V3CertificateGenerator` – devNull Jan 31 '19 at 14:21
  • iTextSharp uses BouncyCastle 1.7 and you are using BouncyCastle 1.8. Try what devNull posted, it should work. – LeonidasFett Jan 31 '19 at 14:23
  • If that still doesn't work, you may need to post more of your code (e.g. the entire file including the using statements, the `.csproj` file for the project) in case the issue lies somewhere else – devNull Jan 31 '19 at 14:39
  • @devNull Yes I did try, and I am using `extern alias BouncyCastleCrypto;`, and I am trying to access `X509V3CertificateGenerator`, and it still gives me an error. I will make another edit. – Friedl Crafts Feb 01 '19 at 08:45
  • Also I am using .net 4.7.2 and some assembly bindings, but both of the libraries in conflict are NuGet packages. – Friedl Crafts Feb 01 '19 at 09:14

1 Answers1

1

Okay so the thing is that I was pretty sure I was using the right type as return type of the GeneratePKI method , which was Org.BouncyCastle.X509.X509Certificate, but in reality the Org.BouncyCastle.X509.X509Certificate was from iTextSharp library, and so the compiler thought it has to covnert it implicitly. When I added the alias before the method return type BouncyCastleCrypto::Org.BouncyCastle.X509.X509Certificate, it all magically started compiling again. Thanks @devNull, for not abandoning me.

Friedl Crafts
  • 47
  • 1
  • 7