cleanup code
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2023 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2023 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#ifndef _ECCX08_CONFIG_H_
|
||||
#define _ECCX08_CONFIG_H_
|
||||
// #ifndef _ECCX08_CONFIG_H_
|
||||
// #define _ECCX08_CONFIG_H_
|
||||
|
||||
#if defined __has_include
|
||||
#if __has_include (<ArduinoECCX08Config.h>)
|
||||
#include <ArduinoECCX08Config.h>
|
||||
#endif
|
||||
#endif
|
||||
// #if defined __has_include
|
||||
// #if __has_include (<ArduinoECCX08Config.h>)
|
||||
// #include <ArduinoECCX08Config.h>
|
||||
// #endif
|
||||
// #endif
|
||||
|
||||
#endif
|
||||
// #endif
|
||||
|
||||
@@ -1,416 +1,416 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#include "ECCX08Config.h"
|
||||
#include "ASN1Utils.h"
|
||||
// #include "ECCX08Config.h"
|
||||
// #include "ASN1Utils.h"
|
||||
|
||||
int ASN1UtilsClass::versionLength()
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
// int ASN1UtilsClass::versionLength()
|
||||
// {
|
||||
// return 3;
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::issuerOrSubjectLength(const String& countryName,
|
||||
const String& stateProvinceName,
|
||||
const String& localityName,
|
||||
const String& organizationName,
|
||||
const String& organizationalUnitName,
|
||||
const String& commonName)
|
||||
{
|
||||
int length = 0;
|
||||
int countryNameLength = countryName.length();
|
||||
int stateProvinceNameLength = stateProvinceName.length();
|
||||
int localityNameLength = localityName.length();
|
||||
int organizationNameLength = organizationName.length();
|
||||
int organizationalUnitNameLength = organizationalUnitName.length();
|
||||
int commonNameLength = commonName.length();
|
||||
// int ASN1UtilsClass::issuerOrSubjectLength(const String& countryName,
|
||||
// const String& stateProvinceName,
|
||||
// const String& localityName,
|
||||
// const String& organizationName,
|
||||
// const String& organizationalUnitName,
|
||||
// const String& commonName)
|
||||
// {
|
||||
// int length = 0;
|
||||
// int countryNameLength = countryName.length();
|
||||
// int stateProvinceNameLength = stateProvinceName.length();
|
||||
// int localityNameLength = localityName.length();
|
||||
// int organizationNameLength = organizationName.length();
|
||||
// int organizationalUnitNameLength = organizationalUnitName.length();
|
||||
// int commonNameLength = commonName.length();
|
||||
|
||||
if (countryNameLength) {
|
||||
length += (11 + countryNameLength);
|
||||
}
|
||||
// if (countryNameLength) {
|
||||
// length += (11 + countryNameLength);
|
||||
// }
|
||||
|
||||
if (stateProvinceNameLength) {
|
||||
length += (11 + stateProvinceNameLength);
|
||||
}
|
||||
// if (stateProvinceNameLength) {
|
||||
// length += (11 + stateProvinceNameLength);
|
||||
// }
|
||||
|
||||
if (localityNameLength) {
|
||||
length += (11 + localityNameLength);
|
||||
}
|
||||
// if (localityNameLength) {
|
||||
// length += (11 + localityNameLength);
|
||||
// }
|
||||
|
||||
if (organizationNameLength) {
|
||||
length += (11 + organizationNameLength);
|
||||
}
|
||||
// if (organizationNameLength) {
|
||||
// length += (11 + organizationNameLength);
|
||||
// }
|
||||
|
||||
if (organizationalUnitNameLength) {
|
||||
length += (11 + organizationalUnitNameLength);
|
||||
}
|
||||
// if (organizationalUnitNameLength) {
|
||||
// length += (11 + organizationalUnitNameLength);
|
||||
// }
|
||||
|
||||
if (commonNameLength) {
|
||||
length += (11 + commonNameLength);
|
||||
}
|
||||
// if (commonNameLength) {
|
||||
// length += (11 + commonNameLength);
|
||||
// }
|
||||
|
||||
return length;
|
||||
}
|
||||
// return length;
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::publicKeyLength()
|
||||
{
|
||||
return (2 + 2 + 9 + 10 + 4 + 64);
|
||||
}
|
||||
// int ASN1UtilsClass::publicKeyLength()
|
||||
// {
|
||||
// return (2 + 2 + 9 + 10 + 4 + 64);
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::signatureLength(const byte signature[])
|
||||
{
|
||||
const byte* r = &signature[0];
|
||||
const byte* s = &signature[32];
|
||||
// int ASN1UtilsClass::signatureLength(const byte signature[])
|
||||
// {
|
||||
// const byte* r = &signature[0];
|
||||
// const byte* s = &signature[32];
|
||||
|
||||
int rLength = 32;
|
||||
int sLength = 32;
|
||||
// int rLength = 32;
|
||||
// int sLength = 32;
|
||||
|
||||
while (*r == 0x00 && rLength > 1) {
|
||||
r++;
|
||||
rLength--;
|
||||
}
|
||||
// while (*r == 0x00 && rLength > 1) {
|
||||
// r++;
|
||||
// rLength--;
|
||||
// }
|
||||
|
||||
if (*r & 0x80) {
|
||||
rLength++;
|
||||
}
|
||||
// if (*r & 0x80) {
|
||||
// rLength++;
|
||||
// }
|
||||
|
||||
while (*s == 0x00 && sLength > 1) {
|
||||
s++;
|
||||
sLength--;
|
||||
}
|
||||
// while (*s == 0x00 && sLength > 1) {
|
||||
// s++;
|
||||
// sLength--;
|
||||
// }
|
||||
|
||||
if (*s & 0x80) {
|
||||
sLength++;
|
||||
}
|
||||
// if (*s & 0x80) {
|
||||
// sLength++;
|
||||
// }
|
||||
|
||||
return (21 + rLength + sLength);
|
||||
}
|
||||
// return (21 + rLength + sLength);
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::serialNumberLength(const byte serialNumber[], int length)
|
||||
{
|
||||
while (*serialNumber == 0 && length > 1) {
|
||||
serialNumber++;
|
||||
length--;
|
||||
}
|
||||
// int ASN1UtilsClass::serialNumberLength(const byte serialNumber[], int length)
|
||||
// {
|
||||
// while (*serialNumber == 0 && length > 1) {
|
||||
// serialNumber++;
|
||||
// length--;
|
||||
// }
|
||||
|
||||
if (*serialNumber & 0x80) {
|
||||
length++;
|
||||
}
|
||||
// if (*serialNumber & 0x80) {
|
||||
// length++;
|
||||
// }
|
||||
|
||||
return (2 + length);
|
||||
}
|
||||
// return (2 + length);
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::sequenceHeaderLength(int length)
|
||||
{
|
||||
if (length > 255) {
|
||||
return 4;
|
||||
} else if (length > 127) {
|
||||
return 3;
|
||||
} else {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
// int ASN1UtilsClass::sequenceHeaderLength(int length)
|
||||
// {
|
||||
// if (length > 255) {
|
||||
// return 4;
|
||||
// } else if (length > 127) {
|
||||
// return 3;
|
||||
// } else {
|
||||
// return 2;
|
||||
// }
|
||||
// }
|
||||
|
||||
void ASN1UtilsClass::appendVersion(int version, byte out[])
|
||||
{
|
||||
out[0] = ASN1_INTEGER;
|
||||
out[1] = 0x01;
|
||||
out[2] = version;
|
||||
}
|
||||
// void ASN1UtilsClass::appendVersion(int version, byte out[])
|
||||
// {
|
||||
// out[0] = ASN1_INTEGER;
|
||||
// out[1] = 0x01;
|
||||
// out[2] = version;
|
||||
// }
|
||||
|
||||
void ASN1UtilsClass::appendIssuerOrSubject(const String& countryName,
|
||||
const String& stateProvinceName,
|
||||
const String& localityName,
|
||||
const String& organizationName,
|
||||
const String& organizationalUnitName,
|
||||
const String& commonName,
|
||||
byte out[])
|
||||
{
|
||||
if (countryName.length() > 0) {
|
||||
out += appendName(countryName, 0x06, out);
|
||||
}
|
||||
// void ASN1UtilsClass::appendIssuerOrSubject(const String& countryName,
|
||||
// const String& stateProvinceName,
|
||||
// const String& localityName,
|
||||
// const String& organizationName,
|
||||
// const String& organizationalUnitName,
|
||||
// const String& commonName,
|
||||
// byte out[])
|
||||
// {
|
||||
// if (countryName.length() > 0) {
|
||||
// out += appendName(countryName, 0x06, out);
|
||||
// }
|
||||
|
||||
if (stateProvinceName.length() > 0) {
|
||||
out += appendName(stateProvinceName, 0x08, out);
|
||||
}
|
||||
// if (stateProvinceName.length() > 0) {
|
||||
// out += appendName(stateProvinceName, 0x08, out);
|
||||
// }
|
||||
|
||||
if (localityName.length() > 0) {
|
||||
out += appendName(localityName, 0x07, out);
|
||||
}
|
||||
// if (localityName.length() > 0) {
|
||||
// out += appendName(localityName, 0x07, out);
|
||||
// }
|
||||
|
||||
if (organizationName.length() > 0) {
|
||||
out += appendName(organizationName, 0x0a, out);
|
||||
}
|
||||
// if (organizationName.length() > 0) {
|
||||
// out += appendName(organizationName, 0x0a, out);
|
||||
// }
|
||||
|
||||
if (organizationalUnitName.length() > 0) {
|
||||
out += appendName(organizationalUnitName, 0x0b, out);
|
||||
}
|
||||
// if (organizationalUnitName.length() > 0) {
|
||||
// out += appendName(organizationalUnitName, 0x0b, out);
|
||||
// }
|
||||
|
||||
if (commonName.length() > 0) {
|
||||
out += appendName(commonName, 0x03, out);
|
||||
}
|
||||
}
|
||||
// if (commonName.length() > 0) {
|
||||
// out += appendName(commonName, 0x03, out);
|
||||
// }
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::appendPublicKey(const byte publicKey[], byte out[])
|
||||
{
|
||||
int subjectPublicKeyDataLength = 2 + 9 + 10 + 4 + 64;
|
||||
// int ASN1UtilsClass::appendPublicKey(const byte publicKey[], byte out[])
|
||||
// {
|
||||
// int subjectPublicKeyDataLength = 2 + 9 + 10 + 4 + 64;
|
||||
|
||||
// subject public key
|
||||
*out++ = ASN1_SEQUENCE;
|
||||
*out++ = (subjectPublicKeyDataLength) & 0xff;
|
||||
// // subject public key
|
||||
// *out++ = ASN1_SEQUENCE;
|
||||
// *out++ = (subjectPublicKeyDataLength) & 0xff;
|
||||
|
||||
*out++ = ASN1_SEQUENCE;
|
||||
*out++ = 0x13;
|
||||
// *out++ = ASN1_SEQUENCE;
|
||||
// *out++ = 0x13;
|
||||
|
||||
// EC public key
|
||||
*out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
*out++ = 0x07;
|
||||
*out++ = 0x2a;
|
||||
*out++ = 0x86;
|
||||
*out++ = 0x48;
|
||||
*out++ = 0xce;
|
||||
*out++ = 0x3d;
|
||||
*out++ = 0x02;
|
||||
*out++ = 0x01;
|
||||
// // EC public key
|
||||
// *out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
// *out++ = 0x07;
|
||||
// *out++ = 0x2a;
|
||||
// *out++ = 0x86;
|
||||
// *out++ = 0x48;
|
||||
// *out++ = 0xce;
|
||||
// *out++ = 0x3d;
|
||||
// *out++ = 0x02;
|
||||
// *out++ = 0x01;
|
||||
|
||||
// PRIME 256 v1
|
||||
*out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
*out++ = 0x08;
|
||||
*out++ = 0x2a;
|
||||
*out++ = 0x86;
|
||||
*out++ = 0x48;
|
||||
*out++ = 0xce;
|
||||
*out++ = 0x3d;
|
||||
*out++ = 0x03;
|
||||
*out++ = 0x01;
|
||||
*out++ = 0x07;
|
||||
// // PRIME 256 v1
|
||||
// *out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
// *out++ = 0x08;
|
||||
// *out++ = 0x2a;
|
||||
// *out++ = 0x86;
|
||||
// *out++ = 0x48;
|
||||
// *out++ = 0xce;
|
||||
// *out++ = 0x3d;
|
||||
// *out++ = 0x03;
|
||||
// *out++ = 0x01;
|
||||
// *out++ = 0x07;
|
||||
|
||||
*out++ = 0x03;
|
||||
*out++ = 0x42;
|
||||
*out++ = 0x00;
|
||||
*out++ = 0x04;
|
||||
// *out++ = 0x03;
|
||||
// *out++ = 0x42;
|
||||
// *out++ = 0x00;
|
||||
// *out++ = 0x04;
|
||||
|
||||
memcpy(out, publicKey, 64);
|
||||
// memcpy(out, publicKey, 64);
|
||||
|
||||
return (2 + subjectPublicKeyDataLength);
|
||||
}
|
||||
// return (2 + subjectPublicKeyDataLength);
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
|
||||
{
|
||||
// signature algorithm
|
||||
*out++ = ASN1_SEQUENCE;
|
||||
*out++ = 0x0a;
|
||||
*out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
*out++ = 0x08;
|
||||
// int ASN1UtilsClass::appendSignature(const byte signature[], byte out[])
|
||||
// {
|
||||
// // signature algorithm
|
||||
// *out++ = ASN1_SEQUENCE;
|
||||
// *out++ = 0x0a;
|
||||
// *out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
// *out++ = 0x08;
|
||||
|
||||
// ECDSA with SHA256
|
||||
*out++ = 0x2a;
|
||||
*out++ = 0x86;
|
||||
*out++ = 0x48;
|
||||
*out++ = 0xce;
|
||||
*out++ = 0x3d;
|
||||
*out++ = 0x04;
|
||||
*out++ = 0x03;
|
||||
*out++ = 0x02;
|
||||
// // ECDSA with SHA256
|
||||
// *out++ = 0x2a;
|
||||
// *out++ = 0x86;
|
||||
// *out++ = 0x48;
|
||||
// *out++ = 0xce;
|
||||
// *out++ = 0x3d;
|
||||
// *out++ = 0x04;
|
||||
// *out++ = 0x03;
|
||||
// *out++ = 0x02;
|
||||
|
||||
const byte* r = &signature[0];
|
||||
const byte* s = &signature[32];
|
||||
// const byte* r = &signature[0];
|
||||
// const byte* s = &signature[32];
|
||||
|
||||
int rLength = 32;
|
||||
int sLength = 32;
|
||||
// int rLength = 32;
|
||||
// int sLength = 32;
|
||||
|
||||
while (*r == 0 && rLength > 1) {
|
||||
r++;
|
||||
rLength--;
|
||||
}
|
||||
// while (*r == 0 && rLength > 1) {
|
||||
// r++;
|
||||
// rLength--;
|
||||
// }
|
||||
|
||||
while (*s == 0 && sLength > 1) {
|
||||
s++;
|
||||
sLength--;
|
||||
}
|
||||
// while (*s == 0 && sLength > 1) {
|
||||
// s++;
|
||||
// sLength--;
|
||||
// }
|
||||
|
||||
if (*r & 0x80) {
|
||||
rLength++;
|
||||
}
|
||||
// if (*r & 0x80) {
|
||||
// rLength++;
|
||||
// }
|
||||
|
||||
if (*s & 0x80) {
|
||||
sLength++;
|
||||
}
|
||||
// if (*s & 0x80) {
|
||||
// sLength++;
|
||||
// }
|
||||
|
||||
*out++ = ASN1_BIT_STRING;
|
||||
*out++ = (rLength + sLength + 7);
|
||||
*out++ = 0;
|
||||
// *out++ = ASN1_BIT_STRING;
|
||||
// *out++ = (rLength + sLength + 7);
|
||||
// *out++ = 0;
|
||||
|
||||
*out++ = ASN1_SEQUENCE;
|
||||
*out++ = (rLength + sLength + 4);
|
||||
// *out++ = ASN1_SEQUENCE;
|
||||
// *out++ = (rLength + sLength + 4);
|
||||
|
||||
*out++ = ASN1_INTEGER;
|
||||
*out++ = rLength;
|
||||
if (*r & 0x80) {
|
||||
*out++ = 0;
|
||||
rLength--;
|
||||
}
|
||||
memcpy(out, r, rLength);
|
||||
out += rLength;
|
||||
// *out++ = ASN1_INTEGER;
|
||||
// *out++ = rLength;
|
||||
// if (*r & 0x80) {
|
||||
// *out++ = 0;
|
||||
// rLength--;
|
||||
// }
|
||||
// memcpy(out, r, rLength);
|
||||
// out += rLength;
|
||||
|
||||
if (*r & 0x80) {
|
||||
rLength++;
|
||||
}
|
||||
// if (*r & 0x80) {
|
||||
// rLength++;
|
||||
// }
|
||||
|
||||
*out++ = ASN1_INTEGER;
|
||||
*out++ = sLength;
|
||||
if (*s & 0x80) {
|
||||
*out++ = 0;
|
||||
sLength--;
|
||||
}
|
||||
memcpy(out, s, sLength);
|
||||
out += sLength;
|
||||
// *out++ = ASN1_INTEGER;
|
||||
// *out++ = sLength;
|
||||
// if (*s & 0x80) {
|
||||
// *out++ = 0;
|
||||
// sLength--;
|
||||
// }
|
||||
// memcpy(out, s, sLength);
|
||||
// out += sLength;
|
||||
|
||||
if (*s & 0x80) {
|
||||
sLength++;
|
||||
}
|
||||
// if (*s & 0x80) {
|
||||
// sLength++;
|
||||
// }
|
||||
|
||||
return (21 + rLength + sLength);
|
||||
}
|
||||
// return (21 + rLength + sLength);
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::appendSerialNumber(const byte serialNumber[], int length, byte out[])
|
||||
{
|
||||
while (*serialNumber == 0 && length > 1) {
|
||||
serialNumber++;
|
||||
length--;
|
||||
}
|
||||
// int ASN1UtilsClass::appendSerialNumber(const byte serialNumber[], int length, byte out[])
|
||||
// {
|
||||
// while (*serialNumber == 0 && length > 1) {
|
||||
// serialNumber++;
|
||||
// length--;
|
||||
// }
|
||||
|
||||
if (*serialNumber & 0x80) {
|
||||
length++;
|
||||
}
|
||||
// if (*serialNumber & 0x80) {
|
||||
// length++;
|
||||
// }
|
||||
|
||||
*out++ = ASN1_INTEGER;
|
||||
*out++ = length;
|
||||
// *out++ = ASN1_INTEGER;
|
||||
// *out++ = length;
|
||||
|
||||
if (*serialNumber & 0x80) {
|
||||
*out++ = 0x00;
|
||||
length--;
|
||||
}
|
||||
// if (*serialNumber & 0x80) {
|
||||
// *out++ = 0x00;
|
||||
// length--;
|
||||
// }
|
||||
|
||||
memcpy(out, serialNumber, length);
|
||||
// memcpy(out, serialNumber, length);
|
||||
|
||||
if (*serialNumber & 0x80) {
|
||||
length++;
|
||||
}
|
||||
// if (*serialNumber & 0x80) {
|
||||
// length++;
|
||||
// }
|
||||
|
||||
return (2 + length);
|
||||
}
|
||||
// return (2 + length);
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::appendName(const String& name, int type, byte out[])
|
||||
{
|
||||
int nameLength = name.length();
|
||||
// int ASN1UtilsClass::appendName(const String& name, int type, byte out[])
|
||||
// {
|
||||
// int nameLength = name.length();
|
||||
|
||||
*out++ = ASN1_SET;
|
||||
*out++ = nameLength + 9;
|
||||
// *out++ = ASN1_SET;
|
||||
// *out++ = nameLength + 9;
|
||||
|
||||
*out++ = ASN1_SEQUENCE;
|
||||
*out++ = nameLength + 7;
|
||||
// *out++ = ASN1_SEQUENCE;
|
||||
// *out++ = nameLength + 7;
|
||||
|
||||
*out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
*out++ = 0x03;
|
||||
*out++ = 0x55;
|
||||
*out++ = 0x04;
|
||||
*out++ = type;
|
||||
// *out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
// *out++ = 0x03;
|
||||
// *out++ = 0x55;
|
||||
// *out++ = 0x04;
|
||||
// *out++ = type;
|
||||
|
||||
*out++ = ASN1_PRINTABLE_STRING;
|
||||
*out++ = nameLength;
|
||||
memcpy(out, name.c_str(), nameLength);
|
||||
// *out++ = ASN1_PRINTABLE_STRING;
|
||||
// *out++ = nameLength;
|
||||
// memcpy(out, name.c_str(), nameLength);
|
||||
|
||||
return (nameLength + 11);
|
||||
}
|
||||
// return (nameLength + 11);
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::appendSequenceHeader(int length, byte out[])
|
||||
{
|
||||
*out++ = ASN1_SEQUENCE;
|
||||
if (length > 255) {
|
||||
*out++ = 0x82;
|
||||
*out++ = (length >> 8) & 0xff;
|
||||
} else if (length > 127) {
|
||||
*out++ = 0x81;
|
||||
}
|
||||
*out++ = (length) & 0xff;
|
||||
// int ASN1UtilsClass::appendSequenceHeader(int length, byte out[])
|
||||
// {
|
||||
// *out++ = ASN1_SEQUENCE;
|
||||
// if (length > 255) {
|
||||
// *out++ = 0x82;
|
||||
// *out++ = (length >> 8) & 0xff;
|
||||
// } else if (length > 127) {
|
||||
// *out++ = 0x81;
|
||||
// }
|
||||
// *out++ = (length) & 0xff;
|
||||
|
||||
if (length > 255) {
|
||||
return 4;
|
||||
} else if (length > 127) {
|
||||
return 3;
|
||||
} else {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
// if (length > 255) {
|
||||
// return 4;
|
||||
// } else if (length > 127) {
|
||||
// return 3;
|
||||
// } else {
|
||||
// return 2;
|
||||
// }
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::appendDate(int year, int month, int day, int hour, int minute, int second, byte out[])
|
||||
{
|
||||
bool useGeneralizedTime = (year > 2049);
|
||||
// int ASN1UtilsClass::appendDate(int year, int month, int day, int hour, int minute, int second, byte out[])
|
||||
// {
|
||||
// bool useGeneralizedTime = (year > 2049);
|
||||
|
||||
if (useGeneralizedTime) {
|
||||
*out++ = 0x18;
|
||||
*out++ = 0x0f;
|
||||
*out++ = '0' + (year / 1000);
|
||||
*out++ = '0' + ((year % 1000) / 100);
|
||||
*out++ = '0' + ((year % 100) / 10);
|
||||
*out++ = '0' + (year % 10);
|
||||
} else {
|
||||
year -= 2000;
|
||||
// if (useGeneralizedTime) {
|
||||
// *out++ = 0x18;
|
||||
// *out++ = 0x0f;
|
||||
// *out++ = '0' + (year / 1000);
|
||||
// *out++ = '0' + ((year % 1000) / 100);
|
||||
// *out++ = '0' + ((year % 100) / 10);
|
||||
// *out++ = '0' + (year % 10);
|
||||
// } else {
|
||||
// year -= 2000;
|
||||
|
||||
*out++ = 0x17;
|
||||
*out++ = 0x0d;
|
||||
*out++ = '0' + (year / 10);
|
||||
*out++ = '0' + (year % 10);
|
||||
}
|
||||
*out++ = '0' + (month / 10);
|
||||
*out++ = '0' + (month % 10);
|
||||
*out++ = '0' + (day / 10);
|
||||
*out++ = '0' + (day % 10);
|
||||
*out++ = '0' + (hour / 10);
|
||||
*out++ = '0' + (hour % 10);
|
||||
*out++ = '0' + (minute / 10);
|
||||
*out++ = '0' + (minute % 10);
|
||||
*out++ = '0' + (second / 10);
|
||||
*out++ = '0' + (second % 10);
|
||||
*out++ = 0x5a; // UTC
|
||||
// *out++ = 0x17;
|
||||
// *out++ = 0x0d;
|
||||
// *out++ = '0' + (year / 10);
|
||||
// *out++ = '0' + (year % 10);
|
||||
// }
|
||||
// *out++ = '0' + (month / 10);
|
||||
// *out++ = '0' + (month % 10);
|
||||
// *out++ = '0' + (day / 10);
|
||||
// *out++ = '0' + (day % 10);
|
||||
// *out++ = '0' + (hour / 10);
|
||||
// *out++ = '0' + (hour % 10);
|
||||
// *out++ = '0' + (minute / 10);
|
||||
// *out++ = '0' + (minute % 10);
|
||||
// *out++ = '0' + (second / 10);
|
||||
// *out++ = '0' + (second % 10);
|
||||
// *out++ = 0x5a; // UTC
|
||||
|
||||
return (useGeneralizedTime ? 17 : 15);
|
||||
}
|
||||
// return (useGeneralizedTime ? 17 : 15);
|
||||
// }
|
||||
|
||||
int ASN1UtilsClass::appendEcdsaWithSHA256(byte out[])
|
||||
{
|
||||
*out++ = ASN1_SEQUENCE;
|
||||
*out++ = 0x0A;
|
||||
*out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
*out++ = 0x08;
|
||||
*out++ = 0x2A;
|
||||
*out++ = 0x86;
|
||||
*out++ = 0x48;
|
||||
*out++ = 0xCE;
|
||||
*out++ = 0x3D;
|
||||
*out++ = 0x04;
|
||||
*out++ = 0x03;
|
||||
*out++ = 0x02;
|
||||
// int ASN1UtilsClass::appendEcdsaWithSHA256(byte out[])
|
||||
// {
|
||||
// *out++ = ASN1_SEQUENCE;
|
||||
// *out++ = 0x0A;
|
||||
// *out++ = ASN1_OBJECT_IDENTIFIER;
|
||||
// *out++ = 0x08;
|
||||
// *out++ = 0x2A;
|
||||
// *out++ = 0x86;
|
||||
// *out++ = 0x48;
|
||||
// *out++ = 0xCE;
|
||||
// *out++ = 0x3D;
|
||||
// *out++ = 0x04;
|
||||
// *out++ = 0x03;
|
||||
// *out++ = 0x02;
|
||||
|
||||
return 12;
|
||||
}
|
||||
// return 12;
|
||||
// }
|
||||
|
||||
#if !defined(ECCX08_DISABLE_ASN1)
|
||||
ASN1UtilsClass ASN1Utils;
|
||||
#endif
|
||||
// #if !defined(ECCX08_DISABLE_ASN1)
|
||||
// ASN1UtilsClass ASN1Utils;
|
||||
// #endif
|
||||
|
||||
@@ -1,79 +1,79 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#ifndef _ASN1_UTILS_H_
|
||||
#define _ASN1_UTILS_H_
|
||||
// #ifndef _ASN1_UTILS_H_
|
||||
// #define _ASN1_UTILS_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
// #include <Arduino.h>
|
||||
|
||||
#define ASN1_INTEGER 0x02
|
||||
#define ASN1_BIT_STRING 0x03
|
||||
#define ASN1_NULL 0x05
|
||||
#define ASN1_OBJECT_IDENTIFIER 0x06
|
||||
#define ASN1_PRINTABLE_STRING 0x13
|
||||
#define ASN1_SEQUENCE 0x30
|
||||
#define ASN1_SET 0x31
|
||||
// #define ASN1_INTEGER 0x02
|
||||
// #define ASN1_BIT_STRING 0x03
|
||||
// #define ASN1_NULL 0x05
|
||||
// #define ASN1_OBJECT_IDENTIFIER 0x06
|
||||
// #define ASN1_PRINTABLE_STRING 0x13
|
||||
// #define ASN1_SEQUENCE 0x30
|
||||
// #define ASN1_SET 0x31
|
||||
|
||||
class ASN1UtilsClass {
|
||||
public:
|
||||
int versionLength();
|
||||
// class ASN1UtilsClass {
|
||||
// public:
|
||||
// int versionLength();
|
||||
|
||||
int issuerOrSubjectLength(const String& countryName,
|
||||
const String& stateProvinceName,
|
||||
const String& localityName,
|
||||
const String& organizationName,
|
||||
const String& organizationalUnitName,
|
||||
const String& commonName);
|
||||
// int issuerOrSubjectLength(const String& countryName,
|
||||
// const String& stateProvinceName,
|
||||
// const String& localityName,
|
||||
// const String& organizationName,
|
||||
// const String& organizationalUnitName,
|
||||
// const String& commonName);
|
||||
|
||||
int publicKeyLength();
|
||||
// int publicKeyLength();
|
||||
|
||||
int signatureLength(const byte signature[]);
|
||||
// int signatureLength(const byte signature[]);
|
||||
|
||||
int serialNumberLength(const byte serialNumber[], int length);
|
||||
// int serialNumberLength(const byte serialNumber[], int length);
|
||||
|
||||
int sequenceHeaderLength(int length);
|
||||
// int sequenceHeaderLength(int length);
|
||||
|
||||
void appendVersion(int version, byte out[]);
|
||||
// void appendVersion(int version, byte out[]);
|
||||
|
||||
void appendIssuerOrSubject(const String& countryName,
|
||||
const String& stateProvinceName,
|
||||
const String& localityName,
|
||||
const String& organizationName,
|
||||
const String& organizationalUnitName,
|
||||
const String& commonName,
|
||||
byte out[]);
|
||||
// void appendIssuerOrSubject(const String& countryName,
|
||||
// const String& stateProvinceName,
|
||||
// const String& localityName,
|
||||
// const String& organizationName,
|
||||
// const String& organizationalUnitName,
|
||||
// const String& commonName,
|
||||
// byte out[]);
|
||||
|
||||
int appendPublicKey(const byte publicKey[], byte out[]);
|
||||
// int appendPublicKey(const byte publicKey[], byte out[]);
|
||||
|
||||
int appendSignature(const byte signature[], byte out[]);
|
||||
// int appendSignature(const byte signature[], byte out[]);
|
||||
|
||||
int appendSerialNumber(const byte serialNumber[], int length, byte out[]);
|
||||
// int appendSerialNumber(const byte serialNumber[], int length, byte out[]);
|
||||
|
||||
int appendName(const String& name, int type, byte out[]);
|
||||
// int appendName(const String& name, int type, byte out[]);
|
||||
|
||||
int appendSequenceHeader(int length, byte out[]);
|
||||
// int appendSequenceHeader(int length, byte out[]);
|
||||
|
||||
int appendDate(int year, int month, int day, int hour, int minute, int second, byte out[]);
|
||||
// int appendDate(int year, int month, int day, int hour, int minute, int second, byte out[]);
|
||||
|
||||
int appendEcdsaWithSHA256(byte out[]);
|
||||
};
|
||||
// int appendEcdsaWithSHA256(byte out[]);
|
||||
// };
|
||||
|
||||
extern ASN1UtilsClass ASN1Utils;
|
||||
// extern ASN1UtilsClass ASN1Utils;
|
||||
|
||||
#endif
|
||||
// #endif
|
||||
|
||||
@@ -1,179 +1,179 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#include "ECCX08Config.h"
|
||||
#include "ECCX08.h"
|
||||
// #include "ECCX08Config.h"
|
||||
// #include "ECCX08.h"
|
||||
|
||||
#include "ASN1Utils.h"
|
||||
#include "PEMUtils.h"
|
||||
// #include "ASN1Utils.h"
|
||||
// #include "PEMUtils.h"
|
||||
|
||||
#include "ECCX08CSR.h"
|
||||
// #include "ECCX08CSR.h"
|
||||
|
||||
ECCX08CSRClass::ECCX08CSRClass()
|
||||
{
|
||||
}
|
||||
// ECCX08CSRClass::ECCX08CSRClass()
|
||||
// {
|
||||
// }
|
||||
|
||||
ECCX08CSRClass::~ECCX08CSRClass()
|
||||
{
|
||||
}
|
||||
// ECCX08CSRClass::~ECCX08CSRClass()
|
||||
// {
|
||||
// }
|
||||
|
||||
int ECCX08CSRClass::begin(int slot, bool newPrivateKey)
|
||||
{
|
||||
_slot = slot;
|
||||
// int ECCX08CSRClass::begin(int slot, bool newPrivateKey)
|
||||
// {
|
||||
// _slot = slot;
|
||||
|
||||
if (newPrivateKey) {
|
||||
if (!ECCX08::generatePrivateKey(slot, _publicKey)) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (!ECCX08::generatePublicKey(slot, _publicKey)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// if (newPrivateKey) {
|
||||
// if (!ECCX08::generatePrivateKey(slot, _publicKey)) {
|
||||
// return 0;
|
||||
// }
|
||||
// } else {
|
||||
// if (!ECCX08::generatePublicKey(slot, _publicKey)) {
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
return 1;
|
||||
}
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
String ECCX08CSRClass::end()
|
||||
{
|
||||
int versionLen = ASN1Utils.versionLength();
|
||||
int subjectLen = ASN1Utils.issuerOrSubjectLength(_countryName,
|
||||
_stateProvinceName,
|
||||
_localityName,
|
||||
_organizationName,
|
||||
_organizationalUnitName,
|
||||
_commonName);
|
||||
int subjectHeaderLen = ASN1Utils.sequenceHeaderLength(subjectLen);
|
||||
int publicKeyLen = ASN1Utils.publicKeyLength();
|
||||
// String ECCX08CSRClass::end()
|
||||
// {
|
||||
// int versionLen = ASN1Utils.versionLength();
|
||||
// int subjectLen = ASN1Utils.issuerOrSubjectLength(_countryName,
|
||||
// _stateProvinceName,
|
||||
// _localityName,
|
||||
// _organizationName,
|
||||
// _organizationalUnitName,
|
||||
// _commonName);
|
||||
// int subjectHeaderLen = ASN1Utils.sequenceHeaderLength(subjectLen);
|
||||
// int publicKeyLen = ASN1Utils.publicKeyLength();
|
||||
|
||||
int csrInfoLen = versionLen + subjectHeaderLen + subjectLen + publicKeyLen + 2;
|
||||
int csrInfoHeaderLen = ASN1Utils.sequenceHeaderLength(csrInfoLen);
|
||||
// int csrInfoLen = versionLen + subjectHeaderLen + subjectLen + publicKeyLen + 2;
|
||||
// int csrInfoHeaderLen = ASN1Utils.sequenceHeaderLength(csrInfoLen);
|
||||
|
||||
byte csrInfo[csrInfoHeaderLen + csrInfoLen];
|
||||
byte* out = csrInfo;
|
||||
// byte csrInfo[csrInfoHeaderLen + csrInfoLen];
|
||||
// byte* out = csrInfo;
|
||||
|
||||
ASN1Utils.appendSequenceHeader(csrInfoLen, out);
|
||||
out += csrInfoHeaderLen;
|
||||
// ASN1Utils.appendSequenceHeader(csrInfoLen, out);
|
||||
// out += csrInfoHeaderLen;
|
||||
|
||||
// version
|
||||
ASN1Utils.appendVersion(0x00, out);
|
||||
out += versionLen;
|
||||
// // version
|
||||
// ASN1Utils.appendVersion(0x00, out);
|
||||
// out += versionLen;
|
||||
|
||||
// subject
|
||||
ASN1Utils.appendSequenceHeader(subjectLen, out);
|
||||
out += subjectHeaderLen;
|
||||
ASN1Utils.appendIssuerOrSubject(_countryName,
|
||||
_stateProvinceName,
|
||||
_localityName,
|
||||
_organizationName,
|
||||
_organizationalUnitName,
|
||||
_commonName, out);
|
||||
out += subjectLen;
|
||||
// // subject
|
||||
// ASN1Utils.appendSequenceHeader(subjectLen, out);
|
||||
// out += subjectHeaderLen;
|
||||
// ASN1Utils.appendIssuerOrSubject(_countryName,
|
||||
// _stateProvinceName,
|
||||
// _localityName,
|
||||
// _organizationName,
|
||||
// _organizationalUnitName,
|
||||
// _commonName, out);
|
||||
// out += subjectLen;
|
||||
|
||||
// public key
|
||||
ASN1Utils.appendPublicKey(_publicKey, out);
|
||||
out += publicKeyLen;
|
||||
// // public key
|
||||
// ASN1Utils.appendPublicKey(_publicKey, out);
|
||||
// out += publicKeyLen;
|
||||
|
||||
// terminator
|
||||
*out++ = 0xa0;
|
||||
*out++ = 0x00;
|
||||
// // terminator
|
||||
// *out++ = 0xa0;
|
||||
// *out++ = 0x00;
|
||||
|
||||
byte csrInfoSha256[64];
|
||||
byte signature[64];
|
||||
// byte csrInfoSha256[64];
|
||||
// byte signature[64];
|
||||
|
||||
if (!ECCX08::beginSHA256()) {
|
||||
return "";
|
||||
}
|
||||
// if (!ECCX08::beginSHA256()) {
|
||||
// return "";
|
||||
// }
|
||||
|
||||
for (int i = 0; i < (csrInfoHeaderLen + csrInfoLen); i += 64) {
|
||||
int chunkLength = (csrInfoHeaderLen + csrInfoLen) - i;
|
||||
// for (int i = 0; i < (csrInfoHeaderLen + csrInfoLen); i += 64) {
|
||||
// int chunkLength = (csrInfoHeaderLen + csrInfoLen) - i;
|
||||
|
||||
if (chunkLength > 64) {
|
||||
chunkLength = 64;
|
||||
}
|
||||
// if (chunkLength > 64) {
|
||||
// chunkLength = 64;
|
||||
// }
|
||||
|
||||
if (chunkLength == 64) {
|
||||
if (!ECCX08::updateSHA256(&csrInfo[i])) {
|
||||
return "";
|
||||
}
|
||||
} else {
|
||||
if (!ECCX08::endSHA256(&csrInfo[i], chunkLength, csrInfoSha256)) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (chunkLength == 64) {
|
||||
// if (!ECCX08::updateSHA256(&csrInfo[i])) {
|
||||
// return "";
|
||||
// }
|
||||
// } else {
|
||||
// if (!ECCX08::endSHA256(&csrInfo[i], chunkLength, csrInfoSha256)) {
|
||||
// return "";
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if (!ECCX08::ecSign(_slot, csrInfoSha256, signature)) {
|
||||
return "";
|
||||
}
|
||||
// if (!ECCX08::ecSign(_slot, csrInfoSha256, signature)) {
|
||||
// return "";
|
||||
// }
|
||||
|
||||
int signatureLen = ASN1Utils.signatureLength(signature);
|
||||
int csrLen = csrInfoHeaderLen + csrInfoLen + signatureLen;
|
||||
int csrHeaderLen = ASN1Utils.sequenceHeaderLength(csrLen);
|
||||
// int signatureLen = ASN1Utils.signatureLength(signature);
|
||||
// int csrLen = csrInfoHeaderLen + csrInfoLen + signatureLen;
|
||||
// int csrHeaderLen = ASN1Utils.sequenceHeaderLength(csrLen);
|
||||
|
||||
byte csr[csrLen + csrHeaderLen];
|
||||
out = csr;
|
||||
// byte csr[csrLen + csrHeaderLen];
|
||||
// out = csr;
|
||||
|
||||
ASN1Utils.appendSequenceHeader(csrLen, out);
|
||||
out += csrHeaderLen;
|
||||
// ASN1Utils.appendSequenceHeader(csrLen, out);
|
||||
// out += csrHeaderLen;
|
||||
|
||||
// info
|
||||
memcpy(out, csrInfo, csrInfoHeaderLen + csrInfoLen);
|
||||
out += (csrInfoHeaderLen + csrInfoLen);
|
||||
// // info
|
||||
// memcpy(out, csrInfo, csrInfoHeaderLen + csrInfoLen);
|
||||
// out += (csrInfoHeaderLen + csrInfoLen);
|
||||
|
||||
// signature
|
||||
ASN1Utils.appendSignature(signature, out);
|
||||
out += signatureLen;
|
||||
// // signature
|
||||
// ASN1Utils.appendSignature(signature, out);
|
||||
// out += signatureLen;
|
||||
|
||||
return PEMUtils.base64Encode(csr, csrLen + csrHeaderLen, "-----BEGIN CERTIFICATE REQUEST-----\n", "\n-----END CERTIFICATE REQUEST-----\n");
|
||||
}
|
||||
// return PEMUtils.base64Encode(csr, csrLen + csrHeaderLen, "-----BEGIN CERTIFICATE REQUEST-----\n", "\n-----END CERTIFICATE REQUEST-----\n");
|
||||
// }
|
||||
|
||||
void ECCX08CSRClass::setCountryName(const char *countryName)
|
||||
{
|
||||
_countryName = countryName;
|
||||
}
|
||||
// void ECCX08CSRClass::setCountryName(const char *countryName)
|
||||
// {
|
||||
// _countryName = countryName;
|
||||
// }
|
||||
|
||||
void ECCX08CSRClass::setStateProvinceName(const char* stateProvinceName)
|
||||
{
|
||||
_stateProvinceName = stateProvinceName;
|
||||
}
|
||||
// void ECCX08CSRClass::setStateProvinceName(const char* stateProvinceName)
|
||||
// {
|
||||
// _stateProvinceName = stateProvinceName;
|
||||
// }
|
||||
|
||||
void ECCX08CSRClass::setLocalityName(const char* localityName)
|
||||
{
|
||||
_localityName = localityName;
|
||||
}
|
||||
// void ECCX08CSRClass::setLocalityName(const char* localityName)
|
||||
// {
|
||||
// _localityName = localityName;
|
||||
// }
|
||||
|
||||
void ECCX08CSRClass::setOrganizationName(const char* organizationName)
|
||||
{
|
||||
_organizationName = organizationName;
|
||||
}
|
||||
// void ECCX08CSRClass::setOrganizationName(const char* organizationName)
|
||||
// {
|
||||
// _organizationName = organizationName;
|
||||
// }
|
||||
|
||||
void ECCX08CSRClass::setOrganizationalUnitName(const char* organizationalUnitName)
|
||||
{
|
||||
_organizationalUnitName = organizationalUnitName;
|
||||
}
|
||||
// void ECCX08CSRClass::setOrganizationalUnitName(const char* organizationalUnitName)
|
||||
// {
|
||||
// _organizationalUnitName = organizationalUnitName;
|
||||
// }
|
||||
|
||||
void ECCX08CSRClass::setCommonName(const char* commonName)
|
||||
{
|
||||
_commonName = commonName;
|
||||
}
|
||||
// void ECCX08CSRClass::setCommonName(const char* commonName)
|
||||
// {
|
||||
// _commonName = commonName;
|
||||
// }
|
||||
|
||||
#if !defined(ECCX08_DISABLE_CSR)
|
||||
ECCX08CSRClass ECCX08CSR;
|
||||
#endif
|
||||
// #if !defined(ECCX08_DISABLE_CSR)
|
||||
// ECCX08CSRClass ECCX08CSR;
|
||||
// #endif
|
||||
|
||||
@@ -1,66 +1,66 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#ifndef _ECCX08_CSR_H_
|
||||
#define _ECCX08_CSR_H_
|
||||
// #ifndef _ECCX08_CSR_H_
|
||||
// #define _ECCX08_CSR_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
// #include <Arduino.h>
|
||||
|
||||
class ECCX08CSRClass {
|
||||
public:
|
||||
ECCX08CSRClass();
|
||||
virtual ~ECCX08CSRClass();
|
||||
// class ECCX08CSRClass {
|
||||
// public:
|
||||
// ECCX08CSRClass();
|
||||
// virtual ~ECCX08CSRClass();
|
||||
|
||||
int begin(int slot, bool newPrivateKey = true);
|
||||
String end();
|
||||
// int begin(int slot, bool newPrivateKey = true);
|
||||
// String end();
|
||||
|
||||
void setCountryName(const char *countryName);
|
||||
void setCountryName(const String& countryName) { setCountryName(countryName.c_str()); }
|
||||
// void setCountryName(const char *countryName);
|
||||
// void setCountryName(const String& countryName) { setCountryName(countryName.c_str()); }
|
||||
|
||||
void setStateProvinceName(const char* stateProvinceName);
|
||||
void setStateProvinceName(const String& stateProvinceName) { setStateProvinceName(stateProvinceName.c_str()); }
|
||||
// void setStateProvinceName(const char* stateProvinceName);
|
||||
// void setStateProvinceName(const String& stateProvinceName) { setStateProvinceName(stateProvinceName.c_str()); }
|
||||
|
||||
void setLocalityName(const char* localityName);
|
||||
void setLocalityName(const String& localityName) { setLocalityName(localityName.c_str()); }
|
||||
// void setLocalityName(const char* localityName);
|
||||
// void setLocalityName(const String& localityName) { setLocalityName(localityName.c_str()); }
|
||||
|
||||
void setOrganizationName(const char* organizationName);
|
||||
void setOrganizationName(const String& organizationName) { setOrganizationName(organizationName.c_str()); }
|
||||
// void setOrganizationName(const char* organizationName);
|
||||
// void setOrganizationName(const String& organizationName) { setOrganizationName(organizationName.c_str()); }
|
||||
|
||||
void setOrganizationalUnitName(const char* organizationalUnitName);
|
||||
void setOrganizationalUnitName(const String& organizationalUnitName) { setOrganizationalUnitName(organizationalUnitName.c_str()); }
|
||||
// void setOrganizationalUnitName(const char* organizationalUnitName);
|
||||
// void setOrganizationalUnitName(const String& organizationalUnitName) { setOrganizationalUnitName(organizationalUnitName.c_str()); }
|
||||
|
||||
void setCommonName(const char* commonName);
|
||||
void setCommonName(const String& commonName) { setCommonName(commonName.c_str()); }
|
||||
// void setCommonName(const char* commonName);
|
||||
// void setCommonName(const String& commonName) { setCommonName(commonName.c_str()); }
|
||||
|
||||
private:
|
||||
int _slot;
|
||||
// private:
|
||||
// int _slot;
|
||||
|
||||
String _countryName;
|
||||
String _stateProvinceName;
|
||||
String _localityName;
|
||||
String _organizationName;
|
||||
String _organizationalUnitName;
|
||||
String _commonName;
|
||||
// String _countryName;
|
||||
// String _stateProvinceName;
|
||||
// String _localityName;
|
||||
// String _organizationName;
|
||||
// String _organizationalUnitName;
|
||||
// String _commonName;
|
||||
|
||||
byte _publicKey[64];
|
||||
};
|
||||
// byte _publicKey[64];
|
||||
// };
|
||||
|
||||
extern ECCX08CSRClass ECCX08CSR;
|
||||
// extern ECCX08CSRClass ECCX08CSR;
|
||||
|
||||
#endif
|
||||
// #endif
|
||||
|
||||
@@ -1,107 +1,107 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#ifndef _ECCX08_DEFAULT_TLS_CONFIG_H_
|
||||
#define _ECCX08_DEFAULT_TLS_CONFIG_H_
|
||||
// #ifndef _ECCX08_DEFAULT_TLS_CONFIG_H_
|
||||
// #define _ECCX08_DEFAULT_TLS_CONFIG_H_
|
||||
|
||||
const byte ECCX08_DEFAULT_TLS_CONFIG[128] = {
|
||||
// Read only - start
|
||||
// SN[0:3]
|
||||
0x01, 0x23, 0x00, 0x00,
|
||||
// RevNum
|
||||
0x00, 0x00, 0x50, 0x00,
|
||||
// SN[4:8]
|
||||
0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
// Reserved
|
||||
0xC0,
|
||||
// I2C_Enable
|
||||
0x71,
|
||||
// Reserved
|
||||
0x00,
|
||||
// Read only - end
|
||||
// I2C_Address
|
||||
0xC0,
|
||||
// Reserved
|
||||
0x00,
|
||||
// OTPmode
|
||||
0x55,
|
||||
// ChipMode
|
||||
0x00,
|
||||
// SlotConfig
|
||||
0x83, 0x20, // External Signatures | Internal Signatures | IsSecret | Write Configure Never, Default: 0x83, 0x20,
|
||||
0x87, 0x20, // External Signatures | Internal Signatures | ECDH | IsSecret | Write Configure Never, Default: 0x87, 0x20,
|
||||
0x87, 0x20, // External Signatures | Internal Signatures | ECDH | IsSecret | Write Configure Never, Default: 0x8F, 0x20,
|
||||
0x87, 0x2F, // External Signatures | Internal Signatures | ECDH | IsSecret | WriteKey all slots | Write Configure Never, Default: 0xC4, 0x8F,
|
||||
0x87, 0x2F, // External Signatures | Internal Signatures | ECDH | IsSecret | WriteKey all slots | Write Configure Never, Default: 0x8F, 0x8F,
|
||||
0x8F, 0x8F,
|
||||
0x9F, 0x8F,
|
||||
0xAF, 0x8F,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0x00, 0x00,
|
||||
0xAF, 0x8F,
|
||||
// Counter[0]
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
|
||||
// Counter[1]
|
||||
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
|
||||
// LastKeyUse
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
0xFF, 0xFF, 0xFF, 0xFF,
|
||||
// Write via commands only - start
|
||||
// UserExtra
|
||||
0x00,
|
||||
// Selector
|
||||
0x00,
|
||||
// LockValue
|
||||
0x55,
|
||||
// LockConfig
|
||||
0x55,
|
||||
// SlotLocked
|
||||
0xFF, 0xFF,
|
||||
// Write via commands only - end
|
||||
// RFU
|
||||
0x00, 0x00,
|
||||
// X509format
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
// KeyConfig
|
||||
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
|
||||
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
|
||||
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
|
||||
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
|
||||
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
|
||||
0x1C, 0x00,
|
||||
0x1C, 0x00,
|
||||
0x1C, 0x00,
|
||||
0x3C, 0x00,
|
||||
0x3C, 0x00,
|
||||
0x3C, 0x00,
|
||||
0x3C, 0x00,
|
||||
0x3C, 0x00,
|
||||
0x3C, 0x00,
|
||||
0x3C, 0x00,
|
||||
0x1C, 0x00
|
||||
};
|
||||
// const byte ECCX08_DEFAULT_TLS_CONFIG[128] = {
|
||||
// // Read only - start
|
||||
// // SN[0:3]
|
||||
// 0x01, 0x23, 0x00, 0x00,
|
||||
// // RevNum
|
||||
// 0x00, 0x00, 0x50, 0x00,
|
||||
// // SN[4:8]
|
||||
// 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
// // Reserved
|
||||
// 0xC0,
|
||||
// // I2C_Enable
|
||||
// 0x71,
|
||||
// // Reserved
|
||||
// 0x00,
|
||||
// // Read only - end
|
||||
// // I2C_Address
|
||||
// 0xC0,
|
||||
// // Reserved
|
||||
// 0x00,
|
||||
// // OTPmode
|
||||
// 0x55,
|
||||
// // ChipMode
|
||||
// 0x00,
|
||||
// // SlotConfig
|
||||
// 0x83, 0x20, // External Signatures | Internal Signatures | IsSecret | Write Configure Never, Default: 0x83, 0x20,
|
||||
// 0x87, 0x20, // External Signatures | Internal Signatures | ECDH | IsSecret | Write Configure Never, Default: 0x87, 0x20,
|
||||
// 0x87, 0x20, // External Signatures | Internal Signatures | ECDH | IsSecret | Write Configure Never, Default: 0x8F, 0x20,
|
||||
// 0x87, 0x2F, // External Signatures | Internal Signatures | ECDH | IsSecret | WriteKey all slots | Write Configure Never, Default: 0xC4, 0x8F,
|
||||
// 0x87, 0x2F, // External Signatures | Internal Signatures | ECDH | IsSecret | WriteKey all slots | Write Configure Never, Default: 0x8F, 0x8F,
|
||||
// 0x8F, 0x8F,
|
||||
// 0x9F, 0x8F,
|
||||
// 0xAF, 0x8F,
|
||||
// 0x00, 0x00,
|
||||
// 0x00, 0x00,
|
||||
// 0x00, 0x00,
|
||||
// 0x00, 0x00,
|
||||
// 0x00, 0x00,
|
||||
// 0x00, 0x00,
|
||||
// 0x00, 0x00,
|
||||
// 0xAF, 0x8F,
|
||||
// // Counter[0]
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
|
||||
// // Counter[1]
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
|
||||
// // LastKeyUse
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
// // Write via commands only - start
|
||||
// // UserExtra
|
||||
// 0x00,
|
||||
// // Selector
|
||||
// 0x00,
|
||||
// // LockValue
|
||||
// 0x55,
|
||||
// // LockConfig
|
||||
// 0x55,
|
||||
// // SlotLocked
|
||||
// 0xFF, 0xFF,
|
||||
// // Write via commands only - end
|
||||
// // RFU
|
||||
// 0x00, 0x00,
|
||||
// // X509format
|
||||
// 0x00, 0x00, 0x00, 0x00,
|
||||
// // KeyConfig
|
||||
// 0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
|
||||
// 0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
|
||||
// 0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
|
||||
// 0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
|
||||
// 0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
|
||||
// 0x1C, 0x00,
|
||||
// 0x1C, 0x00,
|
||||
// 0x1C, 0x00,
|
||||
// 0x3C, 0x00,
|
||||
// 0x3C, 0x00,
|
||||
// 0x3C, 0x00,
|
||||
// 0x3C, 0x00,
|
||||
// 0x3C, 0x00,
|
||||
// 0x3C, 0x00,
|
||||
// 0x3C, 0x00,
|
||||
// 0x1C, 0x00
|
||||
// };
|
||||
|
||||
#endif
|
||||
// #endif
|
||||
|
||||
@@ -1,166 +1,166 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#include "ECCX08Config.h"
|
||||
#include "ECCX08.h"
|
||||
// #include "ECCX08Config.h"
|
||||
// #include "ECCX08.h"
|
||||
|
||||
#include "ASN1Utils.h"
|
||||
#include "PEMUtils.h"
|
||||
// #include "ASN1Utils.h"
|
||||
// #include "PEMUtils.h"
|
||||
|
||||
#include "ECCX08JWS.h"
|
||||
// #include "ECCX08JWS.h"
|
||||
|
||||
static String base64urlEncode(const byte in[], unsigned int length)
|
||||
{
|
||||
static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
|
||||
// static String base64urlEncode(const byte in[], unsigned int length)
|
||||
// {
|
||||
// static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
|
||||
|
||||
int b;
|
||||
String out;
|
||||
// int b;
|
||||
// String out;
|
||||
|
||||
int reserveLength = 4 * ((length + 2) / 3);
|
||||
out.reserve(reserveLength);
|
||||
// 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];
|
||||
// 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];
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
// while (out.lastIndexOf('=') != -1) {
|
||||
// out.remove(out.length() - 1);
|
||||
// }
|
||||
|
||||
return out;
|
||||
}
|
||||
// return out;
|
||||
// }
|
||||
|
||||
ECCX08JWSClass::ECCX08JWSClass()
|
||||
{
|
||||
}
|
||||
// ECCX08JWSClass::ECCX08JWSClass()
|
||||
// {
|
||||
// }
|
||||
|
||||
ECCX08JWSClass::~ECCX08JWSClass()
|
||||
{
|
||||
}
|
||||
// ECCX08JWSClass::~ECCX08JWSClass()
|
||||
// {
|
||||
// }
|
||||
|
||||
String ECCX08JWSClass::publicKey(int slot, bool newPrivateKey)
|
||||
{
|
||||
if (slot < 0 || slot > 8) {
|
||||
return "";
|
||||
}
|
||||
// String ECCX08JWSClass::publicKey(int slot, bool newPrivateKey)
|
||||
// {
|
||||
// if (slot < 0 || slot > 8) {
|
||||
// return "";
|
||||
// }
|
||||
|
||||
byte publicKey[64];
|
||||
// byte publicKey[64];
|
||||
|
||||
if (newPrivateKey) {
|
||||
if (!ECCX08::generatePrivateKey(slot, publicKey)) {
|
||||
return "";
|
||||
}
|
||||
} else {
|
||||
if (!ECCX08::generatePublicKey(slot, publicKey)) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
// if (newPrivateKey) {
|
||||
// if (!ECCX08::generatePrivateKey(slot, publicKey)) {
|
||||
// return "";
|
||||
// }
|
||||
// } else {
|
||||
// if (!ECCX08::generatePublicKey(slot, publicKey)) {
|
||||
// return "";
|
||||
// }
|
||||
// }
|
||||
|
||||
int length = ASN1Utils.publicKeyLength();
|
||||
byte out[length];
|
||||
// int length = ASN1Utils.publicKeyLength();
|
||||
// byte out[length];
|
||||
|
||||
ASN1Utils.appendPublicKey(publicKey, out);
|
||||
// ASN1Utils.appendPublicKey(publicKey, out);
|
||||
|
||||
return PEMUtils.base64Encode(out, length, "-----BEGIN PUBLIC KEY-----\n", "\n-----END PUBLIC KEY-----\n");
|
||||
}
|
||||
// return PEMUtils.base64Encode(out, length, "-----BEGIN PUBLIC KEY-----\n", "\n-----END PUBLIC KEY-----\n");
|
||||
// }
|
||||
|
||||
String ECCX08JWSClass::sign(int slot, const char* header, const char* payload)
|
||||
{
|
||||
if (slot < 0 || slot > 8) {
|
||||
return "";
|
||||
}
|
||||
// String ECCX08JWSClass::sign(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 encodedHeader = base64urlEncode((const byte*)header, strlen(header));
|
||||
// String encodedPayload = base64urlEncode((const byte*)payload, strlen(payload));
|
||||
|
||||
String toSign;
|
||||
toSign.reserve(encodedHeader.length() + 1 + encodedPayload.length());
|
||||
// String toSign;
|
||||
// toSign.reserve(encodedHeader.length() + 1 + encodedPayload.length());
|
||||
|
||||
toSign += encodedHeader;
|
||||
toSign += '.';
|
||||
toSign += encodedPayload;
|
||||
// toSign += encodedHeader;
|
||||
// toSign += '.';
|
||||
// toSign += encodedPayload;
|
||||
|
||||
|
||||
byte toSignSha256[32];
|
||||
byte signature[64];
|
||||
// byte toSignSha256[32];
|
||||
// byte signature[64];
|
||||
|
||||
if (!ECCX08::beginSHA256()) {
|
||||
return "";
|
||||
}
|
||||
// if (!ECCX08::beginSHA256()) {
|
||||
// return "";
|
||||
// }
|
||||
|
||||
for (unsigned int i = 0; i < toSign.length(); i += 64) {
|
||||
int chunkLength = toSign.length() - i;
|
||||
// for (unsigned int i = 0; i < toSign.length(); i += 64) {
|
||||
// int chunkLength = toSign.length() - i;
|
||||
|
||||
if (chunkLength > 64) {
|
||||
chunkLength = 64;
|
||||
}
|
||||
// if (chunkLength > 64) {
|
||||
// chunkLength = 64;
|
||||
// }
|
||||
|
||||
if (chunkLength == 64) {
|
||||
if (!ECCX08::updateSHA256((const byte*)toSign.c_str() + i)) {
|
||||
return "";
|
||||
}
|
||||
} else {
|
||||
if (!ECCX08::endSHA256((const byte*)toSign.c_str() + i, chunkLength, toSignSha256)) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (chunkLength == 64) {
|
||||
// if (!ECCX08::updateSHA256((const byte*)toSign.c_str() + i)) {
|
||||
// return "";
|
||||
// }
|
||||
// } else {
|
||||
// if (!ECCX08::endSHA256((const byte*)toSign.c_str() + i, chunkLength, toSignSha256)) {
|
||||
// return "";
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if (!ECCX08::ecSign(slot, toSignSha256, signature)) {
|
||||
return "";
|
||||
}
|
||||
// if (!ECCX08::ecSign(slot, toSignSha256, signature)) {
|
||||
// return "";
|
||||
// }
|
||||
|
||||
String encodedSignature = base64urlEncode(signature, sizeof(signature));
|
||||
// String encodedSignature = base64urlEncode(signature, sizeof(signature));
|
||||
|
||||
String result;
|
||||
result.reserve(toSign.length() + 1 + encodedSignature.length());
|
||||
// String result;
|
||||
// result.reserve(toSign.length() + 1 + encodedSignature.length());
|
||||
|
||||
result += toSign;
|
||||
result += '.';
|
||||
result += encodedSignature;
|
||||
// result += toSign;
|
||||
// result += '.';
|
||||
// result += encodedSignature;
|
||||
|
||||
return result;
|
||||
}
|
||||
// return result;
|
||||
// }
|
||||
|
||||
String ECCX08JWSClass::sign(int slot, const String& header, const String& payload)
|
||||
{
|
||||
return sign(slot, header.c_str(), payload.c_str());
|
||||
}
|
||||
// String ECCX08JWSClass::sign(int slot, const String& header, const String& payload)
|
||||
// {
|
||||
// return sign(slot, header.c_str(), payload.c_str());
|
||||
// }
|
||||
|
||||
#if !defined(ECCX08_DISABLE_JWS)
|
||||
ECCX08JWSClass ECCX08JWS;
|
||||
#endif
|
||||
// #if !defined(ECCX08_DISABLE_JWS)
|
||||
// ECCX08JWSClass ECCX08JWS;
|
||||
// #endif
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#ifndef _ECCX08_JWS_H_
|
||||
#define _ECCX08_JWS_H_
|
||||
// #ifndef _ECCX08_JWS_H_
|
||||
// #define _ECCX08_JWS_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
// #include <Arduino.h>
|
||||
|
||||
class ECCX08JWSClass {
|
||||
public:
|
||||
ECCX08JWSClass();
|
||||
virtual ~ECCX08JWSClass();
|
||||
// class ECCX08JWSClass {
|
||||
// public:
|
||||
// ECCX08JWSClass();
|
||||
// virtual ~ECCX08JWSClass();
|
||||
|
||||
String publicKey(int slot, bool newPrivateKey = true);
|
||||
// String publicKey(int slot, bool newPrivateKey = true);
|
||||
|
||||
String sign(int slot, const char* header, const char* payload);
|
||||
String sign(int slot, const String& header, const String& payload);
|
||||
};
|
||||
// String sign(int slot, const char* header, const char* payload);
|
||||
// String sign(int slot, const String& header, const String& payload);
|
||||
// };
|
||||
|
||||
extern ECCX08JWSClass ECCX08JWS;
|
||||
// extern ECCX08JWSClass ECCX08JWS;
|
||||
|
||||
#endif
|
||||
// #endif
|
||||
|
||||
@@ -1,412 +1,412 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#include "ECCX08Config.h"
|
||||
#include "ECCX08.h"
|
||||
// #include "ECCX08Config.h"
|
||||
// #include "ECCX08.h"
|
||||
|
||||
extern "C" {
|
||||
#include "sha1.h"
|
||||
}
|
||||
#include "ASN1Utils.h"
|
||||
#include "PEMUtils.h"
|
||||
// extern "C" {
|
||||
// #include "sha1.h"
|
||||
// }
|
||||
// #include "ASN1Utils.h"
|
||||
// #include "PEMUtils.h"
|
||||
|
||||
#include "ECCX08SelfSignedCert.h"
|
||||
// #include "ECCX08SelfSignedCert.h"
|
||||
|
||||
struct __attribute__((__packed__)) CompressedCert {
|
||||
uint8_t signature[64];
|
||||
struct {
|
||||
uint8_t year:5;
|
||||
uint8_t month:4;
|
||||
uint8_t day:5;
|
||||
uint8_t hour:5;
|
||||
uint8_t expires:5;
|
||||
} dates;
|
||||
uint8_t unused[5];
|
||||
};
|
||||
// struct __attribute__((__packed__)) CompressedCert {
|
||||
// uint8_t signature[64];
|
||||
// struct {
|
||||
// uint8_t year:5;
|
||||
// uint8_t month:4;
|
||||
// uint8_t day:5;
|
||||
// uint8_t hour:5;
|
||||
// uint8_t expires:5;
|
||||
// } dates;
|
||||
// uint8_t unused[5];
|
||||
// };
|
||||
|
||||
static const uint8_t DEFAULT_SERIAL_NUMBER[] = { 0x01 };
|
||||
// static const uint8_t DEFAULT_SERIAL_NUMBER[] = { 0x01 };
|
||||
|
||||
ECCX08SelfSignedCertClass::ECCX08SelfSignedCertClass() :
|
||||
_serialNumber(DEFAULT_SERIAL_NUMBER),
|
||||
_serialNumberLength(sizeof(DEFAULT_SERIAL_NUMBER)),
|
||||
_bytes(NULL),
|
||||
_length(0)
|
||||
{
|
||||
}
|
||||
// ECCX08SelfSignedCertClass::ECCX08SelfSignedCertClass() :
|
||||
// _serialNumber(DEFAULT_SERIAL_NUMBER),
|
||||
// _serialNumberLength(sizeof(DEFAULT_SERIAL_NUMBER)),
|
||||
// _bytes(NULL),
|
||||
// _length(0)
|
||||
// {
|
||||
// }
|
||||
|
||||
ECCX08SelfSignedCertClass::~ECCX08SelfSignedCertClass()
|
||||
{
|
||||
if (_bytes) {
|
||||
free(_bytes);
|
||||
_bytes = NULL;
|
||||
}
|
||||
}
|
||||
// ECCX08SelfSignedCertClass::~ECCX08SelfSignedCertClass()
|
||||
// {
|
||||
// if (_bytes) {
|
||||
// free(_bytes);
|
||||
// _bytes = NULL;
|
||||
// }
|
||||
// }
|
||||
|
||||
int ECCX08SelfSignedCertClass::beginStorage(int keySlot, int dateAndSignatureSlot, bool newKey)
|
||||
{
|
||||
if (keySlot < 0 || keySlot > 8) {
|
||||
return 0;
|
||||
}
|
||||
// int ECCX08SelfSignedCertClass::beginStorage(int keySlot, int dateAndSignatureSlot, bool newKey)
|
||||
// {
|
||||
// if (keySlot < 0 || keySlot > 8) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
if (dateAndSignatureSlot < 8 || dateAndSignatureSlot > 15) {
|
||||
return 0;
|
||||
}
|
||||
// if (dateAndSignatureSlot < 8 || dateAndSignatureSlot > 15) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
_keySlot = keySlot;
|
||||
_dateAndSignatureSlot = dateAndSignatureSlot;
|
||||
// _keySlot = keySlot;
|
||||
// _dateAndSignatureSlot = dateAndSignatureSlot;
|
||||
|
||||
byte publicKey[64];
|
||||
// byte publicKey[64];
|
||||
|
||||
if (newKey && !ECCX08::generatePrivateKey(_keySlot, publicKey)) {
|
||||
return 0;
|
||||
}
|
||||
// if (newKey && !ECCX08::generatePrivateKey(_keySlot, publicKey)) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
return 1;
|
||||
}
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
String ECCX08SelfSignedCertClass::endStorage()
|
||||
{
|
||||
if (!buildCert(true)) {
|
||||
return "";
|
||||
}
|
||||
// String ECCX08SelfSignedCertClass::endStorage()
|
||||
// {
|
||||
// if (!buildCert(true)) {
|
||||
// return "";
|
||||
// }
|
||||
|
||||
return PEMUtils.base64Encode(_bytes, _length, "-----BEGIN CERTIFICATE-----\n", "\n-----END CERTIFICATE-----\n");
|
||||
}
|
||||
// return PEMUtils.base64Encode(_bytes, _length, "-----BEGIN CERTIFICATE-----\n", "\n-----END CERTIFICATE-----\n");
|
||||
// }
|
||||
|
||||
int ECCX08SelfSignedCertClass::beginReconstruction(int keySlot, int dateAndSignatureSlot)
|
||||
{
|
||||
if (keySlot < 0 || keySlot > 8) {
|
||||
return 0;
|
||||
}
|
||||
// int ECCX08SelfSignedCertClass::beginReconstruction(int keySlot, int dateAndSignatureSlot)
|
||||
// {
|
||||
// if (keySlot < 0 || keySlot > 8) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
if (dateAndSignatureSlot < 8 || dateAndSignatureSlot > 15) {
|
||||
return 0;
|
||||
}
|
||||
// if (dateAndSignatureSlot < 8 || dateAndSignatureSlot > 15) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
_keySlot = keySlot;
|
||||
_dateAndSignatureSlot = dateAndSignatureSlot;
|
||||
// _keySlot = keySlot;
|
||||
// _dateAndSignatureSlot = dateAndSignatureSlot;
|
||||
|
||||
if (!ECCX08::readSlot(_dateAndSignatureSlot, _temp, sizeof(_temp))) {
|
||||
return 0;
|
||||
}
|
||||
// if (!ECCX08::readSlot(_dateAndSignatureSlot, _temp, sizeof(_temp))) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
return 1;
|
||||
}
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
int ECCX08SelfSignedCertClass::endReconstruction()
|
||||
{
|
||||
if (!buildCert(false)) {
|
||||
return 0;
|
||||
}
|
||||
// int ECCX08SelfSignedCertClass::endReconstruction()
|
||||
// {
|
||||
// if (!buildCert(false)) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
return 1;
|
||||
}
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
uint8_t* ECCX08SelfSignedCertClass::bytes()
|
||||
{
|
||||
return _bytes;
|
||||
}
|
||||
// uint8_t* ECCX08SelfSignedCertClass::bytes()
|
||||
// {
|
||||
// return _bytes;
|
||||
// }
|
||||
|
||||
int ECCX08SelfSignedCertClass::length()
|
||||
{
|
||||
return _length;
|
||||
}
|
||||
// int ECCX08SelfSignedCertClass::length()
|
||||
// {
|
||||
// return _length;
|
||||
// }
|
||||
|
||||
String ECCX08SelfSignedCertClass::sha1()
|
||||
{
|
||||
char result[20 + 1];
|
||||
// String ECCX08SelfSignedCertClass::sha1()
|
||||
// {
|
||||
// char result[20 + 1];
|
||||
|
||||
SHA1(result, (const char*)_bytes, _length);
|
||||
// SHA1(result, (const char*)_bytes, _length);
|
||||
|
||||
String sha1Str;
|
||||
// String sha1Str;
|
||||
|
||||
sha1Str.reserve(40);
|
||||
// sha1Str.reserve(40);
|
||||
|
||||
for (int i = 0; i < 20; i++) {
|
||||
uint8_t b = result[i];
|
||||
// for (int i = 0; i < 20; i++) {
|
||||
// uint8_t b = result[i];
|
||||
|
||||
if (b < 16) {
|
||||
sha1Str += '0';
|
||||
}
|
||||
sha1Str += String(b, HEX);
|
||||
}
|
||||
// if (b < 16) {
|
||||
// sha1Str += '0';
|
||||
// }
|
||||
// sha1Str += String(b, HEX);
|
||||
// }
|
||||
|
||||
return sha1Str;
|
||||
}
|
||||
// return sha1Str;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setIssueYear(int issueYear)
|
||||
{
|
||||
struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
// void ECCX08SelfSignedCertClass::setIssueYear(int issueYear)
|
||||
// {
|
||||
// struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
|
||||
compressedCert->dates.year = (issueYear - 2000);
|
||||
}
|
||||
// compressedCert->dates.year = (issueYear - 2000);
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setIssueMonth(int issueMonth)
|
||||
{
|
||||
struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
// void ECCX08SelfSignedCertClass::setIssueMonth(int issueMonth)
|
||||
// {
|
||||
// struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
|
||||
compressedCert->dates.month = issueMonth;
|
||||
}
|
||||
// compressedCert->dates.month = issueMonth;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setIssueDay(int issueDay)
|
||||
{
|
||||
struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
// void ECCX08SelfSignedCertClass::setIssueDay(int issueDay)
|
||||
// {
|
||||
// struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
|
||||
compressedCert->dates.day = issueDay;
|
||||
}
|
||||
// compressedCert->dates.day = issueDay;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setIssueHour(int issueHour)
|
||||
{
|
||||
struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
// void ECCX08SelfSignedCertClass::setIssueHour(int issueHour)
|
||||
// {
|
||||
// struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
|
||||
compressedCert->dates.hour = issueHour;
|
||||
}
|
||||
// compressedCert->dates.hour = issueHour;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setExpireYears(int expireYears)
|
||||
{
|
||||
struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
// void ECCX08SelfSignedCertClass::setExpireYears(int expireYears)
|
||||
// {
|
||||
// struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
|
||||
compressedCert->dates.expires = expireYears;
|
||||
}
|
||||
// compressedCert->dates.expires = expireYears;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setSerialNumber(const byte serialNumber[], int length)
|
||||
{
|
||||
_serialNumber = serialNumber;
|
||||
_serialNumberLength = length;
|
||||
}
|
||||
// void ECCX08SelfSignedCertClass::setSerialNumber(const byte serialNumber[], int length)
|
||||
// {
|
||||
// _serialNumber = serialNumber;
|
||||
// _serialNumberLength = length;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setCountryName(const char *countryName)
|
||||
{
|
||||
_countryName = countryName;
|
||||
}
|
||||
// void ECCX08SelfSignedCertClass::setCountryName(const char *countryName)
|
||||
// {
|
||||
// _countryName = countryName;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setStateProvinceName(const char* stateProvinceName)
|
||||
{
|
||||
_stateProvinceName = stateProvinceName;
|
||||
}
|
||||
// void ECCX08SelfSignedCertClass::setStateProvinceName(const char* stateProvinceName)
|
||||
// {
|
||||
// _stateProvinceName = stateProvinceName;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setLocalityName(const char* localityName)
|
||||
{
|
||||
_localityName = localityName;
|
||||
}
|
||||
// void ECCX08SelfSignedCertClass::setLocalityName(const char* localityName)
|
||||
// {
|
||||
// _localityName = localityName;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setOrganizationName(const char* organizationName)
|
||||
{
|
||||
_organizationName = organizationName;
|
||||
}
|
||||
// void ECCX08SelfSignedCertClass::setOrganizationName(const char* organizationName)
|
||||
// {
|
||||
// _organizationName = organizationName;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setOrganizationalUnitName(const char* organizationalUnitName)
|
||||
{
|
||||
_organizationalUnitName = organizationalUnitName;
|
||||
}
|
||||
// void ECCX08SelfSignedCertClass::setOrganizationalUnitName(const char* organizationalUnitName)
|
||||
// {
|
||||
// _organizationalUnitName = organizationalUnitName;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::setCommonName(const char* commonName)
|
||||
{
|
||||
_commonName = commonName;
|
||||
}
|
||||
// void ECCX08SelfSignedCertClass::setCommonName(const char* commonName)
|
||||
// {
|
||||
// _commonName = commonName;
|
||||
// }
|
||||
|
||||
int ECCX08SelfSignedCertClass::buildCert(bool buildSignature)
|
||||
{
|
||||
uint8_t publicKey[64];
|
||||
// int ECCX08SelfSignedCertClass::buildCert(bool buildSignature)
|
||||
// {
|
||||
// uint8_t publicKey[64];
|
||||
|
||||
if (!ECCX08::generatePublicKey(_keySlot, publicKey)) {
|
||||
return 0;
|
||||
}
|
||||
// if (!ECCX08::generatePublicKey(_keySlot, publicKey)) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
int certInfoLen = certInfoLength();
|
||||
int certInfoHeaderLen = ASN1Utils.sequenceHeaderLength(certInfoLen);
|
||||
// int certInfoLen = certInfoLength();
|
||||
// int certInfoHeaderLen = ASN1Utils.sequenceHeaderLength(certInfoLen);
|
||||
|
||||
uint8_t certInfo[certInfoLen + certInfoHeaderLen];
|
||||
// uint8_t certInfo[certInfoLen + certInfoHeaderLen];
|
||||
|
||||
appendCertInfo(publicKey, certInfo, certInfoLen);
|
||||
// appendCertInfo(publicKey, certInfo, certInfoLen);
|
||||
|
||||
if (buildSignature) {
|
||||
byte certInfoSha256[64];
|
||||
// if (buildSignature) {
|
||||
// byte certInfoSha256[64];
|
||||
|
||||
memset(certInfoSha256, 0x00, sizeof(certInfoSha256));
|
||||
// memset(certInfoSha256, 0x00, sizeof(certInfoSha256));
|
||||
|
||||
if (!ECCX08::beginSHA256()) {
|
||||
return 0;
|
||||
}
|
||||
// if (!ECCX08::beginSHA256()) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
for (int i = 0; i < (certInfoHeaderLen + certInfoLen); i += 64) {
|
||||
int chunkLength = (certInfoHeaderLen + certInfoLen) - i;
|
||||
// for (int i = 0; i < (certInfoHeaderLen + certInfoLen); i += 64) {
|
||||
// int chunkLength = (certInfoHeaderLen + certInfoLen) - i;
|
||||
|
||||
if (chunkLength > 64) {
|
||||
chunkLength = 64;
|
||||
}
|
||||
// if (chunkLength > 64) {
|
||||
// chunkLength = 64;
|
||||
// }
|
||||
|
||||
if (chunkLength == 64) {
|
||||
if (!ECCX08::updateSHA256(&certInfo[i])) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (!ECCX08::endSHA256(&certInfo[i], chunkLength, certInfoSha256)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if (chunkLength == 64) {
|
||||
// if (!ECCX08::updateSHA256(&certInfo[i])) {
|
||||
// return 0;
|
||||
// }
|
||||
// } else {
|
||||
// if (!ECCX08::endSHA256(&certInfo[i], chunkLength, certInfoSha256)) {
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if (!ECCX08::ecSign(_keySlot, certInfoSha256, _temp)) {
|
||||
return 0;
|
||||
}
|
||||
// if (!ECCX08::ecSign(_keySlot, certInfoSha256, _temp)) {
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
if (!ECCX08::writeSlot(_dateAndSignatureSlot, _temp, sizeof(_temp))) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// if (!ECCX08::writeSlot(_dateAndSignatureSlot, _temp, sizeof(_temp))) {
|
||||
// return 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
int signatureLen = ASN1Utils.signatureLength(_temp);
|
||||
// int signatureLen = ASN1Utils.signatureLength(_temp);
|
||||
|
||||
int certDataLen = certInfoLen + certInfoHeaderLen + signatureLen;
|
||||
int certDataHeaderLen = ASN1Utils.sequenceHeaderLength(certDataLen);
|
||||
// int certDataLen = certInfoLen + certInfoHeaderLen + signatureLen;
|
||||
// int certDataHeaderLen = ASN1Utils.sequenceHeaderLength(certDataLen);
|
||||
|
||||
_length = certDataLen + certDataHeaderLen;
|
||||
_bytes = (byte*)realloc(_bytes, _length);
|
||||
// _length = certDataLen + certDataHeaderLen;
|
||||
// _bytes = (byte*)realloc(_bytes, _length);
|
||||
|
||||
if (!_bytes) {
|
||||
_length = 0;
|
||||
return 0;
|
||||
}
|
||||
// if (!_bytes) {
|
||||
// _length = 0;
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
uint8_t* out = _bytes;
|
||||
// uint8_t* out = _bytes;
|
||||
|
||||
out += ASN1Utils.appendSequenceHeader(certDataLen, out);
|
||||
// out += ASN1Utils.appendSequenceHeader(certDataLen, out);
|
||||
|
||||
memcpy(out, certInfo, certInfoHeaderLen + certInfoLen);
|
||||
out += (certInfoHeaderLen + certInfoLen);
|
||||
// memcpy(out, certInfo, certInfoHeaderLen + certInfoLen);
|
||||
// out += (certInfoHeaderLen + certInfoLen);
|
||||
|
||||
// signature
|
||||
out += ASN1Utils.appendSignature(_temp, out);
|
||||
// // signature
|
||||
// out += ASN1Utils.appendSignature(_temp, out);
|
||||
|
||||
return 1;
|
||||
}
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
int ECCX08SelfSignedCertClass::certInfoLength()
|
||||
{
|
||||
struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
// int ECCX08SelfSignedCertClass::certInfoLength()
|
||||
// {
|
||||
// struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
|
||||
int year = (compressedCert->dates.year + 2000);
|
||||
int expireYears = compressedCert->dates.expires;
|
||||
// int year = (compressedCert->dates.year + 2000);
|
||||
// int expireYears = compressedCert->dates.expires;
|
||||
|
||||
int datesSize = 30;
|
||||
// int datesSize = 30;
|
||||
|
||||
if (year > 2049) {
|
||||
// two more bytes for GeneralizedTime
|
||||
datesSize += 2;
|
||||
}
|
||||
// if (year > 2049) {
|
||||
// // two more bytes for GeneralizedTime
|
||||
// datesSize += 2;
|
||||
// }
|
||||
|
||||
if ((year + expireYears) > 2049) {
|
||||
// two more bytes for GeneralizedTime
|
||||
datesSize += 2;
|
||||
}
|
||||
// if ((year + expireYears) > 2049) {
|
||||
// // two more bytes for GeneralizedTime
|
||||
// datesSize += 2;
|
||||
// }
|
||||
|
||||
int serialNumberLen = ASN1Utils.serialNumberLength(_serialNumber, _serialNumberLength);
|
||||
// int serialNumberLen = ASN1Utils.serialNumberLength(_serialNumber, _serialNumberLength);
|
||||
|
||||
int issuerAndSubjectLen = ASN1Utils.issuerOrSubjectLength(_countryName,
|
||||
_stateProvinceName,
|
||||
_localityName,
|
||||
_organizationName,
|
||||
_organizationalUnitName,
|
||||
_commonName);
|
||||
// int issuerAndSubjectLen = ASN1Utils.issuerOrSubjectLength(_countryName,
|
||||
// _stateProvinceName,
|
||||
// _localityName,
|
||||
// _organizationName,
|
||||
// _organizationalUnitName,
|
||||
// _commonName);
|
||||
|
||||
int issuerAndSubjectHeaderLen = ASN1Utils.sequenceHeaderLength(issuerAndSubjectLen);
|
||||
// int issuerAndSubjectHeaderLen = ASN1Utils.sequenceHeaderLength(issuerAndSubjectLen);
|
||||
|
||||
|
||||
int publicKeyLen = ASN1Utils.publicKeyLength();
|
||||
// int publicKeyLen = ASN1Utils.publicKeyLength();
|
||||
|
||||
|
||||
int certInfoLen = 5 + serialNumberLen + 12 +
|
||||
2 * (issuerAndSubjectHeaderLen + issuerAndSubjectLen) +
|
||||
(datesSize + 2) + publicKeyLen + 4;
|
||||
// int certInfoLen = 5 + serialNumberLen + 12 +
|
||||
// 2 * (issuerAndSubjectHeaderLen + issuerAndSubjectLen) +
|
||||
// (datesSize + 2) + publicKeyLen + 4;
|
||||
|
||||
return certInfoLen;
|
||||
}
|
||||
// return certInfoLen;
|
||||
// }
|
||||
|
||||
void ECCX08SelfSignedCertClass::appendCertInfo(uint8_t publicKey[], uint8_t buffer[], int length)
|
||||
{
|
||||
struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
uint8_t* out = buffer;
|
||||
// void ECCX08SelfSignedCertClass::appendCertInfo(uint8_t publicKey[], uint8_t buffer[], int length)
|
||||
// {
|
||||
// struct CompressedCert* compressedCert = (struct CompressedCert*)_temp;
|
||||
// uint8_t* out = buffer;
|
||||
|
||||
// dates
|
||||
int year = (compressedCert->dates.year + 2000);
|
||||
int month = compressedCert->dates.month;
|
||||
int day = compressedCert->dates.day;
|
||||
int hour = compressedCert->dates.hour;
|
||||
int expireYears = compressedCert->dates.expires;
|
||||
// // dates
|
||||
// int year = (compressedCert->dates.year + 2000);
|
||||
// int month = compressedCert->dates.month;
|
||||
// int day = compressedCert->dates.day;
|
||||
// int hour = compressedCert->dates.hour;
|
||||
// int expireYears = compressedCert->dates.expires;
|
||||
|
||||
out += ASN1Utils.appendSequenceHeader(length, out);
|
||||
// out += ASN1Utils.appendSequenceHeader(length, out);
|
||||
|
||||
// version
|
||||
*out++ = 0xA0;
|
||||
*out++ = 0x03;
|
||||
*out++ = 0x02;
|
||||
*out++ = 0x01;
|
||||
*out++ = 0x02;
|
||||
// // version
|
||||
// *out++ = 0xA0;
|
||||
// *out++ = 0x03;
|
||||
// *out++ = 0x02;
|
||||
// *out++ = 0x01;
|
||||
// *out++ = 0x02;
|
||||
|
||||
// serial number
|
||||
out += ASN1Utils.appendSerialNumber(_serialNumber, _serialNumberLength, out);
|
||||
// // serial number
|
||||
// out += ASN1Utils.appendSerialNumber(_serialNumber, _serialNumberLength, out);
|
||||
|
||||
out += ASN1Utils.appendEcdsaWithSHA256(out);
|
||||
// out += ASN1Utils.appendEcdsaWithSHA256(out);
|
||||
|
||||
// issuer
|
||||
int issuerAndSubjectLen = ASN1Utils.issuerOrSubjectLength(_countryName,
|
||||
_stateProvinceName,
|
||||
_localityName,
|
||||
_organizationName,
|
||||
_organizationalUnitName,
|
||||
_commonName);
|
||||
// // issuer
|
||||
// int issuerAndSubjectLen = ASN1Utils.issuerOrSubjectLength(_countryName,
|
||||
// _stateProvinceName,
|
||||
// _localityName,
|
||||
// _organizationName,
|
||||
// _organizationalUnitName,
|
||||
// _commonName);
|
||||
|
||||
out += ASN1Utils.appendSequenceHeader(issuerAndSubjectLen, out);
|
||||
ASN1Utils.appendIssuerOrSubject(_countryName,
|
||||
_stateProvinceName,
|
||||
_localityName,
|
||||
_organizationName,
|
||||
_organizationalUnitName,
|
||||
_commonName, out);
|
||||
out += issuerAndSubjectLen;
|
||||
// out += ASN1Utils.appendSequenceHeader(issuerAndSubjectLen, out);
|
||||
// ASN1Utils.appendIssuerOrSubject(_countryName,
|
||||
// _stateProvinceName,
|
||||
// _localityName,
|
||||
// _organizationName,
|
||||
// _organizationalUnitName,
|
||||
// _commonName, out);
|
||||
// out += issuerAndSubjectLen;
|
||||
|
||||
*out++ = ASN1_SEQUENCE;
|
||||
*out++ = 30 + ((year > 2049) ? 2 : 0) + (((year + expireYears) > 2049) ? 2 : 0);
|
||||
out += ASN1Utils.appendDate(year, month, day, hour, 0, 0, out);
|
||||
out += ASN1Utils.appendDate(year + expireYears, month, day, hour, 0, 0, out);
|
||||
// *out++ = ASN1_SEQUENCE;
|
||||
// *out++ = 30 + ((year > 2049) ? 2 : 0) + (((year + expireYears) > 2049) ? 2 : 0);
|
||||
// out += ASN1Utils.appendDate(year, month, day, hour, 0, 0, out);
|
||||
// out += ASN1Utils.appendDate(year + expireYears, month, day, hour, 0, 0, out);
|
||||
|
||||
// subject
|
||||
out += ASN1Utils.appendSequenceHeader(issuerAndSubjectLen, out);
|
||||
ASN1Utils.appendIssuerOrSubject(_countryName,
|
||||
_stateProvinceName,
|
||||
_localityName,
|
||||
_organizationName,
|
||||
_organizationalUnitName,
|
||||
_commonName, out);
|
||||
out += issuerAndSubjectLen;
|
||||
// // subject
|
||||
// out += ASN1Utils.appendSequenceHeader(issuerAndSubjectLen, out);
|
||||
// ASN1Utils.appendIssuerOrSubject(_countryName,
|
||||
// _stateProvinceName,
|
||||
// _localityName,
|
||||
// _organizationName,
|
||||
// _organizationalUnitName,
|
||||
// _commonName, out);
|
||||
// out += issuerAndSubjectLen;
|
||||
|
||||
// public key
|
||||
out += ASN1Utils.appendPublicKey(publicKey, out);
|
||||
// // public key
|
||||
// out += ASN1Utils.appendPublicKey(publicKey, out);
|
||||
|
||||
// null sequence
|
||||
*out++ = 0xA3;
|
||||
*out++ = 0x02;
|
||||
*out++ = 0x30;
|
||||
*out++ = 0x00;
|
||||
}
|
||||
// // null sequence
|
||||
// *out++ = 0xA3;
|
||||
// *out++ = 0x02;
|
||||
// *out++ = 0x30;
|
||||
// *out++ = 0x00;
|
||||
// }
|
||||
|
||||
#if !defined(ECCX08_DISABLE_SSC)
|
||||
ECCX08SelfSignedCertClass ECCX08SelfSignedCert;
|
||||
#endif
|
||||
// #if !defined(ECCX08_DISABLE_SSC)
|
||||
// ECCX08SelfSignedCertClass ECCX08SelfSignedCert;
|
||||
// #endif
|
||||
|
||||
@@ -1,93 +1,93 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#ifndef _ECCX08_SELF_SIGNED_CERT_H_
|
||||
#define _ECCX08_SELF_SIGNED_CERT_H_
|
||||
// #ifndef _ECCX08_SELF_SIGNED_CERT_H_
|
||||
// #define _ECCX08_SELF_SIGNED_CERT_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
// #include <Arduino.h>
|
||||
|
||||
class ECCX08SelfSignedCertClass {
|
||||
public:
|
||||
ECCX08SelfSignedCertClass();
|
||||
virtual ~ECCX08SelfSignedCertClass();
|
||||
// class ECCX08SelfSignedCertClass {
|
||||
// public:
|
||||
// ECCX08SelfSignedCertClass();
|
||||
// virtual ~ECCX08SelfSignedCertClass();
|
||||
|
||||
int beginStorage(int keySlot, int dateAndSignatureSlot, bool newKey);
|
||||
String endStorage();
|
||||
// int beginStorage(int keySlot, int dateAndSignatureSlot, bool newKey);
|
||||
// String endStorage();
|
||||
|
||||
int beginReconstruction(int keySlot, int dateAndSignatureSlot);
|
||||
int endReconstruction();
|
||||
// int beginReconstruction(int keySlot, int dateAndSignatureSlot);
|
||||
// int endReconstruction();
|
||||
|
||||
uint8_t* bytes();
|
||||
int length();
|
||||
// uint8_t* bytes();
|
||||
// int length();
|
||||
|
||||
String sha1();
|
||||
// String sha1();
|
||||
|
||||
void setIssueYear(int issueYear);
|
||||
void setIssueMonth(int issueMonth);
|
||||
void setIssueDay(int issueDay);
|
||||
void setIssueHour(int issueHour);
|
||||
void setExpireYears(int expireYears);
|
||||
void setSerialNumber(const uint8_t serialNumber[], int length);
|
||||
// void setIssueYear(int issueYear);
|
||||
// void setIssueMonth(int issueMonth);
|
||||
// void setIssueDay(int issueDay);
|
||||
// void setIssueHour(int issueHour);
|
||||
// void setExpireYears(int expireYears);
|
||||
// void setSerialNumber(const uint8_t serialNumber[], int length);
|
||||
|
||||
void setCountryName(const char *countryName);
|
||||
void setCountryName(const String& countryName) { setCountryName(countryName.c_str()); }
|
||||
// void setCountryName(const char *countryName);
|
||||
// void setCountryName(const String& countryName) { setCountryName(countryName.c_str()); }
|
||||
|
||||
void setStateProvinceName(const char* stateProvinceName);
|
||||
void setStateProvinceName(const String& stateProvinceName) { setStateProvinceName(stateProvinceName.c_str()); }
|
||||
// void setStateProvinceName(const char* stateProvinceName);
|
||||
// void setStateProvinceName(const String& stateProvinceName) { setStateProvinceName(stateProvinceName.c_str()); }
|
||||
|
||||
void setLocalityName(const char* localityName);
|
||||
void setLocalityName(const String& localityName) { setLocalityName(localityName.c_str()); }
|
||||
// void setLocalityName(const char* localityName);
|
||||
// void setLocalityName(const String& localityName) { setLocalityName(localityName.c_str()); }
|
||||
|
||||
void setOrganizationName(const char* organizationName);
|
||||
void setOrganizationName(const String& organizationName) { setOrganizationName(organizationName.c_str()); }
|
||||
// void setOrganizationName(const char* organizationName);
|
||||
// void setOrganizationName(const String& organizationName) { setOrganizationName(organizationName.c_str()); }
|
||||
|
||||
void setOrganizationalUnitName(const char* organizationalUnitName);
|
||||
void setOrganizationalUnitName(const String& organizationalUnitName) { setOrganizationalUnitName(organizationalUnitName.c_str()); }
|
||||
// void setOrganizationalUnitName(const char* organizationalUnitName);
|
||||
// void setOrganizationalUnitName(const String& organizationalUnitName) { setOrganizationalUnitName(organizationalUnitName.c_str()); }
|
||||
|
||||
void setCommonName(const char* commonName);
|
||||
void setCommonName(const String& commonName) { setCommonName(commonName.c_str()); }
|
||||
// void setCommonName(const char* commonName);
|
||||
// void setCommonName(const String& commonName) { setCommonName(commonName.c_str()); }
|
||||
|
||||
private:
|
||||
int buildCert(bool buildSignature);
|
||||
// private:
|
||||
// int buildCert(bool buildSignature);
|
||||
|
||||
int certInfoLength();
|
||||
void appendCertInfo(uint8_t publicKey[], uint8_t buffer[], int length);
|
||||
// int certInfoLength();
|
||||
// void appendCertInfo(uint8_t publicKey[], uint8_t buffer[], int length);
|
||||
|
||||
private:
|
||||
int _keySlot;
|
||||
int _dateAndSignatureSlot;
|
||||
// private:
|
||||
// int _keySlot;
|
||||
// int _dateAndSignatureSlot;
|
||||
|
||||
String _countryName;
|
||||
String _stateProvinceName;
|
||||
String _localityName;
|
||||
String _organizationName;
|
||||
String _organizationalUnitName;
|
||||
String _commonName;
|
||||
// String _countryName;
|
||||
// String _stateProvinceName;
|
||||
// String _localityName;
|
||||
// String _organizationName;
|
||||
// String _organizationalUnitName;
|
||||
// String _commonName;
|
||||
|
||||
uint8_t _temp[72];
|
||||
const uint8_t* _serialNumber;
|
||||
int _serialNumberLength;
|
||||
// uint8_t _temp[72];
|
||||
// const uint8_t* _serialNumber;
|
||||
// int _serialNumberLength;
|
||||
|
||||
uint8_t* _bytes;
|
||||
int _length;
|
||||
};
|
||||
// uint8_t* _bytes;
|
||||
// int _length;
|
||||
// };
|
||||
|
||||
extern ECCX08SelfSignedCertClass ECCX08SelfSignedCert;
|
||||
// extern ECCX08SelfSignedCertClass ECCX08SelfSignedCert;
|
||||
|
||||
#endif
|
||||
// #endif
|
||||
|
||||
@@ -1,74 +1,74 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#include "ECCX08Config.h"
|
||||
#include "PEMUtils.h"
|
||||
// #include "ECCX08Config.h"
|
||||
// #include "PEMUtils.h"
|
||||
|
||||
String PEMUtilsClass::base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix)
|
||||
{
|
||||
static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||
// String PEMUtilsClass::base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix)
|
||||
// {
|
||||
// static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
||||
|
||||
int b;
|
||||
String out;
|
||||
// int b;
|
||||
// String out;
|
||||
|
||||
int reserveLength = 4 * ((length + 2) / 3) + ((length / 3 * 4) / 76) + strlen(prefix) + strlen(suffix);
|
||||
out.reserve(reserveLength);
|
||||
// int reserveLength = 4 * ((length + 2) / 3) + ((length / 3 * 4) / 76) + strlen(prefix) + strlen(suffix);
|
||||
// out.reserve(reserveLength);
|
||||
|
||||
if (prefix) {
|
||||
out += prefix;
|
||||
}
|
||||
// if (prefix) {
|
||||
// out += prefix;
|
||||
// }
|
||||
|
||||
for (unsigned int i = 0; i < length; i += 3) {
|
||||
if (i > 0 && (i / 3 * 4) % 76 == 0) {
|
||||
out += '\n';
|
||||
}
|
||||
// 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] & 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 += "==";
|
||||
}
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
// if (suffix) {
|
||||
// out += suffix;
|
||||
// }
|
||||
|
||||
return out;
|
||||
}
|
||||
// return out;
|
||||
// }
|
||||
|
||||
#if !defined(ECCX08_DISABLE_PEM)
|
||||
PEMUtilsClass PEMUtils;
|
||||
#endif
|
||||
// #if !defined(ECCX08_DISABLE_PEM)
|
||||
// PEMUtilsClass PEMUtils;
|
||||
// #endif
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
/*
|
||||
This file is part of the ArduinoECCX08 library.
|
||||
Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
// /*
|
||||
// This file is part of the ArduinoECCX08 library.
|
||||
// Copyright (c) 2019 Arduino SA. All rights reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Lesser General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
// */
|
||||
|
||||
#ifndef _PEM_UTILS_H_
|
||||
#define _PEM_UTILS_H_
|
||||
// #ifndef _PEM_UTILS_H_
|
||||
// #define _PEM_UTILS_H_
|
||||
|
||||
#include <Arduino.h>
|
||||
// #include <Arduino.h>
|
||||
|
||||
class PEMUtilsClass {
|
||||
public:
|
||||
String base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix);
|
||||
};
|
||||
// class PEMUtilsClass {
|
||||
// public:
|
||||
// String base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix);
|
||||
// };
|
||||
|
||||
extern PEMUtilsClass PEMUtils;
|
||||
// extern PEMUtilsClass PEMUtils;
|
||||
|
||||
#endif
|
||||
// #endif
|
||||
|
||||
@@ -1,300 +1,300 @@
|
||||
/*
|
||||
SHA-1 in C
|
||||
By Steve Reid <steve@edmweb.com>
|
||||
100% Public Domain
|
||||
// /*
|
||||
// SHA-1 in C
|
||||
// By Steve Reid <steve@edmweb.com>
|
||||
// 100% Public Domain
|
||||
|
||||
Test Vectors (from FIPS PUB 180-1)
|
||||
"abc"
|
||||
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
|
||||
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
||||
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
|
||||
A million repetitions of "a"
|
||||
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
|
||||
*/
|
||||
// Test Vectors (from FIPS PUB 180-1)
|
||||
// "abc"
|
||||
// A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
|
||||
// "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
||||
// 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
|
||||
// A million repetitions of "a"
|
||||
// 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
|
||||
// */
|
||||
|
||||
/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
|
||||
/* #define SHA1HANDSOFF * Copies data before messing with it. */
|
||||
// /* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
|
||||
// /* #define SHA1HANDSOFF * Copies data before messing with it. */
|
||||
|
||||
#define SHA1HANDSOFF
|
||||
// #define SHA1HANDSOFF
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
// #include <stdio.h>
|
||||
// #include <string.h>
|
||||
|
||||
/* for uint32_t */
|
||||
#include <stdint.h>
|
||||
// /* for uint32_t */
|
||||
// #include <stdint.h>
|
||||
|
||||
#include "sha1.h"
|
||||
// #include "sha1.h"
|
||||
|
||||
|
||||
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
||||
// #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
|
||||
|
||||
/* blk0() and blk() perform the initial expand. */
|
||||
/* I got the idea of expanding during the round function from SSLeay */
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|
||||
|(rol(block->l[i],8)&0x00FF00FF))
|
||||
#elif BYTE_ORDER == BIG_ENDIAN
|
||||
#define blk0(i) block->l[i]
|
||||
#else
|
||||
#error "Endianness not defined!"
|
||||
#endif
|
||||
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
|
||||
^block->l[(i+2)&15]^block->l[i&15],1))
|
||||
// /* blk0() and blk() perform the initial expand. */
|
||||
// /* I got the idea of expanding during the round function from SSLeay */
|
||||
// #if BYTE_ORDER == LITTLE_ENDIAN
|
||||
// #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|
||||
// |(rol(block->l[i],8)&0x00FF00FF))
|
||||
// #elif BYTE_ORDER == BIG_ENDIAN
|
||||
// #define blk0(i) block->l[i]
|
||||
// #else
|
||||
// #error "Endianness not defined!"
|
||||
// #endif
|
||||
// #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
|
||||
// ^block->l[(i+2)&15]^block->l[i&15],1))
|
||||
|
||||
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
|
||||
#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
||||
#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
||||
#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
|
||||
#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
|
||||
#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
|
||||
// /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
|
||||
// #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
||||
// #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
|
||||
// #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
|
||||
// #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
|
||||
// #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
|
||||
|
||||
|
||||
/* Hash a single 512-bit block. This is the core of the algorithm. */
|
||||
// /* Hash a single 512-bit block. This is the core of the algorithm. */
|
||||
|
||||
void SHA1Transform(
|
||||
uint32_t state[5],
|
||||
const unsigned char buffer[64]
|
||||
)
|
||||
{
|
||||
uint32_t a, b, c, d, e;
|
||||
// void SHA1Transform(
|
||||
// uint32_t state[5],
|
||||
// const unsigned char buffer[64]
|
||||
// )
|
||||
// {
|
||||
// uint32_t a, b, c, d, e;
|
||||
|
||||
typedef union
|
||||
{
|
||||
unsigned char c[64];
|
||||
uint32_t l[16];
|
||||
} CHAR64LONG16;
|
||||
// typedef union
|
||||
// {
|
||||
// unsigned char c[64];
|
||||
// uint32_t l[16];
|
||||
// } CHAR64LONG16;
|
||||
|
||||
#ifdef SHA1HANDSOFF
|
||||
CHAR64LONG16 block[1]; /* use array to appear as a pointer */
|
||||
// #ifdef SHA1HANDSOFF
|
||||
// CHAR64LONG16 block[1]; /* use array to appear as a pointer */
|
||||
|
||||
memcpy(block, buffer, 64);
|
||||
#else
|
||||
/* The following had better never be used because it causes the
|
||||
* pointer-to-const buffer to be cast into a pointer to non-const.
|
||||
* And the result is written through. I threw a "const" in, hoping
|
||||
* this will cause a diagnostic.
|
||||
*/
|
||||
CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
|
||||
#endif
|
||||
/* Copy context->state[] to working vars */
|
||||
a = state[0];
|
||||
b = state[1];
|
||||
c = state[2];
|
||||
d = state[3];
|
||||
e = state[4];
|
||||
/* 4 rounds of 20 operations each. Loop unrolled. */
|
||||
R0(a, b, c, d, e, 0);
|
||||
R0(e, a, b, c, d, 1);
|
||||
R0(d, e, a, b, c, 2);
|
||||
R0(c, d, e, a, b, 3);
|
||||
R0(b, c, d, e, a, 4);
|
||||
R0(a, b, c, d, e, 5);
|
||||
R0(e, a, b, c, d, 6);
|
||||
R0(d, e, a, b, c, 7);
|
||||
R0(c, d, e, a, b, 8);
|
||||
R0(b, c, d, e, a, 9);
|
||||
R0(a, b, c, d, e, 10);
|
||||
R0(e, a, b, c, d, 11);
|
||||
R0(d, e, a, b, c, 12);
|
||||
R0(c, d, e, a, b, 13);
|
||||
R0(b, c, d, e, a, 14);
|
||||
R0(a, b, c, d, e, 15);
|
||||
R1(e, a, b, c, d, 16);
|
||||
R1(d, e, a, b, c, 17);
|
||||
R1(c, d, e, a, b, 18);
|
||||
R1(b, c, d, e, a, 19);
|
||||
R2(a, b, c, d, e, 20);
|
||||
R2(e, a, b, c, d, 21);
|
||||
R2(d, e, a, b, c, 22);
|
||||
R2(c, d, e, a, b, 23);
|
||||
R2(b, c, d, e, a, 24);
|
||||
R2(a, b, c, d, e, 25);
|
||||
R2(e, a, b, c, d, 26);
|
||||
R2(d, e, a, b, c, 27);
|
||||
R2(c, d, e, a, b, 28);
|
||||
R2(b, c, d, e, a, 29);
|
||||
R2(a, b, c, d, e, 30);
|
||||
R2(e, a, b, c, d, 31);
|
||||
R2(d, e, a, b, c, 32);
|
||||
R2(c, d, e, a, b, 33);
|
||||
R2(b, c, d, e, a, 34);
|
||||
R2(a, b, c, d, e, 35);
|
||||
R2(e, a, b, c, d, 36);
|
||||
R2(d, e, a, b, c, 37);
|
||||
R2(c, d, e, a, b, 38);
|
||||
R2(b, c, d, e, a, 39);
|
||||
R3(a, b, c, d, e, 40);
|
||||
R3(e, a, b, c, d, 41);
|
||||
R3(d, e, a, b, c, 42);
|
||||
R3(c, d, e, a, b, 43);
|
||||
R3(b, c, d, e, a, 44);
|
||||
R3(a, b, c, d, e, 45);
|
||||
R3(e, a, b, c, d, 46);
|
||||
R3(d, e, a, b, c, 47);
|
||||
R3(c, d, e, a, b, 48);
|
||||
R3(b, c, d, e, a, 49);
|
||||
R3(a, b, c, d, e, 50);
|
||||
R3(e, a, b, c, d, 51);
|
||||
R3(d, e, a, b, c, 52);
|
||||
R3(c, d, e, a, b, 53);
|
||||
R3(b, c, d, e, a, 54);
|
||||
R3(a, b, c, d, e, 55);
|
||||
R3(e, a, b, c, d, 56);
|
||||
R3(d, e, a, b, c, 57);
|
||||
R3(c, d, e, a, b, 58);
|
||||
R3(b, c, d, e, a, 59);
|
||||
R4(a, b, c, d, e, 60);
|
||||
R4(e, a, b, c, d, 61);
|
||||
R4(d, e, a, b, c, 62);
|
||||
R4(c, d, e, a, b, 63);
|
||||
R4(b, c, d, e, a, 64);
|
||||
R4(a, b, c, d, e, 65);
|
||||
R4(e, a, b, c, d, 66);
|
||||
R4(d, e, a, b, c, 67);
|
||||
R4(c, d, e, a, b, 68);
|
||||
R4(b, c, d, e, a, 69);
|
||||
R4(a, b, c, d, e, 70);
|
||||
R4(e, a, b, c, d, 71);
|
||||
R4(d, e, a, b, c, 72);
|
||||
R4(c, d, e, a, b, 73);
|
||||
R4(b, c, d, e, a, 74);
|
||||
R4(a, b, c, d, e, 75);
|
||||
R4(e, a, b, c, d, 76);
|
||||
R4(d, e, a, b, c, 77);
|
||||
R4(c, d, e, a, b, 78);
|
||||
R4(b, c, d, e, a, 79);
|
||||
/* Add the working vars back into context.state[] */
|
||||
state[0] += a;
|
||||
state[1] += b;
|
||||
state[2] += c;
|
||||
state[3] += d;
|
||||
state[4] += e;
|
||||
/* Wipe variables */
|
||||
a = b = c = d = e = 0;
|
||||
#ifdef SHA1HANDSOFF
|
||||
memset(block, '\0', sizeof(block));
|
||||
#endif
|
||||
}
|
||||
// memcpy(block, buffer, 64);
|
||||
// #else
|
||||
// /* The following had better never be used because it causes the
|
||||
// * pointer-to-const buffer to be cast into a pointer to non-const.
|
||||
// * And the result is written through. I threw a "const" in, hoping
|
||||
// * this will cause a diagnostic.
|
||||
// */
|
||||
// CHAR64LONG16 *block = (const CHAR64LONG16 *) buffer;
|
||||
// #endif
|
||||
// /* Copy context->state[] to working vars */
|
||||
// a = state[0];
|
||||
// b = state[1];
|
||||
// c = state[2];
|
||||
// d = state[3];
|
||||
// e = state[4];
|
||||
// /* 4 rounds of 20 operations each. Loop unrolled. */
|
||||
// R0(a, b, c, d, e, 0);
|
||||
// R0(e, a, b, c, d, 1);
|
||||
// R0(d, e, a, b, c, 2);
|
||||
// R0(c, d, e, a, b, 3);
|
||||
// R0(b, c, d, e, a, 4);
|
||||
// R0(a, b, c, d, e, 5);
|
||||
// R0(e, a, b, c, d, 6);
|
||||
// R0(d, e, a, b, c, 7);
|
||||
// R0(c, d, e, a, b, 8);
|
||||
// R0(b, c, d, e, a, 9);
|
||||
// R0(a, b, c, d, e, 10);
|
||||
// R0(e, a, b, c, d, 11);
|
||||
// R0(d, e, a, b, c, 12);
|
||||
// R0(c, d, e, a, b, 13);
|
||||
// R0(b, c, d, e, a, 14);
|
||||
// R0(a, b, c, d, e, 15);
|
||||
// R1(e, a, b, c, d, 16);
|
||||
// R1(d, e, a, b, c, 17);
|
||||
// R1(c, d, e, a, b, 18);
|
||||
// R1(b, c, d, e, a, 19);
|
||||
// R2(a, b, c, d, e, 20);
|
||||
// R2(e, a, b, c, d, 21);
|
||||
// R2(d, e, a, b, c, 22);
|
||||
// R2(c, d, e, a, b, 23);
|
||||
// R2(b, c, d, e, a, 24);
|
||||
// R2(a, b, c, d, e, 25);
|
||||
// R2(e, a, b, c, d, 26);
|
||||
// R2(d, e, a, b, c, 27);
|
||||
// R2(c, d, e, a, b, 28);
|
||||
// R2(b, c, d, e, a, 29);
|
||||
// R2(a, b, c, d, e, 30);
|
||||
// R2(e, a, b, c, d, 31);
|
||||
// R2(d, e, a, b, c, 32);
|
||||
// R2(c, d, e, a, b, 33);
|
||||
// R2(b, c, d, e, a, 34);
|
||||
// R2(a, b, c, d, e, 35);
|
||||
// R2(e, a, b, c, d, 36);
|
||||
// R2(d, e, a, b, c, 37);
|
||||
// R2(c, d, e, a, b, 38);
|
||||
// R2(b, c, d, e, a, 39);
|
||||
// R3(a, b, c, d, e, 40);
|
||||
// R3(e, a, b, c, d, 41);
|
||||
// R3(d, e, a, b, c, 42);
|
||||
// R3(c, d, e, a, b, 43);
|
||||
// R3(b, c, d, e, a, 44);
|
||||
// R3(a, b, c, d, e, 45);
|
||||
// R3(e, a, b, c, d, 46);
|
||||
// R3(d, e, a, b, c, 47);
|
||||
// R3(c, d, e, a, b, 48);
|
||||
// R3(b, c, d, e, a, 49);
|
||||
// R3(a, b, c, d, e, 50);
|
||||
// R3(e, a, b, c, d, 51);
|
||||
// R3(d, e, a, b, c, 52);
|
||||
// R3(c, d, e, a, b, 53);
|
||||
// R3(b, c, d, e, a, 54);
|
||||
// R3(a, b, c, d, e, 55);
|
||||
// R3(e, a, b, c, d, 56);
|
||||
// R3(d, e, a, b, c, 57);
|
||||
// R3(c, d, e, a, b, 58);
|
||||
// R3(b, c, d, e, a, 59);
|
||||
// R4(a, b, c, d, e, 60);
|
||||
// R4(e, a, b, c, d, 61);
|
||||
// R4(d, e, a, b, c, 62);
|
||||
// R4(c, d, e, a, b, 63);
|
||||
// R4(b, c, d, e, a, 64);
|
||||
// R4(a, b, c, d, e, 65);
|
||||
// R4(e, a, b, c, d, 66);
|
||||
// R4(d, e, a, b, c, 67);
|
||||
// R4(c, d, e, a, b, 68);
|
||||
// R4(b, c, d, e, a, 69);
|
||||
// R4(a, b, c, d, e, 70);
|
||||
// R4(e, a, b, c, d, 71);
|
||||
// R4(d, e, a, b, c, 72);
|
||||
// R4(c, d, e, a, b, 73);
|
||||
// R4(b, c, d, e, a, 74);
|
||||
// R4(a, b, c, d, e, 75);
|
||||
// R4(e, a, b, c, d, 76);
|
||||
// R4(d, e, a, b, c, 77);
|
||||
// R4(c, d, e, a, b, 78);
|
||||
// R4(b, c, d, e, a, 79);
|
||||
// /* Add the working vars back into context.state[] */
|
||||
// state[0] += a;
|
||||
// state[1] += b;
|
||||
// state[2] += c;
|
||||
// state[3] += d;
|
||||
// state[4] += e;
|
||||
// /* Wipe variables */
|
||||
// a = b = c = d = e = 0;
|
||||
// #ifdef SHA1HANDSOFF
|
||||
// memset(block, '\0', sizeof(block));
|
||||
// #endif
|
||||
// }
|
||||
|
||||
|
||||
/* SHA1Init - Initialize new context */
|
||||
// /* SHA1Init - Initialize new context */
|
||||
|
||||
void SHA1Init(
|
||||
SHA1_CTX * context
|
||||
)
|
||||
{
|
||||
/* SHA1 initialization constants */
|
||||
context->state[0] = 0x67452301;
|
||||
context->state[1] = 0xEFCDAB89;
|
||||
context->state[2] = 0x98BADCFE;
|
||||
context->state[3] = 0x10325476;
|
||||
context->state[4] = 0xC3D2E1F0;
|
||||
context->count[0] = context->count[1] = 0;
|
||||
}
|
||||
// void SHA1Init(
|
||||
// SHA1_CTX * context
|
||||
// )
|
||||
// {
|
||||
// /* SHA1 initialization constants */
|
||||
// context->state[0] = 0x67452301;
|
||||
// context->state[1] = 0xEFCDAB89;
|
||||
// context->state[2] = 0x98BADCFE;
|
||||
// context->state[3] = 0x10325476;
|
||||
// context->state[4] = 0xC3D2E1F0;
|
||||
// context->count[0] = context->count[1] = 0;
|
||||
// }
|
||||
|
||||
|
||||
/* Run your data through this. */
|
||||
// /* Run your data through this. */
|
||||
|
||||
void SHA1Update(
|
||||
SHA1_CTX * context,
|
||||
const unsigned char *data,
|
||||
uint32_t len
|
||||
)
|
||||
{
|
||||
uint32_t i;
|
||||
// void SHA1Update(
|
||||
// SHA1_CTX * context,
|
||||
// const unsigned char *data,
|
||||
// uint32_t len
|
||||
// )
|
||||
// {
|
||||
// uint32_t i;
|
||||
|
||||
uint32_t j;
|
||||
// uint32_t j;
|
||||
|
||||
j = context->count[0];
|
||||
if ((context->count[0] += len << 3) < j)
|
||||
context->count[1]++;
|
||||
context->count[1] += (len >> 29);
|
||||
j = (j >> 3) & 63;
|
||||
if ((j + len) > 63)
|
||||
{
|
||||
memcpy(&context->buffer[j], data, (i = 64 - j));
|
||||
SHA1Transform(context->state, context->buffer);
|
||||
for (; i + 63 < len; i += 64)
|
||||
{
|
||||
SHA1Transform(context->state, &data[i]);
|
||||
}
|
||||
j = 0;
|
||||
}
|
||||
else
|
||||
i = 0;
|
||||
memcpy(&context->buffer[j], &data[i], len - i);
|
||||
}
|
||||
// j = context->count[0];
|
||||
// if ((context->count[0] += len << 3) < j)
|
||||
// context->count[1]++;
|
||||
// context->count[1] += (len >> 29);
|
||||
// j = (j >> 3) & 63;
|
||||
// if ((j + len) > 63)
|
||||
// {
|
||||
// memcpy(&context->buffer[j], data, (i = 64 - j));
|
||||
// SHA1Transform(context->state, context->buffer);
|
||||
// for (; i + 63 < len; i += 64)
|
||||
// {
|
||||
// SHA1Transform(context->state, &data[i]);
|
||||
// }
|
||||
// j = 0;
|
||||
// }
|
||||
// else
|
||||
// i = 0;
|
||||
// memcpy(&context->buffer[j], &data[i], len - i);
|
||||
// }
|
||||
|
||||
|
||||
/* Add padding and return the message digest. */
|
||||
// /* Add padding and return the message digest. */
|
||||
|
||||
void SHA1Final(
|
||||
unsigned char digest[20],
|
||||
SHA1_CTX * context
|
||||
)
|
||||
{
|
||||
unsigned i;
|
||||
// void SHA1Final(
|
||||
// unsigned char digest[20],
|
||||
// SHA1_CTX * context
|
||||
// )
|
||||
// {
|
||||
// unsigned i;
|
||||
|
||||
unsigned char finalcount[8];
|
||||
// unsigned char finalcount[8];
|
||||
|
||||
unsigned char c;
|
||||
// unsigned char c;
|
||||
|
||||
#if 0 /* untested "improvement" by DHR */
|
||||
/* Convert context->count to a sequence of bytes
|
||||
* in finalcount. Second element first, but
|
||||
* big-endian order within element.
|
||||
* But we do it all backwards.
|
||||
*/
|
||||
unsigned char *fcp = &finalcount[8];
|
||||
// #if 0 /* untested "improvement" by DHR */
|
||||
// /* Convert context->count to a sequence of bytes
|
||||
// * in finalcount. Second element first, but
|
||||
// * big-endian order within element.
|
||||
// * But we do it all backwards.
|
||||
// */
|
||||
// unsigned char *fcp = &finalcount[8];
|
||||
|
||||
for (i = 0; i < 2; i++)
|
||||
{
|
||||
uint32_t t = context->count[i];
|
||||
// for (i = 0; i < 2; i++)
|
||||
// {
|
||||
// uint32_t t = context->count[i];
|
||||
|
||||
int j;
|
||||
// int j;
|
||||
|
||||
for (j = 0; j < 4; t >>= 8, j++)
|
||||
*--fcp = (unsigned char) t}
|
||||
#else
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
|
||||
}
|
||||
#endif
|
||||
c = 0200;
|
||||
SHA1Update(context, &c, 1);
|
||||
while ((context->count[0] & 504) != 448)
|
||||
{
|
||||
c = 0000;
|
||||
SHA1Update(context, &c, 1);
|
||||
}
|
||||
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
|
||||
for (i = 0; i < 20; i++)
|
||||
{
|
||||
digest[i] = (unsigned char)
|
||||
((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
|
||||
}
|
||||
/* Wipe variables */
|
||||
memset(context, '\0', sizeof(*context));
|
||||
memset(&finalcount, '\0', sizeof(finalcount));
|
||||
}
|
||||
// for (j = 0; j < 4; t >>= 8, j++)
|
||||
// *--fcp = (unsigned char) t}
|
||||
// #else
|
||||
// for (i = 0; i < 8; i++)
|
||||
// {
|
||||
// finalcount[i] = (unsigned char) ((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
|
||||
// }
|
||||
// #endif
|
||||
// c = 0200;
|
||||
// SHA1Update(context, &c, 1);
|
||||
// while ((context->count[0] & 504) != 448)
|
||||
// {
|
||||
// c = 0000;
|
||||
// SHA1Update(context, &c, 1);
|
||||
// }
|
||||
// SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
|
||||
// for (i = 0; i < 20; i++)
|
||||
// {
|
||||
// digest[i] = (unsigned char)
|
||||
// ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
|
||||
// }
|
||||
// /* Wipe variables */
|
||||
// memset(context, '\0', sizeof(*context));
|
||||
// memset(&finalcount, '\0', sizeof(finalcount));
|
||||
// }
|
||||
|
||||
void SHA1(
|
||||
char *hash_out,
|
||||
const char *str,
|
||||
int len)
|
||||
{
|
||||
SHA1_CTX ctx;
|
||||
#ifdef ARDUINO
|
||||
int ii;
|
||||
#else
|
||||
unsigned int ii;
|
||||
#endif
|
||||
// void SHA1(
|
||||
// char *hash_out,
|
||||
// const char *str,
|
||||
// int len)
|
||||
// {
|
||||
// SHA1_CTX ctx;
|
||||
// #ifdef ARDUINO
|
||||
// int ii;
|
||||
// #else
|
||||
// unsigned int ii;
|
||||
// #endif
|
||||
|
||||
SHA1Init(&ctx);
|
||||
for (ii=0; ii<len; ii+=1)
|
||||
SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
|
||||
SHA1Final((unsigned char *)hash_out, &ctx);
|
||||
hash_out[20] = '\0';
|
||||
}
|
||||
// SHA1Init(&ctx);
|
||||
// for (ii=0; ii<len; ii+=1)
|
||||
// SHA1Update(&ctx, (const unsigned char*)str + ii, 1);
|
||||
// SHA1Final((unsigned char *)hash_out, &ctx);
|
||||
// hash_out[20] = '\0';
|
||||
// }
|
||||
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
#ifndef SHA1_H
|
||||
#define SHA1_H
|
||||
// #ifndef SHA1_H
|
||||
// #define SHA1_H
|
||||
|
||||
/*
|
||||
SHA-1 in C
|
||||
By Steve Reid <steve@edmweb.com>
|
||||
100% Public Domain
|
||||
*/
|
||||
// /*
|
||||
// SHA-1 in C
|
||||
// By Steve Reid <steve@edmweb.com>
|
||||
// 100% Public Domain
|
||||
// */
|
||||
|
||||
#include "stdint.h"
|
||||
// #include "stdint.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t state[5];
|
||||
uint32_t count[2];
|
||||
unsigned char buffer[64];
|
||||
} SHA1_CTX;
|
||||
// typedef struct
|
||||
// {
|
||||
// uint32_t state[5];
|
||||
// uint32_t count[2];
|
||||
// unsigned char buffer[64];
|
||||
// } SHA1_CTX;
|
||||
|
||||
void SHA1Transform(
|
||||
uint32_t state[5],
|
||||
const unsigned char buffer[64]
|
||||
);
|
||||
// void SHA1Transform(
|
||||
// uint32_t state[5],
|
||||
// const unsigned char buffer[64]
|
||||
// );
|
||||
|
||||
void SHA1Init(
|
||||
SHA1_CTX * context
|
||||
);
|
||||
// void SHA1Init(
|
||||
// SHA1_CTX * context
|
||||
// );
|
||||
|
||||
void SHA1Update(
|
||||
SHA1_CTX * context,
|
||||
const unsigned char *data,
|
||||
uint32_t len
|
||||
);
|
||||
// void SHA1Update(
|
||||
// SHA1_CTX * context,
|
||||
// const unsigned char *data,
|
||||
// uint32_t len
|
||||
// );
|
||||
|
||||
void SHA1Final(
|
||||
unsigned char digest[20],
|
||||
SHA1_CTX * context
|
||||
);
|
||||
// void SHA1Final(
|
||||
// unsigned char digest[20],
|
||||
// SHA1_CTX * context
|
||||
// );
|
||||
|
||||
#ifdef ARDUINO
|
||||
#define SHA1 SHA1_ // to make the function "internal"
|
||||
#endif
|
||||
// #ifdef ARDUINO
|
||||
// #define SHA1 SHA1_ // to make the function "internal"
|
||||
// #endif
|
||||
|
||||
void SHA1(
|
||||
char *hash_out,
|
||||
const char *str,
|
||||
int len);
|
||||
// void SHA1(
|
||||
// char *hash_out,
|
||||
// const char *str,
|
||||
// int len);
|
||||
|
||||
#endif /* SHA1_H */
|
||||
// #endif /* SHA1_H */
|
||||
|
||||
@@ -24,8 +24,6 @@ const char *compileTime = __TIME__;
|
||||
|
||||
void WebServer::begin()
|
||||
{
|
||||
Serial.println("Starting WebServer");
|
||||
|
||||
app.notFound(std::bind(&WebServer::notFound, std::placeholders::_1, std::placeholders::_2));
|
||||
app.authorize(std::bind(&WebServer::checkAuthentication, std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
@@ -70,10 +68,8 @@ void WebServer::listen(nsapi_connection_status_t status)
|
||||
{
|
||||
if (_serverSocket == nullptr)
|
||||
{
|
||||
Serial.println("Connect WebServer");
|
||||
_serverSocket = new TCPSocket;
|
||||
NetworkInterface *_net = NetworkInterface::get_default_instance();
|
||||
_serverSocket->open(_net);
|
||||
_serverSocket->open(NetworkInterface::get_default_instance());
|
||||
_serverSocket->bind(80);
|
||||
_serverSocket->listen(1);
|
||||
}
|
||||
@@ -83,11 +79,11 @@ void WebServer::listen(nsapi_connection_status_t status)
|
||||
{
|
||||
app.process(_clientSocket);
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("webserver clientsocket error: ");
|
||||
Serial.println(error);
|
||||
}
|
||||
// else
|
||||
// {
|
||||
// Serial.print("webserver clientsocket error: ");
|
||||
// Serial.println(error);
|
||||
// }
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -102,7 +98,7 @@ void WebServer::listen(nsapi_connection_status_t status)
|
||||
//--------------- not found / error -------------------------------
|
||||
void WebServer::notFound(HTTPWebServer::Request &req, HTTPWebServer::Response &res)
|
||||
{
|
||||
Serial.println("Send notFound");
|
||||
//Serial.println("Send notFound");
|
||||
res.status(404);
|
||||
res.header("Content-Type", "text/plain");
|
||||
res.header("Connection", "close");
|
||||
@@ -113,51 +109,24 @@ void WebServer::notFound(HTTPWebServer::Request &req, HTTPWebServer::Response &r
|
||||
bool WebServer::checkAuthentication(HTTPWebServer::Request &req, HTTPWebServer::Response &res)
|
||||
{
|
||||
//Serial.println("CHECK Authentication");
|
||||
|
||||
std::string auth = req.getHeader("Authorization");
|
||||
//Serial.print("|");
|
||||
//Serial.print(auth.c_str());
|
||||
//Serial.println("|");
|
||||
if(auth.size() != 0)
|
||||
{
|
||||
//Serial.println(auth.c_str());
|
||||
|
||||
std::string base64 = auth.substr(auth.find_first_of(' ') + 1);
|
||||
//Serial.print("|");
|
||||
//Serial.print(base64.c_str());
|
||||
//Serial.println("|");
|
||||
|
||||
size_t olen = 0;
|
||||
unsigned char userpass[64];
|
||||
//int ret = mbedtls_base64_decode(userpass, sizeof(userpass), &olen, (const unsigned char *)base64, strlen(base64));
|
||||
int ret = mbedtls_base64_decode(userpass, sizeof(userpass), &olen, (const unsigned char *)base64.c_str(), base64.size());
|
||||
if(ret == 0 )
|
||||
{
|
||||
userpass[olen + 1] = '\0';
|
||||
|
||||
// Serial.print("userpass: ");
|
||||
// for(uint8_t i=0; i< olen ; i++) {
|
||||
// Serial.write(userpass[i]);
|
||||
// }
|
||||
// Serial.println();
|
||||
|
||||
userpass[olen + 1] = '\0';
|
||||
char *pass = strchr((const char *)userpass, ':');
|
||||
*pass = '\0';
|
||||
pass++;
|
||||
|
||||
// Serial.print("user: ");
|
||||
// Serial.println((char *)userpass);
|
||||
|
||||
// Serial.print("pass: ");
|
||||
// Serial.println(pass);
|
||||
|
||||
ConfigStore::Config_t *config = ConfigStore::getConfig();
|
||||
if(strncmp((char *)userpass, config->username.c_str(), config->username.size()) == 0)
|
||||
{
|
||||
//Serial.println("username correct");
|
||||
if(strncmp(pass, config->password.c_str(), config->password.size()) == 0)
|
||||
{
|
||||
//Serial.println("password correct");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -263,10 +232,7 @@ void WebServer::InfoGet(HTTPWebServer::Request &req, HTTPWebServer::Response &re
|
||||
{
|
||||
AddJsonHeader(res);
|
||||
|
||||
//ConfigStore::Config_t *config = ConfigStore::getConfig();
|
||||
JsonDocument infoData;
|
||||
|
||||
//infoData["serialnumber"] = config->serialnumber;
|
||||
infoData["serialnumber"] = ConfigStore::getConfig()->serialnumber;
|
||||
|
||||
NetworkInterface *net = NetworkInterface::get_default_instance();
|
||||
@@ -289,29 +255,28 @@ void WebServer::InfoGet(HTTPWebServer::Request &req, HTTPWebServer::Response &re
|
||||
sprintf(timeStr, "%lu:days %lu:hours %lu:minutes %lu:seconds", Days, Hours, Minutes, seconds);
|
||||
infoData["uptime"] = timeStr;
|
||||
|
||||
SocketAddress address;
|
||||
net->get_ip_address(&address);
|
||||
infoData["ipaddress"] = address.get_ip_address();
|
||||
SocketAddress sock;
|
||||
net->get_ip_address(&sock);
|
||||
infoData["ipaddress"] = sock.get_ip_address();
|
||||
|
||||
SocketAddress mask;
|
||||
net->get_netmask(&mask);
|
||||
infoData["subnet"] = mask.get_ip_address();
|
||||
//SocketAddress mask;
|
||||
net->get_netmask(&sock);
|
||||
infoData["subnet"] = sock.get_ip_address();
|
||||
|
||||
SocketAddress gate;
|
||||
net->get_gateway(&gate);
|
||||
infoData["gateway"] = gate.get_ip_address();
|
||||
//SocketAddress gate;
|
||||
net->get_gateway(&sock);
|
||||
infoData["gateway"] = sock.get_ip_address();
|
||||
|
||||
char if_name[5] {};
|
||||
net->get_interface_name(if_name);
|
||||
|
||||
SocketAddress dns1;
|
||||
net->get_dns_server(0, &dns1, if_name);
|
||||
infoData["dns1"] = dns1.get_ip_address();
|
||||
|
||||
SocketAddress dns2;
|
||||
net->get_dns_server(1, &dns2, if_name);
|
||||
infoData["dns2"] = dns2.get_ip_address();
|
||||
//SocketAddress dns1;
|
||||
net->get_dns_server(0, &sock, if_name);
|
||||
infoData["dns1"] = sock.get_ip_address();
|
||||
|
||||
//SocketAddress dns2;
|
||||
net->get_dns_server(1, &sock, if_name);
|
||||
infoData["dns2"] = sock.get_ip_address();
|
||||
|
||||
reset_reason_t resetReason = STM32H747::getResetReason();
|
||||
infoData["reset"] = getResetString(resetReason);
|
||||
@@ -348,14 +313,14 @@ void WebServer::InfoGet(HTTPWebServer::Request &req, HTTPWebServer::Response &re
|
||||
res.writeContent(json);
|
||||
|
||||
}
|
||||
|
||||
void WebServer::SerialNumberPost(HTTPWebServer::Request &req, HTTPWebServer::Response &res)
|
||||
{
|
||||
//ConfigStore::Config_t *config = ConfigStore::getConfig();
|
||||
//config->serialnumber = req.getFormData("serialnumber");
|
||||
ConfigStore::getConfig()->serialnumber = req.getFormData("serialnumber");
|
||||
ConfigStore::saveConfig();
|
||||
getStaticFile((char *)"/index.html", res);
|
||||
}
|
||||
|
||||
//----------- ACS ----------------------
|
||||
void WebServer::ACSGet(HTTPWebServer::Request &req, HTTPWebServer::Response &res)
|
||||
{
|
||||
@@ -399,6 +364,7 @@ void WebServer::ACSGet(HTTPWebServer::Request &req, HTTPWebServer::Response &res
|
||||
serializeJson(acsData, json);
|
||||
res.writeContent(json);
|
||||
}
|
||||
|
||||
void WebServer::ACSPost(HTTPWebServer::Request &req, HTTPWebServer::Response &res)
|
||||
{
|
||||
ConfigStore::Config_t *config = ConfigStore::getConfig();
|
||||
|
||||
362
src/main.cpp
362
src/main.cpp
@@ -13,7 +13,6 @@
|
||||
#include "WebServer.h"
|
||||
//#include "ECCX08.h"
|
||||
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
//OPTA chips
|
||||
@@ -26,13 +25,13 @@ using namespace std::chrono_literals;
|
||||
enum heartBeatStates {HB_OK, HB_ERROR, HB_STARTUP, HB_APERIOLOST};
|
||||
volatile heartBeatStates heartBeatState;
|
||||
|
||||
volatile nsapi_connection_status currentNetworkState;
|
||||
volatile nsapi_connection_status currentNetworkState = NSAPI_STATUS_DISCONNECTED;
|
||||
|
||||
struct MF700::reader_t reader;
|
||||
rtos::Mutex readerMutex;
|
||||
|
||||
//mbed::BufferedSerial _Serial(P0_20, P0_9, 115200);
|
||||
REDIRECT_STDOUT_TO(Serial);
|
||||
//REDIRECT_STDOUT_TO(Serial);
|
||||
|
||||
mbed::DigitalOut LedRed(LED_RED);
|
||||
mbed::DigitalOut LedGreen(LED_GREEN);
|
||||
@@ -41,7 +40,7 @@ mbed::DigitalIn Button(PE_4);
|
||||
|
||||
rtos::Thread heartBeatThread(osPriorityNormal, 128, NULL, "HeartBeat");
|
||||
rtos::Thread ioThread(osPriorityNormal, 256, NULL, "I/O");
|
||||
rtos::Thread mf700Thread(osPriorityNormal, 256, NULL, "MF700");
|
||||
rtos::Thread mf700Thread(osPriorityNormal, 1024, NULL, "MF700");
|
||||
rtos::Thread consoleThread(osPriorityNormal, 5 * 1024, NULL, "Console");
|
||||
rtos::Thread webserverThread(osPriorityNormal, 15 * 1024, NULL, "Webserver");
|
||||
rtos::Thread aperioThread(osPriorityNormal, 30 * 1024, NULL, "Aperio");
|
||||
@@ -49,7 +48,8 @@ rtos::Thread aperioThread(osPriorityNormal, 30 * 1024, NULL, "Aperio");
|
||||
volatile bool aperioConnected = false;
|
||||
//--------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
void heartBeatLoop() {
|
||||
void heartBeatLoop()
|
||||
{
|
||||
int pingpong = 0;
|
||||
while(true){
|
||||
LedRed.write(0);
|
||||
@@ -93,7 +93,8 @@ void heartBeatLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
void inputOutputTask(){
|
||||
void inputOutputTask()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
Relays::checkForEvent();
|
||||
@@ -103,7 +104,9 @@ void inputOutputTask(){
|
||||
}
|
||||
}
|
||||
|
||||
void mf700ReaderTask(){
|
||||
void mf700ReaderTask()
|
||||
{
|
||||
//MF700::begin(RS485_SERIAL_PORT);
|
||||
while(true)
|
||||
{
|
||||
if(readerMutex.trylock())
|
||||
@@ -113,7 +116,6 @@ void mf700ReaderTask(){
|
||||
reader.cardReady = true;
|
||||
}else{
|
||||
MF700::controlCommand(MF700::MF700controlset::EVERYLEDON, std::chrono::milliseconds(5000));
|
||||
//rtos::ThisThread::sleep_for(250ms);
|
||||
MF700::controlCommand(MF700::MF700controlset::BUZZER3TIME, std::chrono::milliseconds(5000));
|
||||
}
|
||||
}
|
||||
@@ -124,8 +126,129 @@ void mf700ReaderTask(){
|
||||
}
|
||||
}
|
||||
|
||||
void printThreadState(rtos::Thread::State state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case rtos::Thread::State::Ready:
|
||||
Serial.print("ready");
|
||||
break;
|
||||
case rtos::Thread::State::Running:
|
||||
Serial.print("Running");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingDelay:
|
||||
Serial.print("WaitingDelay");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingJoin:
|
||||
Serial.print("WaitingJoin");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingThreadFlag:
|
||||
Serial.print("WaitingThreadFlag");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingEventFlag:
|
||||
Serial.print("WaitingEventFlag");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingMutex:
|
||||
Serial.print("WaitingMutex");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingSemaphore:
|
||||
Serial.print("WaitingSemaphore");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingMemoryPool:
|
||||
Serial.print("WaitingMemoryPool");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingMessageGet:
|
||||
Serial.print("WaitingMessageGet");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingMessagePut:
|
||||
Serial.print("WaitingMessagePut");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingInterval:
|
||||
Serial.print("WaitingInterval");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingOr:
|
||||
Serial.print("WaitingOr");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingAnd:
|
||||
Serial.print("WaitingAnd");
|
||||
break;
|
||||
case rtos::Thread::State::WaitingMailbox:
|
||||
Serial.print("WaitingMailbox");
|
||||
break;
|
||||
default:
|
||||
Serial.print("????");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void printStack()
|
||||
{
|
||||
Serial.print(heartBeatThread.get_name());
|
||||
Serial.print(" : ");
|
||||
printThreadState(heartBeatThread.get_state());
|
||||
Serial.print(" : ");
|
||||
Serial.print(heartBeatThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(heartBeatThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(heartBeatThread.free_stack());
|
||||
|
||||
Serial.print(ioThread.get_name());
|
||||
Serial.print(" : ");
|
||||
printThreadState(ioThread.get_state());
|
||||
Serial.print(" : ");
|
||||
Serial.print(ioThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(ioThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(ioThread.free_stack());
|
||||
|
||||
Serial.print(mf700Thread.get_name());
|
||||
Serial.print(" : ");
|
||||
printThreadState(mf700Thread.get_state());
|
||||
Serial.print(" : ");
|
||||
Serial.print(mf700Thread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(mf700Thread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(mf700Thread.free_stack());
|
||||
|
||||
Serial.print(consoleThread.get_name());
|
||||
Serial.print(" : ");
|
||||
printThreadState(consoleThread.get_state());
|
||||
Serial.print(" : ");
|
||||
Serial.print(consoleThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(consoleThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(consoleThread.free_stack());
|
||||
|
||||
Serial.print(webserverThread.get_name());
|
||||
Serial.print(" : ");
|
||||
printThreadState(webserverThread.get_state());
|
||||
Serial.print(" : ");
|
||||
Serial.print(webserverThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(webserverThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(webserverThread.free_stack());
|
||||
|
||||
Serial.print(aperioThread.get_name());
|
||||
Serial.print(" : ");
|
||||
printThreadState(aperioThread.get_state());
|
||||
Serial.print(" : ");
|
||||
Serial.print(aperioThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(aperioThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(aperioThread.free_stack());
|
||||
|
||||
}
|
||||
|
||||
void consoleTask()
|
||||
{
|
||||
//Console::begin(Serial, printStack);
|
||||
while(true)
|
||||
{
|
||||
Console::executeIfInput();
|
||||
@@ -135,6 +258,7 @@ void consoleTask()
|
||||
|
||||
void webServerTask()
|
||||
{
|
||||
//WebServer::begin();
|
||||
while(true)
|
||||
{
|
||||
WebServer::listen(currentNetworkState);
|
||||
@@ -144,6 +268,7 @@ void webServerTask()
|
||||
|
||||
void aperioTask()
|
||||
{
|
||||
//Aperio::begin();
|
||||
Inputs::inputEvent_t *currentEvent = nullptr;
|
||||
while(true)
|
||||
{
|
||||
@@ -177,55 +302,39 @@ void aperioTask()
|
||||
}
|
||||
|
||||
|
||||
void printStack()
|
||||
{
|
||||
Serial.print(heartBeatThread.get_name());
|
||||
Serial.print(" : ");
|
||||
Serial.print(heartBeatThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(heartBeatThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(heartBeatThread.free_stack());
|
||||
void networkStatusChange(nsapi_event_t status, intptr_t param)
|
||||
{
|
||||
Serial.println("Network state event");
|
||||
|
||||
Serial.print(ioThread.get_name());
|
||||
Serial.print(" : ");
|
||||
Serial.print(ioThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(ioThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(ioThread.free_stack());
|
||||
|
||||
Serial.print(mf700Thread.get_name());
|
||||
Serial.print(" : ");
|
||||
Serial.print(mf700Thread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(mf700Thread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(mf700Thread.free_stack());
|
||||
|
||||
Serial.print(consoleThread.get_name());
|
||||
Serial.print(" : ");
|
||||
Serial.print(consoleThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(consoleThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(consoleThread.free_stack());
|
||||
|
||||
Serial.print(webserverThread.get_name());
|
||||
Serial.print(" : ");
|
||||
Serial.print(webserverThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(webserverThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(webserverThread.free_stack());
|
||||
|
||||
Serial.print(aperioThread.get_name());
|
||||
Serial.print(" : ");
|
||||
Serial.print(aperioThread.stack_size());
|
||||
Serial.print("/");
|
||||
Serial.print(aperioThread.max_stack());
|
||||
Serial.print("/");
|
||||
Serial.println(aperioThread.free_stack());
|
||||
if (status == NSAPI_EVENT_CONNECTION_STATUS_CHANGE) {
|
||||
currentNetworkState = static_cast<nsapi_connection_status>(param);
|
||||
switch(param) {
|
||||
case NSAPI_STATUS_GLOBAL_UP:
|
||||
Serial.println("global up event");
|
||||
heartBeatState = heartBeatStates::HB_OK;
|
||||
break;
|
||||
case NSAPI_STATUS_LOCAL_UP:
|
||||
Serial.println("local up event");
|
||||
heartBeatState = heartBeatStates::HB_OK;
|
||||
break;
|
||||
case NSAPI_STATUS_CONNECTING:
|
||||
Serial.println("connecting event");
|
||||
break;
|
||||
case NSAPI_STATUS_DISCONNECTED:
|
||||
Serial.println("disconnected event");
|
||||
heartBeatState = heartBeatStates::HB_ERROR;
|
||||
NetworkInterface::get_default_instance()->connect();
|
||||
break;
|
||||
case NSAPI_STATUS_ERROR_UNSUPPORTED:
|
||||
Serial.println("error event");
|
||||
heartBeatState = heartBeatStates::HB_ERROR;
|
||||
break;
|
||||
default:
|
||||
Serial.println("default event");
|
||||
heartBeatState = heartBeatStates::HB_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------------------------------------------------------------
|
||||
@@ -235,7 +344,6 @@ void setup()
|
||||
heartBeatState = HB_STARTUP;
|
||||
heartBeatThread.start(heartBeatLoop);
|
||||
|
||||
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {
|
||||
@@ -243,27 +351,12 @@ void setup()
|
||||
}
|
||||
//mbed::mbed_override_console(Serial);
|
||||
|
||||
// if (!ECCX08.begin()) {
|
||||
// Serial.println("Failed to communicate with ECC508/ECC608!");
|
||||
// }
|
||||
if (!ECCX08.begin(0x60)) {
|
||||
Serial.println("Failed to communicate with ECC508/ECC608!");
|
||||
}
|
||||
|
||||
long counter = -1;
|
||||
// if (!ECCX08.incrementCounter(1, counter)) {
|
||||
// Serial.println("Failed to increment counter");
|
||||
// }
|
||||
if (!ECCX08.incrementCounter(1, counter)) {
|
||||
Serial.println("Failed to increment counter");
|
||||
}
|
||||
|
||||
MF700::begin(RS485_SERIAL_PORT);
|
||||
ACSFileSystem::begin();
|
||||
ConfigStore::init();
|
||||
Console::begin(Serial, printStack);
|
||||
WebServer::begin();
|
||||
Aperio::begin();
|
||||
// MF700::begin(RS485_SERIAL_PORT);
|
||||
// ACSFileSystem::begin();
|
||||
// ConfigStore::init();
|
||||
// Console::begin(Serial, printStack);
|
||||
// WebServer::begin();
|
||||
// Aperio::begin();
|
||||
|
||||
NetworkInterface *_net = NetworkInterface::get_default_instance();
|
||||
if (!_net) {
|
||||
@@ -271,6 +364,7 @@ void setup()
|
||||
}
|
||||
_net->set_blocking(false);
|
||||
_net->set_dhcp(true);
|
||||
_net->attach(&networkStatusChange);
|
||||
|
||||
Serial.println("Connecting to the network...");
|
||||
nsapi_size_or_error_t result = _net->connect();
|
||||
@@ -279,6 +373,22 @@ void setup()
|
||||
Serial.println(result);
|
||||
}
|
||||
|
||||
|
||||
// if (!ECCX08.begin(0x60)) {
|
||||
// Serial.println("Failed to communicate with ECC508/ECC608!");
|
||||
// }
|
||||
// long counter = -1;
|
||||
// if (!ECCX08.incrementCounter(1, counter)) {
|
||||
// Serial.println("Failed to increment counter");
|
||||
// }
|
||||
|
||||
MF700::begin(RS485_SERIAL_PORT);
|
||||
ACSFileSystem::begin();
|
||||
ConfigStore::init();
|
||||
Console::begin(Serial, printStack);
|
||||
WebServer::begin();
|
||||
Aperio::begin();
|
||||
|
||||
ioThread.start(inputOutputTask);
|
||||
mf700Thread.start(mf700ReaderTask);
|
||||
webserverThread.start(webServerTask);
|
||||
@@ -289,64 +399,62 @@ void setup()
|
||||
}
|
||||
|
||||
|
||||
void showNetworkStats(NetworkInterface *_net)
|
||||
{
|
||||
SocketAddress a;
|
||||
_net->get_ip_address(&a);
|
||||
Serial.print("IP: ");
|
||||
Serial.println(a.get_ip_address());
|
||||
// void showNetworkStats(NetworkInterface *_net)
|
||||
// {
|
||||
// SocketAddress a;
|
||||
// _net->get_ip_address(&a);
|
||||
// Serial.print("IP: ");
|
||||
// Serial.println(a.get_ip_address());
|
||||
|
||||
_net->get_netmask(&a);
|
||||
Serial.print("Netmask: ");
|
||||
Serial.println(a.get_ip_address());
|
||||
// _net->get_netmask(&a);
|
||||
// Serial.print("Netmask: ");
|
||||
// Serial.println(a.get_ip_address());
|
||||
|
||||
_net->get_gateway(&a);
|
||||
Serial.print("Gateway: ");
|
||||
Serial.println(a.get_ip_address());
|
||||
// _net->get_gateway(&a);
|
||||
// Serial.print("Gateway: ");
|
||||
// Serial.println(a.get_ip_address());
|
||||
|
||||
char mac[6];
|
||||
mbed_mac_address(mac);
|
||||
//Serial.println(mac[0], HEX);
|
||||
//Serial.println(mac[1], HEX);
|
||||
//Serial.println(mac[2], HEX);
|
||||
//Serial.println(mac[3], HEX);
|
||||
//Serial.println(mac[4], HEX);
|
||||
//Serial.println(mac[5], HEX);
|
||||
// char mac[6];
|
||||
// mbed_mac_address(mac);
|
||||
// //Serial.println(mac[0], HEX);
|
||||
// //Serial.println(mac[1], HEX);
|
||||
// //Serial.println(mac[2], HEX);
|
||||
// //Serial.println(mac[3], HEX);
|
||||
// //Serial.println(mac[4], HEX);
|
||||
// //Serial.println(mac[5], HEX);
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
//NetworkInterface *net = NetworkInterface::get_default_instance();
|
||||
//nsapi_connection_status_t status = net->get_connection_status();
|
||||
// nsapi_connection_status_t status = NetworkInterface::get_default_instance()->get_connection_status();
|
||||
// if(status != currentNetworkState)
|
||||
// {
|
||||
// currentNetworkState = status;
|
||||
// if(status == NSAPI_STATUS_LOCAL_UP || status == NSAPI_STATUS_GLOBAL_UP)
|
||||
// {
|
||||
// //Serial.println("Network UP");
|
||||
// //heartBeatState = heartBeatStates::HB_OK;
|
||||
// }
|
||||
// else if(status == NSAPI_STATUS_CONNECTING)
|
||||
// {
|
||||
// //Serial.println("Connecting");
|
||||
// }
|
||||
// else if(status == NSAPI_STATUS_DISCONNECTED)
|
||||
// {
|
||||
// //Serial.println("Network DOWN");
|
||||
// //heartBeatState = heartBeatStates::HB_ERROR;
|
||||
// //NetworkInterface::get_default_instance()->connect();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// //Serial.println("Unknown Network state");
|
||||
// //heartBeatState = heartBeatStates::HB_ERROR;
|
||||
// }
|
||||
// }
|
||||
|
||||
nsapi_connection_status_t status = NetworkInterface::get_default_instance()->get_connection_status();
|
||||
if(status != currentNetworkState)
|
||||
{
|
||||
currentNetworkState = status;
|
||||
if(status == NSAPI_STATUS_LOCAL_UP || status == NSAPI_STATUS_GLOBAL_UP)
|
||||
{
|
||||
Serial.println("Network UP");
|
||||
heartBeatState = heartBeatStates::HB_OK;
|
||||
}
|
||||
else if(status == NSAPI_STATUS_CONNECTING)
|
||||
{
|
||||
Serial.println("Connecting");
|
||||
}
|
||||
else if(status == NSAPI_STATUS_DISCONNECTED)
|
||||
{
|
||||
Serial.println("Network DOWN");
|
||||
heartBeatState = heartBeatStates::HB_ERROR;
|
||||
//net->connect();
|
||||
NetworkInterface::get_default_instance()->connect();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Unknown Network state");
|
||||
heartBeatState = heartBeatStates::HB_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NTP::checkUpdateTime(currentNetworkState);
|
||||
rtos::ThisThread::yield();
|
||||
|
||||
Reference in New Issue
Block a user