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

libptateec: manufacturing protection PTA #352

Closed
wants to merge 1 commit into from

Conversation

ldts
Copy link
Contributor

@ldts ldts commented Aug 24, 2023

This abstraction provides TEEC access to stable PTAs present in the OP-TEE upstream tree.

The first of these miscellaenous PTAs to be integrated in the library is the iMX Manufacturing Protection [1] for which two functions are provided:

  • Retrieval of the EC Public Key.
  • Signature generation.

In order to access the service provided by the manufacturing protection PTA, OP-TEE needs to be built with this option OP-TEE/optee_os#6274

[1] AN13676, i.MX RT1170 Manufacturing Protection

@ldts ldts force-pushed the libptateec-up branch 2 times, most recently from b13f83f to 25cb412 Compare August 24, 2023 10:50
ldts added a commit to foundriesio/lmp-device-register that referenced this pull request Aug 24, 2023
This option allows lmp-device-register to interrogate the CAAM hardware
for an EC public key (256 bits usually)

The key will be stored in /var/sota in PEM format (for reference) and
passed to the gateway in DER format.

Afer registration aktualizer-lite should periodically request the CAAM
hardware to sign random strings and send these signatures to the gateway
along with the message.

See [1] for optee-client reference.

Upon receiving these digests, the gateway shall verify them using the
board associated public key

[1] OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to foundriesio/lmp-device-register that referenced this pull request Aug 25, 2023
This option allows lmp-device-register to interrogate the CAAM hardware
for an EC public key (256 bits usually)

The key will be stored in /var/sota in PEM format (for reference) and
passed to the gateway in DER format.

Afer registration aktualizer-lite should periodically request the CAAM
hardware to sign random strings and send these signatures to the gateway
along with the message.

See [1] for optee-client reference.

Upon receiving these digests, the gateway shall verify them using the
board associated public key

[1] OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to foundriesio/lmp-device-register that referenced this pull request Aug 28, 2023
This option allows lmp-device-register to interrogate the CAAM hardware
for an EC public key (256 bits usually)

The key will be stored in /var/sota in PEM format (for reference) and
passed to the gateway.

Afer registration aktualizer-lite should periodically request the CAAM
hardware to sign random strings and send these signatures to the gateway
along with the message.

See [1] for optee-client reference.

Upon receiving these digests, the gateway shall verify them using the
board associated public key

[1] OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to foundriesio/lmp-device-register that referenced this pull request Aug 28, 2023
This option allows lmp-device-register to interrogate the CAAM hardware
for an EC public key (256 bits usually)

The key will be stored in /var/sota in PEM format (for reference) and
passed to the gateway.

Afer registration aktualizer-lite should periodically request the CAAM
hardware to sign random strings and send these signatures to the gateway
along with the message.

See [1] for optee-client reference.

Upon receiving these digests, the gateway shall verify them using the
board associated public key

[1] OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to foundriesio/lmp-device-register that referenced this pull request Aug 28, 2023
This option allows lmp-device-register to interrogate the CAAM hardware
for an EC public key (256 bits usually)

The key will be stored in /var/sota in PEM format (for reference) and
passed to the gateway.

Afer registration aktualizer-lite should periodically request the CAAM
hardware to sign random strings and send these signatures to the gateway
along with the message.

See [1] for optee-client reference.

Upon receiving these digests, the gateway shall verify them using the
board associated public key

[1] OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to foundriesio/lmp-device-register that referenced this pull request Aug 29, 2023
This option allows lmp-device-register to interrogate the CAAM hardware
for an EC public key (256 bits usually)

The key will be stored in /var/sota in PEM format (for reference) and
passed to the gateway in the CSR using a custom extension [1]

Afer registration aktualizer-lite should periodically request the CAAM
hardware to sign random strings and send these signatures to the gateway
along with the message.

See [2] for optee-client reference.

Upon receiving these digests, the gateway shall verify them using the
board associated public key

[1] "1.3.6.1.4.1.294.1.00"
[2] OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to ldts/optee_os that referenced this pull request Aug 31, 2023
Allow accessing the PTA without a session. Users could then use the
OP-TEE client interface to retrieve the public key as well as to
generate signatures.

See OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to ldts/optee_os that referenced this pull request Aug 31, 2023
Allow opening the PTA without a calling session.

Enabling CFG_NXP_CAAM_MP_NO_ACCESS_CTR permits users to use the OP-TEE
client interface to retrieve the public key as well as to generate
signatures.

See OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to ldts/optee_os that referenced this pull request Aug 31, 2023
Allow opening the PTA without a calling session.

Enabling CFG_NXP_CAAM_MP_NO_ACCESS_CTRL permits users to use the OP-TEE
client interface to retrieve the public key as well as to generate
signatures.

See OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
ldts added a commit to ldts/optee_os that referenced this pull request Sep 4, 2023
Allow opening the PTA without a calling session.

Enabling CFG_NXP_CAAM_MP_NO_ACCESS_CTRL permits users to use the OP-TEE
client interface to retrieve the public key as well as to generate
signatures.

See OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
Acked-by: Clement Faure <[email protected]>
jforissier pushed a commit to OP-TEE/optee_os that referenced this pull request Sep 4, 2023
Allow opening the PTA without a calling session.

Enabling CFG_NXP_CAAM_MP_NO_ACCESS_CTRL permits users to use the OP-TEE
client interface to retrieve the public key as well as to generate
signatures.

See OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
Acked-by: Clement Faure <[email protected]>
@ldts
Copy link
Contributor Author

ldts commented Sep 5, 2023

any comments?

#endif

typedef unsigned long PTA_ULONG;
typedef PTA_ULONG PTA_RV;
Copy link
Contributor

Choose a reason for hiding this comment

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

What's wrong with TEEC_Result?

Copy link
Contributor Author

@ldts ldts Sep 6, 2023

Choose a reason for hiding this comment

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

We dont know and can't foresee what the PTAs will choose to report..errors could have different meanings depending the calls made to the TEE I guess (so the same TEEC_Result could mean something different for different function calls). having another indirection adds flexibility.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're only supporting upstream PTAs here we can encourage them to use TEEC_Result error codes, and if needed pick values in the 0x80000000 – 0x8FFFFFFF range for an occasional extension.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so shall I get rid of the typedef and just cast in case of needing to extend ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please.

xiaoxuZeng pushed a commit to xiaoxuZeng/optee_os that referenced this pull request Sep 7, 2023
Allow opening the PTA without a calling session.

Enabling CFG_NXP_CAAM_MP_NO_ACCESS_CTRL permits users to use the OP-TEE
client interface to retrieve the public key as well as to generate
signatures.

See OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
Acked-by: Clement Faure <[email protected]>
@ldts ldts force-pushed the libptateec-up branch 3 times, most recently from f5a7252 to 33d9c0e Compare September 15, 2023 08:19
@ldts
Copy link
Contributor Author

ldts commented Sep 15, 2023

actioned.

if (!key || !len || !*len)
return TEEC_ERROR_BAD_PARAMETERS;

if (!pta_open_session(&manufacturing_protection_ta_ctx))
Copy link
Contributor

Choose a reason for hiding this comment

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

How is the session closed?

Copy link
Contributor Author

@ldts ldts Sep 21, 2023

Choose a reason for hiding this comment

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

um I didnt think it matter to keep an open session for all calls.

The way I thought the user (the process that communicates with the remote server, responding to requests and so on) would access the call is by periodically signing random values provided by the server (thus generating some sort of attestation token)

The key would only be retrieved once and would be sent to the server in a secure environment during provisioning (server = verifier)

Copy link
Contributor

Choose a reason for hiding this comment

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

Leaving a session open wastes memory so there should be a way to close the session without killing the process.

Copy link
Contributor Author

@ldts ldts Sep 22, 2023

Choose a reason for hiding this comment

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

sure, ok let me address that but it will not be as nice in terms of usability - many threads could be accessing the PTA so with the capability of closing the sessions now locking is a must

LeiSen62 pushed a commit to LeiSen62/optee_os that referenced this pull request Sep 25, 2023
Allow opening the PTA without a calling session.

Enabling CFG_NXP_CAAM_MP_NO_ACCESS_CTRL permits users to use the OP-TEE
client interface to retrieve the public key as well as to generate
signatures.

See OP-TEE/optee_client#352

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
Acked-by: Clement Faure <[email protected]>
Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

LGTM aside the few comments.

* pta_imx_mprotect_get_key() - Retrieves the iMX CAAM Manufacturing Protection
* EC public key. The components x,y are retrieved in RAW format and should
* be converted to DER or PEM as required.
*
Copy link
Contributor

Choose a reason for hiding this comment

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

could you describe the arguments?

#include <tee_client_api.h>
#include <teec_trace.h>

struct ta_context {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also add a description for struct and API functions?

op.params[1].tmpref.buffer = sig;
op.params[1].tmpref.size = *sig_len;
op.params[2].tmpref.buffer = mpmr;
op.params[2].tmpref.size = *mpmr_len;
Copy link
Contributor

Choose a reason for hiding this comment

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

2 space chars

* [out] memref[0].buffer Public key buffer
* [out] memref[0].size Public key size
*
* Return codes:
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest you remove this list. The command can return more codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no I dont think it can. I think we should keep this.

Copy link
Contributor

Choose a reason for hiding this comment

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

It looks the PTA can also return TEE_ERROR_SHORT_BUFFER, TEE_ERROR_OUT_OF_MEMORY, TEE_ERROR_NOT_SUPPORTED or TEE_ERROR_GENERIC.

* Copyright (c) 2023, Foundries.io Ltd
*/
#ifndef PTA_H
#define PTA_H
Copy link
Contributor

Choose a reason for hiding this comment

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

PTA_H seems too short to me for a header file exposed to other numerous libs.
What about PTATEEC_PTA_H

* requiring at least a 64 byte buffer.
*
* @key: [out] Public key in RAW format.
* @len: [in/out] Key length.
Copy link
Contributor

Choose a reason for hiding this comment

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

in bytes

* @return TEEC_ERROR_GENERIC Error unspecified.
* @return TEEC_ERROR_SHORT_BUFFER Error small buffer provided.
* @return TEEC_ERROR_COMMUNICATION Some other thread closed the connection.
* @return TEEC_SUCCESS On success.
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add TEE_ERROR_OUT_OF_MEMORY
ditto for pta_imx_mprotect_sign().

* pta_imx_mprotect_final() - Closes the OP-TEE session
*
* This function may fail with TEEC_ERROR_BUSY if there are unfulfilled calls
* pending
Copy link
Contributor

Choose a reason for hiding this comment

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

period

}
pthread_mutex_unlock(&ctx->lock);
ret = TEEC_InvokeCommand(&ctx->session, cmd_id, op, error_origin);
atomic_fetch_sub(&ctx->count, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

This means 1 call to pta_open_session() must be balanced by a call to pta_invoke_cmd().

Copy link
Contributor

Choose a reason for hiding this comment

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

If so, I think there is no need to have 2 functions. A single call to pta_invoke_cmd() should be is sufficient to open the context session and invoke a PTA. It's a bit strange of a PTA API to require a call to open_session for each calls to invoke_command.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I think it's a bit weird that a generic PTA invocation API requires each call to pta_open_session() be always followed by a single call to pta_invoke_cmd().

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this session handling is a bit more complicated than needed with mutex and reference counter. Couldn't we skip it all and rely on the normal TEE Client primitives?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO being capable of rejecting a close request coming from some separate thread because an invoke is about to follow is a nice feature to have (it really only costs the mutex and reference counter and it has no impact on performance).

But sure I can remove it if you think it adds too much maintenance noise.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove it then.


/**
* pta_open_session() - Opens a session with the PTA uuid in the ta_context.
* If the session is already open it will increment a session counter.
Copy link
Contributor

Choose a reason for hiding this comment

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

 * @ctx Session context, already opened ornot
 * Field @ctx->uuid defines the target PTA

/**
* pta_invoke_cmd() - Invokes a command in the PTA
*/
TEEC_Result pta_invoke_cmd(struct ta_context *ctx, uint32_t cmd_id,
Copy link
Contributor

Choose a reason for hiding this comment

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

 * @ctx Opened session context
 * @cmd_id Command passed to target PTA
 * @operation TEE operation arguments passed to target PTA
 * @error_origin: Output TEE_ORIGIN_* emitter of the result 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.

thanks

This abstraction provides TEEC access to stable PTAs present in the
OP-TEE upstream tree.

The first of these miscellaenous PTAs to be integrated in the library
is the iMX Manufacturing Protection [1] for which two functions are
provided:
  - Retrieval of the EC Public Key.
  - Signature generation.

[1] AN13676, i.MX RT1170 Manufacturing Protection

Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
@ldts
Copy link
Contributor Author

ldts commented Oct 3, 2023

@etienne-lms @jenswi-linaro anything else?

Copy link

github-actions bot commented Nov 4, 2023

This pull request has been marked as a stale pull request because it has been open (more than) 30 days with no activity. Remove the stale label or add a comment, otherwise this pull request will automatically be closed in 5 days. Note, that you can always re-open a closed issue at any time.

@github-actions github-actions bot added the Stale label Nov 4, 2023
@ldts ldts closed this Nov 7, 2023
@jenswi-linaro
Copy link
Contributor

I guess this wasn't needed after all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants