public static ECDSASignature FromDER(byte[] sig)
 {
     try
     {
         Asn1InputStream decoder = new Asn1InputStream(sig);
         var seq = decoder.ReadObject() as DerSequence;
         if(seq == null || seq.Count != 2)
             throw new FormatException(InvalidDERSignature);
         return new ECDSASignature(((DerInteger)seq[0]).Value, ((DerInteger)seq[1]).Value);
     }
     catch(IOException ex)
     {
         throw new FormatException(InvalidDERSignature, ex);
     }
 }
Example #2
0
 public static string DumpDer(byte[] der)
 {
     StringBuilder builder = new StringBuilder();
     Asn1InputStream decoder = new Asn1InputStream(der);
     DerSequence seq = (DerSequence)decoder.ReadObject();
     builder.AppendLine("Version : " + HexEncoding.Instance.GetString(seq[0].GetDerEncoded()));
     builder.AppendLine("Private : " + HexEncoding.Instance.GetString(seq[1].GetDerEncoded()));
     builder.AppendLine("Params : " + HexEncoding.Instance.GetString(((DerTaggedObject)seq[2]).GetObject().GetDerEncoded()));
     builder.AppendLine("Public : " + HexEncoding.Instance.GetString(seq[3].GetDerEncoded()));
     #if !DNXCORE50
     decoder.Close();
     #else
     decoder.Dispose();
     #endif
     return builder.ToString();
 }
Example #3
0
        public static ECKey FromDER(byte[] der)
        {
            // To understand this code, see the definition of the ASN.1 format for EC private keys in the OpenSSL source
            // code in ec_asn1.c:
            //
            // ASN1_SEQUENCE(EC_PRIVATEKEY) = {
            //   ASN1_SIMPLE(EC_PRIVATEKEY, version, LONG),
            //   ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING),
            //   ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0),
            //   ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1)
            // } ASN1_SEQUENCE_END(EC_PRIVATEKEY)
            //

            Asn1InputStream decoder = new Asn1InputStream(der);
            DerSequence seq = (DerSequence)decoder.ReadObject();
            CheckArgument(seq.Count == 4, "Input does not appear to be an ASN.1 OpenSSL EC private key");
            CheckArgument(((DerInteger)seq[0]).Value.Equals(BigInteger.One),
                    "Input is of wrong version");
            byte[] bits = ((DerOctetString)seq[1]).GetOctets();
            #if !DNXCORE50
            decoder.Close();
            #else
            decoder.Dispose();
            #endif
            return new ECKey(bits, true);
        }