Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pkcs#11 services through OP-TEE SKS TA #121

Closed
wants to merge 13 commits into from

Conversation

etienne-lms
Copy link
Contributor

@etienne-lms etienne-lms commented May 22, 2018

This changes introduces an implementation of the Pkcs#11 Cryptoki library that interface a specific trusted application in the secure side.

This P-R implements the basics for the client library. The effective implementation of the services will come later.

This P-R does not introduce the TA API itself that is being reviewed through OP-TEE/optee_os#2342. The client lib will integrate the TA API once its is approved and merged.

@@ -0,0 +1,265 @@
/* Copyright (c) OASIS Open 2016. All Rights Reserved./
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SPDX header please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the OASIS IPR Policy is not registered to SPDX.
Tricky...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i will go another way.
The opencryptoki project includes a package (opencryptoki) that proposes a pkcs11 cryptoki header under the creative commons license, here.

The header file reflects a slightly older pkcs11 and must be updated with more recent IDs and few crypto parameter structures. From license view, it is more suitable for a project like the OP-TEE. @jenswi-linaro, @jforissier, what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[It is the "Common Public License, version 1.0 (CPL-1.0)", SPDX identifier "CPL-1.0". Nothing to do with the Creative Commons licenses AFAICT.]

I think we may have a problem with sections 2.a and 3.a.iv of https://spdx.org/licenses/CPL-1.0.html (source code).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, CPL-1.0, thanks for fixing. From wikipedia, CPL-1.0 is not a good choice.

CPL lacks compatibility with both versions of the GPL because it has a "choice of law" section in section 7, which restricts legal disputes to a certain court. Another source of incompatibility is the differing copyleft requirements. [2]

Rewrite from scratch based on the specs?

Copy link
Contributor

@jbech-linaro jbech-linaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrite from scratch based on the specs?

If we cannot include it as it is, then I think that rewrite is the only option we have. I just wonder, wouldn't you end up with almost the same kind of defines etc in the end.

*/
#include "pkcs11t.h"

#define __PASTE(x,y) x##y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to __JOIN (or __CONCATENATE) might better describe what this actually does?

@etienne-lms
Copy link
Contributor Author

If we cannot include it as it is, then I think that rewrite is the only option we have. I just wonder, wouldn't you end up with almost the same kind of defines etc in the end.

Yes, same IDs, labels, structures and prototypes...
I have started, it's a bit painful... will restrict to what is currently supported/planned for SKS.

@etienne-lms
Copy link
Contributor Author

Pushed proposal for a new pkcs11.h header file implementation.

Implement  pkcs11.h header file that partially covers the PKCS#11
specification. Resources initially planned to be supported are defined.
The header will need to be updated with remaining PKCS#11 definition
when related support will be implemented.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Define the few platform macros expected by the cryptolib header files.
Initial source file: the API functions from pkcs11_api.c.

Build from Makefile or from CMake.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
LOG_ERROR(), LOG_INFO, LOG_DEBUG() currently all calling printf().
ASSERT() exits process on error.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
PKCS#11 specifies library must be initialized prior being used
but for 2 APIs: C_Initialize and C_GetFunctionList.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
@etienne-lms
Copy link
Contributor Author

Comments addressed and squashed.

Copy link
Contributor

@jforissier jforissier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure several commits are justified instead of one single "Add stubbed liboptee_cryptoki"?

#include <stdio.h>
#include <stdlib.h>

#define ASSERT(cond) \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use assert() from <assert.h> directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. thanks.

/*
* Copyright (c) 2017, Linaro Limited
*
* SPDX-License-Identifier: BSD-2-Clause
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// SPDX-License-Identifier: BSD-2-Clause on first line? (should we follow the same rules as in optee_os?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not mandated but ok.

do { \
if (!ptr) \
return CKR_ARGUMENTS_BAD; \
} while (0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i will go another way. thanks to point out.

Makefile Outdated
@@ -129,3 +137,6 @@ copy_export: build
cp -a ${O}/libteec/libteec.a $(DESTDIR)$(LIBDIR)
cp ${O}/tee-supplicant/tee-supplicant $(DESTDIR)$(BINDIR)
cp public/*.h $(DESTDIR)$(INCLUDEDIR)
cp libsks/include/*.h $(DESTDIR)$(INCLUDEDIR)
cp -a ${O}/libsks/liboptee_cryptoki.so* $(DESTDIR)$(LIBDIR)
Copy link
Contributor

@jforissier jforissier Jun 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that the library is not called libsks, or rather, that the targets build-libsks and clean-libsks are not called build-liboptee_cryptoki and clean-liboptee_cryptoki, is mildly disturbing. It diverges from libteec.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. better be cleaned now. yet it find name libsks a bit short for a quite specific library (optee/pkcs11).

@etienne-lms
Copy link
Contributor Author

Not sure several commits are justified instead of one single "Add stubbed liboptee_cryptoki"?

True. I have split initial work into smaller change too easy review. Maybe too much...
I can squash in one if you feel it is better and safer.

Rename library optee_cryptoki into library sks.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Fix CMake script for header files to no install the CMakeLists.txt file.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
@etienne-lms
Copy link
Contributor Author

all comments not addressed yet. in progress...

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Do not use BIT. Some components as xtest build with an util.h from
optee_os that defines macro BIT() whereas other components can
build libsks (and libteec, ...) without relynig on a prebuilt optee_os
that provides such util.h.

As a consequence, use (1U << pos) rather than BIT(pos) in the code.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
Remove ASSERT(). Will use the generic assert() from assert.h when needed.
Fix notice.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
fix notice

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
@etienne-lms
Copy link
Contributor Author

Comments addressed, but the one about squashing all the commits into a single one.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>

static int inited;

static int sanity_lib_init(void)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove. This function sounds like it initializes the library, but actually it does not (C_Initialize() does).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

return inited == 0;
}

static int sanity_nonnull_ptr(void *ptr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

{
(void)res;

if (sanity_lib_init())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!inited)

What about thread safety? Is the API supposed to be thread-safe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but I would rather postpone this issue to later...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

CK_RV C_GetFunctionList(CK_FUNCTION_LIST_PTR_PTR ppFunctionList)
{
/* Note: no need to call sanity_lib_init() here */
if (sanity_nonnull_ptr(ppFunctionList))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!ppFunctionList)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Drop sanity_lib_init()/sanity_nonnull_ptr() in favor to inline
instructions.

Signed-off-by: Etienne Carriere <etienne.carriere@linaro.org>
/* Argument currently unused */
(void)args;

if (lib_inited)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inited? What about ...initiated? Some comment for the other places further down.

@jbech-linaro
Copy link
Contributor

Besides a minor suggestion, I think this looks good. Then we have the golden question, whether to merge something in the middle of being developed or not. It would be nice to live up to "submit early and often" and get pieces of the complete implementation merged, one by one ... @OP-TEE/linaro thoughts?

@jforissier
Copy link
Contributor

@jbech-linaro generally speaking, my opinion is we should "submit early and often" but into a separate branch, until the code actually provides a real service or at least can be somewhat tested. If I understand correctly, it is the case for this PKCS#11 stuff already?
Anyway, I don't feel strongly about it, and I will not object to merging code into master as long as it has been sufficiently reviewed.

@etienne-lms
Copy link
Contributor Author

@jforissier, yes this P-R comes with related P-R in optee_os and optee_test and do provide some test materials. All these P-R rely on configuration directive CFG_SECURE_KEY_SERVICES to build related code in each component. The currently proposed build sequence (OP-TEE/build#261) is independent of this P-R.
Anyway, i strongly agree with "I will not object to merging code into master as long as it has been sufficiently reviewed.".

@jforissier
Copy link
Contributor

@etienne-lms all good then, thanks. I will take all this as soon as 3.2.0 is out, which may be in a couple of days or so (I'd like to better understand OP-TEE/optee_os#2414 and OP-TEE/optee_os#2437).

@etienne-lms
Copy link
Contributor Author

@jforissier, fine with me.

@etienne-lms
Copy link
Contributor Author

Superseded by #138.

@etienne-lms etienne-lms deleted the sks-merge branch October 30, 2020 18:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants