internal static int EcKeyGetSize(SafeEcKeyHandle key) { int keySize; int rc = CryptoNative_EcKeyGetSize(key, out keySize); if (rc == 1) { return keySize; } throw Interop.Crypto.CreateOpenSslCryptographicException(); }
internal static bool EcKeyHasCurveName(SafeEcKeyHandle key) { int nidCurveName; int rc = CryptoNative_EcKeyGetCurveName(key, out nidCurveName); if (rc == 1) { // Key is invalid or doesn't have a curve return (nidCurveName != Interop.Crypto.NID_undef); } throw Interop.Crypto.CreateOpenSslCryptographicException(); }
internal static SafeEcKeyHandle DuplicateHandle(IntPtr handle) { Debug.Assert(handle != IntPtr.Zero); // Reliability: Allocate the SafeHandle before calling EC_KEY_up_ref so // that we don't lose a tracked reference in low-memory situations. SafeEcKeyHandle safeHandle = new SafeEcKeyHandle(); if (!Interop.Crypto.EcKeyUpRef(handle)) { throw Interop.Crypto.CreateOpenSslCryptographicException(); } safeHandle.SetHandle(handle); return safeHandle; }
internal static int EcKeyGetCurveName(SafeEcKeyHandle ecKey) { bool mustRelease = false; try { ecKey.DangerousAddRef(ref mustRelease); IntPtr ecGroup = EC_KEY_get0_group(ecKey.DangerousGetHandle()); int nid = EC_GROUP_get_curve_name(ecGroup); return nid; } finally { if (mustRelease) { ecKey.DangerousRelease(); } } }
internal static string EcKeyGetCurveName(SafeEcKeyHandle key) { int nidCurveName; int rc = CryptoNative_EcKeyGetCurveName(key, out nidCurveName); if (rc == 1) { if (nidCurveName == Interop.Crypto.NID_undef) { Debug.Assert(false); // Key is invalid or doesn't have a curve return string.Empty; } IntPtr objCurveName = Interop.Crypto.ObjNid2Obj(nidCurveName); if (objCurveName != IntPtr.Zero) { return Interop.Crypto.GetOidValue(objCurveName); } } throw Interop.Crypto.CreateOpenSslCryptographicException(); }
internal static ECParameters GetECKeyParameters( SafeEcKeyHandle key, bool includePrivate) { SafeBignumHandle qx_bn, qy_bn, d_bn; IntPtr d_bn_not_owned; int qx_cb, qy_cb, d_cb; ECParameters parameters = new ECParameters(); bool refAdded = false; try { key.DangerousAddRef(ref refAdded); // Protect access to d_bn_not_owned if (!CryptoNative_GetECKeyParameters( key, includePrivate, out qx_bn, out qx_cb, out qy_bn, out qy_cb, out d_bn_not_owned, out d_cb)) { throw Interop.Crypto.CreateOpenSslCryptographicException(); } using (qx_bn) using (qy_bn) using (d_bn = new SafeBignumHandle(d_bn_not_owned, false)) { // Match Windows semantics where qx, qy, and d have same length int cbKey = GetMax(qx_cb, qy_cb, d_cb); parameters.Q = new ECPoint { X = Crypto.ExtractBignum(qx_bn, cbKey), Y = Crypto.ExtractBignum(qy_bn, cbKey) }; parameters.D = d_cb == 0 ? null : Crypto.ExtractBignum(d_bn, cbKey); } } finally { if (refAdded) key.DangerousRelease(); } return parameters; }
internal static extern int ECDSA_size(SafeEcKeyHandle ecKey); // returns the maximum length of a DER encoded ECDSA signature created with this key.
private static extern int CryptoNative_EcKeyGetCurveName(SafeEcKeyHandle ecKey, out int nid);
internal static extern int ECDSA_verify(int type, [In] byte[] dgst, int dgst_len, [In] byte[] sigbuf, int sig_len, SafeEcKeyHandle ecKey);
private static extern int CryptoNative_EcKeyGetSize(SafeEcKeyHandle ecKey, out int keySize);
private void SetKey(SafeEcKeyHandle newKey) { // Use ForceSet instead of the property setter to ensure that LegalKeySizes doesn't interfere // with the already loaded key. ForceSetKeySize(Interop.Crypto.EcKeyGetSize(newKey)); _key = new Lazy<SafeEcKeyHandle>(() => newKey); // Have Lazy<T> consider the key to be loaded var dummy = _key.Value; }
internal static extern bool EVP_PKEY_set1_EC_KEY(SafeEvpPKeyHandle pkey, SafeEcKeyHandle rsa);
internal static extern int EcKeyGetCurveName(SafeEcKeyHandle ecKey);
private static ECParameters ExportExplicitCurveParameters(SafeEcKeyHandle key, bool includePrivateParameters) { CheckInvalidKey(key); ECParameters parameters = Interop.Crypto.GetECCurveParameters(key, includePrivateParameters); bool hasPrivateKey = (parameters.D != null); if (hasPrivateKey != includePrivateParameters) { throw new CryptographicException(SR.Cryptography_CSP_NoPrivateKey); } return parameters; }
internal static extern bool EvpPkeySetEcKey(SafeEvpPKeyHandle pkey, SafeEcKeyHandle key);
internal static extern bool EcDsaSign([In] byte[] dgst, int dlen, [Out] byte[] sig, [In, Out] ref int siglen, SafeEcKeyHandle ecKey);
internal static extern bool EC_KEY_generate_key(SafeEcKeyHandle eckey);
internal static extern int EcDsaSize(SafeEcKeyHandle ecKey);
internal static extern int EcDsaVerify([In] byte[] dgst, int dgst_len, [In] byte[] sigbuf, int sig_len, SafeEcKeyHandle ecKey);
internal static extern bool EcKeyGenerateKey(SafeEcKeyHandle eckey);
internal static extern ECCurve.ECCurveType EcKeyGetCurveType(SafeEcKeyHandle key);
private static ECParameters ExportNamedCurveParameters(SafeEcKeyHandle key, bool includePrivateParameters) { CheckInvalidKey(key); ECParameters parameters = Interop.Crypto.GetECKeyParameters(key, includePrivateParameters); bool hasPrivateKey = (parameters.D != null); if (hasPrivateKey != includePrivateParameters) { throw new CryptographicException(SR.Cryptography_CSP_NoPrivateKey); } // Assign Curve string keyOidValueName = Interop.Crypto.EcKeyGetCurveName(key); parameters.Curve = ECCurve.CreateFromValue(keyOidValueName); return parameters; }
private static extern bool CryptoNative_GetECCurveParameters( SafeEcKeyHandle key, bool includePrivate, out ECCurve.ECCurveType curveType, out SafeBignumHandle qx, out int x_cb, out SafeBignumHandle qy, out int y_cb, out IntPtr d_bn_not_owned, out int d_cb, out SafeBignumHandle p, out int P_cb, out SafeBignumHandle a, out int A_cb, out SafeBignumHandle b, out int B_cb, out SafeBignumHandle gx, out int Gx_cb, out SafeBignumHandle gy, out int Gy_cb, out SafeBignumHandle order, out int order_cb, out SafeBignumHandle cofactor, out int cofactor_cb, out SafeBignumHandle seed, out int seed_cb);
private static void CheckInvalidKey(SafeEcKeyHandle key) { if (key == null || key.IsInvalid) { throw new CryptographicException(SR.Cryptography_OpenInvalidHandle); } }
internal static ECParameters GetECCurveParameters( SafeEcKeyHandle key, bool includePrivate) { ECCurve.ECCurveType curveType; SafeBignumHandle qx_bn, qy_bn, p_bn, a_bn, b_bn, gx_bn, gy_bn, order_bn, cofactor_bn, seed_bn; IntPtr d_bn_not_owned; int qx_cb, qy_cb, p_cb, a_cb, b_cb, gx_cb, gy_cb, order_cb, cofactor_cb, seed_cb, d_cb; bool refAdded = false; try { key.DangerousAddRef(ref refAdded); // Protect access to d_bn_not_owned if (!CryptoNative_GetECCurveParameters( key, includePrivate, out curveType, out qx_bn, out qx_cb, out qy_bn, out qy_cb, out d_bn_not_owned, out d_cb, out p_bn, out p_cb, out a_bn, out a_cb, out b_bn, out b_cb, out gx_bn, out gx_cb, out gy_bn, out gy_cb, out order_bn, out order_cb, out cofactor_bn, out cofactor_cb, out seed_bn, out seed_cb)) { throw Interop.Crypto.CreateOpenSslCryptographicException(); } using (qx_bn) using (qy_bn) using (p_bn) using (a_bn) using (b_bn) using (gx_bn) using (gy_bn) using (order_bn) using (cofactor_bn) using (seed_bn) using (var d_h = new SafeBignumHandle(d_bn_not_owned, false)) { int cbFieldLength; int pFieldLength; if (curveType == ECCurve.ECCurveType.Characteristic2) { // Match Windows semantics where a,b,gx,gy,qx,qy have same length // Treat length of m separately as it is not tied to other fields for Char2 (Char2 not supported by Windows) cbFieldLength = GetMax(new[] { a_cb, b_cb, gx_cb, gy_cb, qx_cb, qy_cb }); pFieldLength = p_cb; } else { // Match Windows semantics where p,a,b,gx,gy,qx,qy have same length cbFieldLength = GetMax(new[] { p_cb, a_cb, b_cb, gx_cb, gy_cb, qx_cb, qy_cb }); pFieldLength = cbFieldLength; } // Match Windows semantics where order and d have same length int cbSubgroupOrder = GetMax(order_cb, d_cb); // Copy values to ECParameters ECParameters parameters = new ECParameters(); parameters.Q = new ECPoint { X = Crypto.ExtractBignum(qx_bn, cbFieldLength), Y = Crypto.ExtractBignum(qy_bn, cbFieldLength) }; parameters.D = d_cb == 0 ? null : Crypto.ExtractBignum(d_h, cbSubgroupOrder); var curve = parameters.Curve; curve.CurveType = curveType; curve.A = Crypto.ExtractBignum(a_bn, cbFieldLength); curve.B = Crypto.ExtractBignum(b_bn, cbFieldLength); curve.G = new ECPoint { X = Crypto.ExtractBignum(gx_bn, cbFieldLength), Y = Crypto.ExtractBignum(gy_bn, cbFieldLength) }; curve.Order = Crypto.ExtractBignum(order_bn, cbSubgroupOrder); if (curveType == ECCurve.ECCurveType.Characteristic2) { curve.Polynomial = Crypto.ExtractBignum(p_bn, pFieldLength); } else { curve.Prime = Crypto.ExtractBignum(p_bn, pFieldLength); } // Optional parameters curve.Cofactor = cofactor_cb == 0 ? null : Crypto.ExtractBignum(cofactor_bn, cofactor_cb); curve.Seed = seed_cb == 0 ? null : Crypto.ExtractBignum(seed_bn, seed_cb); parameters.Curve = curve; return parameters; } } finally { if (refAdded) key.DangerousRelease(); } }
private static extern int EcKeyCreateByKeyParameters( out SafeEcKeyHandle key, string oid, byte[] qx, int qxLength, byte[] qy, int qyLength, byte[] d, int dLength);
private static extern bool CryptoNative_GetECKeyParameters( SafeEcKeyHandle key, bool includePrivate, out SafeBignumHandle qx_bn, out int x_cb, out SafeBignumHandle qy_bn, out int y_cb, out IntPtr d_bn_not_owned, out int d_cb);
private static int GetKeySize(SafeEcKeyHandle ecKeyHandle) { int nid = Interop.Crypto.EcKeyGetCurveName(ecKeyHandle); int keySize = 0; for (int i = 0; i < s_supportedAlgorithms.Length; i++) { if (s_supportedAlgorithms[i].Nid == nid) { keySize = s_supportedAlgorithms[i].KeySize; break; } } if (keySize == 0) { string curveNameOid = Interop.Crypto.GetOidValue(Interop.Crypto.ObjNid2Obj(nid)); throw new NotSupportedException(SR.Format(SR.Cryptography_UnsupportedEcKeyAlgorithm, curveNameOid)); } return keySize; }
internal static extern bool ECDSA_sign(int type, [In] byte[] dgst, int dlen, [Out] byte[] sig, [In, Out] ref int siglen, SafeEcKeyHandle ecKey);