private static bool VerifyRSAKeyPairSign(
        RSA rsaPublicKey,
        RSA rsaPrivateKey)
    {
        Opc.Ua.Test.RandomSource randomSource = new Opc.Ua.Test.RandomSource();
        int blockSize = RsaUtils.GetPlainTextBlockSize(rsaPrivateKey, true);

        byte[] testBlock = new byte[blockSize];
        randomSource.NextBytes(testBlock, 0, blockSize);
        byte[] signature = rsaPrivateKey.SignData(testBlock, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
        return(rsaPublicKey.VerifyData(testBlock, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1));
    }
    private static bool VerifyRSAKeyPairCrypt(
        RSA rsaPublicKey,
        RSA rsaPrivateKey)
    {
        Opc.Ua.Test.RandomSource randomSource = new Opc.Ua.Test.RandomSource();
        int blockSize = RsaUtils.GetPlainTextBlockSize(rsaPrivateKey, true);

        byte[] testBlock = new byte[blockSize];
        randomSource.NextBytes(testBlock, 0, blockSize);
        byte[] encryptedBlock = rsaPublicKey.Encrypt(testBlock, RSAEncryptionPadding.OaepSHA1);
        byte[] decryptedBlock = rsaPrivateKey.Decrypt(encryptedBlock, RSAEncryptionPadding.OaepSHA1);
        if (decryptedBlock != null)
        {
            return(Utils.IsEqual(testBlock, decryptedBlock));
        }
        return(false);
    }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes the data generator.
        /// </summary>
        public DataGenerator(IRandomSource random)
        {
            m_maxArrayLength       = 100;
            m_maxStringLength      = 100;
            m_maxXmlAttributeCount = 10;
            m_maxXmlElementCount   = 10;
            m_minDateTimeValue     = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            m_maxDateTimeValue     = new DateTime(2100, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            m_random = random;
            m_boundaryValueFrequency = 20;
            m_namespaceUris          = new NamespaceTable();
            m_serverUris             = new StringTable();

            // create a random source if none provided.
            if (m_random == null)
            {
                m_random = new RandomSource();
            }

            // load the boundary values.
            m_boundaryValues = new SortedDictionary <string, object[]>();

            for (int ii = 0; ii < s_AvailableBoundaryValues.Length; ii++)
            {
                m_boundaryValues[s_AvailableBoundaryValues[ii].SystemType.Name] = s_AvailableBoundaryValues[ii].Values.ToArray();
            }

            // load the localized tokens.
            m_tokenValues = LoadStringData("Opc.Ua.Types.Utils.LocalizedData.txt");
            if (m_tokenValues.Count == 0)
            {
                m_tokenValues = LoadStringData("Opc.Ua.Utils.LocalizedData.txt");
            }

            // index the available locales.
            m_availableLocales = new string[m_tokenValues.Count];

            int index = 0;

            foreach (string locale in m_tokenValues.Keys)
            {
                m_availableLocales[index++] = locale;
            }
        }
    /// <summary>
    /// Verify RSA key pair of two certificates.
    /// </summary>
    private static bool VerifyRSAKeyPair(
        X509Certificate2 certWithPublicKey,
        X509Certificate2 certWithPrivateKey,
        bool throwOnError = false)
    {
        bool result = false;

        try
        {
            // verify the public and private key match
            using (RSA rsaPrivateKey = certWithPrivateKey.GetRSAPrivateKey())
            {
                using (RSA rsaPublicKey = certWithPublicKey.GetRSAPublicKey())
                {
                    Opc.Ua.Test.RandomSource randomSource = new Opc.Ua.Test.RandomSource();
                    int    blockSize = RsaUtils.GetPlainTextBlockSize(rsaPrivateKey, true);
                    byte[] testBlock = new byte[blockSize];
                    randomSource.NextBytes(testBlock, 0, blockSize);
                    byte[] encryptedBlock = rsaPublicKey.Encrypt(testBlock, RSAEncryptionPadding.OaepSHA1);
                    byte[] decryptedBlock = rsaPrivateKey.Decrypt(encryptedBlock, RSAEncryptionPadding.OaepSHA1);
                    if (decryptedBlock != null)
                    {
                        result = Utils.IsEqual(testBlock, decryptedBlock);
                    }
                }
            }
        }
        catch (Exception e)
        {
            if (throwOnError)
            {
                throw e;
            }
        }
        finally
        {
            if (!result && throwOnError)
            {
                throw new CryptographicException("The public/private key pair in the certficates do not match.");
            }
        }
        return(result);
    }