corePKCS11 v3.1.0
PKCS #11 Cryptoki Library
core_pkcs11.h
Go to the documentation of this file.
1/*
2 * corePKCS11 v3.1.0
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * http://aws.amazon.com/freertos
23 * http://www.FreeRTOS.org
24 */
25
26#ifndef _CORE_PKCS11_H_
27#define _CORE_PKCS11_H_
28
29#include <stdint.h>
30
31/* *INDENT-OFF* */
32#ifdef __cplusplus
33 extern "C" {
34#endif
35/* *INDENT-ON* */
36
37#ifdef _WIN32
38 #pragma pack(push, cryptoki, 1)
39#endif
40
56#define CK_PTR *
57
62#ifndef NULL_PTR
63 #define NULL_PTR 0
64#endif
65
71#define CK_DEFINE_FUNCTION( returnType, name ) returnType name
72
78#define CK_DECLARE_FUNCTION( returnType, name ) returnType name
79
85#define CK_DECLARE_FUNCTION_POINTER( returnType, name ) returnType( CK_PTR name )
86
92#define CK_CALLBACK_FUNCTION( returnType, name ) returnType( CK_PTR name )
93
98#define pkcs11SHA256_DIGEST_LENGTH 32UL
99
104#define pkcs11AES_CMAC_SIGNATURE_LENGTH 16UL
105
112#define pkcs11ECDSA_P256_SIGNATURE_LENGTH 64UL
113
118#define pkcs11ECDSA_P256_KEY_BITS 256UL
119
124#define pkcs11RSA_PUBLIC_EXPONENT { 0x01, 0x00, 0x01 }
125
131#define pkcs11RSA_2048_MODULUS_BITS 2048UL
132
137#define pkcs11RSA_2048_SIGNATURE_LENGTH ( pkcs11RSA_2048_MODULUS_BITS / 8UL )
138
146#define pkcs11RSA_SIGNATURE_INPUT_LENGTH 51UL
147
153#define pkcs11ELLIPTIC_CURVE_NISTP256 "1.2.840.10045.3.1.7"
154
159#define pkcs11MAX_LABEL_LENGTH 32UL /* 31 characters + 1 null terminator. */
160
165#define pkcs11DER_ENCODED_OID_P256 { 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07 }
166
167
173#ifndef pkcs11configIMPORT_PRIVATE_KEYS_SUPPORTED
174 #define pkcs11configIMPORT_PRIVATE_KEYS_SUPPORTED 1
175#endif
176
197#define pkcs11STUFF_APPENDED_TO_RSA_SIG { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 }
198
199/* Bring in the public header. */
200
201/* Undefine the macro for Keil Compiler to avoid conflict */
202#if defined( __PASTE ) && defined( __CC_ARM )
203 /* ARM RCVT stdint.h has a duplicate definition with PKCS #11. */
204 #undef __PASTE
205#endif
206
207#ifdef CreateMutex
208 #undef CreateMutex /* This is a workaround because CreateMutex is redefined to CreateMutexW in synchapi.h in windows. :/ */
209#endif
210
211#include "pkcs11.h"
212
218typedef struct PKCS11_CertificateTemplate
219{
220 CK_ATTRIBUTE xObjectClass;
221 CK_ATTRIBUTE xSubject;
222 CK_ATTRIBUTE xCertificateType;
223 CK_ATTRIBUTE xValue;
224 CK_ATTRIBUTE xLabel;
225 CK_ATTRIBUTE xTokenObject;
227
228/*------------------------ PKCS #11 wrapper functions -------------------------*/
229
235/* @[declare_pkcs11_core_xinitializepkcs11] */
236CK_RV xInitializePKCS11( void );
237/* @[declare_pkcs11_core_xinitializepkcs11] */
238
253/* @[declare_pkcs11_core_xgetslotlist] */
254CK_RV xGetSlotList( CK_SLOT_ID ** ppxSlotId,
255 CK_ULONG * pxSlotCount );
256/* @[declare_pkcs11_core_xgetslotlist] */
257
267/* @[declare_pkcs11_core_xinitializepkcs11session] */
268CK_RV xInitializePkcs11Session( CK_SESSION_HANDLE * pxSession );
269/* @[declare_pkcs11_core_xinitializepkcs11session] */
270
277/* @[declare_pkcs11_core_xinitializepkcs11token] */
278CK_RV xInitializePkcs11Token( void );
279/* @[declare_pkcs11_core_xinitializepkcs11token] */
280
298/* @[declare_pkcs11_core_xfindobjectwithlabelandclass] */
299CK_RV xFindObjectWithLabelAndClass( CK_SESSION_HANDLE xSession,
300 char * pcLabelName,
301 CK_ULONG ulLabelNameLen,
302 CK_OBJECT_CLASS xClass,
303 CK_OBJECT_HANDLE_PTR pxHandle );
304/* @[declare_pkcs11_core_xfindobjectwithlabelandclass] */
305
325/* @[declare_pkcs11_core_vappendsha256algorithmidentifiersequence] */
326CK_RV vAppendSHA256AlgorithmIdentifierSequence( const uint8_t * puc32ByteHashedMessage,
327 uint8_t * puc51ByteHashOidBuffer );
328/* @[declare_pkcs11_core_vappendsha256algorithmidentifiersequence] */
329
330#ifdef _WIN32
331 #pragma pack(pop, cryptoki)
332#endif
333
334/* *INDENT-OFF* */
335#ifdef __cplusplus
336 }
337#endif
338/* *INDENT-ON* */
339
340#endif /* ifndef _CORE_PKCS11_H_ */
CK_RV vAppendSHA256AlgorithmIdentifierSequence(const uint8_t *puc32ByteHashedMessage, uint8_t *puc51ByteHashOidBuffer)
Appends digest algorithm sequence to SHA-256 hash for RSA signatures.
Definition: core_pkcs11.c:376
CK_RV xGetSlotList(CK_SLOT_ID **ppxSlotId, CK_ULONG *pxSlotCount)
Get a list of available PKCS #11 slots.
Definition: core_pkcs11.c:72
CK_RV xInitializePkcs11Session(CK_SESSION_HANDLE *pxSession)
Initializes the PKCS #11 module and opens a session.
Definition: core_pkcs11.c:255
CK_RV xFindObjectWithLabelAndClass(CK_SESSION_HANDLE xSession, char *pcLabelName, CK_ULONG ulLabelNameLen, CK_OBJECT_CLASS xClass, CK_OBJECT_HANDLE_PTR pxHandle)
Searches for an object with a matching label and class provided.
Definition: core_pkcs11.c:311
CK_RV xInitializePkcs11Token(void)
Initializes a PKCS #11 module and token.
Definition: core_pkcs11.c:178
CK_RV xInitializePKCS11(void)
Initializes a PKCS #11 session.
Definition: core_pkcs11.c:152
Certificate Template The object class must be the first attribute in the array.
Definition: core_pkcs11.h:219
CK_ATTRIBUTE xValue
CKA_VALUE, the DER byte array of the certificate contents.
Definition: core_pkcs11.h:223
CK_ATTRIBUTE xSubject
CKA_SUBJECT, this parameter is required by the PKCS #11 standard.
Definition: core_pkcs11.h:221
CK_ATTRIBUTE xCertificateType
CKA_CERTIFICATE_TYPE, set to CKC_X_509.
Definition: core_pkcs11.h:222
CK_ATTRIBUTE xTokenObject
CKA_TOKEN.
Definition: core_pkcs11.h:225
CK_ATTRIBUTE xObjectClass
CKA_CLASS, set to CKO_CERTIFICATE.
Definition: core_pkcs11.h:220
CK_ATTRIBUTE xLabel
CKA_LABEL.
Definition: core_pkcs11.h:224