blob: 6d314a9a73b6d87d72fd8b32b848235ee6be5056 [file] [log] [blame]
Andrzej Kurek753b86c2018-01-23 08:56:17 -05001/**
2 * \file pkcs11_client.h
3 *
4 * \brief Generic wrapper for Cryptoki (PKCS#11) support
5 *
6 * Copyright (C) 2017, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23#ifndef MBEDTLS_PKCS11_CLIENT_H
24#define MBEDTLS_PKCS11_CLIENT_H
25
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
32#if defined(MBEDTLS_PKCS11_CLIENT_C)
33
34#include <pkcs11.h>
35
36#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
37 !defined(inline) && !defined(__cplusplus)
38#define inline __inline
39#endif
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45#define MBEDTLS_PKCS11_FLAG_TOKEN ( (uint32_t) 0x80000000 )
46
47#if defined(MBEDTLS_PK_C)
48
49#define MBEDTLS_PK_FLAG_SENSITIVE ( (uint32_t) 0x00000001 )
50#define MBEDTLS_PK_FLAG_EXTRACTABLE ( (uint32_t) 0x00000002 )
51#define MBEDTLS_PK_FLAG_SIGN ( (uint32_t) 0x00000010 )
52#define MBEDTLS_PK_FLAG_VERIFY ( (uint32_t) 0x00000020 )
53#define MBEDTLS_PK_FLAG_DECRYPT ( (uint32_t) 0x00000040 )
54#define MBEDTLS_PK_FLAG_ENCRYPT ( (uint32_t) 0x00000080 )
55
56#include "pk.h"
57
58/**
Andrzej Kurek686a05e2018-03-02 17:11:39 -050059 * \brief Set up a PK context from a key pair in a PKCS#11 token.
60 * This allows to access the token's cryptographic
61 * functionality through the PK interface.
Andrzej Kurek753b86c2018-01-23 08:56:17 -050062 *
63 * \param ctx PK context to fill, which must have been initialized
64 * with mbedtls_pk_init().
65 * \param hSession Cryptoki session.
66 * \param hPublicKey Cryptoki handle of the public key.
67 * \param hPrivateKey Cryptoki handle of the private key, or
68 * CK_INVALID_HANDLE for a public key rather than a key
69 * pair.
70 *
71 * \return 0 on success,
72 * or MBEDTLS_ERR_PK_XXX error code.
73 *
74 * \note The session and the key(s) must remain valid until the
75 * PK context is closed with mbedtls_pk_free(). As an
76 * exception, it's ok to call mbedtls_pk_free() itself
77 * even if the Cryptoki handles have become invalid.
78 */
79int mbedtls_pk_setup_pkcs11( mbedtls_pk_context *ctx,
80 CK_SESSION_HANDLE hSession,
81 CK_OBJECT_HANDLE hPublicKey,
82 CK_OBJECT_HANDLE hPrivateKey );
83
84/**
85 * \brief Import a transparent key into a PKCS#11 token
86 *
87 * This function imports a PK object containing a
88 * public key or a private-public key pair into a
Andrzej Kurek686a05e2018-03-02 17:11:39 -050089 * PKCS#11 token.
Andrzej Kurek753b86c2018-01-23 08:56:17 -050090 *
91 * \param ctx PK context, which must contain a transparent pk
92 * object (type \c MBEDTLS_PK_RSA,
93 * \c MBEDTLS_PK_RSASSA_PSS, \c MBEDTLS_PK_ECKEY or
94 * \c MBEDTLS_PK_ECDSA).
95 * \param flags Mask of \c MBEDTLS_PKCS11_FLAG_XXX and
96 * \c MBEDTLS_PK_FLAG_XXX, applying as follows:
97 * - \c MBEDTLS_PKCS11_FLAG_TOKEN: PKCS#11 \c CKA_TOKEN
98 * flag: if set, import as token object; if clear,
99 * import as session object.
100 * - \c MBEDTLS_PK_FLAG_EXTRACTABLE: PKCS#11
101 * \c CKA_EXTRACTABLE flag: if set, the key will be
102 * extractable at least in wrapped form; if clear,
103 * the key will not be extractable at all.
104 * - \c MBEDTLS_PK_FLAG_SENSITIVE: PKCS#11
105 * \c CKA_SENSITIVE flag: if set, the key will be
106 * not be extractable in plain form; if clear, the
107 * key will be extractable at least in wrapped form.
108 * - \c MBEDTLS_PK_FLAG_SIGN: if set, the private key
109 * will be authorized for signing.
110 * - \c MBEDTLS_PK_FLAG_VERIFY: if set, the public key
111 * will be authorized for verification.
112 * - \c MBEDTLS_PK_FLAG_DECRYPT: if set, the private key
113 * will be authorized for signing.
114 * - \c MBEDTLS_PK_FLAG_ENCRYPT: if set, the public key
115 * will be authorized for encryption.
116 *
117 * \param hSession Cryptoki session.
Andrzej Kurek686a05e2018-03-02 17:11:39 -0500118 * \param hPublicKey If not NULL, receives the Cryptoki handle of the public
119 * key on success. If NULL, the public key is not
120 * imported.
121 * \param hPrivateKey If not NULL, receives the Cryptoki handle of the
122 * private key on success. If NULL, the private key is
123 * not imported.
Andrzej Kurek753b86c2018-01-23 08:56:17 -0500124 *
125 * \return 0 on success,
126 * or MBEDTLS_ERR_PK_XXX error code.
127 *
128 * \note If \c hPrivateKey is non-null then \c ctx must contain
129 * a full key pair. If \c hPrivateKey is null then \c ctx
130 * may contain a full key pair or just a public key.
131 *
132 * \note On failure, the values returned in \c hPublicKey and
133 * \c hPrivateKey will normally be \c CK_HANDLE_INVALID.
134 * One of them may be a valid handle in the unlikely case
135 * where the creation of one key object succeeded but
136 * the second one failed and destroying the first one
137 * also failed, for example because the token was
138 * disconnected.
139 */
140int mbedtls_pk_import_to_pkcs11( const mbedtls_pk_context *ctx,
141 uint32_t flags,
142 CK_SESSION_HANDLE hSession,
143 CK_OBJECT_HANDLE *hPublicKey,
144 CK_OBJECT_HANDLE *hPrivateKey );
145
146#endif /* MBEDTLS_PK_C */
147
148#ifdef __cplusplus
149}
150#endif
151
152#endif /* MBEDTLS_PKCS11_CLIENT_C */
153
154#endif /* MBEDTLS_PKCS11_H */