public static X509Certificate2 GetX509Certificate2(IntPtr certHandle) { try { if (IsUnix) { CERT_CONTEXT contextCert = Marshal.PtrToStructure <CERT_CONTEXT>(certHandle); byte[] ctx = new byte[contextCert.cbCertEncoded]; Marshal.Copy(contextCert.pbCertEncoded, ctx, 0, ctx.Length); return(new X509Certificate2(ctx)); } else { return(new X509Certificate2(certHandle)); } } catch (Exception ex) { throw new Exception($"Ошибка при попытке преобразовать значение указателя на сертификат к объекту вида X509Certificate2. {ex.Message}."); } }
internal static CERT_INFO GetCertInfo(SafeCertContextHandle certificateContext) { Debug.Assert(certificateContext != null, "certificateContext != null"); Debug.Assert(!certificateContext.IsClosed && !certificateContext.IsInvalid, "!certificateContext.IsClosed && !certificateContext.IsInvalid"); bool addedRef = false; RuntimeHelpers.PrepareConstrainedRegions(); try { certificateContext.DangerousAddRef(ref addedRef); CERT_CONTEXT context = (CERT_CONTEXT)Marshal.PtrToStructure(certificateContext.DangerousGetHandle(), typeof(CERT_CONTEXT)); return((CERT_INFO)Marshal.PtrToStructure(context.pCertInfo, typeof(CERT_INFO))); } finally { if (addedRef) { certificateContext.DangerousRelease(); } } }
public static extern unsafe CERT_CONTEXT* CertEnumCertificatesInStore(IntPtr hCertStore, CERT_CONTEXT* pPrevCertContext);
// // This returns an allocated native memory block. Its lifetime (and that of any allocated subblocks it may point to) is that of "hb". // private static unsafe CERT_ID EncodeRecipientId(CmsRecipient recipient, SafeCertContextHandle hCertContext, CERT_CONTEXT* pCertContext, CERT_INFO* pCertInfo, HeapBlockRetainer hb) { CERT_ID recipientId = default(CERT_ID); SubjectIdentifierType type = recipient.RecipientIdentifierType; switch (type) { case SubjectIdentifierType.IssuerAndSerialNumber: { recipientId.dwIdChoice = CertIdChoice.CERT_ID_ISSUER_SERIAL_NUMBER; recipientId.u.IssuerSerialNumber.Issuer = pCertInfo->Issuer; recipientId.u.IssuerSerialNumber.SerialNumber = pCertInfo->SerialNumber; break; } case SubjectIdentifierType.SubjectKeyIdentifier: { byte[] ski = hCertContext.GetSubjectKeyIdentifer(); IntPtr pSki = hb.AllocBytes(ski); recipientId.dwIdChoice = CertIdChoice.CERT_ID_KEY_IDENTIFIER; recipientId.u.KeyId.cbData = (uint)(ski.Length); recipientId.u.KeyId.pbData = pSki; break; } default: // The public contract for CmsRecipient guarantees that SubjectKeyIdentifier and IssuerAndSerialNumber are the only two possibilities. Debug.Fail($"Unexpected SubjectIdentifierType: {type}"); throw new NotSupportedException(SR.Format(SR.Cryptography_Cms_Invalid_Subject_Identifier_Type, type)); } return recipientId; }