Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
using System;
using System.Drawing;
using MonoMac.Foundation;
using MonoMac.AppKit;
using MonoMac.ObjCRuntime;
using System.IO;
using MonoMac.Security;
using System.Runtime.InteropServices;
using System.Text;
namespace PKCS12Example
public partial class AppDelegate : NSApplicationDelegate
MainWindowController mainWindowController;
public AppDelegate ()
[DllImport ("/System/Library/Frameworks/Security.framework/Security")]
unsafe static extern SecStatusCode SecKeychainOpen (char * pathName, out IntPtr keychain);
[DllImport ("/System/Library/Frameworks/Security.framework/Security")]
extern static SecStatusCode SecItemAdd (/* CFDictionaryRef */ IntPtr attributes, /* CFTypeRef* */ IntPtr result);
public override void DidFinishLaunching (NSNotification notification)
mainWindowController = new MainWindowController ();
mainWindowController.Window.MakeKeyAndOrderFront (this);
Stream certStream = new FileStream ("/Users/donblas/name-cert.p12", FileMode.Open);
NSData data = NSData.FromStream (certStream);
NSDictionary[] array;
SecStatusCode rc;
using (var options = NSDictionary.FromObjectAndKey (new NSString ("password"), SecImportExport.Passphrase)) {
rc = SecImportExport.ImportPkcs12 (data, options, out array);
Console.WriteLine (rc);
SecIdentity ident = new SecIdentity (array [0].LowlevelObjectForKey (SecImportExport.Identity.Handle));
var rec = new SecRecord (SecKind.Certificate) {
Label = "MyCert"
rec.SetValueRef (ident.Certificate);
IntPtr systemKeychainPtr;
string keychainPath = @"/Library/Keychains/System.keychain";
unsafe {
char[] keychainPathArray = keychainPath.ToCharArray ();
fixed (char * charPtr = keychainPathArray)
rc = SecKeychainOpen (charPtr, out systemKeychainPtr);
Console.WriteLine (rc);
rc = SecItemAdd (rec.ToDictionary ().Handle, systemKeychainPtr);
Console.WriteLine (rc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment