Giter VIP home page Giter VIP logo

softhsmv2fortesting's Introduction

SoftHSMv2 (as nuget) for testing

NuGet Status

This project pack SoftHSMv2 v 2.5.0 as nuget package along with minimal code for initialize and destroy SoftHSMv2.

It is designed for testing .Net projects, using PKCS#11 devices (e.g. smart cards, HSM, tokens,...), in CI/CD enviroment.

Getting started

Package manager

Install-Package SoftHSMv2ForTesting

dotnet cli

dotnet add package SoftHSMv2ForTesting

Usage

Usage with MS Test and PKCS#11 Interop.

    [TestClass]
    public class AssemblyInitializedTest
    {
        public const string TokenName = "TestCardToken";
        public const string TokenSoPin = "abcdef";
        public const string TokenUserPin = "abc123*!~";

        private static SoftHsmContext softHsmContext = null;

        public static string Pkcs11LibPath
        {
            get => softHsmContext.Pkcs11LibPath;
        }


        [AssemblyInitialize]
        public static void AssemblyInitialize(TestContext context)
        {
            softHsmContext = SoftHsmInitializer.Init(opt =>
            {
#if NETCOREAPP
                opt.DeployFolder = Path.Combine(Path.GetTempPath(), $"SoftHSMv2-{Guid.NewGuid():D}");
#else
                opt.DeployFolder = Path.Combine(context.DeploymentDirectory, "SoftHSMv2");
#endif

                opt.LabelName = TokenName;
                opt.Pin = TokenUserPin;
                opt.SoPin = TokenSoPin;
            });

            // Add another Token
            // softHsmContext.AddToken("SecondToken", "abc456", "hello");
        }

        [AssemblyCleanup]
        public static void AssemblyCleanup()
        {
            softHsmContext?.Dispose();
        }
    }

    [TestClass]
    public class ExampleTests
    {
        public TestContext TestContext
        {
            get;
            set;
        }

        [TestMethod]
        public void GetTokenSerial()
        {
            Pkcs11InteropFactories interopFactories = new Pkcs11InteropFactories();

            using IPkcs11Library pkcs11 = interopFactories.Pkcs11LibraryFactory.LoadPkcs11Library(interopFactories, AssemblyInitializedTest.Pkcs11LibPath, AppType.MultiThreaded);
            ISlot slot = pkcs11.GetSlotList(SlotsType.WithTokenPresent)
                 .Single(t => t.GetTokenInfo().Label == AssemblyInitializedTest.TokenName);

            using ISession session = slot.OpenSession(SessionType.ReadOnly);
            session.Login(CKU.CKU_USER, AssemblyInitializedTest.TokenUserPin);

            string serialNumber = slot.GetTokenInfo().SerialNumber;
            this.TestContext.WriteLine("Token has serial: {0}", serialNumber);
        }
    }

Supported frmaeworks and platforms

Framewroks:

  • .Netstandrad 2.0
  • .Net Framewrok 4.0

Platforms:

  • Windows x86
  • Windows x64

Contributing

Pull requests, issues and commentary welcome!

Forking the repo is probably the easiest way to get started. There is a nice list of issues, both bugs and features that is up for grabs. Or devise a feature of your own.

Third Party Licenses

  1. SoftHSMv2
  2. SoftHSM2 installer for MS Windows

softhsmv2fortesting's People

Contributors

harrison314 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

softhsmv2fortesting's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.