| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- //
- // CmsSignerTest.cs - NUnit tests for CmsSigner
- //
- // Author:
- // Sebastien Pouliot <[email protected]>
- //
- // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
- // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using NUnit.Framework;
- using System;
- using System.Collections;
- using System.Security.Cryptography;
- using System.Security.Cryptography.Pkcs;
- using System.Security.Cryptography.X509Certificates;
- namespace MonoTests.System.Security.Cryptography.Pkcs {
- [TestFixture]
- public class CmsSignerTest : Assertion {
- static byte[] asnNull = { 0x05, 0x00 };
- static string sha1Oid = "1.3.14.3.2.26";
- static string sha1Name = "sha1";
- static string rsaOid = "1.2.840.113549.1.1.1";
- static string rsaName = "RSA";
- [Test]
- public void ConstructorEmpty ()
- {
- CmsSigner ps = new CmsSigner ();
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- public void ConstructorIssuerAndSerialNumber ()
- {
- CmsSigner ps = new CmsSigner (SubjectIdentifierType.IssuerAndSerialNumber);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- public void ConstructorSubjectKeyIdentifier ()
- {
- CmsSigner ps = new CmsSigner (SubjectIdentifierType.SubjectKeyIdentifier);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.SubjectKeyIdentifier, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- public void ConstructorUnknown ()
- {
- CmsSigner ps = new CmsSigner (SubjectIdentifierType.Unknown);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- // Unknown is converted to IssuerAndSerialNumber
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- // TODO: return valid x509 certifiate with private key
- private X509Certificate2 GetValidCertificateWithPrivateKey ()
- {
- X509Certificate2 x509 = new X509Certificate2 ();
- return x509;
- }
- [Test]
- public void ConstructorX509CertificateEx ()
- {
- X509Certificate2 x509 = GetValidCertificateWithPrivateKey ();
- CmsSigner ps = new CmsSigner (x509);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNotNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- public void ConstructorX509CertificateExEmpty ()
- {
- X509Certificate2 x509 = new X509Certificate2 (); // empty
- CmsSigner ps = new CmsSigner (x509);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNotNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- //BUG [ExpectedException (typeof (ArgumentNullException))]
- public void ConstructorX509CertificateExNull ()
- {
- X509Certificate2 x509 = null;
- CmsSigner ps = new CmsSigner (x509);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- public void ConstructorIssuerAndSerialNumberX509CertificateEx ()
- {
- X509Certificate2 x509 = GetValidCertificateWithPrivateKey ();
- CmsSigner ps = new CmsSigner (SubjectIdentifierType.IssuerAndSerialNumber, x509);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNotNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- public void ConstructorSubjectKeyIdentifierX509CertificateEx ()
- {
- X509Certificate2 x509 = GetValidCertificateWithPrivateKey ();
- CmsSigner ps = new CmsSigner (SubjectIdentifierType.SubjectKeyIdentifier, x509);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNotNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.SubjectKeyIdentifier, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- public void ConstructorUnknownX509CertificateEx ()
- {
- X509Certificate2 x509 = GetValidCertificateWithPrivateKey ();
- CmsSigner ps = new CmsSigner (SubjectIdentifierType.Unknown, x509);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNotNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- // Unknown is converted to IssuerAndSerialNumber
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- //BUG [ExpectedException (typeof (ArgumentNullException))]
- public void ConstructorIssuerAndSerialNumberX509CertificateExNull ()
- {
- CmsSigner ps = new CmsSigner (SubjectIdentifierType.IssuerAndSerialNumber, null);
- // default properties
- AssertEquals ("SignedAttributes", 0, ps.SignedAttributes.Count);
- AssertNull ("Certificate", ps.Certificate);
- AssertEquals ("DigestAlgorithm.FriendlyName", sha1Name, ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", sha1Oid, ps.DigestAlgorithm.Value);
- AssertEquals ("IncludeOption", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- AssertEquals ("SignerIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- AssertEquals ("UnsignedAttributes", 0, ps.UnsignedAttributes.Count);
- }
- [Test]
- public void SignedAttributes ()
- {
- CmsSigner ps = new CmsSigner ();
- AssertEquals ("SignedAttributes=0", 0, ps.SignedAttributes.Count);
- ps.SignedAttributes.Add (new Pkcs9DocumentDescription ("mono"));
- AssertEquals ("SignedAttributes=1", 1, ps.SignedAttributes.Count);
- }
- [Test]
- public void Certificate ()
- {
- CmsSigner ps = new CmsSigner ();
- AssertNull ("Certificate=default(null)", ps.Certificate);
- ps.Certificate = GetValidCertificateWithPrivateKey ();
- AssertNotNull ("Certificate!=null", ps.Certificate);
- ps.Certificate = null;
- AssertNull ("Certificate=null", ps.Certificate);
- }
- [Test]
- public void Digest ()
- {
- CmsSigner ps = new CmsSigner ();
- ps.DigestAlgorithm = new Oid ("1.2.840.113549.2.5");
- AssertEquals ("DigestAlgorithm.FriendlyName", "md5", ps.DigestAlgorithm.FriendlyName);
- AssertEquals ("DigestAlgorithm.Value", "1.2.840.113549.2.5", ps.DigestAlgorithm.Value);
- ps.DigestAlgorithm = null;
- AssertNull ("DigestAlgorithm=null", ps.DigestAlgorithm);
- }
- [Test]
- public void IncludeOption ()
- {
- CmsSigner ps = new CmsSigner ();
- ps.IncludeOption = X509IncludeOption.EndCertOnly;
- AssertEquals ("EndCertOnly", X509IncludeOption.EndCertOnly, ps.IncludeOption);
- ps.IncludeOption = X509IncludeOption.ExcludeRoot;
- AssertEquals ("ExcludeRoot", X509IncludeOption.ExcludeRoot, ps.IncludeOption);
- ps.IncludeOption = X509IncludeOption.None;
- AssertEquals ("None", X509IncludeOption.None, ps.IncludeOption);
- ps.IncludeOption = X509IncludeOption.WholeChain;
- AssertEquals ("WholeChain", X509IncludeOption.WholeChain, ps.IncludeOption);
- }
- [Test]
- public void SubjectIdentifierTypeProperty ()
- {
- CmsSigner ps = new CmsSigner ();
- ps.SignerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber;
- AssertEquals ("IssuerAndSerialNumber", SubjectIdentifierType.IssuerAndSerialNumber, ps.SignerIdentifierType);
- ps.SignerIdentifierType = SubjectIdentifierType.SubjectKeyIdentifier;
- AssertEquals ("SubjectKeyIdentifier", SubjectIdentifierType.SubjectKeyIdentifier, ps.SignerIdentifierType);
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void SubjectIdentifierTypeUnknown ()
- {
- CmsSigner ps = new CmsSigner ();
- ps.SignerIdentifierType = SubjectIdentifierType.Unknown;
- }
- [Test]
- public void UnauthenticatedAttributes ()
- {
- CmsSigner ps = new CmsSigner ();
- AssertEquals ("UnsignedAttributes=0", 0, ps.UnsignedAttributes.Count);
- ps.UnsignedAttributes.Add (new Pkcs9DocumentDescription ("mono"));
- AssertEquals ("UnsignedAttributes=1", 1, ps.UnsignedAttributes.Count);
- }
- }
- }
- #endif
|