private void InitFromHandles(CryptMsgSafeHandle messageHandle, LocalBufferSafeHandle signerHandle) { var signerInfo = Marshal.PtrToStructure <CMSG_SIGNER_INFO>(signerHandle.DangerousGetHandle()); var subjectId = new UniversalSubjectIdentifier(signerInfo.Issuer, signerInfo.SerialNumber); var certs = GetCertificatesFromMessage(messageHandle); if (subjectId.Type == SubjectIdentifierType.SubjectKeyIdentifier) { Certificate = FindCertificate((string)subjectId.Value, certs); } else if (subjectId.Type == SubjectIdentifierType.IssuerAndSerialNumber) { Certificate = FindCertificate((X509IssuerSerial)subjectId.Value, certs); } AdditionalCertificates = certs; DigestAlgorithm = new Oid(signerInfo.HashAlgorithm.pszObjId); HashEncryptionAlgorithm = new Oid(signerInfo.HashEncryptionAlgorithm.pszObjId); SerialNumber = ReadBlob(signerInfo.SerialNumber); UnsignedAttributes = ReadAttributes(signerInfo.UnauthAttrs); SignedAttributes = ReadAttributes(signerInfo.AuthAttrs); }
public unsafe AuthenticodeSignature(AsnEncodedData data, ISignature owningSignature) { OwningSignature = owningSignature; Kind = SignatureKind.AuthenticodeTimestamp; AdditionalCertificates = owningSignature.AdditionalCertificates; fixed(byte *dataPtr = data.RawData) { uint size = 0; LocalBufferSafeHandle localBuffer; if (Crypt32.CryptDecodeObjectEx(EncodingType.PKCS_7_ASN_ENCODING | EncodingType.X509_ASN_ENCODING, (IntPtr)500, new IntPtr(dataPtr), (uint)data.RawData.Length, CryptDecodeFlags.CRYPT_DECODE_ALLOC_FLAG, IntPtr.Zero, out localBuffer, ref size)) { using (localBuffer) { var signerInfo = Marshal.PtrToStructure <CMSG_SIGNER_INFO>(localBuffer.DangerousGetHandle()); DigestAlgorithm = new Oid(signerInfo.HashAlgorithm.pszObjId); HashEncryptionAlgorithm = new Oid(signerInfo.HashEncryptionAlgorithm.pszObjId); SerialNumber = ReadBlob(signerInfo.SerialNumber); UnsignedAttributes = ReadAttributes(signerInfo.UnauthAttrs); SignedAttributes = ReadAttributes(signerInfo.AuthAttrs); var subjectId = new UniversalSubjectIdentifier(signerInfo.Issuer, signerInfo.SerialNumber); if (subjectId.Type == SubjectIdentifierType.SubjectKeyIdentifier) { Certificate = FindCertificate((string)subjectId.Value, OwningSignature.AdditionalCertificates); } else if (subjectId.Type == SubjectIdentifierType.IssuerAndSerialNumber) { Certificate = FindCertificate((X509IssuerSerial)subjectId.Value, OwningSignature.AdditionalCertificates); } } } else { throw new InvalidOperationException("Failed to read Authenticode signature"); } } }