Skip to content

Problem with GOST2012 key - can't read it. #237

@keymsr

Description

@keymsr

We have private key (GOST3410-2012, alg.id 1.2.643.7.1.1.1.1). But we can't read it using BouncyCastle 1.8.6.1

Here is a key data:

-----BEGIN PRIVATE KEY----- MIGiAgEAMCEGCCqFAwcBAQECMBUGCSqFAwcBAgECAQYIKoUDBwEBAgMEQIXnWrZ6 ajvbCU6x9jK49PgQqCP00T/lW3laXCXueMF8X4Q1y3N9zfOJT2s/IgyPJVrUhgtO 1Akp+Roh8bCPPlqgODA2BggqhQMCCQMIATEqBCi72ZvrBVW6mFL/bQeXeMTf8Jh8 p/diI7Cg8ig4mXg3tsIUf4vBi61b -----END PRIVATE KEY-----

And here is a code to read it:

    const string keyPath = "D:\\testkey\\priv.key";
    using (var textReader = File.OpenText(keyPath))
    {
        var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(textReader);
        var pemObj = pemReader.ReadPemObject();
        var seq = (Asn1Sequence)Asn1Object.FromByteArray(pemObj.Content);
        var keyInfo = PrivateKeyInfo.GetInstance(seq);
        var akp = Org.BouncyCastle.Security.PrivateKeyFactory.CreateKey(keyInfo);
    }

this code provide exception: "DER length more than 4 bytes: 103". Does anyone knows how to read it?

This watHave tried this way:

        using (var textReader = File.OpenText(filename))
        {
            var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(textReader);
            var pemObj = pemReader.ReadPemObject();
            var seq = (Org.BouncyCastle.Asn1.Asn1Sequence)Org.BouncyCastle.Asn1.Asn1Object.FromByteArray(pemObj.Content);
            var keyInfo = PrivateKeyInfo.GetInstance(seq);
            Org.BouncyCastle.Asn1.X509.AlgorithmIdentifier algID = keyInfo.PrivateKeyAlgorithm;
            Org.BouncyCastle.Asn1.DerObjectIdentifier algOid = algID.ObjectID;
            if (algOid.Equals(Org.BouncyCastle.Asn1.Rosstandart.RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256))
            {
                var gostParams = new Gost3410PublicKeyAlgParameters(
                Org.BouncyCastle.Asn1.Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object()));
                var i = (Org.BouncyCastle.Asn1.DerInteger)keyInfo.ParsePrivateKey();
            }
            pemReader.ReadObject();
        }

And now have exception: corrupted stream - "out of bounds length found: 47 >= 32"

Old GOST keys (GOST3410-2001) works ok.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions