/// <summary> /// Decodes ASN.1-encoded byte array to an instance of <see cref="X509Extension"/> class. /// </summary> /// <param name="rawData">ASN.1-encoded byte array that represents full extension information.</param> /// <exception cref="ArgumentNullException"><strong>rawData</strong> parameter is null.</exception> /// <exception cref="Asn1InvalidTagException">Decoder encountered an unexpected ASN.1 type identifier.</exception> /// <returns>Decoded extension object.</returns> public static X509Extension Decode(Byte[] rawData) { if (rawData == null) { throw new ArgumentNullException(nameof(rawData)); } Asn1Reader asn = new Asn1Reader(rawData); if (asn.Tag != 48) { throw new Asn1InvalidTagException(asn.Offset); } asn.MoveNext(); if (asn.Tag != (Byte)Asn1Type.OBJECT_IDENTIFIER) { throw new Asn1InvalidTagException(asn.Offset); } Oid oid = new Asn1ObjectIdentifier(asn).Value; Boolean critical = false; asn.MoveNext(); if (asn.Tag == (Byte)Asn1Type.BOOLEAN) { critical = Asn1Utils.DecodeBoolean(asn.GetTagRawData()); asn.MoveNext(); } if (asn.Tag != (Byte)Asn1Type.OCTET_STRING) { throw new Asn1InvalidTagException(asn.Offset); } return(CryptographyUtils.ConvertExtension(new X509Extension(oid, asn.GetPayload(), critical))); }
public static string DecodeEncryptionParamSet(byte[] data) { if (data == null) { throw ExceptionUtility.ArgumentNull("data"); } string encryptionParamSet; try { var asnDecoder = new Asn1BerDecodeBuffer(data); var parameters = new Gost2814789BlobParameters(); parameters.Decode(asnDecoder); encryptionParamSet = Asn1ObjectIdentifier.ToOidString(parameters.EncryptionParamSet); } catch (Exception exception) { throw ExceptionUtility.CryptographicException(exception, Resources.Asn1DecodeError, typeof(Gost2814789BlobParameters).FullName); } return(encryptionParamSet); }
/// <summary> /// Decodes cms from ASN reader. /// </summary> /// <param name="asn">An instance of ASN.1 reader that contains signed PKCS#7 data.</param> protected void DecodeCms(Asn1Reader asn) { reset(); asn.MoveNext(); ContentType = new Asn1ObjectIdentifier(asn).Value; asn.MoveNextAndExpectTags(0xa0); // [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL, 0xa0 asn.MoveNextAndExpectTags(0x30); // SEQUENCE OF ANY asn.MoveNextAndExpectTags((Byte)Asn1Type.INTEGER); // version Version = (Int32) new Asn1Integer(asn).Value; asn.MoveNextCurrentLevelAndExpectTags(0x31); decodeDigestAlgorithms(asn); asn.MoveNextCurrentLevelAndExpectTags(0x30); // ContentInfo Byte[] content = extractContent(asn); while (asn.MoveNextCurrentLevel()) { switch (asn.Tag) { case 0xa0: decodeCertificates(asn); break; case 0xa1: decodeCRLs(asn); break; case 0x31: decodeSignerInfos(asn); break; default: throw new ArgumentException("Invalid type."); } } _rawData.AddRange(asn.GetRawData()); DecodeContent(content); }
public X509AlgorithmIdentifier(Asn1ObjectIdentifier algorithmId) { Id = algorithmId; }
private static IReadOnlyCollection <string> FindObjectIdentifiers(Asn1Node node, Asn1ObjectIdentifier oid) { var results = new HashSet <string>(); FindObjectIdentifiers(node, oid, results); return(results); }
public X509RelativeDistinguishedName(Asn1ObjectIdentifier id) { Id = id; }
private string Get(Asn1ObjectIdentifier id) { var item = _items.FirstOrDefault(i => i.Id == id); return(item?.Value); }
private void getVIDHash(X509Certificate2 cert, out string name, out string hashAlg, out byte[] hash) { // ignore warnings name = ""; hashAlg = ""; hash = new byte[] { }; bool notset_n = true, notset_hal = true, notset_ha = true; foreach (var ext in cert.Extensions) { if (ext.Oid.Value != "2.5.29.17") { continue; } Asn1Reader reader = new Asn1Reader(ext.RawData); bool kisaIdentifyData = false, kisaVid = false; // TO-DO : Caluate Depth do { switch (reader.TagName) { case "OBJECT_IDENTIFIER": Asn1ObjectIdentifier identifier = (Asn1ObjectIdentifier)reader.GetTagObject(); string oid = identifier.Value.Value; if (oid == "1.2.410.200004.10.1.1") { kisaIdentifyData = true; } else if (oid == "1.2.410.200004.10.1.1.1") { kisaVid = true; } else if (kisaVid && notset_hal) { hashAlg = oid; notset_hal = false; } break; case "UTF8String": if (kisaIdentifyData && notset_n) { name = Encoding.UTF8.GetString(reader.GetPayload()); notset_n = false; } break; case "OCTET_STRING": if (kisaVid && notset_ha) { SysadminsLV.Asn1Parser.Universal.Asn1OctetString octetString = new SysadminsLV.Asn1Parser.Universal.Asn1OctetString(reader); hash = octetString.Value; notset_ha = false; } break; default: break; } } while (reader.MoveNext()); if (notset_ha || notset_hal || notset_n) { throw new VIDOperationException("Some of vid informations in certificate are missing"); } } }
public AlgorithmIdentifier(Asn1ObjectIdentifier algorithm, Asn1OpenType parameters) { Algorithm = algorithm; Parameters = parameters; }
public void Init() { publicKeyParamSet = null; digestParamSet = null; encryptionParamSet = null; }
private void Init() { Algorithm = null; Parameters = null; }
public void Init() { algorithm = null; parameters = null; }
public ATTRIBUTE_CLASS() { Type = null; id = null; }
private byte[] GetExtensionValue(Asn1ObjectIdentifier id) { var ext = this.FirstOrDefault(e => e.Id == id); return(ext == null?null:ext.Value); }
public AlgorithmId(Asn1ObjectIdentifier id, Asn1Type type) { Id = id; Type = type; }
public UnknownExtension(Asn1ObjectIdentifier id, byte[] data) { Id = id; _data = data; }
private static Gost2814789ParamSet CreateEncryptionParamSet(string value) { return((value != null) ? new Gost2814789ParamSet(Asn1ObjectIdentifier.FromOidString(value).Value) : null); }
private static void DumpSignature(Asn1Object asn1Obj, int level) { string padding = "".PadLeft(level * 2); string items = ""; Asn1ContextSpecificSequence asn1CtxSeq = asn1Obj as Asn1ContextSpecificSequence; if (asn1CtxSeq != null) { items = asn1CtxSeq.Count == 1 ? "item" : "items"; Console.WriteLine("{0}[{1}] ({2} {3})", padding, (int)asn1CtxSeq.Tag & 0x1F, asn1CtxSeq.Count, items); for (int i = 0; i < asn1CtxSeq.Count; i++) { DumpSignature(asn1CtxSeq[i], level + 1); } } else { switch (asn1Obj.Tag) { case Asn1Tag.BitString: Console.WriteLine("{0}BITSTRING {1}", padding, asn1Obj); break; case Asn1Tag.Boolean: Console.WriteLine("{0}BOOLEAN {1}", padding, asn1Obj); break; case Asn1Tag.IA5String: Console.WriteLine("{0}IA5STRING {1}", padding, asn1Obj); break; case Asn1Tag.Integer: Console.WriteLine("{0}INTEGER {1}", padding, asn1Obj); break; case Asn1Tag.Null: Console.WriteLine("{0}{1}", padding, asn1Obj); break; case Asn1Tag.ObjectIdentifier: Asn1ObjectIdentifier oid = asn1Obj as Asn1ObjectIdentifier; Console.WriteLine("{0}OBJECT IDENTIFIER {1} {2}", padding, oid, oid.FriendlyName); break; case Asn1Tag.OctetString: Console.WriteLine("{0}OCTET STRING {1}", padding, asn1Obj); break; case Asn1Tag.PrintableString: Console.WriteLine("{0}PRINTABLESTRING {1}", padding, asn1Obj); break; case Asn1Tag.Sequence: Asn1Sequence asn1Seq = asn1Obj as Asn1Sequence; items = asn1Seq.Count == 1 ? "item" : "items"; Console.WriteLine("{0}SEQUENCE ({1} {2})", padding, asn1Seq.Count, items); for (int i = 0; i < asn1Seq.Count; i++) { DumpSignature(asn1Seq[i], level + 1); } break; case Asn1Tag.Set: Asn1Set asn1Set = asn1Obj as Asn1Set; items = asn1Set.Count == 1 ? "item" : "items"; Console.WriteLine("{0}SET ({1} {2})", padding, asn1Set.Count, items); for (int i = 0; i < asn1Set.Count; i++) { DumpSignature(asn1Set[i], level + 1); } break; case Asn1Tag.UtcTime: Console.WriteLine("{0}UTCTIME {1}", padding, asn1Obj); break; case Asn1Tag.Utf8String: Console.WriteLine("{0}UTF8STRING {1}", padding, asn1Obj); break; } } }
private void Init() { DigestParamSet = null; PublicKeyParamSet = null; EncryptionParamSet = null; }
public ALGORITHM_ID() { id = null; Type = null; }
private void Init() { _publicKeyParamSet = null; _digestParamSet = null; _encryptionParamSet = null; }
private static T MakeInstanceOfAsn1Object <T>(long nodeStartOffset, Asn1Class asn1Class, bool constructed, int tag, SubStream content) where T : Asn1ObjectBase { T foundObject; Asn1ObjectBase shouldBeType = null; if (asn1Class == Asn1Class.ContextSpecific) { shouldBeType = new Asn1ContextSpecific(constructed, tag, content); } else { switch ((Asn1Type)tag) { case Asn1Type.Eoc: shouldBeType = new Asn1Eoc(); break; case Asn1Type.Boolean: shouldBeType = new Asn1Boolean(content, constructed); break; case Asn1Type.Integer: shouldBeType = new Asn1Integer(content, constructed); break; case Asn1Type.BitString: shouldBeType = new Asn1BitString(content, constructed); break; case Asn1Type.OctetString: shouldBeType = new Asn1OctetString(content, constructed); break; case Asn1Type.Null: shouldBeType = new Asn1Null(constructed); break; case Asn1Type.ObjectIdentifier: shouldBeType = new Asn1ObjectIdentifier(content, constructed); break; case Asn1Type.Real: shouldBeType = new Asn1Real(content, constructed); break; case Asn1Type.Enumerated: shouldBeType = new Asn1Enumerated(content, constructed); break; case Asn1Type.Utf8String: shouldBeType = new Asn1Utf8String(content, constructed); break; case Asn1Type.RelativeOid: shouldBeType = new Asn1RelativeOid(content, constructed); break; case Asn1Type.Sequence: shouldBeType = new Asn1Sequence(asn1Class, content, constructed); break; case Asn1Type.Set: shouldBeType = new Asn1Set(asn1Class, content, constructed); break; case Asn1Type.NumericString: shouldBeType = new Asn1NumericString(content, constructed); break; case Asn1Type.PrintableString: shouldBeType = new Asn1PrintableString(content, constructed); break; case Asn1Type.T61String: shouldBeType = new Asn1T61String(content, constructed); break; case Asn1Type.Ia5String: shouldBeType = new Asn1Ia5String(content, constructed); break; case Asn1Type.UtcTime: shouldBeType = new Asn1UtcTime(content, constructed); break; case Asn1Type.GeneralizedTime: shouldBeType = new Asn1GeneralizedTime(content, constructed); break; case Asn1Type.GraphicString: shouldBeType = new Asn1GraphicString(content, constructed); break; case Asn1Type.GeneralString: shouldBeType = new Asn1GeneralString(content, constructed); break; case Asn1Type.UniversalString: shouldBeType = new Asn1UniversalString(content, constructed); break; case Asn1Type.BmpString: shouldBeType = new Asn1BmpString(content, constructed); break; case Asn1Type.ObjectDescriptor: case Asn1Type.External: case Asn1Type.EmbeddedPdv: case Asn1Type.VideotexString: case Asn1Type.VisibleString: case Asn1Type.CharacterString: case Asn1Type.LongForm: default: throw new NotSupportedException($"ASN.1 tag {tag} is unsupported."); } } foundObject = shouldBeType as T; // sanity check if (foundObject == null) { throw new FormatException(FormattableString.Invariant($"ASN.1 node at offset {nodeStartOffset} is of type {shouldBeType.GetType()} but expected type was {typeof(T)}")); } return(foundObject); }