Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 1 | /** |
| 2 | * \file pkcs11_client.h |
| 3 | * |
| 4 | * \brief Generic wrapper for Cryptoki (PKCS#11) support |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 5 | */ |
| 6 | /* |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 7 | * Copyright (C) 2017, ARM Limited, All Rights Reserved |
| 8 | * 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 |
| 43 | extern "C" { |
| 44 | #endif |
| 45 | |
| 46 | #define MBEDTLS_PKCS11_FLAG_TOKEN ( (uint32_t) 0x80000000 ) |
| 47 | |
| 48 | #if defined(MBEDTLS_PK_C) |
| 49 | |
| 50 | #define MBEDTLS_PK_FLAG_SENSITIVE ( (uint32_t) 0x00000001 ) |
| 51 | #define MBEDTLS_PK_FLAG_EXTRACTABLE ( (uint32_t) 0x00000002 ) |
| 52 | #define MBEDTLS_PK_FLAG_SIGN ( (uint32_t) 0x00000010 ) |
| 53 | #define MBEDTLS_PK_FLAG_VERIFY ( (uint32_t) 0x00000020 ) |
| 54 | #define MBEDTLS_PK_FLAG_DECRYPT ( (uint32_t) 0x00000040 ) |
| 55 | #define MBEDTLS_PK_FLAG_ENCRYPT ( (uint32_t) 0x00000080 ) |
| 56 | |
| 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 | * |
| 73 | * \note The session and the key(s) must remain valid until the |
| 74 | * PK context is closed with mbedtls_pk_free(). As an |
| 75 | * exception, it's ok to call mbedtls_pk_free() itself |
| 76 | * even if the Cryptoki handles have become invalid. |
| 77 | */ |
| 78 | int mbedtls_pk_setup_pkcs11( mbedtls_pk_context *ctx, |
| 79 | CK_SESSION_HANDLE hSession, |
| 80 | CK_OBJECT_HANDLE hPublicKey, |
| 81 | CK_OBJECT_HANDLE hPrivateKey ); |
| 82 | |
| 83 | /** |
| 84 | * \brief Import a transparent key into a PKCS#11 token |
| 85 | * |
| 86 | * This function imports a PK object containing a |
| 87 | * public key or a private-public key pair into a |
| 88 | * PKCS#11 token. |
| 89 | * |
| 90 | * \param ctx PK context, which must contain a transparent pk |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 91 | * object (type #MBEDTLS_PK_RSA, |
| 92 | * #MBEDTLS_PK_RSASSA_PSS, #MBEDTLS_PK_ECKEY or |
| 93 | * #MBEDTLS_PK_ECDSA). |
| 94 | * \param flags Mask of #MBEDTLS_PKCS11_FLAG_XXX and |
| 95 | * #MBEDTLS_PK_FLAG_XXX, applying as follows: |
| 96 | * - #MBEDTLS_PKCS11_FLAG_TOKEN: PKCS#11 \c CKA_TOKEN |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 97 | * flag: if set, import as token object; if clear, |
| 98 | * import as session object. |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 99 | * - #MBEDTLS_PK_FLAG_EXTRACTABLE: PKCS#11 |
| 100 | * \c CKA_EXTRACTABLE flag: if set, the private key |
| 101 | * will be extractable at least in wrapped form; if |
| 102 | * clear, the key will not be extractable at all. |
| 103 | * - #MBEDTLS_PK_FLAG_SENSITIVE: PKCS#11 |
| 104 | * \c CKA_SENSITIVE flag: if set, the private key |
| 105 | * will not be extractable in plain form; if clear, |
| 106 | * the key will be extractable in plain form if |
| 107 | * #MBEDTLS_PK_FLAG_EXTRACTABLE is set. |
| 108 | * - #MBEDTLS_PK_FLAG_SIGN: if set, the private key |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 109 | * will be authorized for signing. |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 110 | * - #MBEDTLS_PK_FLAG_VERIFY: if set, the public key |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 111 | * will be authorized for verification. |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 112 | * - #MBEDTLS_PK_FLAG_DECRYPT: if set, the private key |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 113 | * will be authorized for signing. |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 114 | * - #MBEDTLS_PK_FLAG_ENCRYPT: if set, the public key |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 115 | * will be authorized for encryption. |
| 116 | * |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 117 | * \param hSession Cryptoki session. The session must remain valid as long |
| 118 | * as the PK object is in use. |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 119 | * \param hPublicKey If non-null, on output, Cryptoki handle of the public |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 120 | * key. This handle must remain valid as long as the PK |
| 121 | * object is in use. If null, the public key is not |
| 122 | * imported. |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 123 | * \param hPrivateKey If non-null, on output, Cryptoki handle of the private |
Andrzej Kurek | e1f26b8 | 2018-02-19 03:57:07 -0500 | [diff] [blame^] | 124 | * key. This handle must remain valid as long as the PK |
| 125 | * object is in use. If null, the private key is not |
| 126 | * imported. |
Andrzej Kurek | c53dee3 | 2018-01-23 05:44:20 -0500 | [diff] [blame] | 127 | * |
| 128 | * \return 0 on success, |
| 129 | * or MBEDTLS_ERR_PK_XXX error code. |
| 130 | * |
| 131 | * \note If \c hPrivateKey is non-null then \c ctx must contain |
| 132 | * a full key pair. If \c hPrivateKey is null then \c ctx |
| 133 | * may contain a full key pair or just a public key. |
| 134 | * |
| 135 | * \note On failure, the values returned in \c hPublicKey and |
| 136 | * \c hPrivateKey will normally be \c CK_HANDLE_INVALID. |
| 137 | * One of them may be a valid handle in the unlikely case |
| 138 | * where the creation of one key object succeeded but |
| 139 | * the second one failed and destroying the first one |
| 140 | * also failed, for example because the token was |
| 141 | * disconnected. |
| 142 | */ |
| 143 | int mbedtls_pk_import_to_pkcs11( const mbedtls_pk_context *ctx, |
| 144 | uint32_t flags, |
| 145 | CK_SESSION_HANDLE hSession, |
| 146 | CK_OBJECT_HANDLE *hPublicKey, |
| 147 | CK_OBJECT_HANDLE *hPrivateKey ); |
| 148 | |
| 149 | #endif /* MBEDTLS_PK_C */ |
| 150 | |
| 151 | #ifdef __cplusplus |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | #endif /* MBEDTLS_PKCS11_CLIENT_C */ |
| 156 | |
| 157 | #endif /* MBEDTLS_PKCS11_H */ |