internal DNPart(string oid, AsnElt val) { OID_ = oid; AsnValue_ = val; encodedValue = val.Encode(); uint hc = (uint)oid.GetHashCode(); try { string s = val.GetString(); Value_ = s; s = s.ToUpperInvariant().ToLowerInvariant(); StringBuilder sb = new StringBuilder(); bool lwws = true; foreach (char c in s.Trim()) { if (IsControl(c)) { continue; } if (IsWS(c)) { if (lwws) { continue; } lwws = true; sb.Append(' '); } else { sb.Append(c); } } int n = sb.Length; if (n > 0 && sb[n - 1] == ' ') { sb.Length = n - 1; } normValue = sb.ToString(); hc += (uint)normValue.GetHashCode(); } catch { if (OID_TO_FT.ContainsKey(oid)) { throw; } Value_ = null; foreach (byte b in encodedValue) { hc = ((hc << 7) | (hc >> 25)) ^ (uint)b; } } hashCode = (int)hc; }
static AsnElt Duplicate(AsnElt ae) { if (ae.Constructed) { int n = ae.Sub.Length; AsnElt[] ss = new AsnElt[n]; for (int i = 0; i < n; i++) { ss[i] = Duplicate(ae.Sub[i]); } return(AsnElt.Make(ae.TagClass, ae.TagValue, ss)); } if (ae.TagClass == AsnElt.UNIVERSAL) { switch (ae.TagValue) { case AsnElt.BOOLEAN: return(ae.GetBoolean() ? AsnElt.BOOL_TRUE : AsnElt.BOOL_FALSE); case AsnElt.INTEGER: return(AsnElt.MakeIntegerSigned( ae.CopyValue())); case AsnElt.OBJECT_IDENTIFIER: return(AsnElt.MakeOID(ae.GetOID())); case AsnElt.NumericString: case AsnElt.PrintableString: case AsnElt.IA5String: case AsnElt.TeletexString: case AsnElt.UTF8String: case AsnElt.BMPString: case AsnElt.UniversalString: case AsnElt.UTCTime: case AsnElt.GeneralizedTime: return(AsnElt.MakeString( ae.TagValue, ae.GetString())); } } /* * All other primitive types will be treated as blobs. * We still need to duplicate them in order to avoid * variants in tag/length encoding. */ return(AsnElt.MakePrimitive(ae.TagClass, ae.TagValue, ae.CopyValue())); }
static void Parse(TextWriter tw, int depth, AsnElt ae) { Indent(tw, depth); tw.Write("("); switch (ae.TagClass) { case AsnElt.APPLICATION: tw.Write("[application " + ae.TagValue + "]"); break; case AsnElt.CONTEXT: tw.Write("[" + ae.TagValue + "]"); break; case AsnElt.PRIVATE: tw.Write("[private " + ae.TagValue + "]"); break; default: switch (ae.TagValue) { case AsnElt.BOOLEAN: tw.Write("bool " + ae.GetBoolean() + ")"); return; case AsnElt.INTEGER: tw.Write("int " + ae.GetIntegerHex() + ")"); return; case AsnElt.BIT_STRING: int bitLen; byte[] bs = ae.GetBitString(out bitLen); tw.Write("bits " + (bs.Length * 8 - bitLen)); PrintBytes(tw, depth, bs); tw.Write(")"); return; case AsnElt.OCTET_STRING: tw.Write("blob"); PrintBytes(tw, depth, ae.CopyValue()); tw.Write(")"); return; case AsnElt.NULL: ae.CheckNull(); tw.Write("null)"); return; case AsnElt.OBJECT_IDENTIFIER: string oid = ae.GetOID(); if (!numOID) { oid = AsnOID.ToName(oid); } tw.Write("oid " + oid + ")"); return; case AsnElt.NumericString: tw.Write("numeric " + EscapeString(ae.GetString()) + ")"); return; case AsnElt.PrintableString: tw.Write("printable " + EscapeString(ae.GetString()) + ")"); return; case AsnElt.IA5String: tw.Write("ia5 " + EscapeString(ae.GetString()) + ")"); return; case AsnElt.TeletexString: tw.Write("teletex " + EscapeString(ae.GetString()) + ")"); return; case AsnElt.UTF8String: tw.Write("utf8 " + EscapeString(ae.GetString()) + ")"); return; case AsnElt.BMPString: tw.Write("bmp " + EscapeString(ae.GetString()) + ")"); return; case AsnElt.UniversalString: tw.Write("utf32 " + EscapeString(ae.GetString()) + ")"); return; case AsnElt.UTCTime: tw.Write("utc " + EscapeString(ae.GetString()) + " {" + ae.GetTime() + "} )"); return; case AsnElt.GeneralizedTime: tw.Write("gentime " + EscapeString(ae.GetString()) + " {" + ae.GetTime() + "} )"); return; case AsnElt.SEQUENCE: if (!ae.Constructed) { throw new AsnException( "Non-constructed SEQUENCE"); } tw.Write("sequence"); ParseSubs(tw, depth, ae); tw.Write(")"); return; case AsnElt.SET: if (!ae.Constructed) { throw new AsnException( "Non-constructed SET"); } tw.Write("set"); ParseSubs(tw, depth, ae); tw.Write(")"); return; default: tw.Write( "[universal " + ae.TagValue + "]"); break; } break; } if (ae.Constructed) { tw.Write("sequence"); ParseSubs(tw, depth, ae); tw.Write(")"); } else { tw.Write("blob"); PrintBytes(tw, depth, ae.CopyValue()); tw.Write(")"); } }