internal ECParameters ExportParameters(bool includePrivateParameters, int keySizeInBits)
        {
            SecKeyPair keys = GetOrGenerateKeys(keySizeInBits);

            if (includePrivateParameters && keys.PrivateKey == null)
            {
                throw new CryptographicException(SR.Cryptography_OpenInvalidHandle);
            }

            byte[] keyBlob = Interop.AppleCrypto.SecKeyCopyExternalRepresentation(
                includePrivateParameters ? keys.PrivateKey ! : keys.PublicKey);

            try
            {
                AsymmetricAlgorithmHelpers.DecodeFromUncompressedAnsiX963Key(
                    keyBlob,
                    includePrivateParameters,
                    out ECParameters key);

                switch (GetKeySize(keys))
                {
                case 256: key.Curve = ECCurve.NamedCurves.nistP256; break;

                case 384: key.Curve = ECCurve.NamedCurves.nistP384; break;

                case 521: key.Curve = ECCurve.NamedCurves.nistP521; break;
                }

                return(key);
            }
            finally
            {
                CryptographicOperations.ZeroMemory(keyBlob);
            }
        }
Ejemplo n.º 2
0
        private static bool TryExportDataKeyParameters(
            SecKeyPair keys,
            bool includePrivateParameters,
            ref ECParameters ecParameters)
        {
            if (includePrivateParameters && keys.PrivateKey == null)
            {
                throw new CryptographicException(SR.Cryptography_OpenInvalidHandle);
            }

            bool gotKeyBlob = Interop.AppleCrypto.TrySecKeyCopyExternalRepresentation(
                includePrivateParameters ? keys.PrivateKey ! : keys.PublicKey,
                out byte[] keyBlob);

            if (!gotKeyBlob)
            {
                return(false);
            }

            try
            {
                AsymmetricAlgorithmHelpers.DecodeFromUncompressedAnsiX963Key(
                    keyBlob,
                    includePrivateParameters,
                    out ecParameters);

                switch (GetKeySize(keys))
                {
                case 256: ecParameters.Curve = ECCurve.NamedCurves.nistP256; break;

                case 384: ecParameters.Curve = ECCurve.NamedCurves.nistP384; break;

                case 521: ecParameters.Curve = ECCurve.NamedCurves.nistP521; break;

                default:
                    Debug.Fail("Unsupported curve");
                    throw new CryptographicException();
                }

                return(true);
            }
            finally
            {
                CryptographicOperations.ZeroMemory(keyBlob);
            }
        }