Example #1
0
        internal void Encode(AsnWriter writer, Asn1Tag tag)
        {
            writer.PushSequence(tag);


            // DEFAULT value handler for HashAlgorithm.
            {
                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
                HashAlgorithm.Encode(tmp);

                if (!tmp.EncodedValueEquals(DefaultHashAlgorithm))
                {
                    tmp.CopyTo(writer);
                }
            }

            writer.WriteOctetString(Hash.Span);

            if (IssuerSerial.HasValue)
            {
                IssuerSerial.Value.Encode(writer);
            }

            writer.PopSequence(tag);
        }
Example #2
0
        public static void CopyTo_Empty(AsnEncodingRules ruleSet)
        {
            AsnWriter empty = new AsnWriter(ruleSet);
            AsnWriter dest  = new AsnWriter(AsnEncodingRules.BER);

            Assert.Throws <InvalidOperationException>(() => empty.CopyTo(dest));
        }
Example #3
0
        public static void CopyTo_Null(AsnEncodingRules ruleSet)
        {
            AsnWriter writer = new AsnWriter(ruleSet);

            AssertExtensions.Throws <ArgumentNullException>(
                "destination",
                () => writer.CopyTo(null));
        }
Example #4
0
        public static void CopyTo_Success(AsnEncodingRules ruleSet)
        {
            AsnWriter source = new AsnWriter(ruleSet);
            AsnWriter dest   = new AsnWriter(AsnEncodingRules.BER);

            Assert.True(source.EncodedValueEquals(dest));

            source.WriteBoolean(false);
            Assert.False(source.EncodedValueEquals(dest));

            source.CopyTo(dest);
            Assert.True(source.EncodedValueEquals(dest));

            Assert.Equal(source.Encode(), dest.Encode());

            source.CopyTo(dest);
            Assert.False(source.EncodedValueEquals(dest));
        }
Example #5
0
        public static void CopyTo_TwoValues(AsnEncodingRules ruleSet)
        {
            AsnWriter source = new AsnWriter(ruleSet);
            AsnWriter dest   = new AsnWriter(AsnEncodingRules.BER);

            source.WriteBoolean(false);
            source.WriteBoolean(true);

            Assert.Throws <InvalidOperationException>(() => source.CopyTo(dest));
        }
Example #6
0
        internal void Encode(AsnWriter writer, Asn1Tag tag)
        {
            writer.PushSequence(tag);

            writer.WriteInteger(Version);
            MessageImprint.Encode(writer);

            if (ReqPolicy != null)
            {
                try
                {
                    writer.WriteObjectIdentifier(ReqPolicy);
                }
                catch (ArgumentException e)
                {
                    throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
                }
            }


            if (Nonce.HasValue)
            {
                writer.WriteInteger(Nonce.Value.Span);
            }


            // DEFAULT value handler for CertReq.
            {
                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
                tmp.WriteBoolean(CertReq);

                if (!tmp.EncodedValueEquals(DefaultCertReq))
                {
                    tmp.CopyTo(writer);
                }
            }


            if (Extensions != null)
            {
                writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
                for (int i = 0; i < Extensions.Length; i++)
                {
                    Extensions[i].Encode(writer);
                }
                writer.PopSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
            }

            writer.PopSequence(tag);
        }
Example #7
0
        public static void CopyTo_IncompatibleRules()
        {
            AsnWriter cer = new AsnWriter(AsnEncodingRules.CER);
            AsnWriter der = new AsnWriter(AsnEncodingRules.DER);

            using (cer.PushSequence())
                using (der.PushSequence())
                {
                    cer.WriteBoolean(false);
                    der.WriteBoolean(true);
                }

            Assert.Throws <InvalidOperationException>(() => cer.CopyTo(der));
            Assert.Throws <InvalidOperationException>(() => der.CopyTo(cer));
        }
Example #8
0
        public void SealWithoutIntegrity()
        {
            if (IsSealed)
            {
                throw new InvalidOperationException(SR.Cryptography_Pkcs12_PfxIsSealed);
            }

            AsnWriter contentsWriter = new AsnWriter(AsnEncodingRules.BER);
            AsnWriter writer         = new AsnWriter(AsnEncodingRules.BER);
            {
                contentsWriter.PushSequence();
                if (_contents != null)
                {
                    foreach (ContentInfoAsn contentInfo in _contents)
                    {
                        contentInfo.Encode(contentsWriter);
                    }
                }
                contentsWriter.PopSequence();

                // https://tools.ietf.org/html/rfc7292#section-4
                //
                // PFX ::= SEQUENCE {
                //   version    INTEGER {v3(3)}(v3,...),
                //   authSafe   ContentInfo,
                //   macData    MacData OPTIONAL
                // }
                writer.PushSequence();

                writer.WriteInteger(3);

                using (writer.PushSequence())
                {
                    writer.WriteObjectIdentifierForCrypto(Oids.Pkcs7Data);

                    using (writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 0)))
                        using (writer.PushOctetString())
                        {
                            contentsWriter.CopyTo(writer);
                        }
                }

                writer.PopSequence();
                _sealedData = writer.Encode();
            }
        }
Example #9
0
        internal void Encode(AsnWriter writer, Asn1Tag tag)
        {
            writer.PushSequence(tag);

            Mac.Encode(writer);
            writer.WriteOctetString(MacSalt.Span);

            // DEFAULT value handler for IterationCount.
            {
                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
                tmp.WriteInteger(IterationCount);

                if (!tmp.EncodedValueEquals(DefaultIterationCount))
                {
                    tmp.CopyTo(writer);
                }
            }

            writer.PopSequence(tag);
        }
Example #10
0
        internal ContentInfoAsn EncodeToContentInfo()
        {
            AsnWriter contentsWriter = Encode();

            if (ConfidentialityMode == Pkcs12ConfidentialityMode.None)
            {
                AsnWriter valueWriter = new AsnWriter(AsnEncodingRules.DER);

                using (valueWriter.PushOctetString())
                {
                    contentsWriter.CopyTo(valueWriter);
                }

                return(new ContentInfoAsn
                {
                    ContentType = Oids.Pkcs7Data,
                    Content = valueWriter.Encode(),
                });
            }

            if (ConfidentialityMode == Pkcs12ConfidentialityMode.Password)
            {
                return(new ContentInfoAsn
                {
                    ContentType = Oids.Pkcs7Encrypted,
                    Content = contentsWriter.Encode(),
                });
            }

            if (ConfidentialityMode == Pkcs12ConfidentialityMode.PublicKey)
            {
                return(new ContentInfoAsn
                {
                    ContentType = Oids.Pkcs7Enveloped,
                    Content = contentsWriter.Encode(),
                });
            }

            Debug.Fail($"No handler for {ConfidentialityMode}");
            throw new CryptographicException();
        }
        internal void Encode(AsnWriter writer, Asn1Tag tag)
        {
            writer.PushSequence(tag);


            // DEFAULT value handler for CA.
            {
                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
                tmp.WriteBoolean(CA);

                if (!tmp.EncodedValueEquals(DefaultCA))
                {
                    tmp.CopyTo(writer);
                }
            }


            if (PathLengthConstraint.HasValue)
            {
                writer.WriteInteger(PathLengthConstraint.Value);
            }

            writer.PopSequence(tag);
        }
Example #12
0
        internal void Encode(AsnWriter writer, Asn1Tag tag)
        {
            writer.PushSequence(tag);


            // DEFAULT value handler for Version.
            {
                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
                tmp.WriteInteger(Version);

                if (!tmp.EncodedValueEquals(DefaultVersion))
                {
                    writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
                    tmp.CopyTo(writer);
                    writer.PopSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
                }
            }

            writer.WriteInteger(SerialNumber.Span);
            SignatureAlgorithm.Encode(writer);
            // Validator for tag constraint for Issuer
            {
                if (!Asn1Tag.TryDecode(Issuer.Span, out Asn1Tag validateTag, out _) ||
                    !validateTag.HasSameClassAndValue(new Asn1Tag((UniversalTagNumber)16)))
                {
                    throw new CryptographicException();
                }
            }

            try
            {
                writer.WriteEncodedValue(Issuer.Span);
            }
            catch (ArgumentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }
            Validity.Encode(writer);
            // Validator for tag constraint for Subject
            {
                if (!Asn1Tag.TryDecode(Subject.Span, out Asn1Tag validateTag, out _) ||
                    !validateTag.HasSameClassAndValue(new Asn1Tag((UniversalTagNumber)16)))
                {
                    throw new CryptographicException();
                }
            }

            try
            {
                writer.WriteEncodedValue(Subject.Span);
            }
            catch (ArgumentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }
            SubjectPublicKeyInfo.Encode(writer);

            if (IssuerUniqueId.HasValue)
            {
                writer.WriteBitString(IssuerUniqueId.Value.Span, 0, new Asn1Tag(TagClass.ContextSpecific, 1));
            }


            if (SubjectUniqueId.HasValue)
            {
                writer.WriteBitString(SubjectUniqueId.Value.Span, 0, new Asn1Tag(TagClass.ContextSpecific, 2));
            }


            if (Extensions != null)
            {
                writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 3));

                writer.PushSequence();
                for (int i = 0; i < Extensions.Length; i++)
                {
                    Extensions[i].Encode(writer);
                }
                writer.PopSequence();

                writer.PopSequence(new Asn1Tag(TagClass.ContextSpecific, 3));
            }

            writer.PopSequence(tag);
        }
        internal void Encode(AsnWriter writer, Asn1Tag tag)
        {
            writer.PushSequence(tag);

            writer.WriteInteger(Version);
            try
            {
                writer.WriteObjectIdentifier(Policy);
            }
            catch (ArgumentException e)
            {
                throw new CryptographicException(SR.Cryptography_Der_Invalid_Encoding, e);
            }
            MessageImprint.Encode(writer);
            writer.WriteInteger(SerialNumber.Span);
            writer.WriteGeneralizedTime(GenTime, false);

            if (Accuracy.HasValue)
            {
                Accuracy.Value.Encode(writer);
            }


            // DEFAULT value handler for Ordering.
            {
                AsnWriter tmp = new AsnWriter(AsnEncodingRules.DER);
                tmp.WriteBoolean(Ordering);

                if (!tmp.EncodedValueEquals(DefaultOrdering))
                {
                    tmp.CopyTo(writer);
                }
            }


            if (Nonce.HasValue)
            {
                writer.WriteInteger(Nonce.Value.Span);
            }


            if (Tsa.HasValue)
            {
                writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
                Tsa.Value.Encode(writer);
                writer.PopSequence(new Asn1Tag(TagClass.ContextSpecific, 0));
            }


            if (Extensions != null)
            {
                writer.PushSequence(new Asn1Tag(TagClass.ContextSpecific, 1));
                for (int i = 0; i < Extensions.Length; i++)
                {
                    Extensions[i].Encode(writer);
                }
                writer.PopSequence(new Asn1Tag(TagClass.ContextSpecific, 1));
            }

            writer.PopSequence(tag);
        }