Skip to content

Commit fb33afa

Browse files
committed
release 2.5.0 - support for CKA_ALLOWED_MECHANISMS
1 parent c6ce5af commit fb33afa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+3141
-2456
lines changed

.gnulib

Submodule .gnulib updated 3656 files

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
=======
7+
# [2.5.0]
8+
### Added
9+
- `CKA_ALLOWED_MECHANISMS` support for all key management utilities (`p11keygen`, `p11wrap`, `p11unwrap`, `p11rewrap`, `p11ls`, `p11od`)
10+
11+
### Fixed
12+
- `p11wrap`: fixed memory leaks
13+
814
# [2.4.2]
915
### Fixed
10-
- `p11ls`: removed duplicate `CKA_CHECK_VALUE` attribute from `C_GetAttributeValue()` call on secret keys (may cause issues on some PKCS\#11 tokens)
16+
- `p11ls`: removed duplicate `CKA_CHECK_VALUE` attribute from `C_GetAttributeValue()` call on secret keys (may cause issues on some PKCS\#11 tokens)
1117

1218
# [2.4.1]
1319
### Fixed
@@ -118,6 +124,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
118124
### Added
119125
- Initial public release
120126

127+
[2.5.0]: https://github.com/Mastercard/pkcs11-tools/tree/v2.5.0
121128
[2.4.2]: https://github.com/Mastercard/pkcs11-tools/tree/v2.4.2
122129
[2.4.1]: https://github.com/Mastercard/pkcs11-tools/tree/v2.4.1
123130
[2.4.0]: https://github.com/Mastercard/pkcs11-tools/tree/v2.4.0

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dnl limitations under the License.
1414

1515

1616
AC_PREREQ([2.64])
17-
AC_INIT([pkcs11-tools], [2.4.2], [https://github.com/Mastercard/pkcs11-tools/issues], [pkcs11-tools], [https://github.com/Mastercard/pkcs11-tools])
17+
AC_INIT([pkcs11-tools], [2.5.0], [https://github.com/Mastercard/pkcs11-tools/issues], [pkcs11-tools], [https://github.com/Mastercard/pkcs11-tools])
1818
AC_CONFIG_MACRO_DIR([m4])
1919

2020
dnl adding AM_MAINTAINER_MODE to address autotools issues with git

docs/MANUAL.md

Lines changed: 133 additions & 14 deletions
Large diffs are not rendered by default.

include/pkcs11lib.h

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
#include "cryptoki.h"
3636

3737
/* grammar version, for wrapped keys */
38-
#define SUPPORTED_GRAMMAR_VERSION "2.1"
39-
#define TOOLKIT_VERSION_SUPPORTING_GRAMMAR "2.4.0"
38+
#define SUPPORTED_GRAMMAR_VERSION "2.2"
39+
#define TOOLKIT_VERSION_SUPPORTING_GRAMMAR "2.5.0"
4040

4141
/* Program Error Codes */
4242
#define RC_OK 0x00
@@ -218,13 +218,15 @@ typedef enum {
218218
} hash_alg_t ;
219219

220220

221-
/* cmdLineCtx contains a context that can hold parameters describing attributes. */
222-
/* it currently supports these grammars:
221+
/* attribCtx contains a context that can hold parameters parsed from command line
222+
that contains attributes.
223+
It currently supports these grammars:
223224
- CKA_DERIVE=true CKA_LABEL="label" CKA_UNWRAP_TEMPLATE={ CKA_EXTRACTABLE=false ... }
224225
- the attributes can be shortened by removing the "CKA_" prefix
225-
- boolean attributes can be true/false, CK_TRUE/CK_FALSE, 1/0, yes/no
226+
- boolean attributes can be true/false, CK_TRUE/CK_FALSE, yes/no, on/off
226227
- boolean attributes without a value are set to CK_TRUE
227228
- boolean attributes prefixed with "no" are set to CK_FALSE
229+
- other attributes follow the same value syntax as for wrappedKeyCtx
228230
*/
229231

230232
typedef struct s_p11_attribctx {
@@ -242,7 +244,11 @@ typedef struct s_p11_attribctx {
242244
struct {
243245
CK_ATTRIBUTE *attrlist;
244246
size_t attrnum;
245-
} attrs[4];
247+
} attrs[4];
248+
249+
/* the following two members keep track of allowed mechanisms, when specified */
250+
CK_MECHANISM_TYPE_PTR allowedmechs;
251+
size_t allowedmechs_len;
246252
} attribCtx;
247253

248254
/* pkcs11_unwrap / pkcs11_wrap / pkcs11_wctx */
@@ -254,6 +260,11 @@ typedef struct s_p11_wrappedkeyctx {
254260
char *wrappedkeylabel; /* inner key only - outer key will have random name and ID */
255261

256262
char *filename; /* filename used to write wrapping file */
263+
264+
/* the following two members keep track of allowed mechanisms, when specified */
265+
CK_MECHANISM_TYPE_PTR allowedmechs;
266+
size_t allowedmechs_len;
267+
257268
struct { /* inner or outer but never both (by design) */
258269
CK_MECHANISM_TYPE aes_wrapping_mech; /* used when wrapping_meth is w_rfc3394 or w_rfc5649 */
259270
CK_BYTE_PTR iv; /* used for CKM_XXX_CBC_PAD and CKM_AES_KEY_WRAP_PAD */
@@ -644,6 +655,7 @@ CK_ATTRIBUTE_PTR pkcs11_get_attr_in_array ( CK_ATTRIBUTE_PTR array,
644655
bool pkcs11_read_attr_from_handle ( pkcs11AttrList *attrlist, CK_OBJECT_HANDLE handle);
645656
bool pkcs11_read_attr_from_handle_ext ( pkcs11AttrList *attrlist, CK_OBJECT_HANDLE handle, ... );
646657
bool pkcs11_attr_is_template(CK_ATTRIBUTE_TYPE attrtype);
658+
bool pkcs11_attr_is_allowed_mechanisms(CK_ATTRIBUTE_TYPE attrtype);
647659

648660
pkcs11AttrList *pkcs11_attrlist_extend(pkcs11AttrList *attrlist, CK_ATTRIBUTE_PTR attrs, CK_ULONG numattrs);
649661

@@ -712,10 +724,11 @@ CK_OBJECT_HANDLE pkcs11_import_component_final(KeyImportCtx *kctx);
712724

713725

714726
/* info functions */
715-
const char *get_mechanism_name(CK_MECHANISM_TYPE mech); /* pkcs11_mechanism.c */
716-
CK_ATTRIBUTE_TYPE get_attribute_type_from_name(char *name); /* pkcs11_attrdesc.c */
717-
const char *get_attribute_name_from_type(CK_ATTRIBUTE_TYPE attrtyp);
718-
727+
CK_MECHANISM_TYPE pkcs11_get_mechanism_type_from_name(char *name); /* pkcs11_mechanism.c */
728+
const char *pkcs11_get_mechanism_name_from_type(CK_MECHANISM_TYPE mech); /* pkcs11_mechanism.c */
729+
CK_ATTRIBUTE_TYPE pkcs11_get_attribute_type_from_name(char *name); /* pkcs11_attrdesc.c */
730+
const char *pkcs11_get_attribute_name_from_type(CK_ATTRIBUTE_TYPE attrtyp); /* pkcs11_attrdesc.c */
731+
719732
func_rc pkcs11_info_library(pkcs11Context *p11Context);
720733
func_rc pkcs11_info_slot(pkcs11Context *p11Context);
721734
func_rc pkcs11_info_ecsupport(pkcs11Context *p11Context);
@@ -740,6 +753,10 @@ const CK_OBJECT_HANDLE pkcs11_get_publickeyhandle(wrappedKeyCtx *ctx);
740753

741754
wrappedKeyCtx *pkcs11_new_wrappedkeycontext(pkcs11Context *p11Context);
742755
void pkcs11_free_wrappedkeycontext(wrappedKeyCtx *wctx);
756+
CK_MECHANISM_TYPE_PTR pkcs11_wctx_get_allowed_mechanisms(wrappedKeyCtx *ctx);
757+
size_t pkcs11_wctx_get_allowed_mechanisms_len(wrappedKeyCtx *ctx);
758+
void pkcs11_wctx_free_mechanisms(wrappedKeyCtx *wctx); /* to free allowed mechanisms */
759+
void pkcs11_wctx_forget_mechanisms(wrappedKeyCtx *wctx); /* for transfer of ownership */
743760

744761
/* pkcs11_attribctx */
745762
attribCtx *pkcs11_new_attribcontext();
@@ -749,6 +766,12 @@ CK_ATTRIBUTE_PTR pkcs11_get_attrlist_from_attribctx(attribCtx *ctx);
749766
size_t pkcs11_get_attrnum_from_attribctx(attribCtx *ctx);
750767
void pkcs11_adjust_attrnum_on_attribctx(attribCtx *ctx, size_t value);
751768

769+
func_rc pkcs11_attribctx_add_mechanism(attribCtx *ctx, CK_MECHANISM_TYPE attrtype);
770+
func_rc pkcs11_attribctx_free_mechanisms(attribCtx *ctx);
771+
void pkcs11_attribctx_forget_mechanisms(attribCtx *ctx);
772+
CK_MECHANISM_TYPE_PTR pkcs11_attribctx_get_allowed_mechanisms(attribCtx *ctx);
773+
size_t pkcs11_attribctx_get_allowed_mechanisms_len(attribCtx *ctx);
774+
752775

753776
/* End - Function Prototypes */
754777

lib/Makefile.am

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ wrappedkey_helper.c pkcs11_wrap.c pkcs11_unwrap.c pkcs11_wctx.c: \
110110
wrappedkey_parser.c wrappedkey_parser.h
111111

112112
# The following files depends upon lexer and parser source files
113+
# note: _lexermech.h is actually needed by attribctx_lexer.l,
114+
# but this dependency is not set directly, as it would lead
115+
# to systematically invoke flex. We put it on the produced files instead.
113116
attribctx_lexer.c attribctx_lexer.h: attribctx_lexer.l
114117
attribctx_parser.c attribctx_parser.h: attribctx_parser.y
115118

@@ -133,3 +136,4 @@ _attrinfo.h: Makefile gen_attrinfo_h.pl
133136
>$@
134137

135138

139+

lib/attribctx_helper.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ static int compare_CKA( const void *a, const void *b)
4040
return ((CK_ATTRIBUTE_PTR)a)->type == ((CK_ATTRIBUTE_PTR)b)->type ? 0 : -1;
4141
}
4242

43+
/* append an attribute to the attribute context */
44+
/* when the attribute is a template, the buffer is simply transmitted (as it remains within the attribctx structure) */
45+
/* when the attribute is CKM_ALLOWED_MECHANISMS, the buffer is stolen (note that the caller must free it) */
46+
/* when the attribute is not a template attribute, the buffer is copied */
47+
4348
func_rc _attribctx_parser_append_attr(attribCtx *clctx, CK_ATTRIBUTE_TYPE attrtyp, void *buffer, size_t len)
4449
{
4550
func_rc rc = rc_ok;
@@ -56,7 +61,7 @@ func_rc _attribctx_parser_append_attr(attribCtx *clctx, CK_ATTRIBUTE_TYPE attrty
5661
/* we need to create the buffer and stuff it with what is passed as parameter */
5762
stuffing.type = attrtyp;
5863

59-
if(pkcs11_attr_is_template(attrtyp)) {
64+
if(pkcs11_attr_is_template(attrtyp) || pkcs11_attr_is_allowed_mechanisms(attrtyp)) {
6065
stuffing.pValue = buffer; /* we pass the pointer, we don't allocate */
6166
} else {
6267
stuffing.pValue = malloc(len);
@@ -87,8 +92,10 @@ func_rc _attribctx_parser_append_attr(attribCtx *clctx, CK_ATTRIBUTE_TYPE attrty
8792

8893
*attrnum = argnum; /* trick to adapt on 32 bits architecture, as size(CK_ULONG)!=sizeof int */
8994

90-
if( match == &stuffing) { /* match, we may need to adjust the content */
91-
if(match->pValue && !pkcs11_attr_is_template(match->type)) { free(match->pValue); /* just in case */ }
95+
if(match == &stuffing) { /* match, we may need to adjust the content */
96+
if(match->pValue != NULL && !pkcs11_attr_is_template(match->type)) {
97+
free(match->pValue); /* just in case */
98+
}
9299

93100
match->ulValueLen = stuffing.ulValueLen;
94101
match->pValue = stuffing.pValue; /* we steal the pointer */
@@ -102,7 +109,11 @@ func_rc _attribctx_parser_append_attr(attribCtx *clctx, CK_ATTRIBUTE_TYPE attrty
102109

103110
error:
104111
/* clean up */
105-
if (stuffing.pValue != NULL && !pkcs11_attr_is_template(stuffing.type)) { free(stuffing.pValue); }
112+
if(stuffing.pValue != NULL
113+
&& !pkcs11_attr_is_template(stuffing.type)
114+
&& !pkcs11_attr_is_allowed_mechanisms(stuffing.type)) {
115+
free(stuffing.pValue);
116+
}
106117

107118
return rc;
108119
}
@@ -158,4 +169,5 @@ func_rc _attribctx_parser_assign_list_to_template(attribCtx *clctx, CK_ATTRIBUTE
158169
}
159170

160171

172+
161173
/* EOF */

0 commit comments

Comments
 (0)