public static string GetSubjectSummary(SafeSecCertificateHandle certificate) { if (certificate == null || certificate.IsInvalid) { throw new ArgumentNullException(nameof(certificate)); } var subjectSummaryHandle = IntPtr.Zero; try { subjectSummaryHandle = SecCertificateCopySubjectSummary(certificate.DangerousGetHandle()); return(CFString.AsString(subjectSummaryHandle)); } finally { if (subjectSummaryHandle != IntPtr.Zero) { CFObject.CFRelease(subjectSummaryHandle); } } }
public static byte[] GetRawData(SafeSecCertificateHandle certificate) { if (certificate == null || certificate.IsInvalid) { throw new ArgumentNullException(nameof(certificate)); } var dataPtr = SecCertificateCopyData(certificate.DangerousGetHandle()); if (dataPtr == IntPtr.Zero) { throw new ArgumentException("Not a valid certificate"); } using (var data = new CFData(dataPtr, true)) { var buffer = new byte[(int)data.Length]; Marshal.Copy(data.Bytes, buffer, 0, buffer.Length); return(buffer); } }
public static bool Equals(SafeSecCertificateHandle first, SafeSecCertificateHandle second) { /* * This is a little bit expensive, but unfortunately there is no better API to compare two * SecCertificateRef's for equality. */ if (first == null || first.IsInvalid) { throw new ArgumentNullException(nameof(first)); } if (second == null || second.IsInvalid) { throw new ArgumentNullException(nameof(second)); } if (first.DangerousGetHandle() == second.DangerousGetHandle()) { return(true); } var firstDataPtr = SecCertificateCopyData(first.DangerousGetHandle()); var secondDataPtr = SecCertificateCopyData(first.DangerousGetHandle()); try { if (firstDataPtr == IntPtr.Zero || secondDataPtr == IntPtr.Zero) { throw new ArgumentException("Not a valid certificate."); } if (firstDataPtr == secondDataPtr) { return(true); } var firstLength = (int)CFData.CFDataGetLength(firstDataPtr); var secondLength = (int)CFData.CFDataGetLength(secondDataPtr); if (firstLength != secondLength) { return(false); } var firstBytePtr = CFData.CFDataGetBytePtr(firstDataPtr); var secondBytePtr = CFData.CFDataGetBytePtr(secondDataPtr); if (firstBytePtr == secondBytePtr) { return(true); } var firstBuffer = new byte[firstLength]; var secondBuffer = new byte[secondLength]; Marshal.Copy(firstBytePtr, firstBuffer, 0, firstBuffer.Length); Marshal.Copy(secondBytePtr, secondBuffer, 0, secondBuffer.Length); for (int i = 0; i < firstBuffer.Length; i++) { if (firstBuffer[i] != secondBuffer[i]) { return(false); } } return(true); } finally { if (firstDataPtr != null) { CFObject.CFRelease(firstDataPtr); } if (secondDataPtr != null) { CFObject.CFRelease(secondDataPtr); } } }