private void SetKey(byte[] keyData, string keyType) { int key_blob_size = 0; _key_blob = null; bool result = Crypt32.CryptDecodeObject(Crypt32.X509_ASN_ENCODING, keyType, keyData, keyData.Length, 0, ref _key_blob, ref key_blob_size); _key_blob = new byte[key_blob_size]; result = Crypt32.CryptDecodeObject(Crypt32.X509_ASN_ENCODING, keyType, keyData, keyData.Length, 0, ref _key_blob, ref key_blob_size); import_key(_key_blob); }
static X509CertificateRequestType getRequestFormat(Byte[] rawData) { UInt32 pcbStructInfo = 0; if (Crypt32.CryptDecodeObject(65537, Wincrypt.X509_CERT_REQUEST_TO_BE_SIGNED, rawData, (UInt32)rawData.Length, 8, IntPtr.Zero, ref pcbStructInfo)) { return(X509CertificateRequestType.PKCS10); } try { new X509CertificateRequestCmc(rawData); return(X509CertificateRequestType.PKCS7); } catch { return(X509CertificateRequestType.Invalid); } }
static X509CertificateRequestType getRequestFormat(Byte[] rawData) { UInt32 pcbStructInfo = 0; if (Crypt32.CryptDecodeObject(65537, Wincrypt.X509_CERT_REQUEST_TO_BE_SIGNED, rawData, (UInt32)rawData.Length, 8, IntPtr.Zero, ref pcbStructInfo)) { return(X509CertificateRequestType.PKCS10); } try { PKCS7SignedMessage temp = new PKCS7SignedMessage(rawData); return(temp.Content is X509CertificateRequest[] ? X509CertificateRequestType.PKCS7 : X509CertificateRequestType.Invalid); } catch { return(X509CertificateRequestType.Invalid); } }
void m_decode(Byte[] rawData) { UInt32 pcbStructInfo = 0; if (Crypt32.CryptDecodeObject(1, "1.3.6.1.4.1.311.21.7", rawData, (UInt32)rawData.Length, 0, IntPtr.Zero, ref pcbStructInfo)) { IntPtr pbStructInfo = Marshal.AllocHGlobal((Int32)pcbStructInfo); Crypt32.CryptDecodeObject(1, "1.3.6.1.4.1.311.21.7", rawData, (UInt32)rawData.Length, 0, pbStructInfo, ref pcbStructInfo); Wincrypt.CERT_TEMPLATE_EXT structure = (Wincrypt.CERT_TEMPLATE_EXT)Marshal.PtrToStructure(pbStructInfo, typeof(Wincrypt.CERT_TEMPLATE_EXT)); Marshal.FreeHGlobal(pbStructInfo); TemplateOid = new Oid(structure.pszObjId); MajorVersion = (Int32)structure.dwMajorVersion; MinorVersion = (Int32)structure.dwMinorVersion; } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
void getSignature() { UInt32 pcbStructInfo = 0; if (!Crypt32.CryptDecodeObject(65537, Wincrypt.X509_CERT, RawData, (UInt32)RawData.Length, 8, IntPtr.Zero, ref pcbStructInfo)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr pvStructInfo = Marshal.AllocHGlobal((Int32)pcbStructInfo); Crypt32.CryptDecodeObject(65537, Wincrypt.X509_CERT, RawData, (UInt32)RawData.Length, 8, pvStructInfo, ref pcbStructInfo); signedData = (Wincrypt.CERT_SIGNED_CONTENT_INFO)Marshal.PtrToStructure(pvStructInfo, typeof(Wincrypt.CERT_SIGNED_CONTENT_INFO)); signature = new Byte[signedData.Signature.cbData]; Marshal.Copy(signedData.Signature.pbData, signature, 0, (Int32)signedData.Signature.cbData); sigUnused = signedData.Signature.cUnusedBits; SignatureAlgorithm = new Oid(signedData.SignatureAlgorithm.pszObjId); Array.Reverse(signature); Marshal.FreeHGlobal(pvStructInfo); }
void m_initialize2(Wincrypt.CRYPTOAPI_BLOB blob) { UInt32 pcbStructInfo = 0; if (!Crypt32.CryptDecodeObject(1, Wincrypt.PKCS_CONTENT_INFO, blob.pbData, blob.cbData, 0, IntPtr.Zero, ref pcbStructInfo)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr pvStructInfo = Marshal.AllocHGlobal((Int32)pcbStructInfo); if (!Crypt32.CryptDecodeObject(1, Wincrypt.PKCS_CONTENT_INFO, blob.pbData, blob.cbData, 0, pvStructInfo, ref pcbStructInfo)) { Marshal.FreeHGlobal(pvStructInfo); throw new Win32Exception(Marshal.GetLastWin32Error()); } Wincrypt.CRYPT_CONTENT_INFO contentInfo = (Wincrypt.CRYPT_CONTENT_INFO)Marshal.PtrToStructure(pvStructInfo, typeof(Wincrypt.CRYPT_CONTENT_INFO)); ContentType = new Oid(contentInfo.pszObjId); RawData = new Byte[contentInfo.cbData]; Marshal.Copy(contentInfo.pbData, RawData, 0, (Int32)contentInfo.cbData); Marshal.FreeHGlobal(pvStructInfo); }
void getSequence(ContentInfo2 info) { UInt32 pcbStructInfo = 0; if (!Crypt32.CryptDecodeObject(1, Wincrypt.X509_SEQUENCE_OF_ANY, info.RawData, (UInt32)info.RawData.Length, 0, IntPtr.Zero, ref pcbStructInfo)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr pvStructInfo = Marshal.AllocHGlobal((Int32)pcbStructInfo); try { Crypt32.CryptDecodeObject(1, Wincrypt.X509_SEQUENCE_OF_ANY, info.RawData, (UInt32)info.RawData.Length, 0, pvStructInfo, ref pcbStructInfo); Wincrypt.CRYPTOAPI_BLOB sequence = (Wincrypt.CRYPTOAPI_BLOB)Marshal.PtrToStructure(pvStructInfo, typeof(Wincrypt.CRYPTOAPI_BLOB)); if (sequence.cbData == 0) { return; } UnrollPkcs7(sequence); } finally { Marshal.FreeHGlobal(pvStructInfo); } }
void m_decode() { UInt32 pcbStructInfo = 0; if (Crypt32.CryptDecodeObject(65537, Wincrypt.X509_CERT_REQUEST_TO_BE_SIGNED, RawData, (UInt32)RawData.Length, 0, IntPtr.Zero, ref pcbStructInfo)) { IntPtr pvStructInfo = Marshal.AllocHGlobal((Int32)pcbStructInfo); Crypt32.CryptDecodeObject(65537, Wincrypt.X509_CERT_REQUEST_TO_BE_SIGNED, RawData, (UInt32)RawData.Length, 0, pvStructInfo, ref pcbStructInfo); reqData = (Wincrypt.CERT_REQUEST_INFO)Marshal.PtrToStructure(pvStructInfo, typeof(Wincrypt.CERT_REQUEST_INFO)); Version = (Int32)reqData.dwVersion + 1; getSubject(); getPublickey(); getAttributes(); getSignature(); m_verifysignature(); Marshal.FreeHGlobal(pvStructInfo); } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
void decodeCMC(Byte[] contentBytes) { Asn1Reader asn = new Asn1Reader(contentBytes); asn.MoveNext(); UInt32 pcbStructInfo = 0; if (!Crypt32.CryptDecodeObject(1, Wincrypt.CMC_DATA, asn.GetTagRawData(), (UInt32)asn.TagLength, 0, IntPtr.Zero, ref pcbStructInfo)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr pvStructInfo = Marshal.AllocHGlobal((Int32)pcbStructInfo); try { Crypt32.CryptDecodeObject(1, Wincrypt.CMC_DATA, asn.GetTagRawData(), (UInt32)asn.TagLength, 0, pvStructInfo, ref pcbStructInfo); Wincrypt.CMC_DATA_INFO cmc = (Wincrypt.CMC_DATA_INFO)Marshal.PtrToStructure(pvStructInfo, typeof(Wincrypt.CMC_DATA_INFO)); decodeAttributes(cmc.rgTaggedAttribute, cmc.cTaggedAttribute); decodeRequest(cmc.rgTaggedRequest, cmc.cTaggedRequest); Content = requests.ToArray(); } finally { Marshal.FreeHGlobal(pvStructInfo); } }
void m_initialize(Byte[] rawData) { UInt32 pcbStructInfo = 0; if (!Crypt32.CryptDecodeObject(1, Wincrypt.CMS_SIGNER_INFO, rawData, (UInt32)rawData.Length, 0, IntPtr.Zero, ref pcbStructInfo)) { return; } IntPtr pvStructInfo = Marshal.AllocHGlobal((Int32)pcbStructInfo); Crypt32.CryptDecodeObject(1, Wincrypt.CMS_SIGNER_INFO, rawData, (UInt32)rawData.Length, 0, pvStructInfo, ref pcbStructInfo); try { Wincrypt.CMSG_CMS_SIGNER_INFO info = (Wincrypt.CMSG_CMS_SIGNER_INFO)Marshal.PtrToStructure(pvStructInfo, typeof(Wincrypt.CMSG_CMS_SIGNER_INFO)); Version = (Int32)info.dwVersion; decodeSubject(info.SignerId); decodeHashAlg(info.HashAlgorithm); decodeEncryptedHashAlg(info.HashEncryptionAlgorithm); decodeEncryptedHash(info.EncryptedHash); decodeAuthAttr(info.AuthAttrs); decodeUnauthAttr(info.UnauthAttrs); } finally { Marshal.FreeHGlobal(pvStructInfo); } }