Esempio n. 1
0
        /// <inheritdoc/>
        public ICryptographicKey ImportKeyPair(byte[] keyBlob, CryptographicPrivateKeyBlobType blobType = CryptographicPrivateKeyBlobType.Pkcs8RawPrivateKeyInfo)
        {
            Requires.NotNull(keyBlob, "keyBlob");

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

            string keyIdentifier = Guid.NewGuid().ToString();
            SecKey privateKey    = ImportKey(parameters, RsaCryptographicKey.GetPrivateKeyIdentifierWithTag(keyIdentifier));
            SecKey publicKey     = ImportKey(KeyFormatter.PublicKeyFilter(parameters), RsaCryptographicKey.GetPublicKeyIdentifierWithTag(keyIdentifier));

            return(new RsaCryptographicKey(publicKey, privateKey, keyIdentifier, this.Algorithm));
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public ICryptographicKey CreateKeyPair(int keySize)
        {
            Requires.Range(keySize > 0, "keySize");

            string keyIdentifier        = Guid.NewGuid().ToString();
            string publicKeyIdentifier  = RsaCryptographicKey.GetPublicKeyIdentifierWithTag(keyIdentifier);
            string privateKeyIdentifier = RsaCryptographicKey.GetPrivateKeyIdentifierWithTag(keyIdentifier);

            // Configure parameters for the joint keypair.
            using var keyPairAttr               = new NSMutableDictionary();
            keyPairAttr[KSec.AttrKeyType]       = KSec.AttrKeyTypeRSA;
            keyPairAttr[KSec.AttrKeySizeInBits] = NSNumber.FromInt32(keySize);

            // Configure parameters for the private key
            using var privateKeyAttr                = new NSMutableDictionary();
            privateKeyAttr[KSec.AttrIsPermanent]    = NSNumber.FromBoolean(true);
            privateKeyAttr[KSec.AttrApplicationTag] = NSData.FromString(privateKeyIdentifier, NSStringEncoding.UTF8);

            // Configure parameters for the public key
            using var publicKeyAttr                = new NSMutableDictionary();
            publicKeyAttr[KSec.AttrIsPermanent]    = NSNumber.FromBoolean(true);
            publicKeyAttr[KSec.AttrApplicationTag] = NSData.FromString(publicKeyIdentifier, NSStringEncoding.UTF8);

            // Parent the individual key parameters to the keypair one.
            keyPairAttr[KSec.PublicKeyAttrs]  = publicKeyAttr;
            keyPairAttr[KSec.PrivateKeyAttrs] = privateKeyAttr;

            // Generate the RSA key.
            SecKey?publicKey = null, privateKey = null;

            try
            {
                SecStatusCode code = SecKey.GenerateKeyPair(keyPairAttr, out publicKey, out privateKey);
                Verify.Operation(code == SecStatusCode.Success, "status was " + code);
            }
            catch (InvalidOperationException ex)
            {
                publicKey?.Dispose();
                privateKey?.Dispose();
                throw new ArgumentException(ex.Message, ex);
            }
            catch
            {
                publicKey?.Dispose();
                privateKey?.Dispose();
                throw;
            }

            return(new RsaCryptographicKey(publicKey, privateKey, keyIdentifier, this.algorithm));
        }