Example #1
0
        private static void GenerateKey(Func <DSA, int> getSize)
        {
            int keySize;

            using (DSA dsa = DSAFactory.Create())
            {
                keySize = getSize(dsa);
            }

            using (DSA dsa = DSAFactory.Create(keySize))
            {
                Assert.Equal(keySize, dsa.KeySize);

                // Some providers may generate the key in the constructor, but
                // all of them should have generated it before answering ExportParameters.
                DSAParameters keyParameters = dsa.ExportParameters(false);
                DSAImportExport.ValidateParameters(ref keyParameters);

                // KeySize should still be what we set it to originally.
                Assert.Equal(keySize, dsa.KeySize);

                dsa.ImportParameters(keyParameters);
                Assert.Equal(keySize, dsa.KeySize);
            }
        }
Example #2
0
        public static void FromXmlWrongJ_OK()
        {
            // No one really reads the J value on import, but xmldsig defined it,
            // so we read it.

            // This key comes from FIPS-186-2, Appendix 5, Example of the DSA.
            // The version in DSATestData does not have the seed or counter supplied.

            using (DSA dsa = DSAFactory.Create())
            {
                dsa.FromXmlString(@"
<DSAKeyValue>
  <P>
    jfKklEkidqo9JXWbsGhpy+rA2Dr7jQz3y7gyTw14guXQdi/FtyEOr8Lprawyq3qs
    SWk9+/g3JMLsBzbuMcgCkQ==
  </P>
  <Q>x3MhjHN+yO6ZO08t7TD0jtrOkV8=</Q>
  <G>
    Ym0CeDnqChNBMWOlW0y1ACmdVSKVbO/LO/8Q85nOLC5xy53l+iS6v1jlt5Uhklyc
    xC6fb0ZLCIzFcq9T5teIAg==
  </G>
  <Y>
    GRMYcddbFhKoGfKdeNGw1zRveqd7tiqFm/1sVnXanSEtOjbvFnLvZguMfCVcwOx0
    hY+6M/RMBmmWMKdrAw7jMw==
  </Y>
  <J>AgRO2deYCHK5/u5+ElWfz2J6fdI2PN/mnjBxceE11r5zt7x/DVqcoWAp2+dR</J>
  <Seed>1QFOS2DvK6i2IRtAYroyJOBCfdM=</Seed>
  <PgenCounter>aQ==</PgenCounter>
  <X>IHCzIj26Ny/eHA/8ey47SYsmBhQ=</X>
</DSAKeyValue>");

                DSATestData.GetDSA1024_186_2(out DSAParameters expected, out _, out _);
                DSAImportExport.AssertKeyEquals(expected, dsa.ExportParameters(true));
            }
        }
Example #3
0
        public static void FromToXml()
        {
            using (DSA dsa = DSAFactory.Create())
            {
                DSAParameters pubOnly = dsa.ExportParameters(false);
                DSAParameters pubPriv = dsa.ExportParameters(true);

                string xmlPub  = dsa.ToXmlString(false);
                string xmlPriv = dsa.ToXmlString(true);

                using (DSA dsaPub = DSAFactory.Create())
                {
                    dsaPub.FromXmlString(xmlPub);

                    DSAImportExport.AssertKeyEquals(pubOnly, dsaPub.ExportParameters(false));
                }

                using (DSA dsaPriv = DSAFactory.Create())
                {
                    dsaPriv.FromXmlString(xmlPriv);

                    DSAImportExport.AssertKeyEquals(pubPriv, dsaPriv.ExportParameters(true));
                    DSAImportExport.AssertKeyEquals(pubOnly, dsaPriv.ExportParameters(false));
                }
            }
        }
Example #4
0
        public static void FromXmlWrongCounter_SometimesOK()
        {
            // DSACryptoServiceProvider doesn't check this error state, DSACng does.
            //
            // So, either the import gets rejected (because the counter value should be 105,
            // but says 106) and throws a CryptographicException derivitive, or it succeeds,
            // and exports the correct key material.

            // This key comes from FIPS-186-2, Appendix 5, Example of the DSA.
            // The version in DSATestData does not have the seed or counter supplied.

            using (DSA dsa = DSAFactory.Create())
            {
                bool checkKey = true;

                try
                {
                    dsa.FromXmlString(@"
<DSAKeyValue>
  <P>
    jfKklEkidqo9JXWbsGhpy+rA2Dr7jQz3y7gyTw14guXQdi/FtyEOr8Lprawyq3qs
    SWk9+/g3JMLsBzbuMcgCkQ==
  </P>
  <Q>x3MhjHN+yO6ZO08t7TD0jtrOkV8=</Q>
  <G>
    Ym0CeDnqChNBMWOlW0y1ACmdVSKVbO/LO/8Q85nOLC5xy53l+iS6v1jlt5Uhklyc
    xC6fb0ZLCIzFcq9T5teIAg==
  </G>
  <Y>
    GRMYcddbFhKoGfKdeNGw1zRveqd7tiqFm/1sVnXanSEtOjbvFnLvZguMfCVcwOx0
    hY+6M/RMBmmWMKdrAw7jMw==
  </Y>
  <J>AgRO2deYCHK5/u5+ElWfz2J6fdI2PN/mnjBxceE11r5zt7x/DVqcoWAp2+dr</J>
  <Seed>1QFOS2DvK6i2IRtAYroyJOBCfdM=</Seed>
  <PgenCounter>ag==</PgenCounter>
  <X>IHCzIj26Ny/eHA/8ey47SYsmBhQ=</X>
</DSAKeyValue>");
                }
                catch (CryptographicException)
                {
                    checkKey = false;
                }

                if (checkKey)
                {
                    DSATestData.GetDSA1024_186_2(out DSAParameters expected, out _, out _);
                    DSAImportExport.AssertKeyEquals(expected, dsa.ExportParameters(true));
                }
            }
        }
        public static void ImportFromPem_Pkcs8UnEncrypted_Simple()
        {
            using (DSA dsa = DSAFactory.Create())
            {
                string pem = @"
-----BEGIN PRIVATE KEY-----
MIHGAgEAMIGoBgcqhkjOOAQBMIGcAkEA1qi38cr3ppZNB2Y/xpHSL2q81Vw3rvWN
IHRnQNgv4U4UY2NifZGSUULc3uOEvgoeBO1b9fRxSG9NmG1CoufflQIVAPq19iXV
1eFkMKHvYw6+M4l8wiT5AkAIRMSQ5S71jgWQLGNtZNHV6yxggqDU87/RzgeOh7Q6
fve77OGaTv4qbZwinTYAg86p9yHzmwW6+XBS3vxnpYorBBYCFC49eoTIW2Z4Xh9v
55aYKyKwy5i8
-----END PRIVATE KEY-----";
                dsa.ImportFromPem(pem);
                DSAParameters dsaParameters = dsa.ExportParameters(true);

                DSAImportExport.AssertKeyEquals(DSATestData.Dsa512Parameters, dsaParameters);
            }
        }
        public static void ImportFromPem_SubjectPublicKeyInfo_Simple()
        {
            using (DSA dsa = DSAFactory.Create())
            {
                string pem = @"
-----BEGIN PUBLIC KEY-----
MIHxMIGoBgcqhkjOOAQBMIGcAkEA1qi38cr3ppZNB2Y/xpHSL2q81Vw3rvWNIHRn
QNgv4U4UY2NifZGSUULc3uOEvgoeBO1b9fRxSG9NmG1CoufflQIVAPq19iXV1eFk
MKHvYw6+M4l8wiT5AkAIRMSQ5S71jgWQLGNtZNHV6yxggqDU87/RzgeOh7Q6fve7
7OGaTv4qbZwinTYAg86p9yHzmwW6+XBS3vxnpYorA0QAAkEAwwDg5n2HfmztOf7q
qsHywr1WjmoyRnIn4Stq5FqNlHhUGkgKyAA4qshjgn1uOYQGGiWQXBi9JJmoOWY8
PKRWBQ==
-----END PUBLIC KEY-----";
                dsa.ImportFromPem(pem);
                DSAParameters dsaParameters = dsa.ExportParameters(false);

                DSAImportExport.AssertKeyEquals(DSATestData.Dsa512Parameters.ToPublic(), dsaParameters);
            }
        }
        public static void ImportFromEncryptedPem_Pkcs8_Encrypted_Char_Simple()
        {
            using (DSA dsa = DSAFactory.Create())
            {
                string pem = @"
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIBIDBLBgkqhkiG9w0BBQ0wPjApBgkqhkiG9w0BBQwwHAQIkM/kCKe6rYsCAggA
MAwGCCqGSIb3DQIJBQAwEQYFKw4DAgcECBOccveL65bDBIHQiCcCqwxJs93g1+16
7Gx1D5lL4/nZ94fRa+Hl4nGEX4gmjuxH6pOHKyywwflAyXNTfVhOCP9zBedwENx9
MGHbpaaShD6iJfoGMRX0frr0mMCtuOOZkkjBF9pSpkhaH0TDSq1PrVLxcM0/S4Vs
+//2uPrP8U+CTW9W7CXCZw698BAuevZRuD0koT2Bn9ErhTiuVZZMcOjtLmN2oXHG
dVYwfovccu8ktEAwk5XAOo0r+5CCw2lDDw/hbDeO87BToC5Cc5nu3F5LxAUj8Flc
v8pi3w==
-----END ENCRYPTED PRIVATE KEY-----";
                dsa.ImportFromEncryptedPem(pem, "test");
                DSAParameters dsaParameters = dsa.ExportParameters(true);

                DSAImportExport.AssertKeyEquals(DSATestData.Dsa512Parameters, dsaParameters);
            }
        }
        public static void ImportFromEncryptedPem_Pkcs8_Encrypted_Byte_Simple()
        {
            using (DSA dsa = DSAFactory.Create())
            {
                string pem           = @"
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIBLDBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIfcoipdEY/C4CAggA
MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAECBBC9heEphj00fB89aP6chSOjBIHQ
HF2RLrIw6654q2hjUdCG4PhhYNXlck0zD0mOuaVQHmnKIKArk/1DSpgSrYnKw6aE
2eujwNdySLLEwUj5l+X/IXwhOnPIZDJqUN7oMagUYJX28gnQmXyDvrt3r16utbpd
ho0YNYGUDSgOs6RxBpw1rJUCnAlHNU09peCjEP+aZSrhsxlejN/GpVS4e0JTmMeo
xTL6VO9mx52x6h5WDAQAisMVeMkBoxQUWLANXiw1zSfVbsmB7mDknsRcvD3tcgMs
7YLD7LQMiPAIjDlOP8XP/w==
-----END ENCRYPTED PRIVATE KEY-----";
                byte[] passwordBytes = Encoding.UTF8.GetBytes("test");
                dsa.ImportFromEncryptedPem(pem, passwordBytes);
                DSAParameters dsaParameters = dsa.ExportParameters(true);

                DSAImportExport.AssertKeyEquals(DSATestData.Dsa512Parameters, dsaParameters);
            }
        }
        public static void ImportFromPem_Pkcs8UnEncrypted_IgnoresUnrelatedAlgorithm()
        {
            using (DSA dsa = DSAFactory.Create())
            {
                string pem = @"
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIHChLC2xaEXtVv9oz8IaRys/BNfWhRv2NJ8tfVs0UrOKoAoGCCqGSM49
AwEHoUQDQgAEgQHs5HRkpurXDPaabivT2IaRoyYtIsuk92Ner/JmgKjYoSumHVmS
NfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ==
-----END EC PRIVATE KEY-----
-----BEGIN PRIVATE KEY-----
MIHGAgEAMIGoBgcqhkjOOAQBMIGcAkEA1qi38cr3ppZNB2Y/xpHSL2q81Vw3rvWN
IHRnQNgv4U4UY2NifZGSUULc3uOEvgoeBO1b9fRxSG9NmG1CoufflQIVAPq19iXV
1eFkMKHvYw6+M4l8wiT5AkAIRMSQ5S71jgWQLGNtZNHV6yxggqDU87/RzgeOh7Q6
fve77OGaTv4qbZwinTYAg86p9yHzmwW6+XBS3vxnpYorBBYCFC49eoTIW2Z4Xh9v
55aYKyKwy5i8
-----END PRIVATE KEY-----";
                dsa.ImportFromPem(pem);
                DSAParameters dsaParameters = dsa.ExportParameters(true);

                DSAImportExport.AssertKeyEquals(DSATestData.Dsa512Parameters, dsaParameters);
            }
        }
Example #10
0
        public static void FromXml_CounterOverflow_Succeeds()
        {
            // The counter value should be 105 (0x69).
            // This payload says 0x01_00000069 (4294967401).
            // Since we only end up looking at the last 4 bytes, this is /a/ right answer.

            // This key comes from FIPS-186-2, Appendix 5, Example of the DSA.
            // The version in DSATestData does not have the seed or counter supplied.

            using (DSA dsa = DSAFactory.Create())
            {
                dsa.FromXmlString(@"
<DSAKeyValue>
  <P>
    jfKklEkidqo9JXWbsGhpy+rA2Dr7jQz3y7gyTw14guXQdi/FtyEOr8Lprawyq3qs
    SWk9+/g3JMLsBzbuMcgCkQ==
  </P>
  <Q>x3MhjHN+yO6ZO08t7TD0jtrOkV8=</Q>
  <G>
    Ym0CeDnqChNBMWOlW0y1ACmdVSKVbO/LO/8Q85nOLC5xy53l+iS6v1jlt5Uhklyc
    xC6fb0ZLCIzFcq9T5teIAg==
  </G>
  <Y>
    GRMYcddbFhKoGfKdeNGw1zRveqd7tiqFm/1sVnXanSEtOjbvFnLvZguMfCVcwOx0
    hY+6M/RMBmmWMKdrAw7jMw==
  </Y>
  <J>AgRO2deYCHK5/u5+ElWfz2J6fdI2PN/mnjBxceE11r5zt7x/DVqcoWAp2+dr</J>
  <Seed>1QFOS2DvK6i2IRtAYroyJOBCfdM=</Seed>
  <PgenCounter>AQAAAGk=</PgenCounter>
  <X>IHCzIj26Ny/eHA/8ey47SYsmBhQ=</X>
</DSAKeyValue>");

                DSATestData.GetDSA1024_186_2(out DSAParameters expected, out _, out _);
                DSAImportExport.AssertKeyEquals(expected, dsa.ExportParameters(true));
            }
        }