public ECDSASignature(byte[] derSig) { try { var decoder = new Asn1InputStream(derSig); var seq = decoder.ReadObject() as DerSequence; if ((seq == null) || (seq.Count != 2)) throw new FormatException(InvalidDERSignature); R = ((DerInteger) seq[0]).Value; S = ((DerInteger) seq[1]).Value; } catch (Exception ex) { throw new FormatException(InvalidDERSignature, ex); } }
/// <summary>Create a base ASN.1 object from a byte array.</summary> /// <param name="data">The byte array to parse.</param> /// <returns>The base ASN.1 object represented by the byte array.</returns> /// <exception cref="IOException">If there is a problem parsing the data.</exception> public static Asn1Object FromByteArray( byte[] data) { try { MemoryStream input = new MemoryStream(data, false); Asn1InputStream asn1 = new Asn1InputStream(input, data.Length); Asn1Object result = asn1.ReadObject(); if(input.Position != input.Length) throw new IOException("extra data found after object"); return result; } catch(InvalidCastException) { throw new IOException("cannot recognise object in byte array"); } }
/// <summary>Create a base ASN.1 object from a byte array.</summary> /// <param name="data">The byte array to parse.</param> /// <returns>The base ASN.1 object represented by the byte array.</returns> /// <exception cref="IOException">If there is a problem parsing the data.</exception> public static Asn1Object FromByteArray( byte[] data) { try { MemoryStream input = new MemoryStream(data, false); Asn1InputStream asn1 = new Asn1InputStream(input, data.Length); Asn1Object result = asn1.ReadObject(); if (input.Position != input.Length) { throw new IOException("extra data found after object"); } return(result); } catch (InvalidCastException) { throw new IOException("cannot recognise object in byte array"); } }
public static string DumpDer(byte[] der) { StringBuilder builder = new StringBuilder(); Asn1InputStream decoder = new Asn1InputStream(der); DerSequence seq = (DerSequence)decoder.ReadObject(); builder.AppendLine("Version : " + Encoders.Hex.EncodeData(seq[0].GetDerEncoded())); builder.AppendLine("Private : " + Encoders.Hex.EncodeData(seq[1].GetDerEncoded())); builder.AppendLine("Params : " + Encoders.Hex.EncodeData(((DerTaggedObject)seq[2]).GetObject().GetDerEncoded())); builder.AppendLine("Public : " + Encoders.Hex.EncodeData(seq[3].GetDerEncoded())); #if !PORTABLE decoder.Close(); #else decoder.Dispose(); #endif return builder.ToString(); }
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 !PORTABLE decoder.Close(); #else decoder.Dispose(); #endif return new ECKey(bits, true); }
public virtual IAsn1Convertible ReadObject() { int tag = _in.ReadByte(); if (tag == -1) { return(null); } // turn of looking for "00" while we resolve the tag Set00Check(false); // // calculate tag number // int tagNo = Asn1InputStream.ReadTagNumber(_in, tag); bool isConstructed = (tag & Asn1Tags.Constructed) != 0; // // calculate length // int length = Asn1InputStream.ReadLength(_in, _limit); if (length < 0) // indefinite length method { if (!isConstructed) { throw new IOException("indefinite length primitive encoding encountered"); } IndefiniteLengthInputStream indIn = new IndefiniteLengthInputStream(_in, _limit); Asn1StreamParser sp = new Asn1StreamParser(indIn, _limit); if ((tag & Asn1Tags.Application) != 0) { return(new BerApplicationSpecificParser(tagNo, sp)); } if ((tag & Asn1Tags.Tagged) != 0) { return(new BerTaggedObjectParser(true, tagNo, sp)); } return(sp.ReadIndef(tagNo)); } else { DefiniteLengthInputStream defIn = new DefiniteLengthInputStream(_in, length); if ((tag & Asn1Tags.Application) != 0) { return(new DerApplicationSpecific(isConstructed, tagNo, defIn.ToArray())); } if ((tag & Asn1Tags.Tagged) != 0) { return(new BerTaggedObjectParser(isConstructed, tagNo, new Asn1StreamParser(defIn))); } if (isConstructed) { // TODO There are other tags that may be constructed (e.g. BitString) switch (tagNo) { case Asn1Tags.OctetString: // // yes, people actually do this... // return(new BerOctetStringParser(new Asn1StreamParser(defIn))); case Asn1Tags.Sequence: return(new DerSequenceParser(new Asn1StreamParser(defIn))); case Asn1Tags.Set: return(new DerSetParser(new Asn1StreamParser(defIn))); case Asn1Tags.External: return(new DerExternalParser(new Asn1StreamParser(defIn))); default: throw new IOException("unknown tag " + tagNo + " encountered"); } } // Some primitive encodings can be handled by parsers too... switch (tagNo) { case Asn1Tags.OctetString: return(new DerOctetStringParser(defIn)); } try { return(Asn1InputStream.CreatePrimitiveDerObject(tagNo, defIn, tmpBuffers)); } catch (ArgumentException e) { throw new Asn1Exception("corrupted stream detected", e); } } }
public Asn1StreamParser( Stream inStream) : this(inStream, Asn1InputStream.FindLimit(inStream)) { }