private void DecodeByTagNumber(int tagNum, BEREncoding ulazBER) { /// BAAAD!!! foreach (ASN1Type tip in this.Alternatives) { if (tagNum == tip.Tag) { this.chosen = tip.CreateInstance(ulazBER); this.Tag = tagNum; return; } } throw new Exception("Unrecognized alternative in CHOICE!"); }
static Samples() { /// utcTime utcTime = new ASN1UTCTime(); utcTime.Assign("0401291907+0100"); /// 29/01/2004, 19:07, CET utcTime.DecodeString(); /// Name (X.501) name = new Name(); try { using(FileStream fs = new FileStream(@"test.X501.Name.01.ber", FileMode.Open)) { BinaryReader br = new BinaryReader(fs); byte[] ber_bytes = br.ReadBytes((int)fs.Length); BEREncoding ber = new BEREncoding(ber_bytes); name.fromBER(ber); fs.Close(); } } catch (System.Exception e) { Console.WriteLine(e.ToString()); throw new Exception("Samples pallette failed at initializing .name!"); } /// SubjectPublicKeyInfo subjectPKInfo subjectPKInfo = new SubjectPublicKeyInfo(); // ContentEncryptionAlgorithmIdentifier alg = new ContentEncryptionAlgorithmIdentifier(); // alg.Assign(PKCS7.CMS_AlgorithmIdentifiers.rsaEncryption, null); // subjectPKInfo.Assign(alg, new ASN1BitString(new byte[]{0x1, 0x1, 0x1}, 19)); }
/// <summary> /// Read values from BER code. Inherited by /// ASN.1's string types. /// </summary> /// <param name="ulazBER">BER encoded input</param> public override void fromBER(BEREncoding ulazBER) { ulazBER.Decode(); foreach (BEREncoding objBER in ulazBER.Decompose() ) { Byte[] tmpByteArray = objBER.GetContents(); int len = tmpByteArray.Length; // -------------------------------------------------- Char[] tmpCharArray = new Char[len]; for(int i=0; i<len; i++) { tmpCharArray[i]=Convert.ToChar(tmpByteArray[i]); } this.StringValue += new String(tmpCharArray); // -------------------------------------------------- } this.CheckString(); }
/// <summary> /// Constructor used for initializing ASN.1's type class with /// BER code normally calls this method. /// This method explicitly initializes OctetString's data /// member from BER code. /// </summary> /// <param name="ulazBER">input BER code</param> public override void fromBER(BEREncoding ulazBER) { /// invoke ulazBER's Decode() method. //base.fromBER (ulazBER); this.OctetString = ulazBER.GetContents(); }
public override ASN1Object CreateInstance(BEREncoding ulazBER) { return new ASN1BitString(ulazBER); }
public override ASN1Object CreateInstance(BEREncoding ulazBER) { ASN1Boolean tmp = this.Instance() as ASN1Boolean; tmp.fromBER(ulazBER); return tmp; }
/// <summary> /// Constructor, initializes from BER code. Intended /// for decoding BER to corresponding ASN.1-type. /// </summary> /// <param name="ulazBER">BER encoded input</param> public ASN1PrintableString (BEREncoding ulazBER) { this.fromBER(ulazBER); }
public virtual ASN1SetOf SubCreateInstance(ASN1SetOf setOfObject, BEREncoding ulazBER) { /// ovo treba baciti u .fromBER od setOfObject-a !!!!! ulazBER.Decode(); foreach(BEREncoding ber in ulazBER.Decompose()) { setOfObject.Add(this.type.CreateInstance(ber)); } /// Dolazak ovdje predstavlja gresku! return setOfObject; }
protected ASN1Sequence SubCreateInstance(ASN1Sequence asnseq, BEREncoding ulazBER) { asnseq.fromBER(ulazBER); asnseq.RegisterComponents(); return asnseq; }
public override void fromBER(BEREncoding ulazBER) { /// BER vraca niz BER kodova koji se /// lako dekodiraju. Bitna stvar ovdje je /// provjeriti adekvatnost BER-a i "trenutnog" tipa: /// Ako BER ne odgovara ocekivanom tipu moze biti /// da je tip bio oznacen sa OPTIONAL ili DEFAULT. /// Ako je OPTIONAL, preskocimo tip i pokusamo sa /// sljedecim tipom. Ako je DEFAULT, stavimo na odgovarajuce /// mjesto referencu na DefaultValue i u istoj /// iteraciji petlje pokusamo za sljedeci tip. BEREncoding[] berArray = ulazBER.Decompose(); BEREncoding ber; ASN1ComponentTypeInfo currentTypeInfo; int i=0, /// i je indeks trenutnog BER koda j=0; /// j je indeks trenutne komponente /// Vrijedi: i <= j. /* ___ ___ | 0| | 0| |___| |___| i --> | 1| | 1| |___| |___| | 2| | 2| <-- j |___| |___| | 3| | 3| ... */ // iteriraj po svim dobivenim ber-ovima. #region 1. Petlja po dobivenim ber-ovima for ( ; i < berArray.Length; i++, j++) { ber = berArray[i]; currentTypeInfo = this.ComponentTypes[j]; int tag = BEREncoding.DecodeTagNumber(ber.GetIdOctets()); /// Probaj Match-ati tag i currentTypeInfo.Type.Tag try { while (CheckThisShitUpBitch(tag, j, currentTypeInfo)) { /// ako TAG ne odgovara trenutnoj komponenti, pokusaj povezati /// trenutni BER sa nekom od sljedecih komponenata. /// /// Dok skipas, /// provjeri da li ovaj SEQUENCE tip dozvoljava skipanje /// (tj. da li je komponenta markirana sa /// DEFAULT ili OPTIONAL) if(currentTypeInfo.DefaultValue != null) { this.Components[j] = currentTypeInfo.DefaultValue; } else if (currentTypeInfo.isOptional) { } // else if (currentTypeInfo.DefaultValue != null) // { // this.Components[j] = currentTypeInfo.DefaultValue; // } else { throw new Exception ("A type that was not marked as optional is missing"); } // j++; // currentTypeInfo = this.ComponentTypes[j]; currentTypeInfo = this.ComponentTypes[++j]; } } catch (System.IndexOutOfRangeException) { /* -- to znaci da je puklo pri 'skipanju' optionalnih tipova. * Moglo bi se zakljuciti da nedostaje odgovarajuci tip. */ Console.WriteLine("indeks j je pobjegao van"); throw new Exception("Indeks j u ASN1Sequence.fromBER je pobjegao"); } finally {} this.Components[j] = currentTypeInfo.CreateInstance(ber); } #endregion #region 2. Provjeri da li (ako) fale tipovi markirani sa DEFAULT ili OPTIONAL for ( ; j < this.numOfComponents; j++ ) { /// Vise nemam BER ova, ali ako uopce /// dodje do izvrsavanja ove petlje znaci da /// je ostalo neinicijaliziranih polja ovog tipa. /// Ako su polja optional - to nije problem. No - ako su /// to bila Default polja DER kodiranja, te komponente treba /// inicijalizirati. /// currentTypeInfo = this.ComponentTypes[j]; if (currentTypeInfo.isOptional) { } else { if (currentTypeInfo.DefaultValue != null) { this.Components[j] = currentTypeInfo.DefaultValue; } else { //FIXME throw new Exception("A type is missing (not default, not optional)"); } } } #endregion this.RegisterComponents(); }
public void Assign(BEREncoding nekiBER) { this.fromBER(nekiBER); }
/* -------------------------------------------------------- */ /// <summary> /// <para> /// Factory method - creates ASN1Object instances from their BER /// encoding. /// </para> /// <para> /// Most types don't override this method. /// </para> /// </summary> /// <param name="ulazBER">BER code</param> /// <returns>Instance of ASN1Object derived class.</returns> public virtual ASN1Object CreateInstance(BEREncoding ulazBER) { ASN1Object asn1Object = this.Instance(); asn1Object.fromBER(ulazBER); return asn1Object; }
public virtual void fromBER(BEREncoding ulazBER) { ulazBER.Decode(); }
public ASN1Object(BEREncoding ulazBER):this() { this.fromBER(ulazBER); }
/// <summary> /// Ova metoda vraca polje BER kodova koji su "concatenated" u /// contents dijelu. /// /// /// Mozda bi bilo bolje da se zove DecomposeContents... /// food for thought. /// </summary> /// <returns>polje BER kodova (BEREncoding objekata)</returns> public BEREncoding[] Decompose() { if (this.decQ == false) { this.Decode(); } ArrayList BERList = new ArrayList(); if (this.constructedQ) { int bytesProcessed=0; int totalLen = this.ContentsOctets.Length; int lastIndex; while ( bytesProcessed < totalLen ) { Byte[] tmpByteArray; int i; lastIndex = bytesProcessed; bytesProcessed += BEREncoding.scanBERlen(bytesProcessed, this.ContentsOctets); bytesProcessed++; tmpByteArray = new Byte[bytesProcessed - lastIndex]; for (i=0; i < (bytesProcessed - lastIndex); i++ ) { tmpByteArray[i] = this.ContentsOctets[lastIndex+i]; } BERList.Add(new BEREncoding(tmpByteArray)); } } else { BERList.Add(this); } int l = BERList.Count; BEREncoding[] ret_array = new BEREncoding[l]; BERList.CopyTo(0, ret_array, 0, l); return (ret_array); }
public override ASN1Object CreateInstance(BEREncoding ulazBER) { ASN1Sequence aseq = this.Instance() as ASN1Sequence; aseq.fromBER(ulazBER); return aseq; }
public override void fromBER(BEREncoding ulazBER) { /// OVO TREBA TESTIRATI!!! ulazBER.Decode(); /* FIXME: Dizi exception ako je 'type==null' */ foreach(BEREncoding ber in ulazBER.Decompose()) { this.Add(this.type.CreateInstance(ber)); } }
/// <summary> /// Ovo je metoda koja ce zamijeniti .CreateInstance u /// SEQUENCE type-u, a i .CreateInstance u klasama koje naslijedjuju /// od SEQUENCE-a... /// </summary> /// <param name="ulazBER"></param> /// <returns></returns> public virtual ASN1Object NewCreateInstance(BEREncoding ulazBER) { ASN1Sequence aseq = this.Instance() as ASN1Sequence; aseq.fromBER(ulazBER); return aseq; }
/// <summary> /// ovo sam zasrao. Ovo mora ici u ASN1SetOf.fromBER() /// </summary> /// <param name="ulazBER"></param> /// <returns></returns> public override ASN1Object CreateInstance(BEREncoding ulazBER) { ASN1SetOf setOfObject = this.Instance() as ASN1SetOf; ulazBER.Decode(); foreach(BEREncoding ber in ulazBER.Decompose()) { setOfObject.Add(this.type.CreateInstance(ber)); } /// Dolazak ovdje predstavlja gresku! return setOfObject; }
public override void fromBER(BEREncoding ulazBER) { object[] tmpObj = ulazBER.Decompose(); for (int i=0 ; i < tmpObj.Length; i++) { BEREncoding ber = (tmpObj[i] as BEREncoding); ASN1Object ob = this.type.CreateInstance(ber); this.Add(ob); } }
public ASN1IA5String(BEREncoding ulazniBER): this() { this.fromBER(ulazniBER); }
public override ASN1Object CreateInstance(BEREncoding ulazBER) { ASN1SequenceOf asnseqof = this.Instance() as ASN1SequenceOf; ArrayList BERLista = new ArrayList(ulazBER.Decompose()); /// sad imam listu BERova. /// za svaki BER treba kreirati odgovarajuci objekt. ASN1Object tmpasn; foreach (BEREncoding tmpBER in BERLista) { /// kreiranje odgovarajuceg objekta... tmpasn = type.CreateInstance(tmpBER); /// dodavanje objekta u SequenceOf-ov niz objekata. asnseqof.Add(tmpasn); } return asnseqof; }
public override void fromBER(BEREncoding ulazBER) { ulazBER.Decode(); this.val = (ulazBER.GetContents()[0] != 0x00); }
public void Test_Certificate() { try { BEREncoding ber = BERReader.CreateBEREncoding("test.cert.der"); BERWriter.DumpHEX(ber); Certificate cert = new Certificate(); cert.fromBER(ber); ResourceLoader rl = new ResourceLoader(); foreach (string fileName in ResourceLoader.fileNames) { Console.WriteLine("\n"+fileName); Stream strm = rl.GetStream(fileName); ber = BERReader.CreateBEREncoding(strm); cert.fromBER(ber); X509.Extensions exts = cert.certificate.extensions.underObject as X509.Extensions; foreach(X509.Extension ext in exts.Elements) { //BERWriter.DumpHEX(ext); //BERWriter.DumpHEX(ext.etxnId); //Console.WriteLine(ext.etxnId.ToString()); if (ext.critical.val) { Console.Write("CRITICAL, "); } else { Console.Write("NON-CRITICAL, "); } Console.Write(OID.Helper.OIDHelper.ht.GetDescription(ext.etxnId)+", "); ASN1OctetString os = ext.extnValue; BEREncoding innerBER = new BEREncoding(os.asDER().GetContents()); Console.Write(ext.innerExtnValue.GetType().ToString()+", "); BERWriter.DumpHEX(ext.innerExtnValue); } BERWriter.DumpHEX(cert); } Console.WriteLine("Success."); } catch (UnrecognizedObjectIdentifierException oidException) { Console.WriteLine(oidException.Message); } }
public override void fromBER(BEREncoding ulazBER) { Byte[] tmp; int i; base.fromBER (ulazBER); tmp = ulazBER.GetContents(); this.NumOfBits = (tmp.Length-1)*8 - tmp[0]; this.bitString = new Byte[tmp.Length - 1]; for (i=0;i<this.bitString.Length; i++) { this.bitString[i]=tmp[i+1]; } }
public void Write(BEREncoding ber) { this.Write(ber.GetBERCode()); }
/// <summary> /// Class constructor, used for decoding to ASN.1's type class. /// Initializes with (encoded) BER code. /// </summary> /// <param name="ulazBER">input BER code (as BEREncoding)</param> public ASN1OctetString(BEREncoding ulazBER):this() { this.fromBER(ulazBER); }
public static void DumpHEX(BEREncoding ber) { Console.Write("HEX: "); byte[] code = ber.GetBERCode(); foreach (byte b in code) { if(b<16) { Console.Write("0"); } Console.Write(b.ToString("X")+":"); } Console.WriteLine(); }
public override ASN1Object CreateInstance(BEREncoding ulazBER) { /// A vjerojatno bi trebalo provjeriti da li ovaj /// BER odgovara NULL kodu. return Instance(); }
public override ASN1Object CreateInstance(BEREncoding ulazBER) { return this.SubCreateInstance(this.Instance() as ASN1Choice, ulazBER); }