/// <summary> /// Return the size of the key in bytes. /// </summary> internal static int DsaKeySize(SafeDsaHandle dsa) { int keySize = DsaSizeP(dsa); // Assume an even multiple of 8 bytes \ 64 bits (OpenSsl also makes the same assumption) keySize = (keySize + 7) / 8 * 8; return keySize; }
internal static DSAParameters ExportDsaParameters(SafeDsaHandle key, bool includePrivateParameters) { Debug.Assert( key != null && !key.IsInvalid, "Callers should check the key is invalid and throw an exception with a message"); if (key == null || key.IsInvalid) { throw new CryptographicException(); } IntPtr p_bn, q_bn, g_bn, y_bn, x_bn; // these are not owned int p_cb, q_cb, g_cb, y_cb, x_cb; bool refAdded = false; try { key.DangerousAddRef(ref refAdded); // Protect access to the *_bn variables if (!GetDsaParameters(key, out p_bn, out p_cb, out q_bn, out q_cb, out g_bn, out g_cb, out y_bn, out y_cb, out x_bn, out x_cb)) { throw new CryptographicException(); } // Match Windows semantics where p, g and y have same length int pgy_cb = GetMax(p_cb, g_cb, y_cb); // Match Windows semantics where q and x have same length int qx_cb = GetMax(q_cb, x_cb); DSAParameters dsaParameters = new DSAParameters { P = Crypto.ExtractBignum(p_bn, pgy_cb), Q = Crypto.ExtractBignum(q_bn, qx_cb), G = Crypto.ExtractBignum(g_bn, pgy_cb), Y = Crypto.ExtractBignum(y_bn, pgy_cb), }; if (includePrivateParameters) { dsaParameters.X = Crypto.ExtractBignum(x_bn, qx_cb); } return dsaParameters; } finally { if (refAdded) key.DangerousRelease(); } }
internal static SafeDsaHandle DuplicateHandle(IntPtr handle) { Debug.Assert(handle != IntPtr.Zero); // Reliability: Allocate the SafeHandle before calling Dsa_up_ref so // that we don't lose a tracked reference in low-memory situations. SafeDsaHandle safeHandle = new SafeDsaHandle(); if (!Interop.Crypto.DsaUpRef(handle)) { throw Interop.Crypto.CreateOpenSslCryptographicException(); } safeHandle.SetHandle(handle); return safeHandle; }
private void SetKey(SafeDsaHandle newKey) { // Use ForceSet instead of the property setter to ensure that LegalKeySizes doesn't interfere // with the already loaded key. ForceSetKeySize(BitsPerByte * Interop.Crypto.DsaKeySize(newKey)); _key = new Lazy<SafeDsaHandle>(() => newKey, isThreadSafe:true); // Have Lazy<T> consider the key to be loaded var dummy = _key.Value; }
private static void CheckInvalidKey(SafeDsaHandle key) { if (key == null || key.IsInvalid) { throw new CryptographicException(SR.Cryptography_OpenInvalidHandle); } }
internal static extern bool DsaVerify(SafeDsaHandle dsa, byte[] hash, int hashLength, byte[] signature, int signatureLength);
internal static extern bool DsaSign(SafeDsaHandle dsa, byte[] hash, int hashLength, byte[] refSignature, out int outSignatureLength);
private static extern int DsaSizeP(SafeDsaHandle dsa);
/// <summary> /// Return the maximum size of the DER-encoded key in bytes. /// </summary> internal static int DsaEncodedSignatureSize(SafeDsaHandle dsa) { int size = DsaSizeSignature(dsa); return size; }
/// <summary> /// Return the size of the 'r' or 's' signature fields in bytes. /// </summary> internal static int DsaSignatureFieldSize(SafeDsaHandle dsa) { int size = DsaSizeQ(dsa); Debug.Assert(size * 2 < DsaEncodedSignatureSize(dsa)); return size; }
private static extern int DsaSizeSignature(SafeDsaHandle dsa);
internal static extern bool DsaGenerateKey(out SafeDsaHandle dsa, int bits);
internal static extern bool DsaKeyCreateByExplicitParameters( out SafeDsaHandle dsa, byte[] p, int pLength, byte[] q, int qLength, byte[] g, int gLength, byte[] y, int yLength, byte[] x, int xLength);
private static extern bool GetDsaParameters( SafeDsaHandle key, out IntPtr p, out int p_cb, out IntPtr q, out int q_cb, out IntPtr g, out int g_cb, out IntPtr y, out int y_cb, out IntPtr x, out int x_cb);
internal static extern bool EvpPkeySetDsa(SafeEvpPKeyHandle pkey, SafeDsaHandle key);