Exemple #1
0
        /// <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;
        }
Exemple #2
0
        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;
        }
Exemple #4
0
            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;
            }
Exemple #5
0
 private static void CheckInvalidKey(SafeDsaHandle key)
 {
     if (key == null || key.IsInvalid)
     {
         throw new CryptographicException(SR.Cryptography_OpenInvalidHandle);
     }
 }
Exemple #6
0
 internal static extern bool DsaVerify(SafeDsaHandle dsa, byte[] hash, int hashLength, byte[] signature, int signatureLength);
Exemple #7
0
 internal static extern bool DsaSign(SafeDsaHandle dsa, byte[] hash, int hashLength, byte[] refSignature, out int outSignatureLength);
Exemple #8
0
 private static extern int DsaSizeP(SafeDsaHandle dsa);
Exemple #9
0
 /// <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;
 }
Exemple #10
0
 /// <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;
 }
Exemple #11
0
 private static extern int DsaSizeSignature(SafeDsaHandle dsa);
Exemple #12
0
 internal static extern bool DsaGenerateKey(out SafeDsaHandle dsa, int bits);
Exemple #13
0
 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);
Exemple #14
0
 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);