|
| 1 | +/* |
| 2 | + This file is part of the Arduino_SecureElement library. |
| 3 | +
|
| 4 | + Copyright (c) 2024 Arduino SA |
| 5 | +
|
| 6 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | +*/ |
| 10 | + |
| 11 | +/****************************************************************************** |
| 12 | + * INCLUDE |
| 13 | + ******************************************************************************/ |
| 14 | + |
| 15 | +#include <utility/SElementJWS.h> |
| 16 | +#include <ArduinoECCX08.h> |
| 17 | +#include <utility/ASN1Utils.h> |
| 18 | +#include <utility/PEMUtils.h> |
| 19 | + |
| 20 | +static String base64urlEncode(const byte in[], unsigned int length) |
| 21 | +{ |
| 22 | + static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_="; |
| 23 | + |
| 24 | + int b; |
| 25 | + String out; |
| 26 | + |
| 27 | + int reserveLength = 4 * ((length + 2) / 3); |
| 28 | + out.reserve(reserveLength); |
| 29 | + |
| 30 | + for (unsigned int i = 0; i < length; i += 3) { |
| 31 | + b = (in[i] & 0xFC) >> 2; |
| 32 | + out += CODES[b]; |
| 33 | + |
| 34 | + b = (in[i] & 0x03) << 4; |
| 35 | + if (i + 1 < length) { |
| 36 | + b |= (in[i + 1] & 0xF0) >> 4; |
| 37 | + out += CODES[b]; |
| 38 | + b = (in[i + 1] & 0x0F) << 2; |
| 39 | + if (i + 2 < length) { |
| 40 | + b |= (in[i + 2] & 0xC0) >> 6; |
| 41 | + out += CODES[b]; |
| 42 | + b = in[i + 2] & 0x3F; |
| 43 | + out += CODES[b]; |
| 44 | + } else { |
| 45 | + out += CODES[b]; |
| 46 | + } |
| 47 | + } else { |
| 48 | + out += CODES[b]; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + while (out.lastIndexOf('=') != -1) { |
| 53 | + out.remove(out.length() - 1); |
| 54 | + } |
| 55 | + |
| 56 | + return out; |
| 57 | +} |
| 58 | + |
| 59 | +String SElementJWS::publicKey(SecureElement & se, int slot, bool newPrivateKey) |
| 60 | +{ |
| 61 | + if (slot < 0 || slot > 8) { |
| 62 | + return ""; |
| 63 | + } |
| 64 | + |
| 65 | + byte publicKey[64]; |
| 66 | + |
| 67 | + if (newPrivateKey) { |
| 68 | + if (!se.generatePrivateKey(slot, publicKey)) { |
| 69 | + return ""; |
| 70 | + } |
| 71 | + } else { |
| 72 | + if (!se.generatePublicKey(slot, publicKey)) { |
| 73 | + return ""; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + int length = ASN1Utils.publicKeyLength(); |
| 78 | + byte out[length]; |
| 79 | + |
| 80 | + ASN1Utils.appendPublicKey(publicKey, out); |
| 81 | + |
| 82 | + return PEMUtils.base64Encode(out, length, "-----BEGIN PUBLIC KEY-----\n", "\n-----END PUBLIC KEY-----\n"); |
| 83 | +} |
| 84 | + |
| 85 | +String SElementJWS::sign(SecureElement & se, int slot, const char* header, const char* payload) |
| 86 | +{ |
| 87 | + if (slot < 0 || slot > 8) { |
| 88 | + return ""; |
| 89 | + } |
| 90 | + |
| 91 | + String encodedHeader = base64urlEncode((const byte*)header, strlen(header)); |
| 92 | + String encodedPayload = base64urlEncode((const byte*)payload, strlen(payload)); |
| 93 | + |
| 94 | + String toSign; |
| 95 | + toSign.reserve(encodedHeader.length() + 1 + encodedPayload.length()); |
| 96 | + |
| 97 | + toSign += encodedHeader; |
| 98 | + toSign += '.'; |
| 99 | + toSign += encodedPayload; |
| 100 | + |
| 101 | + |
| 102 | + byte toSignSha256[32]; |
| 103 | + byte signature[64]; |
| 104 | + |
| 105 | + se.SHA256((const uint8_t*)toSign.c_str(), toSign.length(), toSignSha256); |
| 106 | + |
| 107 | + if (!se.ecSign(slot, toSignSha256, signature)) { |
| 108 | + return ""; |
| 109 | + } |
| 110 | + |
| 111 | + String encodedSignature = base64urlEncode(signature, sizeof(signature)); |
| 112 | + |
| 113 | + String result; |
| 114 | + result.reserve(toSign.length() + 1 + encodedSignature.length()); |
| 115 | + |
| 116 | + result += toSign; |
| 117 | + result += '.'; |
| 118 | + result += encodedSignature; |
| 119 | + |
| 120 | + return result; |
| 121 | +} |
| 122 | + |
| 123 | +String SElementJWS::sign(SecureElement & se, int slot, const String& header, const String& payload) |
| 124 | +{ |
| 125 | + return sign(se, slot, header.c_str(), payload.c_str()); |
| 126 | +} |
0 commit comments