controlse: add accesslib for the se05x secure element

This commit is contained in:
Andre Heinemans 2024-04-19 12:29:05 +02:00 • committed by Xiang Xiao
parent fe12ad9444
commit cecc2e762b
28 changed files with 3335 additions and 1495 deletions

View file

@ -0,0 +1,122 @@
//***************************************************************************
// apps/include/crypto/controlse/ccertificate.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//***************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include "crypto/controlse/isecure_element_object.hxx"
#include <mbedtls/x509_crt.h>
//***************************************************************************
// Class definitions
//***************************************************************************
namespace Controlse
{
class ISecureElement;
class CPublicKey;
class CSerialNumber;
class CCertificate : public ISecureElementObject
{
public:
CCertificate(const ISecureElement &se, uint32_t keystore_id);
CCertificate(const uint8_t *crt_der_or_pem, size_t crt_size);
CCertificate(const ISecureElement &se, const uint8_t *csr_der_or_pem,
size_t csr_size, uint32_t keystore_id);
// from_datetime and to_datetime need to have format: YYYYMMDDHHMMSSZ
CCertificate(const ISecureElement &se, const uint8_t *csr_der_or_pem,
size_t csr_size, uint32_t keystore_id,
const char *from_datetime, const char *to_datetime);
CCertificate(const CCertificate &) = delete;
CCertificate(CCertificate &&) = default;
~CCertificate();
CCertificate &operator=(const CCertificate &other) = delete;
bool IsLoaded() const;
bool StoreOnSecureElement(const ISecureElement &se,
uint32_t keystore_id) const;
bool LoadFromSecureElement(const ISecureElement &se, uint32_t keystore_id);
bool LoadFromDerOrPem(const uint8_t *crt_der_or_pem, size_t crt_size);
bool LoadFromCsrDerOrPem(const ISecureElement &se,
const uint8_t *csr_der_or_pem, size_t csr_size,
uint32_t keystore_id, const char *from_datetime,
const char *to_datetime);
bool VerifyAgainst(const ISecureElement &se,
uint32_t verify_against_id) const;
// Test time range is valid
// returns 0 if valid
// -1 when expired
// 1 when not yet valid
int TestValidTimerange(time_t now) const;
// Get public key from certificate
// returns pointer to public key when successful otherwise NULL
// note: must be deleted by caller when not NULL
CPublicKey *GetPublicKey() const;
// Get oid from certificate if available
// oid must be one of MBEDTLS_OID_AT* from mbedtls/oid.h
//
// returns zero terminated text string when successful otherwise NULL
// note: must be deleted by caller when not NULL
char *GetOid(const char *oid) const;
// Get serial number from from certificate
// returns pointer to CSerialNumber when successful otherwise NULL
// note: must be deleted by caller when not NULL
CSerialNumber *GetSerialNumber() const;
size_t GetNumberOfSubjectAlternativeNames() const;
// Get SAN from from certificate
// returns pointer to array when successful otherwise NULL
// note: must be deleted by caller when not NULL
char *GetSubjectAlternativeName(int item) const;
// Get certificate in DER format
// returns size of the der array otherwise 0
// note: der must be deleted by caller when not NULL
size_t GetDer(uint8_t **der) const;
// Get certificate in PEM format
// returns pointer to pem string when successful otherwise NULL
// note: must be deleted by caller when not NULL
char *GetPem() const;
bool ContainsSan(const char *name, size_t size) const;
static constexpr char TAG_ID_SIZE = 18;
private:
bool is_loaded = false;
mbedtls_x509_crt crt;
};
} // namespace Controlse

View file

@ -0,0 +1,92 @@
//***************************************************************************
// apps/include/crypto/controlse/ccsr.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include "crypto/controlse/isecure_element_object.hxx"
#include "mbedtls/x509_csr.h"
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class CCsr : public ISecureElementObject
{
public:
class CsrBuilder;
class CsrBuilder
{
public:
CsrBuilder(ISecureElement &se, const char *subject, uint32_t key_slot_id);
CsrBuilder(const CsrBuilder &) = delete;
CsrBuilder(CsrBuilder &&) = default;
CsrBuilder &operator=(const CsrBuilder &other) = delete;
CsrBuilder *AddExtension(const char *oid, size_t oid_size,
const uint8_t *value, size_t value_size);
// result: pointer to dynamically allocated Csr (to be deleted) or nullptr
// if error
CCsr *Build();
private:
mbedtls_x509write_csr csr_w;
mbedtls_pk_context key;
bool ready;
};
CCsr(const ISecureElement &se, uint32_t keystore_id);
CCsr(const uint8_t *der_or_pem, size_t size);
CCsr(const CCsr &) = delete;
CCsr(CCsr &&) = default;
~CCsr();
CCsr &operator=(const CCsr &other) = delete;
bool IsLoaded() const;
bool StoreOnSecureElement(const ISecureElement &se,
uint32_t keystore_id) const;
bool LoadFromSecureElement(const ISecureElement &se, uint32_t keystore_id);
// Get CSR in DER format
// returns size of the der array otherwise 0
// note: der must be deleted by caller when not NULL
size_t GetDer(uint8_t **der) const;
// Get certificate in PEM format
// returns pointer to pem string when successful otherwise NULL
// note: must be deleted by caller when not NULL
char *GetPem() const;
private:
mbedtls_x509_csr csr;
bool is_loaded = false;
};
} // namespace Controlse

View file

@ -0,0 +1,62 @@
//***************************************************************************
// apps/include/crypto/controlse/chex_util.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include <cstddef>
#include <cstdint>
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class CHexUtil
{
public:
static size_t GetByteArraySizeFromHexString(const char *hex_buffer);
static size_t GetByteArraySizeFromHexStringSize(size_t hex_buffer_size);
static size_t GetHexStringSizeFromByteArraySize(size_t byte_array_size);
// result contains allocated pointer to byte array (delete[] afterwards) if
// successfull otherwise nullptr
static uint8_t *ConvertHexStringToByteArray(const char *hex_buffer);
// result contains allocated pointer to byte array (delete[] afterwards) if
// successfull otherwise nullptr
static uint8_t *ConvertHexStringToByteArray(const char *hex_buffer,
size_t hex_buffer_size);
// result contains allocated pointer to hex string (delete[] afterwards) if
// successfull otherwise nullptr
static char *ByteArrayToHexString(const uint8_t bytearray[], size_t size);
private:
static constexpr size_t AMOUNT_OF_HEXDIGITS_PER_BYTE = 2;
};
} // namespace Controlse

View file

@ -0,0 +1,82 @@
//***************************************************************************
// apps/include/crypto/controlse/cpublic_key.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include "crypto/controlse/isecure_element_object.hxx"
#include <stddef.h>
struct mbedtls_x509_crt;
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class ISecureElement;
class CPublicKey : public ISecureElementObject
{
public:
CPublicKey(const ISecureElement &se, uint32_t keystore_id);
CPublicKey(const uint8_t *buffer, size_t buffer_size);
CPublicKey(const char *pem);
CPublicKey(const CPublicKey &p1);
~CPublicKey();
CPublicKey &operator=(const CPublicKey &other);
bool IsLoaded() const;
bool StoreOnSecureElement(const ISecureElement &se,
uint32_t keystore_id) const;
bool LoadFromSecureElement(const ISecureElement &se, uint32_t keystore_id);
bool operator==(const CPublicKey &a) const;
bool operator!=(const CPublicKey &a) const;
size_t GetRawSize() const;
void GetRaw(uint8_t *raw_buffer) const;
// Get public key in PEM format
// returns pointer to pem string when successful otherwise NULL
// note: must be deleted by caller when not NULL
char *GetPem() const;
private:
void Unload();
static int convert_public_key_raw_to_pem(char *pem_buf, size_t pem_buf_size,
const uint8_t *key_buf,
size_t key_buf_size);
static int convert_public_key_pem_to_raw(uint8_t *key_buf,
size_t key_buf_size,
size_t *key_size,
const char *pem_buf);
uint8_t *m_key = nullptr;
size_t m_size = 0;
};
} // namespace Controlse

View file

@ -0,0 +1,69 @@
//***************************************************************************
// apps/include/crypto/controlse/csan_builder.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include <stddef.h>
#include <stdint.h>
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class CSanBuilder;
class CSanBuilder
{
public:
CSanBuilder() = default;
CSanBuilder(const CSanBuilder &) = delete;
CSanBuilder(CSanBuilder &&) = default;
~CSanBuilder();
CSanBuilder &operator=(const CSanBuilder &other) = delete;
CSanBuilder *AddSan(uint8_t type, const char *value, size_t value_size);
// result: pointer to dynamically allocated san (to be deleted with delete[])
// or error when size == 0
size_t Build(uint8_t **san);
uint32_t GetNumberOfSan();
private:
struct List
{
char *p;
size_t size;
uint8_t type;
List *next;
};
size_t total_size = 0;
List *entry = nullptr;
};
} // namespace Controlse

View file

@ -0,0 +1,73 @@
//***************************************************************************
// apps/include/crypto/controlse/csecure_element.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include "crypto/controlse/isecure_element.hxx"
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class CCertificate;
class CertificateCatalog;
class CPublicKey;
class CSecureElement : public ISecureElement
{
public:
explicit CSecureElement(const char *se05x_device);
explicit CSecureElement(int fd);
CSecureElement(const CSecureElement &) = delete;
CSecureElement(CSecureElement &&) = default;
~CSecureElement();
CSecureElement &operator=(const CSecureElement &other) = delete;
bool IsReady() const;
bool GenerateKey(struct se05x_generate_keypair_s &args) const;
bool SetKey(struct se05x_key_transmission_s &args) const;
bool GetKey(struct se05x_key_transmission_s &args) const;
bool DeleteKey(uint32_t id) const;
bool SetData(struct se05x_key_transmission_s &args) const;
bool GetData(struct se05x_key_transmission_s &args) const;
bool CreateSignature(struct se05x_signature_s &args) const;
bool Verify(struct se05x_signature_s &args) const;
bool DeriveSymmetricalKey(struct se05x_derive_key_s &args) const;
bool GetUid(struct se05x_uid_s &args) const;
bool GetInfo(struct se05x_info_s &args) const;
CCertificate *GetCertificate(uint32_t keystore_id);
CPublicKey *GetPublicKey(uint32_t keystore_id);
private:
const int se05x_fd;
const bool close_device_at_destructor;
};
} // namespace Controlse

View file

@ -0,0 +1,62 @@
//***************************************************************************
// apps/include/crypto/controlse/cserial_number.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include "crypto/controlse/isecure_element_object.hxx"
#include <stddef.h>
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class CSerialNumber : ISecureElementObject
{
public:
static constexpr size_t SERIAL_NUMBER_SIZE = 20;
CSerialNumber(const ISecureElement &se, uint32_t keystore_id);
CSerialNumber(uint8_t const *serial_number_byte_array);
bool operator==(CSerialNumber &a) const;
bool operator!=(CSerialNumber &a) const;
bool operator<(const CSerialNumber &a) const;
bool IsLoaded() const;
bool StoreOnSecureElement(const ISecureElement &se,
uint32_t keystore_id) const;
bool LoadFromSecureElement(const ISecureElement &se, uint32_t keystore_id);
bool
GetSerialNumber(uint8_t serial_number_destination[SERIAL_NUMBER_SIZE]) const;
private:
bool is_loaded = false;
uint8_t serial_number[SERIAL_NUMBER_SIZE];
};
} // namespace Controlse

View file

@ -0,0 +1,64 @@
//***************************************************************************
// apps/include/crypto/controlse/cstring.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include "crypto/controlse/isecure_element_object.hxx"
#include <stddef.h>
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class CString : public ISecureElementObject
{
public:
CString(const ISecureElement &se, uint32_t keystore_id);
CString(char *string, size_t size);
CString(const CString &p1);
~CString();
CString &operator=(const CString &other);
bool operator==(CString &a) const;
bool operator!=(CString &a) const;
// return value is string otherwise nullptr
// NOTE: need to delete[] return value
char *c_str(void) const;
bool IsLoaded() const;
bool StoreOnSecureElement(const ISecureElement &se,
uint32_t keystore_id) const;
bool LoadFromSecureElement(const ISecureElement &se, uint32_t keystore_id);
private:
size_t m_size = 0;
char *m_string = nullptr;
};
} // namespace Controlse

View file

@ -0,0 +1,71 @@
//***************************************************************************
// apps/include/crypto/controlse/isecure_element.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
//***************************************************************************
// Included Files
//***************************************************************************
#pragma once
#include "crypto/controlse/isecure_element_object.hxx"
#include <nuttx/crypto/se05x.h>
#include <stdint.h>
struct se05x_key_transmission_s;
struct se05x_signature_s;
struct se05x_uid_s;
struct se05x_info_s;
struct se05x_generate_keypair_s;
struct se05x_derive_key_s;
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class ISecureElement
{
public:
virtual ~ISecureElement() = default;
virtual bool IsReady() const = 0;
virtual bool GenerateKey(struct se05x_generate_keypair_s &args) const = 0;
virtual bool SetKey(struct se05x_key_transmission_s &args) const = 0;
virtual bool GetKey(struct se05x_key_transmission_s &args) const = 0;
virtual bool DeleteKey(uint32_t id) const = 0;
virtual bool SetData(struct se05x_key_transmission_s &args) const = 0;
virtual bool GetData(struct se05x_key_transmission_s &args) const = 0;
virtual bool CreateSignature(struct se05x_signature_s &args) const = 0;
virtual bool Verify(struct se05x_signature_s &args) const = 0;
virtual bool DeriveSymmetricalKey(struct se05x_derive_key_s &args) const = 0;
virtual bool GetUid(struct se05x_uid_s &args) const = 0;
virtual bool GetInfo(struct se05x_info_s &args) const = 0;
virtual bool Set(uint32_t keystore_id,
const ISecureElementObject &object) const
{
return object.StoreOnSecureElement(*this, keystore_id);
}
};
} // namespace Controlse

View file

@ -0,0 +1,51 @@
//***************************************************************************
// apps/include/crypto/controlse/isecure_element_object.hxx
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. The
// ASF licenses this file to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
//**************************************************************************
// Copyright 2024 NXP
#pragma once
//***************************************************************************
// Included Files
//***************************************************************************
#include <stdint.h>
namespace Controlse
{
//***************************************************************************
// Class definitions
//***************************************************************************
class ISecureElement;
class ISecureElementObject
{
public:
virtual ~ISecureElementObject() = default;
virtual bool IsLoaded() const = 0;
virtual bool StoreOnSecureElement(const ISecureElement &se,
uint32_t keystore_id) const = 0;
virtual bool LoadFromSecureElement(const ISecureElement &se,
uint32_t keystore_id)
= 0;
};
} // namespace Controlse