=encoding utf8

=head1 NAME

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

=head1 SYNPOSIS

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

=head1 DESCRIPTION

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

=head1 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.

=head1 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).

=over 4

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

Create a new L<Crypt::PKCS11::XS> object.

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

Return the textual name of a CKR return value.

=over 4

=item $rv

A CKR value.

=back

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

Load a PKCS #11 library.

=over 4

=item $path

The path to a PKCS #11 dynamic loadable library.

=back

=item $rv = $pkcs11xs->unload

Unload the PKCS #11 library.

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

=over 4

=item $pInitArgs

A HASH reference with the fields listed below.

=over 4

=item flags

A SCALAR.

=back

=back

=item $rv = $pkcs11xs->C_Finalize

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

=over 4

=item $pInfo

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

=over 4

=item cryptokiVersion

A HASH reference with the following fields:

=over 4

=item major

A SCALAR.

=item minor

A SCALAR.

=back

=item manufacturerID

A SCALAR.

=item flags

A SCALAR.

=item libraryDescription

A SCALAR.

=item libraryVersion

A HASH reference with the following fields:

=over 4

=item major

A SCALAR.

=item minor

A SCALAR.

=back

=back

=back

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

=over 4

=item $tokenPresent

A SCALAR.

=item $pSlotList

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

=back

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

=over 4

=item $slotID

A SCALAR.

=item $pInfo

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

=over 4

=item slotDescription

A SCALAR.

=item manufacturerID

A SCALAR.

=item flags

A SCALAR.

=item hardwareVersion

A HASH reference with the following fields:

=over 4

=item major

A SCALAR.

=item minor

A SCALAR.

=back

=item firmwareVersion

A HASH reference with the following fields:

=over 4

=item major

A SCALAR.

=item minor

A SCALAR.

=back

=back

=back

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

=over 4

=item $slotID

A SCALAR.

=item $pInfo

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

=over 4

=item label

A SCALAR.

=item manufacturerID

A SCALAR.

=item model

A SCALAR.

=item serialNumber

A SCALAR.

=item flags

A SCALAR.

=item ulMaxSessionCount

A SCALAR.

=item ulSessionCount

A SCALAR.

=item ulMaxRwSessionCount

A SCALAR.

=item ulRwSessionCount

A SCALAR.

=item ulMaxPinLen

A SCALAR.

=item ulMinPinLen

A SCALAR.

=item ulTotalPublicMemory

A SCALAR.

=item ulFreePublicMemory

A SCALAR.

=item ulTotalPrivateMemory

A SCALAR.

=item ulFreePrivateMemory

A SCALAR.

=item hardwareVersion

A HASH reference with the following fields:

=over 4

=item major

A SCALAR.

=item minor

A SCALAR.

=back

=item firmwareVersion

A HASH reference with the following fields:

=over 4

=item major

A SCALAR.

=item minor

A SCALAR.

=back

=item utcTime

A SCALAR.

=back

=back

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

=over 4

=item $slotID

A SCALAR.

=item $pMechanismList

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

=back

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

=over 4

=item $slotID

A SCALAR.

=item $type

A SCALAR.

=item $pInfo

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

=over 4

=item ulMinKeySize

A SCALAR.

=item ulMaxKeySize

A SCALAR.

=item flags

A SCALAR.

=back

=back

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

=over 4

=item $slotID

A SCALAR.

=item $pPin

A SCALAR.

=item $pLabel

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pPin

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pOldPin

A SCALAR.

=item $pNewPin

A SCALAR.

=back

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

=over 4

=item $slotID

A SCALAR.

=item $flags

A SCALAR.

=item $Notify

A CODE reference.

=item $phSession

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=back

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

=over 4

=item $slotID

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pInfo

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

=over 4

=item slotID

A SCALAR.

=item state

A SCALAR.

=item flags

A SCALAR.

=item ulDeviceError

A SCALAR.

=back

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pOperationState

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pOperationState

A SCALAR.

=item $hEncryptionKey

A SCALAR.

=item $hAuthenticationKey

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $userType

A SCALAR.

=item $pPin

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=item $phObject

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $hObject

A SCALAR.

=item $pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=item $phNewObject

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $hObject

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $hObject

A SCALAR.

=item $pulSize

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $hObject

A SCALAR.

=item $pTemplate

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

=back

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

=over 4

=item $hSession

A SCALAR.

=item $hObject

A SCALAR.

=item $pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $phObject

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

=item $ulMaxObjectCount

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hKey

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pData

A SCALAR.

=item $pEncryptedData

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pPart

A SCALAR.

=item $pEncryptedPart

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pLastEncryptedPart

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hKey

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pEncryptedData

A SCALAR.

=item $pData

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pEncryptedPart

A SCALAR.

=item $pPart

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pLastPart

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pData

A SCALAR.

=item $pDigest

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pPart

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $hKey

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pDigest

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hKey

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pData

A SCALAR.

=item $pSignature

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pPart

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pSignature

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hKey

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pData

A SCALAR.

=item $pSignature

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hKey

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pData

A SCALAR.

=item $pSignature

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pPart

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pSignature

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hKey

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pSignature

A SCALAR.

=item $pData

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pPart

A SCALAR.

=item $pEncryptedPart

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pEncryptedPart

A SCALAR.

=item $pPart

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pPart

A SCALAR.

=item $pEncryptedPart

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pEncryptedPart

A SCALAR.

=item $pPart

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=item $phKey

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $pPublicKeyTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=item $pPrivateKeyTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=item $phPublicKey

A SCALAR output argument.

=item $phPrivateKey

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hWrappingKey

A SCALAR.

=item $hKey

A SCALAR.

=item $pWrappedKey

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hUnwrappingKey

A SCALAR.

=item $pWrappedKey

A SCALAR.

=item $pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=item $phKey

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pMechanism

A HASH reference, see MECHANISM ARGUMENT.

=item $hBaseKey

A SCALAR.

=item $pTemplate

An ARRAY reference, see TEMPLATE ARGUMENT.

=item $phKey

A SCALAR output argument.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $pSeed

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=item $RandomData

A SCALAR output argument.

=item $ulRandomLen

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=back

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

=over 4

=item $hSession

A SCALAR.

=back

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

=over 4

=item $flags

A SCALAR.

=item $pSlot

A SCALAR output argument.

=back

=back

=head1 PRIVATE METHODS

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

=over 4

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

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

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

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

=over 4

=item $sv

A SV to check.

=back

=back

=head1 MECHANISM ARGUMENT

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

=over 4

=item mechanism

A SCALAR.

=item pParameter

A SCALAR.

=back

=head1 TEMPLATE ARGUMENT

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

=over 4

=item type

A SCALAR.

=item pValue

A SCALAR.

=back

=head1 NOTE

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

=head1 AUTHOR

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

=head1 REPORTING BUGS

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

=head1 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.