Coder Social home page Coder Social logo

pkcs11mod's Introduction

pkcs11mod: Go library for creating PKCS#11 modules

pkcs11mod allows you to create PKCS#11 modules in Go. You implement your PKCS#11 functions by providing a struct that implements the same API as pkcs11.Ctx from Miek Gieben's pkcs11 package; pkcs11mod takes care of exposing this as a C ABI library.

Building

Prerequisites:

  1. Ensure you have the Go tools installed.

Option A: Using Go build commands without Go modules (works on any platform with Bash; will not work on Go 1.17+):

  1. Ensure you have the GOPATH environment variable set. (For those not familar with Go, setting it to the path to an empty directory will suffice. The directory will be filled with build files.)

  2. Run export GO111MODULE=off to disable Go modules.

  3. Run go get -d -t -u github.com/namecoin/pkcs11mod. The pkcs11mod source code will be retrieved automatically.

  4. Run go generate github.com/namecoin/pkcs11mod. Some source code will be generated.

  5. Run go get -t -u github.com/namecoin/pkcs11mod. pkcs11mod will build.

  6. You can now import "github.com/namecoin/pkcs11mod" from your Go PKCS#11 module.

Option B: Using Go build commands with Go modules (works on any platform with Bash):

  1. Run the following in the pkcs11mod directory to set up Go modules:

    go mod init github.com/namecoin/pkcs11mod
    go mod tidy
    go generate ./...
    go mod tidy
    
  2. Place your Go PKCS#11 module's directory as a sibling of the pkcs11mod directory.

  3. Run the following in your Go PKCS#11 module's directory:

    go mod edit -replace github.com/namecoin/pkcs11mod=../pkcs11mod
    go mod tidy
    
  4. You can now import "github.com/namecoin/pkcs11mod" from your Go PKCS#11 module.

Example usage

See the pkcs11proxy subdirectory for an example of how to use pkcs11mod. Also consider using the higher-level p11mod library instead of using pkcs11mod directly (see this section).

Tracing

Set the environment variable PKCS11MOD_TRACE=1 to enable debug tracing. To include sensitive data that might be a privacy leak, also set PKCS11MOD_TRACE_SENSITIVE=1. The trace will be outputted to the log file.

What's PKCS#11?

PKCS#11 is a plugin specification frequently used with smartcards and certificate databases. You may find the following links informative:

What's the difference between pkcs11 and pkcs11mod?

Miek Gieben's pkcs11 and p11 packages are for implementing applications that open PKCS#11 modules (e.g. you'd use pkcs11 or p11 if you're creating a web browser that will open a certificate database); pkcs11mod and p11mod are for implementing PKCS#11 modules that are opened by an application (e.g. you'd use pkcs11mod or p11mod if you're creating a certificate database that will be opened by a web browser).

Should I use pkcs11mod or p11mod?

p11mod is much easier to use and more idiomatic to Go. However, p11mod implements less of the PKCS#11 specification than pkcs11mod. If you only need functionality that p11mod has, you will probably find p11mod more pleasant to work with. On the other hand, p11mod is much newer and less battle-tested, so you may find pkcs11mod more reliable.

Development focus/status

pkcs11mod is primarily motivated by the use cases that Namecoin has; as such, the PKCS#11 features we've implemented so far are mostly the features used by applications such as NSS's certificate verifier and PKCS#11 modules such as NSS's CKBI (built-in certificates). We don't have any objection to implementing the rest of the PKCS#11 spec (and we'd happily accept pull requests to this end), but it's unlikely that we'll spend much of our free time on features that aren't relevant to Namecoin.

While we do plan to use pkcs11mod in production in the future, it is not yet used in production, and any horrifying bugs in pkcs11mod probably haven't been noticed by us yet.

Credits / License

Copyright (C) 2018-2022 Namecoin Developers

pkcs11mod is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

pkcs11mod is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with pkcs11mod; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

pkcs11mod is based on:

pkcs11mod's People

Contributors

aerth avatar bernard-wagner avatar jeremyrand avatar robertmin1 avatar yorubad-dev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pkcs11mod's Issues

Rename Cirrus tasks with prefixes

Currently, naming of Cirrus tasks doesn't follow a clear convention, which makes sorting difficult. We should rename them with common prefixes like Lint, Test, and Compile.

C_GetFunctionList not exported on Windows

According to Dependencies, pkcs11proxy.dll is not exporting C_GetFunctionList on Windows. This is probably why I can't get the module to load in Firefox and certutil on Windows. Probably this is due to the changes introduced by golang/go#30674 , which makes sense given that this worked a few years ago.

Test multiple modules

We should test what happens if two PKCS#11 modules that are both created via pkcs11mod are loaded simultaneously by the same application. (It is conceivable that their Go runtimes might conflict.)

Add pkcs11-tool tests

pkcs11-tool is the OpenSC equivalent of NSS's certutil and GnuTLS's p11tool. We should add tests for it, by having it open modules via pkcs11proxy and p11proxy and making sure the behavior doesn't change.

Add Chromium tests

We should be able to use the screenshot trick like we did for Firefox.

Missing license file

The license is specified in the readme, but not in a dedicated file, which makes it hard for bots to determine the license. We should fix that.

Add gccgo builds

We should add gccgo builds. AFAIK gccgo handles cgo somewhat differently from Google's compiler, so this might require refactoring the build scripts.

Add Base Browser tests

Base Browser is a browser maintained by the Tor Browser Team; it's basically Tor Browser without Tor. Similar in concept to Kicksecure's SecBrowser. We should add tests for Base Browser.

pkcs11proxy triggers a Go panic if the target module isn't found

If pkcs11proxy is built with a target module path that doesn't exist, it will cause a Go panic:

pkcs11proxy changed line:

backend := pkcs11.New("/usr/lib64/pkcs11/p11-kit-trustX.so") // This path does not exist

Result:

$ /usr/lib64/nss/unsupported-tools/tstclnt -R ./libmypkcs11module.so  -D -h www.namecoin.org

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7fffbc461d68]

goroutine 17 [running, locked to thread]:
github.com/miekg/pkcs11.(*Ctx).Initialize.func1(0x0, 0x7fffbc3c191c)
	/home/user/go/pkg/mod/github.com/miekg/[email protected]/pkcs11.go:805 +0x38
github.com/miekg/pkcs11.(*Ctx).Initialize(0x0, 0x1, 0x7fffbc540001)
	/home/user/go/pkg/mod/github.com/miekg/[email protected]/pkcs11.go:805 +0x40
github.com/namecoin/pkcs11mod.Go_Initialize(...)
	/home/user/Downloads/pkcs11mod/pkcs11mod.go:68
Aborted (core dumped)

Obviously a nil backend isn't expected to do anything useful, but pkcs11mod should handle this more gracefully than panicking; the application may be able to cope with an erroring PKCS#11 module as long as the module doesn't panic.

Relicense to LGPLv2+?

@aerth @bernard-wagner are you okay with relicensing pkcs11mod (and ncp11) from LGPLv3+ to LGPLv2+? This would allow pkcs11mod to be used with applications licensed under GPLv2. While I am personally very skeptical that loading a PKCS#11 module into an application is legally capable of creating a derivative work (I think PKCS#11 is an interface, so a copyright license like GPL cannot jump across it), there is not much (if any?) case law on this, and the legal uncertainty is not really desirable for us. Also it's not great that pkcs11mod is more compatible with non-free software than with GPLv2 software.

Test more Chromium variants

Remove log file paths

The log file paths in init() should really be part of the backend implementation (e.g. ncp11), not pkcs11mod.

Add p11tool tests

p11tool is the GnuTLS equivalent of NSS's certutil. We should add tests for it, by having it open modules via pkcs11proxy and p11proxy and making sure the behavior doesn't change.

Add certutil+CKBI tests

We should add some tests that use certutil to dump certs and trust objects from CKBI. Probably the tlsrestrictnss code is a good guide for what commands to run.

Test p11proxy with Chromium

We currently test Chromium with pkcs11proxy, but not p11proxy. We should add tests for p11proxy with Chromium.

Add wget tests

wget appears to use GnuTLS, so we should be able to test it easily.

Add GNOME Web tests

GNOME Web uses GnuTLS, so we should be able to test it. Not sure if headless operation will be a problem though.

CloseAllSessions is unimplemented

Currently, CloseAllSessions is unimplemented. NSS's modutil and certutil seem to call it, so it's somewhat relevant to Namecoin, although nothing obviously bad seems to happen when it's unimplemented.

Support tracing in logs

It would be helpful for debugging purposes if pkcs11mod and p11mod could log traces of which functions are being called, with what arguments. This could be enabled via an environment variable. Such functionality is a privacy risk (e.g. it could be used to investigate which .bit domains were accessed in ncp11), so it should come with a warning.

Add Softoken tests

Softoken is the NSS mutable (sqlite-based) certificate database PKCS#11 module. We should add tests for it, by putting it behind pkcs11proxy and p11proxy and making sure the behavior doesn't change.

Implement missing p11mod functions

A lot of functions that exist in pkcs11mod are unimplemented in p11mod. The easy way to find them is by grepping p11mod.go for CKR_FUNCTION_NOT_SUPPORTED. It would be desirable to implement them.

Note that some of those functions may be impossible to implement due to limitations of the p11 API. There are plenty that will work fine though, so if you see one that looks problematic, pick another one -- lots of low-hanging fruit.

Add osclientcerts tests

osclientcerts is the Firefox PKCS#11 module that reflects client certificates from the Windows and macOS OS-wide cert store. We should add tests for it, by putting it behind pkcs11proxy and p11proxy and making sure the behavior doesn't change.

Find more software to test with

Potential places to look:

  • Search GitHub or https://sources.debian.org/ or https://searchfox.org/ for C_GetFunctionList and other PKCS#11 function names (especially function names that we currently don't implement in p11mod).
  • Search GitHub or package repos or Repology for pkcs11 or p11.
  • Search the dependency graph for other software that uses the PKCS#11 libs that are used in our current tests.

Once you find something, create an issue, or jump right in and add integration tests in a PR. If your integration tests don't pass (perhaps because of a bug in pkcs11mod) but you believe they are useful, that's fine, you can submit them anyway, and we can just instruct Cirrus to allow failure on them.

Log process ID + process name

We should consider logging the PID (os.Getpid()) and process name (os.Args[0] and os.Executable()). This would help for debugging things such as Chromium-specific "built-in trust anchor" disabling, and per-process Tor stream isolation. Maybe also the thread ID (syscall.Gettid()).

Test p11proxy with OpenDNSSEC

We currently test OpenDNSSEC with pkcs11proxy, but not p11proxy. We should add tests for p11proxy with OpenDNSSEC.

Test tstclnt on Windows

We should test NSS tstclnt on Windows. This would be easier if tstclnt binaries were readily available.

Investigate PKCS#11 support in GnuTLS on Debian

Running strace gnutls-cli www.namecoin.org on Fedora 34 finds a bunch of references to PKCS#11 modules, including p11-kit-trust.so. These references don't appear on Debian 11, which seems to indicate that PKCS#11 is not being used for certificate trust in GnuTLS on Debian. This might be related to Debian not using the --with-default-trust-store-pkcs11=pkcs11: configure flag. However, on Debian, the library libp11-kit.so.0 is loaded, which might indicate some kind of p11-kit misconfiguration rather than a GnuTLS issue. Not sure. We should investigate further. The p11-kit devs might be able to help.

"p11mod": support for high-level miekg/pkcs11/p11 API

miekg/pkcs11 includes a high-level API under the p11 subpackage. It would be interesting to produce a PKCS#11 module using pkcs11mod that proxies the commands to a package that exposes the p11 API. So the end-to-end testing would look like:

(PKCS#11 test app in C) --> (C ABI PKCS#11 commands) --> (pkcs11mod) --> (p11mod) --> (p11) --> (pkcs11) --> (C ABI PKCS#11 commands) --> (PKCS#11 module in C)

The main benefit here is that the p11 API is much more user-friendly to implement modules with than the pkcs11 API, which makes it easier to audit the modules.

Add Debian tests

Debian's p11-kit situation is less mature than Fedora's, so worth testing there.

Retry browser tests when they report a conection failure

The browser tests (Chromium/Firefox) seem to be very sensitive to network issues, and will often report a connection failure; this causes spurious Cirrus failures for us. It would be desirable to retry those tests a few times if a connection failure happens. Preferably do not retry if a certificate error is reported (not sure how easy it is to disambiguate the two).

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.