Estoy desarrollando una app en Xamarin.Android en la que tengo que insertarle una firma digital a un PDF.
Cuando llamo a la clase que firma dicho PDF, me da el siguiente error:
Unhandled Exception:
System.TypeLoadException: Could not resolve type with token 01000026 (from typeref, class/assembly System.Security.Cryptography.Pkcs.SignedCms, System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)
Esta excepción ocurre en la línea 122 del código (byte[] bytPK = SignMsg(objSHA1.Hash, Certificate, false, Source, TSA);
)
Dejo aquí el código de la clase que uso para firmar a ver si alguien puede encontrar el error, ya que hace un par de días que estoy tratando de solucionarlo y no estoy pudiendo.
public static class PDF
{
/// <param name="Source">Documento origen</param>
/// <param name="Target">Documento destino</param>
/// <param name="Certificate">Certificado a utilizar</param>
/// <param name="Reason">Razón de la firma</param>
/// <param name="Location">Ubicación</param>
/// <param name="AddVisibleSign">Establece si hay que agregar la firma visible al documento</param>
/// FormatPlace. Posibles valores para Lugar de la firma:
/// A4TL: A4 normal, arriba a la izquierda
/// A4TR: A4 normal, arriba a la derecha
/// A4BL: A4 normal, abajo a la izquierda
/// A4BR: A4 normal, abajo a la derecha
/// A4aTL: A4 apaisado, arriba a la izquierda
/// A4aTR: A4 apaisado, arriba a la derecha
/// A4aBL: A4 apaisado, abajo a la izquierda
/// A4aBR: A4 apaisado, abajo a la derecha
public static void SignHashed(string Source, string Target, SysX509.X509Certificate2 Certificate, string Reason, string Location, bool AddVisibleSign, string FormatPlace, string TSA)
{
X509CertificateParser objCP = new X509CertificateParser();
X509Certificate[] objChain = new X509Certificate[] { objCP.ReadCertificate(Certificate.RawData) };
PdfReader objReader = new PdfReader(Source);
PdfStamper objStamper = PdfStamper.CreateSignature(objReader, new FileStream(Target, FileMode.Create), '\0');
PdfSignatureAppearance objSA = objStamper.SignatureAppearance;
int LastPage = objReader.NumberOfPages;
if (AddVisibleSign)
{
switch (FormatPlace)
{
case "A4TL":
objSA.SetVisibleSignature(new Rectangle(50, 750, 300, 780), LastPage, null);
break;
case "A4TR":
objSA.SetVisibleSignature(new Rectangle(350, 750, 600, 780), LastPage, null);
break;
case "A4BL":
objSA.SetVisibleSignature(new Rectangle(50, 50, 300, 80), LastPage, null);
break;
case "A4BR":
objSA.SetVisibleSignature(new Rectangle(350, 50, 600, 80), LastPage, null);
break;
case "A4aTL":
objSA.SetVisibleSignature(new Rectangle(50, 570, 300, 600), LastPage, null);
break;
case "A4aTR":
objSA.SetVisibleSignature(new Rectangle(530, 570, 780, 600), LastPage, null);
break;
case "A4aBL":
objSA.SetVisibleSignature(new Rectangle(50, 20, 300, 50), LastPage, null);
break;
case "A4aBR":
objSA.SetVisibleSignature(new Rectangle(530, 20, 780, 50), LastPage, null);
break;
}
}
objSA.SignDate = DateTime.Now;
objSA.Certificate = objChain[0];
objSA.Reason = Reason;
objSA.Location = Location;
objSA.Acro6Layers = true;
objSA.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
PdfSignature objSignature = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1);
objSignature.Date = new PdfDate(objSA.SignDate);
objSignature.Name = "Test de MQ Emprendimientos";
if (objSA.Reason != null)
objSignature.Reason = objSA.Reason;
if (objSA.Location != null)
objSignature.Location = objSA.Location;
objSA.CryptoDictionary = objSignature;
int intCSize = 4000;
Hashtable objTable = new Hashtable();
objTable[PdfName.CONTENTS] = intCSize * 2 + 2;
var dict = HashtableToDictionary<PdfName, int>(objTable);
objSA.PreClose(dict);
HashAlgorithm objSHA1 = new SHA1CryptoServiceProvider();
Stream objStream = objSA.GetRangeStream();
int intRead = 0;
byte[] bytBuffer = new byte[8192];
while ((intRead = objStream.Read(bytBuffer, 0, 8192)) > 0)
objSHA1.TransformBlock(bytBuffer, 0, intRead, bytBuffer, 0);
objSHA1.TransformFinalBlock(bytBuffer, 0, 0);
byte[] bytPK = SignMsg(objSHA1.Hash, Certificate, false, Source, TSA);
byte[] bytOut = new byte[intCSize];
PdfDictionary objDict = new PdfDictionary();
Array.Copy(bytPK, 0, bytOut, 0, bytPK.Length);
objDict.Put(PdfName.CONTENTS, new PdfString(bytOut).SetHexWriting(true));
objSA.Close(objDict);
}
public static Dictionary<K, V> HashtableToDictionary<K, V>(Hashtable table)
{
return table
.Cast<DictionaryEntry>()
.ToDictionary(kvp => (K)kvp.Key, kvp => (V)kvp.Value);
}
private static byte[] SignMsg(byte[] Message, SysX509.X509Certificate2 SignerCertificate, bool Detached, string Source, string TSA)
{
//Creamos el contenedor
ContentInfo contentInfo = new ContentInfo(Message);
//Instanciamos el objeto SignedCms con el contenedor
SignedCms objSignedCms = new SignedCms(contentInfo, Detached);
//Creamos el "firmante"
CmsSigner objCmsSigner = new CmsSigner(SignerCertificate);
objCmsSigner.IncludeOption = SysX509.X509IncludeOption.EndCertOnly;
// Sign the CMS/PKCS #7 message. The second argument is
// needed to ask for the pin.
objSignedCms.ComputeSignature(objCmsSigner, false);
//Encodeamos el mensaje CMS/PKCS #7
return objSignedCms.Encode();
}
}
InnerException
que te da. De todas maneras, aqui se exponen varias posibles causas, entre ellas no tener actualizados todos los componentes de xamarin