blob: ac858f940e61965e654738cca0718429fb46d60d [file] [log] [blame]
Andrzej Kurekc53dee32018-01-23 05:44:20 -05001/**
2 * \file pkcs11_client.h
3 *
4 * \brief Generic wrapper for Cryptoki (PKCS#11) support
Andrzej Kureke1f26b82018-02-19 03:57:07 -05005 */
6/*
Andrzej Kurekdfedd822018-02-27 09:23:22 -05007 * Copyright (C) 2017-2018, ARM Limited, All Rights Reserved
Andrzej Kurekc53dee32018-01-23 05:44:20 -05008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
24#ifndef MBEDTLS_PKCS11_CLIENT_H
25#define MBEDTLS_PKCS11_CLIENT_H
26
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
33#if defined(MBEDTLS_PKCS11_CLIENT_C)
34
35#include <pkcs11.h>
36
37#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
38 !defined(inline) && !defined(__cplusplus)
39#define inline __inline
40#endif
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46#define MBEDTLS_PKCS11_FLAG_TOKEN ( (uint32_t) 0x80000000 )
47
48#if defined(MBEDTLS_PK_C)
49
Andrzej Kurek7e19f772018-02-19 04:00:27 -050050#define MBEDTLS_PKCS11_FLAG_SENSITIVE ( (uint32_t) 0x00000001 )
51#define MBEDTLS_PKCS11_FLAG_EXTRACTABLE ( (uint32_t) 0x00000002 )
52#define MBEDTLS_PKCS11_FLAG_SIGN ( (uint32_t) 0x00000010 )
53#define MBEDTLS_PKCS11_FLAG_VERIFY ( (uint32_t) 0x00000020 )
54#define MBEDTLS_PKCS11_FLAG_DECRYPT ( (uint32_t) 0x00000040 )
55#define MBEDTLS_PKCS11_FLAG_ENCRYPT ( (uint32_t) 0x00000080 )
Andrzej Kurekc53dee32018-01-23 05:44:20 -050056
57#include "pk.h"
58
59/**
60 * \brief Set up a PK context for a key pair in a PKCS#11 token
61 *
62 * \param ctx PK context to fill, which must have been initialized
63 * with mbedtls_pk_init().
64 * \param hSession Cryptoki session.
65 * \param hPublicKey Cryptoki handle of the public key.
66 * \param hPrivateKey Cryptoki handle of the private key, or
67 * CK_INVALID_HANDLE for a public key rather than a key
68 * pair.
69 *
70 * \return 0 on success,
71 * or MBEDTLS_ERR_PK_XXX error code.
72 *
Andrzej Kurekdfedd822018-02-27 09:23:22 -050073 * \note If any of the handles become invalid, then you may no
74 * longer do anything with the pk object except call
75 * mbedtls_pk_free on it.
Andrzej Kurekc53dee32018-01-23 05:44:20 -050076 */
Andrzej Kurek12603542018-02-19 04:06:05 -050077int mbedtls_pkcs11_setup_pk( mbedtls_pk_context *ctx,
Andrzej Kurekc53dee32018-01-23 05:44:20 -050078 CK_SESSION_HANDLE hSession,
79 CK_OBJECT_HANDLE hPublicKey,
80 CK_OBJECT_HANDLE hPrivateKey );
81
82/**
83 * \brief Import a transparent key into a PKCS#11 token
84 *
85 * This function imports a PK object containing a
86 * public key or a private-public key pair into a
87 * PKCS#11 token.
88 *
89 * \param ctx PK context, which must contain a transparent pk
Andrzej Kureke1f26b82018-02-19 03:57:07 -050090 * object (type #MBEDTLS_PK_RSA,
91 * #MBEDTLS_PK_RSASSA_PSS, #MBEDTLS_PK_ECKEY or
92 * #MBEDTLS_PK_ECDSA).
93 * \param flags Mask of #MBEDTLS_PKCS11_FLAG_XXX and
94 * #MBEDTLS_PK_FLAG_XXX, applying as follows:
95 * - #MBEDTLS_PKCS11_FLAG_TOKEN: PKCS#11 \c CKA_TOKEN
Andrzej Kurekc53dee32018-01-23 05:44:20 -050096 * flag: if set, import as token object; if clear,
97 * import as session object.
Andrzej Kureke1f26b82018-02-19 03:57:07 -050098 * - #MBEDTLS_PK_FLAG_EXTRACTABLE: PKCS#11
99 * \c CKA_EXTRACTABLE flag: if set, the private key
100 * will be extractable at least in wrapped form; if
101 * clear, the key will not be extractable at all.
102 * - #MBEDTLS_PK_FLAG_SENSITIVE: PKCS#11
103 * \c CKA_SENSITIVE flag: if set, the private key
104 * will not be extractable in plain form; if clear,
105 * the key will be extractable in plain form if
106 * #MBEDTLS_PK_FLAG_EXTRACTABLE is set.
107 * - #MBEDTLS_PK_FLAG_SIGN: if set, the private key
Andrzej Kurekc53dee32018-01-23 05:44:20 -0500108 * will be authorized for signing.
Andrzej Kureke1f26b82018-02-19 03:57:07 -0500109 * - #MBEDTLS_PK_FLAG_VERIFY: if set, the public key
Andrzej Kurekc53dee32018-01-23 05:44:20 -0500110 * will be authorized for verification.
Andrzej Kureke1f26b82018-02-19 03:57:07 -0500111 * - #MBEDTLS_PK_FLAG_DECRYPT: if set, the private key
Andrzej Kurekdfedd822018-02-27 09:23:22 -0500112 * will be authorized for decryption.
Andrzej Kureke1f26b82018-02-19 03:57:07 -0500113 * - #MBEDTLS_PK_FLAG_ENCRYPT: if set, the public key
Andrzej Kurekc53dee32018-01-23 05:44:20 -0500114 * will be authorized for encryption.
115 *
Andrzej Kureke1f26b82018-02-19 03:57:07 -0500116 * \param hSession Cryptoki session. The session must remain valid as long
117 * as the PK object is in use.
Andrzej Kurekc53dee32018-01-23 05:44:20 -0500118 * \param hPublicKey If non-null, on output, Cryptoki handle of the public
Andrzej Kureke1f26b82018-02-19 03:57:07 -0500119 * key. This handle must remain valid as long as the PK
120 * object is in use. If null, the public key is not
121 * imported.
Andrzej Kurekc53dee32018-01-23 05:44:20 -0500122 * \param hPrivateKey If non-null, on output, Cryptoki handle of the private
Andrzej Kureke1f26b82018-02-19 03:57:07 -0500123 * key. This handle must remain valid as long as the PK
124 * object is in use. If null, the private key is not
125 * imported.
Andrzej Kurekc53dee32018-01-23 05:44:20 -0500126 *
127 * \return 0 on success,
128 * or MBEDTLS_ERR_PK_XXX error code.
129 *
130 * \note If \c hPrivateKey is non-null then \c ctx must contain
131 * a full key pair. If \c hPrivateKey is null then \c ctx
132 * may contain a full key pair or just a public key.
133 *
134 * \note On failure, the values returned in \c hPublicKey and
135 * \c hPrivateKey will normally be \c CK_HANDLE_INVALID.
136 * One of them may be a valid handle in the unlikely case
137 * where the creation of one key object succeeded but
138 * the second one failed and destroying the first one
139 * also failed, for example because the token was
140 * disconnected.
141 */
Andrzej Kurek12603542018-02-19 04:06:05 -0500142int mbedtls_pkcs11_import_pk( const mbedtls_pk_context *ctx,
Andrzej Kurekc53dee32018-01-23 05:44:20 -0500143 uint32_t flags,
144 CK_SESSION_HANDLE hSession,
145 CK_OBJECT_HANDLE *hPublicKey,
146 CK_OBJECT_HANDLE *hPrivateKey );
147
148#endif /* MBEDTLS_PK_C */
149
150#ifdef __cplusplus
151}
152#endif
153
154#endif /* MBEDTLS_PKCS11_CLIENT_C */
155
156#endif /* MBEDTLS_PKCS11_H */