Ejemplo n.º 1
0
        /// <inheritdoc/>
        public ICryptographicKey ImportPublicKey(byte[] keyBlob, CryptographicPublicKeyBlobType blobType = CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo)
        {
            Requires.NotNull(keyBlob, "keyBlob");

            RSAParameters parameters = KeyFormatter.GetFormatter(blobType).Read(keyBlob);

            // Inject the PKCS#1 public key into the KeyChain.
            string keyIdentifier       = Guid.NewGuid().ToString();
            string publicKeyIdentifier = RsaCryptographicKey.GetPublicKeyIdentifierWithTag(keyIdentifier);
            var    keyQueryDictionary  = RsaCryptographicKey.CreateKeyQueryDictionary(publicKeyIdentifier);

            keyQueryDictionary[KSec.ValueData]    = NSData.FromArray(KeyFormatter.Pkcs1.Write(parameters, includePrivateKey: false));
            keyQueryDictionary[KSec.AttrKeyClass] = KSec.AttrKeyClassPublic;
            keyQueryDictionary[KSec.ReturnRef]    = NSNumber.FromBoolean(true);
            IntPtr resultHandle;
            int    status = RsaCryptographicKey.SecItemAdd(keyQueryDictionary.Handle, out resultHandle);

            if (resultHandle != IntPtr.Zero)
            {
                var key = new SecKey(resultHandle, true);
                return(new RsaCryptographicKey(key, keyIdentifier, this.Algorithm));
            }
            else
            {
                throw new InvalidOperationException("SecItemAdd return " + status);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Imports an RSA key into the iOS keychain.
 /// </summary>
 /// <param name="parameters">The RSA parameters.</param>
 /// <param name="tag">The tag by which this key will be known.</param>
 /// <returns>The security key.</returns>
 private static SecKey ImportKey(RSAParameters parameters, string tag)
 {
     using (var keyQueryDictionary = RsaCryptographicKey.CreateKeyQueryDictionary(tag))
     {
         byte[] pkcs1Key = KeyFormatter.Pkcs1.Write(parameters, parameters.D != null);
         keyQueryDictionary[KSec.ValueData]    = NSData.FromArray(pkcs1Key);
         keyQueryDictionary[KSec.AttrKeyClass] = parameters.D != null ? KSec.AttrKeyClassPrivate : KSec.AttrKeyClassPublic;
         keyQueryDictionary[KSec.ReturnRef]    = NSNumber.FromBoolean(true);
         IntPtr handle;
         int    status = RsaCryptographicKey.SecItemAdd(keyQueryDictionary.Handle, out handle);
         Verify.Operation(status == 0, "SecItemAdd returned {0}", status);
         return(new SecKey(handle, true));
     }
 }