The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Crypt::PKCS11::XS - XS layer for PKCS #11 functions

SYNPOSIS

  use Crypt::PKCS11;
  my $pkcs11xs = Crypt::PKCS11::XS->new;
  $pkcs11xs->C_...

DESCRIPTION

XS layer containing wrappers for all PKCS #11 functions that converts Perl data structures to/from PKCS #11 specific data structures.

RETURN VALUES AND ERROR HANDLING

This is the general return value and error handling for all methods unless otherwise stated.

Return values will be given in output arguments and if the output is an ARRAY or HASH reference then they need to be set prior of using the call.

  my $output_hash = {};
  $pkcs11xs->C_Call($input_arg1, $input_arg2, $output_hash);
  print $output_hash->{output_field};

All functions will return a CKR value and the Perl XS intergration will croak if any argument are not the correct type.

METHODS

All methods that starts with C_ are in direct relation with the PKCS #11 v2.30 functions described in detail at http://www.cryptsoft.com/pkcs11doc/v230/ .

For documentation about what each of those method does please see the documentation for PKCS #11 v2.30, the only thing documented here is the structure of the input and output arguments which uses Perl arrays and hashes (conversion to/from PKCS #11 specific data structures are handled by the methods).

$pkcs11xs = Crypt::PKCS11::XS->new

Create a new Crypt::PKCS11::XS object.

$str = Crypt::PKCS11::XS::rv2str ($rv)

Return the textual name of a CKR return value.

$rv

A CKR value.

$rv = Crypt::PKCS11::XS::setCreateMutex ($pCreateMutex)

Set the global create mutex callback, for more information about mutex callbacks and how to use them please see Crypt::PKCS11::MutexCallbacks.

$pCreateMutex

A CODE reference for the create mutex callback.

Crypt::PKCS11::XS::clearCreateMutex

Clear the stored create mutex callback reference.

$rv = Crypt::PKCS11::XS::setDestroyMutex ($pDestroyMutex)

Set the global destroy mutex callback, for more information about mutex callbacks and how to use them please see Crypt::PKCS11::MutexCallbacks.

$pDestroyMutex

A CODE reference for the destroy mutex callback.

Crypt::PKCS11::XS::clearDestroyMutex

Clear the stored destroy mutex callback reference.

$rv = Crypt::PKCS11::XS::setLockMutex ($pLockMutex)

Set the global lock mutex callback, for more information about mutex callbacks and how to use them please see Crypt::PKCS11::MutexCallbacks.

$pLockMutex

A CODE reference for the lock mutex callback.

Crypt::PKCS11::XS::clearLockMutex

Clear the stored lock mutex callback reference.

$rv = Crypt::PKCS11::XS::setUnlockMutex ($pUnlockMutex)

Set the global unlock mutex callback, for more information about mutex callbacks and how to use them please see Crypt::PKCS11::MutexCallbacks.

$pUnlockMutex

A CODE reference for the unlock mutex callback.

Crypt::PKCS11::XS::clearUnlockMutex

Clear the stored unlock mutex callback reference.

$rv = $pkcs11xs->load ($path)

Load a PKCS #11 library.

$path

The path to a PKCS #11 dynamic loadable library.

$rv = $pkcs11xs->unload

Unload the PKCS #11 library.

$rv = $pkcs11xs->C_Initialize ($pInitArgs)
$pInitArgs

A HASH reference with the fields listed below.

NOTE: For all Mutex CODE references the global callback will be set, there is no support for individual callbacks per Crypt::PKCS11::XS object.

All of the Mutex arguments needs to be set if any should be used.

For more information about mutex callbacks and how to use them please see Crypt::PKCS11::MutexCallbacks.

CreateMutex

A CODE reference to a function to use for creating mutex objects. The global callback for CreateMutex will also be set.

DestroyMutex

A CODE reference to a function to use for destroying mutex objects. The global callback for DestroyMutex will also be set.

LockMutex

A CODE reference to a function to use for locking mutex objects. The global callback for LockMutex will also be set.

UnlockMutex

A CODE reference to a function to use for unlocking mutex objects. The global callback for UnlockMutex will also be set.

flags

A SCALAR.

$rv = $pkcs11xs->C_Finalize
$rv = $pkcs11xs->C_GetInfo ($pInfo)
$pInfo

A HASH reference output argument that will get the following fields set:

cryptokiVersion

A HASH reference with the following fields:

major

A SCALAR.

minor

A SCALAR.

manufacturerID

A SCALAR.

flags

A SCALAR.

libraryDescription

A SCALAR.

libraryVersion

A HASH reference with the following fields:

major

A SCALAR.

minor

A SCALAR.

$rv = $pkcs11xs->C_GetSlotList ($tokenPresent, $pSlotList)
$tokenPresent

A SCALAR.

$pSlotList

An ARRAY reference output argument that will get a list of slotIDs as SCALARs.

$rv = $pkcs11xs->C_GetSlotInfo ($slotID, $pInfo)
$slotID

A SCALAR.

$pInfo

A HASH reference output argument that will get the following fields set:

slotDescription

A SCALAR.

manufacturerID

A SCALAR.

flags

A SCALAR.

hardwareVersion

A HASH reference with the following fields:

major

A SCALAR.

minor

A SCALAR.

firmwareVersion

A HASH reference with the following fields:

major

A SCALAR.

minor

A SCALAR.

$rv = $pkcs11xs->C_GetTokenInfo ($slotID, $pInfo)
$slotID

A SCALAR.

$pInfo

A HASH reference output argument that will get the following fields set:

label

A SCALAR.

manufacturerID

A SCALAR.

model

A SCALAR.

serialNumber

A SCALAR.

flags

A SCALAR.

ulMaxSessionCount

A SCALAR.

ulSessionCount

A SCALAR.

ulMaxRwSessionCount

A SCALAR.

ulRwSessionCount

A SCALAR.

ulMaxPinLen

A SCALAR.

ulMinPinLen

A SCALAR.

ulTotalPublicMemory

A SCALAR.

ulFreePublicMemory

A SCALAR.

ulTotalPrivateMemory

A SCALAR.

ulFreePrivateMemory

A SCALAR.

hardwareVersion

A HASH reference with the following fields:

major

A SCALAR.

minor

A SCALAR.

firmwareVersion

A HASH reference with the following fields:

major

A SCALAR.

minor

A SCALAR.

utcTime

A SCALAR.

$rv = $pkcs11xs->C_GetMechanismList ($slotID, $pMechanismList)
$slotID

A SCALAR.

$pMechanismList

An ARRAY reference output argument that will get a list of mechanisms as SCALARs.

$rv = $pkcs11xs->C_GetMechanismInfo ($slotID, $type, $pInfo)
$slotID

A SCALAR.

$type

A SCALAR.

$pInfo

A HASH reference output argument that will get the following fields set:

ulMinKeySize

A SCALAR.

ulMaxKeySize

A SCALAR.

flags

A SCALAR.

$rv = $pkcs11xs->C_InitToken ($slotID, $pPin, $pLabel)
$slotID

A SCALAR.

$pPin

A SCALAR.

$pLabel

A SCALAR.

$rv = $pkcs11xs->C_InitPIN ($hSession, $pPin)
$hSession

A SCALAR.

$pPin

A SCALAR.

$rv = $pkcs11xs->C_SetPIN ($hSession, $pOldPin, $pNewPin)
$hSession

A SCALAR.

$pOldPin

A SCALAR.

$pNewPin

A SCALAR.

$rv = $pkcs11xs->C_OpenSession ($slotID, $flags, $Notify, $phSession)
$slotID

A SCALAR.

$flags

A SCALAR.

$Notify

A CODE reference.

$phSession

A SCALAR output argument.

$rv = $pkcs11xs->C_CloseSession ($hSession)
$hSession

A SCALAR.

$rv = $pkcs11xs->C_CloseAllSessions ($slotID)
$slotID

A SCALAR.

$rv = $pkcs11xs->C_GetSessionInfo ($hSession, $pInfo)
$hSession

A SCALAR.

$pInfo

A HASH reference output argument that will get the following fields set:

slotID

A SCALAR.

state

A SCALAR.

flags

A SCALAR.

ulDeviceError

A SCALAR.

$rv = $pkcs11xs->C_GetOperationState ($hSession, $pOperationState)
$hSession

A SCALAR.

$pOperationState

A SCALAR output argument.

$rv = $pkcs11xs->C_SetOperationState ($hSession, $pOperationState, $hEncryptionKey, $hAuthenticationKey)
$hSession

A SCALAR.

$pOperationState

A SCALAR.

$hEncryptionKey

A SCALAR.

$hAuthenticationKey

A SCALAR.

$rv = $pkcs11xs->C_Login ($hSession, $userType, $pPin)
$hSession

A SCALAR.

$userType

A SCALAR.

$pPin

A SCALAR.

$rv = $pkcs11xs->C_Logout ($hSession)
$hSession

A SCALAR.

$rv = $pkcs11xs->C_CreateObject ($hSession, $pTemplate, $phObject)
$hSession

A SCALAR.

$pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$phObject

A SCALAR output argument.

$rv = $pkcs11xs->C_CopyObject ($hSession, $hObject, $pTemplate, $phNewObject)
$hSession

A SCALAR.

$hObject

A SCALAR.

$pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$phNewObject

A SCALAR output argument.

$rv = $pkcs11xs->C_DestroyObject ($hSession, $hObject)
$hSession

A SCALAR.

$hObject

A SCALAR.

$rv = $pkcs11xs->C_GetObjectSize ($hSession, $hObject, $pulSize)
$hSession

A SCALAR.

$hObject

A SCALAR.

$pulSize

A SCALAR output argument.

$rv = $pkcs11xs->C_GetAttributeValue ($hSession, $hObject, $pTemplate)
$hSession

A SCALAR.

$hObject

A SCALAR.

$pTemplate

An ARRAY reference output argument, see TEMPLATE ARGUMENT. The attributes you wish to get must be set in the template before the call.

$rv = $pkcs11xs->C_SetAttributeValue ($hSession, $hObject, $pTemplate)
$hSession

A SCALAR.

$hObject

A SCALAR.

$pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$rv = $pkcs11xs->C_FindObjectsInit ($hSession, $pTemplate)
$hSession

A SCALAR.

$pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$rv = $pkcs11xs->C_FindObjects ($hSession, $phObject, $ulMaxObjectCount)
$hSession

A SCALAR.

$phObject

An ARRAY reference output argument that will get a list of SCALARs.

$ulMaxObjectCount

A SCALAR.

$rv = $pkcs11xs->C_FindObjectsFinal ($hSession)
$hSession

A SCALAR.

$rv = $pkcs11xs->C_EncryptInit ($hSession, $pMechanism, $hKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hKey

A SCALAR.

$rv = $pkcs11xs->C_Encrypt ($hSession, $pData, $pEncryptedData)
$hSession

A SCALAR.

$pData

A SCALAR.

$pEncryptedData

A SCALAR output argument.

$rv = $pkcs11xs->C_EncryptUpdate ($hSession, $pPart, $pEncryptedPart)
$hSession

A SCALAR.

$pPart

A SCALAR.

$pEncryptedPart

A SCALAR output argument.

$rv = $pkcs11xs->C_EncryptFinal ($hSession, $pLastEncryptedPart)
$hSession

A SCALAR.

$pLastEncryptedPart

A SCALAR output argument.

$rv = $pkcs11xs->C_DecryptInit ($hSession, $pMechanism, $hKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hKey

A SCALAR.

$rv = $pkcs11xs->C_Decrypt ($hSession, $pEncryptedData, $pData)
$hSession

A SCALAR.

$pEncryptedData

A SCALAR.

$pData

A SCALAR output argument.

$rv = $pkcs11xs->C_DecryptUpdate ($hSession, $pEncryptedPart, $pPart)
$hSession

A SCALAR.

$pEncryptedPart

A SCALAR.

$pPart

A SCALAR output argument.

$rv = $pkcs11xs->C_DecryptFinal ($hSession, $pLastPart)
$hSession

A SCALAR.

$pLastPart

A SCALAR output argument.

$rv = $pkcs11xs->C_DigestInit ($hSession, $pMechanism)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$rv = $pkcs11xs->C_Digest ($hSession, $pData, $pDigest)
$hSession

A SCALAR.

$pData

A SCALAR.

$pDigest

A SCALAR output argument.

$rv = $pkcs11xs->C_DigestUpdate ($hSession, $pPart)
$hSession

A SCALAR.

$pPart

A SCALAR.

$rv = $pkcs11xs->C_DigestKey ($hSession, $hKey)
$hSession

A SCALAR.

$hKey

A SCALAR.

$rv = $pkcs11xs->C_DigestFinal ($hSession, $pDigest)
$hSession

A SCALAR.

$pDigest

A SCALAR output argument.

$rv = $pkcs11xs->C_SignInit ($hSession, $pMechanism, $hKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hKey

A SCALAR.

$rv = $pkcs11xs->C_Sign ($hSession, $pData, $pSignature)
$hSession

A SCALAR.

$pData

A SCALAR.

$pSignature

A SCALAR output argument.

$rv = $pkcs11xs->C_SignUpdate ($hSession, $pPart)
$hSession

A SCALAR.

$pPart

A SCALAR.

$rv = $pkcs11xs->C_SignFinal ($hSession, $pSignature)
$hSession

A SCALAR.

$pSignature

A SCALAR output argument.

$rv = $pkcs11xs->C_SignRecoverInit ($hSession, $pMechanism, $hKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hKey

A SCALAR.

$rv = $pkcs11xs->C_SignRecover ($hSession, $pData, $pSignature)
$hSession

A SCALAR.

$pData

A SCALAR.

$pSignature

A SCALAR output argument.

$rv = $pkcs11xs->C_VerifyInit ($hSession, $pMechanism, $hKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hKey

A SCALAR.

$rv = $pkcs11xs->C_Verify ($hSession, $pData, $pSignature)
$hSession

A SCALAR.

$pData

A SCALAR.

$pSignature

A SCALAR.

$rv = $pkcs11xs->C_VerifyUpdate ($hSession, $pPart)
$hSession

A SCALAR.

$pPart

A SCALAR.

$rv = $pkcs11xs->C_VerifyFinal ($hSession, $pSignature)
$hSession

A SCALAR.

$pSignature

A SCALAR.

$rv = $pkcs11xs->C_VerifyRecoverInit ($hSession, $pMechanism, $hKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hKey

A SCALAR.

$rv = $pkcs11xs->C_VerifyRecover ($hSession, $pSignature, $pData)
$hSession

A SCALAR.

$pSignature

A SCALAR.

$pData

A SCALAR output argument.

$rv = $pkcs11xs->C_DigestEncryptUpdate ($hSession, $pPart, $pEncryptedPart)
$hSession

A SCALAR.

$pPart

A SCALAR.

$pEncryptedPart

A SCALAR output argument.

$rv = $pkcs11xs->C_DecryptDigestUpdate ($hSession, $pEncryptedPart, $pPart)
$hSession

A SCALAR.

$pEncryptedPart

A SCALAR.

$pPart

A SCALAR output argument.

$rv = $pkcs11xs->C_SignEncryptUpdate ($hSession, $pPart, $pEncryptedPart)
$hSession

A SCALAR.

$pPart

A SCALAR.

$pEncryptedPart

A SCALAR output argument.

$rv = $pkcs11xs->C_DecryptVerifyUpdate ($hSession, $pEncryptedPart, $pPart)
$hSession

A SCALAR.

$pEncryptedPart

A SCALAR.

$pPart

A SCALAR output argument.

$rv = $pkcs11xs->C_GenerateKey ($hSession, $pMechanism, $pTemplate, $phKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$phKey

A SCALAR output argument.

$rv = $pkcs11xs->C_GenerateKeyPair ($hSession, $pMechanism, $pPublicKeyTemplate, $pPrivateKeyTemplate, $phPublicKey, $phPrivateKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$pPublicKeyTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$pPrivateKeyTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$phPublicKey

A SCALAR output argument.

$phPrivateKey

A SCALAR output argument.

$rv = $pkcs11xs->C_WrapKey ($hSession, $pMechanism, $hWrappingKey, $hKey, $pWrappedKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hWrappingKey

A SCALAR.

$hKey

A SCALAR.

$pWrappedKey

A SCALAR output argument.

$rv = $pkcs11xs->C_UnwrapKey ($hSession, $pMechanism, $hUnwrappingKey, $pWrappedKey, $pTemplate, $phKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hUnwrappingKey

A SCALAR.

$pWrappedKey

A SCALAR.

$pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$phKey

A SCALAR output argument.

$rv = $pkcs11xs->C_DeriveKey ($hSession, $pMechanism, $hBaseKey, $pTemplate, $phKey)
$hSession

A SCALAR.

$pMechanism

A HASH reference, see MECHANISM ARGUMENT.

$hBaseKey

A SCALAR.

$pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

$phKey

A SCALAR output argument.

$rv = $pkcs11xs->C_SeedRandom ($hSession, $pSeed)
$hSession

A SCALAR.

$pSeed

A SCALAR.

$rv = $pkcs11xs->C_GenerateRandom ($hSession, $RandomData, $ulRandomLen)
$hSession

A SCALAR.

$RandomData

A SCALAR output argument.

$ulRandomLen

A SCALAR.

$rv = $pkcs11xs->C_GetFunctionStatus ($hSession)
$hSession

A SCALAR.

$rv = $pkcs11xs->C_CancelFunction ($hSession)
$hSession

A SCALAR.

$rv = $pkcs11xs->C_WaitForSlotEvent ($flags, $pSlot)
$flags

A SCALAR.

$pSlot

A SCALAR output argument.

PRIVATE METHODS

These are the private methods used within the module and should not be used elsewhere.

Crypt::PKCS11::XS::SvUOK ($sv)

Returns true (1) if the given SV is considered an unsigned number by Perl otherwise return false (0/undef).

Crypt::PKCS11::XS::SvIOK ($sv)

Returns true (1) if the given SV is considered an signed number by Perl otherwise return false (0/undef).

$sv

A SV to check.

MECHANISM ARGUMENT

A HASH reference that represents a CK_MECHANISM struct that has the following fields:

mechanism

A SCALAR.

pParameter

A SCALAR.

TEMPLATE ARGUMENT

An ARRAY reference that contains HASH references which represents CK_ATTRIBUTE structs and the HASH reference has the following fields:

type

A SCALAR.

pValue

A SCALAR.

NOTE

Derived from the RSA Security Inc. PKCS #11 Cryptographic Token Interface (Cryptoki)

AUTHOR

Jerry Lundström <lundstrom.jerry@gmail.com>

REPORTING BUGS

Report bugs at https://github.com/dotse/p5-Crypt-PKCS11/issues .

LICENSE

  Copyright (c) 2015 Jerry Lundström <lundstrom.jerry@gmail.com>
  Copyright (c) 2015 .SE (The Internet Infrastructure Foundation)
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.