-
Notifications
You must be signed in to change notification settings - Fork 3
Add JWS utility #26
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
Merged
Merged
Add JWS utility #26
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
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 <utility/SElementBase64.h> | ||
|
||
namespace arduino { namespace b64 { | ||
|
||
String urlEncode(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 encode(const byte in[], unsigned int length, const char* prefix, const char* suffix) { | ||
static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; | ||
|
||
int b; | ||
String out; | ||
|
||
int reserveLength = 4 * ((length + 2) / 3) + ((length / 3 * 4) / 76) + strlen(prefix) + strlen(suffix); | ||
out.reserve(reserveLength); | ||
|
||
if (prefix) { | ||
out += prefix; | ||
} | ||
|
||
for (unsigned int i = 0; i < length; i += 3) { | ||
if (i > 0 && (i / 3 * 4) % 76 == 0) { | ||
out += '\n'; | ||
} | ||
|
||
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]; | ||
out += '='; | ||
} | ||
} else { | ||
out += CODES[b]; | ||
out += "=="; | ||
} | ||
} | ||
|
||
if (suffix) { | ||
out += suffix; | ||
} | ||
|
||
return out; | ||
} | ||
|
||
}} // arduino::b64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
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/. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <Arduino.h> | ||
|
||
namespace arduino { namespace b64 { | ||
|
||
String urlEncode(const byte in[], unsigned int length); | ||
String encode(const byte in[], unsigned int length, const char* prefix, const char* suffix); | ||
|
||
}} // arduino::b64 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
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 <utility/SElementBase64.h> | ||
|
||
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 = publicKeyLength(); | ||
byte out[length]; | ||
|
||
appendPublicKey(publicKey, out); | ||
|
||
return b64::encode(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 = b64::urlEncode((const byte*)header, strlen(header)); | ||
String encodedPayload = b64::urlEncode((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 = b64::urlEncode(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()); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't we define only encode this way? It seems a bit redundant the definition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this two functions are also using a different alphabet https://datatracker.ietf.org/doc/html/rfc4648#section-5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
encode is also adding \n so i've renamed it to pemEncode