Search code examples
c#.net-assemblyconflicting-libraries

How to solve 'Cannot implicitly convert' error when two assemblies have the same namespace?


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
}

  }
}

Solution

  • 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.