public void _02_LibraryInfoMatches() { Helpers.CheckPlatform(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); CK_INFO libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // Empty attribute pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes(" "); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1;foo=bar"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // All attributes matching pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryManufacturer nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryDescription nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryVersion nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x00 }, Minor = new byte[] { 0x01 } }; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); }
public void _03_SlotInfoMatches() { Helpers.CheckPlatform(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); CK_SLOT_INFO slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); NativeULong slotId = 1; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Empty attribute pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes(" "); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 1; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1;foo=bar"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 1; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // All attributes matching pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 1; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Manufacturer nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 1; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Description nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("foo"); slotId = 1; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Slot id nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 2; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); }
public void _03_SlotInfoMatches() { if (Platform.UnmanagedLongSize != 8 || Platform.StructPackingSize != 0) { Assert.Inconclusive("Test cannot be executed on this platform"); } using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.UseOsLocking)) { List <Slot> slots = pkcs11.GetSlotList(true); Assert.IsTrue(slots != null && slots.Count > 0); SlotInfo slotInfo = slots[0].GetSlotInfo(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:vendor=foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // All attributes matching Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.SlotManufacturer = slotInfo.ManufacturerId; pkcs11UriBuilder.SlotDescription = slotInfo.SlotDescription; pkcs11UriBuilder.SlotId = slotInfo.SlotId; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // Manufacturer nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.SlotManufacturer = "foobar"; pkcs11UriBuilder.SlotDescription = slotInfo.SlotDescription; pkcs11UriBuilder.SlotId = slotInfo.SlotId; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // Description nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.SlotManufacturer = slotInfo.ManufacturerId; pkcs11UriBuilder.SlotDescription = "foobar"; pkcs11UriBuilder.SlotId = slotInfo.SlotId; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // Slot id nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.SlotManufacturer = slotInfo.ManufacturerId; pkcs11UriBuilder.SlotDescription = slotInfo.SlotDescription; pkcs11UriBuilder.SlotId = slotInfo.SlotId + 1; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); } }
public void _02_LibraryInfoMatches() { if (Platform.UnmanagedLongSize != 8 || Platform.StructPackingSize != 0) { Assert.Inconclusive("Test cannot be executed on this platform"); } using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.UseOsLocking)) { LibraryInfo libraryInfo = pkcs11.GetInfo(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:vendor=foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // All attributes matching Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryManufacturer nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = "foobar"; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryDescription nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = "foobar"; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryVersion nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = "0"; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); } }
public void _03_SlotInfoMatches() { Helpers.CheckPlatform(); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType)) { List <Slot> slots = pkcs11.GetSlotList(SlotsType.WithTokenPresent); Assert.IsTrue(slots != null && slots.Count > 0); SlotInfo slotInfo = slots[0].GetSlotInfo(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:vendor=foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // All attributes matching Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.SlotManufacturer = slotInfo.ManufacturerId; pkcs11UriBuilder.SlotDescription = slotInfo.SlotDescription; pkcs11UriBuilder.SlotId = slotInfo.SlotId; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // Manufacturer nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.SlotManufacturer = "foobar"; pkcs11UriBuilder.SlotDescription = slotInfo.SlotDescription; pkcs11UriBuilder.SlotId = slotInfo.SlotId; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // Description nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.SlotManufacturer = slotInfo.ManufacturerId; pkcs11UriBuilder.SlotDescription = "foobar"; pkcs11UriBuilder.SlotId = slotInfo.SlotId; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); // Slot id nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.SlotManufacturer = slotInfo.ManufacturerId; pkcs11UriBuilder.SlotDescription = slotInfo.SlotDescription; pkcs11UriBuilder.SlotId = slotInfo.SlotId + 1; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo)); } }
public void _02_LibraryInfoMatches() { Helpers.CheckPlatform(); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType)) { LibraryInfo libraryInfo = pkcs11.GetInfo(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:vendor=foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // All attributes matching Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryManufacturer nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = "foobar"; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryDescription nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = "foobar"; pkcs11UriBuilder.LibraryVersion = libraryInfo.LibraryVersion; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryVersion nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.LibraryManufacturer = libraryInfo.ManufacturerId; pkcs11UriBuilder.LibraryDescription = libraryInfo.LibraryDescription; pkcs11UriBuilder.LibraryVersion = "0"; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); } }
public void _05_ObjectAttributesMatches() { if (Platform.UnmanagedLongSize != 8 || Platform.StructPackingSize != 0) { Assert.Inconclusive("Test cannot be executed on this platform"); } // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); List <ObjectAttribute> objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Empty attribute pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=;id=%01%02%03"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, string.Empty)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03;foo=bar"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // All attributes matching pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Type nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Object nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "foo bar")); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Id nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x04, 0x05, 0x06 })); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); try { // Type present in URI but missing in list pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11UriException); } try { // Object present in URI but missing in list pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11UriException); } try { // Id present in URI but missing in list pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <ObjectAttribute>(); objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] { 0x04, 0x05, 0x06 })); Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11UriException); } }
public void _04_TokenInfoMatches() { if (Platform.UnmanagedLongSize != 8 || Platform.StructPackingSize != 0) { Assert.Inconclusive("Test cannot be executed on this platform"); } using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.UseOsLocking)) { List <Slot> slots = pkcs11.GetSlotList(true); Assert.IsTrue(slots != null && slots.Count > 0); TokenInfo tokenInfo = slots[0].GetTokenInfo(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:vendor=foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // All attributes matching Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Token nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = "foobar"; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Manufacturer nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = "foobar"; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Serial nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = "foobar"; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Model nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = "foobar"; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); } }
public void _04_TokenInfoMatches() { Helpers.CheckPlatform(); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType)) { List <Slot> slots = pkcs11.GetSlotList(SlotsType.WithTokenPresent); Assert.IsTrue(slots != null && slots.Count > 0); TokenInfo tokenInfo = slots[0].GetTokenInfo(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:vendor=foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // All attributes matching Pkcs11UriBuilder pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Token nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = "foobar"; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Manufacturer nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = "foobar"; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Serial nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = "foobar"; pkcs11UriBuilder.Model = tokenInfo.Model; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Model nonmatching pkcs11UriBuilder = new Pkcs11UriBuilder(); pkcs11UriBuilder.Token = tokenInfo.Label; pkcs11UriBuilder.Manufacturer = tokenInfo.ManufacturerId; pkcs11UriBuilder.Serial = tokenInfo.SerialNumber; pkcs11UriBuilder.Model = "foobar"; pkcs11uri = pkcs11UriBuilder.ToPkcs11Uri(); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); } }
public void _05_ObjectAttributesMatches() { Helpers.CheckPlatform(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); List <CK_ATTRIBUTE> objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Empty attribute pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=;id=%01%02%03"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_LABEL, string.Empty)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03;foo=bar"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // All attributes matching pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Type nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Object nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_LABEL, "foo bar")); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); // Id nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x04, 0x05, 0x06 })); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes)); try { // Type present in URI but missing in list pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_LABEL, "foobar")); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11UriException); } try { // Object present in URI but missing in list pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x01, 0x02, 0x03 })); Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11UriException); } try { // Id present in URI but missing in list pkcs11uri = new Pkcs11Uri(@"pkcs11:type=private;object=foobar;id=%01%02%03"); objectAttributes = new List <CK_ATTRIBUTE>(); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_CLASS, CKO.CKO_PUBLIC_KEY)); objectAttributes.Add(CkaUtils.CreateAttribute(CKA.CKA_ID, new byte[] { 0x04, 0x05, 0x06 })); Pkcs11UriUtils.Matches(pkcs11uri, objectAttributes); Assert.Fail("Exception expected but not thrown"); } catch (Exception ex) { Assert.IsTrue(ex is Pkcs11UriException); } }
public void _04_TokenInfoMatches() { Helpers.CheckPlatform(); // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); CK_TOKEN_INFO tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Empty attribute pkcs11uri = new Pkcs11Uri(@"pkcs11:token=;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes(" "); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar;foo=bar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // All attributes matching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Label nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // ManufacturerId nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // SerialNumber nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("012"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Model nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foo bar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); }
public void _02_LibraryInfoMatches() { if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 0) { Assert.Inconclusive("Test cannot be executed on this platform"); } // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); CK_INFO libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // Empty attribute pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes(" "); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1;foo=bar"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // All attributes matching pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryManufacturer nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryDescription nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x01 }, Minor = new byte[] { 0x00 } }; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); // LibraryVersion nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:library-manufacturer=foo;library-description=bar;library-version=1"); libraryInfo = new CK_INFO(); libraryInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); libraryInfo.LibraryDescription = ConvertUtils.Utf8StringToBytes("bar"); libraryInfo.LibraryVersion = new CK_VERSION() { Major = new byte[] { 0x00 }, Minor = new byte[] { 0x01 } }; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, libraryInfo)); }
public void _04_TokenInfoMatches() { if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 0) { Assert.Inconclusive("Test cannot be executed on this platform"); } // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); CK_TOKEN_INFO tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Empty attribute pkcs11uri = new Pkcs11Uri(@"pkcs11:token=;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes(" "); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar;foo=bar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // All attributes matching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Label nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // ManufacturerId nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // SerialNumber nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("012"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foobar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); // Model nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:token=foo;manufacturer=bar;serial=123;model=foobar"); tokenInfo = new CK_TOKEN_INFO(); tokenInfo.Label = ConvertUtils.Utf8StringToBytes("foo"); tokenInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); tokenInfo.SerialNumber = ConvertUtils.Utf8StringToBytes("123"); tokenInfo.Model = ConvertUtils.Utf8StringToBytes("foo bar"); Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, tokenInfo)); }
public void _03_SlotInfoMatches() { if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 0) { Assert.Inconclusive("Test cannot be executed on this platform"); } // Empty URI Pkcs11Uri pkcs11uri = new Pkcs11Uri(@"pkcs11:"); CK_SLOT_INFO slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); uint slotId = 1; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Empty attribute pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes(" "); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 1; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Unknown path attribute in URI pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1;foo=bar"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 1; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // All attributes matching pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 1; Assert.IsTrue(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Manufacturer nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("bar"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 1; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Description nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("foo"); slotId = 1; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); // Slot id nonmatching pkcs11uri = new Pkcs11Uri(@"pkcs11:slot-manufacturer=foo;slot-description=bar;slot-id=1"); slotInfo = new CK_SLOT_INFO(); slotInfo.ManufacturerId = ConvertUtils.Utf8StringToBytes("foo"); slotInfo.SlotDescription = ConvertUtils.Utf8StringToBytes("bar"); slotId = 2; Assert.IsFalse(Pkcs11UriUtils.Matches(pkcs11uri, slotInfo, slotId)); }