internal unsafe SignerInfo(SignedCms signedCms, SignerInfo parentSignerInfo, byte[] encodedSignerInfo) { // Sanity check. Debug.Assert(signedCms != null && encodedSignerInfo != null && encodedSignerInfo.Length > 0); uint cbCmsgSignerInfo = 0; SafeLocalAllocHandle pbCmsgSignerInfo = SafeLocalAllocHandle.InvalidHandle; fixed(byte *pEncodedSignerInfo = &encodedSignerInfo[0]) { if (!CAPI.DecodeObject(new IntPtr(CAPI.PKCS7_SIGNER_INFO), new IntPtr(pEncodedSignerInfo), (uint)encodedSignerInfo.Length, out pbCmsgSignerInfo, out cbCmsgSignerInfo)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } m_signedCms = signedCms; m_parentSignerInfo = parentSignerInfo; m_encodedSignerInfo = (byte[])encodedSignerInfo.Clone(); m_pbCmsgSignerInfo = pbCmsgSignerInfo; m_cmsgSignerInfo = (CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO)); }
private void DecodeExtension() { uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; if (base.Oid.Value == "2.5.29.10") { if (!CAPI.DecodeObject(new IntPtr(13L), base.m_rawData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPIBase.CERT_BASIC_CONSTRAINTS_INFO cert_basic_constraints_info = (CAPIBase.CERT_BASIC_CONSTRAINTS_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_BASIC_CONSTRAINTS_INFO)); byte[] destination = new byte[1]; Marshal.Copy(cert_basic_constraints_info.SubjectType.pbData, destination, 0, 1); this.m_isCA = (destination[0] & 0x80) != 0; this.m_hasPathLenConstraint = cert_basic_constraints_info.fPathLenConstraint; this.m_pathLenConstraint = (int)cert_basic_constraints_info.dwPathLenConstraint; } else { if (!CAPI.DecodeObject(new IntPtr(15L), base.m_rawData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPIBase.CERT_BASIC_CONSTRAINTS2_INFO cert_basic_constraints_info2 = (CAPIBase.CERT_BASIC_CONSTRAINTS2_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_BASIC_CONSTRAINTS2_INFO)); this.m_isCA = cert_basic_constraints_info2.fCA != 0; this.m_hasPathLenConstraint = cert_basic_constraints_info2.fPathLenConstraint != 0; this.m_pathLenConstraint = (int)cert_basic_constraints_info2.dwPathLenConstraint; } this.m_decoded = true; decodedValue.Dispose(); }
private void DecodeExtension() { uint cbDecoded = 0; SafeLocalAllocHandle decoded = null; bool result = CAPI.DecodeObject(new IntPtr(CAPI.X509_KEY_USAGE), m_rawData, out decoded, out cbDecoded); if (result == false) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CRYPTOAPI_BLOB pKeyUsage = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); if (pKeyUsage.cbData > 4) { pKeyUsage.cbData = 4; } byte[] keyUsage = new byte[4]; if (pKeyUsage.pbData != IntPtr.Zero) { Marshal.Copy(pKeyUsage.pbData, keyUsage, 0, (int)pKeyUsage.cbData); } m_keyUsages = BitConverter.ToUInt32(keyUsage, 0); m_decoded = true; decoded.Dispose(); }
private void DecodeExtension() { uint cbDecoded = 0; SafeLocalAllocHandle decoded = null; bool result = CAPI.DecodeObject(new IntPtr(CAPI.X509_ENHANCED_KEY_USAGE), m_rawData, out decoded, out cbDecoded); if (result == false) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CERT_ENHKEY_USAGE pEnhKeyUsage = (CAPI.CERT_ENHKEY_USAGE)Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CERT_ENHKEY_USAGE)); m_enhancedKeyUsages = new OidCollection(); for (int index = 0; index < pEnhKeyUsage.cUsageIdentifier; index++) { IntPtr pszOid = Marshal.ReadIntPtr(new IntPtr((long)pEnhKeyUsage.rgpszUsageIdentifier + index * Marshal.SizeOf(typeof(IntPtr)))); string oidValue = Marshal.PtrToStringAnsi(pszOid); Oid oid = new Oid(oidValue, OidGroup.ExtensionOrAttribute, false); m_enhancedKeyUsages.Add(oid); } m_decoded = true; decoded.Dispose(); }
private static unsafe int FindCertificatePolicyCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData) { string strA = (string)pvCallbackData; if (strA.Length != 0) { CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); IntPtr ptr = CAPISafe.CertFindExtension("2.5.29.32", cert_info.cExtension, cert_info.rgExtension); if (ptr == IntPtr.Zero) { return(1); } CAPIBase.CERT_EXTENSION cert_extension = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_EXTENSION)); byte[] destination = new byte[cert_extension.Value.cbData]; Marshal.Copy(cert_extension.Value.pbData, destination, 0, destination.Length); uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; if (CAPI.DecodeObject(new IntPtr(0x10L), destination, out decodedValue, out cbDecodedValue)) { CAPIBase.CERT_POLICIES_INFO cert_policies_info = (CAPIBase.CERT_POLICIES_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_POLICIES_INFO)); for (int i = 0; i < cert_policies_info.cPolicyInfo; i++) { IntPtr ptr2 = new IntPtr(((long)cert_policies_info.rgPolicyInfo) + (i * Marshal.SizeOf(typeof(CAPIBase.CERT_POLICY_INFO)))); CAPIBase.CERT_POLICY_INFO cert_policy_info = (CAPIBase.CERT_POLICY_INFO)Marshal.PtrToStructure(ptr2, typeof(CAPIBase.CERT_POLICY_INFO)); if (string.Compare(strA, cert_policy_info.pszPolicyIdentifier, StringComparison.OrdinalIgnoreCase) == 0) { return(0); } } } } return(1); }
private static unsafe int FindTemplateNameCallback(System.Security.Cryptography.SafeCertContextHandle safeCertContextHandle, object pvCallbackData) { IntPtr zero = IntPtr.Zero; IntPtr ptr = IntPtr.Zero; CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *)safeCertContextHandle.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); zero = CAPISafe.CertFindExtension("1.3.6.1.4.1.311.20.2", cert_info.cExtension, cert_info.rgExtension); ptr = CAPISafe.CertFindExtension("1.3.6.1.4.1.311.21.7", cert_info.cExtension, cert_info.rgExtension); if ((zero != IntPtr.Zero) || (ptr != IntPtr.Zero)) { if (zero != IntPtr.Zero) { CAPIBase.CERT_EXTENSION cert_extension = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(zero, typeof(CAPIBase.CERT_EXTENSION)); byte[] destination = new byte[cert_extension.Value.cbData]; Marshal.Copy(cert_extension.Value.pbData, destination, 0, destination.Length); uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; if (CAPI.DecodeObject(new IntPtr(0x18L), destination, out decodedValue, out cbDecodedValue)) { CAPIBase.CERT_NAME_VALUE cert_name_value = (CAPIBase.CERT_NAME_VALUE)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_NAME_VALUE)); if (string.Compare(Marshal.PtrToStringUni(cert_name_value.Value.pbData), (string)pvCallbackData, StringComparison.OrdinalIgnoreCase) == 0) { return(0); } } } if (ptr != IntPtr.Zero) { CAPIBase.CERT_EXTENSION cert_extension2 = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_EXTENSION)); byte[] buffer2 = new byte[cert_extension2.Value.cbData]; Marshal.Copy(cert_extension2.Value.pbData, buffer2, 0, buffer2.Length); uint num2 = 0; SafeLocalAllocHandle handle2 = null; if (CAPI.DecodeObject(new IntPtr(0x40L), buffer2, out handle2, out num2)) { CAPIBase.CERT_TEMPLATE_EXT cert_template_ext = (CAPIBase.CERT_TEMPLATE_EXT)Marshal.PtrToStructure(handle2.DangerousGetHandle(), typeof(CAPIBase.CERT_TEMPLATE_EXT)); string strB = System.Security.Cryptography.X509Certificates.X509Utils.FindOidInfo(2, (string)pvCallbackData, System.Security.Cryptography.OidGroup.Template); if (strB == null) { strB = (string)pvCallbackData; } if (string.Compare(cert_template_ext.pszObjId, strB, StringComparison.OrdinalIgnoreCase) == 0) { return(0); } } } } return(1); }
private void DecodeExtension() { uint cbDecoded = 0; SafeLocalAllocHandle decoded = null; if (Oid.Value == CAPI.szOID_BASIC_CONSTRAINTS) { bool result = CAPI.DecodeObject(new IntPtr(CAPI.X509_BASIC_CONSTRAINTS), m_rawData, out decoded, out cbDecoded); if (result == false) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CERT_BASIC_CONSTRAINTS_INFO pBasicConstraints = (CAPI.CERT_BASIC_CONSTRAINTS_INFO)Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CERT_BASIC_CONSTRAINTS_INFO)); // take the first byte. byte[] isCA = new byte[1]; Marshal.Copy(pBasicConstraints.SubjectType.pbData, isCA, 0, 1); m_isCA = (isCA[0] & CAPI.CERT_CA_SUBJECT_FLAG) != 0 ? true : false; m_hasPathLenConstraint = pBasicConstraints.fPathLenConstraint; m_pathLenConstraint = (int)pBasicConstraints.dwPathLenConstraint; } else { bool result = CAPI.DecodeObject(new IntPtr(CAPI.X509_BASIC_CONSTRAINTS2), m_rawData, out decoded, out cbDecoded); if (result == false) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CERT_BASIC_CONSTRAINTS2_INFO pBasicConstraints2 = (CAPI.CERT_BASIC_CONSTRAINTS2_INFO)Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CERT_BASIC_CONSTRAINTS2_INFO)); m_isCA = pBasicConstraints2.fCA == 0 ? false : true; m_hasPathLenConstraint = pBasicConstraints2.fPathLenConstraint == 0 ? false : true; m_pathLenConstraint = (int)pBasicConstraints2.dwPathLenConstraint; } m_decoded = true; decoded.Dispose(); }
private void Decode() { uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = (SafeLocalAllocHandle)null; if (!CAPI.DecodeObject(new IntPtr(17L), this.RawData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } long fileTime = Marshal.ReadInt64(decodedValue.DangerousGetHandle()); decodedValue.Dispose(); this.m_signingTime = DateTime.FromFileTimeUtc(fileTime); this.m_decoded = true; }
private static void DecodePublicKeyObject(uint aiPubKey, byte[] encodedKeyValue, byte[] encodedParameters, out byte[] decodedData) { decodedData = null; IntPtr zero = IntPtr.Zero; switch (aiPubKey) { case 0xaa01: case 0xaa02: throw new NotSupportedException(SR.GetString("NotSupported_KeyAlgorithm")); case 0xa400: case 0x2400: zero = new IntPtr(0x13L); break; case 0x2200: zero = new IntPtr(0x26L); break; default: throw new NotSupportedException(SR.GetString("NotSupported_KeyAlgorithm")); } SafeLocalAllocHandle decodedValue = null; uint cbDecodedValue = 0; if (!CAPI.DecodeObject(zero, encodedKeyValue, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (((int)zero) == 0x13) { decodedData = new byte[cbDecodedValue]; Marshal.Copy(decodedValue.DangerousGetHandle(), decodedData, 0, decodedData.Length); } else if (((int)zero) == 0x26) { SafeLocalAllocHandle handle2 = null; uint num2 = 0; if (!CAPI.DecodeObject(new IntPtr(0x27L), encodedParameters, out handle2, out num2)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } decodedData = ConstructDSSPubKeyCspBlob(decodedValue, handle2); handle2.Dispose(); } decodedValue.Dispose(); }
internal static byte[] DecodeOctetBytes(byte[] encodedOctetString) { uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = (SafeLocalAllocHandle)null; if (!CAPI.DecodeObject(new IntPtr(25L), encodedOctetString, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if ((int)cbDecodedValue == 0) { return(new byte[0]); } using (decodedValue) return(CAPI.BlobToByteArray(decodedValue.DangerousGetHandle())); }
private void DecodeExtension() { uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; SafeLocalAllocHandle handle2 = System.Security.Cryptography.X509Certificates.X509Utils.StringToAnsiPtr("2.5.29.14"); if (!CAPI.DecodeObject(handle2.DangerousGetHandle(), base.m_rawData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPIBase.CRYPTOAPI_BLOB blob = (CAPIBase.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CRYPTOAPI_BLOB)); byte[] sArray = CAPI.BlobToByteArray(blob); this.m_subjectKeyIdentifier = System.Security.Cryptography.X509Certificates.X509Utils.EncodeHexString(sArray); this.m_decoded = true; decodedValue.Dispose(); handle2.Dispose(); }
internal SignerInfo(SignedCms signedCms, SignerInfo parentSignerInfo, byte[] encodedSignerInfo) { uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = SafeLocalAllocHandle.InvalidHandle; fixed(byte *numPtr = &encodedSignerInfo[0]) { if (!CAPI.DecodeObject(new IntPtr(500L), new IntPtr((void *)numPtr), (uint)encodedSignerInfo.Length, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } } this.m_signedCms = signedCms; this.m_parentSignerInfo = parentSignerInfo; this.m_encodedSignerInfo = (byte[])encodedSignerInfo.Clone(); this.m_pbCmsgSignerInfo = decodedValue; this.m_cmsgSignerInfo = (CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO)); }
private void DecodeExtension() { uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; if (!CAPI.DecodeObject(new IntPtr(0x24L), base.m_rawData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPIBase.CERT_ENHKEY_USAGE cert_enhkey_usage = (CAPIBase.CERT_ENHKEY_USAGE)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_ENHKEY_USAGE)); this.m_enhancedKeyUsages = new OidCollection(); for (int i = 0; i < cert_enhkey_usage.cUsageIdentifier; i++) { Oid oid = new Oid(Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(new IntPtr(((long)cert_enhkey_usage.rgpszUsageIdentifier) + (i * Marshal.SizeOf(typeof(IntPtr)))))), System.Security.Cryptography.OidGroup.ExtensionOrAttribute, false); this.m_enhancedKeyUsages.Add(oid); } this.m_decoded = true; decodedValue.Dispose(); }
private void Decode() { uint cbDecoded = 0; SafeLocalAllocHandle pbDecoded = null; if (!CAPI.DecodeObject(new IntPtr(CAPI.PKCS_UTC_TIME), RawData, out pbDecoded, out cbDecoded)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } long signingTime = Marshal.ReadInt64(pbDecoded.DangerousGetHandle()); pbDecoded.Dispose(); m_signingTime = DateTime.FromFileTimeUtc(signingTime); m_decoded = true; }
internal static string DecodeOctetString(byte[] encodedOctetString) { uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = (SafeLocalAllocHandle)null; if (!CAPI.DecodeObject(new IntPtr(25L), encodedOctetString, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if ((int)cbDecodedValue == 0) { return(string.Empty); } CAPI.CRYPTOAPI_BLOB cryptoapiBlob = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); if ((int)cryptoapiBlob.cbData == 0) { return(string.Empty); } string str = Marshal.PtrToStringUni(cryptoapiBlob.pbData); decodedValue.Dispose(); return(str); }
private void DecodeExtension() { uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; if (!CAPI.DecodeObject(new IntPtr(14L), base.m_rawData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPIBase.CRYPTOAPI_BLOB cryptoapi_blob = (CAPIBase.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CRYPTOAPI_BLOB)); if (cryptoapi_blob.cbData > 4) { cryptoapi_blob.cbData = 4; } byte[] destination = new byte[4]; if (cryptoapi_blob.pbData != IntPtr.Zero) { Marshal.Copy(cryptoapi_blob.pbData, destination, 0, (int)cryptoapi_blob.cbData); } this.m_keyUsages = BitConverter.ToUInt32(destination, 0); this.m_decoded = true; decodedValue.Dispose(); }
private void DecodeExtension() { uint cbDecoded = 0; SafeLocalAllocHandle decoded = null; SafeLocalAllocHandle pb = X509Utils.StringToAnsiPtr(CAPI.szOID_SUBJECT_KEY_IDENTIFIER); bool result = CAPI.DecodeObject(pb.DangerousGetHandle(), m_rawData, out decoded, out cbDecoded); if (!result) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CRYPTOAPI_BLOB pSubjectKeyIdentifier = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decoded.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); byte[] hexArray = CAPI.BlobToByteArray(pSubjectKeyIdentifier); m_subjectKeyIdentifier = X509Utils.EncodeHexString(hexArray); m_decoded = true; decoded.Dispose(); pb.Dispose(); }
public unsafe string GetNameInfo(X509NameType nameType, bool forIssuer) { uint dwFlags = forIssuer ? 1 : 0; uint dwDisplayType = System.Security.Cryptography.X509Certificates.X509Utils.MapNameType(nameType); switch (dwDisplayType) { case 1: return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, dwDisplayType)); case 4: return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, dwDisplayType)); } string str = string.Empty; CAPIBase.CERT_CONTEXT cert_context = *((CAPIBase.CERT_CONTEXT *) this.m_safeCertContext.DangerousGetHandle()); CAPIBase.CERT_INFO cert_info = (CAPIBase.CERT_INFO)Marshal.PtrToStructure(cert_context.pCertInfo, typeof(CAPIBase.CERT_INFO)); IntPtr[] ptrArray = new IntPtr[] { CAPISafe.CertFindExtension(forIssuer ? "2.5.29.8" : "2.5.29.7", cert_info.cExtension, cert_info.rgExtension), CAPISafe.CertFindExtension(forIssuer ? "2.5.29.18" : "2.5.29.17", cert_info.cExtension, cert_info.rgExtension) }; for (int i = 0; i < ptrArray.Length; i++) { if (ptrArray[i] != IntPtr.Zero) { CAPIBase.CERT_EXTENSION cert_extension = (CAPIBase.CERT_EXTENSION)Marshal.PtrToStructure(ptrArray[i], typeof(CAPIBase.CERT_EXTENSION)); byte[] destination = new byte[cert_extension.Value.cbData]; Marshal.Copy(cert_extension.Value.pbData, destination, 0, destination.Length); uint cbDecodedValue = 0; SafeLocalAllocHandle decodedValue = null; SafeLocalAllocHandle handle2 = System.Security.Cryptography.X509Certificates.X509Utils.StringToAnsiPtr(cert_extension.pszObjId); bool flag = CAPI.DecodeObject(handle2.DangerousGetHandle(), destination, out decodedValue, out cbDecodedValue); handle2.Dispose(); if (flag) { CAPIBase.CERT_ALT_NAME_INFO cert_alt_name_info = (CAPIBase.CERT_ALT_NAME_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPIBase.CERT_ALT_NAME_INFO)); for (int j = 0; j < cert_alt_name_info.cAltEntry; j++) { IntPtr ptr = new IntPtr(((long)cert_alt_name_info.rgAltEntry) + (j * Marshal.SizeOf(typeof(CAPIBase.CERT_ALT_NAME_ENTRY)))); CAPIBase.CERT_ALT_NAME_ENTRY cert_alt_name_entry = (CAPIBase.CERT_ALT_NAME_ENTRY)Marshal.PtrToStructure(ptr, typeof(CAPIBase.CERT_ALT_NAME_ENTRY)); switch (dwDisplayType) { case 6: if (cert_alt_name_entry.dwAltNameChoice == 3) { str = Marshal.PtrToStringUni(cert_alt_name_entry.Value.pwszDNSName); } break; case 7: if (cert_alt_name_entry.dwAltNameChoice == 7) { str = Marshal.PtrToStringUni(cert_alt_name_entry.Value.pwszURL); } break; case 8: if (cert_alt_name_entry.dwAltNameChoice == 1) { CAPIBase.CERT_OTHER_NAME cert_other_name = (CAPIBase.CERT_OTHER_NAME)Marshal.PtrToStructure(cert_alt_name_entry.Value.pOtherName, typeof(CAPIBase.CERT_OTHER_NAME)); if (cert_other_name.pszObjId == "1.3.6.1.4.1.311.20.2.3") { uint num6 = 0; SafeLocalAllocHandle handle3 = null; if (CAPI.DecodeObject(new IntPtr(0x18L), System.Security.Cryptography.X509Certificates.X509Utils.PtrToByte(cert_other_name.Value.pbData, cert_other_name.Value.cbData), out handle3, out num6)) { CAPIBase.CERT_NAME_VALUE cert_name_value = (CAPIBase.CERT_NAME_VALUE)Marshal.PtrToStructure(handle3.DangerousGetHandle(), typeof(CAPIBase.CERT_NAME_VALUE)); if (System.Security.Cryptography.X509Certificates.X509Utils.IsCertRdnCharString(cert_name_value.dwValueType)) { str = Marshal.PtrToStringUni(cert_name_value.Value.pbData); } handle3.Dispose(); } } } break; } } decodedValue.Dispose(); } } } if ((nameType != X509NameType.DnsName) || ((str != null) && (str.Length != 0))) { return(str); } return(CAPI.GetCertNameInfo(this.m_safeCertContext, dwFlags, 3)); }
internal AlgorithmIdentifier(CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier) { int keyLength = 0; uint cbParameters = 0; SafeLocalAllocHandle pbParameters = SafeLocalAllocHandle.InvalidHandle; byte[] parameters = new byte[0]; uint algId = X509Utils.OidToAlgId(algorithmIdentifier.pszObjId); if (algId == CAPI.CALG_RC2) { if (algorithmIdentifier.Parameters.cbData > 0) { if (!CAPI.DecodeObject(new IntPtr(CAPI.PKCS_RC2_CBC_PARAMETERS), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out pbParameters, out cbParameters)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CRYPT_RC2_CBC_PARAMETERS rc2Parameters = (CAPI.CRYPT_RC2_CBC_PARAMETERS)Marshal.PtrToStructure(pbParameters.DangerousGetHandle(), typeof(CAPI.CRYPT_RC2_CBC_PARAMETERS)); switch (rc2Parameters.dwVersion) { case CAPI.CRYPT_RC2_40BIT_VERSION: keyLength = 40; break; case CAPI.CRYPT_RC2_56BIT_VERSION: keyLength = 56; break; case CAPI.CRYPT_RC2_128BIT_VERSION: keyLength = 128; break; } // Retrieve IV if available. if (rc2Parameters.fIV) { parameters = (byte[])rc2Parameters.rgbIV.Clone(); } } } else if (algId == CAPI.CALG_RC4 || algId == CAPI.CALG_DES || algId == CAPI.CALG_3DES) { // Retrieve the IV if available. For non RC2, the parameter contains the IV // (for RC4 the IV is really the salt). There are (128 - KeyLength) / 8 // bytes of RC4 salt. if (algorithmIdentifier.Parameters.cbData > 0) { if (!CAPI.DecodeObject(new IntPtr(CAPI.X509_OCTET_STRING), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out pbParameters, out cbParameters)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbParameters > 0) { if (algId == CAPI.CALG_RC4) { CAPI.CRYPTOAPI_BLOB saltBlob = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(pbParameters.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); if (saltBlob.cbData > 0) { parameters = new byte[saltBlob.cbData]; Marshal.Copy(saltBlob.pbData, parameters, 0, parameters.Length); } } else { parameters = new byte[cbParameters]; Marshal.Copy(pbParameters.DangerousGetHandle(), parameters, 0, parameters.Length); } } } // Determine key length. if (algId == CAPI.CALG_RC4) { // For RC4, keyLength = 128 - (salt length * 8). keyLength = 128 - ((int)parameters.Length * 8); } else if (algId == CAPI.CALG_DES) { // DES key length is fixed at 64 (or 56 without the parity bits). keyLength = 64; } else { // 3DES key length is fixed at 192 (or 168 without the parity bits). keyLength = 192; } } else { // Everything else, don't decode it as CAPI may not expose or know how. if (algorithmIdentifier.Parameters.cbData > 0) { parameters = new byte[algorithmIdentifier.Parameters.cbData]; Marshal.Copy(algorithmIdentifier.Parameters.pbData, parameters, 0, parameters.Length); } } Reset(Oid.FromOidValue(algorithmIdentifier.pszObjId, OidGroup.All), keyLength, parameters); pbParameters.Dispose(); }
internal AlgorithmIdentifier(CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier) { int keyLength = 0; uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = SafeLocalAllocHandle.InvalidHandle; byte[] numArray = new byte[0]; uint num = X509Utils.OidToAlgId(algorithmIdentifier.pszObjId); switch (num) { case 26114U: if (algorithmIdentifier.Parameters.cbData > 0U) { if (!CAPI.DecodeObject(new IntPtr(41L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CRYPT_RC2_CBC_PARAMETERS rc2CbcParameters = (CAPI.CRYPT_RC2_CBC_PARAMETERS)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPT_RC2_CBC_PARAMETERS)); switch (rc2CbcParameters.dwVersion) { case 52U: keyLength = 56; break; case 58U: keyLength = 128; break; case 160U: keyLength = 40; break; } if (rc2CbcParameters.fIV) { numArray = (byte[])rc2CbcParameters.rgbIV.Clone(); break; } else { break; } } else { break; } case 26625U: case 26113U: case 26115U: if (algorithmIdentifier.Parameters.cbData > 0U) { if (!CAPI.DecodeObject(new IntPtr(25L), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbDecodedValue > 0U) { if ((int)num == 26625) { CAPI.CRYPTOAPI_BLOB cryptoapiBlob = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); if (cryptoapiBlob.cbData > 0U) { numArray = new byte[(IntPtr)cryptoapiBlob.cbData]; Marshal.Copy(cryptoapiBlob.pbData, numArray, 0, numArray.Length); } } else { numArray = new byte[(IntPtr)cbDecodedValue]; Marshal.Copy(decodedValue.DangerousGetHandle(), numArray, 0, numArray.Length); } } } keyLength = (int)num != 26625 ? ((int)num != 26113 ? 192 : 64) : 128 - numArray.Length * 8; break; default: if (algorithmIdentifier.Parameters.cbData > 0U) { numArray = new byte[(IntPtr)algorithmIdentifier.Parameters.cbData]; Marshal.Copy(algorithmIdentifier.Parameters.pbData, numArray, 0, numArray.Length); break; } else { break; } } this.Reset(new Oid(algorithmIdentifier.pszObjId), keyLength, numArray); decodedValue.Dispose(); }
internal unsafe SubjectIdentifier(CAPI.CRYPTOAPI_BLOB issuer, CAPI.CRYPTOAPI_BLOB serialNumber) { // If serial number is 0, then it is the special SKI encoding or NoSignature bool isSKIorHashOnly = true; byte *pb = (byte *)serialNumber.pbData; for (uint i = 0; i < serialNumber.cbData; i++) { if (*pb++ != (byte)0) { isSKIorHashOnly = false; break; } } if (isSKIorHashOnly) { byte[] issuerBytes = new byte[issuer.cbData]; Marshal.Copy(issuer.pbData, issuerBytes, 0, issuerBytes.Length); X500DistinguishedName dummyName = new X500DistinguishedName(issuerBytes); if (String.Compare(CAPI.DummySignerCommonName, dummyName.Name, StringComparison.OrdinalIgnoreCase) == 0) { Reset(SubjectIdentifierType.NoSignature, null); return; } } if (isSKIorHashOnly) { // Decode disguised SKI in issuer field (See WinCrypt.h for more info). Note that some certificates may contain // an all-zero serial number but not be encoded with an szOID_KEYID_RDN. In order to allow use of signatures created // using these certificates, we will first try to find the szOID_KEYID_RDN, but if it does not exist, fall back to just // decoding the incoming issuer and serial number. m_type = SubjectIdentifierType.SubjectKeyIdentifier; m_value = String.Empty; uint cbCertNameInfo = 0; SafeLocalAllocHandle pbCertNameInfo = SafeLocalAllocHandle.InvalidHandle; if (CAPI.DecodeObject(new IntPtr(CAPI.X509_NAME), issuer.pbData, issuer.cbData, out pbCertNameInfo, out cbCertNameInfo)) { using (pbCertNameInfo) { checked { CAPI.CERT_NAME_INFO certNameInfo = (CAPI.CERT_NAME_INFO)Marshal.PtrToStructure(pbCertNameInfo.DangerousGetHandle(), typeof(CAPI.CERT_NAME_INFO)); for (uint i = 0; i < certNameInfo.cRDN; i++) { CAPI.CERT_RDN certRdn = (CAPI.CERT_RDN)Marshal.PtrToStructure(new IntPtr((long)certNameInfo.rgRDN + (long)(i * Marshal.SizeOf(typeof(CAPI.CERT_RDN)))), typeof(CAPI.CERT_RDN)); for (uint j = 0; j < certRdn.cRDNAttr; j++) { CAPI.CERT_RDN_ATTR certRdnAttr = (CAPI.CERT_RDN_ATTR)Marshal.PtrToStructure(new IntPtr((long)certRdn.rgRDNAttr + (long)(j * Marshal.SizeOf(typeof(CAPI.CERT_RDN_ATTR)))), typeof(CAPI.CERT_RDN_ATTR)); if (String.Compare(CAPI.szOID_KEYID_RDN, certRdnAttr.pszObjId, StringComparison.OrdinalIgnoreCase) == 0) { if (certRdnAttr.dwValueType == CAPI.CERT_RDN_OCTET_STRING) { byte[] ski = new byte[certRdnAttr.Value.cbData]; Marshal.Copy(certRdnAttr.Value.pbData, ski, 0, ski.Length); Reset(SubjectIdentifierType.SubjectKeyIdentifier, X509Utils.EncodeHexString(ski)); return; } } } } } } } } CAPI.CERT_ISSUER_SERIAL_NUMBER IssuerAndSerial; IssuerAndSerial.Issuer = issuer; IssuerAndSerial.SerialNumber = serialNumber; X509IssuerSerial issuerSerial = PkcsUtils.DecodeIssuerSerial(IssuerAndSerial); Reset(SubjectIdentifierType.IssuerAndSerialNumber, issuerSerial); }
internal AlgorithmIdentifier(CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier) { int keyLength = 0; uint cbParameters = 0; SafeLocalAllocHandle pbParameters = SafeLocalAllocHandle.InvalidHandle; byte[] parameters = new byte[0]; uint algId = X509Utils.OidToAlgId(algorithmIdentifier.pszObjId); if (algId == CAPI.CALG_RC2) { if (algorithmIdentifier.Parameters.cbData > 0) { if (!CAPI.DecodeObject(new IntPtr(CAPI.PKCS_RC2_CBC_PARAMETERS), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out pbParameters, out cbParameters)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } CAPI.CRYPT_RC2_CBC_PARAMETERS rc2Parameters = (CAPI.CRYPT_RC2_CBC_PARAMETERS)Marshal.PtrToStructure(pbParameters.DangerousGetHandle(), typeof(CAPI.CRYPT_RC2_CBC_PARAMETERS)); switch (rc2Parameters.dwVersion) { case CAPI.CRYPT_RC2_40BIT_VERSION: keyLength = 40; break; case CAPI.CRYPT_RC2_56BIT_VERSION: keyLength = 56; break; case CAPI.CRYPT_RC2_128BIT_VERSION: keyLength = 128; break; } // Retrieve IV if available. if (rc2Parameters.fIV) { parameters = (byte[])rc2Parameters.rgbIV.Clone(); } } } else if (algId == CAPI.CALG_RC4 || algId == CAPI.CALG_DES || algId == CAPI.CALG_3DES) { // Retrieve the IV if available. For non RC2, the parameter contains the IV // (for RC4 the IV is really the salt). There are (128 - KeyLength) / 8 // bytes of RC4 salt. if (algorithmIdentifier.Parameters.cbData > 0) { if (!CAPI.DecodeObject(new IntPtr(CAPI.X509_OCTET_STRING), algorithmIdentifier.Parameters.pbData, algorithmIdentifier.Parameters.cbData, out pbParameters, out cbParameters)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } if (cbParameters > Marshal.SizeOf(typeof(CAPI.CRYPTOAPI_BLOB))) { CAPI.CRYPTOAPI_BLOB blob = (CAPI.CRYPTOAPI_BLOB)Marshal.PtrToStructure(pbParameters.DangerousGetHandle(), typeof(CAPI.CRYPTOAPI_BLOB)); if (algId == CAPI.CALG_RC4) { if (blob.cbData > 0) { parameters = new byte[blob.cbData]; Marshal.Copy(blob.pbData, parameters, 0, parameters.Length); } } else { // This should be the same as the RC4 code, but for compatibility // * Allocate an array as big as the CRYPTOAPI_BLOB // * Copy in the cbData value // * Copy in the (pbData) value // // But don't copy in the pbData pointer value (or, rather, clear it out), // among other things it makes decoding the same contents into two // different EnvelopedCms objects say the parameters were different. parameters = new byte[cbParameters]; Marshal.Copy(pbParameters.DangerousGetHandle(), parameters, 0, parameters.Length); Array.Clear(parameters, sizeof(uint), (int)(parameters.Length - blob.cbData - sizeof(uint))); } } } // Determine key length. if (algId == CAPI.CALG_RC4) { // For RC4, keyLength = 128 - (salt length * 8). keyLength = 128 - ((int)parameters.Length * 8); } else if (algId == CAPI.CALG_DES) { // DES key length is fixed at 64 (or 56 without the parity bits). keyLength = 64; } else { // 3DES key length is fixed at 192 (or 168 without the parity bits). keyLength = 192; } } else { // Everything else, don't decode it as CAPI may not expose or know how. if (algorithmIdentifier.Parameters.cbData > 0) { parameters = new byte[algorithmIdentifier.Parameters.cbData]; Marshal.Copy(algorithmIdentifier.Parameters.pbData, parameters, 0, parameters.Length); } } Reset(Oid.FromOidValue(algorithmIdentifier.pszObjId, OidGroup.All), keyLength, parameters); pbParameters.Dispose(); }
internal SubjectIdentifier(CAPI.CRYPTOAPI_BLOB issuer, CAPI.CRYPTOAPI_BLOB serialNumber) { bool flag = true; byte *numPtr = (byte *)(void *)serialNumber.pbData; for (uint index = 0U; index < serialNumber.cbData; ++index) { if ((int)*numPtr++ != 0) { flag = false; break; } } if (flag) { byte[] numArray = new byte[(IntPtr)issuer.cbData]; Marshal.Copy(issuer.pbData, numArray, 0, numArray.Length); if (string.Compare("CN=Dummy Signer", new X500DistinguishedName(numArray).Name, StringComparison.OrdinalIgnoreCase) == 0) { this.Reset(SubjectIdentifierType.NoSignature, (object)null); return; } } if (flag) { this.m_type = SubjectIdentifierType.SubjectKeyIdentifier; this.m_value = (object)string.Empty; uint cbDecodedValue = 0U; SafeLocalAllocHandle decodedValue = SafeLocalAllocHandle.InvalidHandle; if (!CAPI.DecodeObject(new IntPtr(7L), issuer.pbData, issuer.cbData, out decodedValue, out cbDecodedValue)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } using (decodedValue) { CAPI.CERT_NAME_INFO certNameInfo = (CAPI.CERT_NAME_INFO)Marshal.PtrToStructure(decodedValue.DangerousGetHandle(), typeof(CAPI.CERT_NAME_INFO)); for (uint index1 = 0U; index1 < certNameInfo.cRDN; ++index1) { CAPI.CERT_RDN certRdn = (CAPI.CERT_RDN)Marshal.PtrToStructure(new IntPtr((long)certNameInfo.rgRDN + (long)index1 * (long)Marshal.SizeOf(typeof(CAPI.CERT_RDN))), typeof(CAPI.CERT_RDN)); for (uint index2 = 0U; index2 < certRdn.cRDNAttr; ++index2) { CAPI.CERT_RDN_ATTR certRdnAttr = (CAPI.CERT_RDN_ATTR)Marshal.PtrToStructure(new IntPtr((long)certRdn.rgRDNAttr + (long)index2 * (long)Marshal.SizeOf(typeof(CAPI.CERT_RDN_ATTR))), typeof(CAPI.CERT_RDN_ATTR)); if (string.Compare("1.3.6.1.4.1.311.10.7.1", certRdnAttr.pszObjId, StringComparison.OrdinalIgnoreCase) == 0 && (int)certRdnAttr.dwValueType == 2) { byte[] numArray = new byte[(IntPtr)certRdnAttr.Value.cbData]; Marshal.Copy(certRdnAttr.Value.pbData, numArray, 0, numArray.Length); this.Reset(SubjectIdentifierType.SubjectKeyIdentifier, (object)X509Utils.EncodeHexString(numArray)); return; } } } } throw new CryptographicException(-2146889715); } else { CAPI.CERT_ISSUER_SERIAL_NUMBER pIssuerAndSerial; pIssuerAndSerial.Issuer = issuer; pIssuerAndSerial.SerialNumber = serialNumber; this.Reset(SubjectIdentifierType.IssuerAndSerialNumber, (object)PkcsUtils.DecodeIssuerSerial(pIssuerAndSerial)); } }