Skip to content

Get cloud jwt #25

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ jobs:
platforms: |
# Install Arduino mbed_nano Boards via Boards Manager
- name: arduino:mbed_nicla
libraries: |
- name: ArduinoECCX08
- board:
platform-name: arduino:mbed_opta
platforms: |
Expand All @@ -121,11 +123,15 @@ jobs:
platforms: |
# Install Arduino renesas_portenta Boards via Boards Manager
- name: arduino:renesas_portenta
libraries: |
- name: ArduinoECCX08
- board:
platform-name: arduino:renesas_uno
platforms: |
# Install Arduino renesas_uno Boards via Boards Manager
- name: arduino:renesas_uno
libraries: |
- name: ArduinoECCX08

steps:
- name: Checkout
Expand Down
23 changes: 23 additions & 0 deletions src/utility/SElementArduinoCloudJWT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
This file is part of the Arduino_SecureElement library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "SElementArduinoCloudJWT.h"

constexpr char JWT_HEADER[] = "{\"alg\":\"ES256\",\"typ\":\"JWT\"}";
String getAIoTCloudJWT(SecureElement &se, String issuer, uint64_t iat, uint8_t slot)
{
SElementJWS jws;
String jwtClaim = "{\"iat\":";
jwtClaim += String((uint32_t)iat);
jwtClaim += ",\"iss\":\"";
jwtClaim += issuer;
jwtClaim += "\"}";
String token = jws.sign(se, slot, JWT_HEADER, jwtClaim.c_str());
return token;
}
17 changes: 17 additions & 0 deletions src/utility/SElementArduinoCloudJWT.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
This file is part of the Arduino_SecureElement library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef SECURE_ELEMENT_AIoTCloud_JWT_H_
#define SECURE_ELEMENT_AIoTCloud_JWT_H_
#include "SElementJWS.h"

String getAIoTCloudJWT(SecureElement &se, String issuer, uint64_t iat, uint8_t slot = 1);

#endif
126 changes: 126 additions & 0 deletions src/utility/SElementJWS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
This file is part of the Arduino_SecureElement library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

/******************************************************************************
* INCLUDE
******************************************************************************/

#include <utility/SElementJWS.h>
#include <ArduinoECCX08.h>
#include <utility/ASN1Utils.h>
#include <utility/PEMUtils.h>

static String base64urlEncode(const byte in[], unsigned int length)
{
static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";

int b;
String out;

int reserveLength = 4 * ((length + 2) / 3);
out.reserve(reserveLength);

for (unsigned int i = 0; i < length; i += 3) {
b = (in[i] & 0xFC) >> 2;
out += CODES[b];

b = (in[i] & 0x03) << 4;
if (i + 1 < length) {
b |= (in[i + 1] & 0xF0) >> 4;
out += CODES[b];
b = (in[i + 1] & 0x0F) << 2;
if (i + 2 < length) {
b |= (in[i + 2] & 0xC0) >> 6;
out += CODES[b];
b = in[i + 2] & 0x3F;
out += CODES[b];
} else {
out += CODES[b];
}
} else {
out += CODES[b];
}
}

while (out.lastIndexOf('=') != -1) {
out.remove(out.length() - 1);
}

return out;
}

String SElementJWS::publicKey(SecureElement & se, int slot, bool newPrivateKey)
{
if (slot < 0 || slot > 8) {
return "";
}

byte publicKey[64];

if (newPrivateKey) {
if (!se.generatePrivateKey(slot, publicKey)) {
return "";
}
} else {
if (!se.generatePublicKey(slot, publicKey)) {
return "";
}
}

int length = ASN1Utils.publicKeyLength();
byte out[length];

ASN1Utils.appendPublicKey(publicKey, out);

return PEMUtils.base64Encode(out, length, "-----BEGIN PUBLIC KEY-----\n", "\n-----END PUBLIC KEY-----\n");
}

String SElementJWS::sign(SecureElement & se, int slot, const char* header, const char* payload)
{
if (slot < 0 || slot > 8) {
return "";
}

String encodedHeader = base64urlEncode((const byte*)header, strlen(header));
String encodedPayload = base64urlEncode((const byte*)payload, strlen(payload));

String toSign;
toSign.reserve(encodedHeader.length() + 1 + encodedPayload.length());

toSign += encodedHeader;
toSign += '.';
toSign += encodedPayload;


byte toSignSha256[32];
byte signature[64];

se.SHA256((const uint8_t*)toSign.c_str(), toSign.length(), toSignSha256);

if (!se.ecSign(slot, toSignSha256, signature)) {
return "";
}

String encodedSignature = base64urlEncode(signature, sizeof(signature));

String result;
result.reserve(toSign.length() + 1 + encodedSignature.length());

result += toSign;
result += '.';
result += encodedSignature;

return result;
}

String SElementJWS::sign(SecureElement & se, int slot, const String& header, const String& payload)
{
return sign(se, slot, header.c_str(), payload.c_str());
}
36 changes: 36 additions & 0 deletions src/utility/SElementJWS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
This file is part of the Arduino_SecureElement library.

Copyright (c) 2024 Arduino SA

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef SECURE_ELEMENT_JWS_H_
#define SECURE_ELEMENT_JWS_H_

/******************************************************************************
* INCLUDE
******************************************************************************/

#include <Arduino_SecureElement.h>

/******************************************************************************
* CLASS DECLARATION
******************************************************************************/

class SElementJWS
{
public:

String publicKey(SecureElement & se, int slot, bool newPrivateKey = true);

String sign(SecureElement & se, int slot, const char* header, const char* payload);
String sign(SecureElement & se, int slot, const String& header, const String& payload);

};


#endif /* SECURE_ELEMENT_JWS_H_ */