|
| 1 | +/* certgen_ca_example.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2021 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | +#include <wolfssl/options.h> |
| 24 | +#include <wolfssl/wolfcrypt/settings.h> |
| 25 | +#include <wolfssl/wolfcrypt/rsa.h> |
| 26 | +#include <wolfssl/wolfcrypt/asn_public.h> |
| 27 | +#include <wolfssl/wolfcrypt/asn.h> |
| 28 | +#include <wolfssl/wolfcrypt/error-crypt.h> |
| 29 | +#include <wolfssl/wolfcrypt/wc_port.h> |
| 30 | + |
| 31 | +#ifdef WOLFSSL_CAAM |
| 32 | + #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h> |
| 33 | +#endif |
| 34 | + |
| 35 | +#if defined(WOLFSSL_CERT_REQ) && defined(WOLFSSL_CERT_GEN) && \ |
| 36 | + defined(WOLFSSL_KEY_GEN) && defined(HAVE_ECC) |
| 37 | + |
| 38 | +#define HEAP_HINT NULL |
| 39 | +#define LARGE_TEMP_SZ 4096 |
| 40 | +static int devId = WOLFSSL_CAAM_DEVID; |
| 41 | + |
| 42 | +static int do_cagen(int argc, char** argv) |
| 43 | +{ |
| 44 | + int ret = 0; |
| 45 | + |
| 46 | + Cert newCert; |
| 47 | + |
| 48 | + FILE* file; |
| 49 | + int derBufSz; |
| 50 | + int caKeySz = 4096; |
| 51 | + |
| 52 | + byte* derBuf = NULL; |
| 53 | + byte* pemBuf = NULL; |
| 54 | + |
| 55 | + /* for MakeCert and SignCert */ |
| 56 | + WC_RNG rng; |
| 57 | + RsaKey newKey; |
| 58 | + int initRng = 0, initNewKey = 0; |
| 59 | + char newCertOutput[] = "./ca-rsa-cert.der"; |
| 60 | + char newKeyOutput[] = "./ca-rsa-key.der"; |
| 61 | + |
| 62 | +#ifdef WOLFSSL_DER_TO_PEM |
| 63 | + char pemOutput[] = "./ca-rsa-cert.pem"; |
| 64 | + char pemKeyOutput[] = "./ca-rsa-key.pem"; |
| 65 | + int pemBufSz; |
| 66 | +#endif |
| 67 | + |
| 68 | + ret = wolfCrypt_Init(); |
| 69 | + if (ret != 0) goto exit; |
| 70 | + |
| 71 | + ret = wc_InitRng(&rng); |
| 72 | + if (ret != 0) goto exit; |
| 73 | + initRng = 1; |
| 74 | + |
| 75 | + printf("Creating the CA RSA private key of size %d\n", caKeySz); |
| 76 | + ret = wc_InitRsaKey_ex(&newKey, HEAP_HINT, devId); |
| 77 | + if (ret != 0) goto exit; |
| 78 | + initNewKey = 1; |
| 79 | + |
| 80 | + ret = wc_MakeRsaKey(&newKey, caKeySz, WC_RSA_EXPONENT, &rng); |
| 81 | + if (ret != 0) goto exit; |
| 82 | + |
| 83 | +#ifdef WOLFSSL_CAAM |
| 84 | + printf("Black key value = %u\n", newKey.blackKey); |
| 85 | +#endif |
| 86 | + |
| 87 | + printf("Successfully created CA Key\n\n"); |
| 88 | + |
| 89 | + derBuf = (byte*)XMALLOC(LARGE_TEMP_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); |
| 90 | + if (derBuf == NULL) goto exit; |
| 91 | + |
| 92 | + /*------------------------------------------------------------------------*/ |
| 93 | + /* write the new key to file in der format */ |
| 94 | + /*------------------------------------------------------------------------*/ |
| 95 | + printf("Writing newly generated DER key to file \"%s\"\n", |
| 96 | + newKeyOutput); |
| 97 | + file = fopen(newKeyOutput, "wb"); |
| 98 | + if (!file) { |
| 99 | + printf("failed to open file: %s\n", newKeyOutput); |
| 100 | + goto exit; |
| 101 | + } |
| 102 | + |
| 103 | + ret = wc_RsaKeyToDer(&newKey, derBuf, LARGE_TEMP_SZ); |
| 104 | + if (ret < 0) { |
| 105 | + goto exit; |
| 106 | + } |
| 107 | + derBufSz = ret; |
| 108 | + |
| 109 | + ret = (int)fwrite(derBuf, 1, derBufSz, file); |
| 110 | + fclose(file); |
| 111 | + printf("Successfully output %d bytes\n", ret); |
| 112 | + |
| 113 | +#ifdef WOLFSSL_DER_TO_PEM |
| 114 | + /*------------------------------------------------------------------------*/ |
| 115 | + /* convert the der to a pem and write it to a file */ |
| 116 | + /*------------------------------------------------------------------------*/ |
| 117 | + printf("Convert the DER key to PEM formatted key\n"); |
| 118 | + |
| 119 | + pemBuf = (byte*)XMALLOC(LARGE_TEMP_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); |
| 120 | + if (pemBuf == NULL) goto exit; |
| 121 | + XMEMSET(pemBuf, 0, LARGE_TEMP_SZ); |
| 122 | + |
| 123 | + pemBufSz = wc_DerToPem(derBuf, derBufSz, pemBuf, LARGE_TEMP_SZ, PRIVATEKEY_TYPE); |
| 124 | + if (pemBufSz < 0) goto exit; |
| 125 | + |
| 126 | + printf("Resulting PEM buffer is %d bytes\n", pemBufSz); |
| 127 | + |
| 128 | + file = fopen(pemKeyOutput, "wb"); |
| 129 | + if (!file) { |
| 130 | + printf("failed to open file: %s\n", pemKeyOutput); |
| 131 | + goto exit; |
| 132 | + } |
| 133 | + fwrite(pemBuf, 1, pemBufSz, file); |
| 134 | + fclose(file); |
| 135 | + printf("Successfully converted the DER to PEM to \"%s\"\n\n", |
| 136 | + pemKeyOutput); |
| 137 | + XFREE(pemBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); |
| 138 | + pemBuf = NULL; |
| 139 | +#endif |
| 140 | + |
| 141 | + /*------------------------------------------------------------------------*/ |
| 142 | + /* Create a new certificate using SUBJECT information from ca cert |
| 143 | + * for ISSUER information in generated cert */ |
| 144 | + /*------------------------------------------------------------------------*/ |
| 145 | + printf("Setting up new cert\n"); |
| 146 | + |
| 147 | + wc_InitCert(&newCert); |
| 148 | + |
| 149 | + strncpy(newCert.subject.country, "US", CTC_NAME_SIZE); |
| 150 | + strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE); |
| 151 | + strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE); |
| 152 | + strncpy(newCert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE); |
| 153 | + strncpy(newCert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE); |
| 154 | + strncpy(newCert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE); |
| 155 | + strncpy( newCert. subject. email, "[email protected]", CTC_NAME_SIZE); |
| 156 | + |
| 157 | + newCert.isCA = 1; |
| 158 | + newCert.sigType = CTC_SHA256wRSA; |
| 159 | + |
| 160 | + ret = wc_MakeSelfCert(&newCert, derBuf, LARGE_TEMP_SZ, &newKey, &rng); |
| 161 | + if (ret < 0) goto exit; |
| 162 | + printf("Make Self Cert returned %d\n", ret); |
| 163 | + derBufSz = ret; |
| 164 | + |
| 165 | + printf("Successfully created new ca certificate\n\n"); |
| 166 | + |
| 167 | + /*------------------------------------------------------------------------*/ |
| 168 | + /* write the new cert to file in der format */ |
| 169 | + /*------------------------------------------------------------------------*/ |
| 170 | + printf("Writing newly generated DER certificate to file \"%s\"\n", |
| 171 | + newCertOutput); |
| 172 | + file = fopen(newCertOutput, "wb"); |
| 173 | + if (!file) { |
| 174 | + printf("failed to open file: %s\n", newCertOutput); |
| 175 | + goto exit; |
| 176 | + } |
| 177 | + |
| 178 | + ret = (int)fwrite(derBuf, 1, derBufSz, file); |
| 179 | + fclose(file); |
| 180 | + printf("Successfully output %d bytes\n", ret); |
| 181 | + |
| 182 | +#ifdef WOLFSSL_DER_TO_PEM |
| 183 | + /*------------------------------------------------------------------------*/ |
| 184 | + /* convert the der to a pem and write it to a file */ |
| 185 | + /*------------------------------------------------------------------------*/ |
| 186 | + printf("Convert the DER cert to PEM formatted cert\n"); |
| 187 | + |
| 188 | + pemBuf = (byte*)XMALLOC(LARGE_TEMP_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); |
| 189 | + if (pemBuf == NULL) goto exit; |
| 190 | + XMEMSET(pemBuf, 0, LARGE_TEMP_SZ); |
| 191 | + |
| 192 | + pemBufSz = wc_DerToPem(derBuf, derBufSz, pemBuf, LARGE_TEMP_SZ, CERT_TYPE); |
| 193 | + if (pemBufSz < 0) goto exit; |
| 194 | + |
| 195 | + printf("Resulting PEM buffer is %d bytes\n", pemBufSz); |
| 196 | + |
| 197 | + file = fopen(pemOutput, "wb"); |
| 198 | + if (!file) { |
| 199 | + printf("failed to open file: %s\n", pemOutput); |
| 200 | + goto exit; |
| 201 | + } |
| 202 | + fwrite(pemBuf, 1, pemBufSz, file); |
| 203 | + fclose(file); |
| 204 | + printf("Successfully converted the DER to PEM to \"%s\"\n\n", |
| 205 | + pemOutput); |
| 206 | +#endif |
| 207 | + |
| 208 | + ret = 0; /* success */ |
| 209 | + |
| 210 | +exit: |
| 211 | + |
| 212 | + XFREE(derBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); |
| 213 | + XFREE(pemBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); |
| 214 | + |
| 215 | + if (initNewKey) |
| 216 | + wc_FreeRsaKey(&newKey); |
| 217 | + if (initRng) { |
| 218 | + wc_FreeRng(&rng); |
| 219 | + } |
| 220 | + |
| 221 | + if (ret == 0) |
| 222 | + printf("Tests passed\n"); |
| 223 | + else |
| 224 | + printf("Failure code was %d\n", ret); |
| 225 | + |
| 226 | + wolfCrypt_Cleanup(); |
| 227 | + return ret; |
| 228 | +} |
| 229 | +#endif |
| 230 | + |
| 231 | +int main(int argc, char** argv) |
| 232 | +{ |
| 233 | +#if !defined(WOLFSSL_CERT_REQ) || !defined(WOLFSSL_CERT_GEN) || \ |
| 234 | + !defined(WOLFSSL_KEY_GEN) || defined(NO_RSA) |
| 235 | + printf("Please compile wolfSSL with --enable-certreq --enable-certgen " |
| 236 | + "--enable-keygen --enable-rsa\n"); |
| 237 | + return 0; |
| 238 | +#else |
| 239 | + return do_cagen(argc, argv); |
| 240 | +#endif |
| 241 | +} |
0 commit comments