// Extract a big integer value. public byte[] GetBigInt(ASN1Type type) { if (Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } int len = Length; int ofs = Offset; byte[] value; if (len > 1 && buffer[offset + ofs] == 0) { // Strip the leading zero byte. value = new byte [len - 1]; Array.Copy(buffer, offset + ofs + 1, value, 0, len - 1); } else { // The first byte is non-zero. value = new byte [len]; Array.Copy(buffer, offset + ofs, value, 0, len); } offset += len + ofs; count -= len + ofs; return(value); }
// Add a container to this builder. public ASN1Builder AddContainer(ASN1Type type) { ASN1Builder container = new ASN1Builder(type); list.Add(container); return(container); }
public bool IsString() { ASN1Type type = Type; return(type == ASN1Type.PrintableString || type == ASN1Type.IA5String); }
public void Skip(ASN1Type type) { if (Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } Skip(); }
public String GetString(ASN1Type type) { if (Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } return(GetStringChecked()); }
public String GetString() { ASN1Type type = Type; if (type != ASN1Type.PrintableString && type != ASN1Type.IA5String) { // Not one of the standard string types. throw new CryptographicException (_("Crypto_InvalidASN1")); } return(GetStringChecked()); }
// Extract a 64-bit integer value. public long GetInt64(ASN1Type type) { if (Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } int len = Length; int ofs = Offset; int posn = 0; long value; int byteval; if (len < 1) { // Need at least 1 octet. throw new CryptographicException (_("Crypto_InvalidASN1")); } byteval = buffer[offset + ofs + posn]; if ((byteval & 0x80) != 0) { value = (-1L << 8) | (long)(uint)byteval; } else { value = (long)byteval; } ++posn; while (posn < len) { byteval = buffer[offset + ofs + posn]; value = (value << 8) | (long)(uint)byteval; ++posn; } offset += len + ofs; count -= len + ofs; return(value); }
// Get a parser for the contents of a field. public ASN1Parser GetContents(ASN1Type type, int adjust) { if (Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } int len = Length; int ofs = Offset; if (len < adjust) { // Contents are no long enough. throw new CryptographicException (_("Crypto_InvalidASN1")); } ASN1Parser parser = new ASN1Parser(buffer, offset + ofs + adjust, len - adjust); offset += len + ofs; count -= len + ofs; return(parser); }
// Get a byte array that contains the contents of a field. public byte[] GetContentsAsArray(ASN1Type type, int adjust) { if (Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } int len = Length; int ofs = Offset; if (len < adjust) { // Contents are no long enough. throw new CryptographicException (_("Crypto_InvalidASN1")); } byte[] result = new byte [len - adjust]; Array.Copy(buffer, offset + ofs + adjust, result, 0, len - adjust); offset += len + ofs; count -= len + ofs; return(result); }
// Extract an object identifier. public byte[] GetObjectIdentifier(ASN1Type type) { return(GetContentsAsArray(type)); }
// Add a big integer to this builder. public void AddBigInt(ASN1Type type, byte[] value) { list.Add(new ASN1BigIntBuilder(type, value)); }
// Add an octet string to this builder. public void AddOctetString(ASN1Type type, byte[] value) { AddByteArray(type, value); }
// Extract a big integer value. public byte[] GetBigInt(ASN1Type type) { if(Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } int len = Length; int ofs = Offset; byte[] value; if(len > 1 && buffer[offset + ofs] == 0) { // Strip the leading zero byte. value = new byte [len - 1]; Array.Copy(buffer, offset + ofs + 1, value, 0, len - 1); } else { // The first byte is non-zero. value = new byte [len]; Array.Copy(buffer, offset + ofs, value, 0, len); } offset += len + ofs; count -= len + ofs; return value; }
public byte[] GetContentsAsArray(ASN1Type type) { return GetContentsAsArray(type, 0); }
public void Skip(ASN1Type type) { if(Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } Skip(); }
/// <summary> /// This is advanced interface - this is what I would like... /// </summary> /// <param name="asnseq"></param> /// <param name="num"></param> /// <param name="tip"></param> public static void Make(ASN1Sequence asnseq, int num, ASN1Type tip) { asnseq.ComponentTypes[num] = ASN1ComponentTypeInfo.Make(tip); }
// Add a bit string to this builder. public void AddBitString(ASN1Type type, byte[] value) { list.Add(new ASN1BitStringBuilder(type, value)); }
// Add a byte array field to this builder. public void AddByteArray(ASN1Type type, byte[] value) { list.Add(new ASN1ByteBuilder(type, value)); }
// Add a set to this builder. public ASN1Builder AddSet(ASN1Type type) { return(AddContainer(type)); }
// Constructor. public ASN1BitStringContentsBuilder(ASN1Type type) : base(type) { }
// Constructor. public ASN1BitStringBuilder(ASN1Type type, byte[] value) : base(type, 0) { this.value = value; }
// Constructor. public ASN1Int32Builder(ASN1Type type, int value) : base(type, 0) { this.value = value; }
protected ASN1Builder(ASN1Type type, int dummy) { this.type = type; this.list = null; }
// Get a UTC time value. public String GetUTCTime(ASN1Type type) { return(GetString(type)); }
public static ASN1ComponentTypeInfo Make(ASN1Type tip, bool optional) { return new ASN1ComponentTypeInfo(tip, optional); }
// Add a UTCTime value to this builder. public void AddUTCTime(ASN1Type type, String value) { AddString(type, value); }
public ASN1ComponentTypeInfo(ASN1Type tip, bool optional) { this.Type = tip; this.isOptional = optional; }
public ASN1Parser GetContents(ASN1Type type) { return GetContents(type, 0); }
// Constructor. public ASN1Int64Builder(ASN1Type type, long value) : base(type, 0) { this.value = value; }
public String GetString(ASN1Type type) { if(Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } return GetStringChecked(); }
public ASN1Builder(ASN1Type type) { this.type = type; this.list = new ArrayList(); }
// Add an object identifier to this builder. public void AddObjectIdentifier(ASN1Type type, byte[] value) { AddByteArray(type, value); }
// Add a container to this builder. public ASN1Builder AddContainer(ASN1Type type) { ASN1Builder container = new ASN1Builder(type); list.Add(container); return container; }
// Add a 64-bit integer to this builder. public void AddInt64(ASN1Type type, long value) { list.Add(new ASN1Int64Builder(type, value)); }
// Extract an octet string. public byte[] GetOctetString(ASN1Type type) { return(GetContentsAsArray(type)); }
// Constructor. public ASN1BitStringContentsBuilder(ASN1Type type) : base(type) {}
public static ASN1ComponentTypeInfo Make(ASN1Type tip) { return new ASN1ComponentTypeInfo(tip); }
// Add a set to this builder. public ASN1Builder AddSet(ASN1Type type) { return AddContainer(type); }
public static ASN1ComponentTypeInfo Make(ASN1Type tip, bool optional, ASN1Object defaultValue) { return new ASN1ComponentTypeInfo(tip, optional, defaultValue); }
/// <summary> /// Basic constructor, type w/out qualifiers. /// </summary> /// <param name="tip">type of the component</param> public ASN1ComponentTypeInfo(ASN1Type tip) { this.Type = tip; this.isOptional = false; }
public ASN1ComponentTypeInfo(ASN1Type tip, ASN1Object defaultValue) { this.Type = tip; this.isOptional = false; this.DefaultValue = defaultValue; }
// Get a parser for the contents of a field. public ASN1Parser GetContents(ASN1Type type, int adjust) { if(Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } int len = Length; int ofs = Offset; if(len < adjust) { // Contents are no long enough. throw new CryptographicException (_("Crypto_InvalidASN1")); } ASN1Parser parser = new ASN1Parser(buffer, offset + ofs + adjust, len - adjust); offset += len + ofs; count -= len + ofs; return parser; }
// Extract an octet string. public byte[] GetOctetString(ASN1Type type) { return GetContentsAsArray(type); }
// Get a byte array that contains the contents of a field. public byte[] GetContentsAsArray(ASN1Type type, int adjust) { if(Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } int len = Length; int ofs = Offset; if(len < adjust) { // Contents are no long enough. throw new CryptographicException (_("Crypto_InvalidASN1")); } byte[] result = new byte [len - adjust]; Array.Copy(buffer, offset + ofs + adjust, result, 0, len - adjust); offset += len + ofs; count -= len + ofs; return result; }
// Add a 32-bit integer to this builder. public void AddInt32(ASN1Type type, int value) { list.Add(new ASN1Int32Builder(type, value)); }
// Extract a bit string. public byte[] GetBitString(ASN1Type type) { return GetContentsAsArray(type, 1); }
public ASN1Parser GetContents(ASN1Type type) { return(GetContents(type, 0)); }
// Extract a 64-bit integer value. public long GetInt64(ASN1Type type) { if(Type != type) { // Not the expected type. throw new CryptographicException (_("Crypto_InvalidASN1")); } int len = Length; int ofs = Offset; int posn = 0; long value; int byteval; if(len < 1) { // Need at least 1 octet. throw new CryptographicException (_("Crypto_InvalidASN1")); } byteval = buffer[offset + ofs + posn]; if((byteval & 0x80) != 0) { value = (-1L << 8) | (long)(uint)byteval; } else { value = (long)byteval; } ++posn; while(posn < len) { byteval = buffer[offset + ofs + posn]; value = (value << 8) | (long)(uint)byteval; ++posn; } offset += len + ofs; count -= len + ofs; return value; }
public byte[] GetContentsAsArray(ASN1Type type) { return(GetContentsAsArray(type, 0)); }
// Extract an object identifier. public byte[] GetObjectIdentifier(ASN1Type type) { return GetContentsAsArray(type); }
// Get a UTC time value. public String GetUTCTime(ASN1Type type) { return GetString(type); }
// Add a string to this builder. public void AddString(ASN1Type type, String value) { list.Add(new ASN1ByteBuilder (type, Encoding.UTF8.GetBytes(value))); }