Example #1
0
        public static void OidHasNoNamespaceRequirement()
        {
            Pkcs12SafeBag safeBag = new TestSafeBag(Oids.Aes192);

            byte[] encoded = safeBag.Encode();
            Assert.NotNull(encoded);
        }
Example #2
0
        public static void GetBagIdIsFactory()
        {
            Pkcs12SafeBag safeBag    = new TestSafeBag(Oids.Aes192);
            Oid           firstCall  = safeBag.GetBagId();
            Oid           secondCall = safeBag.GetBagId();

            Assert.Equal(Oids.Aes192, firstCall.Value);
            Assert.Equal(firstCall.Value, secondCall.Value);
#if !NETCOREAPP
            Assert.NotSame(firstCall, secondCall);
#endif
        }
Example #3
0
        public static void AttributesIsMutable()
        {
            Pkcs12SafeBag safeBag = new TestSafeBag(Oids.Aes192);
            CryptographicAttributeObjectCollection firstCall = safeBag.Attributes;

            Assert.Same(firstCall, safeBag.Attributes);

            Assert.Equal(0, firstCall.Count);
            firstCall.Add(new Pkcs9DocumentDescription("Description"));

            Assert.Equal(1, safeBag.Attributes.Count);
            Assert.Same(firstCall, safeBag.Attributes);
        }
Example #4
0
        public static void GetBagIdIsFactory()
        {
            Pkcs12SafeBag safeBag    = new TestSafeBag(Oids.Aes192);
            Oid           firstCall  = safeBag.GetBagId();
            Oid           secondCall = safeBag.GetBagId();

            Assert.NotSame(firstCall, secondCall);
            Assert.Equal(Oids.Aes192, firstCall.Value);
            Assert.Equal(firstCall.Value, secondCall.Value);

            secondCall.Value = Oids.Cms3DesWrap;
            Assert.NotEqual(firstCall.Value, secondCall.Value);
            Assert.Equal(Oids.Aes192, firstCall.Value);
        }
Example #5
0
        public static void BaseClassHonorsSkipCopy(bool skipCopy)
        {
            byte[]        test   = { 0x05, 0x00 };
            Pkcs12SafeBag bag    = new TestSafeBag(Oids.ContentType, test, skipCopy);
            bool          isSame = test.AsSpan().Overlaps(bag.EncodedBagValue.Span);

            if (skipCopy)
            {
                Assert.True(isSame, "Is same memory");
            }
            else
            {
                Assert.False(isSame, "Is same memory");
            }
        }
Example #6
0
        public static void BaseClassVerifiesSingleBer(string inputHex, bool expectSuccess)
        {
            byte[]             inputBytes = inputHex.HexToByteArray();
            Func <TestSafeBag> func       = () => new TestSafeBag(Oids.BasicConstraints2, inputBytes);

            if (expectSuccess)
            {
                TestSafeBag bag = func();
                Assert.True(bag.EncodedBagValue.Span.SequenceEqual(inputBytes));
            }
            else
            {
                Assert.ThrowsAny <CryptographicException>(func);
            }
        }
Example #7
0
        public static void TryEncodeBoundary()
        {
            TestSafeBag safeBag = new TestSafeBag(Oids.ContentType);

            byte[] encoded = safeBag.Encode();

            byte[] buf = new byte[encoded.Length + 4];
            buf.AsSpan().Fill(0xCA);

            Assert.False(safeBag.TryEncode(buf.AsSpan(0, encoded.Length - 1), out int bytesWritten));
            Assert.Equal(0, bytesWritten);
            Assert.True(buf.All(b => b == 0xCA));

            Assert.True(safeBag.TryEncode(buf.AsSpan(1), out bytesWritten));
            Assert.Equal(encoded.Length, bytesWritten);
            Assert.Equal(0xCA, buf[0]);
            Assert.Equal(0xCA, buf[bytesWritten + 1]);
            Assert.True(encoded.AsSpan().SequenceEqual(buf.AsSpan(1, bytesWritten)));

            buf.AsSpan().Fill(0xCA);
            Assert.True(safeBag.TryEncode(buf.AsSpan(2, bytesWritten), out bytesWritten));
            Assert.Equal(encoded.Length, bytesWritten);
            Assert.True(encoded.AsSpan().SequenceEqual(buf.AsSpan(2, bytesWritten)));
        }
Example #8
0
        public static void OidValidatedLate()
        {
            Pkcs12SafeBag safeBag = new TestSafeBag("potato");

            Assert.ThrowsAny <CryptographicException>(() => safeBag.Encode());
        }