Giter Club home page Giter Club logo

pvpkcs11's Introduction

pvpkcs11

License

pvpkcs11 consists of an input validation library we call core and a set of PKCS#11 implementations that wrap operating system and browser cryptographic and certificate store implementations.

We wanted a solution that provides unified access to the underlying certificate stores and associated cryptographic implementations. PKCS#11 was a natural choice for an API to enable this scenario given its broad adoption.

To make the development on these platforms and user agents easier and to ensure their runtime behavior is uniform, we utilize core to perform input validation. This is similar to how we architected node-webcrypto-ossl, node-webcrypto-p11 and webcrypto-liner where we share webcrypto-core.

With this one library you will be able to access many different underlying cryptographic implementations and certificate stores:

image

Approach

  • Each implementation will be compiled into one library, pvpkcs11.dll/.so, and each one will be exposed via its own slot.
  • RSA keys, ECDSA keys, X509 certificates, and PKCS10's can be persisted.
  • Certificate store operations will be exposed as CKO_X509
  • Certificate requests will be stored via CKO_DATA.
  • Both CKO_X509 and CKO_DATA will be manageable via C_CreateObject, C_DestroyObject, C_CloneObject.
  • AES keys will only be supported as session objects.

Capabilities

  • Basic certificate store management enabling access of certificates, and certificate requests as well as installation and removal.
  • Basic cryptographic operations where supported by underlying cryptographic and certificate store implementation (typically RSA PKCS1, RSA-PSS, ECDSA, ECDH, and AES).
  • Where ECC is supported only secp256r1, secp384r1 and secp521r1 are supported.
  • Where RSA is supported only RSA 1024, 2048, 3072 and 4096 are supported.
  • Where AES is supported key lengths of 128, 192 and 256 are supported.

Class Design

image

WARNING

At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application.

Using

Building

  • At this time only MSCAPI and CommonCrypto (OSX) support is implemented.
  • At this time only building on Windows and OSX is supported.
  • The package does not have a build script at this time.

To build you need Visual Studio and you follow the following steps:

  • build.bat
  • open build/binding.sln
  • Run build

Testing

  • Install dependencies
npm install --ignore-scripts
  • Run tests
npm test

Enviroment Variables

Name Type Description
PV_PKCS11_ERROR any Prints to stdout additional information about errors from PKCS#11 module
PV_PKCS11_ERROR_LEVEL number Combination of flags for different types of messages
Name Value
INFO 1
WARN 2
ERROR 4
DEBUG 8
TRACE 16

Supported Algorithms

MSCAPI

Function Algorithms
Hash SHA1; SHA2; SHA384; SHA512
Sign RSA /w SHA1; RSA PKCS1 /w SHA1, SHA2; RSA PSS /w SHA1, SHA2; ECDSA /w SHA1, SHA2
Exchange ECDH /w SHA1
Encryption RSA OAEP; AES modes CBC, CBC-PAD, GCM, and ECB

OSX

Function Algorithms
Hash SHA1; SHA2; SHA384; SHA512
Sign RSA /w SHA1; RSA PKCS1 /w SHA1, SHA2; RSA PSS /w SHA1, SHA2; ECDSA /w SHA1, SHA2
Exchange ECDH /w SHA1
Encryption RSA OAEP; AES modes CBC, CBC-PAD, GCM, and ECB

Related

pvpkcs11's People

Contributors

donskov avatar microshine avatar mingjunyang avatar rmhrisk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pvpkcs11's Issues

Add support for logging directort configuration

Currently, the log file is placed in /tmp, this makes sense for a system wide installation but on a per user installation, it is better to have it in the users tmp folder.

We should make the default the users tmp folder and have configuration available to place it wherever the user wants.

At the same time, we should be able to disable logging.

Block CKA_ID

Don't change CKA_ID value for Object

OS X

Use kSecAttrApplicationLabel and kSecPublicKeyHash

We can set own ID on object creation, but it can be changed after modules restart.

AES padding

PKCS#11 has 2 mechanisms CKM_AES_CBC and CKM_AES_CBC_PAD. 1st mechanism doesn't use padding and incoming must be multiple to BLOCK LENGTH (16), otherwise throw CKR_DATA_LEN_RANGE error.

I've got this error for CKM_AES_ECB mechanism with SoftHSM in node-webcrypto-p11. I can resolve this error on webcrypto layer.

node-webcrypto-ossl and webcrypto-liner use padding.

@rmhrisk What should I do for pvpkcs11? Current AES-ECB implementation supports padding and I use CKM_AES-ECB mechanism. Should I add vendor mechanism CKM_AES_ECB_PAD for it?

CKM_AES_ECB_PAD

Exception on getting keys from keychain

Error: C_OpenSession
Pkcs11Exception: Error on SecItemCopyMatching
    at Open (../../src/osx/session.cpp:277)
    at Open (../../src/osx/session.cpp:292)
    at OpenSession (../../src/core/slot.cpp:169)

Error: CKR_FUNCTION_FAILED:6
    at Error (native) C_OpenSession:311
    at Slot.open (/Users/rmh/Downloads/electron-app/node_modules/graphene-pk11/build/slot.js:42:33)
    at WebCrypto.open (/Users/rmh/Downloads/electron-app/node_modules/node-webcrypto-p11/built/webcrypto.js:45:34)
    at new WebCrypto (/Users/rmh/Downloads/electron-app/node_modules/node-webcrypto-p11/built/webcrypto.js:29:14)

CKA_ID Attribute Altered During C_CreateObject on MacOS

Description:

When using the pvpkcs11 library on MacOS, the CKA_ID attribute is being altered during the execution of C_CreateObject. This inconsistency leads to an unexpected CKA_ID value in the PKCS#11 object, differing from the original value specified in the template.

Steps to Reproduce:

  1. Create a PKCS#11 template with a specified CKA_ID attribute.
  2. Call C_CreateObject with the created template.
  3. Check the CKA_ID attribute of the created PKCS#11 object.

Expected Behavior:

The CKA_ID attribute in the PKCS#11 object should match the original value provided in the template.

Actual Behavior:

The CKA_ID attribute gets altered and does not match the value specified in the template. For instance:

  • Template CKA_ID: '73e4ac6199ba1ff0c29b44a72d643f43'
  • Resulting PKCS#11 Object CKA_ID: '2d0cf479b87f65381e211287443ade9f1413b666'

Code Snippet:

// Simplified C code snippet to reproduce the issue
// ... (Initialization and other code)
CK_ATTRIBUTE template[] = {
    {CKA_ID, id_data, id_length},
    // ... (Other attributes)
};
C_CreateObject(session, template, count, &object_handle);
// ... (Fetching the CKA_ID of the object and finding it altered)

Suggested Fix:

This appears to be a bug within the pvpkcs11 library, specifically when running on MacOS. The internal implementation of C_CreateObject may need to be revised to preserve the original CKA_ID attribute during object creation.

Error on module load

Error: C_OpenSession
OSXException: Error on SecAsn1Decode -26275. Unable to decode the provided data.
    at GetPublicKeyHash (../../src/osx/certificate.cpp:118)
    at GetPublicKeyHash (../../src/osx/certificate.cpp:150)
    at Assign (../../src/osx/certificate.cpp:92)
    at Open (../../src/osx/session.cpp:291)
    at OpenSession (../../src/core/slot.cpp:169)

Support using Windows mini drivers effectivly

I've got some questions about SmartCard Provider using.

  1. Is there any way to open MS_SMART_CARD_KEY_STORAGE_PROVIDER provider (NCryptOpenStorageProvider) for specific card?
    It looks that Windows opens only first SmartCard and doesn't enum certificates and keys for multi SmartCard using

two tokens in slot

  1. Rutoken
  2. Yubikey NEO
Start SmartCard application
SmartCard GUID: 98364C541F19BC0E25D0B8CD4A7159F5
Provider name: Microsoft Smart Card Key Storage Provider
Certificate: RU, "RSA rutoken #1"
Key enum:
  81d4edc5-8696-4170-b42b-4d5ae9453a50
  New key
Done

first token was removed

  1. Yubikey NEO
Start SmartCard application
SmartCard GUID: F8CD87C7503693F2612A81C4BA57AFBB
Provider name: Microsoft Smart Card Key Storage Provider
Certificate: micro
Key enum:
  c787cdf8-3650-f293-612a-81c4ba5fc10b
Done

fist token supports CSP interface only

  1. SafeNET (CSP only)
  2. Yubikey NEO
Start SmartCard application
SmartCard GUID: F8CD87C7503693F2612A81C4BA57AFBB
Provider name: Microsoft Smart Card Key Storage Provider
Certificate: micro
Key enum:
  c787cdf8-3650-f293-612a-81c4ba5fc10b
Done
  1. Which implementation do we have to use?

Current version

  1. Use SCard API to catch insert/remove tokens. This API allows to determine which Storage Provider SmartCard uses (CSP, KSP or custom)
  2. Create slot for each Reader name. (Certificates from user store have property with linked Reader name)
  3. Use SmartCards for signing only. Cannot generate key.

New version

  1. Create slot for KSP and CSP providers
  2. Generate keys and adding certificates
  3. Remove keys and certificates
  4. Use crypto operations
  • Doesn't support multi smart cards
  • We don't know reader name from provider

Support init params for C_Initialize function

Cryptoki params

typedef struct CK_C_INITIALIZE_ARGS {
  CK_CREATEMUTEX CreateMutex;
  CK_DESTROYMUTEX DestroyMutex;
  CK_LOCKMUTEX LockMutex;
  CK_UNLOCKMUTEX UnlockMutex;
  CK_FLAGS flags;
  CK_VOID_PTR pReserved;
} CK_C_INITIALIZE_ARGS;

NSS params

typedef struct CK_NSS_C_INITIALIZE_ARGS {
    CK_CREATEMUTEX CreateMutex;
    CK_DESTROYMUTEX DestroyMutex;
    CK_LOCKMUTEX LockMutex;
    CK_UNLOCKMUTEX UnlockMutex;
    CK_FLAGS flags;
    CK_CHAR_PTR LibraryParameters;
    CK_VOID_PTR pReserved;
} CK_NSS_C_INITIALIZE_ARGS;

It would be nice to use something like NSS CK_CHAR_PTR LibraryParameters

ECDH different values

JWK EC key

{
  kty: 'EC',
  crv: 'P-256',
  ext: true,
  key_ops: [ 'decrypt', 'deriveKey', 'deriveBits', 'sign', 'unwrapKey' ],
  d: '0QhOR25Onsgw3fySSzx3FYTVWxKbLGkWJLfyD6JGrUI',
  x: '8p8RVlOAWn8Mz6tMEkCzhNsHCs-z_FCv6bDZwGqqX_0',
  y: 'R6PlEjvTilfq2HrgVkxlfyNT9hCzKt6S7apJwisBk9E' 
}

OpenSSL derived result (source):

3709caac756a1cdce41de9cde756e114

pvpkcs11 derived result (source):

641BA0F75D0BC476B71600B532C641A47D075B7D7E556FC81BEB18111D92528D

ECDH key derivation

node-webcrypto-p11 uses CKD_NULL for CK_EC_KDF_TYPE.

@rmhrisk Could you help me to find any examples for CNG key derivation with the same KDF parameter?

Here are some exmples I found:
Example 1
Example 2

Error on certStorage.remove

Action: crypto/certificateStorage/removeItem
Error: C_DestroyObject
Exception: Error on SecItemDelete
    at Destroy (../../src/osx/certificate.cpp:238)
    at Destroy (../../src/osx/certificate.cpp:243)
    at DestroyObject (../../src/core/module.cpp:960)

Error for action: crypto/certificateStorage/removeItem
Error: CKR_FUNCTION_FAILED:6
    at Error (native) C_DestroyObject:504
    at X509Certificate.SessionObject.destroy (/Users/rmh/Downloads/electron-app/node_modules/graphene-pk11/build/object.js:47:18)
    at Pkcs11CertificateStorage.<anonymous> (/Users/rmh/Downloads/electron-app/node_modules/node-webcrypto-p11/built/cert_storage.js:95:35)
    at step (/Users/rmh/Downloads/electron-app/node_modules/tslib/tslib.js:122:27)
    at Object.next (/Users/rmh/Downloads/electron-app/node_modules/tslib/tslib.js:103:57)
    at /Users/rmh/Downloads/electron-app/node_modules/tslib/tslib.js:96:75
    at Object.__awaiter (/Users/rmh/Downloads/electron-app/node_modules/tslib/tslib.js:92:16)
    at Pkcs11CertificateStorage.removeItem (/Users/rmh/Downloads/electron-app/node_modules/node-webcrypto-p11/built/cert_storage.js:90:24)
    at LocalServer.<anonymous> (/Users/rmh/Downloads/electron-app/node_modules/webcrypto-local/out/local/server.js:570:68)
    at step (/Users/rmh/Downloads/electron-app/node_modules/tslib/tslib.js:122:27)
    at Object.next (/Users/rmh/Downloads/electron-app/node_modules/tslib/tslib.js:103:57)

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.