static SecKey ImportPrivateKey(X509Certificate2 certificate) { if (!certificate.HasPrivateKey) { throw new NotSupportedException(); } CFArray items; using (var data = CFData.FromData(ExportKey((RSA)certificate.PrivateKey))) items = ItemImport(data, SecExternalFormat.OpenSSL, SecExternalItemType.PrivateKey); try { if (items.Count != 1) { throw new InvalidOperationException("Private key import failed."); } var imported = items[0]; if (CFType.GetTypeID(imported) != SecKey.GetTypeID()) { throw new InvalidOperationException("Private key import doesn't return SecKey."); } return(new SecKey(imported, items.Handle)); } finally { items.Dispose(); } }
static INativeObject [] QueryAsReference(CFDictionary query, out SecStatusCode result) { if (query == null) { result = SecStatusCode.Param; return(null); } IntPtr ptr; result = SecItem.SecItemCopyMatching(query.Handle, out ptr); if (result == SecStatusCode.Success && ptr != IntPtr.Zero) { var array = CFArray.ArrayFromHandle <INativeObject> (ptr, p => { IntPtr cfType = CFType.GetTypeID(p); if (cfType == SecCertificate.GetTypeID()) { return(new SecCertificate(p, true)); } if (cfType == SecKey.GetTypeID()) { return(new SecKey(p, true)); } if (cfType == SecIdentity.GetTypeID()) { return(new SecIdentity(p, true)); } throw new Exception(String.Format("Unexpected type: 0x{0:x}", cfType)); }); return(array); } return(null); }
public static bool IsSecKey(IntPtr ptr) { if (ptr == IntPtr.Zero) { return(false); } return(CFType.GetTypeID(ptr) == SecKeyGetTypeID()); }
public CFHTTPMessage GetResponseHeader() { var handle = GetProperty(_ResponseHeader); if (handle == IntPtr.Zero) { return(null); } if (CFType.GetTypeID(handle) != CFHTTPMessage.TypeID) { CFType.Release(handle); throw new InvalidCastException(); } return(new CFHTTPMessage(handle)); }
public CVImageBuffer GetImageBuffer() { IntPtr ib = CMSampleBufferGetImageBuffer(handle); if (ib == IntPtr.Zero) { return(null); } var ibt = CFType.GetTypeID(ib); if (ibt == CVPixelBuffer.CVImageBufferType) { return(new CVPixelBuffer(ib, false)); } return(new CVImageBuffer(ib, false)); }
public CFHTTPMessage GetFinalRequest() { var handle = GetProperty(_FinalRequest); if (handle == IntPtr.Zero) { return(null); } if (CFType.GetTypeID(handle) != CFHTTPMessage.GetTypeID()) { CFObject.CFRelease(handle); throw new InvalidCastException(); } return(new CFHTTPMessage(handle)); }
static public SecIdentity ItemImport(X509Certificate2 certificate) { if (!certificate.HasPrivateKey) { throw new NotSupportedException(); } using (var key = ImportPrivateKey(certificate)) using (var cert = new SecCertificate(certificate)) { var identity = SecIdentityCreate(IntPtr.Zero, cert.Handle, key.Handle); if (CFType.GetTypeID(identity) != SecIdentity.GetTypeID()) { throw new InvalidOperationException(); } return(new SecIdentity(identity, true)); } }
public static INativeObject[] QueryAsReference(SecRecord query, int max, out SecStatusCode result) { if (query == null) { result = SecStatusCode.Param; return(null); } using (var copy = query.queryDict.MutableCopy()) { copy.SetValue(CFBoolean.True.Handle, SecItem.ReturnRef); SetLimit(copy, max); IntPtr ptr; result = SecItem.SecItemCopyMatching(copy.Handle, out ptr); if ((result == SecStatusCode.Success) && (ptr != IntPtr.Zero)) { var array = CFArray.ArrayFromHandle <INativeObject> (ptr, p => { IntPtr cfType = CFType.GetTypeID(p); if (cfType == SecCertificate.GetTypeID()) { return(new SecCertificate(p, true)); } else if (cfType == SecKey.GetTypeID()) { return(new SecKey(p, true)); } else if (cfType == SecIdentity.GetTypeID()) { return(new SecIdentity(p, true)); } else { throw new Exception(String.Format("Unexpected type: 0x{0:x}", cfType)); } }); return(array); } return(null); } }