|
| 1 | +/* |
| 2 | + SElementCertificate.cpp |
| 3 | + Copyright (c) 2023 Arduino SA. All right reserved. |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +*/ |
| 19 | + |
| 20 | +/****************************************************************************** |
| 21 | + * INCLUDE |
| 22 | + ******************************************************************************/ |
| 23 | + |
| 24 | +#include <utility/SElementCertificate.h> |
| 25 | + |
| 26 | +int SElementCertificate::build(SecureElement & se, ECP256Certificate & cert, const int keySlot, bool newPrivateKey, bool selfSign) |
| 27 | +{ |
| 28 | + byte publicKey[ECP256_CERT_PUBLIC_KEY_LENGTH]; |
| 29 | + byte signature[ECP256_CERT_SIGNATURE_LENGTH]; |
| 30 | + |
| 31 | + if (!se.generatePublicKey(keySlot, publicKey)) { |
| 32 | + return 0; |
| 33 | + } |
| 34 | + |
| 35 | + /* Store public key in Certificate */ |
| 36 | + if (!cert.setPublicKey(publicKey, ECP256_CERT_PUBLIC_KEY_LENGTH)) { |
| 37 | + return 0; |
| 38 | + } |
| 39 | + |
| 40 | + /* Build Certificate */ |
| 41 | + if (!cert.buildCert()) { |
| 42 | + return 0; |
| 43 | + } |
| 44 | + |
| 45 | + if (selfSign) { |
| 46 | + byte sha256buf[SE_SHA256_BUFFER_LENGTH]; |
| 47 | + se.SHA256(cert.bytes(), cert.length(), sha256buf); |
| 48 | + |
| 49 | + if (!se.ecSign(keySlot, sha256buf, signature)) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + /* self sign Certificate */ |
| 54 | + return cert.signCert(signature); |
| 55 | + } |
| 56 | + |
| 57 | + /* sign Certificate */ |
| 58 | + return cert.signCert(); |
| 59 | +} |
0 commit comments