public string contentString(ASN1Tag tag) { string val = ""; if (tag.Data != null) { val += " Len " + tag.Data.Length.ToString("X2") + ":" + new ByteArray(tag.Data).Sub(0, Math.Min(30, tag.Data.Length)); } return(val); }
/// <summary> /// Ritorna un sotto-tag dell'oggetto verificando che il suo numero di tag corrisponda a quello specificato (sotto forma di intero senza segno raw) /// </summary> /// <param name="tagNum">Il numero di sequenza del sotto-tag (a partire da 0)</param> /// <param name="tagCheck">Il numero del sotto-tag da verificare</param> /// <returns>L'oggetto che contiene il sotto-tag</returns> public ASN1Tag Child(int tagNum, byte[] tagCheck) { ASN1Tag subTag = children[tagNum]; if (!AreEqual(subTag.tag, tagCheck)) { throw new Exception("Check del tag fallito"); } return(subTag); }
/// <summary> /// Ritorna un sotto-tag dell'oggetto verificando che il suo numero di tag corrisponda a quello specificato (sotto forma di intero senza segno raw) /// </summary> /// <param name="tagNum">Il numero di sequenza del sotto-tag (a partire da 0)</param> /// <param name="tagCheck">Il numero del sotto-tag da verificare</param> /// <returns>L'oggetto che contiene il sotto-tag</returns> public ASN1Tag Child(int tagNum, uint tagCheck) { ASN1Tag tag = children[tagNum]; if (tag.tagRawNumber != tagCheck) { throw new Exception("Check del tag fallito"); } return(tag); }
public string contentString(ASN1Tag tag) { try { //primi due numeri; List <int> nums = new List <int>(); int n2 = (tag.Data[0] % 40); int n1 = ((tag.Data[0] - n2) / 40); nums.Add(n1); nums.Add(n2); int pos = 1; int curNum = 0; bool doing = false; while (pos < tag.Data.Length) { byte val = tag.Data[pos]; curNum = (curNum << 7) | (val & 127); if ((val & 0x80) == 0) { nums.Add(curNum); curNum = 0; doing = false; } else { doing = true; } pos++; } if (doing) { return("INVALID OID"); } else { String res = ""; foreach (int i in nums) { res += i.ToString() + "."; } return(res.Substring(0, res.Length - 1)); } } catch { return("INVALID OID"); } }
public string contentString(ASN1Tag tag) { return(ASCIIEncoding.ASCII.GetString(tag.Data)); }
public string contentString(ASN1Tag tag) { return("NULL"); }
internal static ASN1Tag Parse(Stream s, UInt32 start, UInt32 length, ref UInt32 size, bool reparse) { UInt32 readPos = 0; if (readPos == length) { throw new Exception(); } // leggo il tag List <byte> tagVal = new List <byte>(); int tag = s.ReadByte(); readPos++; tagVal.Add((byte)tag); if ((tag & 0x1f) == 0x1f) { // è un tag a più bytes; proseguo finchè non trovo un bit 8 a 0 while (true) { if (readPos == length) { throw new Exception(); } tag = s.ReadByte(); readPos++; tagVal.Add((byte)tag); if ((tag & 0x80) != 0x80) { // è l'ultimo byte del tag break; } } } // leggo la lunghezza if (readPos == length) { throw new Exception(); } UInt32 len = (UInt32)s.ReadByte(); readPos++; if (len > 0x80) { UInt32 lenlen = len - 0x80; len = 0; for (int i = 0; i < lenlen; i++) { if (readPos == length) { throw new Exception(); } len = (UInt32)((len << 8) | (byte)s.ReadByte()); readPos++; } } else if (len == 0x80) { throw new Exception("Lunghezza indefinita non supportata"); } size = (UInt32)(readPos + len); if (size > length) { throw new Exception("ASN1 non valido"); } if (tagVal.Count == 1 && tagVal[0] == 0 && len == 0) { return(null); } byte[] data = new byte[len]; s.Read(data, 0, (int)len); MemoryStream ms = new MemoryStream(data); // quando devo parsare i sotto tag?? // in teoria solo se il tag è constructed, ma // spesso una octetstring o una bitstring sono // delle strutture ASN1... ASN1Tag newTag = new ASN1Tag(tagVal.ToArray()); List <ASN1Tag> childern = null; UInt32 parsedLen = 0; bool parseSubTags = false; if (newTag.tagConstructed) { parseSubTags = true; } else if (reparse && KnownTag(newTag.tag) == "OCTET STRING") { parseSubTags = true; } else if (reparse && KnownTag(newTag.tag) == "BIT STRING") { parseSubTags = true; newTag.unusedBits = (byte)ms.ReadByte(); parsedLen++; } if (parseSubTags) { childern = new List <ASN1Tag>(); while (true) { UInt32 childSize = 0; try { ASN1Tag child = Parse(ms, start + readPos + parsedLen, (UInt32)(len - parsedLen), ref childSize, reparse); if (child != null) { childern.Add(child); } } catch { childern = null; break; } parsedLen += childSize; if (parsedLen > len) { childern = null; break; } else if (parsedLen == len) { data = null; break; } } } newTag.startPos = start; newTag.endPos = start + size; if (childern == null) { newTag.data = data; } else { newTag.children = childern; } return(newTag); }