Zheng L
Zheng L

Reputation: 11

Failed to add Certificate/Key into Xamarin iOS keychain

I am trying to use SecKeyChain to add my certs and private keys into iOS keychain. I tried SecKeyChain.Add() and SecKeyChain.AddIdentity(), but first one return me SecStatusCode "Param", and another one throws "System.InvalidOperationException: Param". Can someone help me to solve this problem? It's hard to find detailed documentation for those methods from Xamarin Website.

using(NSData crt = NSData.FromFile("client1.p12"))
{
    X509Certificate2 certificate = new X509Certificate2(crt.ToArray(), password);
    var identity = SecIdentity.Import(certificate.Export(X509ContentType.Pkcs12, password), password);

    var record = new SecRecord(SecKind.Certificate);
    record.Label = "client1_crt";
    record.SetValueRef(identity.Certificate);
    SecStatusCode secStatus = SecKeyChain.Add(record);

    SecKeyChain.AddIdentity(identity)
}

I also tried this way:

using (NSData crt = NSData.FromFile("client1-crt.der")
{
    SecStatusCode secStatus = SecKeyChain.Add(new SecRecord(SecKind.Certificate)
    {
        ApplicationLabel = "client1_crt",
        KeySizeInBits = 512,
        KeyClass = SecKeyClass.Public,
        ValueData = NSData.FromString(crt)                                                                   
    }); 
}

But secStatus still shows "Param".

Upvotes: 0

Views: 371

Answers (1)

Zheng L
Zheng L

Reputation: 11

Problem solved. Turns out we need to enable keychain in Entitlements.plist file. detail Detail steps:
https://forums.xamarin.com/discussion/comment/330146#Comment_330146

Upvotes: 1

Related Questions