void decode(Asn1Reader asn) { if (asn.Tag != 48) { throw new Asn1InvalidTagException(asn.Offset); } RawData = asn.GetTagRawData(); Int32 offset = asn.Offset; asn.MoveNext(); SerialNumber = Asn1Utils.DecodeInteger(asn.GetTagRawData(), true); asn.MoveNextAndExpectTags(Asn1Type.UTCTime, Asn1Type.GeneralizedTime); switch (asn.Tag) { case (Byte)Asn1Type.UTCTime: RevocationDate = new Asn1UtcTime(asn.GetTagRawData()).Value; break; case (Byte)Asn1Type.GeneralizedTime: RevocationDate = Asn1Utils.DecodeGeneralizedTime(asn.GetTagRawData()); break; } if (asn.MoveNextSibling()) { // use high-performant extension decoder instead of generic one. // Since CRLs may store a hundreds of thousands entries, this is // pretty reasonable to save loops whenever possible. readCrlReasonCode(asn); } asn.Seek(offset); }
void m_initialize(Byte[] rawData) { Asn1Reader asn = new Asn1Reader(rawData); if (asn.Tag != 48) { throw new Asn1InvalidTagException(asn.Offset); } asn.MoveNext(); SerialNumber = Asn1Utils.DecodeInteger(asn.GetTagRawData(), true); asn.MoveNext(); if (asn.Tag != (Byte)Asn1Type.UTCTime && asn.Tag != (Byte)Asn1Type.GeneralizedTime) { throw new Asn1InvalidTagException(asn.Offset); } if (asn.Tag == (Byte)Asn1Type.UTCTime) { RevocationDate = new Asn1UtcTime(asn.GetTagRawData()).Value; } if (asn.Tag == (Byte)Asn1Type.GeneralizedTime) { RevocationDate = Asn1Utils.DecodeGeneralizedTime(asn.GetTagRawData()); } if (asn.MoveNext()) { var extensions = new X509ExtensionCollection(); extensions.Decode(asn.GetTagRawData()); X509Extension crlReason = extensions[X509CertExtensions.X509CRLReasonCode]; if (crlReason != null) { ReasonCode = crlReason.RawData[2]; } } RawData = rawData; }
public void WriteUtcTimeFromString(string correctResultHex, string valueToTest) { var encoded = Helpers.GetExampleBytes(correctResultHex); var datetime = Helpers.ConvertFromUniversalTime(valueToTest); using (var ms = new MemoryStream()) { var asn1Obj = new Asn1UtcTime(datetime); new DerWriter(ms).Write(asn1Obj); var res = Enumerable.SequenceEqual(encoded, ms.ToArray()); Assert.IsTrue(res); } }
public void WriteUtcTime(string correctResultHex, string valueToTest) { var encoded = Helpers.GetExampleBytes(correctResultHex); var datetime = DateTimeOffset.ParseExact(valueToTest, "yyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); using (var ms = new MemoryStream()) { var asn1Obj = new Asn1UtcTime(datetime); new DerWriter(ms).Write(asn1Obj); var res = Enumerable.SequenceEqual(encoded, ms.ToArray()); Assert.IsTrue(res); } }
static String DecodeUtcTime(Asn1Reader asn) { DateTime dt = Asn1UtcTime.Decode(asn); return(dt.ToShortDateString() + " " + dt.ToShortTimeString()); }
static String DecodeUtcTime(Asn1Reader asn) { DateTime dt = new Asn1UtcTime(asn).Value; return(dt.ToString("dd.MM.yyyy hh:mm:ss")); }
static String DecodeUtcTime(Asn1Reader asn) { var dt = new Asn1UtcTime(asn).Value; return(dt.ToShortDateString() + " " + dt.ToShortTimeString()); }
void m_decode(Byte[] rawData) { try { Type = X509CrlType.BaseCrl; var signedInfo = new SignedContentBlob(rawData, ContentBlobType.SignedBlob); // signature and alg signature = signedInfo.Signature.Value; sigUnused = signedInfo.Signature.UnusedBits; SignatureAlgorithm = signedInfo.SignatureAlgorithm.AlgorithmId; // tbs Asn1Reader asn = new Asn1Reader(signedInfo.ToBeSignedData); if (!asn.MoveNext()) { throw new Asn1InvalidTagException(); } // version if (asn.Tag == (Byte)Asn1Type.INTEGER) { Version = (Int32)Asn1Utils.DecodeInteger(asn.GetTagRawData()) + 1; asn.MoveNextCurrentLevel(); } else { Version = 1; } // hash algorithm var h = new AlgorithmIdentifier(asn.GetTagRawData()); if (h.AlgorithmId.Value != SignatureAlgorithm.Value) { throw new CryptographicException("Algorithm mismatch."); } if (!asn.MoveNextCurrentLevel()) { throw new Asn1InvalidTagException(); } // issuer IssuerName = new X500DistinguishedName(asn.GetTagRawData()); // NextUpdate, RevokedCerts and Extensions are optional. Ref: RFC5280, p.118 if (!asn.MoveNextCurrentLevel()) { throw new Asn1InvalidTagException(); } switch (asn.Tag) { case (Byte)Asn1Type.UTCTime: ThisUpdate = new Asn1UtcTime(asn.GetTagRawData()).Value; break; case (Byte)Asn1Type.GeneralizedTime: ThisUpdate = Asn1Utils.DecodeGeneralizedTime(asn.GetTagRawData()); break; default: throw new Asn1InvalidTagException(); } if (!asn.MoveNextCurrentLevel()) { return; } switch (asn.Tag) { case (Byte)Asn1Type.UTCTime: case (Byte)Asn1Type.GeneralizedTime: switch (asn.Tag) { case (Byte)Asn1Type.UTCTime: NextUpdate = new Asn1UtcTime(asn.GetTagRawData()).Value; break; case (Byte)Asn1Type.GeneralizedTime: NextUpdate = Asn1Utils.DecodeGeneralizedTime(asn.GetTagRawData()); break; default: throw new Asn1InvalidTagException(); } if (!asn.MoveNextCurrentLevel()) { return; } if (asn.Tag == 48) { getRevCerts(asn); if (!asn.MoveNextCurrentLevel()) { return; } getExts(asn); } else { getExts(asn); } break; case 48: if (asn.Tag == 48) { getRevCerts(asn); if (!asn.MoveNextCurrentLevel()) { return; } getExts(asn); } else { getExts(asn); } break; default: getExts(asn); break; } } catch (Exception e) { throw new CryptographicException("Cannot find the requested object.", e); } }
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); }