wolfSSL SSL/TLS library, support up to TLS1.3
Dependents: CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more
wolfcrypt/src/asn.c@15:117db924cf7c, 2018-08-18 (annotated)
- Committer:
- wolfSSL
- Date:
- Sat Aug 18 22:20:43 2018 +0000
- Revision:
- 15:117db924cf7c
- Child:
- 16:8e0d178b1d1e
wolfSSL 3.15.3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 15:117db924cf7c | 1 | /* asn.c |
wolfSSL | 15:117db924cf7c | 2 | * |
wolfSSL | 15:117db924cf7c | 3 | * Copyright (C) 2006-2017 wolfSSL Inc. |
wolfSSL | 15:117db924cf7c | 4 | * |
wolfSSL | 15:117db924cf7c | 5 | * This file is part of wolfSSL. |
wolfSSL | 15:117db924cf7c | 6 | * |
wolfSSL | 15:117db924cf7c | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 15:117db924cf7c | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 15:117db924cf7c | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 15:117db924cf7c | 10 | * (at your option) any later version. |
wolfSSL | 15:117db924cf7c | 11 | * |
wolfSSL | 15:117db924cf7c | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 15:117db924cf7c | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 15:117db924cf7c | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 15:117db924cf7c | 15 | * GNU General Public License for more details. |
wolfSSL | 15:117db924cf7c | 16 | * |
wolfSSL | 15:117db924cf7c | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 15:117db924cf7c | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 15:117db924cf7c | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 15:117db924cf7c | 20 | */ |
wolfSSL | 15:117db924cf7c | 21 | |
wolfSSL | 15:117db924cf7c | 22 | |
wolfSSL | 15:117db924cf7c | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 15:117db924cf7c | 24 | #include <config.h> |
wolfSSL | 15:117db924cf7c | 25 | #endif |
wolfSSL | 15:117db924cf7c | 26 | |
wolfSSL | 15:117db924cf7c | 27 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 15:117db924cf7c | 28 | |
wolfSSL | 15:117db924cf7c | 29 | /* |
wolfSSL | 15:117db924cf7c | 30 | ASN Options: |
wolfSSL | 15:117db924cf7c | 31 | * NO_ASN_TIME: Disables time parts of the ASN code for systems without an RTC |
wolfSSL | 15:117db924cf7c | 32 | or wishing to save space. |
wolfSSL | 15:117db924cf7c | 33 | * IGNORE_NAME_CONSTRAINTS: Skip ASN name checks. |
wolfSSL | 15:117db924cf7c | 34 | * ASN_DUMP_OID: Allows dump of OID information for debugging. |
wolfSSL | 15:117db924cf7c | 35 | * RSA_DECODE_EXTRA: Decodes extra information in RSA public key. |
wolfSSL | 15:117db924cf7c | 36 | * WOLFSSL_CERT_GEN: Cert generation. Saves extra certificate info in GetName. |
wolfSSL | 15:117db924cf7c | 37 | * WOLFSSL_NO_ASN_STRICT: Disable strict RFC compliance checks to |
wolfSSL | 15:117db924cf7c | 38 | restore 3.13.0 behavior. |
wolfSSL | 15:117db924cf7c | 39 | * WOLFSSL_NO_OCSP_OPTIONAL_CERTS: Skip optional OCSP certs (responder issuer |
wolfSSL | 15:117db924cf7c | 40 | must still be trusted) |
wolfSSL | 15:117db924cf7c | 41 | * WOLFSSL_NO_TRUSTED_CERTS_VERIFY: Workaround for situation where entire cert |
wolfSSL | 15:117db924cf7c | 42 | chain is not loaded. This only matches on subject and public key and |
wolfSSL | 15:117db924cf7c | 43 | does not perform a PKI validation, so it is not a secure solution. |
wolfSSL | 15:117db924cf7c | 44 | Only enabled for OCSP. |
wolfSSL | 15:117db924cf7c | 45 | * WOLFSSL_NO_OCSP_ISSUER_CHECK: Can be defined for backwards compatibility to |
wolfSSL | 15:117db924cf7c | 46 | disable checking of OCSP subject hash with issuer hash. |
wolfSSL | 15:117db924cf7c | 47 | * WOLFSSL_ALT_CERT_CHAINS: Allows matching multiple CA's to validate |
wolfSSL | 15:117db924cf7c | 48 | chain based on issuer and public key (includes signature confirmation) |
wolfSSL | 15:117db924cf7c | 49 | */ |
wolfSSL | 15:117db924cf7c | 50 | |
wolfSSL | 15:117db924cf7c | 51 | #ifndef NO_ASN |
wolfSSL | 15:117db924cf7c | 52 | |
wolfSSL | 15:117db924cf7c | 53 | #include <wolfssl/wolfcrypt/asn.h> |
wolfSSL | 15:117db924cf7c | 54 | #include <wolfssl/wolfcrypt/coding.h> |
wolfSSL | 15:117db924cf7c | 55 | #include <wolfssl/wolfcrypt/md2.h> |
wolfSSL | 15:117db924cf7c | 56 | #include <wolfssl/wolfcrypt/hmac.h> |
wolfSSL | 15:117db924cf7c | 57 | #include <wolfssl/wolfcrypt/error-crypt.h> |
wolfSSL | 15:117db924cf7c | 58 | #include <wolfssl/wolfcrypt/pwdbased.h> |
wolfSSL | 15:117db924cf7c | 59 | #include <wolfssl/wolfcrypt/des3.h> |
wolfSSL | 15:117db924cf7c | 60 | #include <wolfssl/wolfcrypt/aes.h> |
wolfSSL | 15:117db924cf7c | 61 | #include <wolfssl/wolfcrypt/wc_encrypt.h> |
wolfSSL | 15:117db924cf7c | 62 | #include <wolfssl/wolfcrypt/logging.h> |
wolfSSL | 15:117db924cf7c | 63 | |
wolfSSL | 15:117db924cf7c | 64 | #include <wolfssl/wolfcrypt/random.h> |
wolfSSL | 15:117db924cf7c | 65 | #include <wolfssl/wolfcrypt/hash.h> |
wolfSSL | 15:117db924cf7c | 66 | #ifdef NO_INLINE |
wolfSSL | 15:117db924cf7c | 67 | #include <wolfssl/wolfcrypt/misc.h> |
wolfSSL | 15:117db924cf7c | 68 | #else |
wolfSSL | 15:117db924cf7c | 69 | #define WOLFSSL_MISC_INCLUDED |
wolfSSL | 15:117db924cf7c | 70 | #include <wolfcrypt/src/misc.c> |
wolfSSL | 15:117db924cf7c | 71 | #endif |
wolfSSL | 15:117db924cf7c | 72 | |
wolfSSL | 15:117db924cf7c | 73 | #ifndef NO_PWDBASED |
wolfSSL | 15:117db924cf7c | 74 | #include <wolfssl/wolfcrypt/aes.h> |
wolfSSL | 15:117db924cf7c | 75 | #endif |
wolfSSL | 15:117db924cf7c | 76 | #ifndef NO_RC4 |
wolfSSL | 15:117db924cf7c | 77 | #include <wolfssl/wolfcrypt/arc4.h> |
wolfSSL | 15:117db924cf7c | 78 | #endif |
wolfSSL | 15:117db924cf7c | 79 | |
wolfSSL | 15:117db924cf7c | 80 | #ifdef HAVE_NTRU |
wolfSSL | 15:117db924cf7c | 81 | #include "libntruencrypt/ntru_crypto.h" |
wolfSSL | 15:117db924cf7c | 82 | #endif |
wolfSSL | 15:117db924cf7c | 83 | |
wolfSSL | 15:117db924cf7c | 84 | #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) |
wolfSSL | 15:117db924cf7c | 85 | #include <wolfssl/wolfcrypt/sha512.h> |
wolfSSL | 15:117db924cf7c | 86 | #endif |
wolfSSL | 15:117db924cf7c | 87 | |
wolfSSL | 15:117db924cf7c | 88 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 89 | #include <wolfssl/wolfcrypt/sha256.h> |
wolfSSL | 15:117db924cf7c | 90 | #endif |
wolfSSL | 15:117db924cf7c | 91 | |
wolfSSL | 15:117db924cf7c | 92 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 93 | #include <wolfssl/wolfcrypt/ecc.h> |
wolfSSL | 15:117db924cf7c | 94 | #endif |
wolfSSL | 15:117db924cf7c | 95 | |
wolfSSL | 15:117db924cf7c | 96 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 97 | #include <wolfssl/wolfcrypt/ed25519.h> |
wolfSSL | 15:117db924cf7c | 98 | #endif |
wolfSSL | 15:117db924cf7c | 99 | |
wolfSSL | 15:117db924cf7c | 100 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 101 | #include <wolfssl/wolfcrypt/rsa.h> |
wolfSSL | 15:117db924cf7c | 102 | #endif |
wolfSSL | 15:117db924cf7c | 103 | |
wolfSSL | 15:117db924cf7c | 104 | #ifdef WOLFSSL_DEBUG_ENCODING |
wolfSSL | 15:117db924cf7c | 105 | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
wolfSSL | 15:117db924cf7c | 106 | #if MQX_USE_IO_OLD |
wolfSSL | 15:117db924cf7c | 107 | #include <fio.h> |
wolfSSL | 15:117db924cf7c | 108 | #else |
wolfSSL | 15:117db924cf7c | 109 | #include <nio.h> |
wolfSSL | 15:117db924cf7c | 110 | #endif |
wolfSSL | 15:117db924cf7c | 111 | #else |
wolfSSL | 15:117db924cf7c | 112 | #include <stdio.h> |
wolfSSL | 15:117db924cf7c | 113 | #endif |
wolfSSL | 15:117db924cf7c | 114 | #endif |
wolfSSL | 15:117db924cf7c | 115 | |
wolfSSL | 15:117db924cf7c | 116 | |
wolfSSL | 15:117db924cf7c | 117 | #ifdef _MSC_VER |
wolfSSL | 15:117db924cf7c | 118 | /* 4996 warning to use MS extensions e.g., strcpy_s instead of XSTRNCPY */ |
wolfSSL | 15:117db924cf7c | 119 | #pragma warning(disable: 4996) |
wolfSSL | 15:117db924cf7c | 120 | #endif |
wolfSSL | 15:117db924cf7c | 121 | |
wolfSSL | 15:117db924cf7c | 122 | #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; } |
wolfSSL | 15:117db924cf7c | 123 | |
wolfSSL | 15:117db924cf7c | 124 | WOLFSSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 15:117db924cf7c | 125 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 126 | { |
wolfSSL | 15:117db924cf7c | 127 | int length = 0; |
wolfSSL | 15:117db924cf7c | 128 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 129 | byte b; |
wolfSSL | 15:117db924cf7c | 130 | |
wolfSSL | 15:117db924cf7c | 131 | *len = 0; /* default length */ |
wolfSSL | 15:117db924cf7c | 132 | |
wolfSSL | 15:117db924cf7c | 133 | if ((idx + 1) > maxIdx) { /* for first read */ |
wolfSSL | 15:117db924cf7c | 134 | WOLFSSL_MSG("GetLength bad index on input"); |
wolfSSL | 15:117db924cf7c | 135 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 136 | } |
wolfSSL | 15:117db924cf7c | 137 | |
wolfSSL | 15:117db924cf7c | 138 | b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 139 | if (b >= ASN_LONG_LENGTH) { |
wolfSSL | 15:117db924cf7c | 140 | word32 bytes = b & 0x7F; |
wolfSSL | 15:117db924cf7c | 141 | |
wolfSSL | 15:117db924cf7c | 142 | if ((idx + bytes) > maxIdx) { /* for reading bytes */ |
wolfSSL | 15:117db924cf7c | 143 | WOLFSSL_MSG("GetLength bad long length"); |
wolfSSL | 15:117db924cf7c | 144 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 145 | } |
wolfSSL | 15:117db924cf7c | 146 | |
wolfSSL | 15:117db924cf7c | 147 | while (bytes--) { |
wolfSSL | 15:117db924cf7c | 148 | b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 149 | length = (length << 8) | b; |
wolfSSL | 15:117db924cf7c | 150 | } |
wolfSSL | 15:117db924cf7c | 151 | } |
wolfSSL | 15:117db924cf7c | 152 | else |
wolfSSL | 15:117db924cf7c | 153 | length = b; |
wolfSSL | 15:117db924cf7c | 154 | |
wolfSSL | 15:117db924cf7c | 155 | if ((idx + length) > maxIdx) { /* for user of length */ |
wolfSSL | 15:117db924cf7c | 156 | WOLFSSL_MSG("GetLength value exceeds buffer length"); |
wolfSSL | 15:117db924cf7c | 157 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 158 | } |
wolfSSL | 15:117db924cf7c | 159 | |
wolfSSL | 15:117db924cf7c | 160 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 161 | if (length > 0) |
wolfSSL | 15:117db924cf7c | 162 | *len = length; |
wolfSSL | 15:117db924cf7c | 163 | |
wolfSSL | 15:117db924cf7c | 164 | return length; |
wolfSSL | 15:117db924cf7c | 165 | } |
wolfSSL | 15:117db924cf7c | 166 | |
wolfSSL | 15:117db924cf7c | 167 | |
wolfSSL | 15:117db924cf7c | 168 | /* Get the DER/BER encoding of an ASN.1 header. |
wolfSSL | 15:117db924cf7c | 169 | * |
wolfSSL | 15:117db924cf7c | 170 | * input Buffer holding DER/BER encoded data. |
wolfSSL | 15:117db924cf7c | 171 | * tag ASN.1 tag value expected in header. |
wolfSSL | 15:117db924cf7c | 172 | * inOutIdx Current index into buffer to parse. |
wolfSSL | 15:117db924cf7c | 173 | * len The number of bytes in the ASN.1 data. |
wolfSSL | 15:117db924cf7c | 174 | * maxIdx Length of data in buffer. |
wolfSSL | 15:117db924cf7c | 175 | * returns BUFFER_E when there is not enough data to parse. |
wolfSSL | 15:117db924cf7c | 176 | * ASN_PARSE_E when the expected tag is not found or length is invalid. |
wolfSSL | 15:117db924cf7c | 177 | * Otherwise, the number of bytes in the ASN.1 data. |
wolfSSL | 15:117db924cf7c | 178 | */ |
wolfSSL | 15:117db924cf7c | 179 | static int GetASNHeader(const byte* input, byte tag, word32* inOutIdx, int* len, |
wolfSSL | 15:117db924cf7c | 180 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 181 | { |
wolfSSL | 15:117db924cf7c | 182 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 183 | byte b; |
wolfSSL | 15:117db924cf7c | 184 | int length; |
wolfSSL | 15:117db924cf7c | 185 | |
wolfSSL | 15:117db924cf7c | 186 | if ((idx + 1) > maxIdx) |
wolfSSL | 15:117db924cf7c | 187 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 188 | |
wolfSSL | 15:117db924cf7c | 189 | b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 190 | if (b != tag) |
wolfSSL | 15:117db924cf7c | 191 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 192 | |
wolfSSL | 15:117db924cf7c | 193 | if (GetLength(input, &idx, &length, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 194 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 195 | |
wolfSSL | 15:117db924cf7c | 196 | *len = length; |
wolfSSL | 15:117db924cf7c | 197 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 198 | return length; |
wolfSSL | 15:117db924cf7c | 199 | } |
wolfSSL | 15:117db924cf7c | 200 | |
wolfSSL | 15:117db924cf7c | 201 | WOLFSSL_LOCAL int GetSequence(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 15:117db924cf7c | 202 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 203 | { |
wolfSSL | 15:117db924cf7c | 204 | return GetASNHeader(input, ASN_SEQUENCE | ASN_CONSTRUCTED, inOutIdx, len, |
wolfSSL | 15:117db924cf7c | 205 | maxIdx); |
wolfSSL | 15:117db924cf7c | 206 | } |
wolfSSL | 15:117db924cf7c | 207 | |
wolfSSL | 15:117db924cf7c | 208 | |
wolfSSL | 15:117db924cf7c | 209 | WOLFSSL_LOCAL int GetSet(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 15:117db924cf7c | 210 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 211 | { |
wolfSSL | 15:117db924cf7c | 212 | return GetASNHeader(input, ASN_SET | ASN_CONSTRUCTED, inOutIdx, len, |
wolfSSL | 15:117db924cf7c | 213 | maxIdx); |
wolfSSL | 15:117db924cf7c | 214 | } |
wolfSSL | 15:117db924cf7c | 215 | |
wolfSSL | 15:117db924cf7c | 216 | /* Get the DER/BER encoded ASN.1 NULL element. |
wolfSSL | 15:117db924cf7c | 217 | * Ensure that the all fields are as expected and move index past the element. |
wolfSSL | 15:117db924cf7c | 218 | * |
wolfSSL | 15:117db924cf7c | 219 | * input Buffer holding DER/BER encoded data. |
wolfSSL | 15:117db924cf7c | 220 | * inOutIdx Current index into buffer to parse. |
wolfSSL | 15:117db924cf7c | 221 | * maxIdx Length of data in buffer. |
wolfSSL | 15:117db924cf7c | 222 | * returns BUFFER_E when there is not enough data to parse. |
wolfSSL | 15:117db924cf7c | 223 | * ASN_TAG_NULL_E when the NULL tag is not found. |
wolfSSL | 15:117db924cf7c | 224 | * ASN_EXPECT_0_E when the length is not zero. |
wolfSSL | 15:117db924cf7c | 225 | * Otherwise, 0 to indicate success. |
wolfSSL | 15:117db924cf7c | 226 | */ |
wolfSSL | 15:117db924cf7c | 227 | static int GetASNNull(const byte* input, word32* inOutIdx, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 228 | { |
wolfSSL | 15:117db924cf7c | 229 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 230 | byte b; |
wolfSSL | 15:117db924cf7c | 231 | |
wolfSSL | 15:117db924cf7c | 232 | if ((idx + 2) > maxIdx) |
wolfSSL | 15:117db924cf7c | 233 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 234 | |
wolfSSL | 15:117db924cf7c | 235 | b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 236 | if (b != ASN_TAG_NULL) |
wolfSSL | 15:117db924cf7c | 237 | return ASN_TAG_NULL_E; |
wolfSSL | 15:117db924cf7c | 238 | |
wolfSSL | 15:117db924cf7c | 239 | if (input[idx++] != 0) |
wolfSSL | 15:117db924cf7c | 240 | return ASN_EXPECT_0_E; |
wolfSSL | 15:117db924cf7c | 241 | |
wolfSSL | 15:117db924cf7c | 242 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 243 | return 0; |
wolfSSL | 15:117db924cf7c | 244 | } |
wolfSSL | 15:117db924cf7c | 245 | |
wolfSSL | 15:117db924cf7c | 246 | /* Set the DER/BER encoding of the ASN.1 NULL element. |
wolfSSL | 15:117db924cf7c | 247 | * |
wolfSSL | 15:117db924cf7c | 248 | * output Buffer to write into. |
wolfSSL | 15:117db924cf7c | 249 | * returns the number of bytes added to the buffer. |
wolfSSL | 15:117db924cf7c | 250 | */ |
wolfSSL | 15:117db924cf7c | 251 | static int SetASNNull(byte* output) |
wolfSSL | 15:117db924cf7c | 252 | { |
wolfSSL | 15:117db924cf7c | 253 | output[0] = ASN_TAG_NULL; |
wolfSSL | 15:117db924cf7c | 254 | output[1] = 0; |
wolfSSL | 15:117db924cf7c | 255 | |
wolfSSL | 15:117db924cf7c | 256 | return 2; |
wolfSSL | 15:117db924cf7c | 257 | } |
wolfSSL | 15:117db924cf7c | 258 | |
wolfSSL | 15:117db924cf7c | 259 | /* Get the DER/BER encoding of an ASN.1 BOOLEAN. |
wolfSSL | 15:117db924cf7c | 260 | * |
wolfSSL | 15:117db924cf7c | 261 | * input Buffer holding DER/BER encoded data. |
wolfSSL | 15:117db924cf7c | 262 | * inOutIdx Current index into buffer to parse. |
wolfSSL | 15:117db924cf7c | 263 | * maxIdx Length of data in buffer. |
wolfSSL | 15:117db924cf7c | 264 | * returns BUFFER_E when there is not enough data to parse. |
wolfSSL | 15:117db924cf7c | 265 | * ASN_PARSE_E when the BOOLEAN tag is not found or length is not 1. |
wolfSSL | 15:117db924cf7c | 266 | * Otherwise, 0 to indicate the value was false and 1 to indicate true. |
wolfSSL | 15:117db924cf7c | 267 | */ |
wolfSSL | 15:117db924cf7c | 268 | static int GetBoolean(const byte* input, word32* inOutIdx, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 269 | { |
wolfSSL | 15:117db924cf7c | 270 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 271 | byte b; |
wolfSSL | 15:117db924cf7c | 272 | |
wolfSSL | 15:117db924cf7c | 273 | if ((idx + 3) > maxIdx) |
wolfSSL | 15:117db924cf7c | 274 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 275 | |
wolfSSL | 15:117db924cf7c | 276 | b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 277 | if (b != ASN_BOOLEAN) |
wolfSSL | 15:117db924cf7c | 278 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 279 | |
wolfSSL | 15:117db924cf7c | 280 | if (input[idx++] != 1) |
wolfSSL | 15:117db924cf7c | 281 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 282 | |
wolfSSL | 15:117db924cf7c | 283 | b = input[idx++] != 0; |
wolfSSL | 15:117db924cf7c | 284 | |
wolfSSL | 15:117db924cf7c | 285 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 286 | return b; |
wolfSSL | 15:117db924cf7c | 287 | } |
wolfSSL | 15:117db924cf7c | 288 | |
wolfSSL | 15:117db924cf7c | 289 | #ifdef ASN1_SET_BOOLEAN |
wolfSSL | 15:117db924cf7c | 290 | /* Set the DER/BER encoding of the ASN.1 NULL element. |
wolfSSL | 15:117db924cf7c | 291 | * Note: Function not required as yet. |
wolfSSL | 15:117db924cf7c | 292 | * |
wolfSSL | 15:117db924cf7c | 293 | * val Boolean value to encode. |
wolfSSL | 15:117db924cf7c | 294 | * output Buffer to write into. |
wolfSSL | 15:117db924cf7c | 295 | * returns the number of bytes added to the buffer. |
wolfSSL | 15:117db924cf7c | 296 | */ |
wolfSSL | 15:117db924cf7c | 297 | static int SetBoolean(int val, byte* output) |
wolfSSL | 15:117db924cf7c | 298 | { |
wolfSSL | 15:117db924cf7c | 299 | output[0] = ASN_BOOLEAN; |
wolfSSL | 15:117db924cf7c | 300 | output[1] = 1; |
wolfSSL | 15:117db924cf7c | 301 | output[2] = val ? -1 : 0; |
wolfSSL | 15:117db924cf7c | 302 | |
wolfSSL | 15:117db924cf7c | 303 | return 3; |
wolfSSL | 15:117db924cf7c | 304 | } |
wolfSSL | 15:117db924cf7c | 305 | #endif |
wolfSSL | 15:117db924cf7c | 306 | |
wolfSSL | 15:117db924cf7c | 307 | /* Get the DER/BER encoding of an ASN.1 OCTET_STRING header. |
wolfSSL | 15:117db924cf7c | 308 | * |
wolfSSL | 15:117db924cf7c | 309 | * input Buffer holding DER/BER encoded data. |
wolfSSL | 15:117db924cf7c | 310 | * inOutIdx Current index into buffer to parse. |
wolfSSL | 15:117db924cf7c | 311 | * len The number of bytes in the ASN.1 data. |
wolfSSL | 15:117db924cf7c | 312 | * maxIdx Length of data in buffer. |
wolfSSL | 15:117db924cf7c | 313 | * returns BUFFER_E when there is not enough data to parse. |
wolfSSL | 15:117db924cf7c | 314 | * ASN_PARSE_E when the OCTET_STRING tag is not found or length is |
wolfSSL | 15:117db924cf7c | 315 | * invalid. |
wolfSSL | 15:117db924cf7c | 316 | * Otherwise, the number of bytes in the ASN.1 data. |
wolfSSL | 15:117db924cf7c | 317 | */ |
wolfSSL | 15:117db924cf7c | 318 | static int GetOctetString(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 15:117db924cf7c | 319 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 320 | { |
wolfSSL | 15:117db924cf7c | 321 | return GetASNHeader(input, ASN_OCTET_STRING, inOutIdx, len, maxIdx); |
wolfSSL | 15:117db924cf7c | 322 | } |
wolfSSL | 15:117db924cf7c | 323 | |
wolfSSL | 15:117db924cf7c | 324 | /* Get the DER/BER encoding of an ASN.1 INTEGER header. |
wolfSSL | 15:117db924cf7c | 325 | * Removes the leading zero byte when found. |
wolfSSL | 15:117db924cf7c | 326 | * |
wolfSSL | 15:117db924cf7c | 327 | * input Buffer holding DER/BER encoded data. |
wolfSSL | 15:117db924cf7c | 328 | * inOutIdx Current index into buffer to parse. |
wolfSSL | 15:117db924cf7c | 329 | * len The number of bytes in the ASN.1 data (excluding any leading zero). |
wolfSSL | 15:117db924cf7c | 330 | * maxIdx Length of data in buffer. |
wolfSSL | 15:117db924cf7c | 331 | * returns BUFFER_E when there is not enough data to parse. |
wolfSSL | 15:117db924cf7c | 332 | * ASN_PARSE_E when the INTEGER tag is not found, length is invalid, |
wolfSSL | 15:117db924cf7c | 333 | * or invalid use of or missing leading zero. |
wolfSSL | 15:117db924cf7c | 334 | * Otherwise, 0 to indicate success. |
wolfSSL | 15:117db924cf7c | 335 | */ |
wolfSSL | 15:117db924cf7c | 336 | static int GetASNInt(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 15:117db924cf7c | 337 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 338 | { |
wolfSSL | 15:117db924cf7c | 339 | int ret; |
wolfSSL | 15:117db924cf7c | 340 | |
wolfSSL | 15:117db924cf7c | 341 | ret = GetASNHeader(input, ASN_INTEGER, inOutIdx, len, maxIdx); |
wolfSSL | 15:117db924cf7c | 342 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 343 | return ret; |
wolfSSL | 15:117db924cf7c | 344 | |
wolfSSL | 15:117db924cf7c | 345 | if (*len > 0) { |
wolfSSL | 15:117db924cf7c | 346 | /* remove leading zero, unless there is only one 0x00 byte */ |
wolfSSL | 15:117db924cf7c | 347 | if ((input[*inOutIdx] == 0x00) && (*len > 1)) { |
wolfSSL | 15:117db924cf7c | 348 | (*inOutIdx)++; |
wolfSSL | 15:117db924cf7c | 349 | (*len)--; |
wolfSSL | 15:117db924cf7c | 350 | |
wolfSSL | 15:117db924cf7c | 351 | if (*len > 0 && (input[*inOutIdx] & 0x80) == 0) |
wolfSSL | 15:117db924cf7c | 352 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 353 | } |
wolfSSL | 15:117db924cf7c | 354 | } |
wolfSSL | 15:117db924cf7c | 355 | |
wolfSSL | 15:117db924cf7c | 356 | return 0; |
wolfSSL | 15:117db924cf7c | 357 | } |
wolfSSL | 15:117db924cf7c | 358 | |
wolfSSL | 15:117db924cf7c | 359 | /* Get the DER/BER encoding of an ASN.1 INTEGER that has a value of no more than |
wolfSSL | 15:117db924cf7c | 360 | * 7 bits. |
wolfSSL | 15:117db924cf7c | 361 | * |
wolfSSL | 15:117db924cf7c | 362 | * input Buffer holding DER/BER encoded data. |
wolfSSL | 15:117db924cf7c | 363 | * inOutIdx Current index into buffer to parse. |
wolfSSL | 15:117db924cf7c | 364 | * maxIdx Length of data in buffer. |
wolfSSL | 15:117db924cf7c | 365 | * returns BUFFER_E when there is not enough data to parse. |
wolfSSL | 15:117db924cf7c | 366 | * ASN_PARSE_E when the INTEGER tag is not found or length is invalid. |
wolfSSL | 15:117db924cf7c | 367 | * Otherwise, the 7-bit value. |
wolfSSL | 15:117db924cf7c | 368 | */ |
wolfSSL | 15:117db924cf7c | 369 | static int GetInteger7Bit(const byte* input, word32* inOutIdx, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 370 | { |
wolfSSL | 15:117db924cf7c | 371 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 372 | byte b; |
wolfSSL | 15:117db924cf7c | 373 | |
wolfSSL | 15:117db924cf7c | 374 | if ((idx + 3) > maxIdx) |
wolfSSL | 15:117db924cf7c | 375 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 376 | |
wolfSSL | 15:117db924cf7c | 377 | if (input[idx++] != ASN_INTEGER) |
wolfSSL | 15:117db924cf7c | 378 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 379 | if (input[idx++] != 1) |
wolfSSL | 15:117db924cf7c | 380 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 381 | b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 382 | |
wolfSSL | 15:117db924cf7c | 383 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 384 | return b; |
wolfSSL | 15:117db924cf7c | 385 | } |
wolfSSL | 15:117db924cf7c | 386 | |
wolfSSL | 15:117db924cf7c | 387 | |
wolfSSL | 15:117db924cf7c | 388 | #if !defined(NO_DSA) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 389 | static char sigSha1wDsaName[] = "SHAwDSA"; |
wolfSSL | 15:117db924cf7c | 390 | #endif /* NO_DSA */ |
wolfSSL | 15:117db924cf7c | 391 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 392 | #ifdef WOLFSSL_MD2 |
wolfSSL | 15:117db924cf7c | 393 | static char sigMd2wRsaName[] = "MD2wRSA"; |
wolfSSL | 15:117db924cf7c | 394 | #endif |
wolfSSL | 15:117db924cf7c | 395 | #ifndef NO_MD5 |
wolfSSL | 15:117db924cf7c | 396 | static char sigMd5wRsaName[] = "MD5wRSA"; |
wolfSSL | 15:117db924cf7c | 397 | #endif |
wolfSSL | 15:117db924cf7c | 398 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 399 | static char sigSha1wRsaName[] = "SHAwRSA"; |
wolfSSL | 15:117db924cf7c | 400 | #endif |
wolfSSL | 15:117db924cf7c | 401 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 402 | static char sigSha224wRsaName[] = "SHA224wRSA"; |
wolfSSL | 15:117db924cf7c | 403 | #endif |
wolfSSL | 15:117db924cf7c | 404 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 405 | static char sigSha256wRsaName[] = "SHA256wRSA"; |
wolfSSL | 15:117db924cf7c | 406 | #endif |
wolfSSL | 15:117db924cf7c | 407 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 408 | static char sigSha384wRsaName[] = "SHA384wRSA"; |
wolfSSL | 15:117db924cf7c | 409 | #endif |
wolfSSL | 15:117db924cf7c | 410 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 411 | static char sigSha512wRsaName[] = "SHA512wRSA"; |
wolfSSL | 15:117db924cf7c | 412 | #endif |
wolfSSL | 15:117db924cf7c | 413 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 414 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 415 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 416 | static char sigSha1wEcdsaName[] = "SHAwECDSA"; |
wolfSSL | 15:117db924cf7c | 417 | #endif |
wolfSSL | 15:117db924cf7c | 418 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 419 | static char sigSha224wEcdsaName[] = "SHA224wECDSA"; |
wolfSSL | 15:117db924cf7c | 420 | #endif |
wolfSSL | 15:117db924cf7c | 421 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 422 | static char sigSha256wEcdsaName[] = "SHA256wECDSA"; |
wolfSSL | 15:117db924cf7c | 423 | #endif |
wolfSSL | 15:117db924cf7c | 424 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 425 | static char sigSha384wEcdsaName[] = "SHA384wECDSA"; |
wolfSSL | 15:117db924cf7c | 426 | #endif |
wolfSSL | 15:117db924cf7c | 427 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 428 | static char sigSha512wEcdsaName[] = "SHA512wECDSA"; |
wolfSSL | 15:117db924cf7c | 429 | #endif |
wolfSSL | 15:117db924cf7c | 430 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 431 | static char sigUnknownName[] = "Unknown"; |
wolfSSL | 15:117db924cf7c | 432 | |
wolfSSL | 15:117db924cf7c | 433 | |
wolfSSL | 15:117db924cf7c | 434 | /* Get the human readable string for a signature type |
wolfSSL | 15:117db924cf7c | 435 | * |
wolfSSL | 15:117db924cf7c | 436 | * oid Oid value for signature |
wolfSSL | 15:117db924cf7c | 437 | */ |
wolfSSL | 15:117db924cf7c | 438 | char* GetSigName(int oid) { |
wolfSSL | 15:117db924cf7c | 439 | switch (oid) { |
wolfSSL | 15:117db924cf7c | 440 | #if !defined(NO_DSA) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 441 | case CTC_SHAwDSA: |
wolfSSL | 15:117db924cf7c | 442 | return sigSha1wDsaName; |
wolfSSL | 15:117db924cf7c | 443 | #endif /* NO_DSA && NO_SHA */ |
wolfSSL | 15:117db924cf7c | 444 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 445 | #ifdef WOLFSSL_MD2 |
wolfSSL | 15:117db924cf7c | 446 | case CTC_MD2wRSA: |
wolfSSL | 15:117db924cf7c | 447 | return sigMd2wRsaName; |
wolfSSL | 15:117db924cf7c | 448 | #endif |
wolfSSL | 15:117db924cf7c | 449 | #ifndef NO_MD5 |
wolfSSL | 15:117db924cf7c | 450 | case CTC_MD5wRSA: |
wolfSSL | 15:117db924cf7c | 451 | return sigMd5wRsaName; |
wolfSSL | 15:117db924cf7c | 452 | #endif |
wolfSSL | 15:117db924cf7c | 453 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 454 | case CTC_SHAwRSA: |
wolfSSL | 15:117db924cf7c | 455 | return sigSha1wRsaName; |
wolfSSL | 15:117db924cf7c | 456 | #endif |
wolfSSL | 15:117db924cf7c | 457 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 458 | case CTC_SHA224wRSA: |
wolfSSL | 15:117db924cf7c | 459 | return sigSha224wRsaName; |
wolfSSL | 15:117db924cf7c | 460 | #endif |
wolfSSL | 15:117db924cf7c | 461 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 462 | case CTC_SHA256wRSA: |
wolfSSL | 15:117db924cf7c | 463 | return sigSha256wRsaName; |
wolfSSL | 15:117db924cf7c | 464 | #endif |
wolfSSL | 15:117db924cf7c | 465 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 466 | case CTC_SHA384wRSA: |
wolfSSL | 15:117db924cf7c | 467 | return sigSha384wRsaName; |
wolfSSL | 15:117db924cf7c | 468 | #endif |
wolfSSL | 15:117db924cf7c | 469 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 470 | case CTC_SHA512wRSA: |
wolfSSL | 15:117db924cf7c | 471 | return sigSha512wRsaName; |
wolfSSL | 15:117db924cf7c | 472 | #endif |
wolfSSL | 15:117db924cf7c | 473 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 474 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 475 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 476 | case CTC_SHAwECDSA: |
wolfSSL | 15:117db924cf7c | 477 | return sigSha1wEcdsaName; |
wolfSSL | 15:117db924cf7c | 478 | #endif |
wolfSSL | 15:117db924cf7c | 479 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 480 | case CTC_SHA224wECDSA: |
wolfSSL | 15:117db924cf7c | 481 | return sigSha224wEcdsaName; |
wolfSSL | 15:117db924cf7c | 482 | #endif |
wolfSSL | 15:117db924cf7c | 483 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 484 | case CTC_SHA256wECDSA: |
wolfSSL | 15:117db924cf7c | 485 | return sigSha256wEcdsaName; |
wolfSSL | 15:117db924cf7c | 486 | #endif |
wolfSSL | 15:117db924cf7c | 487 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 488 | case CTC_SHA384wECDSA: |
wolfSSL | 15:117db924cf7c | 489 | return sigSha384wEcdsaName; |
wolfSSL | 15:117db924cf7c | 490 | #endif |
wolfSSL | 15:117db924cf7c | 491 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 492 | case CTC_SHA512wECDSA: |
wolfSSL | 15:117db924cf7c | 493 | return sigSha512wEcdsaName; |
wolfSSL | 15:117db924cf7c | 494 | #endif |
wolfSSL | 15:117db924cf7c | 495 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 496 | default: |
wolfSSL | 15:117db924cf7c | 497 | return sigUnknownName; |
wolfSSL | 15:117db924cf7c | 498 | } |
wolfSSL | 15:117db924cf7c | 499 | } |
wolfSSL | 15:117db924cf7c | 500 | |
wolfSSL | 15:117db924cf7c | 501 | |
wolfSSL | 15:117db924cf7c | 502 | #if !defined(NO_DSA) || defined(HAVE_ECC) || \ |
wolfSSL | 15:117db924cf7c | 503 | (!defined(NO_RSA) && \ |
wolfSSL | 15:117db924cf7c | 504 | (defined(WOLFSSL_CERT_GEN) || \ |
wolfSSL | 15:117db924cf7c | 505 | ((defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)) && !defined(HAVE_USER_RSA)))) |
wolfSSL | 15:117db924cf7c | 506 | /* Set the DER/BER encoding of the ASN.1 INTEGER header. |
wolfSSL | 15:117db924cf7c | 507 | * |
wolfSSL | 15:117db924cf7c | 508 | * len Length of data to encode. |
wolfSSL | 15:117db924cf7c | 509 | * firstByte First byte of data, most significant byte of integer, to encode. |
wolfSSL | 15:117db924cf7c | 510 | * output Buffer to write into. |
wolfSSL | 15:117db924cf7c | 511 | * returns the number of bytes added to the buffer. |
wolfSSL | 15:117db924cf7c | 512 | */ |
wolfSSL | 15:117db924cf7c | 513 | static int SetASNInt(int len, byte firstByte, byte* output) |
wolfSSL | 15:117db924cf7c | 514 | { |
wolfSSL | 15:117db924cf7c | 515 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 516 | |
wolfSSL | 15:117db924cf7c | 517 | output[idx++] = ASN_INTEGER; |
wolfSSL | 15:117db924cf7c | 518 | if (firstByte & 0x80) |
wolfSSL | 15:117db924cf7c | 519 | len++; |
wolfSSL | 15:117db924cf7c | 520 | idx += SetLength(len, output + idx); |
wolfSSL | 15:117db924cf7c | 521 | if (firstByte & 0x80) |
wolfSSL | 15:117db924cf7c | 522 | output[idx++] = 0x00; |
wolfSSL | 15:117db924cf7c | 523 | |
wolfSSL | 15:117db924cf7c | 524 | return idx; |
wolfSSL | 15:117db924cf7c | 525 | } |
wolfSSL | 15:117db924cf7c | 526 | #endif |
wolfSSL | 15:117db924cf7c | 527 | |
wolfSSL | 15:117db924cf7c | 528 | #if !defined(NO_DSA) || defined(HAVE_ECC) || defined(WOLFSSL_CERT_GEN) || \ |
wolfSSL | 15:117db924cf7c | 529 | ((defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)) |
wolfSSL | 15:117db924cf7c | 530 | /* Set the DER/BER encoding of the ASN.1 INTEGER element with an mp_int. |
wolfSSL | 15:117db924cf7c | 531 | * The number is assumed to be positive. |
wolfSSL | 15:117db924cf7c | 532 | * |
wolfSSL | 15:117db924cf7c | 533 | * n Multi-precision integer to encode. |
wolfSSL | 15:117db924cf7c | 534 | * maxSz Maximum size of the encoded integer. |
wolfSSL | 15:117db924cf7c | 535 | * A negative value indicates no check of length requested. |
wolfSSL | 15:117db924cf7c | 536 | * output Buffer to write into. |
wolfSSL | 15:117db924cf7c | 537 | * returns BUFFER_E when the data is too long for the buffer. |
wolfSSL | 15:117db924cf7c | 538 | * MP_TO_E when encoding the integer fails. |
wolfSSL | 15:117db924cf7c | 539 | * Otherwise, the number of bytes added to the buffer. |
wolfSSL | 15:117db924cf7c | 540 | */ |
wolfSSL | 15:117db924cf7c | 541 | static int SetASNIntMP(mp_int* n, int maxSz, byte* output) |
wolfSSL | 15:117db924cf7c | 542 | { |
wolfSSL | 15:117db924cf7c | 543 | int idx = 0; |
wolfSSL | 15:117db924cf7c | 544 | int leadingBit; |
wolfSSL | 15:117db924cf7c | 545 | int length; |
wolfSSL | 15:117db924cf7c | 546 | int err; |
wolfSSL | 15:117db924cf7c | 547 | |
wolfSSL | 15:117db924cf7c | 548 | leadingBit = mp_leading_bit(n); |
wolfSSL | 15:117db924cf7c | 549 | length = mp_unsigned_bin_size(n); |
wolfSSL | 15:117db924cf7c | 550 | idx = SetASNInt(length, leadingBit ? 0x80 : 0x00, output); |
wolfSSL | 15:117db924cf7c | 551 | if (maxSz >= 0 && (idx + length) > maxSz) |
wolfSSL | 15:117db924cf7c | 552 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 553 | |
wolfSSL | 15:117db924cf7c | 554 | err = mp_to_unsigned_bin(n, output + idx); |
wolfSSL | 15:117db924cf7c | 555 | if (err != MP_OKAY) |
wolfSSL | 15:117db924cf7c | 556 | return MP_TO_E; |
wolfSSL | 15:117db924cf7c | 557 | idx += length; |
wolfSSL | 15:117db924cf7c | 558 | |
wolfSSL | 15:117db924cf7c | 559 | return idx; |
wolfSSL | 15:117db924cf7c | 560 | } |
wolfSSL | 15:117db924cf7c | 561 | #endif |
wolfSSL | 15:117db924cf7c | 562 | |
wolfSSL | 15:117db924cf7c | 563 | #if !defined(NO_RSA) && defined(HAVE_USER_RSA) && defined(WOLFSSL_CERT_GEN) |
wolfSSL | 15:117db924cf7c | 564 | /* Set the DER/BER encoding of the ASN.1 INTEGER element with an mp_int from |
wolfSSL | 15:117db924cf7c | 565 | * an RSA key. |
wolfSSL | 15:117db924cf7c | 566 | * The number is assumed to be positive. |
wolfSSL | 15:117db924cf7c | 567 | * |
wolfSSL | 15:117db924cf7c | 568 | * n Multi-precision integer to encode. |
wolfSSL | 15:117db924cf7c | 569 | * output Buffer to write into. |
wolfSSL | 15:117db924cf7c | 570 | * returns BUFFER_E when the data is too long for the buffer. |
wolfSSL | 15:117db924cf7c | 571 | * MP_TO_E when encoding the integer fails. |
wolfSSL | 15:117db924cf7c | 572 | * Otherwise, the number of bytes added to the buffer. |
wolfSSL | 15:117db924cf7c | 573 | */ |
wolfSSL | 15:117db924cf7c | 574 | static int SetASNIntRSA(mp_int* n, byte* output) |
wolfSSL | 15:117db924cf7c | 575 | { |
wolfSSL | 15:117db924cf7c | 576 | int idx = 0; |
wolfSSL | 15:117db924cf7c | 577 | int leadingBit; |
wolfSSL | 15:117db924cf7c | 578 | int length; |
wolfSSL | 15:117db924cf7c | 579 | int err; |
wolfSSL | 15:117db924cf7c | 580 | |
wolfSSL | 15:117db924cf7c | 581 | leadingBit = wc_Rsa_leading_bit(n); |
wolfSSL | 15:117db924cf7c | 582 | length = wc_Rsa_unsigned_bin_size(n); |
wolfSSL | 15:117db924cf7c | 583 | idx = SetASNInt(length, leadingBit ? 0x80 : 0x00, output); |
wolfSSL | 15:117db924cf7c | 584 | if ((idx + length) > MAX_RSA_INT_SZ) |
wolfSSL | 15:117db924cf7c | 585 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 586 | |
wolfSSL | 15:117db924cf7c | 587 | err = wc_Rsa_to_unsigned_bin(n, output + idx, length); |
wolfSSL | 15:117db924cf7c | 588 | if (err != MP_OKAY) |
wolfSSL | 15:117db924cf7c | 589 | return MP_TO_E; |
wolfSSL | 15:117db924cf7c | 590 | idx += length; |
wolfSSL | 15:117db924cf7c | 591 | |
wolfSSL | 15:117db924cf7c | 592 | return idx; |
wolfSSL | 15:117db924cf7c | 593 | } |
wolfSSL | 15:117db924cf7c | 594 | #endif /* !NO_RSA && HAVE_USER_RSA && WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 595 | |
wolfSSL | 15:117db924cf7c | 596 | /* Windows header clash for WinCE using GetVersion */ |
wolfSSL | 15:117db924cf7c | 597 | WOLFSSL_LOCAL int GetMyVersion(const byte* input, word32* inOutIdx, |
wolfSSL | 15:117db924cf7c | 598 | int* version, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 599 | { |
wolfSSL | 15:117db924cf7c | 600 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 601 | |
wolfSSL | 15:117db924cf7c | 602 | if ((idx + MIN_VERSION_SZ) > maxIdx) |
wolfSSL | 15:117db924cf7c | 603 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 604 | |
wolfSSL | 15:117db924cf7c | 605 | if (input[idx++] != ASN_INTEGER) |
wolfSSL | 15:117db924cf7c | 606 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 607 | |
wolfSSL | 15:117db924cf7c | 608 | if (input[idx++] != 0x01) |
wolfSSL | 15:117db924cf7c | 609 | return ASN_VERSION_E; |
wolfSSL | 15:117db924cf7c | 610 | |
wolfSSL | 15:117db924cf7c | 611 | *version = input[idx++]; |
wolfSSL | 15:117db924cf7c | 612 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 613 | |
wolfSSL | 15:117db924cf7c | 614 | return *version; |
wolfSSL | 15:117db924cf7c | 615 | } |
wolfSSL | 15:117db924cf7c | 616 | |
wolfSSL | 15:117db924cf7c | 617 | |
wolfSSL | 15:117db924cf7c | 618 | #ifndef NO_PWDBASED |
wolfSSL | 15:117db924cf7c | 619 | /* Get small count integer, 32 bits or less */ |
wolfSSL | 15:117db924cf7c | 620 | int GetShortInt(const byte* input, word32* inOutIdx, int* number, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 621 | { |
wolfSSL | 15:117db924cf7c | 622 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 623 | word32 len; |
wolfSSL | 15:117db924cf7c | 624 | |
wolfSSL | 15:117db924cf7c | 625 | *number = 0; |
wolfSSL | 15:117db924cf7c | 626 | |
wolfSSL | 15:117db924cf7c | 627 | /* check for type and length bytes */ |
wolfSSL | 15:117db924cf7c | 628 | if ((idx + 2) > maxIdx) |
wolfSSL | 15:117db924cf7c | 629 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 630 | |
wolfSSL | 15:117db924cf7c | 631 | if (input[idx++] != ASN_INTEGER) |
wolfSSL | 15:117db924cf7c | 632 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 633 | |
wolfSSL | 15:117db924cf7c | 634 | len = input[idx++]; |
wolfSSL | 15:117db924cf7c | 635 | if (len > 4) |
wolfSSL | 15:117db924cf7c | 636 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 637 | |
wolfSSL | 15:117db924cf7c | 638 | if (len + idx > maxIdx) |
wolfSSL | 15:117db924cf7c | 639 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 640 | |
wolfSSL | 15:117db924cf7c | 641 | while (len--) { |
wolfSSL | 15:117db924cf7c | 642 | *number = *number << 8 | input[idx++]; |
wolfSSL | 15:117db924cf7c | 643 | } |
wolfSSL | 15:117db924cf7c | 644 | |
wolfSSL | 15:117db924cf7c | 645 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 646 | |
wolfSSL | 15:117db924cf7c | 647 | return *number; |
wolfSSL | 15:117db924cf7c | 648 | } |
wolfSSL | 15:117db924cf7c | 649 | |
wolfSSL | 15:117db924cf7c | 650 | |
wolfSSL | 15:117db924cf7c | 651 | /* Set small integer, 32 bits or less. DER encoding with no leading 0s |
wolfSSL | 15:117db924cf7c | 652 | * returns total amount written including ASN tag and length byte on success */ |
wolfSSL | 15:117db924cf7c | 653 | static int SetShortInt(byte* input, word32* inOutIdx, word32 number, |
wolfSSL | 15:117db924cf7c | 654 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 655 | { |
wolfSSL | 15:117db924cf7c | 656 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 657 | word32 len = 0; |
wolfSSL | 15:117db924cf7c | 658 | int i; |
wolfSSL | 15:117db924cf7c | 659 | byte ar[MAX_LENGTH_SZ]; |
wolfSSL | 15:117db924cf7c | 660 | |
wolfSSL | 15:117db924cf7c | 661 | /* check for room for type and length bytes */ |
wolfSSL | 15:117db924cf7c | 662 | if ((idx + 2) > maxIdx) |
wolfSSL | 15:117db924cf7c | 663 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 664 | |
wolfSSL | 15:117db924cf7c | 665 | input[idx++] = ASN_INTEGER; |
wolfSSL | 15:117db924cf7c | 666 | idx++; /* place holder for length byte */ |
wolfSSL | 15:117db924cf7c | 667 | if (MAX_LENGTH_SZ + idx > maxIdx) |
wolfSSL | 15:117db924cf7c | 668 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 669 | |
wolfSSL | 15:117db924cf7c | 670 | /* find first non zero byte */ |
wolfSSL | 15:117db924cf7c | 671 | XMEMSET(ar, 0, MAX_LENGTH_SZ); |
wolfSSL | 15:117db924cf7c | 672 | c32toa(number, ar); |
wolfSSL | 15:117db924cf7c | 673 | for (i = 0; i < MAX_LENGTH_SZ; i++) { |
wolfSSL | 15:117db924cf7c | 674 | if (ar[i] != 0) { |
wolfSSL | 15:117db924cf7c | 675 | break; |
wolfSSL | 15:117db924cf7c | 676 | } |
wolfSSL | 15:117db924cf7c | 677 | } |
wolfSSL | 15:117db924cf7c | 678 | |
wolfSSL | 15:117db924cf7c | 679 | /* handle case of 0 */ |
wolfSSL | 15:117db924cf7c | 680 | if (i == MAX_LENGTH_SZ) { |
wolfSSL | 15:117db924cf7c | 681 | input[idx++] = 0; len++; |
wolfSSL | 15:117db924cf7c | 682 | } |
wolfSSL | 15:117db924cf7c | 683 | |
wolfSSL | 15:117db924cf7c | 684 | for (; i < MAX_LENGTH_SZ && idx < maxIdx; i++) { |
wolfSSL | 15:117db924cf7c | 685 | input[idx++] = ar[i]; len++; |
wolfSSL | 15:117db924cf7c | 686 | } |
wolfSSL | 15:117db924cf7c | 687 | |
wolfSSL | 15:117db924cf7c | 688 | /* jump back to beginning of input buffer using unaltered inOutIdx value |
wolfSSL | 15:117db924cf7c | 689 | * and set number of bytes for integer, then update the index value */ |
wolfSSL | 15:117db924cf7c | 690 | input[*inOutIdx + 1] = (byte)len; |
wolfSSL | 15:117db924cf7c | 691 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 692 | |
wolfSSL | 15:117db924cf7c | 693 | return len + 2; /* size of integer bytes plus ASN TAG and length byte */ |
wolfSSL | 15:117db924cf7c | 694 | } |
wolfSSL | 15:117db924cf7c | 695 | #endif /* !NO_PWDBASED */ |
wolfSSL | 15:117db924cf7c | 696 | |
wolfSSL | 15:117db924cf7c | 697 | /* May not have one, not an error */ |
wolfSSL | 15:117db924cf7c | 698 | static int GetExplicitVersion(const byte* input, word32* inOutIdx, int* version, |
wolfSSL | 15:117db924cf7c | 699 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 700 | { |
wolfSSL | 15:117db924cf7c | 701 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 702 | |
wolfSSL | 15:117db924cf7c | 703 | WOLFSSL_ENTER("GetExplicitVersion"); |
wolfSSL | 15:117db924cf7c | 704 | |
wolfSSL | 15:117db924cf7c | 705 | if ((idx + 1) > maxIdx) |
wolfSSL | 15:117db924cf7c | 706 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 707 | |
wolfSSL | 15:117db924cf7c | 708 | if (input[idx++] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) { |
wolfSSL | 15:117db924cf7c | 709 | *inOutIdx = ++idx; /* skip header */ |
wolfSSL | 15:117db924cf7c | 710 | return GetMyVersion(input, inOutIdx, version, maxIdx); |
wolfSSL | 15:117db924cf7c | 711 | } |
wolfSSL | 15:117db924cf7c | 712 | |
wolfSSL | 15:117db924cf7c | 713 | /* go back as is */ |
wolfSSL | 15:117db924cf7c | 714 | *version = 0; |
wolfSSL | 15:117db924cf7c | 715 | |
wolfSSL | 15:117db924cf7c | 716 | return 0; |
wolfSSL | 15:117db924cf7c | 717 | } |
wolfSSL | 15:117db924cf7c | 718 | |
wolfSSL | 15:117db924cf7c | 719 | int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 720 | { |
wolfSSL | 15:117db924cf7c | 721 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 722 | int ret; |
wolfSSL | 15:117db924cf7c | 723 | int length; |
wolfSSL | 15:117db924cf7c | 724 | |
wolfSSL | 15:117db924cf7c | 725 | ret = GetASNInt(input, &idx, &length, maxIdx); |
wolfSSL | 15:117db924cf7c | 726 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 727 | return ret; |
wolfSSL | 15:117db924cf7c | 728 | |
wolfSSL | 15:117db924cf7c | 729 | if (mp_init(mpi) != MP_OKAY) |
wolfSSL | 15:117db924cf7c | 730 | return MP_INIT_E; |
wolfSSL | 15:117db924cf7c | 731 | |
wolfSSL | 15:117db924cf7c | 732 | if (mp_read_unsigned_bin(mpi, (byte*)input + idx, length) != 0) { |
wolfSSL | 15:117db924cf7c | 733 | mp_clear(mpi); |
wolfSSL | 15:117db924cf7c | 734 | return ASN_GETINT_E; |
wolfSSL | 15:117db924cf7c | 735 | } |
wolfSSL | 15:117db924cf7c | 736 | |
wolfSSL | 15:117db924cf7c | 737 | #ifdef HAVE_WOLF_BIGINT |
wolfSSL | 15:117db924cf7c | 738 | if (wc_bigint_from_unsigned_bin(&mpi->raw, input + idx, length) != 0) { |
wolfSSL | 15:117db924cf7c | 739 | mp_clear(mpi); |
wolfSSL | 15:117db924cf7c | 740 | return ASN_GETINT_E; |
wolfSSL | 15:117db924cf7c | 741 | } |
wolfSSL | 15:117db924cf7c | 742 | #endif /* HAVE_WOLF_BIGINT */ |
wolfSSL | 15:117db924cf7c | 743 | |
wolfSSL | 15:117db924cf7c | 744 | *inOutIdx = idx + length; |
wolfSSL | 15:117db924cf7c | 745 | |
wolfSSL | 15:117db924cf7c | 746 | return 0; |
wolfSSL | 15:117db924cf7c | 747 | } |
wolfSSL | 15:117db924cf7c | 748 | |
wolfSSL | 15:117db924cf7c | 749 | #if !defined(WOLFSSL_KEY_GEN) && !defined(OPENSSL_EXTRA) && defined(RSA_LOW_MEM) |
wolfSSL | 15:117db924cf7c | 750 | #if !defined(NO_RSA) && !defined(HAVE_USER_RSA) |
wolfSSL | 15:117db924cf7c | 751 | static int SkipInt(const byte* input, word32* inOutIdx, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 752 | { |
wolfSSL | 15:117db924cf7c | 753 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 754 | int ret; |
wolfSSL | 15:117db924cf7c | 755 | int length; |
wolfSSL | 15:117db924cf7c | 756 | |
wolfSSL | 15:117db924cf7c | 757 | ret = GetASNInt(input, &idx, &length, maxIdx); |
wolfSSL | 15:117db924cf7c | 758 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 759 | return ret; |
wolfSSL | 15:117db924cf7c | 760 | |
wolfSSL | 15:117db924cf7c | 761 | *inOutIdx = idx + length; |
wolfSSL | 15:117db924cf7c | 762 | |
wolfSSL | 15:117db924cf7c | 763 | return 0; |
wolfSSL | 15:117db924cf7c | 764 | } |
wolfSSL | 15:117db924cf7c | 765 | #endif |
wolfSSL | 15:117db924cf7c | 766 | #endif |
wolfSSL | 15:117db924cf7c | 767 | |
wolfSSL | 15:117db924cf7c | 768 | static int CheckBitString(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 15:117db924cf7c | 769 | word32 maxIdx, int zeroBits, byte* unusedBits) |
wolfSSL | 15:117db924cf7c | 770 | { |
wolfSSL | 15:117db924cf7c | 771 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 772 | int length; |
wolfSSL | 15:117db924cf7c | 773 | byte b; |
wolfSSL | 15:117db924cf7c | 774 | |
wolfSSL | 15:117db924cf7c | 775 | if ((idx + 1) > maxIdx) |
wolfSSL | 15:117db924cf7c | 776 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 777 | |
wolfSSL | 15:117db924cf7c | 778 | if (input[idx++] != ASN_BIT_STRING) |
wolfSSL | 15:117db924cf7c | 779 | return ASN_BITSTR_E; |
wolfSSL | 15:117db924cf7c | 780 | |
wolfSSL | 15:117db924cf7c | 781 | if (GetLength(input, &idx, &length, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 782 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 783 | |
wolfSSL | 15:117db924cf7c | 784 | /* extra sanity check that length is greater than 0 */ |
wolfSSL | 15:117db924cf7c | 785 | if (length <= 0) { |
wolfSSL | 15:117db924cf7c | 786 | WOLFSSL_MSG("Error length was 0 in CheckBitString"); |
wolfSSL | 15:117db924cf7c | 787 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 788 | } |
wolfSSL | 15:117db924cf7c | 789 | |
wolfSSL | 15:117db924cf7c | 790 | if (idx + 1 > maxIdx) { |
wolfSSL | 15:117db924cf7c | 791 | WOLFSSL_MSG("Attempted buffer read larger than input buffer"); |
wolfSSL | 15:117db924cf7c | 792 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 793 | } |
wolfSSL | 15:117db924cf7c | 794 | |
wolfSSL | 15:117db924cf7c | 795 | b = input[idx]; |
wolfSSL | 15:117db924cf7c | 796 | if (zeroBits && b != 0x00) |
wolfSSL | 15:117db924cf7c | 797 | return ASN_EXPECT_0_E; |
wolfSSL | 15:117db924cf7c | 798 | if (b >= 0x08) |
wolfSSL | 15:117db924cf7c | 799 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 800 | if (b != 0) { |
wolfSSL | 15:117db924cf7c | 801 | if ((byte)(input[idx + length - 1] << (8 - b)) != 0) |
wolfSSL | 15:117db924cf7c | 802 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 803 | } |
wolfSSL | 15:117db924cf7c | 804 | idx++; |
wolfSSL | 15:117db924cf7c | 805 | length--; /* length has been checked for greater than 0 */ |
wolfSSL | 15:117db924cf7c | 806 | |
wolfSSL | 15:117db924cf7c | 807 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 808 | if (len != NULL) |
wolfSSL | 15:117db924cf7c | 809 | *len = length; |
wolfSSL | 15:117db924cf7c | 810 | if (unusedBits != NULL) |
wolfSSL | 15:117db924cf7c | 811 | *unusedBits = b; |
wolfSSL | 15:117db924cf7c | 812 | |
wolfSSL | 15:117db924cf7c | 813 | return 0; |
wolfSSL | 15:117db924cf7c | 814 | } |
wolfSSL | 15:117db924cf7c | 815 | |
wolfSSL | 15:117db924cf7c | 816 | /* RSA (with CertGen or KeyGen) OR ECC OR ED25519 (with CertGen or KeyGen) */ |
wolfSSL | 15:117db924cf7c | 817 | #if (!defined(NO_RSA) && !defined(HAVE_USER_RSA) && \ |
wolfSSL | 15:117db924cf7c | 818 | (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA))) || \ |
wolfSSL | 15:117db924cf7c | 819 | defined(HAVE_ECC) || \ |
wolfSSL | 15:117db924cf7c | 820 | (defined(HAVE_ED25519) && \ |
wolfSSL | 15:117db924cf7c | 821 | (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA))) |
wolfSSL | 15:117db924cf7c | 822 | |
wolfSSL | 15:117db924cf7c | 823 | /* Set the DER/BER encoding of the ASN.1 BIT_STRING header. |
wolfSSL | 15:117db924cf7c | 824 | * |
wolfSSL | 15:117db924cf7c | 825 | * len Length of data to encode. |
wolfSSL | 15:117db924cf7c | 826 | * unusedBits The number of unused bits in the last byte of data. |
wolfSSL | 15:117db924cf7c | 827 | * That is, the number of least significant zero bits before a one. |
wolfSSL | 15:117db924cf7c | 828 | * The last byte is the most-significant non-zero byte of a number. |
wolfSSL | 15:117db924cf7c | 829 | * output Buffer to write into. |
wolfSSL | 15:117db924cf7c | 830 | * returns the number of bytes added to the buffer. |
wolfSSL | 15:117db924cf7c | 831 | */ |
wolfSSL | 15:117db924cf7c | 832 | static word32 SetBitString(word32 len, byte unusedBits, byte* output) |
wolfSSL | 15:117db924cf7c | 833 | { |
wolfSSL | 15:117db924cf7c | 834 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 835 | |
wolfSSL | 15:117db924cf7c | 836 | output[idx++] = ASN_BIT_STRING; |
wolfSSL | 15:117db924cf7c | 837 | idx += SetLength(len + 1, output + idx); |
wolfSSL | 15:117db924cf7c | 838 | output[idx++] = unusedBits; |
wolfSSL | 15:117db924cf7c | 839 | |
wolfSSL | 15:117db924cf7c | 840 | return idx; |
wolfSSL | 15:117db924cf7c | 841 | } |
wolfSSL | 15:117db924cf7c | 842 | #endif /* !NO_RSA || HAVE_ECC || HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 843 | |
wolfSSL | 15:117db924cf7c | 844 | #ifdef ASN_BER_TO_DER |
wolfSSL | 15:117db924cf7c | 845 | /* Convert a BER encoding with indefinite length items to DER. |
wolfSSL | 15:117db924cf7c | 846 | * |
wolfSSL | 15:117db924cf7c | 847 | * ber BER encoded data. |
wolfSSL | 15:117db924cf7c | 848 | * berSz Length of BER encoded data. |
wolfSSL | 15:117db924cf7c | 849 | * der Buffer to hold DER encoded version of data. |
wolfSSL | 15:117db924cf7c | 850 | * NULL indicates only the length is required. |
wolfSSL | 15:117db924cf7c | 851 | * derSz The size of the buffer to hold the DER encoded data. |
wolfSSL | 15:117db924cf7c | 852 | * Will be set if der is NULL, otherwise the value is checked as der is |
wolfSSL | 15:117db924cf7c | 853 | * filled. |
wolfSSL | 15:117db924cf7c | 854 | * returns ASN_PARSE_E if the BER data is invalid and BAD_FUNC_ARG if ber or |
wolfSSL | 15:117db924cf7c | 855 | * derSz are NULL. |
wolfSSL | 15:117db924cf7c | 856 | */ |
wolfSSL | 15:117db924cf7c | 857 | int wc_BerToDer(const byte* ber, word32 berSz, byte* der, word32* derSz) |
wolfSSL | 15:117db924cf7c | 858 | { |
wolfSSL | 15:117db924cf7c | 859 | int ret; |
wolfSSL | 15:117db924cf7c | 860 | word32 i, j, k; |
wolfSSL | 15:117db924cf7c | 861 | int len, l; |
wolfSSL | 15:117db924cf7c | 862 | int indef; |
wolfSSL | 15:117db924cf7c | 863 | int depth = 0; |
wolfSSL | 15:117db924cf7c | 864 | byte type; |
wolfSSL | 15:117db924cf7c | 865 | word32 cnt, sz; |
wolfSSL | 15:117db924cf7c | 866 | word32 outSz; |
wolfSSL | 15:117db924cf7c | 867 | byte lenBytes[4]; |
wolfSSL | 15:117db924cf7c | 868 | |
wolfSSL | 15:117db924cf7c | 869 | if (ber == NULL || derSz == NULL) |
wolfSSL | 15:117db924cf7c | 870 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 871 | |
wolfSSL | 15:117db924cf7c | 872 | outSz = *derSz; |
wolfSSL | 15:117db924cf7c | 873 | |
wolfSSL | 15:117db924cf7c | 874 | for (i = 0, j = 0; i < berSz; ) { |
wolfSSL | 15:117db924cf7c | 875 | /* Check that there is data for an ASN item to parse. */ |
wolfSSL | 15:117db924cf7c | 876 | if (i + 2 > berSz) |
wolfSSL | 15:117db924cf7c | 877 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 878 | |
wolfSSL | 15:117db924cf7c | 879 | /* End Of Content (EOC) mark end of indefinite length items. |
wolfSSL | 15:117db924cf7c | 880 | * EOCs are not encoded in DER. |
wolfSSL | 15:117db924cf7c | 881 | * Keep track of no. indefinite length items that have not been |
wolfSSL | 15:117db924cf7c | 882 | * terminated in depth. |
wolfSSL | 15:117db924cf7c | 883 | */ |
wolfSSL | 15:117db924cf7c | 884 | if (ber[i] == 0 && ber[i+1] == 0) { |
wolfSSL | 15:117db924cf7c | 885 | if (depth == 0) |
wolfSSL | 15:117db924cf7c | 886 | break; |
wolfSSL | 15:117db924cf7c | 887 | if (--depth == 0) |
wolfSSL | 15:117db924cf7c | 888 | break; |
wolfSSL | 15:117db924cf7c | 889 | |
wolfSSL | 15:117db924cf7c | 890 | i += 2; |
wolfSSL | 15:117db924cf7c | 891 | continue; |
wolfSSL | 15:117db924cf7c | 892 | } |
wolfSSL | 15:117db924cf7c | 893 | |
wolfSSL | 15:117db924cf7c | 894 | /* Indefinite length is encoded as: 0x80 */ |
wolfSSL | 15:117db924cf7c | 895 | type = ber[i]; |
wolfSSL | 15:117db924cf7c | 896 | indef = ber[i+1] == ASN_INDEF_LENGTH; |
wolfSSL | 15:117db924cf7c | 897 | if (indef && (type & 0xC0) == 0 && |
wolfSSL | 15:117db924cf7c | 898 | ber[i] != (ASN_SEQUENCE | ASN_CONSTRUCTED) && |
wolfSSL | 15:117db924cf7c | 899 | ber[i] != (ASN_SET | ASN_CONSTRUCTED)) { |
wolfSSL | 15:117db924cf7c | 900 | /* Indefinite length OCTET STRING or other simple type. |
wolfSSL | 15:117db924cf7c | 901 | * Put all the data into one entry. |
wolfSSL | 15:117db924cf7c | 902 | */ |
wolfSSL | 15:117db924cf7c | 903 | |
wolfSSL | 15:117db924cf7c | 904 | /* Type no longer constructed. */ |
wolfSSL | 15:117db924cf7c | 905 | type &= ~ASN_CONSTRUCTED; |
wolfSSL | 15:117db924cf7c | 906 | if (der != NULL) { |
wolfSSL | 15:117db924cf7c | 907 | /* Ensure space for type. */ |
wolfSSL | 15:117db924cf7c | 908 | if (j + 1 >= outSz) |
wolfSSL | 15:117db924cf7c | 909 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 910 | der[j] = type; |
wolfSSL | 15:117db924cf7c | 911 | } |
wolfSSL | 15:117db924cf7c | 912 | i++; j++; |
wolfSSL | 15:117db924cf7c | 913 | /* Skip indefinite length. */ |
wolfSSL | 15:117db924cf7c | 914 | i++; |
wolfSSL | 15:117db924cf7c | 915 | |
wolfSSL | 15:117db924cf7c | 916 | /* There must be further ASN1 items to combine. */ |
wolfSSL | 15:117db924cf7c | 917 | if (i + 2 > berSz) |
wolfSSL | 15:117db924cf7c | 918 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 919 | |
wolfSSL | 15:117db924cf7c | 920 | /* Calculate length of combined data. */ |
wolfSSL | 15:117db924cf7c | 921 | len = 0; |
wolfSSL | 15:117db924cf7c | 922 | k = i; |
wolfSSL | 15:117db924cf7c | 923 | while (ber[k] != 0x00) { |
wolfSSL | 15:117db924cf7c | 924 | /* Each ASN item must be the same type as the constructed. */ |
wolfSSL | 15:117db924cf7c | 925 | if (ber[k] != type) |
wolfSSL | 15:117db924cf7c | 926 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 927 | k++; |
wolfSSL | 15:117db924cf7c | 928 | |
wolfSSL | 15:117db924cf7c | 929 | ret = GetLength(ber, &k, &l, berSz); |
wolfSSL | 15:117db924cf7c | 930 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 931 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 932 | k += l; |
wolfSSL | 15:117db924cf7c | 933 | len += l; |
wolfSSL | 15:117db924cf7c | 934 | |
wolfSSL | 15:117db924cf7c | 935 | /* Must at least have terminating EOC. */ |
wolfSSL | 15:117db924cf7c | 936 | if (k + 2 > berSz) |
wolfSSL | 15:117db924cf7c | 937 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 938 | } |
wolfSSL | 15:117db924cf7c | 939 | /* Ensure a valid EOC ASN item. */ |
wolfSSL | 15:117db924cf7c | 940 | if (ber[k+1] != 0x00) |
wolfSSL | 15:117db924cf7c | 941 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 942 | |
wolfSSL | 15:117db924cf7c | 943 | if (der == NULL) { |
wolfSSL | 15:117db924cf7c | 944 | /* Add length of ASN item length encoding and data. */ |
wolfSSL | 15:117db924cf7c | 945 | j += SetLength(len, lenBytes); |
wolfSSL | 15:117db924cf7c | 946 | j += len; |
wolfSSL | 15:117db924cf7c | 947 | } |
wolfSSL | 15:117db924cf7c | 948 | else { |
wolfSSL | 15:117db924cf7c | 949 | /* Check space for encoded length. */ |
wolfSSL | 15:117db924cf7c | 950 | if (SetLength(len, lenBytes) > outSz - j) |
wolfSSL | 15:117db924cf7c | 951 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 952 | /* Encode new length. */ |
wolfSSL | 15:117db924cf7c | 953 | j += SetLength(len, der + j); |
wolfSSL | 15:117db924cf7c | 954 | |
wolfSSL | 15:117db924cf7c | 955 | /* Encode data in single item. */ |
wolfSSL | 15:117db924cf7c | 956 | k = i; |
wolfSSL | 15:117db924cf7c | 957 | while (ber[k] != 0x00) { |
wolfSSL | 15:117db924cf7c | 958 | /* Skip ASN type. */ |
wolfSSL | 15:117db924cf7c | 959 | k++; |
wolfSSL | 15:117db924cf7c | 960 | |
wolfSSL | 15:117db924cf7c | 961 | /* Find length of data in ASN item. */ |
wolfSSL | 15:117db924cf7c | 962 | ret = GetLength(ber, &k, &l, berSz); |
wolfSSL | 15:117db924cf7c | 963 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 964 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 965 | |
wolfSSL | 15:117db924cf7c | 966 | /* Ensure space for data and copy in. */ |
wolfSSL | 15:117db924cf7c | 967 | if (j + l > outSz) |
wolfSSL | 15:117db924cf7c | 968 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 969 | XMEMCPY(der + j, ber + k, l); |
wolfSSL | 15:117db924cf7c | 970 | k += l; j += l; |
wolfSSL | 15:117db924cf7c | 971 | } |
wolfSSL | 15:117db924cf7c | 972 | } |
wolfSSL | 15:117db924cf7c | 973 | /* Continue conversion after EOC. */ |
wolfSSL | 15:117db924cf7c | 974 | i = k + 2; |
wolfSSL | 15:117db924cf7c | 975 | |
wolfSSL | 15:117db924cf7c | 976 | continue; |
wolfSSL | 15:117db924cf7c | 977 | } |
wolfSSL | 15:117db924cf7c | 978 | |
wolfSSL | 15:117db924cf7c | 979 | if (der != NULL) { |
wolfSSL | 15:117db924cf7c | 980 | /* Ensure space for type and at least one byte of length. */ |
wolfSSL | 15:117db924cf7c | 981 | if (j + 1 >= outSz) |
wolfSSL | 15:117db924cf7c | 982 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 983 | /* Put in type. */ |
wolfSSL | 15:117db924cf7c | 984 | der[j] = ber[i]; |
wolfSSL | 15:117db924cf7c | 985 | } |
wolfSSL | 15:117db924cf7c | 986 | i++; j++; |
wolfSSL | 15:117db924cf7c | 987 | |
wolfSSL | 15:117db924cf7c | 988 | if (indef) { |
wolfSSL | 15:117db924cf7c | 989 | /* Skip indefinite length. */ |
wolfSSL | 15:117db924cf7c | 990 | i++; |
wolfSSL | 15:117db924cf7c | 991 | /* Calculate the size of the data inside constructed. */ |
wolfSSL | 15:117db924cf7c | 992 | ret = wc_BerToDer(ber + i, berSz - i, NULL, &sz); |
wolfSSL | 15:117db924cf7c | 993 | if (ret != LENGTH_ONLY_E) |
wolfSSL | 15:117db924cf7c | 994 | return ret; |
wolfSSL | 15:117db924cf7c | 995 | |
wolfSSL | 15:117db924cf7c | 996 | if (der != NULL) { |
wolfSSL | 15:117db924cf7c | 997 | /* Ensure space for encoded length. */ |
wolfSSL | 15:117db924cf7c | 998 | if (SetLength(sz, lenBytes) > outSz - j) |
wolfSSL | 15:117db924cf7c | 999 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 1000 | /* Encode real length. */ |
wolfSSL | 15:117db924cf7c | 1001 | j += SetLength(sz, der + j); |
wolfSSL | 15:117db924cf7c | 1002 | } |
wolfSSL | 15:117db924cf7c | 1003 | else { |
wolfSSL | 15:117db924cf7c | 1004 | /* Add size of encoded length. */ |
wolfSSL | 15:117db924cf7c | 1005 | j += SetLength(sz, lenBytes); |
wolfSSL | 15:117db924cf7c | 1006 | } |
wolfSSL | 15:117db924cf7c | 1007 | |
wolfSSL | 15:117db924cf7c | 1008 | /* Another EOC to find. */ |
wolfSSL | 15:117db924cf7c | 1009 | depth++; |
wolfSSL | 15:117db924cf7c | 1010 | } |
wolfSSL | 15:117db924cf7c | 1011 | else { |
wolfSSL | 15:117db924cf7c | 1012 | /* Get the size of the encode length and length value. */ |
wolfSSL | 15:117db924cf7c | 1013 | cnt = i; |
wolfSSL | 15:117db924cf7c | 1014 | ret = GetLength(ber, &cnt, &len, berSz); |
wolfSSL | 15:117db924cf7c | 1015 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 1016 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 1017 | cnt -= i; |
wolfSSL | 15:117db924cf7c | 1018 | |
wolfSSL | 15:117db924cf7c | 1019 | /* Check there is enough data to copy out. */ |
wolfSSL | 15:117db924cf7c | 1020 | if (i + cnt + len > berSz) |
wolfSSL | 15:117db924cf7c | 1021 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 1022 | |
wolfSSL | 15:117db924cf7c | 1023 | if (der != NULL) { |
wolfSSL | 15:117db924cf7c | 1024 | /* Ensure space in DER buffer. */ |
wolfSSL | 15:117db924cf7c | 1025 | if (j + cnt + len > outSz) |
wolfSSL | 15:117db924cf7c | 1026 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 1027 | /* Copy length and data into DER buffer. */ |
wolfSSL | 15:117db924cf7c | 1028 | XMEMCPY(der + j, ber + i, cnt + len); |
wolfSSL | 15:117db924cf7c | 1029 | } |
wolfSSL | 15:117db924cf7c | 1030 | /* Continue conversion after this ASN item. */ |
wolfSSL | 15:117db924cf7c | 1031 | i += cnt + len; |
wolfSSL | 15:117db924cf7c | 1032 | j += cnt + len; |
wolfSSL | 15:117db924cf7c | 1033 | } |
wolfSSL | 15:117db924cf7c | 1034 | } |
wolfSSL | 15:117db924cf7c | 1035 | |
wolfSSL | 15:117db924cf7c | 1036 | if (depth >= 1) |
wolfSSL | 15:117db924cf7c | 1037 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 1038 | |
wolfSSL | 15:117db924cf7c | 1039 | /* Return length if no buffer to write to. */ |
wolfSSL | 15:117db924cf7c | 1040 | if (der == NULL) { |
wolfSSL | 15:117db924cf7c | 1041 | *derSz = j; |
wolfSSL | 15:117db924cf7c | 1042 | return LENGTH_ONLY_E; |
wolfSSL | 15:117db924cf7c | 1043 | } |
wolfSSL | 15:117db924cf7c | 1044 | |
wolfSSL | 15:117db924cf7c | 1045 | return 0; |
wolfSSL | 15:117db924cf7c | 1046 | } |
wolfSSL | 15:117db924cf7c | 1047 | #endif |
wolfSSL | 15:117db924cf7c | 1048 | |
wolfSSL | 15:117db924cf7c | 1049 | #if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN) |
wolfSSL | 15:117db924cf7c | 1050 | |
wolfSSL | 15:117db924cf7c | 1051 | #if (!defined(NO_RSA) && !defined(HAVE_USER_RSA)) || \ |
wolfSSL | 15:117db924cf7c | 1052 | defined(HAVE_ECC) || defined(HAVE_ED25519) |
wolfSSL | 15:117db924cf7c | 1053 | |
wolfSSL | 15:117db924cf7c | 1054 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 1055 | /* Set the DER/BER encoding of the ASN.1 BIT_STRING with a 16-bit value. |
wolfSSL | 15:117db924cf7c | 1056 | * |
wolfSSL | 15:117db924cf7c | 1057 | * val 16-bit value to encode. |
wolfSSL | 15:117db924cf7c | 1058 | * output Buffer to write into. |
wolfSSL | 15:117db924cf7c | 1059 | * returns the number of bytes added to the buffer. |
wolfSSL | 15:117db924cf7c | 1060 | */ |
wolfSSL | 15:117db924cf7c | 1061 | static word32 SetBitString16Bit(word16 val, byte* output) |
wolfSSL | 15:117db924cf7c | 1062 | { |
wolfSSL | 15:117db924cf7c | 1063 | word32 idx; |
wolfSSL | 15:117db924cf7c | 1064 | int len; |
wolfSSL | 15:117db924cf7c | 1065 | byte lastByte; |
wolfSSL | 15:117db924cf7c | 1066 | byte unusedBits = 0; |
wolfSSL | 15:117db924cf7c | 1067 | |
wolfSSL | 15:117db924cf7c | 1068 | if ((val >> 8) != 0) { |
wolfSSL | 15:117db924cf7c | 1069 | len = 2; |
wolfSSL | 15:117db924cf7c | 1070 | lastByte = (byte)(val >> 8); |
wolfSSL | 15:117db924cf7c | 1071 | } |
wolfSSL | 15:117db924cf7c | 1072 | else { |
wolfSSL | 15:117db924cf7c | 1073 | len = 1; |
wolfSSL | 15:117db924cf7c | 1074 | lastByte = (byte)val; |
wolfSSL | 15:117db924cf7c | 1075 | } |
wolfSSL | 15:117db924cf7c | 1076 | |
wolfSSL | 15:117db924cf7c | 1077 | while (((lastByte >> unusedBits) & 0x01) == 0x00) |
wolfSSL | 15:117db924cf7c | 1078 | unusedBits++; |
wolfSSL | 15:117db924cf7c | 1079 | |
wolfSSL | 15:117db924cf7c | 1080 | idx = SetBitString(len, unusedBits, output); |
wolfSSL | 15:117db924cf7c | 1081 | output[idx++] = (byte)val; |
wolfSSL | 15:117db924cf7c | 1082 | if (len > 1) |
wolfSSL | 15:117db924cf7c | 1083 | output[idx++] = (byte)(val >> 8); |
wolfSSL | 15:117db924cf7c | 1084 | |
wolfSSL | 15:117db924cf7c | 1085 | return idx; |
wolfSSL | 15:117db924cf7c | 1086 | } |
wolfSSL | 15:117db924cf7c | 1087 | #endif /* WOLFSSL_CERT_EXT */ |
wolfSSL | 15:117db924cf7c | 1088 | #endif /* !NO_RSA || HAVE_ECC || HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 1089 | #endif /* WOLFSSL_CERT_GEN || WOLFSSL_KEY_GEN */ |
wolfSSL | 15:117db924cf7c | 1090 | |
wolfSSL | 15:117db924cf7c | 1091 | |
wolfSSL | 15:117db924cf7c | 1092 | |
wolfSSL | 15:117db924cf7c | 1093 | /* hashType */ |
wolfSSL | 15:117db924cf7c | 1094 | #ifdef WOLFSSL_MD2 |
wolfSSL | 15:117db924cf7c | 1095 | static const byte hashMd2hOid[] = {42, 134, 72, 134, 247, 13, 2, 2}; |
wolfSSL | 15:117db924cf7c | 1096 | #endif |
wolfSSL | 15:117db924cf7c | 1097 | #ifndef NO_MD5 |
wolfSSL | 15:117db924cf7c | 1098 | static const byte hashMd5hOid[] = {42, 134, 72, 134, 247, 13, 2, 5}; |
wolfSSL | 15:117db924cf7c | 1099 | #endif |
wolfSSL | 15:117db924cf7c | 1100 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 1101 | static const byte hashSha1hOid[] = {43, 14, 3, 2, 26}; |
wolfSSL | 15:117db924cf7c | 1102 | #endif |
wolfSSL | 15:117db924cf7c | 1103 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1104 | static const byte hashSha224hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 4}; |
wolfSSL | 15:117db924cf7c | 1105 | #endif |
wolfSSL | 15:117db924cf7c | 1106 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1107 | static const byte hashSha256hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 1}; |
wolfSSL | 15:117db924cf7c | 1108 | #endif |
wolfSSL | 15:117db924cf7c | 1109 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1110 | static const byte hashSha384hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 2}; |
wolfSSL | 15:117db924cf7c | 1111 | #endif |
wolfSSL | 15:117db924cf7c | 1112 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1113 | static const byte hashSha512hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 3}; |
wolfSSL | 15:117db924cf7c | 1114 | #endif |
wolfSSL | 15:117db924cf7c | 1115 | |
wolfSSL | 15:117db924cf7c | 1116 | /* hmacType */ |
wolfSSL | 15:117db924cf7c | 1117 | #ifndef NO_HMAC |
wolfSSL | 15:117db924cf7c | 1118 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1119 | static const byte hmacSha224Oid[] = {42, 134, 72, 134, 247, 13, 2, 8}; |
wolfSSL | 15:117db924cf7c | 1120 | #endif |
wolfSSL | 15:117db924cf7c | 1121 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1122 | static const byte hmacSha256Oid[] = {42, 134, 72, 134, 247, 13, 2, 9}; |
wolfSSL | 15:117db924cf7c | 1123 | #endif |
wolfSSL | 15:117db924cf7c | 1124 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1125 | static const byte hmacSha384Oid[] = {42, 134, 72, 134, 247, 13, 2, 10}; |
wolfSSL | 15:117db924cf7c | 1126 | #endif |
wolfSSL | 15:117db924cf7c | 1127 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1128 | static const byte hmacSha512Oid[] = {42, 134, 72, 134, 247, 13, 2, 11}; |
wolfSSL | 15:117db924cf7c | 1129 | #endif |
wolfSSL | 15:117db924cf7c | 1130 | #endif |
wolfSSL | 15:117db924cf7c | 1131 | |
wolfSSL | 15:117db924cf7c | 1132 | /* sigType */ |
wolfSSL | 15:117db924cf7c | 1133 | #if !defined(NO_DSA) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 1134 | static const byte sigSha1wDsaOid[] = {42, 134, 72, 206, 56, 4, 3}; |
wolfSSL | 15:117db924cf7c | 1135 | #endif /* NO_DSA */ |
wolfSSL | 15:117db924cf7c | 1136 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 1137 | #ifdef WOLFSSL_MD2 |
wolfSSL | 15:117db924cf7c | 1138 | static const byte sigMd2wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 2}; |
wolfSSL | 15:117db924cf7c | 1139 | #endif |
wolfSSL | 15:117db924cf7c | 1140 | #ifndef NO_MD5 |
wolfSSL | 15:117db924cf7c | 1141 | static const byte sigMd5wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 4}; |
wolfSSL | 15:117db924cf7c | 1142 | #endif |
wolfSSL | 15:117db924cf7c | 1143 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 1144 | static const byte sigSha1wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 5}; |
wolfSSL | 15:117db924cf7c | 1145 | #endif |
wolfSSL | 15:117db924cf7c | 1146 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1147 | static const byte sigSha224wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,14}; |
wolfSSL | 15:117db924cf7c | 1148 | #endif |
wolfSSL | 15:117db924cf7c | 1149 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1150 | static const byte sigSha256wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,11}; |
wolfSSL | 15:117db924cf7c | 1151 | #endif |
wolfSSL | 15:117db924cf7c | 1152 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1153 | static const byte sigSha384wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,12}; |
wolfSSL | 15:117db924cf7c | 1154 | #endif |
wolfSSL | 15:117db924cf7c | 1155 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1156 | static const byte sigSha512wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,13}; |
wolfSSL | 15:117db924cf7c | 1157 | #endif |
wolfSSL | 15:117db924cf7c | 1158 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 1159 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 1160 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 1161 | static const byte sigSha1wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 1}; |
wolfSSL | 15:117db924cf7c | 1162 | #endif |
wolfSSL | 15:117db924cf7c | 1163 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1164 | static const byte sigSha224wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 1}; |
wolfSSL | 15:117db924cf7c | 1165 | #endif |
wolfSSL | 15:117db924cf7c | 1166 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1167 | static const byte sigSha256wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 2}; |
wolfSSL | 15:117db924cf7c | 1168 | #endif |
wolfSSL | 15:117db924cf7c | 1169 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1170 | static const byte sigSha384wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 3}; |
wolfSSL | 15:117db924cf7c | 1171 | #endif |
wolfSSL | 15:117db924cf7c | 1172 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1173 | static const byte sigSha512wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 4}; |
wolfSSL | 15:117db924cf7c | 1174 | #endif |
wolfSSL | 15:117db924cf7c | 1175 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 1176 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 1177 | static const byte sigEd25519Oid[] = {43, 101, 112}; |
wolfSSL | 15:117db924cf7c | 1178 | #endif /* HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 1179 | |
wolfSSL | 15:117db924cf7c | 1180 | /* keyType */ |
wolfSSL | 15:117db924cf7c | 1181 | #ifndef NO_DSA |
wolfSSL | 15:117db924cf7c | 1182 | static const byte keyDsaOid[] = {42, 134, 72, 206, 56, 4, 1}; |
wolfSSL | 15:117db924cf7c | 1183 | #endif /* NO_DSA */ |
wolfSSL | 15:117db924cf7c | 1184 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 1185 | static const byte keyRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 1}; |
wolfSSL | 15:117db924cf7c | 1186 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 1187 | #ifdef HAVE_NTRU |
wolfSSL | 15:117db924cf7c | 1188 | static const byte keyNtruOid[] = {43, 6, 1, 4, 1, 193, 22, 1, 1, 1, 1}; |
wolfSSL | 15:117db924cf7c | 1189 | #endif /* HAVE_NTRU */ |
wolfSSL | 15:117db924cf7c | 1190 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 1191 | static const byte keyEcdsaOid[] = {42, 134, 72, 206, 61, 2, 1}; |
wolfSSL | 15:117db924cf7c | 1192 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 1193 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 1194 | static const byte keyEd25519Oid[] = {43, 101, 112}; |
wolfSSL | 15:117db924cf7c | 1195 | #endif /* HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 1196 | |
wolfSSL | 15:117db924cf7c | 1197 | /* curveType */ |
wolfSSL | 15:117db924cf7c | 1198 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 1199 | /* See "ecc_sets" table in ecc.c */ |
wolfSSL | 15:117db924cf7c | 1200 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 1201 | |
wolfSSL | 15:117db924cf7c | 1202 | #ifdef HAVE_AES_CBC |
wolfSSL | 15:117db924cf7c | 1203 | /* blkType */ |
wolfSSL | 15:117db924cf7c | 1204 | #ifdef WOLFSSL_AES_128 |
wolfSSL | 15:117db924cf7c | 1205 | static const byte blkAes128CbcOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 2}; |
wolfSSL | 15:117db924cf7c | 1206 | #endif |
wolfSSL | 15:117db924cf7c | 1207 | #ifdef WOLFSSL_AES_192 |
wolfSSL | 15:117db924cf7c | 1208 | static const byte blkAes192CbcOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 22}; |
wolfSSL | 15:117db924cf7c | 1209 | #endif |
wolfSSL | 15:117db924cf7c | 1210 | #ifdef WOLFSSL_AES_256 |
wolfSSL | 15:117db924cf7c | 1211 | static const byte blkAes256CbcOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 42}; |
wolfSSL | 15:117db924cf7c | 1212 | #endif |
wolfSSL | 15:117db924cf7c | 1213 | #endif /* HAVE_AES_CBC */ |
wolfSSL | 15:117db924cf7c | 1214 | |
wolfSSL | 15:117db924cf7c | 1215 | #ifndef NO_DES3 |
wolfSSL | 15:117db924cf7c | 1216 | static const byte blkDesCbcOid[] = {43, 14, 3, 2, 7}; |
wolfSSL | 15:117db924cf7c | 1217 | static const byte blkDes3CbcOid[] = {42, 134, 72, 134, 247, 13, 3, 7}; |
wolfSSL | 15:117db924cf7c | 1218 | #endif |
wolfSSL | 15:117db924cf7c | 1219 | |
wolfSSL | 15:117db924cf7c | 1220 | /* keyWrapType */ |
wolfSSL | 15:117db924cf7c | 1221 | #ifdef WOLFSSL_AES_128 |
wolfSSL | 15:117db924cf7c | 1222 | static const byte wrapAes128Oid[] = {96, 134, 72, 1, 101, 3, 4, 1, 5}; |
wolfSSL | 15:117db924cf7c | 1223 | #endif |
wolfSSL | 15:117db924cf7c | 1224 | #ifdef WOLFSSL_AES_192 |
wolfSSL | 15:117db924cf7c | 1225 | static const byte wrapAes192Oid[] = {96, 134, 72, 1, 101, 3, 4, 1, 25}; |
wolfSSL | 15:117db924cf7c | 1226 | #endif |
wolfSSL | 15:117db924cf7c | 1227 | #ifdef WOLFSSL_AES_256 |
wolfSSL | 15:117db924cf7c | 1228 | static const byte wrapAes256Oid[] = {96, 134, 72, 1, 101, 3, 4, 1, 45}; |
wolfSSL | 15:117db924cf7c | 1229 | #endif |
wolfSSL | 15:117db924cf7c | 1230 | |
wolfSSL | 15:117db924cf7c | 1231 | /* cmsKeyAgreeType */ |
wolfSSL | 15:117db924cf7c | 1232 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 1233 | static const byte dhSinglePass_stdDH_sha1kdf_Oid[] = |
wolfSSL | 15:117db924cf7c | 1234 | {43, 129, 5, 16, 134, 72, 63, 0, 2}; |
wolfSSL | 15:117db924cf7c | 1235 | #endif |
wolfSSL | 15:117db924cf7c | 1236 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1237 | static const byte dhSinglePass_stdDH_sha224kdf_Oid[] = {43, 129, 4, 1, 11, 0}; |
wolfSSL | 15:117db924cf7c | 1238 | #endif |
wolfSSL | 15:117db924cf7c | 1239 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1240 | static const byte dhSinglePass_stdDH_sha256kdf_Oid[] = {43, 129, 4, 1, 11, 1}; |
wolfSSL | 15:117db924cf7c | 1241 | #endif |
wolfSSL | 15:117db924cf7c | 1242 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1243 | static const byte dhSinglePass_stdDH_sha384kdf_Oid[] = {43, 129, 4, 1, 11, 2}; |
wolfSSL | 15:117db924cf7c | 1244 | #endif |
wolfSSL | 15:117db924cf7c | 1245 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1246 | static const byte dhSinglePass_stdDH_sha512kdf_Oid[] = {43, 129, 4, 1, 11, 3}; |
wolfSSL | 15:117db924cf7c | 1247 | #endif |
wolfSSL | 15:117db924cf7c | 1248 | |
wolfSSL | 15:117db924cf7c | 1249 | /* ocspType */ |
wolfSSL | 15:117db924cf7c | 1250 | #ifdef HAVE_OCSP |
wolfSSL | 15:117db924cf7c | 1251 | static const byte ocspBasicOid[] = {43, 6, 1, 5, 5, 7, 48, 1, 1}; |
wolfSSL | 15:117db924cf7c | 1252 | static const byte ocspNonceOid[] = {43, 6, 1, 5, 5, 7, 48, 1, 2}; |
wolfSSL | 15:117db924cf7c | 1253 | #endif /* HAVE_OCSP */ |
wolfSSL | 15:117db924cf7c | 1254 | |
wolfSSL | 15:117db924cf7c | 1255 | /* certExtType */ |
wolfSSL | 15:117db924cf7c | 1256 | static const byte extBasicCaOid[] = {85, 29, 19}; |
wolfSSL | 15:117db924cf7c | 1257 | static const byte extAltNamesOid[] = {85, 29, 17}; |
wolfSSL | 15:117db924cf7c | 1258 | static const byte extCrlDistOid[] = {85, 29, 31}; |
wolfSSL | 15:117db924cf7c | 1259 | static const byte extAuthInfoOid[] = {43, 6, 1, 5, 5, 7, 1, 1}; |
wolfSSL | 15:117db924cf7c | 1260 | static const byte extAuthKeyOid[] = {85, 29, 35}; |
wolfSSL | 15:117db924cf7c | 1261 | static const byte extSubjKeyOid[] = {85, 29, 14}; |
wolfSSL | 15:117db924cf7c | 1262 | static const byte extCertPolicyOid[] = {85, 29, 32}; |
wolfSSL | 15:117db924cf7c | 1263 | static const byte extKeyUsageOid[] = {85, 29, 15}; |
wolfSSL | 15:117db924cf7c | 1264 | static const byte extInhibitAnyOid[] = {85, 29, 54}; |
wolfSSL | 15:117db924cf7c | 1265 | static const byte extExtKeyUsageOid[] = {85, 29, 37}; |
wolfSSL | 15:117db924cf7c | 1266 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 1267 | static const byte extNameConsOid[] = {85, 29, 30}; |
wolfSSL | 15:117db924cf7c | 1268 | #endif |
wolfSSL | 15:117db924cf7c | 1269 | |
wolfSSL | 15:117db924cf7c | 1270 | /* certAuthInfoType */ |
wolfSSL | 15:117db924cf7c | 1271 | #ifdef HAVE_OCSP |
wolfSSL | 15:117db924cf7c | 1272 | static const byte extAuthInfoOcspOid[] = {43, 6, 1, 5, 5, 7, 48, 1}; |
wolfSSL | 15:117db924cf7c | 1273 | #endif |
wolfSSL | 15:117db924cf7c | 1274 | static const byte extAuthInfoCaIssuerOid[] = {43, 6, 1, 5, 5, 7, 48, 2}; |
wolfSSL | 15:117db924cf7c | 1275 | |
wolfSSL | 15:117db924cf7c | 1276 | /* certPolicyType */ |
wolfSSL | 15:117db924cf7c | 1277 | static const byte extCertPolicyAnyOid[] = {85, 29, 32, 0}; |
wolfSSL | 15:117db924cf7c | 1278 | |
wolfSSL | 15:117db924cf7c | 1279 | /* certKeyUseType */ |
wolfSSL | 15:117db924cf7c | 1280 | static const byte extAltNamesHwNameOid[] = {43, 6, 1, 5, 5, 7, 8, 4}; |
wolfSSL | 15:117db924cf7c | 1281 | |
wolfSSL | 15:117db924cf7c | 1282 | /* certKeyUseType */ |
wolfSSL | 15:117db924cf7c | 1283 | static const byte extExtKeyUsageAnyOid[] = {85, 29, 37, 0}; |
wolfSSL | 15:117db924cf7c | 1284 | static const byte extExtKeyUsageServerAuthOid[] = {43, 6, 1, 5, 5, 7, 3, 1}; |
wolfSSL | 15:117db924cf7c | 1285 | static const byte extExtKeyUsageClientAuthOid[] = {43, 6, 1, 5, 5, 7, 3, 2}; |
wolfSSL | 15:117db924cf7c | 1286 | static const byte extExtKeyUsageCodeSigningOid[] = {43, 6, 1, 5, 5, 7, 3, 3}; |
wolfSSL | 15:117db924cf7c | 1287 | static const byte extExtKeyUsageEmailProtectOid[] = {43, 6, 1, 5, 5, 7, 3, 4}; |
wolfSSL | 15:117db924cf7c | 1288 | static const byte extExtKeyUsageTimestampOid[] = {43, 6, 1, 5, 5, 7, 3, 8}; |
wolfSSL | 15:117db924cf7c | 1289 | static const byte extExtKeyUsageOcspSignOid[] = {43, 6, 1, 5, 5, 7, 3, 9}; |
wolfSSL | 15:117db924cf7c | 1290 | |
wolfSSL | 15:117db924cf7c | 1291 | /* kdfType */ |
wolfSSL | 15:117db924cf7c | 1292 | static const byte pbkdf2Oid[] = {42, 134, 72, 134, 247, 13, 1, 5, 12}; |
wolfSSL | 15:117db924cf7c | 1293 | |
wolfSSL | 15:117db924cf7c | 1294 | /* PKCS5 */ |
wolfSSL | 15:117db924cf7c | 1295 | #if !defined(NO_DES3) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 1296 | static const byte pbeSha1Des[] = {42, 134, 72, 134, 247, 13, 1, 5, 10}; |
wolfSSL | 15:117db924cf7c | 1297 | #endif |
wolfSSL | 15:117db924cf7c | 1298 | |
wolfSSL | 15:117db924cf7c | 1299 | /* PKCS12 */ |
wolfSSL | 15:117db924cf7c | 1300 | #if !defined(NO_RC4) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 1301 | static const byte pbeSha1RC4128[] = {42, 134, 72, 134, 247, 13, 1, 12, 1, 1}; |
wolfSSL | 15:117db924cf7c | 1302 | #endif |
wolfSSL | 15:117db924cf7c | 1303 | #if !defined(NO_DES3) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 1304 | static const byte pbeSha1Des3[] = {42, 134, 72, 134, 247, 13, 1, 12, 1, 3}; |
wolfSSL | 15:117db924cf7c | 1305 | #endif |
wolfSSL | 15:117db924cf7c | 1306 | |
wolfSSL | 15:117db924cf7c | 1307 | |
wolfSSL | 15:117db924cf7c | 1308 | /* returns a pointer to the OID string on success and NULL on fail */ |
wolfSSL | 15:117db924cf7c | 1309 | const byte* OidFromId(word32 id, word32 type, word32* oidSz) |
wolfSSL | 15:117db924cf7c | 1310 | { |
wolfSSL | 15:117db924cf7c | 1311 | const byte* oid = NULL; |
wolfSSL | 15:117db924cf7c | 1312 | |
wolfSSL | 15:117db924cf7c | 1313 | *oidSz = 0; |
wolfSSL | 15:117db924cf7c | 1314 | |
wolfSSL | 15:117db924cf7c | 1315 | switch (type) { |
wolfSSL | 15:117db924cf7c | 1316 | |
wolfSSL | 15:117db924cf7c | 1317 | case oidHashType: |
wolfSSL | 15:117db924cf7c | 1318 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1319 | #ifdef WOLFSSL_MD2 |
wolfSSL | 15:117db924cf7c | 1320 | case MD2h: |
wolfSSL | 15:117db924cf7c | 1321 | oid = hashMd2hOid; |
wolfSSL | 15:117db924cf7c | 1322 | *oidSz = sizeof(hashMd2hOid); |
wolfSSL | 15:117db924cf7c | 1323 | break; |
wolfSSL | 15:117db924cf7c | 1324 | #endif |
wolfSSL | 15:117db924cf7c | 1325 | #ifndef NO_MD5 |
wolfSSL | 15:117db924cf7c | 1326 | case MD5h: |
wolfSSL | 15:117db924cf7c | 1327 | oid = hashMd5hOid; |
wolfSSL | 15:117db924cf7c | 1328 | *oidSz = sizeof(hashMd5hOid); |
wolfSSL | 15:117db924cf7c | 1329 | break; |
wolfSSL | 15:117db924cf7c | 1330 | #endif |
wolfSSL | 15:117db924cf7c | 1331 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 1332 | case SHAh: |
wolfSSL | 15:117db924cf7c | 1333 | oid = hashSha1hOid; |
wolfSSL | 15:117db924cf7c | 1334 | *oidSz = sizeof(hashSha1hOid); |
wolfSSL | 15:117db924cf7c | 1335 | break; |
wolfSSL | 15:117db924cf7c | 1336 | #endif |
wolfSSL | 15:117db924cf7c | 1337 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1338 | case SHA224h: |
wolfSSL | 15:117db924cf7c | 1339 | oid = hashSha224hOid; |
wolfSSL | 15:117db924cf7c | 1340 | *oidSz = sizeof(hashSha224hOid); |
wolfSSL | 15:117db924cf7c | 1341 | break; |
wolfSSL | 15:117db924cf7c | 1342 | #endif |
wolfSSL | 15:117db924cf7c | 1343 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1344 | case SHA256h: |
wolfSSL | 15:117db924cf7c | 1345 | oid = hashSha256hOid; |
wolfSSL | 15:117db924cf7c | 1346 | *oidSz = sizeof(hashSha256hOid); |
wolfSSL | 15:117db924cf7c | 1347 | break; |
wolfSSL | 15:117db924cf7c | 1348 | #endif |
wolfSSL | 15:117db924cf7c | 1349 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1350 | case SHA384h: |
wolfSSL | 15:117db924cf7c | 1351 | oid = hashSha384hOid; |
wolfSSL | 15:117db924cf7c | 1352 | *oidSz = sizeof(hashSha384hOid); |
wolfSSL | 15:117db924cf7c | 1353 | break; |
wolfSSL | 15:117db924cf7c | 1354 | #endif |
wolfSSL | 15:117db924cf7c | 1355 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1356 | case SHA512h: |
wolfSSL | 15:117db924cf7c | 1357 | oid = hashSha512hOid; |
wolfSSL | 15:117db924cf7c | 1358 | *oidSz = sizeof(hashSha512hOid); |
wolfSSL | 15:117db924cf7c | 1359 | break; |
wolfSSL | 15:117db924cf7c | 1360 | #endif |
wolfSSL | 15:117db924cf7c | 1361 | } |
wolfSSL | 15:117db924cf7c | 1362 | break; |
wolfSSL | 15:117db924cf7c | 1363 | |
wolfSSL | 15:117db924cf7c | 1364 | case oidSigType: |
wolfSSL | 15:117db924cf7c | 1365 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1366 | #if !defined(NO_DSA) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 1367 | case CTC_SHAwDSA: |
wolfSSL | 15:117db924cf7c | 1368 | oid = sigSha1wDsaOid; |
wolfSSL | 15:117db924cf7c | 1369 | *oidSz = sizeof(sigSha1wDsaOid); |
wolfSSL | 15:117db924cf7c | 1370 | break; |
wolfSSL | 15:117db924cf7c | 1371 | #endif /* NO_DSA */ |
wolfSSL | 15:117db924cf7c | 1372 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 1373 | #ifdef WOLFSSL_MD2 |
wolfSSL | 15:117db924cf7c | 1374 | case CTC_MD2wRSA: |
wolfSSL | 15:117db924cf7c | 1375 | oid = sigMd2wRsaOid; |
wolfSSL | 15:117db924cf7c | 1376 | *oidSz = sizeof(sigMd2wRsaOid); |
wolfSSL | 15:117db924cf7c | 1377 | break; |
wolfSSL | 15:117db924cf7c | 1378 | #endif |
wolfSSL | 15:117db924cf7c | 1379 | #ifndef NO_MD5 |
wolfSSL | 15:117db924cf7c | 1380 | case CTC_MD5wRSA: |
wolfSSL | 15:117db924cf7c | 1381 | oid = sigMd5wRsaOid; |
wolfSSL | 15:117db924cf7c | 1382 | *oidSz = sizeof(sigMd5wRsaOid); |
wolfSSL | 15:117db924cf7c | 1383 | break; |
wolfSSL | 15:117db924cf7c | 1384 | #endif |
wolfSSL | 15:117db924cf7c | 1385 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 1386 | case CTC_SHAwRSA: |
wolfSSL | 15:117db924cf7c | 1387 | oid = sigSha1wRsaOid; |
wolfSSL | 15:117db924cf7c | 1388 | *oidSz = sizeof(sigSha1wRsaOid); |
wolfSSL | 15:117db924cf7c | 1389 | break; |
wolfSSL | 15:117db924cf7c | 1390 | #endif |
wolfSSL | 15:117db924cf7c | 1391 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1392 | case CTC_SHA224wRSA: |
wolfSSL | 15:117db924cf7c | 1393 | oid = sigSha224wRsaOid; |
wolfSSL | 15:117db924cf7c | 1394 | *oidSz = sizeof(sigSha224wRsaOid); |
wolfSSL | 15:117db924cf7c | 1395 | break; |
wolfSSL | 15:117db924cf7c | 1396 | #endif |
wolfSSL | 15:117db924cf7c | 1397 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1398 | case CTC_SHA256wRSA: |
wolfSSL | 15:117db924cf7c | 1399 | oid = sigSha256wRsaOid; |
wolfSSL | 15:117db924cf7c | 1400 | *oidSz = sizeof(sigSha256wRsaOid); |
wolfSSL | 15:117db924cf7c | 1401 | break; |
wolfSSL | 15:117db924cf7c | 1402 | #endif |
wolfSSL | 15:117db924cf7c | 1403 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1404 | case CTC_SHA384wRSA: |
wolfSSL | 15:117db924cf7c | 1405 | oid = sigSha384wRsaOid; |
wolfSSL | 15:117db924cf7c | 1406 | *oidSz = sizeof(sigSha384wRsaOid); |
wolfSSL | 15:117db924cf7c | 1407 | break; |
wolfSSL | 15:117db924cf7c | 1408 | #endif |
wolfSSL | 15:117db924cf7c | 1409 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1410 | case CTC_SHA512wRSA: |
wolfSSL | 15:117db924cf7c | 1411 | oid = sigSha512wRsaOid; |
wolfSSL | 15:117db924cf7c | 1412 | *oidSz = sizeof(sigSha512wRsaOid); |
wolfSSL | 15:117db924cf7c | 1413 | break; |
wolfSSL | 15:117db924cf7c | 1414 | #endif /* WOLFSSL_SHA512 */ |
wolfSSL | 15:117db924cf7c | 1415 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 1416 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 1417 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 1418 | case CTC_SHAwECDSA: |
wolfSSL | 15:117db924cf7c | 1419 | oid = sigSha1wEcdsaOid; |
wolfSSL | 15:117db924cf7c | 1420 | *oidSz = sizeof(sigSha1wEcdsaOid); |
wolfSSL | 15:117db924cf7c | 1421 | break; |
wolfSSL | 15:117db924cf7c | 1422 | #endif |
wolfSSL | 15:117db924cf7c | 1423 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1424 | case CTC_SHA224wECDSA: |
wolfSSL | 15:117db924cf7c | 1425 | oid = sigSha224wEcdsaOid; |
wolfSSL | 15:117db924cf7c | 1426 | *oidSz = sizeof(sigSha224wEcdsaOid); |
wolfSSL | 15:117db924cf7c | 1427 | break; |
wolfSSL | 15:117db924cf7c | 1428 | #endif |
wolfSSL | 15:117db924cf7c | 1429 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1430 | case CTC_SHA256wECDSA: |
wolfSSL | 15:117db924cf7c | 1431 | oid = sigSha256wEcdsaOid; |
wolfSSL | 15:117db924cf7c | 1432 | *oidSz = sizeof(sigSha256wEcdsaOid); |
wolfSSL | 15:117db924cf7c | 1433 | break; |
wolfSSL | 15:117db924cf7c | 1434 | #endif |
wolfSSL | 15:117db924cf7c | 1435 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1436 | case CTC_SHA384wECDSA: |
wolfSSL | 15:117db924cf7c | 1437 | oid = sigSha384wEcdsaOid; |
wolfSSL | 15:117db924cf7c | 1438 | *oidSz = sizeof(sigSha384wEcdsaOid); |
wolfSSL | 15:117db924cf7c | 1439 | break; |
wolfSSL | 15:117db924cf7c | 1440 | #endif |
wolfSSL | 15:117db924cf7c | 1441 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1442 | case CTC_SHA512wECDSA: |
wolfSSL | 15:117db924cf7c | 1443 | oid = sigSha512wEcdsaOid; |
wolfSSL | 15:117db924cf7c | 1444 | *oidSz = sizeof(sigSha512wEcdsaOid); |
wolfSSL | 15:117db924cf7c | 1445 | break; |
wolfSSL | 15:117db924cf7c | 1446 | #endif |
wolfSSL | 15:117db924cf7c | 1447 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 1448 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 1449 | case CTC_ED25519: |
wolfSSL | 15:117db924cf7c | 1450 | oid = sigEd25519Oid; |
wolfSSL | 15:117db924cf7c | 1451 | *oidSz = sizeof(sigEd25519Oid); |
wolfSSL | 15:117db924cf7c | 1452 | break; |
wolfSSL | 15:117db924cf7c | 1453 | #endif |
wolfSSL | 15:117db924cf7c | 1454 | default: |
wolfSSL | 15:117db924cf7c | 1455 | break; |
wolfSSL | 15:117db924cf7c | 1456 | } |
wolfSSL | 15:117db924cf7c | 1457 | break; |
wolfSSL | 15:117db924cf7c | 1458 | |
wolfSSL | 15:117db924cf7c | 1459 | case oidKeyType: |
wolfSSL | 15:117db924cf7c | 1460 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1461 | #ifndef NO_DSA |
wolfSSL | 15:117db924cf7c | 1462 | case DSAk: |
wolfSSL | 15:117db924cf7c | 1463 | oid = keyDsaOid; |
wolfSSL | 15:117db924cf7c | 1464 | *oidSz = sizeof(keyDsaOid); |
wolfSSL | 15:117db924cf7c | 1465 | break; |
wolfSSL | 15:117db924cf7c | 1466 | #endif /* NO_DSA */ |
wolfSSL | 15:117db924cf7c | 1467 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 1468 | case RSAk: |
wolfSSL | 15:117db924cf7c | 1469 | oid = keyRsaOid; |
wolfSSL | 15:117db924cf7c | 1470 | *oidSz = sizeof(keyRsaOid); |
wolfSSL | 15:117db924cf7c | 1471 | break; |
wolfSSL | 15:117db924cf7c | 1472 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 1473 | #ifdef HAVE_NTRU |
wolfSSL | 15:117db924cf7c | 1474 | case NTRUk: |
wolfSSL | 15:117db924cf7c | 1475 | oid = keyNtruOid; |
wolfSSL | 15:117db924cf7c | 1476 | *oidSz = sizeof(keyNtruOid); |
wolfSSL | 15:117db924cf7c | 1477 | break; |
wolfSSL | 15:117db924cf7c | 1478 | #endif /* HAVE_NTRU */ |
wolfSSL | 15:117db924cf7c | 1479 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 1480 | case ECDSAk: |
wolfSSL | 15:117db924cf7c | 1481 | oid = keyEcdsaOid; |
wolfSSL | 15:117db924cf7c | 1482 | *oidSz = sizeof(keyEcdsaOid); |
wolfSSL | 15:117db924cf7c | 1483 | break; |
wolfSSL | 15:117db924cf7c | 1484 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 1485 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 1486 | case ED25519k: |
wolfSSL | 15:117db924cf7c | 1487 | oid = keyEd25519Oid; |
wolfSSL | 15:117db924cf7c | 1488 | *oidSz = sizeof(keyEd25519Oid); |
wolfSSL | 15:117db924cf7c | 1489 | break; |
wolfSSL | 15:117db924cf7c | 1490 | #endif /* HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 1491 | default: |
wolfSSL | 15:117db924cf7c | 1492 | break; |
wolfSSL | 15:117db924cf7c | 1493 | } |
wolfSSL | 15:117db924cf7c | 1494 | break; |
wolfSSL | 15:117db924cf7c | 1495 | |
wolfSSL | 15:117db924cf7c | 1496 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 1497 | case oidCurveType: |
wolfSSL | 15:117db924cf7c | 1498 | if (wc_ecc_get_oid(id, &oid, oidSz) < 0) { |
wolfSSL | 15:117db924cf7c | 1499 | WOLFSSL_MSG("ECC OID not found"); |
wolfSSL | 15:117db924cf7c | 1500 | } |
wolfSSL | 15:117db924cf7c | 1501 | break; |
wolfSSL | 15:117db924cf7c | 1502 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 1503 | |
wolfSSL | 15:117db924cf7c | 1504 | case oidBlkType: |
wolfSSL | 15:117db924cf7c | 1505 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1506 | #ifdef HAVE_AES_CBC |
wolfSSL | 15:117db924cf7c | 1507 | #ifdef WOLFSSL_AES_128 |
wolfSSL | 15:117db924cf7c | 1508 | case AES128CBCb: |
wolfSSL | 15:117db924cf7c | 1509 | oid = blkAes128CbcOid; |
wolfSSL | 15:117db924cf7c | 1510 | *oidSz = sizeof(blkAes128CbcOid); |
wolfSSL | 15:117db924cf7c | 1511 | break; |
wolfSSL | 15:117db924cf7c | 1512 | #endif |
wolfSSL | 15:117db924cf7c | 1513 | #ifdef WOLFSSL_AES_192 |
wolfSSL | 15:117db924cf7c | 1514 | case AES192CBCb: |
wolfSSL | 15:117db924cf7c | 1515 | oid = blkAes192CbcOid; |
wolfSSL | 15:117db924cf7c | 1516 | *oidSz = sizeof(blkAes192CbcOid); |
wolfSSL | 15:117db924cf7c | 1517 | break; |
wolfSSL | 15:117db924cf7c | 1518 | #endif |
wolfSSL | 15:117db924cf7c | 1519 | #ifdef WOLFSSL_AES_256 |
wolfSSL | 15:117db924cf7c | 1520 | case AES256CBCb: |
wolfSSL | 15:117db924cf7c | 1521 | oid = blkAes256CbcOid; |
wolfSSL | 15:117db924cf7c | 1522 | *oidSz = sizeof(blkAes256CbcOid); |
wolfSSL | 15:117db924cf7c | 1523 | break; |
wolfSSL | 15:117db924cf7c | 1524 | #endif |
wolfSSL | 15:117db924cf7c | 1525 | #endif /* HAVE_AES_CBC */ |
wolfSSL | 15:117db924cf7c | 1526 | #ifndef NO_DES3 |
wolfSSL | 15:117db924cf7c | 1527 | case DESb: |
wolfSSL | 15:117db924cf7c | 1528 | oid = blkDesCbcOid; |
wolfSSL | 15:117db924cf7c | 1529 | *oidSz = sizeof(blkDesCbcOid); |
wolfSSL | 15:117db924cf7c | 1530 | break; |
wolfSSL | 15:117db924cf7c | 1531 | case DES3b: |
wolfSSL | 15:117db924cf7c | 1532 | oid = blkDes3CbcOid; |
wolfSSL | 15:117db924cf7c | 1533 | *oidSz = sizeof(blkDes3CbcOid); |
wolfSSL | 15:117db924cf7c | 1534 | break; |
wolfSSL | 15:117db924cf7c | 1535 | #endif /* !NO_DES3 */ |
wolfSSL | 15:117db924cf7c | 1536 | } |
wolfSSL | 15:117db924cf7c | 1537 | break; |
wolfSSL | 15:117db924cf7c | 1538 | |
wolfSSL | 15:117db924cf7c | 1539 | #ifdef HAVE_OCSP |
wolfSSL | 15:117db924cf7c | 1540 | case oidOcspType: |
wolfSSL | 15:117db924cf7c | 1541 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1542 | case OCSP_BASIC_OID: |
wolfSSL | 15:117db924cf7c | 1543 | oid = ocspBasicOid; |
wolfSSL | 15:117db924cf7c | 1544 | *oidSz = sizeof(ocspBasicOid); |
wolfSSL | 15:117db924cf7c | 1545 | break; |
wolfSSL | 15:117db924cf7c | 1546 | case OCSP_NONCE_OID: |
wolfSSL | 15:117db924cf7c | 1547 | oid = ocspNonceOid; |
wolfSSL | 15:117db924cf7c | 1548 | *oidSz = sizeof(ocspNonceOid); |
wolfSSL | 15:117db924cf7c | 1549 | break; |
wolfSSL | 15:117db924cf7c | 1550 | } |
wolfSSL | 15:117db924cf7c | 1551 | break; |
wolfSSL | 15:117db924cf7c | 1552 | #endif /* HAVE_OCSP */ |
wolfSSL | 15:117db924cf7c | 1553 | |
wolfSSL | 15:117db924cf7c | 1554 | case oidCertExtType: |
wolfSSL | 15:117db924cf7c | 1555 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1556 | case BASIC_CA_OID: |
wolfSSL | 15:117db924cf7c | 1557 | oid = extBasicCaOid; |
wolfSSL | 15:117db924cf7c | 1558 | *oidSz = sizeof(extBasicCaOid); |
wolfSSL | 15:117db924cf7c | 1559 | break; |
wolfSSL | 15:117db924cf7c | 1560 | case ALT_NAMES_OID: |
wolfSSL | 15:117db924cf7c | 1561 | oid = extAltNamesOid; |
wolfSSL | 15:117db924cf7c | 1562 | *oidSz = sizeof(extAltNamesOid); |
wolfSSL | 15:117db924cf7c | 1563 | break; |
wolfSSL | 15:117db924cf7c | 1564 | case CRL_DIST_OID: |
wolfSSL | 15:117db924cf7c | 1565 | oid = extCrlDistOid; |
wolfSSL | 15:117db924cf7c | 1566 | *oidSz = sizeof(extCrlDistOid); |
wolfSSL | 15:117db924cf7c | 1567 | break; |
wolfSSL | 15:117db924cf7c | 1568 | case AUTH_INFO_OID: |
wolfSSL | 15:117db924cf7c | 1569 | oid = extAuthInfoOid; |
wolfSSL | 15:117db924cf7c | 1570 | *oidSz = sizeof(extAuthInfoOid); |
wolfSSL | 15:117db924cf7c | 1571 | break; |
wolfSSL | 15:117db924cf7c | 1572 | case AUTH_KEY_OID: |
wolfSSL | 15:117db924cf7c | 1573 | oid = extAuthKeyOid; |
wolfSSL | 15:117db924cf7c | 1574 | *oidSz = sizeof(extAuthKeyOid); |
wolfSSL | 15:117db924cf7c | 1575 | break; |
wolfSSL | 15:117db924cf7c | 1576 | case SUBJ_KEY_OID: |
wolfSSL | 15:117db924cf7c | 1577 | oid = extSubjKeyOid; |
wolfSSL | 15:117db924cf7c | 1578 | *oidSz = sizeof(extSubjKeyOid); |
wolfSSL | 15:117db924cf7c | 1579 | break; |
wolfSSL | 15:117db924cf7c | 1580 | case CERT_POLICY_OID: |
wolfSSL | 15:117db924cf7c | 1581 | oid = extCertPolicyOid; |
wolfSSL | 15:117db924cf7c | 1582 | *oidSz = sizeof(extCertPolicyOid); |
wolfSSL | 15:117db924cf7c | 1583 | break; |
wolfSSL | 15:117db924cf7c | 1584 | case KEY_USAGE_OID: |
wolfSSL | 15:117db924cf7c | 1585 | oid = extKeyUsageOid; |
wolfSSL | 15:117db924cf7c | 1586 | *oidSz = sizeof(extKeyUsageOid); |
wolfSSL | 15:117db924cf7c | 1587 | break; |
wolfSSL | 15:117db924cf7c | 1588 | case INHIBIT_ANY_OID: |
wolfSSL | 15:117db924cf7c | 1589 | oid = extInhibitAnyOid; |
wolfSSL | 15:117db924cf7c | 1590 | *oidSz = sizeof(extInhibitAnyOid); |
wolfSSL | 15:117db924cf7c | 1591 | break; |
wolfSSL | 15:117db924cf7c | 1592 | case EXT_KEY_USAGE_OID: |
wolfSSL | 15:117db924cf7c | 1593 | oid = extExtKeyUsageOid; |
wolfSSL | 15:117db924cf7c | 1594 | *oidSz = sizeof(extExtKeyUsageOid); |
wolfSSL | 15:117db924cf7c | 1595 | break; |
wolfSSL | 15:117db924cf7c | 1596 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 1597 | case NAME_CONS_OID: |
wolfSSL | 15:117db924cf7c | 1598 | oid = extNameConsOid; |
wolfSSL | 15:117db924cf7c | 1599 | *oidSz = sizeof(extNameConsOid); |
wolfSSL | 15:117db924cf7c | 1600 | break; |
wolfSSL | 15:117db924cf7c | 1601 | #endif |
wolfSSL | 15:117db924cf7c | 1602 | } |
wolfSSL | 15:117db924cf7c | 1603 | break; |
wolfSSL | 15:117db924cf7c | 1604 | |
wolfSSL | 15:117db924cf7c | 1605 | case oidCertAuthInfoType: |
wolfSSL | 15:117db924cf7c | 1606 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1607 | #ifdef HAVE_OCSP |
wolfSSL | 15:117db924cf7c | 1608 | case AIA_OCSP_OID: |
wolfSSL | 15:117db924cf7c | 1609 | oid = extAuthInfoOcspOid; |
wolfSSL | 15:117db924cf7c | 1610 | *oidSz = sizeof(extAuthInfoOcspOid); |
wolfSSL | 15:117db924cf7c | 1611 | break; |
wolfSSL | 15:117db924cf7c | 1612 | #endif |
wolfSSL | 15:117db924cf7c | 1613 | case AIA_CA_ISSUER_OID: |
wolfSSL | 15:117db924cf7c | 1614 | oid = extAuthInfoCaIssuerOid; |
wolfSSL | 15:117db924cf7c | 1615 | *oidSz = sizeof(extAuthInfoCaIssuerOid); |
wolfSSL | 15:117db924cf7c | 1616 | break; |
wolfSSL | 15:117db924cf7c | 1617 | } |
wolfSSL | 15:117db924cf7c | 1618 | break; |
wolfSSL | 15:117db924cf7c | 1619 | |
wolfSSL | 15:117db924cf7c | 1620 | case oidCertPolicyType: |
wolfSSL | 15:117db924cf7c | 1621 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1622 | case CP_ANY_OID: |
wolfSSL | 15:117db924cf7c | 1623 | oid = extCertPolicyAnyOid; |
wolfSSL | 15:117db924cf7c | 1624 | *oidSz = sizeof(extCertPolicyAnyOid); |
wolfSSL | 15:117db924cf7c | 1625 | break; |
wolfSSL | 15:117db924cf7c | 1626 | } |
wolfSSL | 15:117db924cf7c | 1627 | break; |
wolfSSL | 15:117db924cf7c | 1628 | |
wolfSSL | 15:117db924cf7c | 1629 | case oidCertAltNameType: |
wolfSSL | 15:117db924cf7c | 1630 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1631 | case HW_NAME_OID: |
wolfSSL | 15:117db924cf7c | 1632 | oid = extAltNamesHwNameOid; |
wolfSSL | 15:117db924cf7c | 1633 | *oidSz = sizeof(extAltNamesHwNameOid); |
wolfSSL | 15:117db924cf7c | 1634 | break; |
wolfSSL | 15:117db924cf7c | 1635 | } |
wolfSSL | 15:117db924cf7c | 1636 | break; |
wolfSSL | 15:117db924cf7c | 1637 | |
wolfSSL | 15:117db924cf7c | 1638 | case oidCertKeyUseType: |
wolfSSL | 15:117db924cf7c | 1639 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1640 | case EKU_ANY_OID: |
wolfSSL | 15:117db924cf7c | 1641 | oid = extExtKeyUsageAnyOid; |
wolfSSL | 15:117db924cf7c | 1642 | *oidSz = sizeof(extExtKeyUsageAnyOid); |
wolfSSL | 15:117db924cf7c | 1643 | break; |
wolfSSL | 15:117db924cf7c | 1644 | case EKU_SERVER_AUTH_OID: |
wolfSSL | 15:117db924cf7c | 1645 | oid = extExtKeyUsageServerAuthOid; |
wolfSSL | 15:117db924cf7c | 1646 | *oidSz = sizeof(extExtKeyUsageServerAuthOid); |
wolfSSL | 15:117db924cf7c | 1647 | break; |
wolfSSL | 15:117db924cf7c | 1648 | case EKU_CLIENT_AUTH_OID: |
wolfSSL | 15:117db924cf7c | 1649 | oid = extExtKeyUsageClientAuthOid; |
wolfSSL | 15:117db924cf7c | 1650 | *oidSz = sizeof(extExtKeyUsageClientAuthOid); |
wolfSSL | 15:117db924cf7c | 1651 | break; |
wolfSSL | 15:117db924cf7c | 1652 | case EKU_CODESIGNING_OID: |
wolfSSL | 15:117db924cf7c | 1653 | oid = extExtKeyUsageCodeSigningOid; |
wolfSSL | 15:117db924cf7c | 1654 | *oidSz = sizeof(extExtKeyUsageCodeSigningOid); |
wolfSSL | 15:117db924cf7c | 1655 | break; |
wolfSSL | 15:117db924cf7c | 1656 | case EKU_EMAILPROTECT_OID: |
wolfSSL | 15:117db924cf7c | 1657 | oid = extExtKeyUsageEmailProtectOid; |
wolfSSL | 15:117db924cf7c | 1658 | *oidSz = sizeof(extExtKeyUsageEmailProtectOid); |
wolfSSL | 15:117db924cf7c | 1659 | break; |
wolfSSL | 15:117db924cf7c | 1660 | case EKU_TIMESTAMP_OID: |
wolfSSL | 15:117db924cf7c | 1661 | oid = extExtKeyUsageTimestampOid; |
wolfSSL | 15:117db924cf7c | 1662 | *oidSz = sizeof(extExtKeyUsageTimestampOid); |
wolfSSL | 15:117db924cf7c | 1663 | break; |
wolfSSL | 15:117db924cf7c | 1664 | case EKU_OCSP_SIGN_OID: |
wolfSSL | 15:117db924cf7c | 1665 | oid = extExtKeyUsageOcspSignOid; |
wolfSSL | 15:117db924cf7c | 1666 | *oidSz = sizeof(extExtKeyUsageOcspSignOid); |
wolfSSL | 15:117db924cf7c | 1667 | break; |
wolfSSL | 15:117db924cf7c | 1668 | } |
wolfSSL | 15:117db924cf7c | 1669 | break; |
wolfSSL | 15:117db924cf7c | 1670 | |
wolfSSL | 15:117db924cf7c | 1671 | case oidKdfType: |
wolfSSL | 15:117db924cf7c | 1672 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1673 | case PBKDF2_OID: |
wolfSSL | 15:117db924cf7c | 1674 | oid = pbkdf2Oid; |
wolfSSL | 15:117db924cf7c | 1675 | *oidSz = sizeof(pbkdf2Oid); |
wolfSSL | 15:117db924cf7c | 1676 | break; |
wolfSSL | 15:117db924cf7c | 1677 | } |
wolfSSL | 15:117db924cf7c | 1678 | break; |
wolfSSL | 15:117db924cf7c | 1679 | |
wolfSSL | 15:117db924cf7c | 1680 | case oidPBEType: |
wolfSSL | 15:117db924cf7c | 1681 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1682 | #if !defined(NO_SHA) && !defined(NO_RC4) |
wolfSSL | 15:117db924cf7c | 1683 | case PBE_SHA1_RC4_128: |
wolfSSL | 15:117db924cf7c | 1684 | oid = pbeSha1RC4128; |
wolfSSL | 15:117db924cf7c | 1685 | *oidSz = sizeof(pbeSha1RC4128); |
wolfSSL | 15:117db924cf7c | 1686 | break; |
wolfSSL | 15:117db924cf7c | 1687 | #endif |
wolfSSL | 15:117db924cf7c | 1688 | #if !defined(NO_SHA) && !defined(NO_DES3) |
wolfSSL | 15:117db924cf7c | 1689 | case PBE_SHA1_DES: |
wolfSSL | 15:117db924cf7c | 1690 | oid = pbeSha1Des; |
wolfSSL | 15:117db924cf7c | 1691 | *oidSz = sizeof(pbeSha1Des); |
wolfSSL | 15:117db924cf7c | 1692 | break; |
wolfSSL | 15:117db924cf7c | 1693 | |
wolfSSL | 15:117db924cf7c | 1694 | #endif |
wolfSSL | 15:117db924cf7c | 1695 | #if !defined(NO_SHA) && !defined(NO_DES3) |
wolfSSL | 15:117db924cf7c | 1696 | case PBE_SHA1_DES3: |
wolfSSL | 15:117db924cf7c | 1697 | oid = pbeSha1Des3; |
wolfSSL | 15:117db924cf7c | 1698 | *oidSz = sizeof(pbeSha1Des3); |
wolfSSL | 15:117db924cf7c | 1699 | break; |
wolfSSL | 15:117db924cf7c | 1700 | #endif |
wolfSSL | 15:117db924cf7c | 1701 | } |
wolfSSL | 15:117db924cf7c | 1702 | break; |
wolfSSL | 15:117db924cf7c | 1703 | |
wolfSSL | 15:117db924cf7c | 1704 | case oidKeyWrapType: |
wolfSSL | 15:117db924cf7c | 1705 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1706 | #ifdef WOLFSSL_AES_128 |
wolfSSL | 15:117db924cf7c | 1707 | case AES128_WRAP: |
wolfSSL | 15:117db924cf7c | 1708 | oid = wrapAes128Oid; |
wolfSSL | 15:117db924cf7c | 1709 | *oidSz = sizeof(wrapAes128Oid); |
wolfSSL | 15:117db924cf7c | 1710 | break; |
wolfSSL | 15:117db924cf7c | 1711 | #endif |
wolfSSL | 15:117db924cf7c | 1712 | #ifdef WOLFSSL_AES_192 |
wolfSSL | 15:117db924cf7c | 1713 | case AES192_WRAP: |
wolfSSL | 15:117db924cf7c | 1714 | oid = wrapAes192Oid; |
wolfSSL | 15:117db924cf7c | 1715 | *oidSz = sizeof(wrapAes192Oid); |
wolfSSL | 15:117db924cf7c | 1716 | break; |
wolfSSL | 15:117db924cf7c | 1717 | #endif |
wolfSSL | 15:117db924cf7c | 1718 | #ifdef WOLFSSL_AES_256 |
wolfSSL | 15:117db924cf7c | 1719 | case AES256_WRAP: |
wolfSSL | 15:117db924cf7c | 1720 | oid = wrapAes256Oid; |
wolfSSL | 15:117db924cf7c | 1721 | *oidSz = sizeof(wrapAes256Oid); |
wolfSSL | 15:117db924cf7c | 1722 | break; |
wolfSSL | 15:117db924cf7c | 1723 | #endif |
wolfSSL | 15:117db924cf7c | 1724 | } |
wolfSSL | 15:117db924cf7c | 1725 | break; |
wolfSSL | 15:117db924cf7c | 1726 | |
wolfSSL | 15:117db924cf7c | 1727 | case oidCmsKeyAgreeType: |
wolfSSL | 15:117db924cf7c | 1728 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1729 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 1730 | case dhSinglePass_stdDH_sha1kdf_scheme: |
wolfSSL | 15:117db924cf7c | 1731 | oid = dhSinglePass_stdDH_sha1kdf_Oid; |
wolfSSL | 15:117db924cf7c | 1732 | *oidSz = sizeof(dhSinglePass_stdDH_sha1kdf_Oid); |
wolfSSL | 15:117db924cf7c | 1733 | break; |
wolfSSL | 15:117db924cf7c | 1734 | #endif |
wolfSSL | 15:117db924cf7c | 1735 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1736 | case dhSinglePass_stdDH_sha224kdf_scheme: |
wolfSSL | 15:117db924cf7c | 1737 | oid = dhSinglePass_stdDH_sha224kdf_Oid; |
wolfSSL | 15:117db924cf7c | 1738 | *oidSz = sizeof(dhSinglePass_stdDH_sha224kdf_Oid); |
wolfSSL | 15:117db924cf7c | 1739 | break; |
wolfSSL | 15:117db924cf7c | 1740 | #endif |
wolfSSL | 15:117db924cf7c | 1741 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1742 | case dhSinglePass_stdDH_sha256kdf_scheme: |
wolfSSL | 15:117db924cf7c | 1743 | oid = dhSinglePass_stdDH_sha256kdf_Oid; |
wolfSSL | 15:117db924cf7c | 1744 | *oidSz = sizeof(dhSinglePass_stdDH_sha256kdf_Oid); |
wolfSSL | 15:117db924cf7c | 1745 | break; |
wolfSSL | 15:117db924cf7c | 1746 | #endif |
wolfSSL | 15:117db924cf7c | 1747 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1748 | case dhSinglePass_stdDH_sha384kdf_scheme: |
wolfSSL | 15:117db924cf7c | 1749 | oid = dhSinglePass_stdDH_sha384kdf_Oid; |
wolfSSL | 15:117db924cf7c | 1750 | *oidSz = sizeof(dhSinglePass_stdDH_sha384kdf_Oid); |
wolfSSL | 15:117db924cf7c | 1751 | break; |
wolfSSL | 15:117db924cf7c | 1752 | #endif |
wolfSSL | 15:117db924cf7c | 1753 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1754 | case dhSinglePass_stdDH_sha512kdf_scheme: |
wolfSSL | 15:117db924cf7c | 1755 | oid = dhSinglePass_stdDH_sha512kdf_Oid; |
wolfSSL | 15:117db924cf7c | 1756 | *oidSz = sizeof(dhSinglePass_stdDH_sha512kdf_Oid); |
wolfSSL | 15:117db924cf7c | 1757 | break; |
wolfSSL | 15:117db924cf7c | 1758 | #endif |
wolfSSL | 15:117db924cf7c | 1759 | } |
wolfSSL | 15:117db924cf7c | 1760 | break; |
wolfSSL | 15:117db924cf7c | 1761 | |
wolfSSL | 15:117db924cf7c | 1762 | #ifndef NO_HMAC |
wolfSSL | 15:117db924cf7c | 1763 | case oidHmacType: |
wolfSSL | 15:117db924cf7c | 1764 | switch (id) { |
wolfSSL | 15:117db924cf7c | 1765 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 1766 | case HMAC_SHA224_OID: |
wolfSSL | 15:117db924cf7c | 1767 | oid = hmacSha224Oid; |
wolfSSL | 15:117db924cf7c | 1768 | *oidSz = sizeof(hmacSha224Oid); |
wolfSSL | 15:117db924cf7c | 1769 | break; |
wolfSSL | 15:117db924cf7c | 1770 | #endif |
wolfSSL | 15:117db924cf7c | 1771 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 1772 | case HMAC_SHA256_OID: |
wolfSSL | 15:117db924cf7c | 1773 | oid = hmacSha256Oid; |
wolfSSL | 15:117db924cf7c | 1774 | *oidSz = sizeof(hmacSha256Oid); |
wolfSSL | 15:117db924cf7c | 1775 | break; |
wolfSSL | 15:117db924cf7c | 1776 | #endif |
wolfSSL | 15:117db924cf7c | 1777 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 1778 | case HMAC_SHA384_OID: |
wolfSSL | 15:117db924cf7c | 1779 | oid = hmacSha384Oid; |
wolfSSL | 15:117db924cf7c | 1780 | *oidSz = sizeof(hmacSha384Oid); |
wolfSSL | 15:117db924cf7c | 1781 | break; |
wolfSSL | 15:117db924cf7c | 1782 | #endif |
wolfSSL | 15:117db924cf7c | 1783 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 1784 | case HMAC_SHA512_OID: |
wolfSSL | 15:117db924cf7c | 1785 | oid = hmacSha512Oid; |
wolfSSL | 15:117db924cf7c | 1786 | *oidSz = sizeof(hmacSha512Oid); |
wolfSSL | 15:117db924cf7c | 1787 | break; |
wolfSSL | 15:117db924cf7c | 1788 | #endif |
wolfSSL | 15:117db924cf7c | 1789 | } |
wolfSSL | 15:117db924cf7c | 1790 | break; |
wolfSSL | 15:117db924cf7c | 1791 | #endif /* !NO_HMAC */ |
wolfSSL | 15:117db924cf7c | 1792 | |
wolfSSL | 15:117db924cf7c | 1793 | case oidIgnoreType: |
wolfSSL | 15:117db924cf7c | 1794 | default: |
wolfSSL | 15:117db924cf7c | 1795 | break; |
wolfSSL | 15:117db924cf7c | 1796 | } |
wolfSSL | 15:117db924cf7c | 1797 | |
wolfSSL | 15:117db924cf7c | 1798 | return oid; |
wolfSSL | 15:117db924cf7c | 1799 | } |
wolfSSL | 15:117db924cf7c | 1800 | |
wolfSSL | 15:117db924cf7c | 1801 | #ifdef HAVE_OID_ENCODING |
wolfSSL | 15:117db924cf7c | 1802 | int EncodeObjectId(const word16* in, word32 inSz, byte* out, word32* outSz) |
wolfSSL | 15:117db924cf7c | 1803 | { |
wolfSSL | 15:117db924cf7c | 1804 | int i, x, len; |
wolfSSL | 15:117db924cf7c | 1805 | word32 d, t; |
wolfSSL | 15:117db924cf7c | 1806 | |
wolfSSL | 15:117db924cf7c | 1807 | /* check args */ |
wolfSSL | 15:117db924cf7c | 1808 | if (in == NULL || outSz == NULL) { |
wolfSSL | 15:117db924cf7c | 1809 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 1810 | } |
wolfSSL | 15:117db924cf7c | 1811 | |
wolfSSL | 15:117db924cf7c | 1812 | /* compute length of encoded OID */ |
wolfSSL | 15:117db924cf7c | 1813 | d = (in[0] * 40) + in[1]; |
wolfSSL | 15:117db924cf7c | 1814 | len = 0; |
wolfSSL | 15:117db924cf7c | 1815 | for (i = 1; i < (int)inSz; i++) { |
wolfSSL | 15:117db924cf7c | 1816 | x = 0; |
wolfSSL | 15:117db924cf7c | 1817 | t = d; |
wolfSSL | 15:117db924cf7c | 1818 | while (t) { |
wolfSSL | 15:117db924cf7c | 1819 | x++; |
wolfSSL | 15:117db924cf7c | 1820 | t >>= 1; |
wolfSSL | 15:117db924cf7c | 1821 | } |
wolfSSL | 15:117db924cf7c | 1822 | len += (x / 7) + ((x % 7) ? 1 : 0) + (d == 0 ? 1 : 0); |
wolfSSL | 15:117db924cf7c | 1823 | |
wolfSSL | 15:117db924cf7c | 1824 | if (i < (int)inSz - 1) { |
wolfSSL | 15:117db924cf7c | 1825 | d = in[i + 1]; |
wolfSSL | 15:117db924cf7c | 1826 | } |
wolfSSL | 15:117db924cf7c | 1827 | } |
wolfSSL | 15:117db924cf7c | 1828 | |
wolfSSL | 15:117db924cf7c | 1829 | if (out) { |
wolfSSL | 15:117db924cf7c | 1830 | /* verify length */ |
wolfSSL | 15:117db924cf7c | 1831 | if ((int)*outSz < len) { |
wolfSSL | 15:117db924cf7c | 1832 | return BUFFER_E; /* buffer provided is not large enough */ |
wolfSSL | 15:117db924cf7c | 1833 | } |
wolfSSL | 15:117db924cf7c | 1834 | |
wolfSSL | 15:117db924cf7c | 1835 | /* calc first byte */ |
wolfSSL | 15:117db924cf7c | 1836 | d = (in[0] * 40) + in[1]; |
wolfSSL | 15:117db924cf7c | 1837 | |
wolfSSL | 15:117db924cf7c | 1838 | /* encode bytes */ |
wolfSSL | 15:117db924cf7c | 1839 | x = 0; |
wolfSSL | 15:117db924cf7c | 1840 | for (i = 1; i < (int)inSz; i++) { |
wolfSSL | 15:117db924cf7c | 1841 | if (d) { |
wolfSSL | 15:117db924cf7c | 1842 | int y = x, z; |
wolfSSL | 15:117db924cf7c | 1843 | byte mask = 0; |
wolfSSL | 15:117db924cf7c | 1844 | while (d) { |
wolfSSL | 15:117db924cf7c | 1845 | out[x++] = (byte)((d & 0x7F) | mask); |
wolfSSL | 15:117db924cf7c | 1846 | d >>= 7; |
wolfSSL | 15:117db924cf7c | 1847 | mask |= 0x80; /* upper bit is set on all but the last byte */ |
wolfSSL | 15:117db924cf7c | 1848 | } |
wolfSSL | 15:117db924cf7c | 1849 | /* now swap bytes y...x-1 */ |
wolfSSL | 15:117db924cf7c | 1850 | z = x - 1; |
wolfSSL | 15:117db924cf7c | 1851 | while (y < z) { |
wolfSSL | 15:117db924cf7c | 1852 | mask = out[y]; |
wolfSSL | 15:117db924cf7c | 1853 | out[y] = out[z]; |
wolfSSL | 15:117db924cf7c | 1854 | out[z] = mask; |
wolfSSL | 15:117db924cf7c | 1855 | ++y; |
wolfSSL | 15:117db924cf7c | 1856 | --z; |
wolfSSL | 15:117db924cf7c | 1857 | } |
wolfSSL | 15:117db924cf7c | 1858 | } |
wolfSSL | 15:117db924cf7c | 1859 | else { |
wolfSSL | 15:117db924cf7c | 1860 | out[x++] = 0x00; /* zero value */ |
wolfSSL | 15:117db924cf7c | 1861 | } |
wolfSSL | 15:117db924cf7c | 1862 | |
wolfSSL | 15:117db924cf7c | 1863 | /* next word */ |
wolfSSL | 15:117db924cf7c | 1864 | if (i < (int)inSz - 1) { |
wolfSSL | 15:117db924cf7c | 1865 | d = in[i + 1]; |
wolfSSL | 15:117db924cf7c | 1866 | } |
wolfSSL | 15:117db924cf7c | 1867 | } |
wolfSSL | 15:117db924cf7c | 1868 | } |
wolfSSL | 15:117db924cf7c | 1869 | |
wolfSSL | 15:117db924cf7c | 1870 | /* return length */ |
wolfSSL | 15:117db924cf7c | 1871 | *outSz = len; |
wolfSSL | 15:117db924cf7c | 1872 | |
wolfSSL | 15:117db924cf7c | 1873 | return 0; |
wolfSSL | 15:117db924cf7c | 1874 | } |
wolfSSL | 15:117db924cf7c | 1875 | #endif /* HAVE_OID_ENCODING */ |
wolfSSL | 15:117db924cf7c | 1876 | |
wolfSSL | 15:117db924cf7c | 1877 | #ifdef HAVE_OID_DECODING |
wolfSSL | 15:117db924cf7c | 1878 | int DecodeObjectId(const byte* in, word32 inSz, word16* out, word32* outSz) |
wolfSSL | 15:117db924cf7c | 1879 | { |
wolfSSL | 15:117db924cf7c | 1880 | int x = 0, y = 0; |
wolfSSL | 15:117db924cf7c | 1881 | word32 t = 0; |
wolfSSL | 15:117db924cf7c | 1882 | |
wolfSSL | 15:117db924cf7c | 1883 | /* check args */ |
wolfSSL | 15:117db924cf7c | 1884 | if (in == NULL || outSz == NULL) { |
wolfSSL | 15:117db924cf7c | 1885 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 1886 | } |
wolfSSL | 15:117db924cf7c | 1887 | |
wolfSSL | 15:117db924cf7c | 1888 | /* decode bytes */ |
wolfSSL | 15:117db924cf7c | 1889 | while (inSz--) { |
wolfSSL | 15:117db924cf7c | 1890 | t = (t << 7) | (in[x] & 0x7F); |
wolfSSL | 15:117db924cf7c | 1891 | if (!(in[x] & 0x80)) { |
wolfSSL | 15:117db924cf7c | 1892 | if (y >= (int)*outSz) { |
wolfSSL | 15:117db924cf7c | 1893 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 1894 | } |
wolfSSL | 15:117db924cf7c | 1895 | if (y == 0) { |
wolfSSL | 15:117db924cf7c | 1896 | out[0] = (t / 40); |
wolfSSL | 15:117db924cf7c | 1897 | out[1] = (t % 40); |
wolfSSL | 15:117db924cf7c | 1898 | y = 2; |
wolfSSL | 15:117db924cf7c | 1899 | } |
wolfSSL | 15:117db924cf7c | 1900 | else { |
wolfSSL | 15:117db924cf7c | 1901 | out[y++] = t; |
wolfSSL | 15:117db924cf7c | 1902 | } |
wolfSSL | 15:117db924cf7c | 1903 | t = 0; /* reset tmp */ |
wolfSSL | 15:117db924cf7c | 1904 | } |
wolfSSL | 15:117db924cf7c | 1905 | x++; |
wolfSSL | 15:117db924cf7c | 1906 | } |
wolfSSL | 15:117db924cf7c | 1907 | |
wolfSSL | 15:117db924cf7c | 1908 | /* return length */ |
wolfSSL | 15:117db924cf7c | 1909 | *outSz = y; |
wolfSSL | 15:117db924cf7c | 1910 | |
wolfSSL | 15:117db924cf7c | 1911 | return 0; |
wolfSSL | 15:117db924cf7c | 1912 | } |
wolfSSL | 15:117db924cf7c | 1913 | #endif /* HAVE_OID_DECODING */ |
wolfSSL | 15:117db924cf7c | 1914 | |
wolfSSL | 15:117db924cf7c | 1915 | /* Get the DER/BER encoding of an ASN.1 OBJECT_ID header. |
wolfSSL | 15:117db924cf7c | 1916 | * |
wolfSSL | 15:117db924cf7c | 1917 | * input Buffer holding DER/BER encoded data. |
wolfSSL | 15:117db924cf7c | 1918 | * inOutIdx Current index into buffer to parse. |
wolfSSL | 15:117db924cf7c | 1919 | * len The number of bytes in the ASN.1 data. |
wolfSSL | 15:117db924cf7c | 1920 | * maxIdx Length of data in buffer. |
wolfSSL | 15:117db924cf7c | 1921 | * returns BUFFER_E when there is not enough data to parse. |
wolfSSL | 15:117db924cf7c | 1922 | * ASN_OBJECt_ID_E when the OBJECT_ID tag is not found. |
wolfSSL | 15:117db924cf7c | 1923 | * ASN_PARSE_E when length is invalid. |
wolfSSL | 15:117db924cf7c | 1924 | * Otherwise, 0 to indicate success. |
wolfSSL | 15:117db924cf7c | 1925 | */ |
wolfSSL | 15:117db924cf7c | 1926 | static int GetASNObjectId(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 15:117db924cf7c | 1927 | word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 1928 | { |
wolfSSL | 15:117db924cf7c | 1929 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 1930 | byte b; |
wolfSSL | 15:117db924cf7c | 1931 | int length; |
wolfSSL | 15:117db924cf7c | 1932 | |
wolfSSL | 15:117db924cf7c | 1933 | if ((idx + 1) > maxIdx) |
wolfSSL | 15:117db924cf7c | 1934 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 1935 | |
wolfSSL | 15:117db924cf7c | 1936 | b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 1937 | if (b != ASN_OBJECT_ID) |
wolfSSL | 15:117db924cf7c | 1938 | return ASN_OBJECT_ID_E; |
wolfSSL | 15:117db924cf7c | 1939 | |
wolfSSL | 15:117db924cf7c | 1940 | if (GetLength(input, &idx, &length, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 1941 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 1942 | |
wolfSSL | 15:117db924cf7c | 1943 | *len = length; |
wolfSSL | 15:117db924cf7c | 1944 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 1945 | return 0; |
wolfSSL | 15:117db924cf7c | 1946 | } |
wolfSSL | 15:117db924cf7c | 1947 | |
wolfSSL | 15:117db924cf7c | 1948 | /* Set the DER/BER encoding of the ASN.1 OBJECT_ID header. |
wolfSSL | 15:117db924cf7c | 1949 | * |
wolfSSL | 15:117db924cf7c | 1950 | * len Length of the OBJECT_ID data. |
wolfSSL | 15:117db924cf7c | 1951 | * output Buffer to write into. |
wolfSSL | 15:117db924cf7c | 1952 | * returns the number of bytes added to the buffer. |
wolfSSL | 15:117db924cf7c | 1953 | */ |
wolfSSL | 15:117db924cf7c | 1954 | static int SetObjectId(int len, byte* output) |
wolfSSL | 15:117db924cf7c | 1955 | { |
wolfSSL | 15:117db924cf7c | 1956 | int idx = 0; |
wolfSSL | 15:117db924cf7c | 1957 | |
wolfSSL | 15:117db924cf7c | 1958 | output[idx++] = ASN_OBJECT_ID; |
wolfSSL | 15:117db924cf7c | 1959 | idx += SetLength(len, output + idx); |
wolfSSL | 15:117db924cf7c | 1960 | |
wolfSSL | 15:117db924cf7c | 1961 | return idx; |
wolfSSL | 15:117db924cf7c | 1962 | } |
wolfSSL | 15:117db924cf7c | 1963 | |
wolfSSL | 15:117db924cf7c | 1964 | int GetObjectId(const byte* input, word32* inOutIdx, word32* oid, |
wolfSSL | 15:117db924cf7c | 1965 | word32 oidType, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 1966 | { |
wolfSSL | 15:117db924cf7c | 1967 | int ret = 0, length; |
wolfSSL | 15:117db924cf7c | 1968 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 1969 | #ifndef NO_VERIFY_OID |
wolfSSL | 15:117db924cf7c | 1970 | word32 actualOidSz = 0; |
wolfSSL | 15:117db924cf7c | 1971 | const byte* actualOid; |
wolfSSL | 15:117db924cf7c | 1972 | #endif /* NO_VERIFY_OID */ |
wolfSSL | 15:117db924cf7c | 1973 | |
wolfSSL | 15:117db924cf7c | 1974 | (void)oidType; |
wolfSSL | 15:117db924cf7c | 1975 | WOLFSSL_ENTER("GetObjectId()"); |
wolfSSL | 15:117db924cf7c | 1976 | *oid = 0; |
wolfSSL | 15:117db924cf7c | 1977 | |
wolfSSL | 15:117db924cf7c | 1978 | ret = GetASNObjectId(input, &idx, &length, maxIdx); |
wolfSSL | 15:117db924cf7c | 1979 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 1980 | return ret; |
wolfSSL | 15:117db924cf7c | 1981 | |
wolfSSL | 15:117db924cf7c | 1982 | #ifndef NO_VERIFY_OID |
wolfSSL | 15:117db924cf7c | 1983 | actualOid = &input[idx]; |
wolfSSL | 15:117db924cf7c | 1984 | if (length > 0) |
wolfSSL | 15:117db924cf7c | 1985 | actualOidSz = (word32)length; |
wolfSSL | 15:117db924cf7c | 1986 | #endif /* NO_VERIFY_OID */ |
wolfSSL | 15:117db924cf7c | 1987 | |
wolfSSL | 15:117db924cf7c | 1988 | while (length--) { |
wolfSSL | 15:117db924cf7c | 1989 | /* odd HC08 compiler behavior here when input[idx++] */ |
wolfSSL | 15:117db924cf7c | 1990 | *oid += (word32)input[idx]; |
wolfSSL | 15:117db924cf7c | 1991 | idx++; |
wolfSSL | 15:117db924cf7c | 1992 | } |
wolfSSL | 15:117db924cf7c | 1993 | /* just sum it up for now */ |
wolfSSL | 15:117db924cf7c | 1994 | |
wolfSSL | 15:117db924cf7c | 1995 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 1996 | |
wolfSSL | 15:117db924cf7c | 1997 | #ifndef NO_VERIFY_OID |
wolfSSL | 15:117db924cf7c | 1998 | { |
wolfSSL | 15:117db924cf7c | 1999 | const byte* checkOid = NULL; |
wolfSSL | 15:117db924cf7c | 2000 | word32 checkOidSz; |
wolfSSL | 15:117db924cf7c | 2001 | #ifdef ASN_DUMP_OID |
wolfSSL | 15:117db924cf7c | 2002 | word32 i; |
wolfSSL | 15:117db924cf7c | 2003 | #endif |
wolfSSL | 15:117db924cf7c | 2004 | |
wolfSSL | 15:117db924cf7c | 2005 | if (oidType != oidIgnoreType) { |
wolfSSL | 15:117db924cf7c | 2006 | checkOid = OidFromId(*oid, oidType, &checkOidSz); |
wolfSSL | 15:117db924cf7c | 2007 | |
wolfSSL | 15:117db924cf7c | 2008 | #ifdef ASN_DUMP_OID |
wolfSSL | 15:117db924cf7c | 2009 | /* support for dumping OID information */ |
wolfSSL | 15:117db924cf7c | 2010 | printf("OID (Type %d, Sz %d, Sum %d): ", oidType, actualOidSz, *oid); |
wolfSSL | 15:117db924cf7c | 2011 | for (i=0; i<actualOidSz; i++) { |
wolfSSL | 15:117db924cf7c | 2012 | printf("%d, ", actualOid[i]); |
wolfSSL | 15:117db924cf7c | 2013 | } |
wolfSSL | 15:117db924cf7c | 2014 | printf("\n"); |
wolfSSL | 15:117db924cf7c | 2015 | #ifdef HAVE_OID_DECODING |
wolfSSL | 15:117db924cf7c | 2016 | { |
wolfSSL | 15:117db924cf7c | 2017 | word16 decOid[16]; |
wolfSSL | 15:117db924cf7c | 2018 | word32 decOidSz = sizeof(decOid); |
wolfSSL | 15:117db924cf7c | 2019 | ret = DecodeObjectId(actualOid, actualOidSz, decOid, &decOidSz); |
wolfSSL | 15:117db924cf7c | 2020 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 2021 | printf(" Decoded (Sz %d): ", decOidSz); |
wolfSSL | 15:117db924cf7c | 2022 | for (i=0; i<decOidSz; i++) { |
wolfSSL | 15:117db924cf7c | 2023 | printf("%d.", decOid[i]); |
wolfSSL | 15:117db924cf7c | 2024 | } |
wolfSSL | 15:117db924cf7c | 2025 | printf("\n"); |
wolfSSL | 15:117db924cf7c | 2026 | } |
wolfSSL | 15:117db924cf7c | 2027 | else { |
wolfSSL | 15:117db924cf7c | 2028 | printf("DecodeObjectId failed: %d\n", ret); |
wolfSSL | 15:117db924cf7c | 2029 | } |
wolfSSL | 15:117db924cf7c | 2030 | } |
wolfSSL | 15:117db924cf7c | 2031 | #endif /* HAVE_OID_DECODING */ |
wolfSSL | 15:117db924cf7c | 2032 | #endif /* ASN_DUMP_OID */ |
wolfSSL | 15:117db924cf7c | 2033 | |
wolfSSL | 15:117db924cf7c | 2034 | if (checkOid != NULL && |
wolfSSL | 15:117db924cf7c | 2035 | (checkOidSz != actualOidSz || |
wolfSSL | 15:117db924cf7c | 2036 | XMEMCMP(actualOid, checkOid, checkOidSz) != 0)) { |
wolfSSL | 15:117db924cf7c | 2037 | WOLFSSL_MSG("OID Check Failed"); |
wolfSSL | 15:117db924cf7c | 2038 | return ASN_UNKNOWN_OID_E; |
wolfSSL | 15:117db924cf7c | 2039 | } |
wolfSSL | 15:117db924cf7c | 2040 | } |
wolfSSL | 15:117db924cf7c | 2041 | } |
wolfSSL | 15:117db924cf7c | 2042 | #endif /* NO_VERIFY_OID */ |
wolfSSL | 15:117db924cf7c | 2043 | |
wolfSSL | 15:117db924cf7c | 2044 | return ret; |
wolfSSL | 15:117db924cf7c | 2045 | } |
wolfSSL | 15:117db924cf7c | 2046 | |
wolfSSL | 15:117db924cf7c | 2047 | static int SkipObjectId(const byte* input, word32* inOutIdx, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 2048 | { |
wolfSSL | 15:117db924cf7c | 2049 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 2050 | int length; |
wolfSSL | 15:117db924cf7c | 2051 | int ret; |
wolfSSL | 15:117db924cf7c | 2052 | |
wolfSSL | 15:117db924cf7c | 2053 | ret = GetASNObjectId(input, &idx, &length, maxIdx); |
wolfSSL | 15:117db924cf7c | 2054 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 2055 | return ret; |
wolfSSL | 15:117db924cf7c | 2056 | |
wolfSSL | 15:117db924cf7c | 2057 | idx += length; |
wolfSSL | 15:117db924cf7c | 2058 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 2059 | |
wolfSSL | 15:117db924cf7c | 2060 | return 0; |
wolfSSL | 15:117db924cf7c | 2061 | } |
wolfSSL | 15:117db924cf7c | 2062 | |
wolfSSL | 15:117db924cf7c | 2063 | WOLFSSL_LOCAL int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid, |
wolfSSL | 15:117db924cf7c | 2064 | word32 oidType, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 2065 | { |
wolfSSL | 15:117db924cf7c | 2066 | int length; |
wolfSSL | 15:117db924cf7c | 2067 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 2068 | int ret; |
wolfSSL | 15:117db924cf7c | 2069 | *oid = 0; |
wolfSSL | 15:117db924cf7c | 2070 | |
wolfSSL | 15:117db924cf7c | 2071 | WOLFSSL_ENTER("GetAlgoId"); |
wolfSSL | 15:117db924cf7c | 2072 | |
wolfSSL | 15:117db924cf7c | 2073 | if (GetSequence(input, &idx, &length, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 2074 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 2075 | |
wolfSSL | 15:117db924cf7c | 2076 | if (GetObjectId(input, &idx, oid, oidType, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 2077 | return ASN_OBJECT_ID_E; |
wolfSSL | 15:117db924cf7c | 2078 | |
wolfSSL | 15:117db924cf7c | 2079 | /* could have NULL tag and 0 terminator, but may not */ |
wolfSSL | 15:117db924cf7c | 2080 | if (idx < maxIdx && input[idx] == ASN_TAG_NULL) { |
wolfSSL | 15:117db924cf7c | 2081 | ret = GetASNNull(input, &idx, maxIdx); |
wolfSSL | 15:117db924cf7c | 2082 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 2083 | return ret; |
wolfSSL | 15:117db924cf7c | 2084 | } |
wolfSSL | 15:117db924cf7c | 2085 | |
wolfSSL | 15:117db924cf7c | 2086 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 2087 | |
wolfSSL | 15:117db924cf7c | 2088 | return 0; |
wolfSSL | 15:117db924cf7c | 2089 | } |
wolfSSL | 15:117db924cf7c | 2090 | |
wolfSSL | 15:117db924cf7c | 2091 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 2092 | |
wolfSSL | 15:117db924cf7c | 2093 | #ifndef HAVE_USER_RSA |
wolfSSL | 15:117db924cf7c | 2094 | int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key, |
wolfSSL | 15:117db924cf7c | 2095 | word32 inSz) |
wolfSSL | 15:117db924cf7c | 2096 | { |
wolfSSL | 15:117db924cf7c | 2097 | int version, length; |
wolfSSL | 15:117db924cf7c | 2098 | |
wolfSSL | 15:117db924cf7c | 2099 | if (inOutIdx == NULL) { |
wolfSSL | 15:117db924cf7c | 2100 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2101 | } |
wolfSSL | 15:117db924cf7c | 2102 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 2103 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 2104 | |
wolfSSL | 15:117db924cf7c | 2105 | if (GetMyVersion(input, inOutIdx, &version, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 2106 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 2107 | |
wolfSSL | 15:117db924cf7c | 2108 | key->type = RSA_PRIVATE; |
wolfSSL | 15:117db924cf7c | 2109 | |
wolfSSL | 15:117db924cf7c | 2110 | if (GetInt(&key->n, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 2111 | GetInt(&key->e, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 2112 | GetInt(&key->d, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 2113 | GetInt(&key->p, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 2114 | GetInt(&key->q, input, inOutIdx, inSz) < 0) return ASN_RSA_KEY_E; |
wolfSSL | 15:117db924cf7c | 2115 | #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) |
wolfSSL | 15:117db924cf7c | 2116 | if (GetInt(&key->dP, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 2117 | GetInt(&key->dQ, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 2118 | GetInt(&key->u, input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E; |
wolfSSL | 15:117db924cf7c | 2119 | #else |
wolfSSL | 15:117db924cf7c | 2120 | if (SkipInt(input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 2121 | SkipInt(input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 2122 | SkipInt(input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E; |
wolfSSL | 15:117db924cf7c | 2123 | #endif |
wolfSSL | 15:117db924cf7c | 2124 | |
wolfSSL | 15:117db924cf7c | 2125 | #ifdef WOLFSSL_XILINX_CRYPT |
wolfSSL | 15:117db924cf7c | 2126 | if (wc_InitRsaHw(key) != 0) { |
wolfSSL | 15:117db924cf7c | 2127 | return BAD_STATE_E; |
wolfSSL | 15:117db924cf7c | 2128 | } |
wolfSSL | 15:117db924cf7c | 2129 | #endif |
wolfSSL | 15:117db924cf7c | 2130 | |
wolfSSL | 15:117db924cf7c | 2131 | return 0; |
wolfSSL | 15:117db924cf7c | 2132 | } |
wolfSSL | 15:117db924cf7c | 2133 | #endif /* HAVE_USER_RSA */ |
wolfSSL | 15:117db924cf7c | 2134 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 2135 | |
wolfSSL | 15:117db924cf7c | 2136 | /* Remove PKCS8 header, place inOutIdx at beginning of traditional, |
wolfSSL | 15:117db924cf7c | 2137 | * return traditional length on success, negative on error */ |
wolfSSL | 15:117db924cf7c | 2138 | int ToTraditionalInline(const byte* input, word32* inOutIdx, word32 sz) |
wolfSSL | 15:117db924cf7c | 2139 | { |
wolfSSL | 15:117db924cf7c | 2140 | word32 idx, oid; |
wolfSSL | 15:117db924cf7c | 2141 | int version, length; |
wolfSSL | 15:117db924cf7c | 2142 | int ret; |
wolfSSL | 15:117db924cf7c | 2143 | |
wolfSSL | 15:117db924cf7c | 2144 | if (input == NULL || inOutIdx == NULL) |
wolfSSL | 15:117db924cf7c | 2145 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2146 | |
wolfSSL | 15:117db924cf7c | 2147 | idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 2148 | |
wolfSSL | 15:117db924cf7c | 2149 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 2150 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 2151 | |
wolfSSL | 15:117db924cf7c | 2152 | if (GetMyVersion(input, &idx, &version, sz) < 0) |
wolfSSL | 15:117db924cf7c | 2153 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 2154 | |
wolfSSL | 15:117db924cf7c | 2155 | if (GetAlgoId(input, &idx, &oid, oidKeyType, sz) < 0) |
wolfSSL | 15:117db924cf7c | 2156 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 2157 | |
wolfSSL | 15:117db924cf7c | 2158 | if (input[idx] == ASN_OBJECT_ID) { |
wolfSSL | 15:117db924cf7c | 2159 | if (SkipObjectId(input, &idx, sz) < 0) |
wolfSSL | 15:117db924cf7c | 2160 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 2161 | } |
wolfSSL | 15:117db924cf7c | 2162 | |
wolfSSL | 15:117db924cf7c | 2163 | ret = GetOctetString(input, &idx, &length, sz); |
wolfSSL | 15:117db924cf7c | 2164 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 2165 | return ret; |
wolfSSL | 15:117db924cf7c | 2166 | |
wolfSSL | 15:117db924cf7c | 2167 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 2168 | |
wolfSSL | 15:117db924cf7c | 2169 | return length; |
wolfSSL | 15:117db924cf7c | 2170 | } |
wolfSSL | 15:117db924cf7c | 2171 | |
wolfSSL | 15:117db924cf7c | 2172 | /* Remove PKCS8 header, move beginning of traditional to beginning of input */ |
wolfSSL | 15:117db924cf7c | 2173 | int ToTraditional(byte* input, word32 sz) |
wolfSSL | 15:117db924cf7c | 2174 | { |
wolfSSL | 15:117db924cf7c | 2175 | word32 inOutIdx = 0; |
wolfSSL | 15:117db924cf7c | 2176 | int length; |
wolfSSL | 15:117db924cf7c | 2177 | |
wolfSSL | 15:117db924cf7c | 2178 | if (input == NULL) |
wolfSSL | 15:117db924cf7c | 2179 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2180 | |
wolfSSL | 15:117db924cf7c | 2181 | length = ToTraditionalInline(input, &inOutIdx, sz); |
wolfSSL | 15:117db924cf7c | 2182 | if (length < 0) |
wolfSSL | 15:117db924cf7c | 2183 | return length; |
wolfSSL | 15:117db924cf7c | 2184 | |
wolfSSL | 15:117db924cf7c | 2185 | XMEMMOVE(input, input + inOutIdx, length); |
wolfSSL | 15:117db924cf7c | 2186 | |
wolfSSL | 15:117db924cf7c | 2187 | return length; |
wolfSSL | 15:117db924cf7c | 2188 | } |
wolfSSL | 15:117db924cf7c | 2189 | |
wolfSSL | 15:117db924cf7c | 2190 | |
wolfSSL | 15:117db924cf7c | 2191 | /* find beginning of traditional key inside PKCS#8 unencrypted buffer |
wolfSSL | 15:117db924cf7c | 2192 | * return traditional length on success, with inOutIdx at beginning of |
wolfSSL | 15:117db924cf7c | 2193 | * traditional |
wolfSSL | 15:117db924cf7c | 2194 | * return negative on failure/error */ |
wolfSSL | 15:117db924cf7c | 2195 | int wc_GetPkcs8TraditionalOffset(byte* input, word32* inOutIdx, word32 sz) |
wolfSSL | 15:117db924cf7c | 2196 | { |
wolfSSL | 15:117db924cf7c | 2197 | int length; |
wolfSSL | 15:117db924cf7c | 2198 | |
wolfSSL | 15:117db924cf7c | 2199 | if (input == NULL || inOutIdx == NULL || (*inOutIdx > sz)) |
wolfSSL | 15:117db924cf7c | 2200 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2201 | |
wolfSSL | 15:117db924cf7c | 2202 | length = ToTraditionalInline(input, inOutIdx, sz); |
wolfSSL | 15:117db924cf7c | 2203 | |
wolfSSL | 15:117db924cf7c | 2204 | return length; |
wolfSSL | 15:117db924cf7c | 2205 | } |
wolfSSL | 15:117db924cf7c | 2206 | |
wolfSSL | 15:117db924cf7c | 2207 | |
wolfSSL | 15:117db924cf7c | 2208 | /* PKCS#8 from RFC 5208 |
wolfSSL | 15:117db924cf7c | 2209 | * This function takes in a DER key and converts it to PKCS#8 format. Used |
wolfSSL | 15:117db924cf7c | 2210 | * in creating PKCS#12 shrouded key bags. |
wolfSSL | 15:117db924cf7c | 2211 | * Reverse of ToTraditional |
wolfSSL | 15:117db924cf7c | 2212 | * |
wolfSSL | 15:117db924cf7c | 2213 | * PrivateKeyInfo ::= SEQUENCE { |
wolfSSL | 15:117db924cf7c | 2214 | * version Version, |
wolfSSL | 15:117db924cf7c | 2215 | * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, |
wolfSSL | 15:117db924cf7c | 2216 | * privateKey PrivateKey, |
wolfSSL | 15:117db924cf7c | 2217 | * attributes optional |
wolfSSL | 15:117db924cf7c | 2218 | * } |
wolfSSL | 15:117db924cf7c | 2219 | * Version ::= INTEGER |
wolfSSL | 15:117db924cf7c | 2220 | * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier |
wolfSSL | 15:117db924cf7c | 2221 | * PrivateKey ::= OCTET STRING |
wolfSSL | 15:117db924cf7c | 2222 | * |
wolfSSL | 15:117db924cf7c | 2223 | * out buffer to place result in |
wolfSSL | 15:117db924cf7c | 2224 | * outSz size of out buffer |
wolfSSL | 15:117db924cf7c | 2225 | * key buffer with DER key |
wolfSSL | 15:117db924cf7c | 2226 | * keySz size of key buffer |
wolfSSL | 15:117db924cf7c | 2227 | * algoID algorithm ID i.e. RSAk |
wolfSSL | 15:117db924cf7c | 2228 | * curveOID ECC curve oid if used. Should be NULL for RSA keys. |
wolfSSL | 15:117db924cf7c | 2229 | * oidSz size of curve oid. Is set to 0 if curveOID is NULL. |
wolfSSL | 15:117db924cf7c | 2230 | * |
wolfSSL | 15:117db924cf7c | 2231 | * Returns the size of PKCS#8 placed into out. In error cases returns negative |
wolfSSL | 15:117db924cf7c | 2232 | * values. |
wolfSSL | 15:117db924cf7c | 2233 | */ |
wolfSSL | 15:117db924cf7c | 2234 | int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz, |
wolfSSL | 15:117db924cf7c | 2235 | int algoID, const byte* curveOID, word32 oidSz) |
wolfSSL | 15:117db924cf7c | 2236 | { |
wolfSSL | 15:117db924cf7c | 2237 | word32 keyIdx = 0; |
wolfSSL | 15:117db924cf7c | 2238 | word32 tmpSz = 0; |
wolfSSL | 15:117db924cf7c | 2239 | word32 sz; |
wolfSSL | 15:117db924cf7c | 2240 | |
wolfSSL | 15:117db924cf7c | 2241 | |
wolfSSL | 15:117db924cf7c | 2242 | /* If out is NULL then return the max size needed |
wolfSSL | 15:117db924cf7c | 2243 | * + 2 for ASN_OBJECT_ID and ASN_OCTET_STRING tags */ |
wolfSSL | 15:117db924cf7c | 2244 | if (out == NULL && outSz != NULL) { |
wolfSSL | 15:117db924cf7c | 2245 | *outSz = keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ |
wolfSSL | 15:117db924cf7c | 2246 | + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2; |
wolfSSL | 15:117db924cf7c | 2247 | |
wolfSSL | 15:117db924cf7c | 2248 | if (curveOID != NULL) |
wolfSSL | 15:117db924cf7c | 2249 | *outSz += oidSz + MAX_LENGTH_SZ + 1; |
wolfSSL | 15:117db924cf7c | 2250 | |
wolfSSL | 15:117db924cf7c | 2251 | WOLFSSL_MSG("Checking size of PKCS8"); |
wolfSSL | 15:117db924cf7c | 2252 | |
wolfSSL | 15:117db924cf7c | 2253 | return LENGTH_ONLY_E; |
wolfSSL | 15:117db924cf7c | 2254 | } |
wolfSSL | 15:117db924cf7c | 2255 | |
wolfSSL | 15:117db924cf7c | 2256 | WOLFSSL_ENTER("wc_CreatePKCS8Key()"); |
wolfSSL | 15:117db924cf7c | 2257 | |
wolfSSL | 15:117db924cf7c | 2258 | if (key == NULL || out == NULL || outSz == NULL) { |
wolfSSL | 15:117db924cf7c | 2259 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2260 | } |
wolfSSL | 15:117db924cf7c | 2261 | |
wolfSSL | 15:117db924cf7c | 2262 | /* check the buffer has enough room for largest possible size */ |
wolfSSL | 15:117db924cf7c | 2263 | if (curveOID != NULL) { |
wolfSSL | 15:117db924cf7c | 2264 | if (*outSz < (keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ |
wolfSSL | 15:117db924cf7c | 2265 | + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 3 + oidSz + MAX_LENGTH_SZ)) |
wolfSSL | 15:117db924cf7c | 2266 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 2267 | } |
wolfSSL | 15:117db924cf7c | 2268 | else { |
wolfSSL | 15:117db924cf7c | 2269 | oidSz = 0; /* with no curveOID oid size must be 0 */ |
wolfSSL | 15:117db924cf7c | 2270 | if (*outSz < (keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ |
wolfSSL | 15:117db924cf7c | 2271 | + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2)) |
wolfSSL | 15:117db924cf7c | 2272 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 2273 | } |
wolfSSL | 15:117db924cf7c | 2274 | |
wolfSSL | 15:117db924cf7c | 2275 | /* PrivateKeyInfo ::= SEQUENCE */ |
wolfSSL | 15:117db924cf7c | 2276 | keyIdx += MAX_SEQ_SZ; /* save room for sequence */ |
wolfSSL | 15:117db924cf7c | 2277 | |
wolfSSL | 15:117db924cf7c | 2278 | /* version Version |
wolfSSL | 15:117db924cf7c | 2279 | * no header information just INTEGER */ |
wolfSSL | 15:117db924cf7c | 2280 | sz = SetMyVersion(PKCS8v0, out + keyIdx, 0); |
wolfSSL | 15:117db924cf7c | 2281 | tmpSz += sz; keyIdx += sz; |
wolfSSL | 15:117db924cf7c | 2282 | |
wolfSSL | 15:117db924cf7c | 2283 | /* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier */ |
wolfSSL | 15:117db924cf7c | 2284 | sz = 0; /* set sz to 0 and get privateKey oid buffer size needed */ |
wolfSSL | 15:117db924cf7c | 2285 | if (curveOID != NULL && oidSz > 0) { |
wolfSSL | 15:117db924cf7c | 2286 | byte buf[MAX_LENGTH_SZ]; |
wolfSSL | 15:117db924cf7c | 2287 | sz = SetLength(oidSz, buf); |
wolfSSL | 15:117db924cf7c | 2288 | sz += 1; /* plus one for ASN object id */ |
wolfSSL | 15:117db924cf7c | 2289 | } |
wolfSSL | 15:117db924cf7c | 2290 | sz = SetAlgoID(algoID, out + keyIdx, oidKeyType, oidSz + sz); |
wolfSSL | 15:117db924cf7c | 2291 | tmpSz += sz; keyIdx += sz; |
wolfSSL | 15:117db924cf7c | 2292 | |
wolfSSL | 15:117db924cf7c | 2293 | /* privateKey PrivateKey * |
wolfSSL | 15:117db924cf7c | 2294 | * pkcs8 ecc uses slightly different format. Places curve oid in |
wolfSSL | 15:117db924cf7c | 2295 | * buffer */ |
wolfSSL | 15:117db924cf7c | 2296 | if (curveOID != NULL && oidSz > 0) { |
wolfSSL | 15:117db924cf7c | 2297 | sz = SetObjectId(oidSz, out + keyIdx); |
wolfSSL | 15:117db924cf7c | 2298 | keyIdx += sz; tmpSz += sz; |
wolfSSL | 15:117db924cf7c | 2299 | XMEMCPY(out + keyIdx, curveOID, oidSz); |
wolfSSL | 15:117db924cf7c | 2300 | keyIdx += oidSz; tmpSz += oidSz; |
wolfSSL | 15:117db924cf7c | 2301 | } |
wolfSSL | 15:117db924cf7c | 2302 | |
wolfSSL | 15:117db924cf7c | 2303 | sz = SetOctetString(keySz, out + keyIdx); |
wolfSSL | 15:117db924cf7c | 2304 | keyIdx += sz; tmpSz += sz; |
wolfSSL | 15:117db924cf7c | 2305 | XMEMCPY(out + keyIdx, key, keySz); |
wolfSSL | 15:117db924cf7c | 2306 | tmpSz += keySz; |
wolfSSL | 15:117db924cf7c | 2307 | |
wolfSSL | 15:117db924cf7c | 2308 | /* attributes optional |
wolfSSL | 15:117db924cf7c | 2309 | * No attributes currently added */ |
wolfSSL | 15:117db924cf7c | 2310 | |
wolfSSL | 15:117db924cf7c | 2311 | /* rewind and add sequence */ |
wolfSSL | 15:117db924cf7c | 2312 | sz = SetSequence(tmpSz, out); |
wolfSSL | 15:117db924cf7c | 2313 | XMEMMOVE(out + sz, out + MAX_SEQ_SZ, tmpSz); |
wolfSSL | 15:117db924cf7c | 2314 | |
wolfSSL | 15:117db924cf7c | 2315 | return tmpSz + sz; |
wolfSSL | 15:117db924cf7c | 2316 | } |
wolfSSL | 15:117db924cf7c | 2317 | |
wolfSSL | 15:117db924cf7c | 2318 | |
wolfSSL | 15:117db924cf7c | 2319 | /* check that the private key is a pair for the public key in certificate |
wolfSSL | 15:117db924cf7c | 2320 | * return 1 (true) on match |
wolfSSL | 15:117db924cf7c | 2321 | * return 0 or negative value on failure/error |
wolfSSL | 15:117db924cf7c | 2322 | * |
wolfSSL | 15:117db924cf7c | 2323 | * key : buffer holding DER fromat key |
wolfSSL | 15:117db924cf7c | 2324 | * keySz : size of key buffer |
wolfSSL | 15:117db924cf7c | 2325 | * der : a initialized and parsed DecodedCert holding a certificate */ |
wolfSSL | 15:117db924cf7c | 2326 | int wc_CheckPrivateKey(byte* key, word32 keySz, DecodedCert* der) |
wolfSSL | 15:117db924cf7c | 2327 | { |
wolfSSL | 15:117db924cf7c | 2328 | int ret; |
wolfSSL | 15:117db924cf7c | 2329 | (void)keySz; |
wolfSSL | 15:117db924cf7c | 2330 | |
wolfSSL | 15:117db924cf7c | 2331 | if (key == NULL || der == NULL) { |
wolfSSL | 15:117db924cf7c | 2332 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2333 | } |
wolfSSL | 15:117db924cf7c | 2334 | |
wolfSSL | 15:117db924cf7c | 2335 | #if !defined(NO_RSA) |
wolfSSL | 15:117db924cf7c | 2336 | /* test if RSA key */ |
wolfSSL | 15:117db924cf7c | 2337 | if (der->keyOID == RSAk) { |
wolfSSL | 15:117db924cf7c | 2338 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2339 | RsaKey* a = NULL; |
wolfSSL | 15:117db924cf7c | 2340 | RsaKey* b = NULL; |
wolfSSL | 15:117db924cf7c | 2341 | #else |
wolfSSL | 15:117db924cf7c | 2342 | RsaKey a[1], b[1]; |
wolfSSL | 15:117db924cf7c | 2343 | #endif |
wolfSSL | 15:117db924cf7c | 2344 | word32 keyIdx = 0; |
wolfSSL | 15:117db924cf7c | 2345 | |
wolfSSL | 15:117db924cf7c | 2346 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2347 | a = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2348 | if (a == NULL) |
wolfSSL | 15:117db924cf7c | 2349 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2350 | b = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2351 | if (b == NULL) { |
wolfSSL | 15:117db924cf7c | 2352 | XFREE(a, NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2353 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2354 | } |
wolfSSL | 15:117db924cf7c | 2355 | #endif |
wolfSSL | 15:117db924cf7c | 2356 | |
wolfSSL | 15:117db924cf7c | 2357 | if ((ret = wc_InitRsaKey(a, NULL)) < 0) { |
wolfSSL | 15:117db924cf7c | 2358 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2359 | XFREE(b, NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2360 | XFREE(a, NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2361 | #endif |
wolfSSL | 15:117db924cf7c | 2362 | return ret; |
wolfSSL | 15:117db924cf7c | 2363 | } |
wolfSSL | 15:117db924cf7c | 2364 | if ((ret = wc_InitRsaKey(b, NULL)) < 0) { |
wolfSSL | 15:117db924cf7c | 2365 | wc_FreeRsaKey(a); |
wolfSSL | 15:117db924cf7c | 2366 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2367 | XFREE(b, NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2368 | XFREE(a, NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2369 | #endif |
wolfSSL | 15:117db924cf7c | 2370 | return ret; |
wolfSSL | 15:117db924cf7c | 2371 | } |
wolfSSL | 15:117db924cf7c | 2372 | if ((ret = wc_RsaPrivateKeyDecode(key, &keyIdx, a, keySz)) == 0) { |
wolfSSL | 15:117db924cf7c | 2373 | WOLFSSL_MSG("Checking RSA key pair"); |
wolfSSL | 15:117db924cf7c | 2374 | keyIdx = 0; /* reset to 0 for parsing public key */ |
wolfSSL | 15:117db924cf7c | 2375 | |
wolfSSL | 15:117db924cf7c | 2376 | if ((ret = wc_RsaPublicKeyDecode(der->publicKey, &keyIdx, b, |
wolfSSL | 15:117db924cf7c | 2377 | der->pubKeySize)) == 0) { |
wolfSSL | 15:117db924cf7c | 2378 | /* limit for user RSA crypto because of RsaKey |
wolfSSL | 15:117db924cf7c | 2379 | * dereference. */ |
wolfSSL | 15:117db924cf7c | 2380 | #if defined(HAVE_USER_RSA) |
wolfSSL | 15:117db924cf7c | 2381 | WOLFSSL_MSG("Cannot verify RSA pair with user RSA"); |
wolfSSL | 15:117db924cf7c | 2382 | ret = 1; /* return first RSA cert as match */ |
wolfSSL | 15:117db924cf7c | 2383 | #else |
wolfSSL | 15:117db924cf7c | 2384 | /* both keys extracted successfully now check n and e |
wolfSSL | 15:117db924cf7c | 2385 | * values are the same. This is dereferencing RsaKey */ |
wolfSSL | 15:117db924cf7c | 2386 | if (mp_cmp(&(a->n), &(b->n)) != MP_EQ || |
wolfSSL | 15:117db924cf7c | 2387 | mp_cmp(&(a->e), &(b->e)) != MP_EQ) { |
wolfSSL | 15:117db924cf7c | 2388 | ret = MP_CMP_E; |
wolfSSL | 15:117db924cf7c | 2389 | } |
wolfSSL | 15:117db924cf7c | 2390 | else |
wolfSSL | 15:117db924cf7c | 2391 | ret = 1; |
wolfSSL | 15:117db924cf7c | 2392 | #endif |
wolfSSL | 15:117db924cf7c | 2393 | } |
wolfSSL | 15:117db924cf7c | 2394 | } |
wolfSSL | 15:117db924cf7c | 2395 | wc_FreeRsaKey(b); |
wolfSSL | 15:117db924cf7c | 2396 | wc_FreeRsaKey(a); |
wolfSSL | 15:117db924cf7c | 2397 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2398 | XFREE(b, NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2399 | XFREE(a, NULL, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 2400 | #endif |
wolfSSL | 15:117db924cf7c | 2401 | } |
wolfSSL | 15:117db924cf7c | 2402 | else |
wolfSSL | 15:117db924cf7c | 2403 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 2404 | |
wolfSSL | 15:117db924cf7c | 2405 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 2406 | if (der->keyOID == ECDSAk) { |
wolfSSL | 15:117db924cf7c | 2407 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2408 | ecc_key* key_pair = NULL; |
wolfSSL | 15:117db924cf7c | 2409 | byte* privDer; |
wolfSSL | 15:117db924cf7c | 2410 | #else |
wolfSSL | 15:117db924cf7c | 2411 | ecc_key key_pair[1]; |
wolfSSL | 15:117db924cf7c | 2412 | byte privDer[MAX_ECC_BYTES]; |
wolfSSL | 15:117db924cf7c | 2413 | #endif |
wolfSSL | 15:117db924cf7c | 2414 | word32 privSz = MAX_ECC_BYTES; |
wolfSSL | 15:117db924cf7c | 2415 | word32 keyIdx = 0; |
wolfSSL | 15:117db924cf7c | 2416 | |
wolfSSL | 15:117db924cf7c | 2417 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2418 | key_pair = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 2419 | if (key_pair == NULL) |
wolfSSL | 15:117db924cf7c | 2420 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2421 | privDer = (byte*)XMALLOC(MAX_ECC_BYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2422 | if (privDer == NULL) { |
wolfSSL | 15:117db924cf7c | 2423 | XFREE(key_pair, NULL, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 2424 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2425 | } |
wolfSSL | 15:117db924cf7c | 2426 | #endif |
wolfSSL | 15:117db924cf7c | 2427 | |
wolfSSL | 15:117db924cf7c | 2428 | if ((ret = wc_ecc_init(key_pair)) < 0) { |
wolfSSL | 15:117db924cf7c | 2429 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2430 | XFREE(privDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2431 | XFREE(key_pair, NULL, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 2432 | #endif |
wolfSSL | 15:117db924cf7c | 2433 | return ret; |
wolfSSL | 15:117db924cf7c | 2434 | } |
wolfSSL | 15:117db924cf7c | 2435 | |
wolfSSL | 15:117db924cf7c | 2436 | if ((ret = wc_EccPrivateKeyDecode(key, &keyIdx, key_pair, |
wolfSSL | 15:117db924cf7c | 2437 | keySz)) == 0) { |
wolfSSL | 15:117db924cf7c | 2438 | WOLFSSL_MSG("Checking ECC key pair"); |
wolfSSL | 15:117db924cf7c | 2439 | |
wolfSSL | 15:117db924cf7c | 2440 | if ((ret = wc_ecc_export_private_only(key_pair, privDer, &privSz)) |
wolfSSL | 15:117db924cf7c | 2441 | == 0) { |
wolfSSL | 15:117db924cf7c | 2442 | wc_ecc_free(key_pair); |
wolfSSL | 15:117db924cf7c | 2443 | ret = wc_ecc_init(key_pair); |
wolfSSL | 15:117db924cf7c | 2444 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 2445 | ret = wc_ecc_import_private_key((const byte*)privDer, |
wolfSSL | 15:117db924cf7c | 2446 | privSz, (const byte*)der->publicKey, |
wolfSSL | 15:117db924cf7c | 2447 | der->pubKeySize, key_pair); |
wolfSSL | 15:117db924cf7c | 2448 | } |
wolfSSL | 15:117db924cf7c | 2449 | |
wolfSSL | 15:117db924cf7c | 2450 | /* public and private extracted successfuly now check if is |
wolfSSL | 15:117db924cf7c | 2451 | * a pair and also do sanity checks on key. wc_ecc_check_key |
wolfSSL | 15:117db924cf7c | 2452 | * checks that private * base generator equals pubkey */ |
wolfSSL | 15:117db924cf7c | 2453 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 2454 | if ((ret = wc_ecc_check_key(key_pair)) == 0) { |
wolfSSL | 15:117db924cf7c | 2455 | ret = 1; |
wolfSSL | 15:117db924cf7c | 2456 | } |
wolfSSL | 15:117db924cf7c | 2457 | } |
wolfSSL | 15:117db924cf7c | 2458 | ForceZero(privDer, privSz); |
wolfSSL | 15:117db924cf7c | 2459 | } |
wolfSSL | 15:117db924cf7c | 2460 | } |
wolfSSL | 15:117db924cf7c | 2461 | wc_ecc_free(key_pair); |
wolfSSL | 15:117db924cf7c | 2462 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2463 | XFREE(privDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2464 | XFREE(key_pair, NULL, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 2465 | #endif |
wolfSSL | 15:117db924cf7c | 2466 | } |
wolfSSL | 15:117db924cf7c | 2467 | else |
wolfSSL | 15:117db924cf7c | 2468 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 2469 | |
wolfSSL | 15:117db924cf7c | 2470 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 2471 | if (der->keyOID == ED25519k) { |
wolfSSL | 15:117db924cf7c | 2472 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2473 | ed25519_key* key_pair = NULL; |
wolfSSL | 15:117db924cf7c | 2474 | #else |
wolfSSL | 15:117db924cf7c | 2475 | ed25519_key key_pair[1]; |
wolfSSL | 15:117db924cf7c | 2476 | #endif |
wolfSSL | 15:117db924cf7c | 2477 | word32 keyIdx = 0; |
wolfSSL | 15:117db924cf7c | 2478 | |
wolfSSL | 15:117db924cf7c | 2479 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2480 | key_pair = (ed25519_key*)XMALLOC(sizeof(ed25519_key), NULL, |
wolfSSL | 15:117db924cf7c | 2481 | DYNAMIC_TYPE_ED25519); |
wolfSSL | 15:117db924cf7c | 2482 | if (key_pair == NULL) |
wolfSSL | 15:117db924cf7c | 2483 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2484 | #endif |
wolfSSL | 15:117db924cf7c | 2485 | |
wolfSSL | 15:117db924cf7c | 2486 | if ((ret = wc_ed25519_init(key_pair)) < 0) { |
wolfSSL | 15:117db924cf7c | 2487 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2488 | XFREE(key_pair, NULL, DYNAMIC_TYPE_ED25519); |
wolfSSL | 15:117db924cf7c | 2489 | #endif |
wolfSSL | 15:117db924cf7c | 2490 | return ret; |
wolfSSL | 15:117db924cf7c | 2491 | } |
wolfSSL | 15:117db924cf7c | 2492 | if ((ret = wc_Ed25519PrivateKeyDecode(key, &keyIdx, key_pair, |
wolfSSL | 15:117db924cf7c | 2493 | keySz)) == 0) { |
wolfSSL | 15:117db924cf7c | 2494 | WOLFSSL_MSG("Checking ED25519 key pair"); |
wolfSSL | 15:117db924cf7c | 2495 | keyIdx = 0; |
wolfSSL | 15:117db924cf7c | 2496 | if ((ret = wc_ed25519_import_public(der->publicKey, der->pubKeySize, |
wolfSSL | 15:117db924cf7c | 2497 | key_pair)) == 0) { |
wolfSSL | 15:117db924cf7c | 2498 | /* public and private extracted successfuly no check if is |
wolfSSL | 15:117db924cf7c | 2499 | * a pair and also do sanity checks on key. wc_ecc_check_key |
wolfSSL | 15:117db924cf7c | 2500 | * checks that private * base generator equals pubkey */ |
wolfSSL | 15:117db924cf7c | 2501 | if ((ret = wc_ed25519_check_key(key_pair)) == 0) |
wolfSSL | 15:117db924cf7c | 2502 | ret = 1; |
wolfSSL | 15:117db924cf7c | 2503 | } |
wolfSSL | 15:117db924cf7c | 2504 | } |
wolfSSL | 15:117db924cf7c | 2505 | wc_ed25519_free(key_pair); |
wolfSSL | 15:117db924cf7c | 2506 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2507 | XFREE(key_pair, NULL, DYNAMIC_TYPE_ED25519); |
wolfSSL | 15:117db924cf7c | 2508 | #endif |
wolfSSL | 15:117db924cf7c | 2509 | } |
wolfSSL | 15:117db924cf7c | 2510 | else |
wolfSSL | 15:117db924cf7c | 2511 | #endif |
wolfSSL | 15:117db924cf7c | 2512 | { |
wolfSSL | 15:117db924cf7c | 2513 | ret = 0; |
wolfSSL | 15:117db924cf7c | 2514 | } |
wolfSSL | 15:117db924cf7c | 2515 | |
wolfSSL | 15:117db924cf7c | 2516 | (void)keySz; |
wolfSSL | 15:117db924cf7c | 2517 | |
wolfSSL | 15:117db924cf7c | 2518 | return ret; |
wolfSSL | 15:117db924cf7c | 2519 | } |
wolfSSL | 15:117db924cf7c | 2520 | |
wolfSSL | 15:117db924cf7c | 2521 | #ifndef NO_PWDBASED |
wolfSSL | 15:117db924cf7c | 2522 | |
wolfSSL | 15:117db924cf7c | 2523 | /* Check To see if PKCS version algo is supported, set id if it is return 0 |
wolfSSL | 15:117db924cf7c | 2524 | < 0 on error */ |
wolfSSL | 15:117db924cf7c | 2525 | static int CheckAlgo(int first, int second, int* id, int* version) |
wolfSSL | 15:117db924cf7c | 2526 | { |
wolfSSL | 15:117db924cf7c | 2527 | *id = ALGO_ID_E; |
wolfSSL | 15:117db924cf7c | 2528 | *version = PKCS5; /* default */ |
wolfSSL | 15:117db924cf7c | 2529 | |
wolfSSL | 15:117db924cf7c | 2530 | if (first == 1) { |
wolfSSL | 15:117db924cf7c | 2531 | switch (second) { |
wolfSSL | 15:117db924cf7c | 2532 | #if !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 2533 | #ifndef NO_RC4 |
wolfSSL | 15:117db924cf7c | 2534 | case PBE_SHA1_RC4_128: |
wolfSSL | 15:117db924cf7c | 2535 | *id = PBE_SHA1_RC4_128; |
wolfSSL | 15:117db924cf7c | 2536 | *version = PKCS12v1; |
wolfSSL | 15:117db924cf7c | 2537 | return 0; |
wolfSSL | 15:117db924cf7c | 2538 | #endif |
wolfSSL | 15:117db924cf7c | 2539 | #ifndef NO_DES3 |
wolfSSL | 15:117db924cf7c | 2540 | case PBE_SHA1_DES3: |
wolfSSL | 15:117db924cf7c | 2541 | *id = PBE_SHA1_DES3; |
wolfSSL | 15:117db924cf7c | 2542 | *version = PKCS12v1; |
wolfSSL | 15:117db924cf7c | 2543 | return 0; |
wolfSSL | 15:117db924cf7c | 2544 | #endif |
wolfSSL | 15:117db924cf7c | 2545 | #endif /* !NO_SHA */ |
wolfSSL | 15:117db924cf7c | 2546 | default: |
wolfSSL | 15:117db924cf7c | 2547 | return ALGO_ID_E; |
wolfSSL | 15:117db924cf7c | 2548 | } |
wolfSSL | 15:117db924cf7c | 2549 | } |
wolfSSL | 15:117db924cf7c | 2550 | |
wolfSSL | 15:117db924cf7c | 2551 | if (first != PKCS5) |
wolfSSL | 15:117db924cf7c | 2552 | return ASN_INPUT_E; /* VERSION ERROR */ |
wolfSSL | 15:117db924cf7c | 2553 | |
wolfSSL | 15:117db924cf7c | 2554 | if (second == PBES2) { |
wolfSSL | 15:117db924cf7c | 2555 | *version = PKCS5v2; |
wolfSSL | 15:117db924cf7c | 2556 | return 0; |
wolfSSL | 15:117db924cf7c | 2557 | } |
wolfSSL | 15:117db924cf7c | 2558 | |
wolfSSL | 15:117db924cf7c | 2559 | switch (second) { |
wolfSSL | 15:117db924cf7c | 2560 | #ifndef NO_DES3 |
wolfSSL | 15:117db924cf7c | 2561 | #ifndef NO_MD5 |
wolfSSL | 15:117db924cf7c | 2562 | case 3: /* see RFC 2898 for ids */ |
wolfSSL | 15:117db924cf7c | 2563 | *id = PBE_MD5_DES; |
wolfSSL | 15:117db924cf7c | 2564 | return 0; |
wolfSSL | 15:117db924cf7c | 2565 | #endif |
wolfSSL | 15:117db924cf7c | 2566 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 2567 | case 10: |
wolfSSL | 15:117db924cf7c | 2568 | *id = PBE_SHA1_DES; |
wolfSSL | 15:117db924cf7c | 2569 | return 0; |
wolfSSL | 15:117db924cf7c | 2570 | #endif |
wolfSSL | 15:117db924cf7c | 2571 | #endif /* !NO_DES3 */ |
wolfSSL | 15:117db924cf7c | 2572 | default: |
wolfSSL | 15:117db924cf7c | 2573 | return ALGO_ID_E; |
wolfSSL | 15:117db924cf7c | 2574 | |
wolfSSL | 15:117db924cf7c | 2575 | } |
wolfSSL | 15:117db924cf7c | 2576 | } |
wolfSSL | 15:117db924cf7c | 2577 | |
wolfSSL | 15:117db924cf7c | 2578 | |
wolfSSL | 15:117db924cf7c | 2579 | /* Check To see if PKCS v2 algo is supported, set id if it is return 0 |
wolfSSL | 15:117db924cf7c | 2580 | < 0 on error */ |
wolfSSL | 15:117db924cf7c | 2581 | static int CheckAlgoV2(int oid, int* id) |
wolfSSL | 15:117db924cf7c | 2582 | { |
wolfSSL | 15:117db924cf7c | 2583 | (void)id; /* not used if AES and DES3 disabled */ |
wolfSSL | 15:117db924cf7c | 2584 | switch (oid) { |
wolfSSL | 15:117db924cf7c | 2585 | #if !defined(NO_DES3) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 2586 | case DESb: |
wolfSSL | 15:117db924cf7c | 2587 | *id = PBE_SHA1_DES; |
wolfSSL | 15:117db924cf7c | 2588 | return 0; |
wolfSSL | 15:117db924cf7c | 2589 | case DES3b: |
wolfSSL | 15:117db924cf7c | 2590 | *id = PBE_SHA1_DES3; |
wolfSSL | 15:117db924cf7c | 2591 | return 0; |
wolfSSL | 15:117db924cf7c | 2592 | #endif |
wolfSSL | 15:117db924cf7c | 2593 | #ifdef WOLFSSL_AES_256 |
wolfSSL | 15:117db924cf7c | 2594 | case AES256CBCb: |
wolfSSL | 15:117db924cf7c | 2595 | *id = PBE_AES256_CBC; |
wolfSSL | 15:117db924cf7c | 2596 | return 0; |
wolfSSL | 15:117db924cf7c | 2597 | #endif |
wolfSSL | 15:117db924cf7c | 2598 | default: |
wolfSSL | 15:117db924cf7c | 2599 | return ALGO_ID_E; |
wolfSSL | 15:117db924cf7c | 2600 | |
wolfSSL | 15:117db924cf7c | 2601 | } |
wolfSSL | 15:117db924cf7c | 2602 | } |
wolfSSL | 15:117db924cf7c | 2603 | |
wolfSSL | 15:117db924cf7c | 2604 | |
wolfSSL | 15:117db924cf7c | 2605 | int wc_GetKeyOID(byte* key, word32 keySz, const byte** curveOID, word32* oidSz, |
wolfSSL | 15:117db924cf7c | 2606 | int* algoID, void* heap) |
wolfSSL | 15:117db924cf7c | 2607 | { |
wolfSSL | 15:117db924cf7c | 2608 | word32 tmpIdx = 0; |
wolfSSL | 15:117db924cf7c | 2609 | |
wolfSSL | 15:117db924cf7c | 2610 | if (key == NULL || algoID == NULL) |
wolfSSL | 15:117db924cf7c | 2611 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2612 | |
wolfSSL | 15:117db924cf7c | 2613 | *algoID = 0; |
wolfSSL | 15:117db924cf7c | 2614 | |
wolfSSL | 15:117db924cf7c | 2615 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 2616 | { |
wolfSSL | 15:117db924cf7c | 2617 | RsaKey rsa; |
wolfSSL | 15:117db924cf7c | 2618 | |
wolfSSL | 15:117db924cf7c | 2619 | wc_InitRsaKey(&rsa, heap); |
wolfSSL | 15:117db924cf7c | 2620 | if (wc_RsaPrivateKeyDecode(key, &tmpIdx, &rsa, keySz) == 0) { |
wolfSSL | 15:117db924cf7c | 2621 | *algoID = RSAk; |
wolfSSL | 15:117db924cf7c | 2622 | } |
wolfSSL | 15:117db924cf7c | 2623 | else { |
wolfSSL | 15:117db924cf7c | 2624 | WOLFSSL_MSG("Not RSA DER key"); |
wolfSSL | 15:117db924cf7c | 2625 | } |
wolfSSL | 15:117db924cf7c | 2626 | wc_FreeRsaKey(&rsa); |
wolfSSL | 15:117db924cf7c | 2627 | } |
wolfSSL | 15:117db924cf7c | 2628 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 2629 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 2630 | if (*algoID == 0) { |
wolfSSL | 15:117db924cf7c | 2631 | ecc_key ecc; |
wolfSSL | 15:117db924cf7c | 2632 | |
wolfSSL | 15:117db924cf7c | 2633 | tmpIdx = 0; |
wolfSSL | 15:117db924cf7c | 2634 | wc_ecc_init_ex(&ecc, heap, INVALID_DEVID); |
wolfSSL | 15:117db924cf7c | 2635 | if (wc_EccPrivateKeyDecode(key, &tmpIdx, &ecc, keySz) == 0) { |
wolfSSL | 15:117db924cf7c | 2636 | *algoID = ECDSAk; |
wolfSSL | 15:117db924cf7c | 2637 | |
wolfSSL | 15:117db924cf7c | 2638 | /* now find oid */ |
wolfSSL | 15:117db924cf7c | 2639 | if (wc_ecc_get_oid(ecc.dp->oidSum, curveOID, oidSz) < 0) { |
wolfSSL | 15:117db924cf7c | 2640 | WOLFSSL_MSG("Error getting ECC curve OID"); |
wolfSSL | 15:117db924cf7c | 2641 | wc_ecc_free(&ecc); |
wolfSSL | 15:117db924cf7c | 2642 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2643 | } |
wolfSSL | 15:117db924cf7c | 2644 | } |
wolfSSL | 15:117db924cf7c | 2645 | else { |
wolfSSL | 15:117db924cf7c | 2646 | WOLFSSL_MSG("Not ECC DER key either"); |
wolfSSL | 15:117db924cf7c | 2647 | } |
wolfSSL | 15:117db924cf7c | 2648 | wc_ecc_free(&ecc); |
wolfSSL | 15:117db924cf7c | 2649 | } |
wolfSSL | 15:117db924cf7c | 2650 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 2651 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 2652 | if (*algoID != RSAk && *algoID != ECDSAk) { |
wolfSSL | 15:117db924cf7c | 2653 | ed25519_key ed25519; |
wolfSSL | 15:117db924cf7c | 2654 | |
wolfSSL | 15:117db924cf7c | 2655 | tmpIdx = 0; |
wolfSSL | 15:117db924cf7c | 2656 | if (wc_ed25519_init(&ed25519) == 0) { |
wolfSSL | 15:117db924cf7c | 2657 | if (wc_Ed25519PrivateKeyDecode(key, &tmpIdx, &ed25519, keySz) |
wolfSSL | 15:117db924cf7c | 2658 | == 0) { |
wolfSSL | 15:117db924cf7c | 2659 | *algoID = ED25519k; |
wolfSSL | 15:117db924cf7c | 2660 | } |
wolfSSL | 15:117db924cf7c | 2661 | else { |
wolfSSL | 15:117db924cf7c | 2662 | WOLFSSL_MSG("Not ED25519 DER key"); |
wolfSSL | 15:117db924cf7c | 2663 | } |
wolfSSL | 15:117db924cf7c | 2664 | wc_ed25519_free(&ed25519); |
wolfSSL | 15:117db924cf7c | 2665 | } |
wolfSSL | 15:117db924cf7c | 2666 | else { |
wolfSSL | 15:117db924cf7c | 2667 | WOLFSSL_MSG("GetKeyOID wc_ed25519_init failed"); |
wolfSSL | 15:117db924cf7c | 2668 | } |
wolfSSL | 15:117db924cf7c | 2669 | } |
wolfSSL | 15:117db924cf7c | 2670 | #endif |
wolfSSL | 15:117db924cf7c | 2671 | |
wolfSSL | 15:117db924cf7c | 2672 | /* if flag is not set then is neither RSA or ECC key that could be |
wolfSSL | 15:117db924cf7c | 2673 | * found */ |
wolfSSL | 15:117db924cf7c | 2674 | if (*algoID == 0) { |
wolfSSL | 15:117db924cf7c | 2675 | WOLFSSL_MSG("Bad key DER or compile options"); |
wolfSSL | 15:117db924cf7c | 2676 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2677 | } |
wolfSSL | 15:117db924cf7c | 2678 | |
wolfSSL | 15:117db924cf7c | 2679 | (void)curveOID; |
wolfSSL | 15:117db924cf7c | 2680 | (void)oidSz; |
wolfSSL | 15:117db924cf7c | 2681 | |
wolfSSL | 15:117db924cf7c | 2682 | return 1; |
wolfSSL | 15:117db924cf7c | 2683 | } |
wolfSSL | 15:117db924cf7c | 2684 | |
wolfSSL | 15:117db924cf7c | 2685 | |
wolfSSL | 15:117db924cf7c | 2686 | /* |
wolfSSL | 15:117db924cf7c | 2687 | * Used when creating PKCS12 shrouded key bags |
wolfSSL | 15:117db924cf7c | 2688 | * vPKCS is the version of PKCS to use |
wolfSSL | 15:117db924cf7c | 2689 | * vAlgo is the algorithm version to use |
wolfSSL | 15:117db924cf7c | 2690 | * |
wolfSSL | 15:117db924cf7c | 2691 | * if salt is NULL a random number is generated |
wolfSSL | 15:117db924cf7c | 2692 | * |
wolfSSL | 15:117db924cf7c | 2693 | * returns the size of encrypted data on success |
wolfSSL | 15:117db924cf7c | 2694 | */ |
wolfSSL | 15:117db924cf7c | 2695 | int UnTraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz, |
wolfSSL | 15:117db924cf7c | 2696 | const char* password,int passwordSz, int vPKCS, int vAlgo, |
wolfSSL | 15:117db924cf7c | 2697 | byte* salt, word32 saltSz, int itt, WC_RNG* rng, void* heap) |
wolfSSL | 15:117db924cf7c | 2698 | { |
wolfSSL | 15:117db924cf7c | 2699 | int algoID = 0; |
wolfSSL | 15:117db924cf7c | 2700 | byte* tmp; |
wolfSSL | 15:117db924cf7c | 2701 | word32 tmpSz = 0; |
wolfSSL | 15:117db924cf7c | 2702 | word32 sz; |
wolfSSL | 15:117db924cf7c | 2703 | word32 seqSz; |
wolfSSL | 15:117db924cf7c | 2704 | word32 inOutIdx = 0; |
wolfSSL | 15:117db924cf7c | 2705 | word32 totalSz = 0; |
wolfSSL | 15:117db924cf7c | 2706 | int version, id; |
wolfSSL | 15:117db924cf7c | 2707 | int ret; |
wolfSSL | 15:117db924cf7c | 2708 | |
wolfSSL | 15:117db924cf7c | 2709 | const byte* curveOID = NULL; |
wolfSSL | 15:117db924cf7c | 2710 | word32 oidSz = 0; |
wolfSSL | 15:117db924cf7c | 2711 | |
wolfSSL | 15:117db924cf7c | 2712 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2713 | byte* saltTmp = NULL; |
wolfSSL | 15:117db924cf7c | 2714 | byte* cbcIv = NULL; |
wolfSSL | 15:117db924cf7c | 2715 | #else |
wolfSSL | 15:117db924cf7c | 2716 | byte saltTmp[MAX_IV_SIZE]; |
wolfSSL | 15:117db924cf7c | 2717 | byte cbcIv[MAX_IV_SIZE]; |
wolfSSL | 15:117db924cf7c | 2718 | #endif |
wolfSSL | 15:117db924cf7c | 2719 | |
wolfSSL | 15:117db924cf7c | 2720 | WOLFSSL_ENTER("UnTraditionalEnc()"); |
wolfSSL | 15:117db924cf7c | 2721 | |
wolfSSL | 15:117db924cf7c | 2722 | if (saltSz > MAX_SALT_SIZE) |
wolfSSL | 15:117db924cf7c | 2723 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 2724 | |
wolfSSL | 15:117db924cf7c | 2725 | |
wolfSSL | 15:117db924cf7c | 2726 | inOutIdx += MAX_SEQ_SZ; /* leave room for size of finished shroud */ |
wolfSSL | 15:117db924cf7c | 2727 | if (CheckAlgo(vPKCS, vAlgo, &id, &version) < 0) { |
wolfSSL | 15:117db924cf7c | 2728 | WOLFSSL_MSG("Bad/Unsupported algorithm ID"); |
wolfSSL | 15:117db924cf7c | 2729 | return ASN_INPUT_E; /* Algo ID error */ |
wolfSSL | 15:117db924cf7c | 2730 | } |
wolfSSL | 15:117db924cf7c | 2731 | |
wolfSSL | 15:117db924cf7c | 2732 | if (out != NULL) { |
wolfSSL | 15:117db924cf7c | 2733 | if (*outSz < inOutIdx + MAX_ALGO_SZ + MAX_SALT_SIZE + MAX_SEQ_SZ + 1 + |
wolfSSL | 15:117db924cf7c | 2734 | MAX_LENGTH_SZ + MAX_SHORT_SZ + 1) |
wolfSSL | 15:117db924cf7c | 2735 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 2736 | |
wolfSSL | 15:117db924cf7c | 2737 | if (version == PKCS5v2) { |
wolfSSL | 15:117db924cf7c | 2738 | WOLFSSL_MSG("PKCS5v2 Not supported yet\n"); |
wolfSSL | 15:117db924cf7c | 2739 | return ASN_VERSION_E; |
wolfSSL | 15:117db924cf7c | 2740 | } |
wolfSSL | 15:117db924cf7c | 2741 | |
wolfSSL | 15:117db924cf7c | 2742 | if (salt == NULL || saltSz <= 0) { |
wolfSSL | 15:117db924cf7c | 2743 | saltSz = 8; |
wolfSSL | 15:117db924cf7c | 2744 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2745 | saltTmp = (byte*)XMALLOC(saltSz, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2746 | if (saltTmp == NULL) |
wolfSSL | 15:117db924cf7c | 2747 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2748 | #endif |
wolfSSL | 15:117db924cf7c | 2749 | salt = saltTmp; |
wolfSSL | 15:117db924cf7c | 2750 | |
wolfSSL | 15:117db924cf7c | 2751 | if ((ret = wc_RNG_GenerateBlock(rng, saltTmp, saltSz)) != 0) { |
wolfSSL | 15:117db924cf7c | 2752 | WOLFSSL_MSG("Error generating random salt"); |
wolfSSL | 15:117db924cf7c | 2753 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2754 | if (saltTmp != NULL) |
wolfSSL | 15:117db924cf7c | 2755 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2756 | #endif |
wolfSSL | 15:117db924cf7c | 2757 | return ret; |
wolfSSL | 15:117db924cf7c | 2758 | } |
wolfSSL | 15:117db924cf7c | 2759 | } |
wolfSSL | 15:117db924cf7c | 2760 | |
wolfSSL | 15:117db924cf7c | 2761 | |
wolfSSL | 15:117db924cf7c | 2762 | /* leave room for a sequence (contains salt and iterations int) */ |
wolfSSL | 15:117db924cf7c | 2763 | inOutIdx += MAX_SEQ_SZ; sz = 0; |
wolfSSL | 15:117db924cf7c | 2764 | inOutIdx += MAX_ALGO_SZ; |
wolfSSL | 15:117db924cf7c | 2765 | |
wolfSSL | 15:117db924cf7c | 2766 | /* place salt in buffer */ |
wolfSSL | 15:117db924cf7c | 2767 | out[inOutIdx++] = ASN_OCTET_STRING; sz++; |
wolfSSL | 15:117db924cf7c | 2768 | tmpSz = SetLength(saltSz, out + inOutIdx); |
wolfSSL | 15:117db924cf7c | 2769 | inOutIdx += tmpSz; sz += tmpSz; |
wolfSSL | 15:117db924cf7c | 2770 | XMEMCPY(out + inOutIdx, salt, saltSz); |
wolfSSL | 15:117db924cf7c | 2771 | inOutIdx += saltSz; sz += saltSz; |
wolfSSL | 15:117db924cf7c | 2772 | |
wolfSSL | 15:117db924cf7c | 2773 | /* place iteration count in buffer */ |
wolfSSL | 15:117db924cf7c | 2774 | ret = SetShortInt(out, &inOutIdx, itt, *outSz); |
wolfSSL | 15:117db924cf7c | 2775 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 2776 | return ret; |
wolfSSL | 15:117db924cf7c | 2777 | } |
wolfSSL | 15:117db924cf7c | 2778 | sz += (word32)ret; |
wolfSSL | 15:117db924cf7c | 2779 | |
wolfSSL | 15:117db924cf7c | 2780 | /* wind back index and set sequence then clean up buffer */ |
wolfSSL | 15:117db924cf7c | 2781 | inOutIdx -= (sz + MAX_SEQ_SZ); |
wolfSSL | 15:117db924cf7c | 2782 | tmpSz = SetSequence(sz, out + inOutIdx); |
wolfSSL | 15:117db924cf7c | 2783 | XMEMMOVE(out + inOutIdx + tmpSz, out + inOutIdx + MAX_SEQ_SZ, sz); |
wolfSSL | 15:117db924cf7c | 2784 | totalSz += tmpSz + sz; sz += tmpSz; |
wolfSSL | 15:117db924cf7c | 2785 | |
wolfSSL | 15:117db924cf7c | 2786 | /* add in algo ID */ |
wolfSSL | 15:117db924cf7c | 2787 | inOutIdx -= MAX_ALGO_SZ; |
wolfSSL | 15:117db924cf7c | 2788 | tmpSz = SetAlgoID(id, out + inOutIdx, oidPBEType, sz); |
wolfSSL | 15:117db924cf7c | 2789 | XMEMMOVE(out + inOutIdx + tmpSz, out + inOutIdx + MAX_ALGO_SZ, sz); |
wolfSSL | 15:117db924cf7c | 2790 | totalSz += tmpSz; inOutIdx += tmpSz + sz; |
wolfSSL | 15:117db924cf7c | 2791 | |
wolfSSL | 15:117db924cf7c | 2792 | /* octet string containing encrypted key */ |
wolfSSL | 15:117db924cf7c | 2793 | out[inOutIdx++] = ASN_OCTET_STRING; totalSz++; |
wolfSSL | 15:117db924cf7c | 2794 | } |
wolfSSL | 15:117db924cf7c | 2795 | |
wolfSSL | 15:117db924cf7c | 2796 | /* check key type and get OID if ECC */ |
wolfSSL | 15:117db924cf7c | 2797 | if ((ret = wc_GetKeyOID(key, keySz, &curveOID, &oidSz, &algoID, heap))< 0) { |
wolfSSL | 15:117db924cf7c | 2798 | return ret; |
wolfSSL | 15:117db924cf7c | 2799 | } |
wolfSSL | 15:117db924cf7c | 2800 | |
wolfSSL | 15:117db924cf7c | 2801 | /* PKCS#8 wrapping around key */ |
wolfSSL | 15:117db924cf7c | 2802 | if (wc_CreatePKCS8Key(NULL, &tmpSz, key, keySz, algoID, curveOID, oidSz) |
wolfSSL | 15:117db924cf7c | 2803 | != LENGTH_ONLY_E) { |
wolfSSL | 15:117db924cf7c | 2804 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2805 | if (saltTmp != NULL) |
wolfSSL | 15:117db924cf7c | 2806 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2807 | #endif |
wolfSSL | 15:117db924cf7c | 2808 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2809 | } |
wolfSSL | 15:117db924cf7c | 2810 | |
wolfSSL | 15:117db924cf7c | 2811 | /* check if should return max size */ |
wolfSSL | 15:117db924cf7c | 2812 | if (out == NULL) { |
wolfSSL | 15:117db924cf7c | 2813 | /* account for salt size */ |
wolfSSL | 15:117db924cf7c | 2814 | if (salt == NULL || saltSz <= 0) { |
wolfSSL | 15:117db924cf7c | 2815 | tmpSz += MAX_SALT_SIZE; |
wolfSSL | 15:117db924cf7c | 2816 | } |
wolfSSL | 15:117db924cf7c | 2817 | else { |
wolfSSL | 15:117db924cf7c | 2818 | tmpSz += saltSz; |
wolfSSL | 15:117db924cf7c | 2819 | } |
wolfSSL | 15:117db924cf7c | 2820 | |
wolfSSL | 15:117db924cf7c | 2821 | /* plus 3 for tags */ |
wolfSSL | 15:117db924cf7c | 2822 | *outSz = tmpSz + MAX_ALGO_SZ + MAX_LENGTH_SZ +MAX_LENGTH_SZ + MAX_SEQ_SZ |
wolfSSL | 15:117db924cf7c | 2823 | + MAX_LENGTH_SZ + MAX_SEQ_SZ + 3; |
wolfSSL | 15:117db924cf7c | 2824 | return LENGTH_ONLY_E; |
wolfSSL | 15:117db924cf7c | 2825 | } |
wolfSSL | 15:117db924cf7c | 2826 | |
wolfSSL | 15:117db924cf7c | 2827 | tmp = (byte*)XMALLOC(tmpSz, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2828 | if (tmp == NULL) { |
wolfSSL | 15:117db924cf7c | 2829 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2830 | if (saltTmp != NULL) |
wolfSSL | 15:117db924cf7c | 2831 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2832 | #endif |
wolfSSL | 15:117db924cf7c | 2833 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2834 | } |
wolfSSL | 15:117db924cf7c | 2835 | |
wolfSSL | 15:117db924cf7c | 2836 | if ((ret = wc_CreatePKCS8Key(tmp, &tmpSz, key, keySz, algoID, curveOID, |
wolfSSL | 15:117db924cf7c | 2837 | oidSz)) < 0) { |
wolfSSL | 15:117db924cf7c | 2838 | XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2839 | WOLFSSL_MSG("Error wrapping key with PKCS#8"); |
wolfSSL | 15:117db924cf7c | 2840 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2841 | if (saltTmp != NULL) |
wolfSSL | 15:117db924cf7c | 2842 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2843 | #endif |
wolfSSL | 15:117db924cf7c | 2844 | return ret; |
wolfSSL | 15:117db924cf7c | 2845 | } |
wolfSSL | 15:117db924cf7c | 2846 | tmpSz = ret; |
wolfSSL | 15:117db924cf7c | 2847 | |
wolfSSL | 15:117db924cf7c | 2848 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2849 | cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2850 | if (cbcIv == NULL) { |
wolfSSL | 15:117db924cf7c | 2851 | if (saltTmp != NULL) |
wolfSSL | 15:117db924cf7c | 2852 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2853 | XFREE(salt, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2854 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 2855 | } |
wolfSSL | 15:117db924cf7c | 2856 | #endif |
wolfSSL | 15:117db924cf7c | 2857 | |
wolfSSL | 15:117db924cf7c | 2858 | /* encrypt PKCS#8 wrapped key */ |
wolfSSL | 15:117db924cf7c | 2859 | if ((ret = wc_CryptKey(password, passwordSz, salt, saltSz, itt, id, |
wolfSSL | 15:117db924cf7c | 2860 | tmp, tmpSz, version, cbcIv, 1)) < 0) { |
wolfSSL | 15:117db924cf7c | 2861 | XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2862 | WOLFSSL_MSG("Error encrypting key"); |
wolfSSL | 15:117db924cf7c | 2863 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2864 | if (saltTmp != NULL) |
wolfSSL | 15:117db924cf7c | 2865 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2866 | if (cbcIv != NULL) |
wolfSSL | 15:117db924cf7c | 2867 | XFREE(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2868 | #endif |
wolfSSL | 15:117db924cf7c | 2869 | return ret; /* encryption failure */ |
wolfSSL | 15:117db924cf7c | 2870 | } |
wolfSSL | 15:117db924cf7c | 2871 | totalSz += tmpSz; |
wolfSSL | 15:117db924cf7c | 2872 | |
wolfSSL | 15:117db924cf7c | 2873 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2874 | if (saltTmp != NULL) |
wolfSSL | 15:117db924cf7c | 2875 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2876 | if (cbcIv != NULL) |
wolfSSL | 15:117db924cf7c | 2877 | XFREE(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2878 | #endif |
wolfSSL | 15:117db924cf7c | 2879 | |
wolfSSL | 15:117db924cf7c | 2880 | if (*outSz < inOutIdx + tmpSz + MAX_LENGTH_SZ) { |
wolfSSL | 15:117db924cf7c | 2881 | XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2882 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 2883 | } |
wolfSSL | 15:117db924cf7c | 2884 | |
wolfSSL | 15:117db924cf7c | 2885 | /* set length of key and copy over encrypted key */ |
wolfSSL | 15:117db924cf7c | 2886 | seqSz = SetLength(tmpSz, out + inOutIdx); |
wolfSSL | 15:117db924cf7c | 2887 | inOutIdx += seqSz; totalSz += seqSz; |
wolfSSL | 15:117db924cf7c | 2888 | XMEMCPY(out + inOutIdx, tmp, tmpSz); |
wolfSSL | 15:117db924cf7c | 2889 | XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2890 | |
wolfSSL | 15:117db924cf7c | 2891 | /* set total size at begining */ |
wolfSSL | 15:117db924cf7c | 2892 | sz = SetSequence(totalSz, out); |
wolfSSL | 15:117db924cf7c | 2893 | XMEMMOVE(out + sz, out + MAX_SEQ_SZ, totalSz); |
wolfSSL | 15:117db924cf7c | 2894 | |
wolfSSL | 15:117db924cf7c | 2895 | return totalSz + sz; |
wolfSSL | 15:117db924cf7c | 2896 | } |
wolfSSL | 15:117db924cf7c | 2897 | |
wolfSSL | 15:117db924cf7c | 2898 | |
wolfSSL | 15:117db924cf7c | 2899 | /* Remove Encrypted PKCS8 header, move beginning of traditional to beginning |
wolfSSL | 15:117db924cf7c | 2900 | of input */ |
wolfSSL | 15:117db924cf7c | 2901 | int ToTraditionalEnc(byte* input, word32 sz,const char* password,int passwordSz) |
wolfSSL | 15:117db924cf7c | 2902 | { |
wolfSSL | 15:117db924cf7c | 2903 | word32 inOutIdx = 0, seqEnd, oid; |
wolfSSL | 15:117db924cf7c | 2904 | int ret = 0, first, second, length = 0, version, saltSz, id; |
wolfSSL | 15:117db924cf7c | 2905 | int iterations = 0, keySz = 0; |
wolfSSL | 15:117db924cf7c | 2906 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2907 | byte* salt = NULL; |
wolfSSL | 15:117db924cf7c | 2908 | byte* cbcIv = NULL; |
wolfSSL | 15:117db924cf7c | 2909 | #else |
wolfSSL | 15:117db924cf7c | 2910 | byte salt[MAX_SALT_SIZE]; |
wolfSSL | 15:117db924cf7c | 2911 | byte cbcIv[MAX_IV_SIZE]; |
wolfSSL | 15:117db924cf7c | 2912 | #endif |
wolfSSL | 15:117db924cf7c | 2913 | |
wolfSSL | 15:117db924cf7c | 2914 | if (passwordSz < 0) { |
wolfSSL | 15:117db924cf7c | 2915 | WOLFSSL_MSG("Bad password size"); |
wolfSSL | 15:117db924cf7c | 2916 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 2917 | } |
wolfSSL | 15:117db924cf7c | 2918 | |
wolfSSL | 15:117db924cf7c | 2919 | if (GetSequence(input, &inOutIdx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 2920 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2921 | } |
wolfSSL | 15:117db924cf7c | 2922 | |
wolfSSL | 15:117db924cf7c | 2923 | if (GetAlgoId(input, &inOutIdx, &oid, oidIgnoreType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 2924 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2925 | } |
wolfSSL | 15:117db924cf7c | 2926 | |
wolfSSL | 15:117db924cf7c | 2927 | first = input[inOutIdx - 2]; /* PKCS version always 2nd to last byte */ |
wolfSSL | 15:117db924cf7c | 2928 | second = input[inOutIdx - 1]; /* version.algo, algo id last byte */ |
wolfSSL | 15:117db924cf7c | 2929 | |
wolfSSL | 15:117db924cf7c | 2930 | if (CheckAlgo(first, second, &id, &version) < 0) { |
wolfSSL | 15:117db924cf7c | 2931 | ERROR_OUT(ASN_INPUT_E, exit_tte); /* Algo ID error */ |
wolfSSL | 15:117db924cf7c | 2932 | } |
wolfSSL | 15:117db924cf7c | 2933 | |
wolfSSL | 15:117db924cf7c | 2934 | if (version == PKCS5v2) { |
wolfSSL | 15:117db924cf7c | 2935 | if (GetSequence(input, &inOutIdx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 2936 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2937 | } |
wolfSSL | 15:117db924cf7c | 2938 | |
wolfSSL | 15:117db924cf7c | 2939 | if (GetAlgoId(input, &inOutIdx, &oid, oidKdfType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 2940 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2941 | } |
wolfSSL | 15:117db924cf7c | 2942 | |
wolfSSL | 15:117db924cf7c | 2943 | if (oid != PBKDF2_OID) { |
wolfSSL | 15:117db924cf7c | 2944 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2945 | } |
wolfSSL | 15:117db924cf7c | 2946 | } |
wolfSSL | 15:117db924cf7c | 2947 | |
wolfSSL | 15:117db924cf7c | 2948 | if (GetSequence(input, &inOutIdx, &length, sz) <= 0) { |
wolfSSL | 15:117db924cf7c | 2949 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2950 | } |
wolfSSL | 15:117db924cf7c | 2951 | /* Find the end of this SEQUENCE so we can check for the OPTIONAL and |
wolfSSL | 15:117db924cf7c | 2952 | * DEFAULT items. */ |
wolfSSL | 15:117db924cf7c | 2953 | seqEnd = inOutIdx + length; |
wolfSSL | 15:117db924cf7c | 2954 | |
wolfSSL | 15:117db924cf7c | 2955 | ret = GetOctetString(input, &inOutIdx, &saltSz, sz); |
wolfSSL | 15:117db924cf7c | 2956 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 2957 | goto exit_tte; |
wolfSSL | 15:117db924cf7c | 2958 | |
wolfSSL | 15:117db924cf7c | 2959 | if (saltSz > MAX_SALT_SIZE) { |
wolfSSL | 15:117db924cf7c | 2960 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2961 | } |
wolfSSL | 15:117db924cf7c | 2962 | |
wolfSSL | 15:117db924cf7c | 2963 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2964 | salt = (byte*)XMALLOC(MAX_SALT_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2965 | if (salt == NULL) { |
wolfSSL | 15:117db924cf7c | 2966 | ERROR_OUT(MEMORY_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2967 | } |
wolfSSL | 15:117db924cf7c | 2968 | #endif |
wolfSSL | 15:117db924cf7c | 2969 | |
wolfSSL | 15:117db924cf7c | 2970 | XMEMCPY(salt, &input[inOutIdx], saltSz); |
wolfSSL | 15:117db924cf7c | 2971 | inOutIdx += saltSz; |
wolfSSL | 15:117db924cf7c | 2972 | |
wolfSSL | 15:117db924cf7c | 2973 | if (GetShortInt(input, &inOutIdx, &iterations, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 2974 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2975 | } |
wolfSSL | 15:117db924cf7c | 2976 | |
wolfSSL | 15:117db924cf7c | 2977 | /* OPTIONAL key length */ |
wolfSSL | 15:117db924cf7c | 2978 | if (seqEnd > inOutIdx && input[inOutIdx] == ASN_INTEGER) { |
wolfSSL | 15:117db924cf7c | 2979 | if (GetShortInt(input, &inOutIdx, &keySz, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 2980 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2981 | } |
wolfSSL | 15:117db924cf7c | 2982 | } |
wolfSSL | 15:117db924cf7c | 2983 | |
wolfSSL | 15:117db924cf7c | 2984 | /* DEFAULT HMAC is SHA-1 */ |
wolfSSL | 15:117db924cf7c | 2985 | if (seqEnd > inOutIdx) { |
wolfSSL | 15:117db924cf7c | 2986 | if (GetAlgoId(input, &inOutIdx, &oid, oidHmacType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 2987 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2988 | } |
wolfSSL | 15:117db924cf7c | 2989 | } |
wolfSSL | 15:117db924cf7c | 2990 | |
wolfSSL | 15:117db924cf7c | 2991 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 2992 | cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 2993 | if (cbcIv == NULL) { |
wolfSSL | 15:117db924cf7c | 2994 | ERROR_OUT(MEMORY_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 2995 | } |
wolfSSL | 15:117db924cf7c | 2996 | #endif |
wolfSSL | 15:117db924cf7c | 2997 | |
wolfSSL | 15:117db924cf7c | 2998 | if (version == PKCS5v2) { |
wolfSSL | 15:117db924cf7c | 2999 | /* get encryption algo */ |
wolfSSL | 15:117db924cf7c | 3000 | if (GetAlgoId(input, &inOutIdx, &oid, oidBlkType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3001 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 3002 | } |
wolfSSL | 15:117db924cf7c | 3003 | |
wolfSSL | 15:117db924cf7c | 3004 | if (CheckAlgoV2(oid, &id) < 0) { |
wolfSSL | 15:117db924cf7c | 3005 | ERROR_OUT(ASN_PARSE_E, exit_tte); /* PKCS v2 algo id error */ |
wolfSSL | 15:117db924cf7c | 3006 | } |
wolfSSL | 15:117db924cf7c | 3007 | |
wolfSSL | 15:117db924cf7c | 3008 | ret = GetOctetString(input, &inOutIdx, &length, sz); |
wolfSSL | 15:117db924cf7c | 3009 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 3010 | goto exit_tte; |
wolfSSL | 15:117db924cf7c | 3011 | |
wolfSSL | 15:117db924cf7c | 3012 | if (length > MAX_IV_SIZE) { |
wolfSSL | 15:117db924cf7c | 3013 | ERROR_OUT(ASN_PARSE_E, exit_tte); |
wolfSSL | 15:117db924cf7c | 3014 | } |
wolfSSL | 15:117db924cf7c | 3015 | |
wolfSSL | 15:117db924cf7c | 3016 | XMEMCPY(cbcIv, &input[inOutIdx], length); |
wolfSSL | 15:117db924cf7c | 3017 | inOutIdx += length; |
wolfSSL | 15:117db924cf7c | 3018 | } |
wolfSSL | 15:117db924cf7c | 3019 | |
wolfSSL | 15:117db924cf7c | 3020 | ret = GetOctetString(input, &inOutIdx, &length, sz); |
wolfSSL | 15:117db924cf7c | 3021 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 3022 | goto exit_tte; |
wolfSSL | 15:117db924cf7c | 3023 | |
wolfSSL | 15:117db924cf7c | 3024 | ret = wc_CryptKey(password, passwordSz, salt, saltSz, iterations, id, |
wolfSSL | 15:117db924cf7c | 3025 | input + inOutIdx, length, version, cbcIv, 0); |
wolfSSL | 15:117db924cf7c | 3026 | |
wolfSSL | 15:117db924cf7c | 3027 | exit_tte: |
wolfSSL | 15:117db924cf7c | 3028 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3029 | XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3030 | XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3031 | #endif |
wolfSSL | 15:117db924cf7c | 3032 | |
wolfSSL | 15:117db924cf7c | 3033 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 3034 | XMEMMOVE(input, input + inOutIdx, length); |
wolfSSL | 15:117db924cf7c | 3035 | ret = ToTraditional(input, length); |
wolfSSL | 15:117db924cf7c | 3036 | } |
wolfSSL | 15:117db924cf7c | 3037 | |
wolfSSL | 15:117db924cf7c | 3038 | return ret; |
wolfSSL | 15:117db924cf7c | 3039 | } |
wolfSSL | 15:117db924cf7c | 3040 | |
wolfSSL | 15:117db924cf7c | 3041 | |
wolfSSL | 15:117db924cf7c | 3042 | /* encrypt PKCS 12 content |
wolfSSL | 15:117db924cf7c | 3043 | * |
wolfSSL | 15:117db924cf7c | 3044 | * NOTE: if out is NULL then outSz is set with the total buffer size needed and |
wolfSSL | 15:117db924cf7c | 3045 | * the error value LENGTH_ONLY_E is returned. |
wolfSSL | 15:117db924cf7c | 3046 | * |
wolfSSL | 15:117db924cf7c | 3047 | * input data to encrypt |
wolfSSL | 15:117db924cf7c | 3048 | * inputSz size of input buffer |
wolfSSL | 15:117db924cf7c | 3049 | * out buffer to hold the result |
wolfSSL | 15:117db924cf7c | 3050 | * outSz size of out buffer |
wolfSSL | 15:117db924cf7c | 3051 | * password password if used. Can be NULL for no password |
wolfSSL | 15:117db924cf7c | 3052 | * passwordSz size of password buffer |
wolfSSL | 15:117db924cf7c | 3053 | * vPKCS version of PKCS i.e. PKCS5v2 |
wolfSSL | 15:117db924cf7c | 3054 | * vAlgo algorithm version |
wolfSSL | 15:117db924cf7c | 3055 | * salt buffer holding salt if used. If NULL then a random salt is created |
wolfSSL | 15:117db924cf7c | 3056 | * saltSz size of salt buffer if it is not NULL |
wolfSSL | 15:117db924cf7c | 3057 | * itt number of iterations used |
wolfSSL | 15:117db924cf7c | 3058 | * rng random number generator to use |
wolfSSL | 15:117db924cf7c | 3059 | * heap possible heap hint for mallocs/frees |
wolfSSL | 15:117db924cf7c | 3060 | * |
wolfSSL | 15:117db924cf7c | 3061 | * returns the total size of encrypted content on success. |
wolfSSL | 15:117db924cf7c | 3062 | */ |
wolfSSL | 15:117db924cf7c | 3063 | int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz, |
wolfSSL | 15:117db924cf7c | 3064 | const char* password, int passwordSz, int vPKCS, int vAlgo, |
wolfSSL | 15:117db924cf7c | 3065 | byte* salt, word32 saltSz, int itt, WC_RNG* rng, void* heap) |
wolfSSL | 15:117db924cf7c | 3066 | { |
wolfSSL | 15:117db924cf7c | 3067 | word32 sz; |
wolfSSL | 15:117db924cf7c | 3068 | word32 inOutIdx = 0; |
wolfSSL | 15:117db924cf7c | 3069 | word32 tmpIdx = 0; |
wolfSSL | 15:117db924cf7c | 3070 | word32 totalSz = 0; |
wolfSSL | 15:117db924cf7c | 3071 | word32 seqSz; |
wolfSSL | 15:117db924cf7c | 3072 | int ret; |
wolfSSL | 15:117db924cf7c | 3073 | int version, id; |
wolfSSL | 15:117db924cf7c | 3074 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3075 | byte* saltTmp = NULL; |
wolfSSL | 15:117db924cf7c | 3076 | byte* cbcIv = NULL; |
wolfSSL | 15:117db924cf7c | 3077 | #else |
wolfSSL | 15:117db924cf7c | 3078 | byte saltTmp[MAX_SALT_SIZE]; |
wolfSSL | 15:117db924cf7c | 3079 | byte cbcIv[MAX_IV_SIZE]; |
wolfSSL | 15:117db924cf7c | 3080 | #endif |
wolfSSL | 15:117db924cf7c | 3081 | |
wolfSSL | 15:117db924cf7c | 3082 | (void)heap; |
wolfSSL | 15:117db924cf7c | 3083 | |
wolfSSL | 15:117db924cf7c | 3084 | WOLFSSL_ENTER("EncryptContent()"); |
wolfSSL | 15:117db924cf7c | 3085 | |
wolfSSL | 15:117db924cf7c | 3086 | if (CheckAlgo(vPKCS, vAlgo, &id, &version) < 0) |
wolfSSL | 15:117db924cf7c | 3087 | return ASN_INPUT_E; /* Algo ID error */ |
wolfSSL | 15:117db924cf7c | 3088 | |
wolfSSL | 15:117db924cf7c | 3089 | if (version == PKCS5v2) { |
wolfSSL | 15:117db924cf7c | 3090 | WOLFSSL_MSG("PKCS#5 version 2 not supported yet"); |
wolfSSL | 15:117db924cf7c | 3091 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3092 | } |
wolfSSL | 15:117db924cf7c | 3093 | |
wolfSSL | 15:117db924cf7c | 3094 | if (saltSz > MAX_SALT_SIZE) |
wolfSSL | 15:117db924cf7c | 3095 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3096 | |
wolfSSL | 15:117db924cf7c | 3097 | if (outSz == NULL) { |
wolfSSL | 15:117db924cf7c | 3098 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3099 | } |
wolfSSL | 15:117db924cf7c | 3100 | |
wolfSSL | 15:117db924cf7c | 3101 | if (out == NULL) { |
wolfSSL | 15:117db924cf7c | 3102 | sz = inputSz; |
wolfSSL | 15:117db924cf7c | 3103 | switch (id) { |
wolfSSL | 15:117db924cf7c | 3104 | #if !defined(NO_DES3) && (!defined(NO_MD5) || !defined(NO_SHA)) |
wolfSSL | 15:117db924cf7c | 3105 | case PBE_MD5_DES: |
wolfSSL | 15:117db924cf7c | 3106 | case PBE_SHA1_DES: |
wolfSSL | 15:117db924cf7c | 3107 | case PBE_SHA1_DES3: |
wolfSSL | 15:117db924cf7c | 3108 | /* set to block size of 8 for DES operations. This rounds up |
wolfSSL | 15:117db924cf7c | 3109 | * to the nearset multiple of 8 */ |
wolfSSL | 15:117db924cf7c | 3110 | sz &= 0xfffffff8; |
wolfSSL | 15:117db924cf7c | 3111 | sz += 8; |
wolfSSL | 15:117db924cf7c | 3112 | break; |
wolfSSL | 15:117db924cf7c | 3113 | #endif /* !NO_DES3 && (!NO_MD5 || !NO_SHA) */ |
wolfSSL | 15:117db924cf7c | 3114 | #if !defined(NO_RC4) && !defined(NO_SHA) |
wolfSSL | 15:117db924cf7c | 3115 | case PBE_SHA1_RC4_128: |
wolfSSL | 15:117db924cf7c | 3116 | break; |
wolfSSL | 15:117db924cf7c | 3117 | #endif |
wolfSSL | 15:117db924cf7c | 3118 | case -1: |
wolfSSL | 15:117db924cf7c | 3119 | break; |
wolfSSL | 15:117db924cf7c | 3120 | |
wolfSSL | 15:117db924cf7c | 3121 | default: |
wolfSSL | 15:117db924cf7c | 3122 | return ALGO_ID_E; |
wolfSSL | 15:117db924cf7c | 3123 | } |
wolfSSL | 15:117db924cf7c | 3124 | |
wolfSSL | 15:117db924cf7c | 3125 | if (saltSz <= 0) { |
wolfSSL | 15:117db924cf7c | 3126 | sz += MAX_SALT_SIZE; |
wolfSSL | 15:117db924cf7c | 3127 | } |
wolfSSL | 15:117db924cf7c | 3128 | else { |
wolfSSL | 15:117db924cf7c | 3129 | sz += saltSz; |
wolfSSL | 15:117db924cf7c | 3130 | } |
wolfSSL | 15:117db924cf7c | 3131 | |
wolfSSL | 15:117db924cf7c | 3132 | /* add 2 for tags */ |
wolfSSL | 15:117db924cf7c | 3133 | *outSz = sz + MAX_ALGO_SZ + MAX_SEQ_SZ + MAX_LENGTH_SZ + |
wolfSSL | 15:117db924cf7c | 3134 | MAX_LENGTH_SZ + MAX_LENGTH_SZ + MAX_SHORT_SZ + 2; |
wolfSSL | 15:117db924cf7c | 3135 | |
wolfSSL | 15:117db924cf7c | 3136 | return LENGTH_ONLY_E; |
wolfSSL | 15:117db924cf7c | 3137 | } |
wolfSSL | 15:117db924cf7c | 3138 | |
wolfSSL | 15:117db924cf7c | 3139 | if (inOutIdx + MAX_ALGO_SZ + MAX_SEQ_SZ + 1 > *outSz) |
wolfSSL | 15:117db924cf7c | 3140 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 3141 | |
wolfSSL | 15:117db924cf7c | 3142 | sz = SetAlgoID(id, out + inOutIdx, oidPBEType, 0); |
wolfSSL | 15:117db924cf7c | 3143 | inOutIdx += sz; totalSz += sz; |
wolfSSL | 15:117db924cf7c | 3144 | tmpIdx = inOutIdx; |
wolfSSL | 15:117db924cf7c | 3145 | tmpIdx += MAX_SEQ_SZ; /* save room for salt and itter sequence */ |
wolfSSL | 15:117db924cf7c | 3146 | out[tmpIdx++] = ASN_OCTET_STRING; |
wolfSSL | 15:117db924cf7c | 3147 | |
wolfSSL | 15:117db924cf7c | 3148 | /* create random salt if one not provided */ |
wolfSSL | 15:117db924cf7c | 3149 | if (salt == NULL || saltSz <= 0) { |
wolfSSL | 15:117db924cf7c | 3150 | saltSz = 8; |
wolfSSL | 15:117db924cf7c | 3151 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3152 | saltTmp = (byte*)XMALLOC(saltSz, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3153 | if (saltTmp == NULL) |
wolfSSL | 15:117db924cf7c | 3154 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 3155 | #endif |
wolfSSL | 15:117db924cf7c | 3156 | salt = saltTmp; |
wolfSSL | 15:117db924cf7c | 3157 | |
wolfSSL | 15:117db924cf7c | 3158 | if ((ret = wc_RNG_GenerateBlock(rng, saltTmp, saltSz)) != 0) { |
wolfSSL | 15:117db924cf7c | 3159 | WOLFSSL_MSG("Error generating random salt"); |
wolfSSL | 15:117db924cf7c | 3160 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3161 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3162 | #endif |
wolfSSL | 15:117db924cf7c | 3163 | return ret; |
wolfSSL | 15:117db924cf7c | 3164 | } |
wolfSSL | 15:117db924cf7c | 3165 | } |
wolfSSL | 15:117db924cf7c | 3166 | |
wolfSSL | 15:117db924cf7c | 3167 | if (tmpIdx + MAX_LENGTH_SZ + saltSz + MAX_SHORT_SZ > *outSz) { |
wolfSSL | 15:117db924cf7c | 3168 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3169 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3170 | #endif |
wolfSSL | 15:117db924cf7c | 3171 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 3172 | } |
wolfSSL | 15:117db924cf7c | 3173 | |
wolfSSL | 15:117db924cf7c | 3174 | sz = SetLength(saltSz, out + tmpIdx); |
wolfSSL | 15:117db924cf7c | 3175 | tmpIdx += sz; |
wolfSSL | 15:117db924cf7c | 3176 | |
wolfSSL | 15:117db924cf7c | 3177 | XMEMCPY(out + tmpIdx, salt, saltSz); |
wolfSSL | 15:117db924cf7c | 3178 | tmpIdx += saltSz; |
wolfSSL | 15:117db924cf7c | 3179 | |
wolfSSL | 15:117db924cf7c | 3180 | /* place itteration setting in buffer */ |
wolfSSL | 15:117db924cf7c | 3181 | ret = SetShortInt(out, &tmpIdx, itt, *outSz); |
wolfSSL | 15:117db924cf7c | 3182 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 3183 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3184 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3185 | #endif |
wolfSSL | 15:117db924cf7c | 3186 | return ret; |
wolfSSL | 15:117db924cf7c | 3187 | } |
wolfSSL | 15:117db924cf7c | 3188 | |
wolfSSL | 15:117db924cf7c | 3189 | /* rewind and place sequence */ |
wolfSSL | 15:117db924cf7c | 3190 | sz = tmpIdx - inOutIdx - MAX_SEQ_SZ; |
wolfSSL | 15:117db924cf7c | 3191 | seqSz = SetSequence(sz, out + inOutIdx); |
wolfSSL | 15:117db924cf7c | 3192 | XMEMMOVE(out + inOutIdx + seqSz, out + inOutIdx + MAX_SEQ_SZ, sz); |
wolfSSL | 15:117db924cf7c | 3193 | inOutIdx += seqSz; totalSz += seqSz; |
wolfSSL | 15:117db924cf7c | 3194 | inOutIdx += sz; totalSz += sz; |
wolfSSL | 15:117db924cf7c | 3195 | |
wolfSSL | 15:117db924cf7c | 3196 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3197 | cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3198 | if (cbcIv == NULL) { |
wolfSSL | 15:117db924cf7c | 3199 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3200 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 3201 | } |
wolfSSL | 15:117db924cf7c | 3202 | #endif |
wolfSSL | 15:117db924cf7c | 3203 | |
wolfSSL | 15:117db924cf7c | 3204 | if ((ret = wc_CryptKey(password, passwordSz, salt, saltSz, itt, id, |
wolfSSL | 15:117db924cf7c | 3205 | input, inputSz, version, cbcIv, 1)) < 0) { |
wolfSSL | 15:117db924cf7c | 3206 | |
wolfSSL | 15:117db924cf7c | 3207 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3208 | XFREE(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3209 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3210 | #endif |
wolfSSL | 15:117db924cf7c | 3211 | return ret; /* encrypt failure */ |
wolfSSL | 15:117db924cf7c | 3212 | } |
wolfSSL | 15:117db924cf7c | 3213 | |
wolfSSL | 15:117db924cf7c | 3214 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3215 | XFREE(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3216 | XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3217 | #endif |
wolfSSL | 15:117db924cf7c | 3218 | |
wolfSSL | 15:117db924cf7c | 3219 | if (inOutIdx + 1 + MAX_LENGTH_SZ + inputSz > *outSz) |
wolfSSL | 15:117db924cf7c | 3220 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 3221 | |
wolfSSL | 15:117db924cf7c | 3222 | out[inOutIdx++] = ASN_LONG_LENGTH; totalSz++; |
wolfSSL | 15:117db924cf7c | 3223 | sz = SetLength(inputSz, out + inOutIdx); |
wolfSSL | 15:117db924cf7c | 3224 | inOutIdx += sz; totalSz += sz; |
wolfSSL | 15:117db924cf7c | 3225 | XMEMCPY(out + inOutIdx, input, inputSz); |
wolfSSL | 15:117db924cf7c | 3226 | totalSz += inputSz; |
wolfSSL | 15:117db924cf7c | 3227 | |
wolfSSL | 15:117db924cf7c | 3228 | return totalSz; |
wolfSSL | 15:117db924cf7c | 3229 | } |
wolfSSL | 15:117db924cf7c | 3230 | |
wolfSSL | 15:117db924cf7c | 3231 | |
wolfSSL | 15:117db924cf7c | 3232 | /* decrypt PKCS |
wolfSSL | 15:117db924cf7c | 3233 | * |
wolfSSL | 15:117db924cf7c | 3234 | * NOTE: input buffer is overwritten with decrypted data! |
wolfSSL | 15:117db924cf7c | 3235 | * |
wolfSSL | 15:117db924cf7c | 3236 | * input[in/out] data to decrypt and results are written to |
wolfSSL | 15:117db924cf7c | 3237 | * sz size of input buffer |
wolfSSL | 15:117db924cf7c | 3238 | * password password if used. Can be NULL for no password |
wolfSSL | 15:117db924cf7c | 3239 | * passwordSz size of password buffer |
wolfSSL | 15:117db924cf7c | 3240 | * |
wolfSSL | 15:117db924cf7c | 3241 | * returns the total size of decrypted content on success. |
wolfSSL | 15:117db924cf7c | 3242 | */ |
wolfSSL | 15:117db924cf7c | 3243 | int DecryptContent(byte* input, word32 sz,const char* password,int passwordSz) |
wolfSSL | 15:117db924cf7c | 3244 | { |
wolfSSL | 15:117db924cf7c | 3245 | word32 inOutIdx = 0, seqEnd, oid; |
wolfSSL | 15:117db924cf7c | 3246 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 3247 | int first, second, length = 0, version, saltSz, id; |
wolfSSL | 15:117db924cf7c | 3248 | int iterations = 0, keySz = 0; |
wolfSSL | 15:117db924cf7c | 3249 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3250 | byte* salt = NULL; |
wolfSSL | 15:117db924cf7c | 3251 | byte* cbcIv = NULL; |
wolfSSL | 15:117db924cf7c | 3252 | #else |
wolfSSL | 15:117db924cf7c | 3253 | byte salt[MAX_SALT_SIZE]; |
wolfSSL | 15:117db924cf7c | 3254 | byte cbcIv[MAX_IV_SIZE]; |
wolfSSL | 15:117db924cf7c | 3255 | #endif |
wolfSSL | 15:117db924cf7c | 3256 | |
wolfSSL | 15:117db924cf7c | 3257 | if (GetAlgoId(input, &inOutIdx, &oid, oidIgnoreType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3258 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3259 | } |
wolfSSL | 15:117db924cf7c | 3260 | |
wolfSSL | 15:117db924cf7c | 3261 | first = input[inOutIdx - 2]; /* PKCS version always 2nd to last byte */ |
wolfSSL | 15:117db924cf7c | 3262 | second = input[inOutIdx - 1]; /* version.algo, algo id last byte */ |
wolfSSL | 15:117db924cf7c | 3263 | |
wolfSSL | 15:117db924cf7c | 3264 | if (CheckAlgo(first, second, &id, &version) < 0) { |
wolfSSL | 15:117db924cf7c | 3265 | ERROR_OUT(ASN_INPUT_E, exit_dc); /* Algo ID error */ |
wolfSSL | 15:117db924cf7c | 3266 | } |
wolfSSL | 15:117db924cf7c | 3267 | |
wolfSSL | 15:117db924cf7c | 3268 | if (version == PKCS5v2) { |
wolfSSL | 15:117db924cf7c | 3269 | if (GetSequence(input, &inOutIdx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3270 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3271 | } |
wolfSSL | 15:117db924cf7c | 3272 | |
wolfSSL | 15:117db924cf7c | 3273 | if (GetAlgoId(input, &inOutIdx, &oid, oidKdfType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3274 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3275 | } |
wolfSSL | 15:117db924cf7c | 3276 | |
wolfSSL | 15:117db924cf7c | 3277 | if (oid != PBKDF2_OID) { |
wolfSSL | 15:117db924cf7c | 3278 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3279 | } |
wolfSSL | 15:117db924cf7c | 3280 | } |
wolfSSL | 15:117db924cf7c | 3281 | |
wolfSSL | 15:117db924cf7c | 3282 | if (GetSequence(input, &inOutIdx, &length, sz) <= 0) { |
wolfSSL | 15:117db924cf7c | 3283 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3284 | } |
wolfSSL | 15:117db924cf7c | 3285 | /* Find the end of this SEQUENCE so we can check for the OPTIONAL and |
wolfSSL | 15:117db924cf7c | 3286 | * DEFAULT items. */ |
wolfSSL | 15:117db924cf7c | 3287 | seqEnd = inOutIdx + length; |
wolfSSL | 15:117db924cf7c | 3288 | |
wolfSSL | 15:117db924cf7c | 3289 | ret = GetOctetString(input, &inOutIdx, &saltSz, sz); |
wolfSSL | 15:117db924cf7c | 3290 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 3291 | goto exit_dc; |
wolfSSL | 15:117db924cf7c | 3292 | |
wolfSSL | 15:117db924cf7c | 3293 | if (saltSz > MAX_SALT_SIZE) { |
wolfSSL | 15:117db924cf7c | 3294 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3295 | } |
wolfSSL | 15:117db924cf7c | 3296 | |
wolfSSL | 15:117db924cf7c | 3297 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3298 | salt = (byte*)XMALLOC(MAX_SALT_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3299 | if (salt == NULL) { |
wolfSSL | 15:117db924cf7c | 3300 | ERROR_OUT(MEMORY_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3301 | } |
wolfSSL | 15:117db924cf7c | 3302 | #endif |
wolfSSL | 15:117db924cf7c | 3303 | |
wolfSSL | 15:117db924cf7c | 3304 | XMEMCPY(salt, &input[inOutIdx], saltSz); |
wolfSSL | 15:117db924cf7c | 3305 | inOutIdx += saltSz; |
wolfSSL | 15:117db924cf7c | 3306 | |
wolfSSL | 15:117db924cf7c | 3307 | if (GetShortInt(input, &inOutIdx, &iterations, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3308 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3309 | } |
wolfSSL | 15:117db924cf7c | 3310 | |
wolfSSL | 15:117db924cf7c | 3311 | /* OPTIONAL key length */ |
wolfSSL | 15:117db924cf7c | 3312 | if (seqEnd > inOutIdx && input[inOutIdx] == ASN_INTEGER) { |
wolfSSL | 15:117db924cf7c | 3313 | if (GetShortInt(input, &inOutIdx, &keySz, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3314 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3315 | } |
wolfSSL | 15:117db924cf7c | 3316 | } |
wolfSSL | 15:117db924cf7c | 3317 | |
wolfSSL | 15:117db924cf7c | 3318 | /* DEFAULT HMAC is SHA-1 */ |
wolfSSL | 15:117db924cf7c | 3319 | if (seqEnd > inOutIdx) { |
wolfSSL | 15:117db924cf7c | 3320 | if (GetAlgoId(input, &inOutIdx, &oid, oidHmacType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3321 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3322 | } |
wolfSSL | 15:117db924cf7c | 3323 | } |
wolfSSL | 15:117db924cf7c | 3324 | |
wolfSSL | 15:117db924cf7c | 3325 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3326 | cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3327 | if (cbcIv == NULL) { |
wolfSSL | 15:117db924cf7c | 3328 | ERROR_OUT(MEMORY_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3329 | } |
wolfSSL | 15:117db924cf7c | 3330 | #endif |
wolfSSL | 15:117db924cf7c | 3331 | |
wolfSSL | 15:117db924cf7c | 3332 | if (version == PKCS5v2) { |
wolfSSL | 15:117db924cf7c | 3333 | /* get encryption algo */ |
wolfSSL | 15:117db924cf7c | 3334 | if (GetAlgoId(input, &inOutIdx, &oid, oidBlkType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3335 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3336 | } |
wolfSSL | 15:117db924cf7c | 3337 | |
wolfSSL | 15:117db924cf7c | 3338 | if (CheckAlgoV2(oid, &id) < 0) { |
wolfSSL | 15:117db924cf7c | 3339 | ERROR_OUT(ASN_PARSE_E, exit_dc); /* PKCS v2 algo id error */ |
wolfSSL | 15:117db924cf7c | 3340 | } |
wolfSSL | 15:117db924cf7c | 3341 | |
wolfSSL | 15:117db924cf7c | 3342 | ret = GetOctetString(input, &inOutIdx, &length, sz); |
wolfSSL | 15:117db924cf7c | 3343 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 3344 | goto exit_dc; |
wolfSSL | 15:117db924cf7c | 3345 | |
wolfSSL | 15:117db924cf7c | 3346 | if (length > MAX_IV_SIZE) { |
wolfSSL | 15:117db924cf7c | 3347 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3348 | } |
wolfSSL | 15:117db924cf7c | 3349 | |
wolfSSL | 15:117db924cf7c | 3350 | XMEMCPY(cbcIv, &input[inOutIdx], length); |
wolfSSL | 15:117db924cf7c | 3351 | inOutIdx += length; |
wolfSSL | 15:117db924cf7c | 3352 | } |
wolfSSL | 15:117db924cf7c | 3353 | |
wolfSSL | 15:117db924cf7c | 3354 | if (input[inOutIdx++] != (ASN_CONTEXT_SPECIFIC | 0)) { |
wolfSSL | 15:117db924cf7c | 3355 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3356 | } |
wolfSSL | 15:117db924cf7c | 3357 | |
wolfSSL | 15:117db924cf7c | 3358 | if (GetLength(input, &inOutIdx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 3359 | ERROR_OUT(ASN_PARSE_E, exit_dc); |
wolfSSL | 15:117db924cf7c | 3360 | } |
wolfSSL | 15:117db924cf7c | 3361 | |
wolfSSL | 15:117db924cf7c | 3362 | ret = wc_CryptKey(password, passwordSz, salt, saltSz, iterations, id, |
wolfSSL | 15:117db924cf7c | 3363 | input + inOutIdx, length, version, cbcIv, 0); |
wolfSSL | 15:117db924cf7c | 3364 | |
wolfSSL | 15:117db924cf7c | 3365 | exit_dc: |
wolfSSL | 15:117db924cf7c | 3366 | |
wolfSSL | 15:117db924cf7c | 3367 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3368 | XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3369 | XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3370 | #endif |
wolfSSL | 15:117db924cf7c | 3371 | |
wolfSSL | 15:117db924cf7c | 3372 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 3373 | XMEMMOVE(input, input + inOutIdx, length); |
wolfSSL | 15:117db924cf7c | 3374 | ret = length; |
wolfSSL | 15:117db924cf7c | 3375 | } |
wolfSSL | 15:117db924cf7c | 3376 | |
wolfSSL | 15:117db924cf7c | 3377 | return ret; |
wolfSSL | 15:117db924cf7c | 3378 | } |
wolfSSL | 15:117db924cf7c | 3379 | #endif /* NO_PWDBASED */ |
wolfSSL | 15:117db924cf7c | 3380 | |
wolfSSL | 15:117db924cf7c | 3381 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 3382 | |
wolfSSL | 15:117db924cf7c | 3383 | #ifndef HAVE_USER_RSA |
wolfSSL | 15:117db924cf7c | 3384 | int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key, |
wolfSSL | 15:117db924cf7c | 3385 | word32 inSz) |
wolfSSL | 15:117db924cf7c | 3386 | { |
wolfSSL | 15:117db924cf7c | 3387 | int length; |
wolfSSL | 15:117db924cf7c | 3388 | #if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA) |
wolfSSL | 15:117db924cf7c | 3389 | byte b; |
wolfSSL | 15:117db924cf7c | 3390 | #endif |
wolfSSL | 15:117db924cf7c | 3391 | int ret; |
wolfSSL | 15:117db924cf7c | 3392 | |
wolfSSL | 15:117db924cf7c | 3393 | if (input == NULL || inOutIdx == NULL || key == NULL) |
wolfSSL | 15:117db924cf7c | 3394 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3395 | |
wolfSSL | 15:117db924cf7c | 3396 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3397 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3398 | |
wolfSSL | 15:117db924cf7c | 3399 | key->type = RSA_PUBLIC; |
wolfSSL | 15:117db924cf7c | 3400 | |
wolfSSL | 15:117db924cf7c | 3401 | #if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA) |
wolfSSL | 15:117db924cf7c | 3402 | if ((*inOutIdx + 1) > inSz) |
wolfSSL | 15:117db924cf7c | 3403 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 3404 | |
wolfSSL | 15:117db924cf7c | 3405 | b = input[*inOutIdx]; |
wolfSSL | 15:117db924cf7c | 3406 | if (b != ASN_INTEGER) { |
wolfSSL | 15:117db924cf7c | 3407 | /* not from decoded cert, will have algo id, skip past */ |
wolfSSL | 15:117db924cf7c | 3408 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3409 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3410 | |
wolfSSL | 15:117db924cf7c | 3411 | if (SkipObjectId(input, inOutIdx, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3412 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3413 | |
wolfSSL | 15:117db924cf7c | 3414 | /* Option NULL ASN.1 tag */ |
wolfSSL | 15:117db924cf7c | 3415 | if (*inOutIdx >= inSz) { |
wolfSSL | 15:117db924cf7c | 3416 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 3417 | } |
wolfSSL | 15:117db924cf7c | 3418 | if (input[*inOutIdx] == ASN_TAG_NULL) { |
wolfSSL | 15:117db924cf7c | 3419 | ret = GetASNNull(input, inOutIdx, inSz); |
wolfSSL | 15:117db924cf7c | 3420 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 3421 | return ret; |
wolfSSL | 15:117db924cf7c | 3422 | } |
wolfSSL | 15:117db924cf7c | 3423 | |
wolfSSL | 15:117db924cf7c | 3424 | /* should have bit tag length and seq next */ |
wolfSSL | 15:117db924cf7c | 3425 | ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL); |
wolfSSL | 15:117db924cf7c | 3426 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 3427 | return ret; |
wolfSSL | 15:117db924cf7c | 3428 | |
wolfSSL | 15:117db924cf7c | 3429 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3430 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3431 | } |
wolfSSL | 15:117db924cf7c | 3432 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 3433 | |
wolfSSL | 15:117db924cf7c | 3434 | if (GetInt(&key->n, input, inOutIdx, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3435 | return ASN_RSA_KEY_E; |
wolfSSL | 15:117db924cf7c | 3436 | if (GetInt(&key->e, input, inOutIdx, inSz) < 0) { |
wolfSSL | 15:117db924cf7c | 3437 | mp_clear(&key->n); |
wolfSSL | 15:117db924cf7c | 3438 | return ASN_RSA_KEY_E; |
wolfSSL | 15:117db924cf7c | 3439 | } |
wolfSSL | 15:117db924cf7c | 3440 | |
wolfSSL | 15:117db924cf7c | 3441 | #ifdef WOLFSSL_XILINX_CRYPT |
wolfSSL | 15:117db924cf7c | 3442 | if (wc_InitRsaHw(key) != 0) { |
wolfSSL | 15:117db924cf7c | 3443 | return BAD_STATE_E; |
wolfSSL | 15:117db924cf7c | 3444 | } |
wolfSSL | 15:117db924cf7c | 3445 | #endif |
wolfSSL | 15:117db924cf7c | 3446 | |
wolfSSL | 15:117db924cf7c | 3447 | return 0; |
wolfSSL | 15:117db924cf7c | 3448 | } |
wolfSSL | 15:117db924cf7c | 3449 | |
wolfSSL | 15:117db924cf7c | 3450 | /* import RSA public key elements (n, e) into RsaKey structure (key) */ |
wolfSSL | 15:117db924cf7c | 3451 | int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz, const byte* e, |
wolfSSL | 15:117db924cf7c | 3452 | word32 eSz, RsaKey* key) |
wolfSSL | 15:117db924cf7c | 3453 | { |
wolfSSL | 15:117db924cf7c | 3454 | if (n == NULL || e == NULL || key == NULL) |
wolfSSL | 15:117db924cf7c | 3455 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3456 | |
wolfSSL | 15:117db924cf7c | 3457 | key->type = RSA_PUBLIC; |
wolfSSL | 15:117db924cf7c | 3458 | |
wolfSSL | 15:117db924cf7c | 3459 | if (mp_init(&key->n) != MP_OKAY) |
wolfSSL | 15:117db924cf7c | 3460 | return MP_INIT_E; |
wolfSSL | 15:117db924cf7c | 3461 | |
wolfSSL | 15:117db924cf7c | 3462 | if (mp_read_unsigned_bin(&key->n, n, nSz) != 0) { |
wolfSSL | 15:117db924cf7c | 3463 | mp_clear(&key->n); |
wolfSSL | 15:117db924cf7c | 3464 | return ASN_GETINT_E; |
wolfSSL | 15:117db924cf7c | 3465 | } |
wolfSSL | 15:117db924cf7c | 3466 | |
wolfSSL | 15:117db924cf7c | 3467 | if (mp_init(&key->e) != MP_OKAY) { |
wolfSSL | 15:117db924cf7c | 3468 | mp_clear(&key->n); |
wolfSSL | 15:117db924cf7c | 3469 | return MP_INIT_E; |
wolfSSL | 15:117db924cf7c | 3470 | } |
wolfSSL | 15:117db924cf7c | 3471 | |
wolfSSL | 15:117db924cf7c | 3472 | if (mp_read_unsigned_bin(&key->e, e, eSz) != 0) { |
wolfSSL | 15:117db924cf7c | 3473 | mp_clear(&key->n); |
wolfSSL | 15:117db924cf7c | 3474 | mp_clear(&key->e); |
wolfSSL | 15:117db924cf7c | 3475 | return ASN_GETINT_E; |
wolfSSL | 15:117db924cf7c | 3476 | } |
wolfSSL | 15:117db924cf7c | 3477 | |
wolfSSL | 15:117db924cf7c | 3478 | #ifdef WOLFSSL_XILINX_CRYPT |
wolfSSL | 15:117db924cf7c | 3479 | if (wc_InitRsaHw(key) != 0) { |
wolfSSL | 15:117db924cf7c | 3480 | return BAD_STATE_E; |
wolfSSL | 15:117db924cf7c | 3481 | } |
wolfSSL | 15:117db924cf7c | 3482 | #endif |
wolfSSL | 15:117db924cf7c | 3483 | |
wolfSSL | 15:117db924cf7c | 3484 | return 0; |
wolfSSL | 15:117db924cf7c | 3485 | } |
wolfSSL | 15:117db924cf7c | 3486 | #endif /* HAVE_USER_RSA */ |
wolfSSL | 15:117db924cf7c | 3487 | #endif |
wolfSSL | 15:117db924cf7c | 3488 | |
wolfSSL | 15:117db924cf7c | 3489 | #ifndef NO_DH |
wolfSSL | 15:117db924cf7c | 3490 | |
wolfSSL | 15:117db924cf7c | 3491 | int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz) |
wolfSSL | 15:117db924cf7c | 3492 | { |
wolfSSL | 15:117db924cf7c | 3493 | int length; |
wolfSSL | 15:117db924cf7c | 3494 | |
wolfSSL | 15:117db924cf7c | 3495 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3496 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3497 | |
wolfSSL | 15:117db924cf7c | 3498 | if (GetInt(&key->p, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 3499 | GetInt(&key->g, input, inOutIdx, inSz) < 0) { |
wolfSSL | 15:117db924cf7c | 3500 | return ASN_DH_KEY_E; |
wolfSSL | 15:117db924cf7c | 3501 | } |
wolfSSL | 15:117db924cf7c | 3502 | |
wolfSSL | 15:117db924cf7c | 3503 | return 0; |
wolfSSL | 15:117db924cf7c | 3504 | } |
wolfSSL | 15:117db924cf7c | 3505 | |
wolfSSL | 15:117db924cf7c | 3506 | |
wolfSSL | 15:117db924cf7c | 3507 | int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p, word32* pInOutSz, |
wolfSSL | 15:117db924cf7c | 3508 | byte* g, word32* gInOutSz) |
wolfSSL | 15:117db924cf7c | 3509 | { |
wolfSSL | 15:117db924cf7c | 3510 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 3511 | int ret; |
wolfSSL | 15:117db924cf7c | 3512 | int length; |
wolfSSL | 15:117db924cf7c | 3513 | |
wolfSSL | 15:117db924cf7c | 3514 | if (GetSequence(input, &idx, &length, inSz) <= 0) |
wolfSSL | 15:117db924cf7c | 3515 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3516 | |
wolfSSL | 15:117db924cf7c | 3517 | ret = GetASNInt(input, &idx, &length, inSz); |
wolfSSL | 15:117db924cf7c | 3518 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 3519 | return ret; |
wolfSSL | 15:117db924cf7c | 3520 | |
wolfSSL | 15:117db924cf7c | 3521 | if (length <= (int)*pInOutSz) { |
wolfSSL | 15:117db924cf7c | 3522 | XMEMCPY(p, &input[idx], length); |
wolfSSL | 15:117db924cf7c | 3523 | *pInOutSz = length; |
wolfSSL | 15:117db924cf7c | 3524 | } |
wolfSSL | 15:117db924cf7c | 3525 | else { |
wolfSSL | 15:117db924cf7c | 3526 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 3527 | } |
wolfSSL | 15:117db924cf7c | 3528 | idx += length; |
wolfSSL | 15:117db924cf7c | 3529 | |
wolfSSL | 15:117db924cf7c | 3530 | ret = GetASNInt(input, &idx, &length, inSz); |
wolfSSL | 15:117db924cf7c | 3531 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 3532 | return ret; |
wolfSSL | 15:117db924cf7c | 3533 | |
wolfSSL | 15:117db924cf7c | 3534 | if (length <= (int)*gInOutSz) { |
wolfSSL | 15:117db924cf7c | 3535 | XMEMCPY(g, &input[idx], length); |
wolfSSL | 15:117db924cf7c | 3536 | *gInOutSz = length; |
wolfSSL | 15:117db924cf7c | 3537 | } |
wolfSSL | 15:117db924cf7c | 3538 | else { |
wolfSSL | 15:117db924cf7c | 3539 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 3540 | } |
wolfSSL | 15:117db924cf7c | 3541 | |
wolfSSL | 15:117db924cf7c | 3542 | return 0; |
wolfSSL | 15:117db924cf7c | 3543 | } |
wolfSSL | 15:117db924cf7c | 3544 | |
wolfSSL | 15:117db924cf7c | 3545 | #endif /* NO_DH */ |
wolfSSL | 15:117db924cf7c | 3546 | |
wolfSSL | 15:117db924cf7c | 3547 | |
wolfSSL | 15:117db924cf7c | 3548 | #ifndef NO_DSA |
wolfSSL | 15:117db924cf7c | 3549 | |
wolfSSL | 15:117db924cf7c | 3550 | int DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, |
wolfSSL | 15:117db924cf7c | 3551 | word32 inSz) |
wolfSSL | 15:117db924cf7c | 3552 | { |
wolfSSL | 15:117db924cf7c | 3553 | int length; |
wolfSSL | 15:117db924cf7c | 3554 | |
wolfSSL | 15:117db924cf7c | 3555 | if (input == NULL || inOutIdx == NULL || key == NULL) { |
wolfSSL | 15:117db924cf7c | 3556 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3557 | } |
wolfSSL | 15:117db924cf7c | 3558 | |
wolfSSL | 15:117db924cf7c | 3559 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3560 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3561 | |
wolfSSL | 15:117db924cf7c | 3562 | if (GetInt(&key->p, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 3563 | GetInt(&key->q, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 3564 | GetInt(&key->g, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 3565 | GetInt(&key->y, input, inOutIdx, inSz) < 0 ) |
wolfSSL | 15:117db924cf7c | 3566 | return ASN_DH_KEY_E; |
wolfSSL | 15:117db924cf7c | 3567 | |
wolfSSL | 15:117db924cf7c | 3568 | key->type = DSA_PUBLIC; |
wolfSSL | 15:117db924cf7c | 3569 | return 0; |
wolfSSL | 15:117db924cf7c | 3570 | } |
wolfSSL | 15:117db924cf7c | 3571 | |
wolfSSL | 15:117db924cf7c | 3572 | |
wolfSSL | 15:117db924cf7c | 3573 | int DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, |
wolfSSL | 15:117db924cf7c | 3574 | word32 inSz) |
wolfSSL | 15:117db924cf7c | 3575 | { |
wolfSSL | 15:117db924cf7c | 3576 | int length, version; |
wolfSSL | 15:117db924cf7c | 3577 | |
wolfSSL | 15:117db924cf7c | 3578 | /* Sanity checks on input */ |
wolfSSL | 15:117db924cf7c | 3579 | if (input == NULL || inOutIdx == NULL || key == NULL) { |
wolfSSL | 15:117db924cf7c | 3580 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3581 | } |
wolfSSL | 15:117db924cf7c | 3582 | |
wolfSSL | 15:117db924cf7c | 3583 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3584 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3585 | |
wolfSSL | 15:117db924cf7c | 3586 | if (GetMyVersion(input, inOutIdx, &version, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 3587 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3588 | |
wolfSSL | 15:117db924cf7c | 3589 | if (GetInt(&key->p, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 3590 | GetInt(&key->q, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 3591 | GetInt(&key->g, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 3592 | GetInt(&key->y, input, inOutIdx, inSz) < 0 || |
wolfSSL | 15:117db924cf7c | 3593 | GetInt(&key->x, input, inOutIdx, inSz) < 0 ) |
wolfSSL | 15:117db924cf7c | 3594 | return ASN_DH_KEY_E; |
wolfSSL | 15:117db924cf7c | 3595 | |
wolfSSL | 15:117db924cf7c | 3596 | key->type = DSA_PRIVATE; |
wolfSSL | 15:117db924cf7c | 3597 | return 0; |
wolfSSL | 15:117db924cf7c | 3598 | } |
wolfSSL | 15:117db924cf7c | 3599 | |
wolfSSL | 15:117db924cf7c | 3600 | static mp_int* GetDsaInt(DsaKey* key, int idx) |
wolfSSL | 15:117db924cf7c | 3601 | { |
wolfSSL | 15:117db924cf7c | 3602 | if (idx == 0) |
wolfSSL | 15:117db924cf7c | 3603 | return &key->p; |
wolfSSL | 15:117db924cf7c | 3604 | if (idx == 1) |
wolfSSL | 15:117db924cf7c | 3605 | return &key->q; |
wolfSSL | 15:117db924cf7c | 3606 | if (idx == 2) |
wolfSSL | 15:117db924cf7c | 3607 | return &key->g; |
wolfSSL | 15:117db924cf7c | 3608 | if (idx == 3) |
wolfSSL | 15:117db924cf7c | 3609 | return &key->y; |
wolfSSL | 15:117db924cf7c | 3610 | if (idx == 4) |
wolfSSL | 15:117db924cf7c | 3611 | return &key->x; |
wolfSSL | 15:117db924cf7c | 3612 | |
wolfSSL | 15:117db924cf7c | 3613 | return NULL; |
wolfSSL | 15:117db924cf7c | 3614 | } |
wolfSSL | 15:117db924cf7c | 3615 | |
wolfSSL | 15:117db924cf7c | 3616 | /* Release Tmp DSA resources */ |
wolfSSL | 15:117db924cf7c | 3617 | static WC_INLINE void FreeTmpDsas(byte** tmps, void* heap) |
wolfSSL | 15:117db924cf7c | 3618 | { |
wolfSSL | 15:117db924cf7c | 3619 | int i; |
wolfSSL | 15:117db924cf7c | 3620 | |
wolfSSL | 15:117db924cf7c | 3621 | for (i = 0; i < DSA_INTS; i++) |
wolfSSL | 15:117db924cf7c | 3622 | XFREE(tmps[i], heap, DYNAMIC_TYPE_DSA); |
wolfSSL | 15:117db924cf7c | 3623 | |
wolfSSL | 15:117db924cf7c | 3624 | (void)heap; |
wolfSSL | 15:117db924cf7c | 3625 | } |
wolfSSL | 15:117db924cf7c | 3626 | |
wolfSSL | 15:117db924cf7c | 3627 | /* Convert DsaKey key to DER format, write to output (inLen), return bytes |
wolfSSL | 15:117db924cf7c | 3628 | written */ |
wolfSSL | 15:117db924cf7c | 3629 | int wc_DsaKeyToDer(DsaKey* key, byte* output, word32 inLen) |
wolfSSL | 15:117db924cf7c | 3630 | { |
wolfSSL | 15:117db924cf7c | 3631 | word32 seqSz, verSz, rawLen, intTotalLen = 0; |
wolfSSL | 15:117db924cf7c | 3632 | word32 sizes[DSA_INTS]; |
wolfSSL | 15:117db924cf7c | 3633 | int i, j, outLen, ret = 0, mpSz; |
wolfSSL | 15:117db924cf7c | 3634 | |
wolfSSL | 15:117db924cf7c | 3635 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 3636 | byte ver[MAX_VERSION_SZ]; |
wolfSSL | 15:117db924cf7c | 3637 | byte* tmps[DSA_INTS]; |
wolfSSL | 15:117db924cf7c | 3638 | |
wolfSSL | 15:117db924cf7c | 3639 | if (!key || !output) |
wolfSSL | 15:117db924cf7c | 3640 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3641 | |
wolfSSL | 15:117db924cf7c | 3642 | if (key->type != DSA_PRIVATE) |
wolfSSL | 15:117db924cf7c | 3643 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3644 | |
wolfSSL | 15:117db924cf7c | 3645 | for (i = 0; i < DSA_INTS; i++) |
wolfSSL | 15:117db924cf7c | 3646 | tmps[i] = NULL; |
wolfSSL | 15:117db924cf7c | 3647 | |
wolfSSL | 15:117db924cf7c | 3648 | /* write all big ints from key to DER tmps */ |
wolfSSL | 15:117db924cf7c | 3649 | for (i = 0; i < DSA_INTS; i++) { |
wolfSSL | 15:117db924cf7c | 3650 | mp_int* keyInt = GetDsaInt(key, i); |
wolfSSL | 15:117db924cf7c | 3651 | |
wolfSSL | 15:117db924cf7c | 3652 | rawLen = mp_unsigned_bin_size(keyInt) + 1; |
wolfSSL | 15:117db924cf7c | 3653 | tmps[i] = (byte*)XMALLOC(rawLen + MAX_SEQ_SZ, key->heap, |
wolfSSL | 15:117db924cf7c | 3654 | DYNAMIC_TYPE_DSA); |
wolfSSL | 15:117db924cf7c | 3655 | if (tmps[i] == NULL) { |
wolfSSL | 15:117db924cf7c | 3656 | ret = MEMORY_E; |
wolfSSL | 15:117db924cf7c | 3657 | break; |
wolfSSL | 15:117db924cf7c | 3658 | } |
wolfSSL | 15:117db924cf7c | 3659 | |
wolfSSL | 15:117db924cf7c | 3660 | mpSz = SetASNIntMP(keyInt, -1, tmps[i]); |
wolfSSL | 15:117db924cf7c | 3661 | if (mpSz < 0) { |
wolfSSL | 15:117db924cf7c | 3662 | ret = mpSz; |
wolfSSL | 15:117db924cf7c | 3663 | break; |
wolfSSL | 15:117db924cf7c | 3664 | } |
wolfSSL | 15:117db924cf7c | 3665 | intTotalLen += (sizes[i] = mpSz); |
wolfSSL | 15:117db924cf7c | 3666 | } |
wolfSSL | 15:117db924cf7c | 3667 | |
wolfSSL | 15:117db924cf7c | 3668 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 3669 | FreeTmpDsas(tmps, key->heap); |
wolfSSL | 15:117db924cf7c | 3670 | return ret; |
wolfSSL | 15:117db924cf7c | 3671 | } |
wolfSSL | 15:117db924cf7c | 3672 | |
wolfSSL | 15:117db924cf7c | 3673 | /* make headers */ |
wolfSSL | 15:117db924cf7c | 3674 | verSz = SetMyVersion(0, ver, FALSE); |
wolfSSL | 15:117db924cf7c | 3675 | seqSz = SetSequence(verSz + intTotalLen, seq); |
wolfSSL | 15:117db924cf7c | 3676 | |
wolfSSL | 15:117db924cf7c | 3677 | outLen = seqSz + verSz + intTotalLen; |
wolfSSL | 15:117db924cf7c | 3678 | if (outLen > (int)inLen) |
wolfSSL | 15:117db924cf7c | 3679 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 3680 | |
wolfSSL | 15:117db924cf7c | 3681 | /* write to output */ |
wolfSSL | 15:117db924cf7c | 3682 | XMEMCPY(output, seq, seqSz); |
wolfSSL | 15:117db924cf7c | 3683 | j = seqSz; |
wolfSSL | 15:117db924cf7c | 3684 | XMEMCPY(output + j, ver, verSz); |
wolfSSL | 15:117db924cf7c | 3685 | j += verSz; |
wolfSSL | 15:117db924cf7c | 3686 | |
wolfSSL | 15:117db924cf7c | 3687 | for (i = 0; i < DSA_INTS; i++) { |
wolfSSL | 15:117db924cf7c | 3688 | XMEMCPY(output + j, tmps[i], sizes[i]); |
wolfSSL | 15:117db924cf7c | 3689 | j += sizes[i]; |
wolfSSL | 15:117db924cf7c | 3690 | } |
wolfSSL | 15:117db924cf7c | 3691 | FreeTmpDsas(tmps, key->heap); |
wolfSSL | 15:117db924cf7c | 3692 | |
wolfSSL | 15:117db924cf7c | 3693 | return outLen; |
wolfSSL | 15:117db924cf7c | 3694 | } |
wolfSSL | 15:117db924cf7c | 3695 | |
wolfSSL | 15:117db924cf7c | 3696 | #endif /* NO_DSA */ |
wolfSSL | 15:117db924cf7c | 3697 | |
wolfSSL | 15:117db924cf7c | 3698 | |
wolfSSL | 15:117db924cf7c | 3699 | void InitDecodedCert(DecodedCert* cert, byte* source, word32 inSz, void* heap) |
wolfSSL | 15:117db924cf7c | 3700 | { |
wolfSSL | 15:117db924cf7c | 3701 | if (cert != NULL) { |
wolfSSL | 15:117db924cf7c | 3702 | XMEMSET(cert, 0, sizeof(DecodedCert)); |
wolfSSL | 15:117db924cf7c | 3703 | |
wolfSSL | 15:117db924cf7c | 3704 | cert->subjectCNEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 3705 | cert->issuer[0] = '\0'; |
wolfSSL | 15:117db924cf7c | 3706 | cert->subject[0] = '\0'; |
wolfSSL | 15:117db924cf7c | 3707 | cert->source = source; /* don't own */ |
wolfSSL | 15:117db924cf7c | 3708 | cert->maxIdx = inSz; /* can't go over this index */ |
wolfSSL | 15:117db924cf7c | 3709 | cert->heap = heap; |
wolfSSL | 15:117db924cf7c | 3710 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 3711 | cert->subjectSNEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 3712 | cert->subjectCEnc = CTC_PRINTABLE; |
wolfSSL | 15:117db924cf7c | 3713 | cert->subjectLEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 3714 | cert->subjectSTEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 3715 | cert->subjectOEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 3716 | cert->subjectOUEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 3717 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 3718 | |
wolfSSL | 15:117db924cf7c | 3719 | InitSignatureCtx(&cert->sigCtx, heap, INVALID_DEVID); |
wolfSSL | 15:117db924cf7c | 3720 | } |
wolfSSL | 15:117db924cf7c | 3721 | } |
wolfSSL | 15:117db924cf7c | 3722 | |
wolfSSL | 15:117db924cf7c | 3723 | |
wolfSSL | 15:117db924cf7c | 3724 | void FreeAltNames(DNS_entry* altNames, void* heap) |
wolfSSL | 15:117db924cf7c | 3725 | { |
wolfSSL | 15:117db924cf7c | 3726 | (void)heap; |
wolfSSL | 15:117db924cf7c | 3727 | |
wolfSSL | 15:117db924cf7c | 3728 | while (altNames) { |
wolfSSL | 15:117db924cf7c | 3729 | DNS_entry* tmp = altNames->next; |
wolfSSL | 15:117db924cf7c | 3730 | |
wolfSSL | 15:117db924cf7c | 3731 | XFREE(altNames->name, heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 3732 | XFREE(altNames, heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 3733 | altNames = tmp; |
wolfSSL | 15:117db924cf7c | 3734 | } |
wolfSSL | 15:117db924cf7c | 3735 | } |
wolfSSL | 15:117db924cf7c | 3736 | |
wolfSSL | 15:117db924cf7c | 3737 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 3738 | |
wolfSSL | 15:117db924cf7c | 3739 | void FreeNameSubtrees(Base_entry* names, void* heap) |
wolfSSL | 15:117db924cf7c | 3740 | { |
wolfSSL | 15:117db924cf7c | 3741 | (void)heap; |
wolfSSL | 15:117db924cf7c | 3742 | |
wolfSSL | 15:117db924cf7c | 3743 | while (names) { |
wolfSSL | 15:117db924cf7c | 3744 | Base_entry* tmp = names->next; |
wolfSSL | 15:117db924cf7c | 3745 | |
wolfSSL | 15:117db924cf7c | 3746 | XFREE(names->name, heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 3747 | XFREE(names, heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 3748 | names = tmp; |
wolfSSL | 15:117db924cf7c | 3749 | } |
wolfSSL | 15:117db924cf7c | 3750 | } |
wolfSSL | 15:117db924cf7c | 3751 | |
wolfSSL | 15:117db924cf7c | 3752 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 3753 | |
wolfSSL | 15:117db924cf7c | 3754 | void FreeDecodedCert(DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 3755 | { |
wolfSSL | 15:117db924cf7c | 3756 | if (cert->subjectCNStored == 1) |
wolfSSL | 15:117db924cf7c | 3757 | XFREE(cert->subjectCN, cert->heap, DYNAMIC_TYPE_SUBJECT_CN); |
wolfSSL | 15:117db924cf7c | 3758 | if (cert->pubKeyStored == 1) |
wolfSSL | 15:117db924cf7c | 3759 | XFREE(cert->publicKey, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 15:117db924cf7c | 3760 | if (cert->weOwnAltNames && cert->altNames) |
wolfSSL | 15:117db924cf7c | 3761 | FreeAltNames(cert->altNames, cert->heap); |
wolfSSL | 15:117db924cf7c | 3762 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 3763 | if (cert->altEmailNames) |
wolfSSL | 15:117db924cf7c | 3764 | FreeAltNames(cert->altEmailNames, cert->heap); |
wolfSSL | 15:117db924cf7c | 3765 | if (cert->permittedNames) |
wolfSSL | 15:117db924cf7c | 3766 | FreeNameSubtrees(cert->permittedNames, cert->heap); |
wolfSSL | 15:117db924cf7c | 3767 | if (cert->excludedNames) |
wolfSSL | 15:117db924cf7c | 3768 | FreeNameSubtrees(cert->excludedNames, cert->heap); |
wolfSSL | 15:117db924cf7c | 3769 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 3770 | #ifdef WOLFSSL_SEP |
wolfSSL | 15:117db924cf7c | 3771 | XFREE(cert->deviceType, cert->heap, DYNAMIC_TYPE_X509_EXT); |
wolfSSL | 15:117db924cf7c | 3772 | XFREE(cert->hwType, cert->heap, DYNAMIC_TYPE_X509_EXT); |
wolfSSL | 15:117db924cf7c | 3773 | XFREE(cert->hwSerialNum, cert->heap, DYNAMIC_TYPE_X509_EXT); |
wolfSSL | 15:117db924cf7c | 3774 | #endif /* WOLFSSL_SEP */ |
wolfSSL | 15:117db924cf7c | 3775 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 3776 | if (cert->issuerName.fullName != NULL) |
wolfSSL | 15:117db924cf7c | 3777 | XFREE(cert->issuerName.fullName, cert->heap, DYNAMIC_TYPE_X509); |
wolfSSL | 15:117db924cf7c | 3778 | if (cert->subjectName.fullName != NULL) |
wolfSSL | 15:117db924cf7c | 3779 | XFREE(cert->subjectName.fullName, cert->heap, DYNAMIC_TYPE_X509); |
wolfSSL | 15:117db924cf7c | 3780 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 3781 | FreeSignatureCtx(&cert->sigCtx); |
wolfSSL | 15:117db924cf7c | 3782 | } |
wolfSSL | 15:117db924cf7c | 3783 | |
wolfSSL | 15:117db924cf7c | 3784 | static int GetCertHeader(DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 3785 | { |
wolfSSL | 15:117db924cf7c | 3786 | int ret = 0, len; |
wolfSSL | 15:117db924cf7c | 3787 | |
wolfSSL | 15:117db924cf7c | 3788 | if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 3789 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3790 | |
wolfSSL | 15:117db924cf7c | 3791 | cert->certBegin = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 3792 | |
wolfSSL | 15:117db924cf7c | 3793 | if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 3794 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3795 | cert->sigIndex = len + cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 3796 | |
wolfSSL | 15:117db924cf7c | 3797 | if (GetExplicitVersion(cert->source, &cert->srcIdx, &cert->version, |
wolfSSL | 15:117db924cf7c | 3798 | cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 3799 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3800 | |
wolfSSL | 15:117db924cf7c | 3801 | if (GetSerialNumber(cert->source, &cert->srcIdx, cert->serial, |
wolfSSL | 15:117db924cf7c | 3802 | &cert->serialSz, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 3803 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3804 | |
wolfSSL | 15:117db924cf7c | 3805 | return ret; |
wolfSSL | 15:117db924cf7c | 3806 | } |
wolfSSL | 15:117db924cf7c | 3807 | |
wolfSSL | 15:117db924cf7c | 3808 | #if !defined(NO_RSA) |
wolfSSL | 15:117db924cf7c | 3809 | /* Store Rsa Key, may save later, Dsa could use in future */ |
wolfSSL | 15:117db924cf7c | 3810 | static int StoreRsaKey(DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 3811 | { |
wolfSSL | 15:117db924cf7c | 3812 | int length; |
wolfSSL | 15:117db924cf7c | 3813 | word32 recvd = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 3814 | |
wolfSSL | 15:117db924cf7c | 3815 | if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 3816 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3817 | |
wolfSSL | 15:117db924cf7c | 3818 | recvd = cert->srcIdx - recvd; |
wolfSSL | 15:117db924cf7c | 3819 | length += recvd; |
wolfSSL | 15:117db924cf7c | 3820 | |
wolfSSL | 15:117db924cf7c | 3821 | while (recvd--) |
wolfSSL | 15:117db924cf7c | 3822 | cert->srcIdx--; |
wolfSSL | 15:117db924cf7c | 3823 | |
wolfSSL | 15:117db924cf7c | 3824 | cert->pubKeySize = length; |
wolfSSL | 15:117db924cf7c | 3825 | cert->publicKey = cert->source + cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 3826 | cert->srcIdx += length; |
wolfSSL | 15:117db924cf7c | 3827 | |
wolfSSL | 15:117db924cf7c | 3828 | return 0; |
wolfSSL | 15:117db924cf7c | 3829 | } |
wolfSSL | 15:117db924cf7c | 3830 | #endif /* !NO_RSA */ |
wolfSSL | 15:117db924cf7c | 3831 | |
wolfSSL | 15:117db924cf7c | 3832 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 3833 | |
wolfSSL | 15:117db924cf7c | 3834 | /* return 0 on success if the ECC curve oid sum is supported */ |
wolfSSL | 15:117db924cf7c | 3835 | static int CheckCurve(word32 oid) |
wolfSSL | 15:117db924cf7c | 3836 | { |
wolfSSL | 15:117db924cf7c | 3837 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 3838 | word32 oidSz = 0; |
wolfSSL | 15:117db924cf7c | 3839 | |
wolfSSL | 15:117db924cf7c | 3840 | ret = wc_ecc_get_oid(oid, NULL, &oidSz); |
wolfSSL | 15:117db924cf7c | 3841 | if (ret < 0 || oidSz <= 0) { |
wolfSSL | 15:117db924cf7c | 3842 | WOLFSSL_MSG("CheckCurve not found"); |
wolfSSL | 15:117db924cf7c | 3843 | ret = ALGO_ID_E; |
wolfSSL | 15:117db924cf7c | 3844 | } |
wolfSSL | 15:117db924cf7c | 3845 | |
wolfSSL | 15:117db924cf7c | 3846 | return ret; |
wolfSSL | 15:117db924cf7c | 3847 | } |
wolfSSL | 15:117db924cf7c | 3848 | |
wolfSSL | 15:117db924cf7c | 3849 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 3850 | |
wolfSSL | 15:117db924cf7c | 3851 | static int GetKey(DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 3852 | { |
wolfSSL | 15:117db924cf7c | 3853 | int length; |
wolfSSL | 15:117db924cf7c | 3854 | #if defined(HAVE_ECC) || defined(HAVE_NTRU) |
wolfSSL | 15:117db924cf7c | 3855 | int tmpIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 3856 | #endif |
wolfSSL | 15:117db924cf7c | 3857 | |
wolfSSL | 15:117db924cf7c | 3858 | if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 3859 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3860 | |
wolfSSL | 15:117db924cf7c | 3861 | if (GetAlgoId(cert->source, &cert->srcIdx, |
wolfSSL | 15:117db924cf7c | 3862 | &cert->keyOID, oidKeyType, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 3863 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3864 | |
wolfSSL | 15:117db924cf7c | 3865 | switch (cert->keyOID) { |
wolfSSL | 15:117db924cf7c | 3866 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 3867 | case RSAk: |
wolfSSL | 15:117db924cf7c | 3868 | { |
wolfSSL | 15:117db924cf7c | 3869 | int ret; |
wolfSSL | 15:117db924cf7c | 3870 | ret = CheckBitString(cert->source, &cert->srcIdx, NULL, |
wolfSSL | 15:117db924cf7c | 3871 | cert->maxIdx, 1, NULL); |
wolfSSL | 15:117db924cf7c | 3872 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 3873 | return ret; |
wolfSSL | 15:117db924cf7c | 3874 | |
wolfSSL | 15:117db924cf7c | 3875 | return StoreRsaKey(cert); |
wolfSSL | 15:117db924cf7c | 3876 | } |
wolfSSL | 15:117db924cf7c | 3877 | |
wolfSSL | 15:117db924cf7c | 3878 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 3879 | #ifdef HAVE_NTRU |
wolfSSL | 15:117db924cf7c | 3880 | case NTRUk: |
wolfSSL | 15:117db924cf7c | 3881 | { |
wolfSSL | 15:117db924cf7c | 3882 | const byte* key = &cert->source[tmpIdx]; |
wolfSSL | 15:117db924cf7c | 3883 | byte* next = (byte*)key; |
wolfSSL | 15:117db924cf7c | 3884 | word16 keyLen; |
wolfSSL | 15:117db924cf7c | 3885 | word32 rc; |
wolfSSL | 15:117db924cf7c | 3886 | word32 remaining = cert->maxIdx - cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 3887 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3888 | byte* keyBlob = NULL; |
wolfSSL | 15:117db924cf7c | 3889 | #else |
wolfSSL | 15:117db924cf7c | 3890 | byte keyBlob[MAX_NTRU_KEY_SZ]; |
wolfSSL | 15:117db924cf7c | 3891 | #endif |
wolfSSL | 15:117db924cf7c | 3892 | rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key, |
wolfSSL | 15:117db924cf7c | 3893 | &keyLen, NULL, &next, &remaining); |
wolfSSL | 15:117db924cf7c | 3894 | if (rc != NTRU_OK) |
wolfSSL | 15:117db924cf7c | 3895 | return ASN_NTRU_KEY_E; |
wolfSSL | 15:117db924cf7c | 3896 | if (keyLen > MAX_NTRU_KEY_SZ) |
wolfSSL | 15:117db924cf7c | 3897 | return ASN_NTRU_KEY_E; |
wolfSSL | 15:117db924cf7c | 3898 | |
wolfSSL | 15:117db924cf7c | 3899 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3900 | keyBlob = (byte*)XMALLOC(MAX_NTRU_KEY_SZ, cert->heap, |
wolfSSL | 15:117db924cf7c | 3901 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3902 | if (keyBlob == NULL) |
wolfSSL | 15:117db924cf7c | 3903 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 3904 | #endif |
wolfSSL | 15:117db924cf7c | 3905 | |
wolfSSL | 15:117db924cf7c | 3906 | rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key, |
wolfSSL | 15:117db924cf7c | 3907 | &keyLen, keyBlob, &next, &remaining); |
wolfSSL | 15:117db924cf7c | 3908 | if (rc != NTRU_OK) { |
wolfSSL | 15:117db924cf7c | 3909 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3910 | XFREE(keyBlob, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3911 | #endif |
wolfSSL | 15:117db924cf7c | 3912 | return ASN_NTRU_KEY_E; |
wolfSSL | 15:117db924cf7c | 3913 | } |
wolfSSL | 15:117db924cf7c | 3914 | |
wolfSSL | 15:117db924cf7c | 3915 | if ( (next - key) < 0) { |
wolfSSL | 15:117db924cf7c | 3916 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3917 | XFREE(keyBlob, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3918 | #endif |
wolfSSL | 15:117db924cf7c | 3919 | return ASN_NTRU_KEY_E; |
wolfSSL | 15:117db924cf7c | 3920 | } |
wolfSSL | 15:117db924cf7c | 3921 | |
wolfSSL | 15:117db924cf7c | 3922 | cert->srcIdx = tmpIdx + (int)(next - key); |
wolfSSL | 15:117db924cf7c | 3923 | |
wolfSSL | 15:117db924cf7c | 3924 | cert->publicKey = (byte*)XMALLOC(keyLen, cert->heap, |
wolfSSL | 15:117db924cf7c | 3925 | DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 15:117db924cf7c | 3926 | if (cert->publicKey == NULL) { |
wolfSSL | 15:117db924cf7c | 3927 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3928 | XFREE(keyBlob, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3929 | #endif |
wolfSSL | 15:117db924cf7c | 3930 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 3931 | } |
wolfSSL | 15:117db924cf7c | 3932 | XMEMCPY(cert->publicKey, keyBlob, keyLen); |
wolfSSL | 15:117db924cf7c | 3933 | cert->pubKeyStored = 1; |
wolfSSL | 15:117db924cf7c | 3934 | cert->pubKeySize = keyLen; |
wolfSSL | 15:117db924cf7c | 3935 | |
wolfSSL | 15:117db924cf7c | 3936 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 3937 | XFREE(keyBlob, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 3938 | #endif |
wolfSSL | 15:117db924cf7c | 3939 | |
wolfSSL | 15:117db924cf7c | 3940 | return 0; |
wolfSSL | 15:117db924cf7c | 3941 | } |
wolfSSL | 15:117db924cf7c | 3942 | #endif /* HAVE_NTRU */ |
wolfSSL | 15:117db924cf7c | 3943 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 3944 | case ECDSAk: |
wolfSSL | 15:117db924cf7c | 3945 | { |
wolfSSL | 15:117db924cf7c | 3946 | int ret; |
wolfSSL | 15:117db924cf7c | 3947 | byte seq[5]; |
wolfSSL | 15:117db924cf7c | 3948 | int pubLen = length + 1 + SetLength(length, seq); |
wolfSSL | 15:117db924cf7c | 3949 | |
wolfSSL | 15:117db924cf7c | 3950 | if (cert->source[cert->srcIdx] != |
wolfSSL | 15:117db924cf7c | 3951 | (ASN_SEQUENCE | ASN_CONSTRUCTED)) { |
wolfSSL | 15:117db924cf7c | 3952 | if (GetObjectId(cert->source, &cert->srcIdx, |
wolfSSL | 15:117db924cf7c | 3953 | &cert->pkCurveOID, oidCurveType, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 3954 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 3955 | |
wolfSSL | 15:117db924cf7c | 3956 | if (CheckCurve(cert->pkCurveOID) < 0) |
wolfSSL | 15:117db924cf7c | 3957 | return ECC_CURVE_OID_E; |
wolfSSL | 15:117db924cf7c | 3958 | |
wolfSSL | 15:117db924cf7c | 3959 | /* key header */ |
wolfSSL | 15:117db924cf7c | 3960 | ret = CheckBitString(cert->source, &cert->srcIdx, &length, |
wolfSSL | 15:117db924cf7c | 3961 | cert->maxIdx, 1, NULL); |
wolfSSL | 15:117db924cf7c | 3962 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 3963 | return ret; |
wolfSSL | 15:117db924cf7c | 3964 | } |
wolfSSL | 15:117db924cf7c | 3965 | |
wolfSSL | 15:117db924cf7c | 3966 | cert->publicKey = (byte*)XMALLOC(pubLen, cert->heap, |
wolfSSL | 15:117db924cf7c | 3967 | DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 15:117db924cf7c | 3968 | if (cert->publicKey == NULL) |
wolfSSL | 15:117db924cf7c | 3969 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 3970 | XMEMCPY(cert->publicKey, &cert->source[tmpIdx], pubLen); |
wolfSSL | 15:117db924cf7c | 3971 | cert->pubKeyStored = 1; |
wolfSSL | 15:117db924cf7c | 3972 | cert->pubKeySize = pubLen; |
wolfSSL | 15:117db924cf7c | 3973 | |
wolfSSL | 15:117db924cf7c | 3974 | cert->srcIdx = tmpIdx + pubLen; |
wolfSSL | 15:117db924cf7c | 3975 | |
wolfSSL | 15:117db924cf7c | 3976 | return 0; |
wolfSSL | 15:117db924cf7c | 3977 | } |
wolfSSL | 15:117db924cf7c | 3978 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 3979 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 3980 | case ED25519k: |
wolfSSL | 15:117db924cf7c | 3981 | { |
wolfSSL | 15:117db924cf7c | 3982 | int ret; |
wolfSSL | 15:117db924cf7c | 3983 | |
wolfSSL | 15:117db924cf7c | 3984 | cert->pkCurveOID = ED25519k; |
wolfSSL | 15:117db924cf7c | 3985 | |
wolfSSL | 15:117db924cf7c | 3986 | ret = CheckBitString(cert->source, &cert->srcIdx, &length, |
wolfSSL | 15:117db924cf7c | 3987 | cert->maxIdx, 1, NULL); |
wolfSSL | 15:117db924cf7c | 3988 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 3989 | return ret; |
wolfSSL | 15:117db924cf7c | 3990 | |
wolfSSL | 15:117db924cf7c | 3991 | cert->publicKey = (byte*) XMALLOC(length, cert->heap, |
wolfSSL | 15:117db924cf7c | 3992 | DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 15:117db924cf7c | 3993 | if (cert->publicKey == NULL) |
wolfSSL | 15:117db924cf7c | 3994 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 3995 | XMEMCPY(cert->publicKey, &cert->source[cert->srcIdx], length); |
wolfSSL | 15:117db924cf7c | 3996 | cert->pubKeyStored = 1; |
wolfSSL | 15:117db924cf7c | 3997 | cert->pubKeySize = length; |
wolfSSL | 15:117db924cf7c | 3998 | |
wolfSSL | 15:117db924cf7c | 3999 | cert->srcIdx += length; |
wolfSSL | 15:117db924cf7c | 4000 | |
wolfSSL | 15:117db924cf7c | 4001 | return 0; |
wolfSSL | 15:117db924cf7c | 4002 | } |
wolfSSL | 15:117db924cf7c | 4003 | #endif /* HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 4004 | default: |
wolfSSL | 15:117db924cf7c | 4005 | return ASN_UNKNOWN_OID_E; |
wolfSSL | 15:117db924cf7c | 4006 | } |
wolfSSL | 15:117db924cf7c | 4007 | } |
wolfSSL | 15:117db924cf7c | 4008 | |
wolfSSL | 15:117db924cf7c | 4009 | /* process NAME, either issuer or subject */ |
wolfSSL | 15:117db924cf7c | 4010 | static int GetName(DecodedCert* cert, int nameType) |
wolfSSL | 15:117db924cf7c | 4011 | { |
wolfSSL | 15:117db924cf7c | 4012 | int length; /* length of all distinguished names */ |
wolfSSL | 15:117db924cf7c | 4013 | int dummy; |
wolfSSL | 15:117db924cf7c | 4014 | int ret; |
wolfSSL | 15:117db924cf7c | 4015 | char* full; |
wolfSSL | 15:117db924cf7c | 4016 | byte* hash; |
wolfSSL | 15:117db924cf7c | 4017 | word32 idx; |
wolfSSL | 15:117db924cf7c | 4018 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4019 | DecodedName* dName = |
wolfSSL | 15:117db924cf7c | 4020 | (nameType == ISSUER) ? &cert->issuerName : &cert->subjectName; |
wolfSSL | 15:117db924cf7c | 4021 | int dcnum = 0; |
wolfSSL | 15:117db924cf7c | 4022 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4023 | |
wolfSSL | 15:117db924cf7c | 4024 | WOLFSSL_MSG("Getting Cert Name"); |
wolfSSL | 15:117db924cf7c | 4025 | |
wolfSSL | 15:117db924cf7c | 4026 | if (nameType == ISSUER) { |
wolfSSL | 15:117db924cf7c | 4027 | full = cert->issuer; |
wolfSSL | 15:117db924cf7c | 4028 | hash = cert->issuerHash; |
wolfSSL | 15:117db924cf7c | 4029 | } |
wolfSSL | 15:117db924cf7c | 4030 | else { |
wolfSSL | 15:117db924cf7c | 4031 | full = cert->subject; |
wolfSSL | 15:117db924cf7c | 4032 | hash = cert->subjectHash; |
wolfSSL | 15:117db924cf7c | 4033 | } |
wolfSSL | 15:117db924cf7c | 4034 | |
wolfSSL | 15:117db924cf7c | 4035 | if (cert->srcIdx >= cert->maxIdx) { |
wolfSSL | 15:117db924cf7c | 4036 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 4037 | } |
wolfSSL | 15:117db924cf7c | 4038 | |
wolfSSL | 15:117db924cf7c | 4039 | if (cert->source[cert->srcIdx] == ASN_OBJECT_ID) { |
wolfSSL | 15:117db924cf7c | 4040 | WOLFSSL_MSG("Trying optional prefix..."); |
wolfSSL | 15:117db924cf7c | 4041 | |
wolfSSL | 15:117db924cf7c | 4042 | if (SkipObjectId(cert->source, &cert->srcIdx, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 4043 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4044 | WOLFSSL_MSG("Got optional prefix"); |
wolfSSL | 15:117db924cf7c | 4045 | } |
wolfSSL | 15:117db924cf7c | 4046 | |
wolfSSL | 15:117db924cf7c | 4047 | /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be |
wolfSSL | 15:117db924cf7c | 4048 | * calculated over the entire DER encoding of the Name field, including |
wolfSSL | 15:117db924cf7c | 4049 | * the tag and length. */ |
wolfSSL | 15:117db924cf7c | 4050 | idx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4051 | if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 4052 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4053 | |
wolfSSL | 15:117db924cf7c | 4054 | #ifdef NO_SHA |
wolfSSL | 15:117db924cf7c | 4055 | ret = wc_Sha256Hash(&cert->source[idx], length + cert->srcIdx - idx, hash); |
wolfSSL | 15:117db924cf7c | 4056 | #else |
wolfSSL | 15:117db924cf7c | 4057 | ret = wc_ShaHash(&cert->source[idx], length + cert->srcIdx - idx, hash); |
wolfSSL | 15:117db924cf7c | 4058 | #endif |
wolfSSL | 15:117db924cf7c | 4059 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 4060 | return ret; |
wolfSSL | 15:117db924cf7c | 4061 | |
wolfSSL | 15:117db924cf7c | 4062 | length += cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4063 | idx = 0; |
wolfSSL | 15:117db924cf7c | 4064 | |
wolfSSL | 15:117db924cf7c | 4065 | #ifdef HAVE_PKCS7 |
wolfSSL | 15:117db924cf7c | 4066 | /* store pointer to raw issuer */ |
wolfSSL | 15:117db924cf7c | 4067 | if (nameType == ISSUER) { |
wolfSSL | 15:117db924cf7c | 4068 | cert->issuerRaw = &cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4069 | cert->issuerRawLen = length - cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4070 | } |
wolfSSL | 15:117db924cf7c | 4071 | #endif |
wolfSSL | 15:117db924cf7c | 4072 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 4073 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4074 | cert->subjectRaw = &cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4075 | cert->subjectRawLen = length - cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4076 | } |
wolfSSL | 15:117db924cf7c | 4077 | #endif |
wolfSSL | 15:117db924cf7c | 4078 | |
wolfSSL | 15:117db924cf7c | 4079 | while (cert->srcIdx < (word32)length) { |
wolfSSL | 15:117db924cf7c | 4080 | byte b; |
wolfSSL | 15:117db924cf7c | 4081 | byte joint[2]; |
wolfSSL | 15:117db924cf7c | 4082 | byte tooBig = FALSE; |
wolfSSL | 15:117db924cf7c | 4083 | int oidSz; |
wolfSSL | 15:117db924cf7c | 4084 | |
wolfSSL | 15:117db924cf7c | 4085 | if (GetSet(cert->source, &cert->srcIdx, &dummy, cert->maxIdx) < 0) { |
wolfSSL | 15:117db924cf7c | 4086 | WOLFSSL_MSG("Cert name lacks set header, trying sequence"); |
wolfSSL | 15:117db924cf7c | 4087 | } |
wolfSSL | 15:117db924cf7c | 4088 | |
wolfSSL | 15:117db924cf7c | 4089 | if (GetSequence(cert->source, &cert->srcIdx, &dummy, cert->maxIdx) <= 0) |
wolfSSL | 15:117db924cf7c | 4090 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4091 | |
wolfSSL | 15:117db924cf7c | 4092 | ret = GetASNObjectId(cert->source, &cert->srcIdx, &oidSz, cert->maxIdx); |
wolfSSL | 15:117db924cf7c | 4093 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 4094 | return ret; |
wolfSSL | 15:117db924cf7c | 4095 | |
wolfSSL | 15:117db924cf7c | 4096 | /* make sure there is room for joint */ |
wolfSSL | 15:117db924cf7c | 4097 | if ((cert->srcIdx + sizeof(joint)) > cert->maxIdx) |
wolfSSL | 15:117db924cf7c | 4098 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4099 | |
wolfSSL | 15:117db924cf7c | 4100 | XMEMCPY(joint, &cert->source[cert->srcIdx], sizeof(joint)); |
wolfSSL | 15:117db924cf7c | 4101 | |
wolfSSL | 15:117db924cf7c | 4102 | /* v1 name types */ |
wolfSSL | 15:117db924cf7c | 4103 | if (joint[0] == 0x55 && joint[1] == 0x04) { |
wolfSSL | 15:117db924cf7c | 4104 | const char* copy = NULL; |
wolfSSL | 15:117db924cf7c | 4105 | int strLen; |
wolfSSL | 15:117db924cf7c | 4106 | byte id; |
wolfSSL | 15:117db924cf7c | 4107 | |
wolfSSL | 15:117db924cf7c | 4108 | cert->srcIdx += 2; |
wolfSSL | 15:117db924cf7c | 4109 | id = cert->source[cert->srcIdx++]; |
wolfSSL | 15:117db924cf7c | 4110 | b = cert->source[cert->srcIdx++]; /* encoding */ |
wolfSSL | 15:117db924cf7c | 4111 | |
wolfSSL | 15:117db924cf7c | 4112 | if (GetLength(cert->source, &cert->srcIdx, &strLen, |
wolfSSL | 15:117db924cf7c | 4113 | cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 4114 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4115 | |
wolfSSL | 15:117db924cf7c | 4116 | if ( (strLen + 14) > (int)(ASN_NAME_MAX - idx)) { |
wolfSSL | 15:117db924cf7c | 4117 | /* include biggest pre fix header too 4 = "/serialNumber=" */ |
wolfSSL | 15:117db924cf7c | 4118 | WOLFSSL_MSG("ASN Name too big, skipping"); |
wolfSSL | 15:117db924cf7c | 4119 | tooBig = TRUE; |
wolfSSL | 15:117db924cf7c | 4120 | } |
wolfSSL | 15:117db924cf7c | 4121 | |
wolfSSL | 15:117db924cf7c | 4122 | if (id == ASN_COMMON_NAME) { |
wolfSSL | 15:117db924cf7c | 4123 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4124 | cert->subjectCN = (char *)&cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4125 | cert->subjectCNLen = strLen; |
wolfSSL | 15:117db924cf7c | 4126 | cert->subjectCNEnc = b; |
wolfSSL | 15:117db924cf7c | 4127 | } |
wolfSSL | 15:117db924cf7c | 4128 | |
wolfSSL | 15:117db924cf7c | 4129 | copy = WOLFSSL_COMMON_NAME; |
wolfSSL | 15:117db924cf7c | 4130 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4131 | dName->cnIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4132 | dName->cnLen = strLen; |
wolfSSL | 15:117db924cf7c | 4133 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4134 | } |
wolfSSL | 15:117db924cf7c | 4135 | else if (id == ASN_SUR_NAME) { |
wolfSSL | 15:117db924cf7c | 4136 | copy = WOLFSSL_SUR_NAME; |
wolfSSL | 15:117db924cf7c | 4137 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 4138 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4139 | cert->subjectSN = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4140 | cert->subjectSNLen = strLen; |
wolfSSL | 15:117db924cf7c | 4141 | cert->subjectSNEnc = b; |
wolfSSL | 15:117db924cf7c | 4142 | } |
wolfSSL | 15:117db924cf7c | 4143 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 4144 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4145 | dName->snIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4146 | dName->snLen = strLen; |
wolfSSL | 15:117db924cf7c | 4147 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4148 | } |
wolfSSL | 15:117db924cf7c | 4149 | else if (id == ASN_COUNTRY_NAME) { |
wolfSSL | 15:117db924cf7c | 4150 | copy = WOLFSSL_COUNTRY_NAME; |
wolfSSL | 15:117db924cf7c | 4151 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 4152 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4153 | cert->subjectC = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4154 | cert->subjectCLen = strLen; |
wolfSSL | 15:117db924cf7c | 4155 | cert->subjectCEnc = b; |
wolfSSL | 15:117db924cf7c | 4156 | } |
wolfSSL | 15:117db924cf7c | 4157 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 4158 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4159 | dName->cIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4160 | dName->cLen = strLen; |
wolfSSL | 15:117db924cf7c | 4161 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4162 | } |
wolfSSL | 15:117db924cf7c | 4163 | else if (id == ASN_LOCALITY_NAME) { |
wolfSSL | 15:117db924cf7c | 4164 | copy = WOLFSSL_LOCALITY_NAME; |
wolfSSL | 15:117db924cf7c | 4165 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 4166 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4167 | cert->subjectL = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4168 | cert->subjectLLen = strLen; |
wolfSSL | 15:117db924cf7c | 4169 | cert->subjectLEnc = b; |
wolfSSL | 15:117db924cf7c | 4170 | } |
wolfSSL | 15:117db924cf7c | 4171 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 4172 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4173 | dName->lIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4174 | dName->lLen = strLen; |
wolfSSL | 15:117db924cf7c | 4175 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4176 | } |
wolfSSL | 15:117db924cf7c | 4177 | else if (id == ASN_STATE_NAME) { |
wolfSSL | 15:117db924cf7c | 4178 | copy = WOLFSSL_STATE_NAME; |
wolfSSL | 15:117db924cf7c | 4179 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 4180 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4181 | cert->subjectST = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4182 | cert->subjectSTLen = strLen; |
wolfSSL | 15:117db924cf7c | 4183 | cert->subjectSTEnc = b; |
wolfSSL | 15:117db924cf7c | 4184 | } |
wolfSSL | 15:117db924cf7c | 4185 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 4186 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4187 | dName->stIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4188 | dName->stLen = strLen; |
wolfSSL | 15:117db924cf7c | 4189 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4190 | } |
wolfSSL | 15:117db924cf7c | 4191 | else if (id == ASN_ORG_NAME) { |
wolfSSL | 15:117db924cf7c | 4192 | copy = WOLFSSL_ORG_NAME; |
wolfSSL | 15:117db924cf7c | 4193 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 4194 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4195 | cert->subjectO = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4196 | cert->subjectOLen = strLen; |
wolfSSL | 15:117db924cf7c | 4197 | cert->subjectOEnc = b; |
wolfSSL | 15:117db924cf7c | 4198 | } |
wolfSSL | 15:117db924cf7c | 4199 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 4200 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4201 | dName->oIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4202 | dName->oLen = strLen; |
wolfSSL | 15:117db924cf7c | 4203 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4204 | } |
wolfSSL | 15:117db924cf7c | 4205 | else if (id == ASN_ORGUNIT_NAME) { |
wolfSSL | 15:117db924cf7c | 4206 | copy = WOLFSSL_ORGUNIT_NAME; |
wolfSSL | 15:117db924cf7c | 4207 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 4208 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4209 | cert->subjectOU = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4210 | cert->subjectOULen = strLen; |
wolfSSL | 15:117db924cf7c | 4211 | cert->subjectOUEnc = b; |
wolfSSL | 15:117db924cf7c | 4212 | } |
wolfSSL | 15:117db924cf7c | 4213 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 4214 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4215 | dName->ouIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4216 | dName->ouLen = strLen; |
wolfSSL | 15:117db924cf7c | 4217 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4218 | } |
wolfSSL | 15:117db924cf7c | 4219 | else if (id == ASN_SERIAL_NUMBER) { |
wolfSSL | 15:117db924cf7c | 4220 | copy = WOLFSSL_SERIAL_NUMBER; |
wolfSSL | 15:117db924cf7c | 4221 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4222 | dName->snIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4223 | dName->snLen = strLen; |
wolfSSL | 15:117db924cf7c | 4224 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4225 | } |
wolfSSL | 15:117db924cf7c | 4226 | if (copy && !tooBig) { |
wolfSSL | 15:117db924cf7c | 4227 | XMEMCPY(&full[idx], copy, XSTRLEN(copy)); |
wolfSSL | 15:117db924cf7c | 4228 | idx += (word32)XSTRLEN(copy); |
wolfSSL | 15:117db924cf7c | 4229 | #ifdef WOLFSSL_WPAS |
wolfSSL | 15:117db924cf7c | 4230 | full[idx] = '='; |
wolfSSL | 15:117db924cf7c | 4231 | idx++; |
wolfSSL | 15:117db924cf7c | 4232 | #endif |
wolfSSL | 15:117db924cf7c | 4233 | XMEMCPY(&full[idx], &cert->source[cert->srcIdx], strLen); |
wolfSSL | 15:117db924cf7c | 4234 | idx += strLen; |
wolfSSL | 15:117db924cf7c | 4235 | } |
wolfSSL | 15:117db924cf7c | 4236 | |
wolfSSL | 15:117db924cf7c | 4237 | cert->srcIdx += strLen; |
wolfSSL | 15:117db924cf7c | 4238 | } |
wolfSSL | 15:117db924cf7c | 4239 | else { |
wolfSSL | 15:117db924cf7c | 4240 | /* skip */ |
wolfSSL | 15:117db924cf7c | 4241 | byte email = FALSE; |
wolfSSL | 15:117db924cf7c | 4242 | byte pilot = FALSE; |
wolfSSL | 15:117db924cf7c | 4243 | byte id = 0; |
wolfSSL | 15:117db924cf7c | 4244 | int adv; |
wolfSSL | 15:117db924cf7c | 4245 | |
wolfSSL | 15:117db924cf7c | 4246 | if (joint[0] == 0x2a && joint[1] == 0x86) /* email id hdr */ |
wolfSSL | 15:117db924cf7c | 4247 | email = TRUE; |
wolfSSL | 15:117db924cf7c | 4248 | |
wolfSSL | 15:117db924cf7c | 4249 | if (joint[0] == 0x9 && joint[1] == 0x92) { /* uid id hdr */ |
wolfSSL | 15:117db924cf7c | 4250 | /* last value of OID is the type of pilot attribute */ |
wolfSSL | 15:117db924cf7c | 4251 | id = cert->source[cert->srcIdx + oidSz - 1]; |
wolfSSL | 15:117db924cf7c | 4252 | pilot = TRUE; |
wolfSSL | 15:117db924cf7c | 4253 | } |
wolfSSL | 15:117db924cf7c | 4254 | |
wolfSSL | 15:117db924cf7c | 4255 | cert->srcIdx += oidSz + 1; |
wolfSSL | 15:117db924cf7c | 4256 | |
wolfSSL | 15:117db924cf7c | 4257 | if (GetLength(cert->source, &cert->srcIdx, &adv, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 4258 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4259 | |
wolfSSL | 15:117db924cf7c | 4260 | if (adv > (int)(ASN_NAME_MAX - idx)) { |
wolfSSL | 15:117db924cf7c | 4261 | WOLFSSL_MSG("ASN name too big, skipping"); |
wolfSSL | 15:117db924cf7c | 4262 | tooBig = TRUE; |
wolfSSL | 15:117db924cf7c | 4263 | } |
wolfSSL | 15:117db924cf7c | 4264 | |
wolfSSL | 15:117db924cf7c | 4265 | if (email) { |
wolfSSL | 15:117db924cf7c | 4266 | if ( (14 + adv) > (int)(ASN_NAME_MAX - idx)) { |
wolfSSL | 15:117db924cf7c | 4267 | WOLFSSL_MSG("ASN name too big, skipping"); |
wolfSSL | 15:117db924cf7c | 4268 | tooBig = TRUE; |
wolfSSL | 15:117db924cf7c | 4269 | } |
wolfSSL | 15:117db924cf7c | 4270 | if (!tooBig) { |
wolfSSL | 15:117db924cf7c | 4271 | XMEMCPY(&full[idx], "/emailAddress=", 14); |
wolfSSL | 15:117db924cf7c | 4272 | idx += 14; |
wolfSSL | 15:117db924cf7c | 4273 | } |
wolfSSL | 15:117db924cf7c | 4274 | |
wolfSSL | 15:117db924cf7c | 4275 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 4276 | if (nameType == SUBJECT) { |
wolfSSL | 15:117db924cf7c | 4277 | cert->subjectEmail = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4278 | cert->subjectEmailLen = adv; |
wolfSSL | 15:117db924cf7c | 4279 | } |
wolfSSL | 15:117db924cf7c | 4280 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 4281 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4282 | dName->emailIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4283 | dName->emailLen = adv; |
wolfSSL | 15:117db924cf7c | 4284 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4285 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 4286 | { |
wolfSSL | 15:117db924cf7c | 4287 | DNS_entry* emailName = NULL; |
wolfSSL | 15:117db924cf7c | 4288 | |
wolfSSL | 15:117db924cf7c | 4289 | emailName = (DNS_entry*)XMALLOC(sizeof(DNS_entry), |
wolfSSL | 15:117db924cf7c | 4290 | cert->heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 4291 | if (emailName == NULL) { |
wolfSSL | 15:117db924cf7c | 4292 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 4293 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 4294 | } |
wolfSSL | 15:117db924cf7c | 4295 | emailName->type = 0; |
wolfSSL | 15:117db924cf7c | 4296 | emailName->name = (char*)XMALLOC(adv + 1, |
wolfSSL | 15:117db924cf7c | 4297 | cert->heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 4298 | if (emailName->name == NULL) { |
wolfSSL | 15:117db924cf7c | 4299 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 4300 | XFREE(emailName, cert->heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 4301 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 4302 | } |
wolfSSL | 15:117db924cf7c | 4303 | emailName->len = adv; |
wolfSSL | 15:117db924cf7c | 4304 | XMEMCPY(emailName->name, |
wolfSSL | 15:117db924cf7c | 4305 | &cert->source[cert->srcIdx], adv); |
wolfSSL | 15:117db924cf7c | 4306 | emailName->name[adv] = '\0'; |
wolfSSL | 15:117db924cf7c | 4307 | |
wolfSSL | 15:117db924cf7c | 4308 | emailName->next = cert->altEmailNames; |
wolfSSL | 15:117db924cf7c | 4309 | cert->altEmailNames = emailName; |
wolfSSL | 15:117db924cf7c | 4310 | } |
wolfSSL | 15:117db924cf7c | 4311 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 4312 | if (!tooBig) { |
wolfSSL | 15:117db924cf7c | 4313 | XMEMCPY(&full[idx], &cert->source[cert->srcIdx], adv); |
wolfSSL | 15:117db924cf7c | 4314 | idx += adv; |
wolfSSL | 15:117db924cf7c | 4315 | } |
wolfSSL | 15:117db924cf7c | 4316 | } |
wolfSSL | 15:117db924cf7c | 4317 | |
wolfSSL | 15:117db924cf7c | 4318 | if (pilot) { |
wolfSSL | 15:117db924cf7c | 4319 | if ( (5 + adv) > (int)(ASN_NAME_MAX - idx)) { |
wolfSSL | 15:117db924cf7c | 4320 | WOLFSSL_MSG("ASN name too big, skipping"); |
wolfSSL | 15:117db924cf7c | 4321 | tooBig = TRUE; |
wolfSSL | 15:117db924cf7c | 4322 | } |
wolfSSL | 15:117db924cf7c | 4323 | if (!tooBig) { |
wolfSSL | 15:117db924cf7c | 4324 | switch (id) { |
wolfSSL | 15:117db924cf7c | 4325 | case ASN_USER_ID: |
wolfSSL | 15:117db924cf7c | 4326 | XMEMCPY(&full[idx], "/UID=", 5); |
wolfSSL | 15:117db924cf7c | 4327 | idx += 5; |
wolfSSL | 15:117db924cf7c | 4328 | #if defined(OPENSSL_EXTRA) || \ |
wolfSSL | 15:117db924cf7c | 4329 | defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4330 | dName->uidIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4331 | dName->uidLen = adv; |
wolfSSL | 15:117db924cf7c | 4332 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4333 | break; |
wolfSSL | 15:117db924cf7c | 4334 | |
wolfSSL | 15:117db924cf7c | 4335 | case ASN_DOMAIN_COMPONENT: |
wolfSSL | 15:117db924cf7c | 4336 | XMEMCPY(&full[idx], "/DC=", 4); |
wolfSSL | 15:117db924cf7c | 4337 | idx += 4; |
wolfSSL | 15:117db924cf7c | 4338 | #if defined(OPENSSL_EXTRA) || \ |
wolfSSL | 15:117db924cf7c | 4339 | defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4340 | dName->dcIdx[dcnum] = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4341 | dName->dcLen[dcnum] = adv; |
wolfSSL | 15:117db924cf7c | 4342 | dName->dcNum = dcnum + 1; |
wolfSSL | 15:117db924cf7c | 4343 | dcnum++; |
wolfSSL | 15:117db924cf7c | 4344 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4345 | break; |
wolfSSL | 15:117db924cf7c | 4346 | |
wolfSSL | 15:117db924cf7c | 4347 | default: |
wolfSSL | 15:117db924cf7c | 4348 | WOLFSSL_MSG("Unknown pilot attribute type"); |
wolfSSL | 15:117db924cf7c | 4349 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4350 | } |
wolfSSL | 15:117db924cf7c | 4351 | XMEMCPY(&full[idx], &cert->source[cert->srcIdx], adv); |
wolfSSL | 15:117db924cf7c | 4352 | idx += adv; |
wolfSSL | 15:117db924cf7c | 4353 | } |
wolfSSL | 15:117db924cf7c | 4354 | } |
wolfSSL | 15:117db924cf7c | 4355 | |
wolfSSL | 15:117db924cf7c | 4356 | cert->srcIdx += adv; |
wolfSSL | 15:117db924cf7c | 4357 | } |
wolfSSL | 15:117db924cf7c | 4358 | } |
wolfSSL | 15:117db924cf7c | 4359 | full[idx++] = 0; |
wolfSSL | 15:117db924cf7c | 4360 | |
wolfSSL | 15:117db924cf7c | 4361 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 4362 | { |
wolfSSL | 15:117db924cf7c | 4363 | int totalLen = 0; |
wolfSSL | 15:117db924cf7c | 4364 | int i = 0; |
wolfSSL | 15:117db924cf7c | 4365 | |
wolfSSL | 15:117db924cf7c | 4366 | if (dName->cnLen != 0) |
wolfSSL | 15:117db924cf7c | 4367 | totalLen += dName->cnLen + 4; |
wolfSSL | 15:117db924cf7c | 4368 | if (dName->snLen != 0) |
wolfSSL | 15:117db924cf7c | 4369 | totalLen += dName->snLen + 4; |
wolfSSL | 15:117db924cf7c | 4370 | if (dName->cLen != 0) |
wolfSSL | 15:117db924cf7c | 4371 | totalLen += dName->cLen + 3; |
wolfSSL | 15:117db924cf7c | 4372 | if (dName->lLen != 0) |
wolfSSL | 15:117db924cf7c | 4373 | totalLen += dName->lLen + 3; |
wolfSSL | 15:117db924cf7c | 4374 | if (dName->stLen != 0) |
wolfSSL | 15:117db924cf7c | 4375 | totalLen += dName->stLen + 4; |
wolfSSL | 15:117db924cf7c | 4376 | if (dName->oLen != 0) |
wolfSSL | 15:117db924cf7c | 4377 | totalLen += dName->oLen + 3; |
wolfSSL | 15:117db924cf7c | 4378 | if (dName->ouLen != 0) |
wolfSSL | 15:117db924cf7c | 4379 | totalLen += dName->ouLen + 4; |
wolfSSL | 15:117db924cf7c | 4380 | if (dName->emailLen != 0) |
wolfSSL | 15:117db924cf7c | 4381 | totalLen += dName->emailLen + 14; |
wolfSSL | 15:117db924cf7c | 4382 | if (dName->uidLen != 0) |
wolfSSL | 15:117db924cf7c | 4383 | totalLen += dName->uidLen + 5; |
wolfSSL | 15:117db924cf7c | 4384 | if (dName->serialLen != 0) |
wolfSSL | 15:117db924cf7c | 4385 | totalLen += dName->serialLen + 14; |
wolfSSL | 15:117db924cf7c | 4386 | if (dName->dcNum != 0){ |
wolfSSL | 15:117db924cf7c | 4387 | for (i = 0;i < dName->dcNum;i++) |
wolfSSL | 15:117db924cf7c | 4388 | totalLen += dName->dcLen[i] + 4; |
wolfSSL | 15:117db924cf7c | 4389 | } |
wolfSSL | 15:117db924cf7c | 4390 | |
wolfSSL | 15:117db924cf7c | 4391 | dName->fullName = (char*)XMALLOC(totalLen + 1, cert->heap, |
wolfSSL | 15:117db924cf7c | 4392 | DYNAMIC_TYPE_X509); |
wolfSSL | 15:117db924cf7c | 4393 | if (dName->fullName != NULL) { |
wolfSSL | 15:117db924cf7c | 4394 | idx = 0; |
wolfSSL | 15:117db924cf7c | 4395 | |
wolfSSL | 15:117db924cf7c | 4396 | if (dName->cnLen != 0) { |
wolfSSL | 15:117db924cf7c | 4397 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4398 | XMEMCPY(&dName->fullName[idx], WOLFSSL_COMMON_NAME, 4); |
wolfSSL | 15:117db924cf7c | 4399 | idx += 4; |
wolfSSL | 15:117db924cf7c | 4400 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4401 | &cert->source[dName->cnIdx], dName->cnLen); |
wolfSSL | 15:117db924cf7c | 4402 | dName->cnIdx = idx; |
wolfSSL | 15:117db924cf7c | 4403 | idx += dName->cnLen; |
wolfSSL | 15:117db924cf7c | 4404 | } |
wolfSSL | 15:117db924cf7c | 4405 | if (dName->snLen != 0) { |
wolfSSL | 15:117db924cf7c | 4406 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4407 | XMEMCPY(&dName->fullName[idx], WOLFSSL_SUR_NAME, 4); |
wolfSSL | 15:117db924cf7c | 4408 | idx += 4; |
wolfSSL | 15:117db924cf7c | 4409 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4410 | &cert->source[dName->snIdx], dName->snLen); |
wolfSSL | 15:117db924cf7c | 4411 | dName->snIdx = idx; |
wolfSSL | 15:117db924cf7c | 4412 | idx += dName->snLen; |
wolfSSL | 15:117db924cf7c | 4413 | } |
wolfSSL | 15:117db924cf7c | 4414 | if (dName->cLen != 0) { |
wolfSSL | 15:117db924cf7c | 4415 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4416 | XMEMCPY(&dName->fullName[idx], WOLFSSL_COUNTRY_NAME, 3); |
wolfSSL | 15:117db924cf7c | 4417 | idx += 3; |
wolfSSL | 15:117db924cf7c | 4418 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4419 | &cert->source[dName->cIdx], dName->cLen); |
wolfSSL | 15:117db924cf7c | 4420 | dName->cIdx = idx; |
wolfSSL | 15:117db924cf7c | 4421 | idx += dName->cLen; |
wolfSSL | 15:117db924cf7c | 4422 | } |
wolfSSL | 15:117db924cf7c | 4423 | if (dName->lLen != 0) { |
wolfSSL | 15:117db924cf7c | 4424 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4425 | XMEMCPY(&dName->fullName[idx], WOLFSSL_LOCALITY_NAME, 3); |
wolfSSL | 15:117db924cf7c | 4426 | idx += 3; |
wolfSSL | 15:117db924cf7c | 4427 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4428 | &cert->source[dName->lIdx], dName->lLen); |
wolfSSL | 15:117db924cf7c | 4429 | dName->lIdx = idx; |
wolfSSL | 15:117db924cf7c | 4430 | idx += dName->lLen; |
wolfSSL | 15:117db924cf7c | 4431 | } |
wolfSSL | 15:117db924cf7c | 4432 | if (dName->stLen != 0) { |
wolfSSL | 15:117db924cf7c | 4433 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4434 | XMEMCPY(&dName->fullName[idx], WOLFSSL_STATE_NAME, 4); |
wolfSSL | 15:117db924cf7c | 4435 | idx += 4; |
wolfSSL | 15:117db924cf7c | 4436 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4437 | &cert->source[dName->stIdx], dName->stLen); |
wolfSSL | 15:117db924cf7c | 4438 | dName->stIdx = idx; |
wolfSSL | 15:117db924cf7c | 4439 | idx += dName->stLen; |
wolfSSL | 15:117db924cf7c | 4440 | } |
wolfSSL | 15:117db924cf7c | 4441 | if (dName->oLen != 0) { |
wolfSSL | 15:117db924cf7c | 4442 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4443 | XMEMCPY(&dName->fullName[idx], WOLFSSL_ORG_NAME, 3); |
wolfSSL | 15:117db924cf7c | 4444 | idx += 3; |
wolfSSL | 15:117db924cf7c | 4445 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4446 | &cert->source[dName->oIdx], dName->oLen); |
wolfSSL | 15:117db924cf7c | 4447 | dName->oIdx = idx; |
wolfSSL | 15:117db924cf7c | 4448 | idx += dName->oLen; |
wolfSSL | 15:117db924cf7c | 4449 | } |
wolfSSL | 15:117db924cf7c | 4450 | if (dName->ouLen != 0) { |
wolfSSL | 15:117db924cf7c | 4451 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4452 | XMEMCPY(&dName->fullName[idx], WOLFSSL_ORGUNIT_NAME, 4); |
wolfSSL | 15:117db924cf7c | 4453 | idx += 4; |
wolfSSL | 15:117db924cf7c | 4454 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4455 | &cert->source[dName->ouIdx], dName->ouLen); |
wolfSSL | 15:117db924cf7c | 4456 | dName->ouIdx = idx; |
wolfSSL | 15:117db924cf7c | 4457 | idx += dName->ouLen; |
wolfSSL | 15:117db924cf7c | 4458 | } |
wolfSSL | 15:117db924cf7c | 4459 | if (dName->emailLen != 0) { |
wolfSSL | 15:117db924cf7c | 4460 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4461 | XMEMCPY(&dName->fullName[idx], "/emailAddress=", 14); |
wolfSSL | 15:117db924cf7c | 4462 | idx += 14; |
wolfSSL | 15:117db924cf7c | 4463 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4464 | &cert->source[dName->emailIdx], dName->emailLen); |
wolfSSL | 15:117db924cf7c | 4465 | dName->emailIdx = idx; |
wolfSSL | 15:117db924cf7c | 4466 | idx += dName->emailLen; |
wolfSSL | 15:117db924cf7c | 4467 | } |
wolfSSL | 15:117db924cf7c | 4468 | for (i = 0;i < dName->dcNum;i++){ |
wolfSSL | 15:117db924cf7c | 4469 | if (dName->dcLen[i] != 0) { |
wolfSSL | 15:117db924cf7c | 4470 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4471 | XMEMCPY(&dName->fullName[idx], WOLFSSL_DOMAIN_COMPONENT, 4); |
wolfSSL | 15:117db924cf7c | 4472 | idx += 4; |
wolfSSL | 15:117db924cf7c | 4473 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4474 | &cert->source[dName->dcIdx[i]], dName->dcLen[i]); |
wolfSSL | 15:117db924cf7c | 4475 | dName->dcIdx[i] = idx; |
wolfSSL | 15:117db924cf7c | 4476 | idx += dName->dcLen[i]; |
wolfSSL | 15:117db924cf7c | 4477 | } |
wolfSSL | 15:117db924cf7c | 4478 | } |
wolfSSL | 15:117db924cf7c | 4479 | if (dName->uidLen != 0) { |
wolfSSL | 15:117db924cf7c | 4480 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4481 | XMEMCPY(&dName->fullName[idx], "/UID=", 5); |
wolfSSL | 15:117db924cf7c | 4482 | idx += 5; |
wolfSSL | 15:117db924cf7c | 4483 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4484 | &cert->source[dName->uidIdx], dName->uidLen); |
wolfSSL | 15:117db924cf7c | 4485 | dName->uidIdx = idx; |
wolfSSL | 15:117db924cf7c | 4486 | idx += dName->uidLen; |
wolfSSL | 15:117db924cf7c | 4487 | } |
wolfSSL | 15:117db924cf7c | 4488 | if (dName->serialLen != 0) { |
wolfSSL | 15:117db924cf7c | 4489 | dName->entryCount++; |
wolfSSL | 15:117db924cf7c | 4490 | XMEMCPY(&dName->fullName[idx], WOLFSSL_SERIAL_NUMBER, 14); |
wolfSSL | 15:117db924cf7c | 4491 | idx += 14; |
wolfSSL | 15:117db924cf7c | 4492 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 15:117db924cf7c | 4493 | &cert->source[dName->serialIdx], dName->serialLen); |
wolfSSL | 15:117db924cf7c | 4494 | dName->serialIdx = idx; |
wolfSSL | 15:117db924cf7c | 4495 | idx += dName->serialLen; |
wolfSSL | 15:117db924cf7c | 4496 | } |
wolfSSL | 15:117db924cf7c | 4497 | dName->fullName[idx] = '\0'; |
wolfSSL | 15:117db924cf7c | 4498 | dName->fullNameLen = totalLen; |
wolfSSL | 15:117db924cf7c | 4499 | } |
wolfSSL | 15:117db924cf7c | 4500 | } |
wolfSSL | 15:117db924cf7c | 4501 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 4502 | |
wolfSSL | 15:117db924cf7c | 4503 | return 0; |
wolfSSL | 15:117db924cf7c | 4504 | } |
wolfSSL | 15:117db924cf7c | 4505 | |
wolfSSL | 15:117db924cf7c | 4506 | |
wolfSSL | 15:117db924cf7c | 4507 | #ifndef NO_ASN_TIME |
wolfSSL | 15:117db924cf7c | 4508 | |
wolfSSL | 15:117db924cf7c | 4509 | /* two byte date/time, add to value */ |
wolfSSL | 15:117db924cf7c | 4510 | static WC_INLINE void GetTime(int* value, const byte* date, int* idx) |
wolfSSL | 15:117db924cf7c | 4511 | { |
wolfSSL | 15:117db924cf7c | 4512 | int i = *idx; |
wolfSSL | 15:117db924cf7c | 4513 | |
wolfSSL | 15:117db924cf7c | 4514 | *value += btoi(date[i++]) * 10; |
wolfSSL | 15:117db924cf7c | 4515 | *value += btoi(date[i++]); |
wolfSSL | 15:117db924cf7c | 4516 | |
wolfSSL | 15:117db924cf7c | 4517 | *idx = i; |
wolfSSL | 15:117db924cf7c | 4518 | } |
wolfSSL | 15:117db924cf7c | 4519 | |
wolfSSL | 15:117db924cf7c | 4520 | int ExtractDate(const unsigned char* date, unsigned char format, |
wolfSSL | 15:117db924cf7c | 4521 | struct tm* certTime, int* idx) |
wolfSSL | 15:117db924cf7c | 4522 | { |
wolfSSL | 15:117db924cf7c | 4523 | XMEMSET(certTime, 0, sizeof(struct tm)); |
wolfSSL | 15:117db924cf7c | 4524 | |
wolfSSL | 15:117db924cf7c | 4525 | if (format == ASN_UTC_TIME) { |
wolfSSL | 15:117db924cf7c | 4526 | if (btoi(date[0]) >= 5) |
wolfSSL | 15:117db924cf7c | 4527 | certTime->tm_year = 1900; |
wolfSSL | 15:117db924cf7c | 4528 | else |
wolfSSL | 15:117db924cf7c | 4529 | certTime->tm_year = 2000; |
wolfSSL | 15:117db924cf7c | 4530 | } |
wolfSSL | 15:117db924cf7c | 4531 | else { /* format == GENERALIZED_TIME */ |
wolfSSL | 15:117db924cf7c | 4532 | certTime->tm_year += btoi(date[*idx]) * 1000; *idx = *idx + 1; |
wolfSSL | 15:117db924cf7c | 4533 | certTime->tm_year += btoi(date[*idx]) * 100; *idx = *idx + 1; |
wolfSSL | 15:117db924cf7c | 4534 | } |
wolfSSL | 15:117db924cf7c | 4535 | |
wolfSSL | 15:117db924cf7c | 4536 | /* adjust tm_year, tm_mon */ |
wolfSSL | 15:117db924cf7c | 4537 | GetTime((int*)&certTime->tm_year, date, idx); certTime->tm_year -= 1900; |
wolfSSL | 15:117db924cf7c | 4538 | GetTime((int*)&certTime->tm_mon, date, idx); certTime->tm_mon -= 1; |
wolfSSL | 15:117db924cf7c | 4539 | GetTime((int*)&certTime->tm_mday, date, idx); |
wolfSSL | 15:117db924cf7c | 4540 | GetTime((int*)&certTime->tm_hour, date, idx); |
wolfSSL | 15:117db924cf7c | 4541 | GetTime((int*)&certTime->tm_min, date, idx); |
wolfSSL | 15:117db924cf7c | 4542 | GetTime((int*)&certTime->tm_sec, date, idx); |
wolfSSL | 15:117db924cf7c | 4543 | |
wolfSSL | 15:117db924cf7c | 4544 | return 1; |
wolfSSL | 15:117db924cf7c | 4545 | } |
wolfSSL | 15:117db924cf7c | 4546 | |
wolfSSL | 15:117db924cf7c | 4547 | |
wolfSSL | 15:117db924cf7c | 4548 | #if defined(OPENSSL_ALL) || defined(WOLFSSL_MYSQL_COMPATIBLE) || \ |
wolfSSL | 15:117db924cf7c | 4549 | defined(OPENSSL_EXTRA) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) |
wolfSSL | 15:117db924cf7c | 4550 | int GetTimeString(byte* date, int format, char* buf, int len) |
wolfSSL | 15:117db924cf7c | 4551 | { |
wolfSSL | 15:117db924cf7c | 4552 | struct tm t; |
wolfSSL | 15:117db924cf7c | 4553 | int idx = 0; |
wolfSSL | 15:117db924cf7c | 4554 | |
wolfSSL | 15:117db924cf7c | 4555 | if (!ExtractDate(date, (unsigned char)format, &t, &idx)) { |
wolfSSL | 15:117db924cf7c | 4556 | return 0; |
wolfSSL | 15:117db924cf7c | 4557 | } |
wolfSSL | 15:117db924cf7c | 4558 | |
wolfSSL | 15:117db924cf7c | 4559 | if (date[idx] != 'Z') { |
wolfSSL | 15:117db924cf7c | 4560 | WOLFSSL_MSG("UTCtime, not Zulu") ; |
wolfSSL | 15:117db924cf7c | 4561 | return 0; |
wolfSSL | 15:117db924cf7c | 4562 | } |
wolfSSL | 15:117db924cf7c | 4563 | |
wolfSSL | 15:117db924cf7c | 4564 | /* place month in buffer */ |
wolfSSL | 15:117db924cf7c | 4565 | buf[0] = '\0'; |
wolfSSL | 15:117db924cf7c | 4566 | switch(t.tm_mon) { |
wolfSSL | 15:117db924cf7c | 4567 | case 0: XSTRNCAT(buf, "Jan ", 4); break; |
wolfSSL | 15:117db924cf7c | 4568 | case 1: XSTRNCAT(buf, "Feb ", 4); break; |
wolfSSL | 15:117db924cf7c | 4569 | case 2: XSTRNCAT(buf, "Mar ", 4); break; |
wolfSSL | 15:117db924cf7c | 4570 | case 3: XSTRNCAT(buf, "Apr ", 4); break; |
wolfSSL | 15:117db924cf7c | 4571 | case 4: XSTRNCAT(buf, "May ", 4); break; |
wolfSSL | 15:117db924cf7c | 4572 | case 5: XSTRNCAT(buf, "Jun ", 4); break; |
wolfSSL | 15:117db924cf7c | 4573 | case 6: XSTRNCAT(buf, "Jul ", 4); break; |
wolfSSL | 15:117db924cf7c | 4574 | case 7: XSTRNCAT(buf, "Aug ", 4); break; |
wolfSSL | 15:117db924cf7c | 4575 | case 8: XSTRNCAT(buf, "Sep ", 4); break; |
wolfSSL | 15:117db924cf7c | 4576 | case 9: XSTRNCAT(buf, "Oct ", 4); break; |
wolfSSL | 15:117db924cf7c | 4577 | case 10: XSTRNCAT(buf, "Nov ", 4); break; |
wolfSSL | 15:117db924cf7c | 4578 | case 11: XSTRNCAT(buf, "Dec ", 4); break; |
wolfSSL | 15:117db924cf7c | 4579 | default: |
wolfSSL | 15:117db924cf7c | 4580 | return 0; |
wolfSSL | 15:117db924cf7c | 4581 | |
wolfSSL | 15:117db924cf7c | 4582 | } |
wolfSSL | 15:117db924cf7c | 4583 | idx = 4; /* use idx now for char buffer */ |
wolfSSL | 15:117db924cf7c | 4584 | buf[idx] = ' '; |
wolfSSL | 15:117db924cf7c | 4585 | |
wolfSSL | 15:117db924cf7c | 4586 | XSNPRINTF(buf + idx, len - idx, "%2d %02d:%02d:%02d %d GMT", |
wolfSSL | 15:117db924cf7c | 4587 | t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, t.tm_year + 1900); |
wolfSSL | 15:117db924cf7c | 4588 | |
wolfSSL | 15:117db924cf7c | 4589 | return 1; |
wolfSSL | 15:117db924cf7c | 4590 | } |
wolfSSL | 15:117db924cf7c | 4591 | #endif /* OPENSSL_ALL || WOLFSSL_MYSQL_COMPATIBLE || WOLFSSL_NGINX || WOLFSSL_HAPROXY */ |
wolfSSL | 15:117db924cf7c | 4592 | |
wolfSSL | 15:117db924cf7c | 4593 | |
wolfSSL | 15:117db924cf7c | 4594 | #if defined(USE_WOLF_VALIDDATE) |
wolfSSL | 15:117db924cf7c | 4595 | |
wolfSSL | 15:117db924cf7c | 4596 | /* to the second */ |
wolfSSL | 15:117db924cf7c | 4597 | static int DateGreaterThan(const struct tm* a, const struct tm* b) |
wolfSSL | 15:117db924cf7c | 4598 | { |
wolfSSL | 15:117db924cf7c | 4599 | if (a->tm_year > b->tm_year) |
wolfSSL | 15:117db924cf7c | 4600 | return 1; |
wolfSSL | 15:117db924cf7c | 4601 | |
wolfSSL | 15:117db924cf7c | 4602 | if (a->tm_year == b->tm_year && a->tm_mon > b->tm_mon) |
wolfSSL | 15:117db924cf7c | 4603 | return 1; |
wolfSSL | 15:117db924cf7c | 4604 | |
wolfSSL | 15:117db924cf7c | 4605 | if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && |
wolfSSL | 15:117db924cf7c | 4606 | a->tm_mday > b->tm_mday) |
wolfSSL | 15:117db924cf7c | 4607 | return 1; |
wolfSSL | 15:117db924cf7c | 4608 | |
wolfSSL | 15:117db924cf7c | 4609 | if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && |
wolfSSL | 15:117db924cf7c | 4610 | a->tm_mday == b->tm_mday && a->tm_hour > b->tm_hour) |
wolfSSL | 15:117db924cf7c | 4611 | return 1; |
wolfSSL | 15:117db924cf7c | 4612 | |
wolfSSL | 15:117db924cf7c | 4613 | if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && |
wolfSSL | 15:117db924cf7c | 4614 | a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour && |
wolfSSL | 15:117db924cf7c | 4615 | a->tm_min > b->tm_min) |
wolfSSL | 15:117db924cf7c | 4616 | return 1; |
wolfSSL | 15:117db924cf7c | 4617 | |
wolfSSL | 15:117db924cf7c | 4618 | if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && |
wolfSSL | 15:117db924cf7c | 4619 | a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour && |
wolfSSL | 15:117db924cf7c | 4620 | a->tm_min == b->tm_min && a->tm_sec > b->tm_sec) |
wolfSSL | 15:117db924cf7c | 4621 | return 1; |
wolfSSL | 15:117db924cf7c | 4622 | |
wolfSSL | 15:117db924cf7c | 4623 | return 0; /* false */ |
wolfSSL | 15:117db924cf7c | 4624 | } |
wolfSSL | 15:117db924cf7c | 4625 | |
wolfSSL | 15:117db924cf7c | 4626 | |
wolfSSL | 15:117db924cf7c | 4627 | static WC_INLINE int DateLessThan(const struct tm* a, const struct tm* b) |
wolfSSL | 15:117db924cf7c | 4628 | { |
wolfSSL | 15:117db924cf7c | 4629 | return DateGreaterThan(b,a); |
wolfSSL | 15:117db924cf7c | 4630 | } |
wolfSSL | 15:117db924cf7c | 4631 | |
wolfSSL | 15:117db924cf7c | 4632 | /* like atoi but only use first byte */ |
wolfSSL | 15:117db924cf7c | 4633 | /* Make sure before and after dates are valid */ |
wolfSSL | 15:117db924cf7c | 4634 | int ValidateDate(const byte* date, byte format, int dateType) |
wolfSSL | 15:117db924cf7c | 4635 | { |
wolfSSL | 15:117db924cf7c | 4636 | time_t ltime; |
wolfSSL | 15:117db924cf7c | 4637 | struct tm certTime; |
wolfSSL | 15:117db924cf7c | 4638 | struct tm* localTime; |
wolfSSL | 15:117db924cf7c | 4639 | struct tm* tmpTime = NULL; |
wolfSSL | 15:117db924cf7c | 4640 | int i = 0; |
wolfSSL | 15:117db924cf7c | 4641 | int timeDiff = 0 ; |
wolfSSL | 15:117db924cf7c | 4642 | int diffHH = 0 ; int diffMM = 0 ; |
wolfSSL | 15:117db924cf7c | 4643 | int diffSign = 0 ; |
wolfSSL | 15:117db924cf7c | 4644 | |
wolfSSL | 15:117db924cf7c | 4645 | #if defined(NEED_TMP_TIME) |
wolfSSL | 15:117db924cf7c | 4646 | struct tm tmpTimeStorage; |
wolfSSL | 15:117db924cf7c | 4647 | tmpTime = &tmpTimeStorage; |
wolfSSL | 15:117db924cf7c | 4648 | #else |
wolfSSL | 15:117db924cf7c | 4649 | (void)tmpTime; |
wolfSSL | 15:117db924cf7c | 4650 | #endif |
wolfSSL | 15:117db924cf7c | 4651 | |
wolfSSL | 15:117db924cf7c | 4652 | ltime = XTIME(0); |
wolfSSL | 15:117db924cf7c | 4653 | |
wolfSSL | 15:117db924cf7c | 4654 | #ifdef WOLFSSL_BEFORE_DATE_CLOCK_SKEW |
wolfSSL | 15:117db924cf7c | 4655 | if (dateType == BEFORE) { |
wolfSSL | 15:117db924cf7c | 4656 | WOLFSSL_MSG("Skewing local time for before date check"); |
wolfSSL | 15:117db924cf7c | 4657 | ltime += WOLFSSL_BEFORE_DATE_CLOCK_SKEW; |
wolfSSL | 15:117db924cf7c | 4658 | } |
wolfSSL | 15:117db924cf7c | 4659 | #endif |
wolfSSL | 15:117db924cf7c | 4660 | |
wolfSSL | 15:117db924cf7c | 4661 | #ifdef WOLFSSL_AFTER_DATE_CLOCK_SKEW |
wolfSSL | 15:117db924cf7c | 4662 | if (dateType == AFTER) { |
wolfSSL | 15:117db924cf7c | 4663 | WOLFSSL_MSG("Skewing local time for after date check"); |
wolfSSL | 15:117db924cf7c | 4664 | ltime -= WOLFSSL_AFTER_DATE_CLOCK_SKEW; |
wolfSSL | 15:117db924cf7c | 4665 | } |
wolfSSL | 15:117db924cf7c | 4666 | #endif |
wolfSSL | 15:117db924cf7c | 4667 | |
wolfSSL | 15:117db924cf7c | 4668 | if (!ExtractDate(date, format, &certTime, &i)) { |
wolfSSL | 15:117db924cf7c | 4669 | WOLFSSL_MSG("Error extracting the date"); |
wolfSSL | 15:117db924cf7c | 4670 | return 0; |
wolfSSL | 15:117db924cf7c | 4671 | } |
wolfSSL | 15:117db924cf7c | 4672 | |
wolfSSL | 15:117db924cf7c | 4673 | if ((date[i] == '+') || (date[i] == '-')) { |
wolfSSL | 15:117db924cf7c | 4674 | WOLFSSL_MSG("Using time differential, not Zulu") ; |
wolfSSL | 15:117db924cf7c | 4675 | diffSign = date[i++] == '+' ? 1 : -1 ; |
wolfSSL | 15:117db924cf7c | 4676 | GetTime(&diffHH, date, &i); |
wolfSSL | 15:117db924cf7c | 4677 | GetTime(&diffMM, date, &i); |
wolfSSL | 15:117db924cf7c | 4678 | timeDiff = diffSign * (diffHH*60 + diffMM) * 60 ; |
wolfSSL | 15:117db924cf7c | 4679 | } else if (date[i] != 'Z') { |
wolfSSL | 15:117db924cf7c | 4680 | WOLFSSL_MSG("UTCtime, niether Zulu or time differential") ; |
wolfSSL | 15:117db924cf7c | 4681 | return 0; |
wolfSSL | 15:117db924cf7c | 4682 | } |
wolfSSL | 15:117db924cf7c | 4683 | |
wolfSSL | 15:117db924cf7c | 4684 | ltime -= (time_t)timeDiff ; |
wolfSSL | 15:117db924cf7c | 4685 | localTime = XGMTIME(<ime, tmpTime); |
wolfSSL | 15:117db924cf7c | 4686 | |
wolfSSL | 15:117db924cf7c | 4687 | if (localTime == NULL) { |
wolfSSL | 15:117db924cf7c | 4688 | WOLFSSL_MSG("XGMTIME failed"); |
wolfSSL | 15:117db924cf7c | 4689 | return 0; |
wolfSSL | 15:117db924cf7c | 4690 | } |
wolfSSL | 15:117db924cf7c | 4691 | |
wolfSSL | 15:117db924cf7c | 4692 | if (dateType == BEFORE) { |
wolfSSL | 15:117db924cf7c | 4693 | if (DateLessThan(localTime, &certTime)) { |
wolfSSL | 15:117db924cf7c | 4694 | WOLFSSL_MSG("Date BEFORE check failed"); |
wolfSSL | 15:117db924cf7c | 4695 | return 0; |
wolfSSL | 15:117db924cf7c | 4696 | } |
wolfSSL | 15:117db924cf7c | 4697 | } |
wolfSSL | 15:117db924cf7c | 4698 | else { /* dateType == AFTER */ |
wolfSSL | 15:117db924cf7c | 4699 | if (DateGreaterThan(localTime, &certTime)) { |
wolfSSL | 15:117db924cf7c | 4700 | WOLFSSL_MSG("Date AFTER check failed"); |
wolfSSL | 15:117db924cf7c | 4701 | return 0; |
wolfSSL | 15:117db924cf7c | 4702 | } |
wolfSSL | 15:117db924cf7c | 4703 | } |
wolfSSL | 15:117db924cf7c | 4704 | |
wolfSSL | 15:117db924cf7c | 4705 | return 1; |
wolfSSL | 15:117db924cf7c | 4706 | } |
wolfSSL | 15:117db924cf7c | 4707 | #endif /* USE_WOLF_VALIDDATE */ |
wolfSSL | 15:117db924cf7c | 4708 | |
wolfSSL | 15:117db924cf7c | 4709 | int wc_GetTime(void* timePtr, word32 timeSize) |
wolfSSL | 15:117db924cf7c | 4710 | { |
wolfSSL | 15:117db924cf7c | 4711 | time_t* ltime = (time_t*)timePtr; |
wolfSSL | 15:117db924cf7c | 4712 | |
wolfSSL | 15:117db924cf7c | 4713 | if (timePtr == NULL) { |
wolfSSL | 15:117db924cf7c | 4714 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 4715 | } |
wolfSSL | 15:117db924cf7c | 4716 | |
wolfSSL | 15:117db924cf7c | 4717 | if ((word32)sizeof(time_t) > timeSize) { |
wolfSSL | 15:117db924cf7c | 4718 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 4719 | } |
wolfSSL | 15:117db924cf7c | 4720 | |
wolfSSL | 15:117db924cf7c | 4721 | *ltime = XTIME(0); |
wolfSSL | 15:117db924cf7c | 4722 | |
wolfSSL | 15:117db924cf7c | 4723 | return 0; |
wolfSSL | 15:117db924cf7c | 4724 | } |
wolfSSL | 15:117db924cf7c | 4725 | |
wolfSSL | 15:117db924cf7c | 4726 | #endif /* !NO_ASN_TIME */ |
wolfSSL | 15:117db924cf7c | 4727 | |
wolfSSL | 15:117db924cf7c | 4728 | |
wolfSSL | 15:117db924cf7c | 4729 | /* Get date buffer, format and length. Returns 0=success or error */ |
wolfSSL | 15:117db924cf7c | 4730 | static int GetDateInfo(const byte* source, word32* idx, const byte** pDate, |
wolfSSL | 15:117db924cf7c | 4731 | byte* pFormat, int* pLength, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 4732 | { |
wolfSSL | 15:117db924cf7c | 4733 | int length; |
wolfSSL | 15:117db924cf7c | 4734 | byte format; |
wolfSSL | 15:117db924cf7c | 4735 | |
wolfSSL | 15:117db924cf7c | 4736 | if (source == NULL || idx == NULL) |
wolfSSL | 15:117db924cf7c | 4737 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 4738 | |
wolfSSL | 15:117db924cf7c | 4739 | /* get ASN format header */ |
wolfSSL | 15:117db924cf7c | 4740 | if (*idx+1 > maxIdx) |
wolfSSL | 15:117db924cf7c | 4741 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 4742 | format = source[*idx]; |
wolfSSL | 15:117db924cf7c | 4743 | *idx += 1; |
wolfSSL | 15:117db924cf7c | 4744 | if (format != ASN_UTC_TIME && format != ASN_GENERALIZED_TIME) |
wolfSSL | 15:117db924cf7c | 4745 | return ASN_TIME_E; |
wolfSSL | 15:117db924cf7c | 4746 | |
wolfSSL | 15:117db924cf7c | 4747 | /* get length */ |
wolfSSL | 15:117db924cf7c | 4748 | if (GetLength(source, idx, &length, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 4749 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4750 | if (length > MAX_DATE_SIZE || length < MIN_DATE_SIZE) |
wolfSSL | 15:117db924cf7c | 4751 | return ASN_DATE_SZ_E; |
wolfSSL | 15:117db924cf7c | 4752 | |
wolfSSL | 15:117db924cf7c | 4753 | /* return format, date and length */ |
wolfSSL | 15:117db924cf7c | 4754 | if (pFormat) |
wolfSSL | 15:117db924cf7c | 4755 | *pFormat = format; |
wolfSSL | 15:117db924cf7c | 4756 | if (pDate) |
wolfSSL | 15:117db924cf7c | 4757 | *pDate = &source[*idx]; |
wolfSSL | 15:117db924cf7c | 4758 | if (pLength) |
wolfSSL | 15:117db924cf7c | 4759 | *pLength = length; |
wolfSSL | 15:117db924cf7c | 4760 | |
wolfSSL | 15:117db924cf7c | 4761 | *idx += length; |
wolfSSL | 15:117db924cf7c | 4762 | |
wolfSSL | 15:117db924cf7c | 4763 | return 0; |
wolfSSL | 15:117db924cf7c | 4764 | } |
wolfSSL | 15:117db924cf7c | 4765 | |
wolfSSL | 15:117db924cf7c | 4766 | static int GetDate(DecodedCert* cert, int dateType, int verify) |
wolfSSL | 15:117db924cf7c | 4767 | { |
wolfSSL | 15:117db924cf7c | 4768 | int ret, length; |
wolfSSL | 15:117db924cf7c | 4769 | const byte *datePtr = NULL; |
wolfSSL | 15:117db924cf7c | 4770 | byte date[MAX_DATE_SIZE]; |
wolfSSL | 15:117db924cf7c | 4771 | byte format; |
wolfSSL | 15:117db924cf7c | 4772 | word32 startIdx = 0; |
wolfSSL | 15:117db924cf7c | 4773 | |
wolfSSL | 15:117db924cf7c | 4774 | if (dateType == BEFORE) |
wolfSSL | 15:117db924cf7c | 4775 | cert->beforeDate = &cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4776 | else |
wolfSSL | 15:117db924cf7c | 4777 | cert->afterDate = &cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4778 | startIdx = cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 4779 | |
wolfSSL | 15:117db924cf7c | 4780 | ret = GetDateInfo(cert->source, &cert->srcIdx, &datePtr, &format, |
wolfSSL | 15:117db924cf7c | 4781 | &length, cert->maxIdx); |
wolfSSL | 15:117db924cf7c | 4782 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 4783 | return ret; |
wolfSSL | 15:117db924cf7c | 4784 | |
wolfSSL | 15:117db924cf7c | 4785 | XMEMSET(date, 0, MAX_DATE_SIZE); |
wolfSSL | 15:117db924cf7c | 4786 | XMEMCPY(date, datePtr, length); |
wolfSSL | 15:117db924cf7c | 4787 | |
wolfSSL | 15:117db924cf7c | 4788 | if (dateType == BEFORE) |
wolfSSL | 15:117db924cf7c | 4789 | cert->beforeDateLen = cert->srcIdx - startIdx; |
wolfSSL | 15:117db924cf7c | 4790 | else |
wolfSSL | 15:117db924cf7c | 4791 | cert->afterDateLen = cert->srcIdx - startIdx; |
wolfSSL | 15:117db924cf7c | 4792 | |
wolfSSL | 15:117db924cf7c | 4793 | #ifndef NO_ASN_TIME |
wolfSSL | 15:117db924cf7c | 4794 | if (verify != NO_VERIFY && !XVALIDATE_DATE(date, format, dateType)) { |
wolfSSL | 15:117db924cf7c | 4795 | if (dateType == BEFORE) |
wolfSSL | 15:117db924cf7c | 4796 | return ASN_BEFORE_DATE_E; |
wolfSSL | 15:117db924cf7c | 4797 | else |
wolfSSL | 15:117db924cf7c | 4798 | return ASN_AFTER_DATE_E; |
wolfSSL | 15:117db924cf7c | 4799 | } |
wolfSSL | 15:117db924cf7c | 4800 | #else |
wolfSSL | 15:117db924cf7c | 4801 | (void)verify; |
wolfSSL | 15:117db924cf7c | 4802 | #endif |
wolfSSL | 15:117db924cf7c | 4803 | |
wolfSSL | 15:117db924cf7c | 4804 | return 0; |
wolfSSL | 15:117db924cf7c | 4805 | } |
wolfSSL | 15:117db924cf7c | 4806 | |
wolfSSL | 15:117db924cf7c | 4807 | static int GetValidity(DecodedCert* cert, int verify) |
wolfSSL | 15:117db924cf7c | 4808 | { |
wolfSSL | 15:117db924cf7c | 4809 | int length; |
wolfSSL | 15:117db924cf7c | 4810 | int badDate = 0; |
wolfSSL | 15:117db924cf7c | 4811 | |
wolfSSL | 15:117db924cf7c | 4812 | if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 4813 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 4814 | |
wolfSSL | 15:117db924cf7c | 4815 | if (GetDate(cert, BEFORE, verify) < 0) |
wolfSSL | 15:117db924cf7c | 4816 | badDate = ASN_BEFORE_DATE_E; /* continue parsing */ |
wolfSSL | 15:117db924cf7c | 4817 | |
wolfSSL | 15:117db924cf7c | 4818 | if (GetDate(cert, AFTER, verify) < 0) |
wolfSSL | 15:117db924cf7c | 4819 | return ASN_AFTER_DATE_E; |
wolfSSL | 15:117db924cf7c | 4820 | |
wolfSSL | 15:117db924cf7c | 4821 | if (badDate != 0) |
wolfSSL | 15:117db924cf7c | 4822 | return badDate; |
wolfSSL | 15:117db924cf7c | 4823 | |
wolfSSL | 15:117db924cf7c | 4824 | return 0; |
wolfSSL | 15:117db924cf7c | 4825 | } |
wolfSSL | 15:117db924cf7c | 4826 | |
wolfSSL | 15:117db924cf7c | 4827 | |
wolfSSL | 15:117db924cf7c | 4828 | int wc_GetDateInfo(const byte* certDate, int certDateSz, const byte** date, |
wolfSSL | 15:117db924cf7c | 4829 | byte* format, int* length) |
wolfSSL | 15:117db924cf7c | 4830 | { |
wolfSSL | 15:117db924cf7c | 4831 | int ret; |
wolfSSL | 15:117db924cf7c | 4832 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 4833 | |
wolfSSL | 15:117db924cf7c | 4834 | ret = GetDateInfo(certDate, &idx, date, format, length, certDateSz); |
wolfSSL | 15:117db924cf7c | 4835 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 4836 | return ret; |
wolfSSL | 15:117db924cf7c | 4837 | |
wolfSSL | 15:117db924cf7c | 4838 | return 0; |
wolfSSL | 15:117db924cf7c | 4839 | } |
wolfSSL | 15:117db924cf7c | 4840 | |
wolfSSL | 15:117db924cf7c | 4841 | #ifndef NO_ASN_TIME |
wolfSSL | 15:117db924cf7c | 4842 | int wc_GetDateAsCalendarTime(const byte* date, int length, byte format, |
wolfSSL | 15:117db924cf7c | 4843 | struct tm* timearg) |
wolfSSL | 15:117db924cf7c | 4844 | { |
wolfSSL | 15:117db924cf7c | 4845 | int idx = 0; |
wolfSSL | 15:117db924cf7c | 4846 | (void)length; |
wolfSSL | 15:117db924cf7c | 4847 | if (!ExtractDate(date, format, timearg, &idx)) |
wolfSSL | 15:117db924cf7c | 4848 | return ASN_TIME_E; |
wolfSSL | 15:117db924cf7c | 4849 | return 0; |
wolfSSL | 15:117db924cf7c | 4850 | } |
wolfSSL | 15:117db924cf7c | 4851 | |
wolfSSL | 15:117db924cf7c | 4852 | #if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) |
wolfSSL | 15:117db924cf7c | 4853 | int wc_GetCertDates(Cert* cert, struct tm* before, struct tm* after) |
wolfSSL | 15:117db924cf7c | 4854 | { |
wolfSSL | 15:117db924cf7c | 4855 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 4856 | const byte* date; |
wolfSSL | 15:117db924cf7c | 4857 | byte format; |
wolfSSL | 15:117db924cf7c | 4858 | int length; |
wolfSSL | 15:117db924cf7c | 4859 | |
wolfSSL | 15:117db924cf7c | 4860 | if (cert == NULL) |
wolfSSL | 15:117db924cf7c | 4861 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 4862 | |
wolfSSL | 15:117db924cf7c | 4863 | if (before && cert->beforeDateSz > 0) { |
wolfSSL | 15:117db924cf7c | 4864 | ret = wc_GetDateInfo(cert->beforeDate, cert->beforeDateSz, &date, |
wolfSSL | 15:117db924cf7c | 4865 | &format, &length); |
wolfSSL | 15:117db924cf7c | 4866 | if (ret == 0) |
wolfSSL | 15:117db924cf7c | 4867 | ret = wc_GetDateAsCalendarTime(date, length, format, before); |
wolfSSL | 15:117db924cf7c | 4868 | } |
wolfSSL | 15:117db924cf7c | 4869 | if (after && cert->afterDateSz > 0) { |
wolfSSL | 15:117db924cf7c | 4870 | ret = wc_GetDateInfo(cert->afterDate, cert->afterDateSz, &date, |
wolfSSL | 15:117db924cf7c | 4871 | &format, &length); |
wolfSSL | 15:117db924cf7c | 4872 | if (ret == 0) |
wolfSSL | 15:117db924cf7c | 4873 | ret = wc_GetDateAsCalendarTime(date, length, format, after); |
wolfSSL | 15:117db924cf7c | 4874 | } |
wolfSSL | 15:117db924cf7c | 4875 | |
wolfSSL | 15:117db924cf7c | 4876 | return ret; |
wolfSSL | 15:117db924cf7c | 4877 | } |
wolfSSL | 15:117db924cf7c | 4878 | #endif /* WOLFSSL_CERT_GEN && WOLFSSL_ALT_NAMES */ |
wolfSSL | 15:117db924cf7c | 4879 | #endif /* !NO_ASN_TIME */ |
wolfSSL | 15:117db924cf7c | 4880 | |
wolfSSL | 15:117db924cf7c | 4881 | |
wolfSSL | 15:117db924cf7c | 4882 | int DecodeToKey(DecodedCert* cert, int verify) |
wolfSSL | 15:117db924cf7c | 4883 | { |
wolfSSL | 15:117db924cf7c | 4884 | int badDate = 0; |
wolfSSL | 15:117db924cf7c | 4885 | int ret; |
wolfSSL | 15:117db924cf7c | 4886 | |
wolfSSL | 15:117db924cf7c | 4887 | if ( (ret = GetCertHeader(cert)) < 0) |
wolfSSL | 15:117db924cf7c | 4888 | return ret; |
wolfSSL | 15:117db924cf7c | 4889 | |
wolfSSL | 15:117db924cf7c | 4890 | WOLFSSL_MSG("Got Cert Header"); |
wolfSSL | 15:117db924cf7c | 4891 | |
wolfSSL | 15:117db924cf7c | 4892 | if ( (ret = GetAlgoId(cert->source, &cert->srcIdx, &cert->signatureOID, |
wolfSSL | 15:117db924cf7c | 4893 | oidSigType, cert->maxIdx)) < 0) |
wolfSSL | 15:117db924cf7c | 4894 | return ret; |
wolfSSL | 15:117db924cf7c | 4895 | |
wolfSSL | 15:117db924cf7c | 4896 | WOLFSSL_MSG("Got Algo ID"); |
wolfSSL | 15:117db924cf7c | 4897 | |
wolfSSL | 15:117db924cf7c | 4898 | if ( (ret = GetName(cert, ISSUER)) < 0) |
wolfSSL | 15:117db924cf7c | 4899 | return ret; |
wolfSSL | 15:117db924cf7c | 4900 | |
wolfSSL | 15:117db924cf7c | 4901 | if ( (ret = GetValidity(cert, verify)) < 0) |
wolfSSL | 15:117db924cf7c | 4902 | badDate = ret; |
wolfSSL | 15:117db924cf7c | 4903 | |
wolfSSL | 15:117db924cf7c | 4904 | if ( (ret = GetName(cert, SUBJECT)) < 0) |
wolfSSL | 15:117db924cf7c | 4905 | return ret; |
wolfSSL | 15:117db924cf7c | 4906 | |
wolfSSL | 15:117db924cf7c | 4907 | WOLFSSL_MSG("Got Subject Name"); |
wolfSSL | 15:117db924cf7c | 4908 | |
wolfSSL | 15:117db924cf7c | 4909 | if ( (ret = GetKey(cert)) < 0) |
wolfSSL | 15:117db924cf7c | 4910 | return ret; |
wolfSSL | 15:117db924cf7c | 4911 | |
wolfSSL | 15:117db924cf7c | 4912 | WOLFSSL_MSG("Got Key"); |
wolfSSL | 15:117db924cf7c | 4913 | |
wolfSSL | 15:117db924cf7c | 4914 | if (badDate != 0) |
wolfSSL | 15:117db924cf7c | 4915 | return badDate; |
wolfSSL | 15:117db924cf7c | 4916 | |
wolfSSL | 15:117db924cf7c | 4917 | return ret; |
wolfSSL | 15:117db924cf7c | 4918 | } |
wolfSSL | 15:117db924cf7c | 4919 | |
wolfSSL | 15:117db924cf7c | 4920 | static int GetSignature(DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 4921 | { |
wolfSSL | 15:117db924cf7c | 4922 | int length; |
wolfSSL | 15:117db924cf7c | 4923 | int ret; |
wolfSSL | 15:117db924cf7c | 4924 | ret = CheckBitString(cert->source, &cert->srcIdx, &length, cert->maxIdx, 1, |
wolfSSL | 15:117db924cf7c | 4925 | NULL); |
wolfSSL | 15:117db924cf7c | 4926 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 4927 | return ret; |
wolfSSL | 15:117db924cf7c | 4928 | |
wolfSSL | 15:117db924cf7c | 4929 | cert->sigLength = length; |
wolfSSL | 15:117db924cf7c | 4930 | cert->signature = &cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 4931 | cert->srcIdx += cert->sigLength; |
wolfSSL | 15:117db924cf7c | 4932 | |
wolfSSL | 15:117db924cf7c | 4933 | return 0; |
wolfSSL | 15:117db924cf7c | 4934 | } |
wolfSSL | 15:117db924cf7c | 4935 | |
wolfSSL | 15:117db924cf7c | 4936 | static word32 SetOctetString8Bit(word32 len, byte* output) |
wolfSSL | 15:117db924cf7c | 4937 | { |
wolfSSL | 15:117db924cf7c | 4938 | output[0] = ASN_OCTET_STRING; |
wolfSSL | 15:117db924cf7c | 4939 | output[1] = (byte)len; |
wolfSSL | 15:117db924cf7c | 4940 | return 2; |
wolfSSL | 15:117db924cf7c | 4941 | } |
wolfSSL | 15:117db924cf7c | 4942 | |
wolfSSL | 15:117db924cf7c | 4943 | static word32 SetDigest(const byte* digest, word32 digSz, byte* output) |
wolfSSL | 15:117db924cf7c | 4944 | { |
wolfSSL | 15:117db924cf7c | 4945 | word32 idx = SetOctetString8Bit(digSz, output); |
wolfSSL | 15:117db924cf7c | 4946 | XMEMCPY(&output[idx], digest, digSz); |
wolfSSL | 15:117db924cf7c | 4947 | |
wolfSSL | 15:117db924cf7c | 4948 | return idx + digSz; |
wolfSSL | 15:117db924cf7c | 4949 | } |
wolfSSL | 15:117db924cf7c | 4950 | |
wolfSSL | 15:117db924cf7c | 4951 | |
wolfSSL | 15:117db924cf7c | 4952 | static word32 BytePrecision(word32 value) |
wolfSSL | 15:117db924cf7c | 4953 | { |
wolfSSL | 15:117db924cf7c | 4954 | word32 i; |
wolfSSL | 15:117db924cf7c | 4955 | for (i = sizeof(value); i; --i) |
wolfSSL | 15:117db924cf7c | 4956 | if (value >> ((i - 1) * WOLFSSL_BIT_SIZE)) |
wolfSSL | 15:117db924cf7c | 4957 | break; |
wolfSSL | 15:117db924cf7c | 4958 | |
wolfSSL | 15:117db924cf7c | 4959 | return i; |
wolfSSL | 15:117db924cf7c | 4960 | } |
wolfSSL | 15:117db924cf7c | 4961 | |
wolfSSL | 15:117db924cf7c | 4962 | |
wolfSSL | 15:117db924cf7c | 4963 | WOLFSSL_LOCAL word32 SetLength(word32 length, byte* output) |
wolfSSL | 15:117db924cf7c | 4964 | { |
wolfSSL | 15:117db924cf7c | 4965 | word32 i = 0, j; |
wolfSSL | 15:117db924cf7c | 4966 | |
wolfSSL | 15:117db924cf7c | 4967 | if (length < ASN_LONG_LENGTH) |
wolfSSL | 15:117db924cf7c | 4968 | output[i++] = (byte)length; |
wolfSSL | 15:117db924cf7c | 4969 | else { |
wolfSSL | 15:117db924cf7c | 4970 | output[i++] = (byte)(BytePrecision(length) | ASN_LONG_LENGTH); |
wolfSSL | 15:117db924cf7c | 4971 | |
wolfSSL | 15:117db924cf7c | 4972 | for (j = BytePrecision(length); j; --j) { |
wolfSSL | 15:117db924cf7c | 4973 | output[i] = (byte)(length >> ((j - 1) * WOLFSSL_BIT_SIZE)); |
wolfSSL | 15:117db924cf7c | 4974 | i++; |
wolfSSL | 15:117db924cf7c | 4975 | } |
wolfSSL | 15:117db924cf7c | 4976 | } |
wolfSSL | 15:117db924cf7c | 4977 | |
wolfSSL | 15:117db924cf7c | 4978 | return i; |
wolfSSL | 15:117db924cf7c | 4979 | } |
wolfSSL | 15:117db924cf7c | 4980 | |
wolfSSL | 15:117db924cf7c | 4981 | |
wolfSSL | 15:117db924cf7c | 4982 | WOLFSSL_LOCAL word32 SetSequence(word32 len, byte* output) |
wolfSSL | 15:117db924cf7c | 4983 | { |
wolfSSL | 15:117db924cf7c | 4984 | output[0] = ASN_SEQUENCE | ASN_CONSTRUCTED; |
wolfSSL | 15:117db924cf7c | 4985 | return SetLength(len, output + 1) + 1; |
wolfSSL | 15:117db924cf7c | 4986 | } |
wolfSSL | 15:117db924cf7c | 4987 | |
wolfSSL | 15:117db924cf7c | 4988 | WOLFSSL_LOCAL word32 SetOctetString(word32 len, byte* output) |
wolfSSL | 15:117db924cf7c | 4989 | { |
wolfSSL | 15:117db924cf7c | 4990 | output[0] = ASN_OCTET_STRING; |
wolfSSL | 15:117db924cf7c | 4991 | return SetLength(len, output + 1) + 1; |
wolfSSL | 15:117db924cf7c | 4992 | } |
wolfSSL | 15:117db924cf7c | 4993 | |
wolfSSL | 15:117db924cf7c | 4994 | /* Write a set header to output */ |
wolfSSL | 15:117db924cf7c | 4995 | WOLFSSL_LOCAL word32 SetSet(word32 len, byte* output) |
wolfSSL | 15:117db924cf7c | 4996 | { |
wolfSSL | 15:117db924cf7c | 4997 | output[0] = ASN_SET | ASN_CONSTRUCTED; |
wolfSSL | 15:117db924cf7c | 4998 | return SetLength(len, output + 1) + 1; |
wolfSSL | 15:117db924cf7c | 4999 | } |
wolfSSL | 15:117db924cf7c | 5000 | |
wolfSSL | 15:117db924cf7c | 5001 | WOLFSSL_LOCAL word32 SetImplicit(byte tag, byte number, word32 len, byte* output) |
wolfSSL | 15:117db924cf7c | 5002 | { |
wolfSSL | 15:117db924cf7c | 5003 | |
wolfSSL | 15:117db924cf7c | 5004 | output[0] = ((tag == ASN_SEQUENCE || tag == ASN_SET) ? ASN_CONSTRUCTED : 0) |
wolfSSL | 15:117db924cf7c | 5005 | | ASN_CONTEXT_SPECIFIC | number; |
wolfSSL | 15:117db924cf7c | 5006 | return SetLength(len, output + 1) + 1; |
wolfSSL | 15:117db924cf7c | 5007 | } |
wolfSSL | 15:117db924cf7c | 5008 | |
wolfSSL | 15:117db924cf7c | 5009 | WOLFSSL_LOCAL word32 SetExplicit(byte number, word32 len, byte* output) |
wolfSSL | 15:117db924cf7c | 5010 | { |
wolfSSL | 15:117db924cf7c | 5011 | output[0] = ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | number; |
wolfSSL | 15:117db924cf7c | 5012 | return SetLength(len, output + 1) + 1; |
wolfSSL | 15:117db924cf7c | 5013 | } |
wolfSSL | 15:117db924cf7c | 5014 | |
wolfSSL | 15:117db924cf7c | 5015 | |
wolfSSL | 15:117db924cf7c | 5016 | #if defined(HAVE_ECC) |
wolfSSL | 15:117db924cf7c | 5017 | |
wolfSSL | 15:117db924cf7c | 5018 | static int SetCurve(ecc_key* key, byte* output) |
wolfSSL | 15:117db924cf7c | 5019 | { |
wolfSSL | 15:117db924cf7c | 5020 | #ifdef HAVE_OID_ENCODING |
wolfSSL | 15:117db924cf7c | 5021 | int ret; |
wolfSSL | 15:117db924cf7c | 5022 | #endif |
wolfSSL | 15:117db924cf7c | 5023 | int idx = 0; |
wolfSSL | 15:117db924cf7c | 5024 | word32 oidSz = 0; |
wolfSSL | 15:117db924cf7c | 5025 | |
wolfSSL | 15:117db924cf7c | 5026 | /* validate key */ |
wolfSSL | 15:117db924cf7c | 5027 | if (key == NULL || key->dp == NULL) { |
wolfSSL | 15:117db924cf7c | 5028 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 5029 | } |
wolfSSL | 15:117db924cf7c | 5030 | |
wolfSSL | 15:117db924cf7c | 5031 | #ifdef HAVE_OID_ENCODING |
wolfSSL | 15:117db924cf7c | 5032 | ret = EncodeObjectId(key->dp->oid, key->dp->oidSz, NULL, &oidSz); |
wolfSSL | 15:117db924cf7c | 5033 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 5034 | return ret; |
wolfSSL | 15:117db924cf7c | 5035 | } |
wolfSSL | 15:117db924cf7c | 5036 | #else |
wolfSSL | 15:117db924cf7c | 5037 | oidSz = key->dp->oidSz; |
wolfSSL | 15:117db924cf7c | 5038 | #endif |
wolfSSL | 15:117db924cf7c | 5039 | |
wolfSSL | 15:117db924cf7c | 5040 | idx += SetObjectId(oidSz, output); |
wolfSSL | 15:117db924cf7c | 5041 | |
wolfSSL | 15:117db924cf7c | 5042 | #ifdef HAVE_OID_ENCODING |
wolfSSL | 15:117db924cf7c | 5043 | ret = EncodeObjectId(key->dp->oid, key->dp->oidSz, output+idx, &oidSz); |
wolfSSL | 15:117db924cf7c | 5044 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 5045 | return ret; |
wolfSSL | 15:117db924cf7c | 5046 | } |
wolfSSL | 15:117db924cf7c | 5047 | #else |
wolfSSL | 15:117db924cf7c | 5048 | XMEMCPY(output+idx, key->dp->oid, oidSz); |
wolfSSL | 15:117db924cf7c | 5049 | #endif |
wolfSSL | 15:117db924cf7c | 5050 | idx += oidSz; |
wolfSSL | 15:117db924cf7c | 5051 | |
wolfSSL | 15:117db924cf7c | 5052 | return idx; |
wolfSSL | 15:117db924cf7c | 5053 | } |
wolfSSL | 15:117db924cf7c | 5054 | |
wolfSSL | 15:117db924cf7c | 5055 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 5056 | |
wolfSSL | 15:117db924cf7c | 5057 | |
wolfSSL | 15:117db924cf7c | 5058 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 5059 | static WC_INLINE int IsSigAlgoECDSA(int algoOID) |
wolfSSL | 15:117db924cf7c | 5060 | { |
wolfSSL | 15:117db924cf7c | 5061 | /* ECDSA sigAlgo must not have ASN1 NULL parameters */ |
wolfSSL | 15:117db924cf7c | 5062 | if (algoOID == CTC_SHAwECDSA || algoOID == CTC_SHA256wECDSA || |
wolfSSL | 15:117db924cf7c | 5063 | algoOID == CTC_SHA384wECDSA || algoOID == CTC_SHA512wECDSA) { |
wolfSSL | 15:117db924cf7c | 5064 | return 1; |
wolfSSL | 15:117db924cf7c | 5065 | } |
wolfSSL | 15:117db924cf7c | 5066 | |
wolfSSL | 15:117db924cf7c | 5067 | return 0; |
wolfSSL | 15:117db924cf7c | 5068 | } |
wolfSSL | 15:117db924cf7c | 5069 | #endif |
wolfSSL | 15:117db924cf7c | 5070 | |
wolfSSL | 15:117db924cf7c | 5071 | WOLFSSL_LOCAL word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz) |
wolfSSL | 15:117db924cf7c | 5072 | { |
wolfSSL | 15:117db924cf7c | 5073 | word32 tagSz, idSz, seqSz, algoSz = 0; |
wolfSSL | 15:117db924cf7c | 5074 | const byte* algoName = 0; |
wolfSSL | 15:117db924cf7c | 5075 | byte ID_Length[1 + MAX_LENGTH_SZ]; |
wolfSSL | 15:117db924cf7c | 5076 | byte seqArray[MAX_SEQ_SZ + 1]; /* add object_id to end */ |
wolfSSL | 15:117db924cf7c | 5077 | |
wolfSSL | 15:117db924cf7c | 5078 | tagSz = (type == oidHashType || |
wolfSSL | 15:117db924cf7c | 5079 | (type == oidSigType |
wolfSSL | 15:117db924cf7c | 5080 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 5081 | && !IsSigAlgoECDSA(algoOID) |
wolfSSL | 15:117db924cf7c | 5082 | #endif |
wolfSSL | 15:117db924cf7c | 5083 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 5084 | && algoOID != ED25519k |
wolfSSL | 15:117db924cf7c | 5085 | #endif |
wolfSSL | 15:117db924cf7c | 5086 | ) || |
wolfSSL | 15:117db924cf7c | 5087 | (type == oidKeyType && algoOID == RSAk)) ? 2 : 0; |
wolfSSL | 15:117db924cf7c | 5088 | |
wolfSSL | 15:117db924cf7c | 5089 | algoName = OidFromId(algoOID, type, &algoSz); |
wolfSSL | 15:117db924cf7c | 5090 | |
wolfSSL | 15:117db924cf7c | 5091 | if (algoName == NULL) { |
wolfSSL | 15:117db924cf7c | 5092 | WOLFSSL_MSG("Unknown Algorithm"); |
wolfSSL | 15:117db924cf7c | 5093 | return 0; |
wolfSSL | 15:117db924cf7c | 5094 | } |
wolfSSL | 15:117db924cf7c | 5095 | |
wolfSSL | 15:117db924cf7c | 5096 | idSz = SetObjectId(algoSz, ID_Length); |
wolfSSL | 15:117db924cf7c | 5097 | seqSz = SetSequence(idSz + algoSz + tagSz + curveSz, seqArray); |
wolfSSL | 15:117db924cf7c | 5098 | |
wolfSSL | 15:117db924cf7c | 5099 | XMEMCPY(output, seqArray, seqSz); |
wolfSSL | 15:117db924cf7c | 5100 | XMEMCPY(output + seqSz, ID_Length, idSz); |
wolfSSL | 15:117db924cf7c | 5101 | XMEMCPY(output + seqSz + idSz, algoName, algoSz); |
wolfSSL | 15:117db924cf7c | 5102 | if (tagSz == 2) |
wolfSSL | 15:117db924cf7c | 5103 | SetASNNull(&output[seqSz + idSz + algoSz]); |
wolfSSL | 15:117db924cf7c | 5104 | |
wolfSSL | 15:117db924cf7c | 5105 | return seqSz + idSz + algoSz + tagSz; |
wolfSSL | 15:117db924cf7c | 5106 | |
wolfSSL | 15:117db924cf7c | 5107 | } |
wolfSSL | 15:117db924cf7c | 5108 | |
wolfSSL | 15:117db924cf7c | 5109 | |
wolfSSL | 15:117db924cf7c | 5110 | word32 wc_EncodeSignature(byte* out, const byte* digest, word32 digSz, |
wolfSSL | 15:117db924cf7c | 5111 | int hashOID) |
wolfSSL | 15:117db924cf7c | 5112 | { |
wolfSSL | 15:117db924cf7c | 5113 | byte digArray[MAX_ENCODED_DIG_SZ]; |
wolfSSL | 15:117db924cf7c | 5114 | byte algoArray[MAX_ALGO_SZ]; |
wolfSSL | 15:117db924cf7c | 5115 | byte seqArray[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 5116 | word32 encDigSz, algoSz, seqSz; |
wolfSSL | 15:117db924cf7c | 5117 | |
wolfSSL | 15:117db924cf7c | 5118 | encDigSz = SetDigest(digest, digSz, digArray); |
wolfSSL | 15:117db924cf7c | 5119 | algoSz = SetAlgoID(hashOID, algoArray, oidHashType, 0); |
wolfSSL | 15:117db924cf7c | 5120 | seqSz = SetSequence(encDigSz + algoSz, seqArray); |
wolfSSL | 15:117db924cf7c | 5121 | |
wolfSSL | 15:117db924cf7c | 5122 | XMEMCPY(out, seqArray, seqSz); |
wolfSSL | 15:117db924cf7c | 5123 | XMEMCPY(out + seqSz, algoArray, algoSz); |
wolfSSL | 15:117db924cf7c | 5124 | XMEMCPY(out + seqSz + algoSz, digArray, encDigSz); |
wolfSSL | 15:117db924cf7c | 5125 | |
wolfSSL | 15:117db924cf7c | 5126 | return encDigSz + algoSz + seqSz; |
wolfSSL | 15:117db924cf7c | 5127 | } |
wolfSSL | 15:117db924cf7c | 5128 | |
wolfSSL | 15:117db924cf7c | 5129 | |
wolfSSL | 15:117db924cf7c | 5130 | int wc_GetCTC_HashOID(int type) |
wolfSSL | 15:117db924cf7c | 5131 | { |
wolfSSL | 15:117db924cf7c | 5132 | int ret; |
wolfSSL | 15:117db924cf7c | 5133 | enum wc_HashType hType; |
wolfSSL | 15:117db924cf7c | 5134 | |
wolfSSL | 15:117db924cf7c | 5135 | hType = wc_HashTypeConvert(type); |
wolfSSL | 15:117db924cf7c | 5136 | ret = wc_HashGetOID(hType); |
wolfSSL | 15:117db924cf7c | 5137 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 5138 | ret = 0; /* backwards compatibility */ |
wolfSSL | 15:117db924cf7c | 5139 | |
wolfSSL | 15:117db924cf7c | 5140 | return ret; |
wolfSSL | 15:117db924cf7c | 5141 | } |
wolfSSL | 15:117db924cf7c | 5142 | |
wolfSSL | 15:117db924cf7c | 5143 | void InitSignatureCtx(SignatureCtx* sigCtx, void* heap, int devId) |
wolfSSL | 15:117db924cf7c | 5144 | { |
wolfSSL | 15:117db924cf7c | 5145 | if (sigCtx) { |
wolfSSL | 15:117db924cf7c | 5146 | XMEMSET(sigCtx, 0, sizeof(SignatureCtx)); |
wolfSSL | 15:117db924cf7c | 5147 | sigCtx->devId = devId; |
wolfSSL | 15:117db924cf7c | 5148 | sigCtx->heap = heap; |
wolfSSL | 15:117db924cf7c | 5149 | } |
wolfSSL | 15:117db924cf7c | 5150 | } |
wolfSSL | 15:117db924cf7c | 5151 | |
wolfSSL | 15:117db924cf7c | 5152 | void FreeSignatureCtx(SignatureCtx* sigCtx) |
wolfSSL | 15:117db924cf7c | 5153 | { |
wolfSSL | 15:117db924cf7c | 5154 | if (sigCtx == NULL) |
wolfSSL | 15:117db924cf7c | 5155 | return; |
wolfSSL | 15:117db924cf7c | 5156 | |
wolfSSL | 15:117db924cf7c | 5157 | if (sigCtx->digest) { |
wolfSSL | 15:117db924cf7c | 5158 | XFREE(sigCtx->digest, sigCtx->heap, DYNAMIC_TYPE_DIGEST); |
wolfSSL | 15:117db924cf7c | 5159 | sigCtx->digest = NULL; |
wolfSSL | 15:117db924cf7c | 5160 | } |
wolfSSL | 15:117db924cf7c | 5161 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 5162 | if (sigCtx->plain) { |
wolfSSL | 15:117db924cf7c | 5163 | XFREE(sigCtx->plain, sigCtx->heap, DYNAMIC_TYPE_SIGNATURE); |
wolfSSL | 15:117db924cf7c | 5164 | sigCtx->plain = NULL; |
wolfSSL | 15:117db924cf7c | 5165 | } |
wolfSSL | 15:117db924cf7c | 5166 | #endif |
wolfSSL | 15:117db924cf7c | 5167 | if (sigCtx->key.ptr) { |
wolfSSL | 15:117db924cf7c | 5168 | switch (sigCtx->keyOID) { |
wolfSSL | 15:117db924cf7c | 5169 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 5170 | case RSAk: |
wolfSSL | 15:117db924cf7c | 5171 | wc_FreeRsaKey(sigCtx->key.rsa); |
wolfSSL | 15:117db924cf7c | 5172 | XFREE(sigCtx->key.ptr, sigCtx->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 5173 | break; |
wolfSSL | 15:117db924cf7c | 5174 | #endif /* !NO_RSA */ |
wolfSSL | 15:117db924cf7c | 5175 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 5176 | case ECDSAk: |
wolfSSL | 15:117db924cf7c | 5177 | wc_ecc_free(sigCtx->key.ecc); |
wolfSSL | 15:117db924cf7c | 5178 | XFREE(sigCtx->key.ecc, sigCtx->heap, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 5179 | break; |
wolfSSL | 15:117db924cf7c | 5180 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 5181 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 5182 | case ED25519k: |
wolfSSL | 15:117db924cf7c | 5183 | wc_ed25519_free(sigCtx->key.ed25519); |
wolfSSL | 15:117db924cf7c | 5184 | XFREE(sigCtx->key.ed25519, sigCtx->heap, DYNAMIC_TYPE_ED25519); |
wolfSSL | 15:117db924cf7c | 5185 | break; |
wolfSSL | 15:117db924cf7c | 5186 | #endif /* HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 5187 | default: |
wolfSSL | 15:117db924cf7c | 5188 | break; |
wolfSSL | 15:117db924cf7c | 5189 | } /* switch (keyOID) */ |
wolfSSL | 15:117db924cf7c | 5190 | sigCtx->key.ptr = NULL; |
wolfSSL | 15:117db924cf7c | 5191 | } |
wolfSSL | 15:117db924cf7c | 5192 | |
wolfSSL | 15:117db924cf7c | 5193 | /* reset state, we are done */ |
wolfSSL | 15:117db924cf7c | 5194 | sigCtx->state = SIG_STATE_BEGIN; |
wolfSSL | 15:117db924cf7c | 5195 | } |
wolfSSL | 15:117db924cf7c | 5196 | |
wolfSSL | 15:117db924cf7c | 5197 | static int HashForSignature(const byte* buf, word32 bufSz, word32 sigOID, |
wolfSSL | 15:117db924cf7c | 5198 | byte* digest, int* typeH, int* digestSz, int verify) |
wolfSSL | 15:117db924cf7c | 5199 | { |
wolfSSL | 15:117db924cf7c | 5200 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 5201 | |
wolfSSL | 15:117db924cf7c | 5202 | (void)verify; |
wolfSSL | 15:117db924cf7c | 5203 | |
wolfSSL | 15:117db924cf7c | 5204 | switch (sigOID) { |
wolfSSL | 15:117db924cf7c | 5205 | #if defined(WOLFSSL_MD2) |
wolfSSL | 15:117db924cf7c | 5206 | case CTC_MD2wRSA: |
wolfSSL | 15:117db924cf7c | 5207 | if (!verify) { |
wolfSSL | 15:117db924cf7c | 5208 | ret = HASH_TYPE_E; |
wolfSSL | 15:117db924cf7c | 5209 | WOLFSSL_MSG("MD2 not supported for signing"); |
wolfSSL | 15:117db924cf7c | 5210 | } |
wolfSSL | 15:117db924cf7c | 5211 | else if ((ret = wc_Md2Hash(buf, bufSz, digest)) == 0) { |
wolfSSL | 15:117db924cf7c | 5212 | *typeH = MD2h; |
wolfSSL | 15:117db924cf7c | 5213 | *digestSz = MD2_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 5214 | } |
wolfSSL | 15:117db924cf7c | 5215 | break; |
wolfSSL | 15:117db924cf7c | 5216 | #endif |
wolfSSL | 15:117db924cf7c | 5217 | #ifndef NO_MD5 |
wolfSSL | 15:117db924cf7c | 5218 | case CTC_MD5wRSA: |
wolfSSL | 15:117db924cf7c | 5219 | if ((ret = wc_Md5Hash(buf, bufSz, digest)) == 0) { |
wolfSSL | 15:117db924cf7c | 5220 | *typeH = MD5h; |
wolfSSL | 15:117db924cf7c | 5221 | *digestSz = WC_MD5_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 5222 | } |
wolfSSL | 15:117db924cf7c | 5223 | break; |
wolfSSL | 15:117db924cf7c | 5224 | #endif |
wolfSSL | 15:117db924cf7c | 5225 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 5226 | case CTC_SHAwRSA: |
wolfSSL | 15:117db924cf7c | 5227 | case CTC_SHAwDSA: |
wolfSSL | 15:117db924cf7c | 5228 | case CTC_SHAwECDSA: |
wolfSSL | 15:117db924cf7c | 5229 | if ((ret = wc_ShaHash(buf, bufSz, digest)) == 0) { |
wolfSSL | 15:117db924cf7c | 5230 | *typeH = SHAh; |
wolfSSL | 15:117db924cf7c | 5231 | *digestSz = WC_SHA_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 5232 | } |
wolfSSL | 15:117db924cf7c | 5233 | break; |
wolfSSL | 15:117db924cf7c | 5234 | #endif |
wolfSSL | 15:117db924cf7c | 5235 | #ifdef WOLFSSL_SHA224 |
wolfSSL | 15:117db924cf7c | 5236 | case CTC_SHA224wRSA: |
wolfSSL | 15:117db924cf7c | 5237 | case CTC_SHA224wECDSA: |
wolfSSL | 15:117db924cf7c | 5238 | if ((ret = wc_Sha224Hash(buf, bufSz, digest)) == 0) { |
wolfSSL | 15:117db924cf7c | 5239 | *typeH = SHA224h; |
wolfSSL | 15:117db924cf7c | 5240 | *digestSz = WC_SHA224_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 5241 | } |
wolfSSL | 15:117db924cf7c | 5242 | break; |
wolfSSL | 15:117db924cf7c | 5243 | #endif |
wolfSSL | 15:117db924cf7c | 5244 | #ifndef NO_SHA256 |
wolfSSL | 15:117db924cf7c | 5245 | case CTC_SHA256wRSA: |
wolfSSL | 15:117db924cf7c | 5246 | case CTC_SHA256wECDSA: |
wolfSSL | 15:117db924cf7c | 5247 | if ((ret = wc_Sha256Hash(buf, bufSz, digest)) == 0) { |
wolfSSL | 15:117db924cf7c | 5248 | *typeH = SHA256h; |
wolfSSL | 15:117db924cf7c | 5249 | *digestSz = WC_SHA256_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 5250 | } |
wolfSSL | 15:117db924cf7c | 5251 | break; |
wolfSSL | 15:117db924cf7c | 5252 | #endif |
wolfSSL | 15:117db924cf7c | 5253 | #ifdef WOLFSSL_SHA384 |
wolfSSL | 15:117db924cf7c | 5254 | case CTC_SHA384wRSA: |
wolfSSL | 15:117db924cf7c | 5255 | case CTC_SHA384wECDSA: |
wolfSSL | 15:117db924cf7c | 5256 | if ((ret = wc_Sha384Hash(buf, bufSz, digest)) == 0) { |
wolfSSL | 15:117db924cf7c | 5257 | *typeH = SHA384h; |
wolfSSL | 15:117db924cf7c | 5258 | *digestSz = WC_SHA384_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 5259 | } |
wolfSSL | 15:117db924cf7c | 5260 | break; |
wolfSSL | 15:117db924cf7c | 5261 | #endif |
wolfSSL | 15:117db924cf7c | 5262 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 15:117db924cf7c | 5263 | case CTC_SHA512wRSA: |
wolfSSL | 15:117db924cf7c | 5264 | case CTC_SHA512wECDSA: |
wolfSSL | 15:117db924cf7c | 5265 | if ((ret = wc_Sha512Hash(buf, bufSz, digest)) == 0) { |
wolfSSL | 15:117db924cf7c | 5266 | *typeH = SHA512h; |
wolfSSL | 15:117db924cf7c | 5267 | *digestSz = WC_SHA512_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 5268 | } |
wolfSSL | 15:117db924cf7c | 5269 | break; |
wolfSSL | 15:117db924cf7c | 5270 | #endif |
wolfSSL | 15:117db924cf7c | 5271 | case CTC_ED25519: |
wolfSSL | 15:117db924cf7c | 5272 | /* Hashes done in signing operation. |
wolfSSL | 15:117db924cf7c | 5273 | * Two dependent hashes with prefixes performed. |
wolfSSL | 15:117db924cf7c | 5274 | */ |
wolfSSL | 15:117db924cf7c | 5275 | break; |
wolfSSL | 15:117db924cf7c | 5276 | default: |
wolfSSL | 15:117db924cf7c | 5277 | ret = HASH_TYPE_E; |
wolfSSL | 15:117db924cf7c | 5278 | WOLFSSL_MSG("Hash for Signature has unsupported type"); |
wolfSSL | 15:117db924cf7c | 5279 | } |
wolfSSL | 15:117db924cf7c | 5280 | |
wolfSSL | 15:117db924cf7c | 5281 | return ret; |
wolfSSL | 15:117db924cf7c | 5282 | } |
wolfSSL | 15:117db924cf7c | 5283 | |
wolfSSL | 15:117db924cf7c | 5284 | /* Return codes: 0=Success, Negative (see error-crypt.h), ASN_SIG_CONFIRM_E */ |
wolfSSL | 15:117db924cf7c | 5285 | static int ConfirmSignature(SignatureCtx* sigCtx, |
wolfSSL | 15:117db924cf7c | 5286 | const byte* buf, word32 bufSz, |
wolfSSL | 15:117db924cf7c | 5287 | const byte* key, word32 keySz, word32 keyOID, |
wolfSSL | 15:117db924cf7c | 5288 | const byte* sig, word32 sigSz, word32 sigOID) |
wolfSSL | 15:117db924cf7c | 5289 | { |
wolfSSL | 15:117db924cf7c | 5290 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 5291 | |
wolfSSL | 15:117db924cf7c | 5292 | if (sigCtx == NULL || buf == NULL || bufSz == 0 || key == NULL || |
wolfSSL | 15:117db924cf7c | 5293 | keySz == 0 || sig == NULL || sigSz == 0) { |
wolfSSL | 15:117db924cf7c | 5294 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 5295 | } |
wolfSSL | 15:117db924cf7c | 5296 | |
wolfSSL | 15:117db924cf7c | 5297 | (void)key; |
wolfSSL | 15:117db924cf7c | 5298 | (void)keySz; |
wolfSSL | 15:117db924cf7c | 5299 | (void)sig; |
wolfSSL | 15:117db924cf7c | 5300 | (void)sigSz; |
wolfSSL | 15:117db924cf7c | 5301 | |
wolfSSL | 15:117db924cf7c | 5302 | WOLFSSL_ENTER("ConfirmSignature"); |
wolfSSL | 15:117db924cf7c | 5303 | |
wolfSSL | 15:117db924cf7c | 5304 | switch (sigCtx->state) { |
wolfSSL | 15:117db924cf7c | 5305 | case SIG_STATE_BEGIN: |
wolfSSL | 15:117db924cf7c | 5306 | { |
wolfSSL | 15:117db924cf7c | 5307 | sigCtx->digest = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, sigCtx->heap, |
wolfSSL | 15:117db924cf7c | 5308 | DYNAMIC_TYPE_DIGEST); |
wolfSSL | 15:117db924cf7c | 5309 | if (sigCtx->digest == NULL) { |
wolfSSL | 15:117db924cf7c | 5310 | ERROR_OUT(MEMORY_E, exit_cs); |
wolfSSL | 15:117db924cf7c | 5311 | } |
wolfSSL | 15:117db924cf7c | 5312 | |
wolfSSL | 15:117db924cf7c | 5313 | sigCtx->state = SIG_STATE_HASH; |
wolfSSL | 15:117db924cf7c | 5314 | } /* SIG_STATE_BEGIN */ |
wolfSSL | 15:117db924cf7c | 5315 | FALL_THROUGH; |
wolfSSL | 15:117db924cf7c | 5316 | |
wolfSSL | 15:117db924cf7c | 5317 | case SIG_STATE_HASH: |
wolfSSL | 15:117db924cf7c | 5318 | { |
wolfSSL | 15:117db924cf7c | 5319 | ret = HashForSignature(buf, bufSz, sigOID, sigCtx->digest, |
wolfSSL | 15:117db924cf7c | 5320 | &sigCtx->typeH, &sigCtx->digestSz, 1); |
wolfSSL | 15:117db924cf7c | 5321 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 5322 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5323 | } |
wolfSSL | 15:117db924cf7c | 5324 | |
wolfSSL | 15:117db924cf7c | 5325 | sigCtx->state = SIG_STATE_KEY; |
wolfSSL | 15:117db924cf7c | 5326 | } /* SIG_STATE_HASH */ |
wolfSSL | 15:117db924cf7c | 5327 | FALL_THROUGH; |
wolfSSL | 15:117db924cf7c | 5328 | |
wolfSSL | 15:117db924cf7c | 5329 | case SIG_STATE_KEY: |
wolfSSL | 15:117db924cf7c | 5330 | { |
wolfSSL | 15:117db924cf7c | 5331 | sigCtx->keyOID = keyOID; |
wolfSSL | 15:117db924cf7c | 5332 | |
wolfSSL | 15:117db924cf7c | 5333 | switch (keyOID) { |
wolfSSL | 15:117db924cf7c | 5334 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 5335 | case RSAk: |
wolfSSL | 15:117db924cf7c | 5336 | { |
wolfSSL | 15:117db924cf7c | 5337 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 5338 | |
wolfSSL | 15:117db924cf7c | 5339 | sigCtx->key.rsa = (RsaKey*)XMALLOC(sizeof(RsaKey), |
wolfSSL | 15:117db924cf7c | 5340 | sigCtx->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 5341 | sigCtx->plain = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, |
wolfSSL | 15:117db924cf7c | 5342 | sigCtx->heap, DYNAMIC_TYPE_SIGNATURE); |
wolfSSL | 15:117db924cf7c | 5343 | if (sigCtx->key.rsa == NULL || sigCtx->plain == NULL) { |
wolfSSL | 15:117db924cf7c | 5344 | ERROR_OUT(MEMORY_E, exit_cs); |
wolfSSL | 15:117db924cf7c | 5345 | } |
wolfSSL | 15:117db924cf7c | 5346 | if ((ret = wc_InitRsaKey_ex(sigCtx->key.rsa, sigCtx->heap, |
wolfSSL | 15:117db924cf7c | 5347 | sigCtx->devId)) != 0) { |
wolfSSL | 15:117db924cf7c | 5348 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5349 | } |
wolfSSL | 15:117db924cf7c | 5350 | if (sigSz > MAX_ENCODED_SIG_SZ) { |
wolfSSL | 15:117db924cf7c | 5351 | WOLFSSL_MSG("Verify Signature is too big"); |
wolfSSL | 15:117db924cf7c | 5352 | ERROR_OUT(BUFFER_E, exit_cs); |
wolfSSL | 15:117db924cf7c | 5353 | } |
wolfSSL | 15:117db924cf7c | 5354 | if ((ret = wc_RsaPublicKeyDecode(key, &idx, sigCtx->key.rsa, |
wolfSSL | 15:117db924cf7c | 5355 | keySz)) != 0) { |
wolfSSL | 15:117db924cf7c | 5356 | WOLFSSL_MSG("ASN Key decode error RSA"); |
wolfSSL | 15:117db924cf7c | 5357 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5358 | } |
wolfSSL | 15:117db924cf7c | 5359 | XMEMCPY(sigCtx->plain, sig, sigSz); |
wolfSSL | 15:117db924cf7c | 5360 | sigCtx->out = NULL; |
wolfSSL | 15:117db924cf7c | 5361 | |
wolfSSL | 15:117db924cf7c | 5362 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 5363 | sigCtx->asyncDev = &sigCtx->key.rsa->asyncDev; |
wolfSSL | 15:117db924cf7c | 5364 | #endif |
wolfSSL | 15:117db924cf7c | 5365 | break; |
wolfSSL | 15:117db924cf7c | 5366 | } |
wolfSSL | 15:117db924cf7c | 5367 | #endif /* !NO_RSA */ |
wolfSSL | 15:117db924cf7c | 5368 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 5369 | case ECDSAk: |
wolfSSL | 15:117db924cf7c | 5370 | { |
wolfSSL | 15:117db924cf7c | 5371 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 5372 | |
wolfSSL | 15:117db924cf7c | 5373 | sigCtx->verify = 0; |
wolfSSL | 15:117db924cf7c | 5374 | sigCtx->key.ecc = (ecc_key*)XMALLOC(sizeof(ecc_key), |
wolfSSL | 15:117db924cf7c | 5375 | sigCtx->heap, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 5376 | if (sigCtx->key.ecc == NULL) { |
wolfSSL | 15:117db924cf7c | 5377 | ERROR_OUT(MEMORY_E, exit_cs); |
wolfSSL | 15:117db924cf7c | 5378 | } |
wolfSSL | 15:117db924cf7c | 5379 | if ((ret = wc_ecc_init_ex(sigCtx->key.ecc, sigCtx->heap, |
wolfSSL | 15:117db924cf7c | 5380 | sigCtx->devId)) < 0) { |
wolfSSL | 15:117db924cf7c | 5381 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5382 | } |
wolfSSL | 15:117db924cf7c | 5383 | ret = wc_EccPublicKeyDecode(key, &idx, sigCtx->key.ecc, |
wolfSSL | 15:117db924cf7c | 5384 | keySz); |
wolfSSL | 15:117db924cf7c | 5385 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 5386 | WOLFSSL_MSG("ASN Key import error ECC"); |
wolfSSL | 15:117db924cf7c | 5387 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5388 | } |
wolfSSL | 15:117db924cf7c | 5389 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 5390 | sigCtx->asyncDev = &sigCtx->key.ecc->asyncDev; |
wolfSSL | 15:117db924cf7c | 5391 | #endif |
wolfSSL | 15:117db924cf7c | 5392 | break; |
wolfSSL | 15:117db924cf7c | 5393 | } |
wolfSSL | 15:117db924cf7c | 5394 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 5395 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 5396 | case ED25519k: |
wolfSSL | 15:117db924cf7c | 5397 | { |
wolfSSL | 15:117db924cf7c | 5398 | sigCtx->verify = 0; |
wolfSSL | 15:117db924cf7c | 5399 | sigCtx->key.ed25519 = (ed25519_key*)XMALLOC( |
wolfSSL | 15:117db924cf7c | 5400 | sizeof(ed25519_key), sigCtx->heap, |
wolfSSL | 15:117db924cf7c | 5401 | DYNAMIC_TYPE_ED25519); |
wolfSSL | 15:117db924cf7c | 5402 | if (sigCtx->key.ed25519 == NULL) { |
wolfSSL | 15:117db924cf7c | 5403 | ERROR_OUT(MEMORY_E, exit_cs); |
wolfSSL | 15:117db924cf7c | 5404 | } |
wolfSSL | 15:117db924cf7c | 5405 | if ((ret = wc_ed25519_init(sigCtx->key.ed25519)) < 0) { |
wolfSSL | 15:117db924cf7c | 5406 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5407 | } |
wolfSSL | 15:117db924cf7c | 5408 | if ((ret = wc_ed25519_import_public(key, keySz, |
wolfSSL | 15:117db924cf7c | 5409 | sigCtx->key.ed25519)) < 0) { |
wolfSSL | 15:117db924cf7c | 5410 | WOLFSSL_MSG("ASN Key import error ED25519"); |
wolfSSL | 15:117db924cf7c | 5411 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5412 | } |
wolfSSL | 15:117db924cf7c | 5413 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 5414 | sigCtx->asyncDev = &sigCtx->key.ed25519->asyncDev; |
wolfSSL | 15:117db924cf7c | 5415 | #endif |
wolfSSL | 15:117db924cf7c | 5416 | break; |
wolfSSL | 15:117db924cf7c | 5417 | } |
wolfSSL | 15:117db924cf7c | 5418 | #endif |
wolfSSL | 15:117db924cf7c | 5419 | default: |
wolfSSL | 15:117db924cf7c | 5420 | WOLFSSL_MSG("Verify Key type unknown"); |
wolfSSL | 15:117db924cf7c | 5421 | ret = ASN_UNKNOWN_OID_E; |
wolfSSL | 15:117db924cf7c | 5422 | break; |
wolfSSL | 15:117db924cf7c | 5423 | } /* switch (keyOID) */ |
wolfSSL | 15:117db924cf7c | 5424 | |
wolfSSL | 15:117db924cf7c | 5425 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 5426 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5427 | } |
wolfSSL | 15:117db924cf7c | 5428 | |
wolfSSL | 15:117db924cf7c | 5429 | sigCtx->state = SIG_STATE_DO; |
wolfSSL | 15:117db924cf7c | 5430 | |
wolfSSL | 15:117db924cf7c | 5431 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 5432 | if (sigCtx->devId != INVALID_DEVID && sigCtx->asyncDev && sigCtx->asyncCtx) { |
wolfSSL | 15:117db924cf7c | 5433 | /* make sure event is intialized */ |
wolfSSL | 15:117db924cf7c | 5434 | WOLF_EVENT* event = &sigCtx->asyncDev->event; |
wolfSSL | 15:117db924cf7c | 5435 | ret = wolfAsync_EventInit(event, WOLF_EVENT_TYPE_ASYNC_WOLFSSL, |
wolfSSL | 15:117db924cf7c | 5436 | sigCtx->asyncCtx, WC_ASYNC_FLAG_CALL_AGAIN); |
wolfSSL | 15:117db924cf7c | 5437 | } |
wolfSSL | 15:117db924cf7c | 5438 | #endif |
wolfSSL | 15:117db924cf7c | 5439 | } /* SIG_STATE_KEY */ |
wolfSSL | 15:117db924cf7c | 5440 | FALL_THROUGH; |
wolfSSL | 15:117db924cf7c | 5441 | |
wolfSSL | 15:117db924cf7c | 5442 | case SIG_STATE_DO: |
wolfSSL | 15:117db924cf7c | 5443 | { |
wolfSSL | 15:117db924cf7c | 5444 | switch (keyOID) { |
wolfSSL | 15:117db924cf7c | 5445 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 5446 | case RSAk: |
wolfSSL | 15:117db924cf7c | 5447 | { |
wolfSSL | 15:117db924cf7c | 5448 | #ifdef HAVE_PK_CALLBACKS |
wolfSSL | 15:117db924cf7c | 5449 | if (sigCtx->pkCbRsa) { |
wolfSSL | 15:117db924cf7c | 5450 | ret = sigCtx->pkCbRsa( |
wolfSSL | 15:117db924cf7c | 5451 | sigCtx->plain, sigSz, &sigCtx->out, |
wolfSSL | 15:117db924cf7c | 5452 | key, keySz, |
wolfSSL | 15:117db924cf7c | 5453 | sigCtx->pkCtxRsa); |
wolfSSL | 15:117db924cf7c | 5454 | } |
wolfSSL | 15:117db924cf7c | 5455 | else |
wolfSSL | 15:117db924cf7c | 5456 | #endif /* HAVE_PK_CALLBACKS */ |
wolfSSL | 15:117db924cf7c | 5457 | { |
wolfSSL | 15:117db924cf7c | 5458 | ret = wc_RsaSSL_VerifyInline(sigCtx->plain, sigSz, |
wolfSSL | 15:117db924cf7c | 5459 | &sigCtx->out, sigCtx->key.rsa); |
wolfSSL | 15:117db924cf7c | 5460 | } |
wolfSSL | 15:117db924cf7c | 5461 | break; |
wolfSSL | 15:117db924cf7c | 5462 | } |
wolfSSL | 15:117db924cf7c | 5463 | #endif /* !NO_RSA */ |
wolfSSL | 15:117db924cf7c | 5464 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 5465 | case ECDSAk: |
wolfSSL | 15:117db924cf7c | 5466 | { |
wolfSSL | 15:117db924cf7c | 5467 | #ifdef HAVE_PK_CALLBACKS |
wolfSSL | 15:117db924cf7c | 5468 | if (sigCtx->pkCbEcc) { |
wolfSSL | 15:117db924cf7c | 5469 | ret = sigCtx->pkCbEcc( |
wolfSSL | 15:117db924cf7c | 5470 | sig, sigSz, |
wolfSSL | 15:117db924cf7c | 5471 | sigCtx->digest, sigCtx->digestSz, |
wolfSSL | 15:117db924cf7c | 5472 | key, keySz, &sigCtx->verify, |
wolfSSL | 15:117db924cf7c | 5473 | sigCtx->pkCtxEcc); |
wolfSSL | 15:117db924cf7c | 5474 | } |
wolfSSL | 15:117db924cf7c | 5475 | else |
wolfSSL | 15:117db924cf7c | 5476 | #endif /* HAVE_PK_CALLBACKS */ |
wolfSSL | 15:117db924cf7c | 5477 | { |
wolfSSL | 15:117db924cf7c | 5478 | ret = wc_ecc_verify_hash(sig, sigSz, sigCtx->digest, |
wolfSSL | 15:117db924cf7c | 5479 | sigCtx->digestSz, &sigCtx->verify, |
wolfSSL | 15:117db924cf7c | 5480 | sigCtx->key.ecc); |
wolfSSL | 15:117db924cf7c | 5481 | } |
wolfSSL | 15:117db924cf7c | 5482 | break; |
wolfSSL | 15:117db924cf7c | 5483 | } |
wolfSSL | 15:117db924cf7c | 5484 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 5485 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 5486 | case ED25519k: |
wolfSSL | 15:117db924cf7c | 5487 | { |
wolfSSL | 15:117db924cf7c | 5488 | ret = wc_ed25519_verify_msg(sig, sigSz, buf, bufSz, |
wolfSSL | 15:117db924cf7c | 5489 | &sigCtx->verify, sigCtx->key.ed25519); |
wolfSSL | 15:117db924cf7c | 5490 | break; |
wolfSSL | 15:117db924cf7c | 5491 | } |
wolfSSL | 15:117db924cf7c | 5492 | #endif |
wolfSSL | 15:117db924cf7c | 5493 | default: |
wolfSSL | 15:117db924cf7c | 5494 | break; |
wolfSSL | 15:117db924cf7c | 5495 | } /* switch (keyOID) */ |
wolfSSL | 15:117db924cf7c | 5496 | |
wolfSSL | 15:117db924cf7c | 5497 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 5498 | /* treat all non async RSA errors as ASN_SIG_CONFIRM_E */ |
wolfSSL | 15:117db924cf7c | 5499 | if (ret != WC_PENDING_E) |
wolfSSL | 15:117db924cf7c | 5500 | ret = ASN_SIG_CONFIRM_E; |
wolfSSL | 15:117db924cf7c | 5501 | goto exit_cs; |
wolfSSL | 15:117db924cf7c | 5502 | } |
wolfSSL | 15:117db924cf7c | 5503 | |
wolfSSL | 15:117db924cf7c | 5504 | sigCtx->state = SIG_STATE_CHECK; |
wolfSSL | 15:117db924cf7c | 5505 | } /* SIG_STATE_DO */ |
wolfSSL | 15:117db924cf7c | 5506 | FALL_THROUGH; |
wolfSSL | 15:117db924cf7c | 5507 | |
wolfSSL | 15:117db924cf7c | 5508 | case SIG_STATE_CHECK: |
wolfSSL | 15:117db924cf7c | 5509 | { |
wolfSSL | 15:117db924cf7c | 5510 | switch (keyOID) { |
wolfSSL | 15:117db924cf7c | 5511 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 5512 | case RSAk: |
wolfSSL | 15:117db924cf7c | 5513 | { |
wolfSSL | 15:117db924cf7c | 5514 | int encodedSigSz, verifySz; |
wolfSSL | 15:117db924cf7c | 5515 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 5516 | byte* encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, |
wolfSSL | 15:117db924cf7c | 5517 | sigCtx->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 5518 | if (encodedSig == NULL) { |
wolfSSL | 15:117db924cf7c | 5519 | ERROR_OUT(MEMORY_E, exit_cs); |
wolfSSL | 15:117db924cf7c | 5520 | } |
wolfSSL | 15:117db924cf7c | 5521 | #else |
wolfSSL | 15:117db924cf7c | 5522 | byte encodedSig[MAX_ENCODED_SIG_SZ]; |
wolfSSL | 15:117db924cf7c | 5523 | #endif |
wolfSSL | 15:117db924cf7c | 5524 | |
wolfSSL | 15:117db924cf7c | 5525 | verifySz = ret; |
wolfSSL | 15:117db924cf7c | 5526 | |
wolfSSL | 15:117db924cf7c | 5527 | /* make sure we're right justified */ |
wolfSSL | 15:117db924cf7c | 5528 | encodedSigSz = wc_EncodeSignature(encodedSig, |
wolfSSL | 15:117db924cf7c | 5529 | sigCtx->digest, sigCtx->digestSz, sigCtx->typeH); |
wolfSSL | 15:117db924cf7c | 5530 | if (encodedSigSz == verifySz && sigCtx->out != NULL && |
wolfSSL | 15:117db924cf7c | 5531 | XMEMCMP(sigCtx->out, encodedSig, encodedSigSz) == 0) { |
wolfSSL | 15:117db924cf7c | 5532 | ret = 0; |
wolfSSL | 15:117db924cf7c | 5533 | } |
wolfSSL | 15:117db924cf7c | 5534 | else { |
wolfSSL | 15:117db924cf7c | 5535 | WOLFSSL_MSG("RSA SSL verify match encode error"); |
wolfSSL | 15:117db924cf7c | 5536 | ret = ASN_SIG_CONFIRM_E; |
wolfSSL | 15:117db924cf7c | 5537 | } |
wolfSSL | 15:117db924cf7c | 5538 | |
wolfSSL | 15:117db924cf7c | 5539 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 5540 | XFREE(encodedSig, sigCtx->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 5541 | #endif |
wolfSSL | 15:117db924cf7c | 5542 | break; |
wolfSSL | 15:117db924cf7c | 5543 | } |
wolfSSL | 15:117db924cf7c | 5544 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 5545 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 5546 | case ECDSAk: |
wolfSSL | 15:117db924cf7c | 5547 | { |
wolfSSL | 15:117db924cf7c | 5548 | if (sigCtx->verify == 1) { |
wolfSSL | 15:117db924cf7c | 5549 | ret = 0; |
wolfSSL | 15:117db924cf7c | 5550 | } |
wolfSSL | 15:117db924cf7c | 5551 | else { |
wolfSSL | 15:117db924cf7c | 5552 | WOLFSSL_MSG("ECC Verify didn't match"); |
wolfSSL | 15:117db924cf7c | 5553 | ret = ASN_SIG_CONFIRM_E; |
wolfSSL | 15:117db924cf7c | 5554 | } |
wolfSSL | 15:117db924cf7c | 5555 | break; |
wolfSSL | 15:117db924cf7c | 5556 | } |
wolfSSL | 15:117db924cf7c | 5557 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 5558 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 5559 | case ED25519k: |
wolfSSL | 15:117db924cf7c | 5560 | { |
wolfSSL | 15:117db924cf7c | 5561 | if (sigCtx->verify == 1) { |
wolfSSL | 15:117db924cf7c | 5562 | ret = 0; |
wolfSSL | 15:117db924cf7c | 5563 | } |
wolfSSL | 15:117db924cf7c | 5564 | else { |
wolfSSL | 15:117db924cf7c | 5565 | WOLFSSL_MSG("ED25519 Verify didn't match"); |
wolfSSL | 15:117db924cf7c | 5566 | ret = ASN_SIG_CONFIRM_E; |
wolfSSL | 15:117db924cf7c | 5567 | } |
wolfSSL | 15:117db924cf7c | 5568 | break; |
wolfSSL | 15:117db924cf7c | 5569 | } |
wolfSSL | 15:117db924cf7c | 5570 | #endif /* HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 5571 | default: |
wolfSSL | 15:117db924cf7c | 5572 | break; |
wolfSSL | 15:117db924cf7c | 5573 | } /* switch (keyOID) */ |
wolfSSL | 15:117db924cf7c | 5574 | |
wolfSSL | 15:117db924cf7c | 5575 | break; |
wolfSSL | 15:117db924cf7c | 5576 | } /* SIG_STATE_CHECK */ |
wolfSSL | 15:117db924cf7c | 5577 | } /* switch (sigCtx->state) */ |
wolfSSL | 15:117db924cf7c | 5578 | |
wolfSSL | 15:117db924cf7c | 5579 | exit_cs: |
wolfSSL | 15:117db924cf7c | 5580 | |
wolfSSL | 15:117db924cf7c | 5581 | WOLFSSL_LEAVE("ConfirmSignature", ret); |
wolfSSL | 15:117db924cf7c | 5582 | |
wolfSSL | 15:117db924cf7c | 5583 | if (ret != WC_PENDING_E) { |
wolfSSL | 15:117db924cf7c | 5584 | FreeSignatureCtx(sigCtx); |
wolfSSL | 15:117db924cf7c | 5585 | } |
wolfSSL | 15:117db924cf7c | 5586 | |
wolfSSL | 15:117db924cf7c | 5587 | return ret; |
wolfSSL | 15:117db924cf7c | 5588 | } |
wolfSSL | 15:117db924cf7c | 5589 | |
wolfSSL | 15:117db924cf7c | 5590 | |
wolfSSL | 15:117db924cf7c | 5591 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 5592 | |
wolfSSL | 15:117db924cf7c | 5593 | static int MatchBaseName(int type, const char* name, int nameSz, |
wolfSSL | 15:117db924cf7c | 5594 | const char* base, int baseSz) |
wolfSSL | 15:117db924cf7c | 5595 | { |
wolfSSL | 15:117db924cf7c | 5596 | if (base == NULL || baseSz <= 0 || name == NULL || nameSz <= 0 || |
wolfSSL | 15:117db924cf7c | 5597 | name[0] == '.' || nameSz < baseSz || |
wolfSSL | 15:117db924cf7c | 5598 | (type != ASN_RFC822_TYPE && type != ASN_DNS_TYPE)) |
wolfSSL | 15:117db924cf7c | 5599 | return 0; |
wolfSSL | 15:117db924cf7c | 5600 | |
wolfSSL | 15:117db924cf7c | 5601 | /* If an email type, handle special cases where the base is only |
wolfSSL | 15:117db924cf7c | 5602 | * a domain, or is an email address itself. */ |
wolfSSL | 15:117db924cf7c | 5603 | if (type == ASN_RFC822_TYPE) { |
wolfSSL | 15:117db924cf7c | 5604 | const char* p = NULL; |
wolfSSL | 15:117db924cf7c | 5605 | int count = 0; |
wolfSSL | 15:117db924cf7c | 5606 | |
wolfSSL | 15:117db924cf7c | 5607 | if (base[0] != '.') { |
wolfSSL | 15:117db924cf7c | 5608 | p = base; |
wolfSSL | 15:117db924cf7c | 5609 | count = 0; |
wolfSSL | 15:117db924cf7c | 5610 | |
wolfSSL | 15:117db924cf7c | 5611 | /* find the '@' in the base */ |
wolfSSL | 15:117db924cf7c | 5612 | while (*p != '@' && count < baseSz) { |
wolfSSL | 15:117db924cf7c | 5613 | count++; |
wolfSSL | 15:117db924cf7c | 5614 | p++; |
wolfSSL | 15:117db924cf7c | 5615 | } |
wolfSSL | 15:117db924cf7c | 5616 | |
wolfSSL | 15:117db924cf7c | 5617 | /* No '@' in base, reset p to NULL */ |
wolfSSL | 15:117db924cf7c | 5618 | if (count >= baseSz) |
wolfSSL | 15:117db924cf7c | 5619 | p = NULL; |
wolfSSL | 15:117db924cf7c | 5620 | } |
wolfSSL | 15:117db924cf7c | 5621 | |
wolfSSL | 15:117db924cf7c | 5622 | if (p == NULL) { |
wolfSSL | 15:117db924cf7c | 5623 | /* Base isn't an email address, it is a domain name, |
wolfSSL | 15:117db924cf7c | 5624 | * wind the name forward one character past its '@'. */ |
wolfSSL | 15:117db924cf7c | 5625 | p = name; |
wolfSSL | 15:117db924cf7c | 5626 | count = 0; |
wolfSSL | 15:117db924cf7c | 5627 | while (*p != '@' && count < baseSz) { |
wolfSSL | 15:117db924cf7c | 5628 | count++; |
wolfSSL | 15:117db924cf7c | 5629 | p++; |
wolfSSL | 15:117db924cf7c | 5630 | } |
wolfSSL | 15:117db924cf7c | 5631 | |
wolfSSL | 15:117db924cf7c | 5632 | if (count < baseSz && *p == '@') { |
wolfSSL | 15:117db924cf7c | 5633 | name = p + 1; |
wolfSSL | 15:117db924cf7c | 5634 | nameSz -= count + 1; |
wolfSSL | 15:117db924cf7c | 5635 | } |
wolfSSL | 15:117db924cf7c | 5636 | } |
wolfSSL | 15:117db924cf7c | 5637 | } |
wolfSSL | 15:117db924cf7c | 5638 | |
wolfSSL | 15:117db924cf7c | 5639 | if ((type == ASN_DNS_TYPE || type == ASN_RFC822_TYPE) && base[0] == '.') { |
wolfSSL | 15:117db924cf7c | 5640 | int szAdjust = nameSz - baseSz; |
wolfSSL | 15:117db924cf7c | 5641 | name += szAdjust; |
wolfSSL | 15:117db924cf7c | 5642 | nameSz -= szAdjust; |
wolfSSL | 15:117db924cf7c | 5643 | } |
wolfSSL | 15:117db924cf7c | 5644 | |
wolfSSL | 15:117db924cf7c | 5645 | while (nameSz > 0) { |
wolfSSL | 15:117db924cf7c | 5646 | if (XTOLOWER((unsigned char)*name++) != |
wolfSSL | 15:117db924cf7c | 5647 | XTOLOWER((unsigned char)*base++)) |
wolfSSL | 15:117db924cf7c | 5648 | return 0; |
wolfSSL | 15:117db924cf7c | 5649 | nameSz--; |
wolfSSL | 15:117db924cf7c | 5650 | } |
wolfSSL | 15:117db924cf7c | 5651 | |
wolfSSL | 15:117db924cf7c | 5652 | return 1; |
wolfSSL | 15:117db924cf7c | 5653 | } |
wolfSSL | 15:117db924cf7c | 5654 | |
wolfSSL | 15:117db924cf7c | 5655 | |
wolfSSL | 15:117db924cf7c | 5656 | static int ConfirmNameConstraints(Signer* signer, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 5657 | { |
wolfSSL | 15:117db924cf7c | 5658 | if (signer == NULL || cert == NULL) |
wolfSSL | 15:117db924cf7c | 5659 | return 0; |
wolfSSL | 15:117db924cf7c | 5660 | |
wolfSSL | 15:117db924cf7c | 5661 | /* Check against the excluded list */ |
wolfSSL | 15:117db924cf7c | 5662 | if (signer->excludedNames) { |
wolfSSL | 15:117db924cf7c | 5663 | Base_entry* base = signer->excludedNames; |
wolfSSL | 15:117db924cf7c | 5664 | |
wolfSSL | 15:117db924cf7c | 5665 | while (base != NULL) { |
wolfSSL | 15:117db924cf7c | 5666 | switch (base->type) { |
wolfSSL | 15:117db924cf7c | 5667 | case ASN_DNS_TYPE: |
wolfSSL | 15:117db924cf7c | 5668 | { |
wolfSSL | 15:117db924cf7c | 5669 | DNS_entry* name = cert->altNames; |
wolfSSL | 15:117db924cf7c | 5670 | while (name != NULL) { |
wolfSSL | 15:117db924cf7c | 5671 | if (MatchBaseName(ASN_DNS_TYPE, |
wolfSSL | 15:117db924cf7c | 5672 | name->name, name->len, |
wolfSSL | 15:117db924cf7c | 5673 | base->name, base->nameSz)) { |
wolfSSL | 15:117db924cf7c | 5674 | return 0; |
wolfSSL | 15:117db924cf7c | 5675 | } |
wolfSSL | 15:117db924cf7c | 5676 | name = name->next; |
wolfSSL | 15:117db924cf7c | 5677 | } |
wolfSSL | 15:117db924cf7c | 5678 | break; |
wolfSSL | 15:117db924cf7c | 5679 | } |
wolfSSL | 15:117db924cf7c | 5680 | case ASN_RFC822_TYPE: |
wolfSSL | 15:117db924cf7c | 5681 | { |
wolfSSL | 15:117db924cf7c | 5682 | DNS_entry* name = cert->altEmailNames; |
wolfSSL | 15:117db924cf7c | 5683 | while (name != NULL) { |
wolfSSL | 15:117db924cf7c | 5684 | if (MatchBaseName(ASN_RFC822_TYPE, |
wolfSSL | 15:117db924cf7c | 5685 | name->name, name->len, |
wolfSSL | 15:117db924cf7c | 5686 | base->name, base->nameSz)) { |
wolfSSL | 15:117db924cf7c | 5687 | return 0; |
wolfSSL | 15:117db924cf7c | 5688 | } |
wolfSSL | 15:117db924cf7c | 5689 | name = name->next; |
wolfSSL | 15:117db924cf7c | 5690 | } |
wolfSSL | 15:117db924cf7c | 5691 | break; |
wolfSSL | 15:117db924cf7c | 5692 | } |
wolfSSL | 15:117db924cf7c | 5693 | case ASN_DIR_TYPE: |
wolfSSL | 15:117db924cf7c | 5694 | { |
wolfSSL | 15:117db924cf7c | 5695 | /* allow permitted dirName smaller than actual subject */ |
wolfSSL | 15:117db924cf7c | 5696 | if (cert->subjectRawLen >= base->nameSz && |
wolfSSL | 15:117db924cf7c | 5697 | XMEMCMP(cert->subjectRaw, base->name, |
wolfSSL | 15:117db924cf7c | 5698 | base->nameSz) == 0) { |
wolfSSL | 15:117db924cf7c | 5699 | return 0; |
wolfSSL | 15:117db924cf7c | 5700 | } |
wolfSSL | 15:117db924cf7c | 5701 | break; |
wolfSSL | 15:117db924cf7c | 5702 | } |
wolfSSL | 15:117db924cf7c | 5703 | }; /* switch */ |
wolfSSL | 15:117db924cf7c | 5704 | base = base->next; |
wolfSSL | 15:117db924cf7c | 5705 | } |
wolfSSL | 15:117db924cf7c | 5706 | } |
wolfSSL | 15:117db924cf7c | 5707 | |
wolfSSL | 15:117db924cf7c | 5708 | /* Check against the permitted list */ |
wolfSSL | 15:117db924cf7c | 5709 | if (signer->permittedNames != NULL) { |
wolfSSL | 15:117db924cf7c | 5710 | int needDns = 0; |
wolfSSL | 15:117db924cf7c | 5711 | int matchDns = 0; |
wolfSSL | 15:117db924cf7c | 5712 | int needEmail = 0; |
wolfSSL | 15:117db924cf7c | 5713 | int matchEmail = 0; |
wolfSSL | 15:117db924cf7c | 5714 | int needDir = 0; |
wolfSSL | 15:117db924cf7c | 5715 | int matchDir = 0; |
wolfSSL | 15:117db924cf7c | 5716 | Base_entry* base = signer->permittedNames; |
wolfSSL | 15:117db924cf7c | 5717 | |
wolfSSL | 15:117db924cf7c | 5718 | while (base != NULL) { |
wolfSSL | 15:117db924cf7c | 5719 | switch (base->type) { |
wolfSSL | 15:117db924cf7c | 5720 | case ASN_DNS_TYPE: |
wolfSSL | 15:117db924cf7c | 5721 | { |
wolfSSL | 15:117db924cf7c | 5722 | DNS_entry* name = cert->altNames; |
wolfSSL | 15:117db924cf7c | 5723 | |
wolfSSL | 15:117db924cf7c | 5724 | if (name != NULL) |
wolfSSL | 15:117db924cf7c | 5725 | needDns = 1; |
wolfSSL | 15:117db924cf7c | 5726 | |
wolfSSL | 15:117db924cf7c | 5727 | while (name != NULL) { |
wolfSSL | 15:117db924cf7c | 5728 | matchDns = MatchBaseName(ASN_DNS_TYPE, |
wolfSSL | 15:117db924cf7c | 5729 | name->name, name->len, |
wolfSSL | 15:117db924cf7c | 5730 | base->name, base->nameSz); |
wolfSSL | 15:117db924cf7c | 5731 | name = name->next; |
wolfSSL | 15:117db924cf7c | 5732 | } |
wolfSSL | 15:117db924cf7c | 5733 | break; |
wolfSSL | 15:117db924cf7c | 5734 | } |
wolfSSL | 15:117db924cf7c | 5735 | case ASN_RFC822_TYPE: |
wolfSSL | 15:117db924cf7c | 5736 | { |
wolfSSL | 15:117db924cf7c | 5737 | DNS_entry* name = cert->altEmailNames; |
wolfSSL | 15:117db924cf7c | 5738 | |
wolfSSL | 15:117db924cf7c | 5739 | if (name != NULL) |
wolfSSL | 15:117db924cf7c | 5740 | needEmail = 1; |
wolfSSL | 15:117db924cf7c | 5741 | |
wolfSSL | 15:117db924cf7c | 5742 | while (name != NULL) { |
wolfSSL | 15:117db924cf7c | 5743 | matchEmail = MatchBaseName(ASN_DNS_TYPE, |
wolfSSL | 15:117db924cf7c | 5744 | name->name, name->len, |
wolfSSL | 15:117db924cf7c | 5745 | base->name, base->nameSz); |
wolfSSL | 15:117db924cf7c | 5746 | name = name->next; |
wolfSSL | 15:117db924cf7c | 5747 | } |
wolfSSL | 15:117db924cf7c | 5748 | break; |
wolfSSL | 15:117db924cf7c | 5749 | } |
wolfSSL | 15:117db924cf7c | 5750 | case ASN_DIR_TYPE: |
wolfSSL | 15:117db924cf7c | 5751 | { |
wolfSSL | 15:117db924cf7c | 5752 | /* allow permitted dirName smaller than actual subject */ |
wolfSSL | 15:117db924cf7c | 5753 | needDir = 1; |
wolfSSL | 15:117db924cf7c | 5754 | if (cert->subjectRaw != NULL && |
wolfSSL | 15:117db924cf7c | 5755 | cert->subjectRawLen >= base->nameSz && |
wolfSSL | 15:117db924cf7c | 5756 | XMEMCMP(cert->subjectRaw, base->name, |
wolfSSL | 15:117db924cf7c | 5757 | base->nameSz) == 0) { |
wolfSSL | 15:117db924cf7c | 5758 | matchDir = 1; |
wolfSSL | 15:117db924cf7c | 5759 | } |
wolfSSL | 15:117db924cf7c | 5760 | break; |
wolfSSL | 15:117db924cf7c | 5761 | } |
wolfSSL | 15:117db924cf7c | 5762 | } /* switch */ |
wolfSSL | 15:117db924cf7c | 5763 | base = base->next; |
wolfSSL | 15:117db924cf7c | 5764 | } |
wolfSSL | 15:117db924cf7c | 5765 | |
wolfSSL | 15:117db924cf7c | 5766 | if ((needDns && !matchDns) || |
wolfSSL | 15:117db924cf7c | 5767 | (needEmail && !matchEmail) || |
wolfSSL | 15:117db924cf7c | 5768 | (needDir && !matchDir)) { |
wolfSSL | 15:117db924cf7c | 5769 | return 0; |
wolfSSL | 15:117db924cf7c | 5770 | } |
wolfSSL | 15:117db924cf7c | 5771 | } |
wolfSSL | 15:117db924cf7c | 5772 | |
wolfSSL | 15:117db924cf7c | 5773 | return 1; |
wolfSSL | 15:117db924cf7c | 5774 | } |
wolfSSL | 15:117db924cf7c | 5775 | |
wolfSSL | 15:117db924cf7c | 5776 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 5777 | |
wolfSSL | 15:117db924cf7c | 5778 | static int DecodeAltNames(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 5779 | { |
wolfSSL | 15:117db924cf7c | 5780 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 5781 | int length = 0; |
wolfSSL | 15:117db924cf7c | 5782 | |
wolfSSL | 15:117db924cf7c | 5783 | WOLFSSL_ENTER("DecodeAltNames"); |
wolfSSL | 15:117db924cf7c | 5784 | |
wolfSSL | 15:117db924cf7c | 5785 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 5786 | WOLFSSL_MSG("\tBad Sequence"); |
wolfSSL | 15:117db924cf7c | 5787 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5788 | } |
wolfSSL | 15:117db924cf7c | 5789 | |
wolfSSL | 15:117db924cf7c | 5790 | cert->weOwnAltNames = 1; |
wolfSSL | 15:117db924cf7c | 5791 | |
wolfSSL | 15:117db924cf7c | 5792 | while (length > 0) { |
wolfSSL | 15:117db924cf7c | 5793 | byte b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 5794 | |
wolfSSL | 15:117db924cf7c | 5795 | length--; |
wolfSSL | 15:117db924cf7c | 5796 | |
wolfSSL | 15:117db924cf7c | 5797 | /* Save DNS Type names in the altNames list. */ |
wolfSSL | 15:117db924cf7c | 5798 | /* Save Other Type names in the cert's OidMap */ |
wolfSSL | 15:117db924cf7c | 5799 | if (b == (ASN_CONTEXT_SPECIFIC | ASN_DNS_TYPE)) { |
wolfSSL | 15:117db924cf7c | 5800 | DNS_entry* dnsEntry; |
wolfSSL | 15:117db924cf7c | 5801 | int strLen; |
wolfSSL | 15:117db924cf7c | 5802 | word32 lenStartIdx = idx; |
wolfSSL | 15:117db924cf7c | 5803 | |
wolfSSL | 15:117db924cf7c | 5804 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 5805 | WOLFSSL_MSG("\tfail: str length"); |
wolfSSL | 15:117db924cf7c | 5806 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5807 | } |
wolfSSL | 15:117db924cf7c | 5808 | length -= (idx - lenStartIdx); |
wolfSSL | 15:117db924cf7c | 5809 | |
wolfSSL | 15:117db924cf7c | 5810 | dnsEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap, |
wolfSSL | 15:117db924cf7c | 5811 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5812 | if (dnsEntry == NULL) { |
wolfSSL | 15:117db924cf7c | 5813 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 5814 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 5815 | } |
wolfSSL | 15:117db924cf7c | 5816 | |
wolfSSL | 15:117db924cf7c | 5817 | dnsEntry->type = ASN_DNS_TYPE; |
wolfSSL | 15:117db924cf7c | 5818 | dnsEntry->name = (char*)XMALLOC(strLen + 1, cert->heap, |
wolfSSL | 15:117db924cf7c | 5819 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5820 | if (dnsEntry->name == NULL) { |
wolfSSL | 15:117db924cf7c | 5821 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 5822 | XFREE(dnsEntry, cert->heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5823 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 5824 | } |
wolfSSL | 15:117db924cf7c | 5825 | dnsEntry->len = strLen; |
wolfSSL | 15:117db924cf7c | 5826 | XMEMCPY(dnsEntry->name, &input[idx], strLen); |
wolfSSL | 15:117db924cf7c | 5827 | dnsEntry->name[strLen] = '\0'; |
wolfSSL | 15:117db924cf7c | 5828 | |
wolfSSL | 15:117db924cf7c | 5829 | dnsEntry->next = cert->altNames; |
wolfSSL | 15:117db924cf7c | 5830 | cert->altNames = dnsEntry; |
wolfSSL | 15:117db924cf7c | 5831 | |
wolfSSL | 15:117db924cf7c | 5832 | length -= strLen; |
wolfSSL | 15:117db924cf7c | 5833 | idx += strLen; |
wolfSSL | 15:117db924cf7c | 5834 | } |
wolfSSL | 15:117db924cf7c | 5835 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 5836 | else if (b == (ASN_CONTEXT_SPECIFIC | ASN_RFC822_TYPE)) { |
wolfSSL | 15:117db924cf7c | 5837 | DNS_entry* emailEntry; |
wolfSSL | 15:117db924cf7c | 5838 | int strLen; |
wolfSSL | 15:117db924cf7c | 5839 | word32 lenStartIdx = idx; |
wolfSSL | 15:117db924cf7c | 5840 | |
wolfSSL | 15:117db924cf7c | 5841 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 5842 | WOLFSSL_MSG("\tfail: str length"); |
wolfSSL | 15:117db924cf7c | 5843 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5844 | } |
wolfSSL | 15:117db924cf7c | 5845 | length -= (idx - lenStartIdx); |
wolfSSL | 15:117db924cf7c | 5846 | |
wolfSSL | 15:117db924cf7c | 5847 | emailEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap, |
wolfSSL | 15:117db924cf7c | 5848 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5849 | if (emailEntry == NULL) { |
wolfSSL | 15:117db924cf7c | 5850 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 5851 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 5852 | } |
wolfSSL | 15:117db924cf7c | 5853 | |
wolfSSL | 15:117db924cf7c | 5854 | emailEntry->type = ASN_RFC822_TYPE; |
wolfSSL | 15:117db924cf7c | 5855 | emailEntry->name = (char*)XMALLOC(strLen + 1, cert->heap, |
wolfSSL | 15:117db924cf7c | 5856 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5857 | if (emailEntry->name == NULL) { |
wolfSSL | 15:117db924cf7c | 5858 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 5859 | XFREE(emailEntry, cert->heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5860 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 5861 | } |
wolfSSL | 15:117db924cf7c | 5862 | emailEntry->len = strLen; |
wolfSSL | 15:117db924cf7c | 5863 | XMEMCPY(emailEntry->name, &input[idx], strLen); |
wolfSSL | 15:117db924cf7c | 5864 | emailEntry->name[strLen] = '\0'; |
wolfSSL | 15:117db924cf7c | 5865 | |
wolfSSL | 15:117db924cf7c | 5866 | emailEntry->next = cert->altEmailNames; |
wolfSSL | 15:117db924cf7c | 5867 | cert->altEmailNames = emailEntry; |
wolfSSL | 15:117db924cf7c | 5868 | |
wolfSSL | 15:117db924cf7c | 5869 | length -= strLen; |
wolfSSL | 15:117db924cf7c | 5870 | idx += strLen; |
wolfSSL | 15:117db924cf7c | 5871 | } |
wolfSSL | 15:117db924cf7c | 5872 | else if (b == (ASN_CONTEXT_SPECIFIC | ASN_URI_TYPE)) { |
wolfSSL | 15:117db924cf7c | 5873 | DNS_entry* uriEntry; |
wolfSSL | 15:117db924cf7c | 5874 | int strLen; |
wolfSSL | 15:117db924cf7c | 5875 | word32 lenStartIdx = idx; |
wolfSSL | 15:117db924cf7c | 5876 | |
wolfSSL | 15:117db924cf7c | 5877 | WOLFSSL_MSG("\tPutting URI into list but not using"); |
wolfSSL | 15:117db924cf7c | 5878 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 5879 | WOLFSSL_MSG("\tfail: str length"); |
wolfSSL | 15:117db924cf7c | 5880 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5881 | } |
wolfSSL | 15:117db924cf7c | 5882 | length -= (idx - lenStartIdx); |
wolfSSL | 15:117db924cf7c | 5883 | |
wolfSSL | 15:117db924cf7c | 5884 | /* check that strLen at index is not past input buffer */ |
wolfSSL | 15:117db924cf7c | 5885 | if (strLen + (int)idx > sz) { |
wolfSSL | 15:117db924cf7c | 5886 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 5887 | } |
wolfSSL | 15:117db924cf7c | 5888 | |
wolfSSL | 15:117db924cf7c | 5889 | #ifndef WOLFSSL_NO_ASN_STRICT |
wolfSSL | 15:117db924cf7c | 5890 | /* Verify RFC 5280 Sec 4.2.1.6 rule: |
wolfSSL | 15:117db924cf7c | 5891 | "The name MUST NOT be a relative URI" */ |
wolfSSL | 15:117db924cf7c | 5892 | |
wolfSSL | 15:117db924cf7c | 5893 | { |
wolfSSL | 15:117db924cf7c | 5894 | int i; |
wolfSSL | 15:117db924cf7c | 5895 | |
wolfSSL | 15:117db924cf7c | 5896 | /* skip past scheme (i.e http,ftp,...) finding first ':' char */ |
wolfSSL | 15:117db924cf7c | 5897 | for (i = 0; i < strLen; i++) { |
wolfSSL | 15:117db924cf7c | 5898 | if (input[idx + i] == ':') { |
wolfSSL | 15:117db924cf7c | 5899 | break; |
wolfSSL | 15:117db924cf7c | 5900 | } |
wolfSSL | 15:117db924cf7c | 5901 | if (input[idx + i] == '/') { |
wolfSSL | 15:117db924cf7c | 5902 | i = strLen; /* error, found relative path since '/' was |
wolfSSL | 15:117db924cf7c | 5903 | * encountered before ':'. Returning error |
wolfSSL | 15:117db924cf7c | 5904 | * value in next if statement. */ |
wolfSSL | 15:117db924cf7c | 5905 | } |
wolfSSL | 15:117db924cf7c | 5906 | } |
wolfSSL | 15:117db924cf7c | 5907 | |
wolfSSL | 15:117db924cf7c | 5908 | /* test if no ':' char was found and test that the next two |
wolfSSL | 15:117db924cf7c | 5909 | * chars are // to match the pattern "://" */ |
wolfSSL | 15:117db924cf7c | 5910 | if (i >= strLen - 2 || (input[idx + i + 1] != '/' || |
wolfSSL | 15:117db924cf7c | 5911 | input[idx + i + 2] != '/')) { |
wolfSSL | 15:117db924cf7c | 5912 | WOLFSSL_MSG("\tAlt Name must be absolute URI"); |
wolfSSL | 15:117db924cf7c | 5913 | return ASN_ALT_NAME_E; |
wolfSSL | 15:117db924cf7c | 5914 | } |
wolfSSL | 15:117db924cf7c | 5915 | } |
wolfSSL | 15:117db924cf7c | 5916 | #endif |
wolfSSL | 15:117db924cf7c | 5917 | |
wolfSSL | 15:117db924cf7c | 5918 | uriEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap, |
wolfSSL | 15:117db924cf7c | 5919 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5920 | if (uriEntry == NULL) { |
wolfSSL | 15:117db924cf7c | 5921 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 5922 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 5923 | } |
wolfSSL | 15:117db924cf7c | 5924 | |
wolfSSL | 15:117db924cf7c | 5925 | uriEntry->type = ASN_URI_TYPE; |
wolfSSL | 15:117db924cf7c | 5926 | uriEntry->name = (char*)XMALLOC(strLen + 1, cert->heap, |
wolfSSL | 15:117db924cf7c | 5927 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5928 | if (uriEntry->name == NULL) { |
wolfSSL | 15:117db924cf7c | 5929 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 5930 | XFREE(uriEntry, cert->heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 5931 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 5932 | } |
wolfSSL | 15:117db924cf7c | 5933 | uriEntry->len = strLen; |
wolfSSL | 15:117db924cf7c | 5934 | XMEMCPY(uriEntry->name, &input[idx], strLen); |
wolfSSL | 15:117db924cf7c | 5935 | uriEntry->name[strLen] = '\0'; |
wolfSSL | 15:117db924cf7c | 5936 | |
wolfSSL | 15:117db924cf7c | 5937 | uriEntry->next = cert->altNames; |
wolfSSL | 15:117db924cf7c | 5938 | cert->altNames = uriEntry; |
wolfSSL | 15:117db924cf7c | 5939 | |
wolfSSL | 15:117db924cf7c | 5940 | length -= strLen; |
wolfSSL | 15:117db924cf7c | 5941 | idx += strLen; |
wolfSSL | 15:117db924cf7c | 5942 | } |
wolfSSL | 15:117db924cf7c | 5943 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 5944 | #ifdef WOLFSSL_SEP |
wolfSSL | 15:117db924cf7c | 5945 | else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_OTHER_TYPE)) |
wolfSSL | 15:117db924cf7c | 5946 | { |
wolfSSL | 15:117db924cf7c | 5947 | int strLen; |
wolfSSL | 15:117db924cf7c | 5948 | word32 lenStartIdx = idx; |
wolfSSL | 15:117db924cf7c | 5949 | word32 oid = 0; |
wolfSSL | 15:117db924cf7c | 5950 | int ret; |
wolfSSL | 15:117db924cf7c | 5951 | |
wolfSSL | 15:117db924cf7c | 5952 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 5953 | WOLFSSL_MSG("\tfail: other name length"); |
wolfSSL | 15:117db924cf7c | 5954 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5955 | } |
wolfSSL | 15:117db924cf7c | 5956 | /* Consume the rest of this sequence. */ |
wolfSSL | 15:117db924cf7c | 5957 | length -= (strLen + idx - lenStartIdx); |
wolfSSL | 15:117db924cf7c | 5958 | |
wolfSSL | 15:117db924cf7c | 5959 | if (GetObjectId(input, &idx, &oid, oidCertAltNameType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 5960 | WOLFSSL_MSG("\tbad OID"); |
wolfSSL | 15:117db924cf7c | 5961 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5962 | } |
wolfSSL | 15:117db924cf7c | 5963 | |
wolfSSL | 15:117db924cf7c | 5964 | if (oid != HW_NAME_OID) { |
wolfSSL | 15:117db924cf7c | 5965 | WOLFSSL_MSG("\tincorrect OID"); |
wolfSSL | 15:117db924cf7c | 5966 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5967 | } |
wolfSSL | 15:117db924cf7c | 5968 | |
wolfSSL | 15:117db924cf7c | 5969 | if (input[idx++] != (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) { |
wolfSSL | 15:117db924cf7c | 5970 | WOLFSSL_MSG("\twrong type"); |
wolfSSL | 15:117db924cf7c | 5971 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5972 | } |
wolfSSL | 15:117db924cf7c | 5973 | |
wolfSSL | 15:117db924cf7c | 5974 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 5975 | WOLFSSL_MSG("\tfail: str len"); |
wolfSSL | 15:117db924cf7c | 5976 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5977 | } |
wolfSSL | 15:117db924cf7c | 5978 | |
wolfSSL | 15:117db924cf7c | 5979 | if (GetSequence(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 5980 | WOLFSSL_MSG("\tBad Sequence"); |
wolfSSL | 15:117db924cf7c | 5981 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 5982 | } |
wolfSSL | 15:117db924cf7c | 5983 | |
wolfSSL | 15:117db924cf7c | 5984 | ret = GetASNObjectId(input, &idx, &strLen, sz); |
wolfSSL | 15:117db924cf7c | 5985 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 5986 | WOLFSSL_MSG("\tbad OID"); |
wolfSSL | 15:117db924cf7c | 5987 | return ret; |
wolfSSL | 15:117db924cf7c | 5988 | } |
wolfSSL | 15:117db924cf7c | 5989 | |
wolfSSL | 15:117db924cf7c | 5990 | cert->hwType = (byte*)XMALLOC(strLen, cert->heap, |
wolfSSL | 15:117db924cf7c | 5991 | DYNAMIC_TYPE_X509_EXT); |
wolfSSL | 15:117db924cf7c | 5992 | if (cert->hwType == NULL) { |
wolfSSL | 15:117db924cf7c | 5993 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 5994 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 5995 | } |
wolfSSL | 15:117db924cf7c | 5996 | |
wolfSSL | 15:117db924cf7c | 5997 | XMEMCPY(cert->hwType, &input[idx], strLen); |
wolfSSL | 15:117db924cf7c | 5998 | cert->hwTypeSz = strLen; |
wolfSSL | 15:117db924cf7c | 5999 | idx += strLen; |
wolfSSL | 15:117db924cf7c | 6000 | |
wolfSSL | 15:117db924cf7c | 6001 | ret = GetOctetString(input, &idx, &strLen, sz); |
wolfSSL | 15:117db924cf7c | 6002 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 6003 | return ret; |
wolfSSL | 15:117db924cf7c | 6004 | |
wolfSSL | 15:117db924cf7c | 6005 | cert->hwSerialNum = (byte*)XMALLOC(strLen + 1, cert->heap, |
wolfSSL | 15:117db924cf7c | 6006 | DYNAMIC_TYPE_X509_EXT); |
wolfSSL | 15:117db924cf7c | 6007 | if (cert->hwSerialNum == NULL) { |
wolfSSL | 15:117db924cf7c | 6008 | WOLFSSL_MSG("\tOut of Memory"); |
wolfSSL | 15:117db924cf7c | 6009 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 6010 | } |
wolfSSL | 15:117db924cf7c | 6011 | |
wolfSSL | 15:117db924cf7c | 6012 | XMEMCPY(cert->hwSerialNum, &input[idx], strLen); |
wolfSSL | 15:117db924cf7c | 6013 | cert->hwSerialNum[strLen] = '\0'; |
wolfSSL | 15:117db924cf7c | 6014 | cert->hwSerialNumSz = strLen; |
wolfSSL | 15:117db924cf7c | 6015 | idx += strLen; |
wolfSSL | 15:117db924cf7c | 6016 | } |
wolfSSL | 15:117db924cf7c | 6017 | #endif /* WOLFSSL_SEP */ |
wolfSSL | 15:117db924cf7c | 6018 | else { |
wolfSSL | 15:117db924cf7c | 6019 | int strLen; |
wolfSSL | 15:117db924cf7c | 6020 | word32 lenStartIdx = idx; |
wolfSSL | 15:117db924cf7c | 6021 | |
wolfSSL | 15:117db924cf7c | 6022 | WOLFSSL_MSG("\tUnsupported name type, skipping"); |
wolfSSL | 15:117db924cf7c | 6023 | |
wolfSSL | 15:117db924cf7c | 6024 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6025 | WOLFSSL_MSG("\tfail: unsupported name length"); |
wolfSSL | 15:117db924cf7c | 6026 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6027 | } |
wolfSSL | 15:117db924cf7c | 6028 | length -= (strLen + idx - lenStartIdx); |
wolfSSL | 15:117db924cf7c | 6029 | idx += strLen; |
wolfSSL | 15:117db924cf7c | 6030 | } |
wolfSSL | 15:117db924cf7c | 6031 | } |
wolfSSL | 15:117db924cf7c | 6032 | return 0; |
wolfSSL | 15:117db924cf7c | 6033 | } |
wolfSSL | 15:117db924cf7c | 6034 | |
wolfSSL | 15:117db924cf7c | 6035 | static int DecodeBasicCaConstraint(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6036 | { |
wolfSSL | 15:117db924cf7c | 6037 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6038 | int length = 0; |
wolfSSL | 15:117db924cf7c | 6039 | int ret; |
wolfSSL | 15:117db924cf7c | 6040 | |
wolfSSL | 15:117db924cf7c | 6041 | WOLFSSL_ENTER("DecodeBasicCaConstraint"); |
wolfSSL | 15:117db924cf7c | 6042 | |
wolfSSL | 15:117db924cf7c | 6043 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6044 | WOLFSSL_MSG("\tfail: bad SEQUENCE"); |
wolfSSL | 15:117db924cf7c | 6045 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6046 | } |
wolfSSL | 15:117db924cf7c | 6047 | |
wolfSSL | 15:117db924cf7c | 6048 | if (length == 0) |
wolfSSL | 15:117db924cf7c | 6049 | return 0; |
wolfSSL | 15:117db924cf7c | 6050 | |
wolfSSL | 15:117db924cf7c | 6051 | /* If the basic ca constraint is false, this extension may be named, but |
wolfSSL | 15:117db924cf7c | 6052 | * left empty. So, if the length is 0, just return. */ |
wolfSSL | 15:117db924cf7c | 6053 | |
wolfSSL | 15:117db924cf7c | 6054 | ret = GetBoolean(input, &idx, sz); |
wolfSSL | 15:117db924cf7c | 6055 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 6056 | WOLFSSL_MSG("\tfail: constraint not valid BOOLEAN"); |
wolfSSL | 15:117db924cf7c | 6057 | return ret; |
wolfSSL | 15:117db924cf7c | 6058 | } |
wolfSSL | 15:117db924cf7c | 6059 | |
wolfSSL | 15:117db924cf7c | 6060 | cert->isCA = (byte)ret; |
wolfSSL | 15:117db924cf7c | 6061 | |
wolfSSL | 15:117db924cf7c | 6062 | /* If there isn't any more data, return. */ |
wolfSSL | 15:117db924cf7c | 6063 | if (idx >= (word32)sz) |
wolfSSL | 15:117db924cf7c | 6064 | return 0; |
wolfSSL | 15:117db924cf7c | 6065 | |
wolfSSL | 15:117db924cf7c | 6066 | ret = GetInteger7Bit(input, &idx, sz); |
wolfSSL | 15:117db924cf7c | 6067 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 6068 | return ret; |
wolfSSL | 15:117db924cf7c | 6069 | |
wolfSSL | 15:117db924cf7c | 6070 | cert->pathLength = (byte)ret; |
wolfSSL | 15:117db924cf7c | 6071 | cert->pathLengthSet = 1; |
wolfSSL | 15:117db924cf7c | 6072 | |
wolfSSL | 15:117db924cf7c | 6073 | return 0; |
wolfSSL | 15:117db924cf7c | 6074 | } |
wolfSSL | 15:117db924cf7c | 6075 | |
wolfSSL | 15:117db924cf7c | 6076 | |
wolfSSL | 15:117db924cf7c | 6077 | #define CRLDP_FULL_NAME 0 |
wolfSSL | 15:117db924cf7c | 6078 | /* From RFC3280 SS4.2.1.14, Distribution Point Name*/ |
wolfSSL | 15:117db924cf7c | 6079 | #define GENERALNAME_URI 6 |
wolfSSL | 15:117db924cf7c | 6080 | /* From RFC3280 SS4.2.1.7, GeneralName */ |
wolfSSL | 15:117db924cf7c | 6081 | |
wolfSSL | 15:117db924cf7c | 6082 | static int DecodeCrlDist(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6083 | { |
wolfSSL | 15:117db924cf7c | 6084 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6085 | int length = 0; |
wolfSSL | 15:117db924cf7c | 6086 | |
wolfSSL | 15:117db924cf7c | 6087 | WOLFSSL_ENTER("DecodeCrlDist"); |
wolfSSL | 15:117db924cf7c | 6088 | |
wolfSSL | 15:117db924cf7c | 6089 | /* Unwrap the list of Distribution Points*/ |
wolfSSL | 15:117db924cf7c | 6090 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6091 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6092 | |
wolfSSL | 15:117db924cf7c | 6093 | /* Unwrap a single Distribution Point */ |
wolfSSL | 15:117db924cf7c | 6094 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6095 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6096 | |
wolfSSL | 15:117db924cf7c | 6097 | /* The Distribution Point has three explicit optional members |
wolfSSL | 15:117db924cf7c | 6098 | * First check for a DistributionPointName |
wolfSSL | 15:117db924cf7c | 6099 | */ |
wolfSSL | 15:117db924cf7c | 6100 | if (input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) |
wolfSSL | 15:117db924cf7c | 6101 | { |
wolfSSL | 15:117db924cf7c | 6102 | idx++; |
wolfSSL | 15:117db924cf7c | 6103 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6104 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6105 | |
wolfSSL | 15:117db924cf7c | 6106 | if (input[idx] == |
wolfSSL | 15:117db924cf7c | 6107 | (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | CRLDP_FULL_NAME)) |
wolfSSL | 15:117db924cf7c | 6108 | { |
wolfSSL | 15:117db924cf7c | 6109 | idx++; |
wolfSSL | 15:117db924cf7c | 6110 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6111 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6112 | |
wolfSSL | 15:117db924cf7c | 6113 | if (input[idx] == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI)) |
wolfSSL | 15:117db924cf7c | 6114 | { |
wolfSSL | 15:117db924cf7c | 6115 | idx++; |
wolfSSL | 15:117db924cf7c | 6116 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6117 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6118 | |
wolfSSL | 15:117db924cf7c | 6119 | cert->extCrlInfoSz = length; |
wolfSSL | 15:117db924cf7c | 6120 | cert->extCrlInfo = input + idx; |
wolfSSL | 15:117db924cf7c | 6121 | idx += length; |
wolfSSL | 15:117db924cf7c | 6122 | } |
wolfSSL | 15:117db924cf7c | 6123 | else |
wolfSSL | 15:117db924cf7c | 6124 | /* This isn't a URI, skip it. */ |
wolfSSL | 15:117db924cf7c | 6125 | idx += length; |
wolfSSL | 15:117db924cf7c | 6126 | } |
wolfSSL | 15:117db924cf7c | 6127 | else { |
wolfSSL | 15:117db924cf7c | 6128 | /* This isn't a FULLNAME, skip it. */ |
wolfSSL | 15:117db924cf7c | 6129 | idx += length; |
wolfSSL | 15:117db924cf7c | 6130 | } |
wolfSSL | 15:117db924cf7c | 6131 | } |
wolfSSL | 15:117db924cf7c | 6132 | |
wolfSSL | 15:117db924cf7c | 6133 | /* Check for reasonFlags */ |
wolfSSL | 15:117db924cf7c | 6134 | if (idx < (word32)sz && |
wolfSSL | 15:117db924cf7c | 6135 | input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1)) |
wolfSSL | 15:117db924cf7c | 6136 | { |
wolfSSL | 15:117db924cf7c | 6137 | idx++; |
wolfSSL | 15:117db924cf7c | 6138 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6139 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6140 | idx += length; |
wolfSSL | 15:117db924cf7c | 6141 | } |
wolfSSL | 15:117db924cf7c | 6142 | |
wolfSSL | 15:117db924cf7c | 6143 | /* Check for cRLIssuer */ |
wolfSSL | 15:117db924cf7c | 6144 | if (idx < (word32)sz && |
wolfSSL | 15:117db924cf7c | 6145 | input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 2)) |
wolfSSL | 15:117db924cf7c | 6146 | { |
wolfSSL | 15:117db924cf7c | 6147 | idx++; |
wolfSSL | 15:117db924cf7c | 6148 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6149 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6150 | idx += length; |
wolfSSL | 15:117db924cf7c | 6151 | } |
wolfSSL | 15:117db924cf7c | 6152 | |
wolfSSL | 15:117db924cf7c | 6153 | if (idx < (word32)sz) |
wolfSSL | 15:117db924cf7c | 6154 | { |
wolfSSL | 15:117db924cf7c | 6155 | WOLFSSL_MSG("\tThere are more CRL Distribution Point records, " |
wolfSSL | 15:117db924cf7c | 6156 | "but we only use the first one."); |
wolfSSL | 15:117db924cf7c | 6157 | } |
wolfSSL | 15:117db924cf7c | 6158 | |
wolfSSL | 15:117db924cf7c | 6159 | return 0; |
wolfSSL | 15:117db924cf7c | 6160 | } |
wolfSSL | 15:117db924cf7c | 6161 | |
wolfSSL | 15:117db924cf7c | 6162 | |
wolfSSL | 15:117db924cf7c | 6163 | static int DecodeAuthInfo(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6164 | /* |
wolfSSL | 15:117db924cf7c | 6165 | * Read the first of the Authority Information Access records. If there are |
wolfSSL | 15:117db924cf7c | 6166 | * any issues, return without saving the record. |
wolfSSL | 15:117db924cf7c | 6167 | */ |
wolfSSL | 15:117db924cf7c | 6168 | { |
wolfSSL | 15:117db924cf7c | 6169 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6170 | int length = 0; |
wolfSSL | 15:117db924cf7c | 6171 | byte b; |
wolfSSL | 15:117db924cf7c | 6172 | word32 oid; |
wolfSSL | 15:117db924cf7c | 6173 | |
wolfSSL | 15:117db924cf7c | 6174 | WOLFSSL_ENTER("DecodeAuthInfo"); |
wolfSSL | 15:117db924cf7c | 6175 | |
wolfSSL | 15:117db924cf7c | 6176 | /* Unwrap the list of AIAs */ |
wolfSSL | 15:117db924cf7c | 6177 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6178 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6179 | |
wolfSSL | 15:117db924cf7c | 6180 | while (idx < (word32)sz) { |
wolfSSL | 15:117db924cf7c | 6181 | /* Unwrap a single AIA */ |
wolfSSL | 15:117db924cf7c | 6182 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6183 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6184 | |
wolfSSL | 15:117db924cf7c | 6185 | oid = 0; |
wolfSSL | 15:117db924cf7c | 6186 | if (GetObjectId(input, &idx, &oid, oidCertAuthInfoType, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6187 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6188 | |
wolfSSL | 15:117db924cf7c | 6189 | |
wolfSSL | 15:117db924cf7c | 6190 | /* Only supporting URIs right now. */ |
wolfSSL | 15:117db924cf7c | 6191 | b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 6192 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6193 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6194 | |
wolfSSL | 15:117db924cf7c | 6195 | if (b == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI) && |
wolfSSL | 15:117db924cf7c | 6196 | oid == AIA_OCSP_OID) |
wolfSSL | 15:117db924cf7c | 6197 | { |
wolfSSL | 15:117db924cf7c | 6198 | cert->extAuthInfoSz = length; |
wolfSSL | 15:117db924cf7c | 6199 | cert->extAuthInfo = input + idx; |
wolfSSL | 15:117db924cf7c | 6200 | break; |
wolfSSL | 15:117db924cf7c | 6201 | } |
wolfSSL | 15:117db924cf7c | 6202 | idx += length; |
wolfSSL | 15:117db924cf7c | 6203 | } |
wolfSSL | 15:117db924cf7c | 6204 | |
wolfSSL | 15:117db924cf7c | 6205 | return 0; |
wolfSSL | 15:117db924cf7c | 6206 | } |
wolfSSL | 15:117db924cf7c | 6207 | |
wolfSSL | 15:117db924cf7c | 6208 | |
wolfSSL | 15:117db924cf7c | 6209 | static int DecodeAuthKeyId(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6210 | { |
wolfSSL | 15:117db924cf7c | 6211 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6212 | int length = 0, ret = 0; |
wolfSSL | 15:117db924cf7c | 6213 | |
wolfSSL | 15:117db924cf7c | 6214 | WOLFSSL_ENTER("DecodeAuthKeyId"); |
wolfSSL | 15:117db924cf7c | 6215 | |
wolfSSL | 15:117db924cf7c | 6216 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6217 | WOLFSSL_MSG("\tfail: should be a SEQUENCE\n"); |
wolfSSL | 15:117db924cf7c | 6218 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6219 | } |
wolfSSL | 15:117db924cf7c | 6220 | |
wolfSSL | 15:117db924cf7c | 6221 | if (input[idx++] != (ASN_CONTEXT_SPECIFIC | 0)) { |
wolfSSL | 15:117db924cf7c | 6222 | WOLFSSL_MSG("\tinfo: OPTIONAL item 0, not available\n"); |
wolfSSL | 15:117db924cf7c | 6223 | return 0; |
wolfSSL | 15:117db924cf7c | 6224 | } |
wolfSSL | 15:117db924cf7c | 6225 | |
wolfSSL | 15:117db924cf7c | 6226 | if (GetLength(input, &idx, &length, sz) <= 0) { |
wolfSSL | 15:117db924cf7c | 6227 | WOLFSSL_MSG("\tfail: extension data length"); |
wolfSSL | 15:117db924cf7c | 6228 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6229 | } |
wolfSSL | 15:117db924cf7c | 6230 | |
wolfSSL | 15:117db924cf7c | 6231 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6232 | cert->extAuthKeyIdSrc = &input[idx]; |
wolfSSL | 15:117db924cf7c | 6233 | cert->extAuthKeyIdSz = length; |
wolfSSL | 15:117db924cf7c | 6234 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 6235 | |
wolfSSL | 15:117db924cf7c | 6236 | if (length == KEYID_SIZE) { |
wolfSSL | 15:117db924cf7c | 6237 | XMEMCPY(cert->extAuthKeyId, input + idx, length); |
wolfSSL | 15:117db924cf7c | 6238 | } |
wolfSSL | 15:117db924cf7c | 6239 | else { |
wolfSSL | 15:117db924cf7c | 6240 | #ifdef NO_SHA |
wolfSSL | 15:117db924cf7c | 6241 | ret = wc_Sha256Hash(input + idx, length, cert->extAuthKeyId); |
wolfSSL | 15:117db924cf7c | 6242 | #else |
wolfSSL | 15:117db924cf7c | 6243 | ret = wc_ShaHash(input + idx, length, cert->extAuthKeyId); |
wolfSSL | 15:117db924cf7c | 6244 | #endif |
wolfSSL | 15:117db924cf7c | 6245 | } |
wolfSSL | 15:117db924cf7c | 6246 | |
wolfSSL | 15:117db924cf7c | 6247 | return ret; |
wolfSSL | 15:117db924cf7c | 6248 | } |
wolfSSL | 15:117db924cf7c | 6249 | |
wolfSSL | 15:117db924cf7c | 6250 | |
wolfSSL | 15:117db924cf7c | 6251 | static int DecodeSubjKeyId(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6252 | { |
wolfSSL | 15:117db924cf7c | 6253 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6254 | int length = 0, ret = 0; |
wolfSSL | 15:117db924cf7c | 6255 | |
wolfSSL | 15:117db924cf7c | 6256 | WOLFSSL_ENTER("DecodeSubjKeyId"); |
wolfSSL | 15:117db924cf7c | 6257 | |
wolfSSL | 15:117db924cf7c | 6258 | if (sz <= 0) |
wolfSSL | 15:117db924cf7c | 6259 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6260 | |
wolfSSL | 15:117db924cf7c | 6261 | ret = GetOctetString(input, &idx, &length, sz); |
wolfSSL | 15:117db924cf7c | 6262 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 6263 | return ret; |
wolfSSL | 15:117db924cf7c | 6264 | |
wolfSSL | 15:117db924cf7c | 6265 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6266 | cert->extSubjKeyIdSrc = &input[idx]; |
wolfSSL | 15:117db924cf7c | 6267 | cert->extSubjKeyIdSz = length; |
wolfSSL | 15:117db924cf7c | 6268 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 15:117db924cf7c | 6269 | |
wolfSSL | 15:117db924cf7c | 6270 | if (length == SIGNER_DIGEST_SIZE) { |
wolfSSL | 15:117db924cf7c | 6271 | XMEMCPY(cert->extSubjKeyId, input + idx, length); |
wolfSSL | 15:117db924cf7c | 6272 | } |
wolfSSL | 15:117db924cf7c | 6273 | else { |
wolfSSL | 15:117db924cf7c | 6274 | #ifdef NO_SHA |
wolfSSL | 15:117db924cf7c | 6275 | ret = wc_Sha256Hash(input + idx, length, cert->extSubjKeyId); |
wolfSSL | 15:117db924cf7c | 6276 | #else |
wolfSSL | 15:117db924cf7c | 6277 | ret = wc_ShaHash(input + idx, length, cert->extSubjKeyId); |
wolfSSL | 15:117db924cf7c | 6278 | #endif |
wolfSSL | 15:117db924cf7c | 6279 | } |
wolfSSL | 15:117db924cf7c | 6280 | |
wolfSSL | 15:117db924cf7c | 6281 | return ret; |
wolfSSL | 15:117db924cf7c | 6282 | } |
wolfSSL | 15:117db924cf7c | 6283 | |
wolfSSL | 15:117db924cf7c | 6284 | |
wolfSSL | 15:117db924cf7c | 6285 | static int DecodeKeyUsage(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6286 | { |
wolfSSL | 15:117db924cf7c | 6287 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6288 | int length; |
wolfSSL | 15:117db924cf7c | 6289 | int ret; |
wolfSSL | 15:117db924cf7c | 6290 | WOLFSSL_ENTER("DecodeKeyUsage"); |
wolfSSL | 15:117db924cf7c | 6291 | |
wolfSSL | 15:117db924cf7c | 6292 | ret = CheckBitString(input, &idx, &length, sz, 0, NULL); |
wolfSSL | 15:117db924cf7c | 6293 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 6294 | return ret; |
wolfSSL | 15:117db924cf7c | 6295 | |
wolfSSL | 15:117db924cf7c | 6296 | cert->extKeyUsage = (word16)(input[idx]); |
wolfSSL | 15:117db924cf7c | 6297 | if (length == 2) |
wolfSSL | 15:117db924cf7c | 6298 | cert->extKeyUsage |= (word16)(input[idx+1] << 8); |
wolfSSL | 15:117db924cf7c | 6299 | |
wolfSSL | 15:117db924cf7c | 6300 | return 0; |
wolfSSL | 15:117db924cf7c | 6301 | } |
wolfSSL | 15:117db924cf7c | 6302 | |
wolfSSL | 15:117db924cf7c | 6303 | |
wolfSSL | 15:117db924cf7c | 6304 | static int DecodeExtKeyUsage(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6305 | { |
wolfSSL | 15:117db924cf7c | 6306 | word32 idx = 0, oid; |
wolfSSL | 15:117db924cf7c | 6307 | int length; |
wolfSSL | 15:117db924cf7c | 6308 | |
wolfSSL | 15:117db924cf7c | 6309 | WOLFSSL_ENTER("DecodeExtKeyUsage"); |
wolfSSL | 15:117db924cf7c | 6310 | |
wolfSSL | 15:117db924cf7c | 6311 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6312 | WOLFSSL_MSG("\tfail: should be a SEQUENCE"); |
wolfSSL | 15:117db924cf7c | 6313 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6314 | } |
wolfSSL | 15:117db924cf7c | 6315 | |
wolfSSL | 15:117db924cf7c | 6316 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6317 | cert->extExtKeyUsageSrc = input + idx; |
wolfSSL | 15:117db924cf7c | 6318 | cert->extExtKeyUsageSz = length; |
wolfSSL | 15:117db924cf7c | 6319 | #endif |
wolfSSL | 15:117db924cf7c | 6320 | |
wolfSSL | 15:117db924cf7c | 6321 | while (idx < (word32)sz) { |
wolfSSL | 15:117db924cf7c | 6322 | if (GetObjectId(input, &idx, &oid, oidCertKeyUseType, sz) < 0) |
wolfSSL | 15:117db924cf7c | 6323 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6324 | |
wolfSSL | 15:117db924cf7c | 6325 | switch (oid) { |
wolfSSL | 15:117db924cf7c | 6326 | case EKU_ANY_OID: |
wolfSSL | 15:117db924cf7c | 6327 | cert->extExtKeyUsage |= EXTKEYUSE_ANY; |
wolfSSL | 15:117db924cf7c | 6328 | break; |
wolfSSL | 15:117db924cf7c | 6329 | case EKU_SERVER_AUTH_OID: |
wolfSSL | 15:117db924cf7c | 6330 | cert->extExtKeyUsage |= EXTKEYUSE_SERVER_AUTH; |
wolfSSL | 15:117db924cf7c | 6331 | break; |
wolfSSL | 15:117db924cf7c | 6332 | case EKU_CLIENT_AUTH_OID: |
wolfSSL | 15:117db924cf7c | 6333 | cert->extExtKeyUsage |= EXTKEYUSE_CLIENT_AUTH; |
wolfSSL | 15:117db924cf7c | 6334 | break; |
wolfSSL | 15:117db924cf7c | 6335 | case EKU_CODESIGNING_OID: |
wolfSSL | 15:117db924cf7c | 6336 | cert->extExtKeyUsage |= EXTKEYUSE_CODESIGN; |
wolfSSL | 15:117db924cf7c | 6337 | break; |
wolfSSL | 15:117db924cf7c | 6338 | case EKU_EMAILPROTECT_OID: |
wolfSSL | 15:117db924cf7c | 6339 | cert->extExtKeyUsage |= EXTKEYUSE_EMAILPROT; |
wolfSSL | 15:117db924cf7c | 6340 | break; |
wolfSSL | 15:117db924cf7c | 6341 | case EKU_TIMESTAMP_OID: |
wolfSSL | 15:117db924cf7c | 6342 | cert->extExtKeyUsage |= EXTKEYUSE_TIMESTAMP; |
wolfSSL | 15:117db924cf7c | 6343 | break; |
wolfSSL | 15:117db924cf7c | 6344 | case EKU_OCSP_SIGN_OID: |
wolfSSL | 15:117db924cf7c | 6345 | cert->extExtKeyUsage |= EXTKEYUSE_OCSP_SIGN; |
wolfSSL | 15:117db924cf7c | 6346 | break; |
wolfSSL | 15:117db924cf7c | 6347 | } |
wolfSSL | 15:117db924cf7c | 6348 | |
wolfSSL | 15:117db924cf7c | 6349 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6350 | cert->extExtKeyUsageCount++; |
wolfSSL | 15:117db924cf7c | 6351 | #endif |
wolfSSL | 15:117db924cf7c | 6352 | } |
wolfSSL | 15:117db924cf7c | 6353 | |
wolfSSL | 15:117db924cf7c | 6354 | return 0; |
wolfSSL | 15:117db924cf7c | 6355 | } |
wolfSSL | 15:117db924cf7c | 6356 | |
wolfSSL | 15:117db924cf7c | 6357 | |
wolfSSL | 15:117db924cf7c | 6358 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 6359 | #define ASN_TYPE_MASK 0xF |
wolfSSL | 15:117db924cf7c | 6360 | static int DecodeSubtree(byte* input, int sz, Base_entry** head, void* heap) |
wolfSSL | 15:117db924cf7c | 6361 | { |
wolfSSL | 15:117db924cf7c | 6362 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6363 | |
wolfSSL | 15:117db924cf7c | 6364 | (void)heap; |
wolfSSL | 15:117db924cf7c | 6365 | |
wolfSSL | 15:117db924cf7c | 6366 | while (idx < (word32)sz) { |
wolfSSL | 15:117db924cf7c | 6367 | int seqLength, strLength; |
wolfSSL | 15:117db924cf7c | 6368 | word32 nameIdx; |
wolfSSL | 15:117db924cf7c | 6369 | byte b, bType; |
wolfSSL | 15:117db924cf7c | 6370 | |
wolfSSL | 15:117db924cf7c | 6371 | if (GetSequence(input, &idx, &seqLength, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6372 | WOLFSSL_MSG("\tfail: should be a SEQUENCE"); |
wolfSSL | 15:117db924cf7c | 6373 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6374 | } |
wolfSSL | 15:117db924cf7c | 6375 | nameIdx = idx; |
wolfSSL | 15:117db924cf7c | 6376 | b = input[nameIdx++]; |
wolfSSL | 15:117db924cf7c | 6377 | |
wolfSSL | 15:117db924cf7c | 6378 | if (GetLength(input, &nameIdx, &strLength, sz) <= 0) { |
wolfSSL | 15:117db924cf7c | 6379 | WOLFSSL_MSG("\tinvalid length"); |
wolfSSL | 15:117db924cf7c | 6380 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6381 | } |
wolfSSL | 15:117db924cf7c | 6382 | |
wolfSSL | 15:117db924cf7c | 6383 | /* Get type, LSB 4-bits */ |
wolfSSL | 15:117db924cf7c | 6384 | bType = (b & ASN_TYPE_MASK); |
wolfSSL | 15:117db924cf7c | 6385 | |
wolfSSL | 15:117db924cf7c | 6386 | if (bType == ASN_DNS_TYPE || bType == ASN_RFC822_TYPE || |
wolfSSL | 15:117db924cf7c | 6387 | bType == ASN_DIR_TYPE) { |
wolfSSL | 15:117db924cf7c | 6388 | Base_entry* entry; |
wolfSSL | 15:117db924cf7c | 6389 | |
wolfSSL | 15:117db924cf7c | 6390 | /* if constructed has leading sequence */ |
wolfSSL | 15:117db924cf7c | 6391 | if (b & ASN_CONSTRUCTED) { |
wolfSSL | 15:117db924cf7c | 6392 | if (GetSequence(input, &nameIdx, &strLength, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6393 | WOLFSSL_MSG("\tfail: constructed be a SEQUENCE"); |
wolfSSL | 15:117db924cf7c | 6394 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6395 | } |
wolfSSL | 15:117db924cf7c | 6396 | } |
wolfSSL | 15:117db924cf7c | 6397 | |
wolfSSL | 15:117db924cf7c | 6398 | entry = (Base_entry*)XMALLOC(sizeof(Base_entry), heap, |
wolfSSL | 15:117db924cf7c | 6399 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 6400 | if (entry == NULL) { |
wolfSSL | 15:117db924cf7c | 6401 | WOLFSSL_MSG("allocate error"); |
wolfSSL | 15:117db924cf7c | 6402 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 6403 | } |
wolfSSL | 15:117db924cf7c | 6404 | |
wolfSSL | 15:117db924cf7c | 6405 | entry->name = (char*)XMALLOC(strLength, heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 6406 | if (entry->name == NULL) { |
wolfSSL | 15:117db924cf7c | 6407 | WOLFSSL_MSG("allocate error"); |
wolfSSL | 15:117db924cf7c | 6408 | XFREE(entry, heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 15:117db924cf7c | 6409 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 6410 | } |
wolfSSL | 15:117db924cf7c | 6411 | |
wolfSSL | 15:117db924cf7c | 6412 | XMEMCPY(entry->name, &input[nameIdx], strLength); |
wolfSSL | 15:117db924cf7c | 6413 | entry->nameSz = strLength; |
wolfSSL | 15:117db924cf7c | 6414 | entry->type = bType; |
wolfSSL | 15:117db924cf7c | 6415 | |
wolfSSL | 15:117db924cf7c | 6416 | entry->next = *head; |
wolfSSL | 15:117db924cf7c | 6417 | *head = entry; |
wolfSSL | 15:117db924cf7c | 6418 | } |
wolfSSL | 15:117db924cf7c | 6419 | |
wolfSSL | 15:117db924cf7c | 6420 | idx += seqLength; |
wolfSSL | 15:117db924cf7c | 6421 | } |
wolfSSL | 15:117db924cf7c | 6422 | |
wolfSSL | 15:117db924cf7c | 6423 | return 0; |
wolfSSL | 15:117db924cf7c | 6424 | } |
wolfSSL | 15:117db924cf7c | 6425 | |
wolfSSL | 15:117db924cf7c | 6426 | |
wolfSSL | 15:117db924cf7c | 6427 | static int DecodeNameConstraints(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6428 | { |
wolfSSL | 15:117db924cf7c | 6429 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6430 | int length = 0; |
wolfSSL | 15:117db924cf7c | 6431 | |
wolfSSL | 15:117db924cf7c | 6432 | WOLFSSL_ENTER("DecodeNameConstraints"); |
wolfSSL | 15:117db924cf7c | 6433 | |
wolfSSL | 15:117db924cf7c | 6434 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6435 | WOLFSSL_MSG("\tfail: should be a SEQUENCE"); |
wolfSSL | 15:117db924cf7c | 6436 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6437 | } |
wolfSSL | 15:117db924cf7c | 6438 | |
wolfSSL | 15:117db924cf7c | 6439 | while (idx < (word32)sz) { |
wolfSSL | 15:117db924cf7c | 6440 | byte b = input[idx++]; |
wolfSSL | 15:117db924cf7c | 6441 | Base_entry** subtree = NULL; |
wolfSSL | 15:117db924cf7c | 6442 | |
wolfSSL | 15:117db924cf7c | 6443 | if (GetLength(input, &idx, &length, sz) <= 0) { |
wolfSSL | 15:117db924cf7c | 6444 | WOLFSSL_MSG("\tinvalid length"); |
wolfSSL | 15:117db924cf7c | 6445 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6446 | } |
wolfSSL | 15:117db924cf7c | 6447 | |
wolfSSL | 15:117db924cf7c | 6448 | if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0)) |
wolfSSL | 15:117db924cf7c | 6449 | subtree = &cert->permittedNames; |
wolfSSL | 15:117db924cf7c | 6450 | else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1)) |
wolfSSL | 15:117db924cf7c | 6451 | subtree = &cert->excludedNames; |
wolfSSL | 15:117db924cf7c | 6452 | else { |
wolfSSL | 15:117db924cf7c | 6453 | WOLFSSL_MSG("\tinvalid subtree"); |
wolfSSL | 15:117db924cf7c | 6454 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6455 | } |
wolfSSL | 15:117db924cf7c | 6456 | |
wolfSSL | 15:117db924cf7c | 6457 | DecodeSubtree(input + idx, length, subtree, cert->heap); |
wolfSSL | 15:117db924cf7c | 6458 | |
wolfSSL | 15:117db924cf7c | 6459 | idx += length; |
wolfSSL | 15:117db924cf7c | 6460 | } |
wolfSSL | 15:117db924cf7c | 6461 | |
wolfSSL | 15:117db924cf7c | 6462 | return 0; |
wolfSSL | 15:117db924cf7c | 6463 | } |
wolfSSL | 15:117db924cf7c | 6464 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 6465 | |
wolfSSL | 15:117db924cf7c | 6466 | #if (defined(WOLFSSL_CERT_EXT) && !defined(WOLFSSL_SEP)) || defined(OPENSSL_EXTRA) |
wolfSSL | 15:117db924cf7c | 6467 | |
wolfSSL | 15:117db924cf7c | 6468 | static int Word32ToString(char* d, word32 number) |
wolfSSL | 15:117db924cf7c | 6469 | { |
wolfSSL | 15:117db924cf7c | 6470 | int i = 0; |
wolfSSL | 15:117db924cf7c | 6471 | |
wolfSSL | 15:117db924cf7c | 6472 | if (d != NULL) { |
wolfSSL | 15:117db924cf7c | 6473 | word32 order = 1000000000; |
wolfSSL | 15:117db924cf7c | 6474 | word32 digit; |
wolfSSL | 15:117db924cf7c | 6475 | |
wolfSSL | 15:117db924cf7c | 6476 | if (number == 0) { |
wolfSSL | 15:117db924cf7c | 6477 | d[i++] = '0'; |
wolfSSL | 15:117db924cf7c | 6478 | } |
wolfSSL | 15:117db924cf7c | 6479 | else { |
wolfSSL | 15:117db924cf7c | 6480 | while (order) { |
wolfSSL | 15:117db924cf7c | 6481 | digit = number / order; |
wolfSSL | 15:117db924cf7c | 6482 | if (i > 0 || digit != 0) { |
wolfSSL | 15:117db924cf7c | 6483 | d[i++] = (char)digit + '0'; |
wolfSSL | 15:117db924cf7c | 6484 | } |
wolfSSL | 15:117db924cf7c | 6485 | if (digit != 0) |
wolfSSL | 15:117db924cf7c | 6486 | number %= digit * order; |
wolfSSL | 15:117db924cf7c | 6487 | if (order > 1) |
wolfSSL | 15:117db924cf7c | 6488 | order /= 10; |
wolfSSL | 15:117db924cf7c | 6489 | else |
wolfSSL | 15:117db924cf7c | 6490 | order = 0; |
wolfSSL | 15:117db924cf7c | 6491 | } |
wolfSSL | 15:117db924cf7c | 6492 | } |
wolfSSL | 15:117db924cf7c | 6493 | d[i] = 0; |
wolfSSL | 15:117db924cf7c | 6494 | } |
wolfSSL | 15:117db924cf7c | 6495 | |
wolfSSL | 15:117db924cf7c | 6496 | return i; |
wolfSSL | 15:117db924cf7c | 6497 | } |
wolfSSL | 15:117db924cf7c | 6498 | |
wolfSSL | 15:117db924cf7c | 6499 | |
wolfSSL | 15:117db924cf7c | 6500 | /* Decode ITU-T X.690 OID format to a string representation |
wolfSSL | 15:117db924cf7c | 6501 | * return string length */ |
wolfSSL | 15:117db924cf7c | 6502 | int DecodePolicyOID(char *out, word32 outSz, byte *in, word32 inSz) |
wolfSSL | 15:117db924cf7c | 6503 | { |
wolfSSL | 15:117db924cf7c | 6504 | word32 val, idx = 0, nb_bytes; |
wolfSSL | 15:117db924cf7c | 6505 | size_t w_bytes = 0; |
wolfSSL | 15:117db924cf7c | 6506 | |
wolfSSL | 15:117db924cf7c | 6507 | if (out == NULL || in == NULL || outSz < 4 || inSz < 2) |
wolfSSL | 15:117db924cf7c | 6508 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 6509 | |
wolfSSL | 15:117db924cf7c | 6510 | /* first two byte must be interpreted as : 40 * int1 + int2 */ |
wolfSSL | 15:117db924cf7c | 6511 | val = (word16)in[idx++]; |
wolfSSL | 15:117db924cf7c | 6512 | |
wolfSSL | 15:117db924cf7c | 6513 | w_bytes = Word32ToString(out, val / 40); |
wolfSSL | 15:117db924cf7c | 6514 | out[w_bytes++] = '.'; |
wolfSSL | 15:117db924cf7c | 6515 | w_bytes += Word32ToString(out+w_bytes, val % 40); |
wolfSSL | 15:117db924cf7c | 6516 | |
wolfSSL | 15:117db924cf7c | 6517 | while (idx < inSz) { |
wolfSSL | 15:117db924cf7c | 6518 | /* init value */ |
wolfSSL | 15:117db924cf7c | 6519 | val = 0; |
wolfSSL | 15:117db924cf7c | 6520 | nb_bytes = 0; |
wolfSSL | 15:117db924cf7c | 6521 | |
wolfSSL | 15:117db924cf7c | 6522 | /* check that output size is ok */ |
wolfSSL | 15:117db924cf7c | 6523 | if (w_bytes > (outSz - 3)) |
wolfSSL | 15:117db924cf7c | 6524 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 6525 | |
wolfSSL | 15:117db924cf7c | 6526 | /* first bit is used to set if value is coded on 1 or multiple bytes */ |
wolfSSL | 15:117db924cf7c | 6527 | while ((in[idx+nb_bytes] & 0x80)) |
wolfSSL | 15:117db924cf7c | 6528 | nb_bytes++; |
wolfSSL | 15:117db924cf7c | 6529 | |
wolfSSL | 15:117db924cf7c | 6530 | if (!nb_bytes) |
wolfSSL | 15:117db924cf7c | 6531 | val = (word32)(in[idx++] & 0x7f); |
wolfSSL | 15:117db924cf7c | 6532 | else { |
wolfSSL | 15:117db924cf7c | 6533 | word32 base = 1, tmp = nb_bytes; |
wolfSSL | 15:117db924cf7c | 6534 | |
wolfSSL | 15:117db924cf7c | 6535 | while (tmp != 0) { |
wolfSSL | 15:117db924cf7c | 6536 | val += (word32)(in[idx+tmp] & 0x7f) * base; |
wolfSSL | 15:117db924cf7c | 6537 | base *= 128; |
wolfSSL | 15:117db924cf7c | 6538 | tmp--; |
wolfSSL | 15:117db924cf7c | 6539 | } |
wolfSSL | 15:117db924cf7c | 6540 | val += (word32)(in[idx++] & 0x7f) * base; |
wolfSSL | 15:117db924cf7c | 6541 | |
wolfSSL | 15:117db924cf7c | 6542 | idx += nb_bytes; |
wolfSSL | 15:117db924cf7c | 6543 | } |
wolfSSL | 15:117db924cf7c | 6544 | |
wolfSSL | 15:117db924cf7c | 6545 | out[w_bytes++] = '.'; |
wolfSSL | 15:117db924cf7c | 6546 | w_bytes += Word32ToString(out+w_bytes, val); |
wolfSSL | 15:117db924cf7c | 6547 | } |
wolfSSL | 15:117db924cf7c | 6548 | |
wolfSSL | 15:117db924cf7c | 6549 | return (int)w_bytes; |
wolfSSL | 15:117db924cf7c | 6550 | } |
wolfSSL | 15:117db924cf7c | 6551 | #endif /* WOLFSSL_CERT_EXT && !WOLFSSL_SEP */ |
wolfSSL | 15:117db924cf7c | 6552 | |
wolfSSL | 15:117db924cf7c | 6553 | #if defined(WOLFSSL_SEP) || defined(WOLFSSL_CERT_EXT) |
wolfSSL | 15:117db924cf7c | 6554 | /* Reference: https://tools.ietf.org/html/rfc5280#section-4.2.1.4 */ |
wolfSSL | 15:117db924cf7c | 6555 | static int DecodeCertPolicy(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6556 | { |
wolfSSL | 15:117db924cf7c | 6557 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6558 | word32 oldIdx; |
wolfSSL | 15:117db924cf7c | 6559 | int ret; |
wolfSSL | 15:117db924cf7c | 6560 | int total_length = 0, policy_length = 0, length = 0; |
wolfSSL | 15:117db924cf7c | 6561 | #if !defined(WOLFSSL_SEP) && defined(WOLFSSL_CERT_EXT) && \ |
wolfSSL | 15:117db924cf7c | 6562 | !defined(WOLFSSL_DUP_CERTPOL) |
wolfSSL | 15:117db924cf7c | 6563 | int i; |
wolfSSL | 15:117db924cf7c | 6564 | #endif |
wolfSSL | 15:117db924cf7c | 6565 | |
wolfSSL | 15:117db924cf7c | 6566 | WOLFSSL_ENTER("DecodeCertPolicy"); |
wolfSSL | 15:117db924cf7c | 6567 | |
wolfSSL | 15:117db924cf7c | 6568 | if (GetSequence(input, &idx, &total_length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6569 | WOLFSSL_MSG("\tGet CertPolicy total seq failed"); |
wolfSSL | 15:117db924cf7c | 6570 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6571 | } |
wolfSSL | 15:117db924cf7c | 6572 | |
wolfSSL | 15:117db924cf7c | 6573 | /* Validate total length */ |
wolfSSL | 15:117db924cf7c | 6574 | if (total_length > (sz - (int)idx)) { |
wolfSSL | 15:117db924cf7c | 6575 | WOLFSSL_MSG("\tCertPolicy length mismatch"); |
wolfSSL | 15:117db924cf7c | 6576 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6577 | } |
wolfSSL | 15:117db924cf7c | 6578 | |
wolfSSL | 15:117db924cf7c | 6579 | /* Unwrap certificatePolicies */ |
wolfSSL | 15:117db924cf7c | 6580 | do { |
wolfSSL | 15:117db924cf7c | 6581 | if (GetSequence(input, &idx, &policy_length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6582 | WOLFSSL_MSG("\tGet CertPolicy seq failed"); |
wolfSSL | 15:117db924cf7c | 6583 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6584 | } |
wolfSSL | 15:117db924cf7c | 6585 | |
wolfSSL | 15:117db924cf7c | 6586 | oldIdx = idx; |
wolfSSL | 15:117db924cf7c | 6587 | ret = GetASNObjectId(input, &idx, &length, sz); |
wolfSSL | 15:117db924cf7c | 6588 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 6589 | return ret; |
wolfSSL | 15:117db924cf7c | 6590 | policy_length -= idx - oldIdx; |
wolfSSL | 15:117db924cf7c | 6591 | |
wolfSSL | 15:117db924cf7c | 6592 | if (length > 0) { |
wolfSSL | 15:117db924cf7c | 6593 | /* Verify length won't overrun buffer */ |
wolfSSL | 15:117db924cf7c | 6594 | if (length > (sz - (int)idx)) { |
wolfSSL | 15:117db924cf7c | 6595 | WOLFSSL_MSG("\tCertPolicy length exceeds input buffer"); |
wolfSSL | 15:117db924cf7c | 6596 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6597 | } |
wolfSSL | 15:117db924cf7c | 6598 | |
wolfSSL | 15:117db924cf7c | 6599 | #if defined(WOLFSSL_SEP) |
wolfSSL | 15:117db924cf7c | 6600 | cert->deviceType = (byte*)XMALLOC(length, cert->heap, |
wolfSSL | 15:117db924cf7c | 6601 | DYNAMIC_TYPE_X509_EXT); |
wolfSSL | 15:117db924cf7c | 6602 | if (cert->deviceType == NULL) { |
wolfSSL | 15:117db924cf7c | 6603 | WOLFSSL_MSG("\tCouldn't alloc memory for deviceType"); |
wolfSSL | 15:117db924cf7c | 6604 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 6605 | } |
wolfSSL | 15:117db924cf7c | 6606 | cert->deviceTypeSz = length; |
wolfSSL | 15:117db924cf7c | 6607 | XMEMCPY(cert->deviceType, input + idx, length); |
wolfSSL | 15:117db924cf7c | 6608 | break; |
wolfSSL | 15:117db924cf7c | 6609 | #elif defined(WOLFSSL_CERT_EXT) |
wolfSSL | 15:117db924cf7c | 6610 | /* decode cert policy */ |
wolfSSL | 15:117db924cf7c | 6611 | if (DecodePolicyOID(cert->extCertPolicies[cert->extCertPoliciesNb], MAX_CERTPOL_SZ, |
wolfSSL | 15:117db924cf7c | 6612 | input + idx, length) <= 0) { |
wolfSSL | 15:117db924cf7c | 6613 | WOLFSSL_MSG("\tCouldn't decode CertPolicy"); |
wolfSSL | 15:117db924cf7c | 6614 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6615 | } |
wolfSSL | 15:117db924cf7c | 6616 | #ifndef WOLFSSL_DUP_CERTPOL |
wolfSSL | 15:117db924cf7c | 6617 | /* From RFC 5280 section 4.2.1.3 "A certificate policy OID MUST |
wolfSSL | 15:117db924cf7c | 6618 | * NOT appear more than once in a certificate policies |
wolfSSL | 15:117db924cf7c | 6619 | * extension". This is a sanity check for duplicates. |
wolfSSL | 15:117db924cf7c | 6620 | * extCertPolicies should only have OID values, additional |
wolfSSL | 15:117db924cf7c | 6621 | * qualifiers need to be stored in a seperate array. */ |
wolfSSL | 15:117db924cf7c | 6622 | for (i = 0; i < cert->extCertPoliciesNb; i++) { |
wolfSSL | 15:117db924cf7c | 6623 | if (XMEMCMP(cert->extCertPolicies[i], |
wolfSSL | 15:117db924cf7c | 6624 | cert->extCertPolicies[cert->extCertPoliciesNb], |
wolfSSL | 15:117db924cf7c | 6625 | MAX_CERTPOL_SZ) == 0) { |
wolfSSL | 15:117db924cf7c | 6626 | WOLFSSL_MSG("Duplicate policy OIDs not allowed"); |
wolfSSL | 15:117db924cf7c | 6627 | WOLFSSL_MSG("Use WOLFSSL_DUP_CERTPOL if wanted"); |
wolfSSL | 15:117db924cf7c | 6628 | return CERTPOLICIES_E; |
wolfSSL | 15:117db924cf7c | 6629 | } |
wolfSSL | 15:117db924cf7c | 6630 | } |
wolfSSL | 15:117db924cf7c | 6631 | #endif /* !WOLFSSL_DUP_CERTPOL */ |
wolfSSL | 15:117db924cf7c | 6632 | cert->extCertPoliciesNb++; |
wolfSSL | 15:117db924cf7c | 6633 | #else |
wolfSSL | 15:117db924cf7c | 6634 | WOLFSSL_LEAVE("DecodeCertPolicy : unsupported mode", 0); |
wolfSSL | 15:117db924cf7c | 6635 | return 0; |
wolfSSL | 15:117db924cf7c | 6636 | #endif |
wolfSSL | 15:117db924cf7c | 6637 | } |
wolfSSL | 15:117db924cf7c | 6638 | idx += policy_length; |
wolfSSL | 15:117db924cf7c | 6639 | } while((int)idx < total_length |
wolfSSL | 15:117db924cf7c | 6640 | #if defined(WOLFSSL_CERT_EXT) |
wolfSSL | 15:117db924cf7c | 6641 | && cert->extCertPoliciesNb < MAX_CERTPOL_NB |
wolfSSL | 15:117db924cf7c | 6642 | #endif |
wolfSSL | 15:117db924cf7c | 6643 | ); |
wolfSSL | 15:117db924cf7c | 6644 | |
wolfSSL | 15:117db924cf7c | 6645 | WOLFSSL_LEAVE("DecodeCertPolicy", 0); |
wolfSSL | 15:117db924cf7c | 6646 | return 0; |
wolfSSL | 15:117db924cf7c | 6647 | } |
wolfSSL | 15:117db924cf7c | 6648 | #endif /* WOLFSSL_SEP */ |
wolfSSL | 15:117db924cf7c | 6649 | |
wolfSSL | 15:117db924cf7c | 6650 | /* Macro to check if bit is set, if not sets and return success. |
wolfSSL | 15:117db924cf7c | 6651 | Otherwise returns failure */ |
wolfSSL | 15:117db924cf7c | 6652 | /* Macro required here because bit-field operation */ |
wolfSSL | 15:117db924cf7c | 6653 | #ifndef WOLFSSL_NO_ASN_STRICT |
wolfSSL | 15:117db924cf7c | 6654 | #define VERIFY_AND_SET_OID(bit) \ |
wolfSSL | 15:117db924cf7c | 6655 | if (bit == 0) \ |
wolfSSL | 15:117db924cf7c | 6656 | bit = 1; \ |
wolfSSL | 15:117db924cf7c | 6657 | else \ |
wolfSSL | 15:117db924cf7c | 6658 | return ASN_OBJECT_ID_E; |
wolfSSL | 15:117db924cf7c | 6659 | #else |
wolfSSL | 15:117db924cf7c | 6660 | /* With no strict defined, the verify is skipped */ |
wolfSSL | 15:117db924cf7c | 6661 | #define VERIFY_AND_SET_OID(bit) bit = 1; |
wolfSSL | 15:117db924cf7c | 6662 | #endif |
wolfSSL | 15:117db924cf7c | 6663 | |
wolfSSL | 15:117db924cf7c | 6664 | static int DecodeCertExtensions(DecodedCert* cert) |
wolfSSL | 15:117db924cf7c | 6665 | /* |
wolfSSL | 15:117db924cf7c | 6666 | * Processing the Certificate Extensions. This does not modify the current |
wolfSSL | 15:117db924cf7c | 6667 | * index. It is works starting with the recorded extensions pointer. |
wolfSSL | 15:117db924cf7c | 6668 | */ |
wolfSSL | 15:117db924cf7c | 6669 | { |
wolfSSL | 15:117db924cf7c | 6670 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 6671 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 6672 | int sz = cert->extensionsSz; |
wolfSSL | 15:117db924cf7c | 6673 | byte* input = cert->extensions; |
wolfSSL | 15:117db924cf7c | 6674 | int length; |
wolfSSL | 15:117db924cf7c | 6675 | word32 oid; |
wolfSSL | 15:117db924cf7c | 6676 | byte critical = 0; |
wolfSSL | 15:117db924cf7c | 6677 | byte criticalFail = 0; |
wolfSSL | 15:117db924cf7c | 6678 | |
wolfSSL | 15:117db924cf7c | 6679 | WOLFSSL_ENTER("DecodeCertExtensions"); |
wolfSSL | 15:117db924cf7c | 6680 | |
wolfSSL | 15:117db924cf7c | 6681 | if (input == NULL || sz == 0) |
wolfSSL | 15:117db924cf7c | 6682 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 6683 | |
wolfSSL | 15:117db924cf7c | 6684 | if (input[idx++] != ASN_EXTENSIONS) { |
wolfSSL | 15:117db924cf7c | 6685 | WOLFSSL_MSG("\tfail: should be an EXTENSIONS"); |
wolfSSL | 15:117db924cf7c | 6686 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6687 | } |
wolfSSL | 15:117db924cf7c | 6688 | |
wolfSSL | 15:117db924cf7c | 6689 | if (GetLength(input, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6690 | WOLFSSL_MSG("\tfail: invalid length"); |
wolfSSL | 15:117db924cf7c | 6691 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6692 | } |
wolfSSL | 15:117db924cf7c | 6693 | |
wolfSSL | 15:117db924cf7c | 6694 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6695 | WOLFSSL_MSG("\tfail: should be a SEQUENCE (1)"); |
wolfSSL | 15:117db924cf7c | 6696 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6697 | } |
wolfSSL | 15:117db924cf7c | 6698 | |
wolfSSL | 15:117db924cf7c | 6699 | while (idx < (word32)sz) { |
wolfSSL | 15:117db924cf7c | 6700 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 6701 | WOLFSSL_MSG("\tfail: should be a SEQUENCE"); |
wolfSSL | 15:117db924cf7c | 6702 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6703 | } |
wolfSSL | 15:117db924cf7c | 6704 | |
wolfSSL | 15:117db924cf7c | 6705 | oid = 0; |
wolfSSL | 15:117db924cf7c | 6706 | if ((ret = GetObjectId(input, &idx, &oid, oidCertExtType, sz)) < 0) { |
wolfSSL | 15:117db924cf7c | 6707 | WOLFSSL_MSG("\tfail: OBJECT ID"); |
wolfSSL | 15:117db924cf7c | 6708 | return ret; |
wolfSSL | 15:117db924cf7c | 6709 | } |
wolfSSL | 15:117db924cf7c | 6710 | |
wolfSSL | 15:117db924cf7c | 6711 | /* check for critical flag */ |
wolfSSL | 15:117db924cf7c | 6712 | critical = 0; |
wolfSSL | 15:117db924cf7c | 6713 | if (input[idx] == ASN_BOOLEAN) { |
wolfSSL | 15:117db924cf7c | 6714 | ret = GetBoolean(input, &idx, sz); |
wolfSSL | 15:117db924cf7c | 6715 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 6716 | WOLFSSL_MSG("\tfail: critical boolean"); |
wolfSSL | 15:117db924cf7c | 6717 | return ret; |
wolfSSL | 15:117db924cf7c | 6718 | } |
wolfSSL | 15:117db924cf7c | 6719 | |
wolfSSL | 15:117db924cf7c | 6720 | critical = (byte)ret; |
wolfSSL | 15:117db924cf7c | 6721 | } |
wolfSSL | 15:117db924cf7c | 6722 | |
wolfSSL | 15:117db924cf7c | 6723 | /* process the extension based on the OID */ |
wolfSSL | 15:117db924cf7c | 6724 | ret = GetOctetString(input, &idx, &length, sz); |
wolfSSL | 15:117db924cf7c | 6725 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 6726 | WOLFSSL_MSG("\tfail: bad OCTET STRING"); |
wolfSSL | 15:117db924cf7c | 6727 | return ret; |
wolfSSL | 15:117db924cf7c | 6728 | } |
wolfSSL | 15:117db924cf7c | 6729 | |
wolfSSL | 15:117db924cf7c | 6730 | switch (oid) { |
wolfSSL | 15:117db924cf7c | 6731 | case BASIC_CA_OID: |
wolfSSL | 15:117db924cf7c | 6732 | VERIFY_AND_SET_OID(cert->extBasicConstSet); |
wolfSSL | 15:117db924cf7c | 6733 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6734 | cert->extBasicConstCrit = critical; |
wolfSSL | 15:117db924cf7c | 6735 | #endif |
wolfSSL | 15:117db924cf7c | 6736 | if (DecodeBasicCaConstraint(&input[idx], length, cert) < 0) |
wolfSSL | 15:117db924cf7c | 6737 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6738 | break; |
wolfSSL | 15:117db924cf7c | 6739 | |
wolfSSL | 15:117db924cf7c | 6740 | case CRL_DIST_OID: |
wolfSSL | 15:117db924cf7c | 6741 | VERIFY_AND_SET_OID(cert->extCRLdistSet); |
wolfSSL | 15:117db924cf7c | 6742 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6743 | cert->extCRLdistCrit = critical; |
wolfSSL | 15:117db924cf7c | 6744 | #endif |
wolfSSL | 15:117db924cf7c | 6745 | if (DecodeCrlDist(&input[idx], length, cert) < 0) |
wolfSSL | 15:117db924cf7c | 6746 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6747 | break; |
wolfSSL | 15:117db924cf7c | 6748 | |
wolfSSL | 15:117db924cf7c | 6749 | case AUTH_INFO_OID: |
wolfSSL | 15:117db924cf7c | 6750 | VERIFY_AND_SET_OID(cert->extAuthInfoSet); |
wolfSSL | 15:117db924cf7c | 6751 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6752 | cert->extAuthInfoCrit = critical; |
wolfSSL | 15:117db924cf7c | 6753 | #endif |
wolfSSL | 15:117db924cf7c | 6754 | if (DecodeAuthInfo(&input[idx], length, cert) < 0) |
wolfSSL | 15:117db924cf7c | 6755 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6756 | break; |
wolfSSL | 15:117db924cf7c | 6757 | |
wolfSSL | 15:117db924cf7c | 6758 | case ALT_NAMES_OID: |
wolfSSL | 15:117db924cf7c | 6759 | VERIFY_AND_SET_OID(cert->extSubjAltNameSet); |
wolfSSL | 15:117db924cf7c | 6760 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6761 | cert->extSubjAltNameCrit = critical; |
wolfSSL | 15:117db924cf7c | 6762 | #endif |
wolfSSL | 15:117db924cf7c | 6763 | ret = DecodeAltNames(&input[idx], length, cert); |
wolfSSL | 15:117db924cf7c | 6764 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 6765 | return ret; |
wolfSSL | 15:117db924cf7c | 6766 | break; |
wolfSSL | 15:117db924cf7c | 6767 | |
wolfSSL | 15:117db924cf7c | 6768 | case AUTH_KEY_OID: |
wolfSSL | 15:117db924cf7c | 6769 | VERIFY_AND_SET_OID(cert->extAuthKeyIdSet); |
wolfSSL | 15:117db924cf7c | 6770 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6771 | cert->extAuthKeyIdCrit = critical; |
wolfSSL | 15:117db924cf7c | 6772 | #endif |
wolfSSL | 15:117db924cf7c | 6773 | #ifndef WOLFSSL_ALLOW_CRIT_SKID |
wolfSSL | 15:117db924cf7c | 6774 | /* This check is added due to RFC 5280 section 4.2.1.1 |
wolfSSL | 15:117db924cf7c | 6775 | * stating that conforming CA's must mark this extension |
wolfSSL | 15:117db924cf7c | 6776 | * as non-critical. When parsing extensions check that |
wolfSSL | 15:117db924cf7c | 6777 | * certificate was made in compliance with this. */ |
wolfSSL | 15:117db924cf7c | 6778 | if (critical) { |
wolfSSL | 15:117db924cf7c | 6779 | WOLFSSL_MSG("Critical Auth Key ID is not allowed"); |
wolfSSL | 15:117db924cf7c | 6780 | WOLFSSL_MSG("Use macro WOLFSSL_ALLOW_CRIT_SKID if wanted"); |
wolfSSL | 15:117db924cf7c | 6781 | return ASN_CRIT_EXT_E; |
wolfSSL | 15:117db924cf7c | 6782 | } |
wolfSSL | 15:117db924cf7c | 6783 | #endif |
wolfSSL | 15:117db924cf7c | 6784 | if (DecodeAuthKeyId(&input[idx], length, cert) < 0) |
wolfSSL | 15:117db924cf7c | 6785 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6786 | break; |
wolfSSL | 15:117db924cf7c | 6787 | |
wolfSSL | 15:117db924cf7c | 6788 | case SUBJ_KEY_OID: |
wolfSSL | 15:117db924cf7c | 6789 | VERIFY_AND_SET_OID(cert->extSubjKeyIdSet); |
wolfSSL | 15:117db924cf7c | 6790 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6791 | cert->extSubjKeyIdCrit = critical; |
wolfSSL | 15:117db924cf7c | 6792 | #endif |
wolfSSL | 15:117db924cf7c | 6793 | #ifndef WOLFSSL_ALLOW_CRIT_SKID |
wolfSSL | 15:117db924cf7c | 6794 | /* This check is added due to RFC 5280 section 4.2.1.2 |
wolfSSL | 15:117db924cf7c | 6795 | * stating that conforming CA's must mark this extension |
wolfSSL | 15:117db924cf7c | 6796 | * as non-critical. When parsing extensions check that |
wolfSSL | 15:117db924cf7c | 6797 | * certificate was made in compliance with this. */ |
wolfSSL | 15:117db924cf7c | 6798 | if (critical) { |
wolfSSL | 15:117db924cf7c | 6799 | WOLFSSL_MSG("Critical Subject Key ID is not allowed"); |
wolfSSL | 15:117db924cf7c | 6800 | WOLFSSL_MSG("Use macro WOLFSSL_ALLOW_CRIT_SKID if wanted"); |
wolfSSL | 15:117db924cf7c | 6801 | return ASN_CRIT_EXT_E; |
wolfSSL | 15:117db924cf7c | 6802 | } |
wolfSSL | 15:117db924cf7c | 6803 | #endif |
wolfSSL | 15:117db924cf7c | 6804 | |
wolfSSL | 15:117db924cf7c | 6805 | if (DecodeSubjKeyId(&input[idx], length, cert) < 0) |
wolfSSL | 15:117db924cf7c | 6806 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6807 | break; |
wolfSSL | 15:117db924cf7c | 6808 | |
wolfSSL | 15:117db924cf7c | 6809 | case CERT_POLICY_OID: |
wolfSSL | 15:117db924cf7c | 6810 | #ifdef WOLFSSL_SEP |
wolfSSL | 15:117db924cf7c | 6811 | VERIFY_AND_SET_OID(cert->extCertPolicySet); |
wolfSSL | 15:117db924cf7c | 6812 | #if defined(OPENSSL_EXTRA) || \ |
wolfSSL | 15:117db924cf7c | 6813 | defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6814 | cert->extCertPolicyCrit = critical; |
wolfSSL | 15:117db924cf7c | 6815 | #endif |
wolfSSL | 15:117db924cf7c | 6816 | #endif |
wolfSSL | 15:117db924cf7c | 6817 | #if defined(WOLFSSL_SEP) || defined(WOLFSSL_CERT_EXT) |
wolfSSL | 15:117db924cf7c | 6818 | if (DecodeCertPolicy(&input[idx], length, cert) < 0) { |
wolfSSL | 15:117db924cf7c | 6819 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6820 | } |
wolfSSL | 15:117db924cf7c | 6821 | #else |
wolfSSL | 15:117db924cf7c | 6822 | WOLFSSL_MSG("Certificate Policy extension not supported yet."); |
wolfSSL | 15:117db924cf7c | 6823 | #endif |
wolfSSL | 15:117db924cf7c | 6824 | break; |
wolfSSL | 15:117db924cf7c | 6825 | |
wolfSSL | 15:117db924cf7c | 6826 | case KEY_USAGE_OID: |
wolfSSL | 15:117db924cf7c | 6827 | VERIFY_AND_SET_OID(cert->extKeyUsageSet); |
wolfSSL | 15:117db924cf7c | 6828 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6829 | cert->extKeyUsageCrit = critical; |
wolfSSL | 15:117db924cf7c | 6830 | #endif |
wolfSSL | 15:117db924cf7c | 6831 | if (DecodeKeyUsage(&input[idx], length, cert) < 0) |
wolfSSL | 15:117db924cf7c | 6832 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6833 | break; |
wolfSSL | 15:117db924cf7c | 6834 | |
wolfSSL | 15:117db924cf7c | 6835 | case EXT_KEY_USAGE_OID: |
wolfSSL | 15:117db924cf7c | 6836 | VERIFY_AND_SET_OID(cert->extExtKeyUsageSet); |
wolfSSL | 15:117db924cf7c | 6837 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6838 | cert->extExtKeyUsageCrit = critical; |
wolfSSL | 15:117db924cf7c | 6839 | #endif |
wolfSSL | 15:117db924cf7c | 6840 | if (DecodeExtKeyUsage(&input[idx], length, cert) < 0) |
wolfSSL | 15:117db924cf7c | 6841 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6842 | break; |
wolfSSL | 15:117db924cf7c | 6843 | |
wolfSSL | 15:117db924cf7c | 6844 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 6845 | case NAME_CONS_OID: |
wolfSSL | 15:117db924cf7c | 6846 | #ifndef WOLFSSL_NO_ASN_STRICT |
wolfSSL | 15:117db924cf7c | 6847 | /* Verify RFC 5280 Sec 4.2.1.10 rule: |
wolfSSL | 15:117db924cf7c | 6848 | "The name constraints extension, |
wolfSSL | 15:117db924cf7c | 6849 | which MUST be used only in a CA certificate" */ |
wolfSSL | 15:117db924cf7c | 6850 | if (!cert->isCA) { |
wolfSSL | 15:117db924cf7c | 6851 | WOLFSSL_MSG("Name constraints allowed only for CA certs"); |
wolfSSL | 15:117db924cf7c | 6852 | return ASN_NAME_INVALID_E; |
wolfSSL | 15:117db924cf7c | 6853 | } |
wolfSSL | 15:117db924cf7c | 6854 | #endif |
wolfSSL | 15:117db924cf7c | 6855 | VERIFY_AND_SET_OID(cert->extNameConstraintSet); |
wolfSSL | 15:117db924cf7c | 6856 | #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) |
wolfSSL | 15:117db924cf7c | 6857 | cert->extNameConstraintCrit = critical; |
wolfSSL | 15:117db924cf7c | 6858 | #endif |
wolfSSL | 15:117db924cf7c | 6859 | if (DecodeNameConstraints(&input[idx], length, cert) < 0) |
wolfSSL | 15:117db924cf7c | 6860 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 6861 | break; |
wolfSSL | 15:117db924cf7c | 6862 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 6863 | |
wolfSSL | 15:117db924cf7c | 6864 | case INHIBIT_ANY_OID: |
wolfSSL | 15:117db924cf7c | 6865 | VERIFY_AND_SET_OID(cert->inhibitAnyOidSet); |
wolfSSL | 15:117db924cf7c | 6866 | WOLFSSL_MSG("Inhibit anyPolicy extension not supported yet."); |
wolfSSL | 15:117db924cf7c | 6867 | break; |
wolfSSL | 15:117db924cf7c | 6868 | |
wolfSSL | 15:117db924cf7c | 6869 | default: |
wolfSSL | 15:117db924cf7c | 6870 | /* While it is a failure to not support critical extensions, |
wolfSSL | 15:117db924cf7c | 6871 | * still parse the certificate ignoring the unsupported |
wolfSSL | 15:117db924cf7c | 6872 | * extension to allow caller to accept it with the verify |
wolfSSL | 15:117db924cf7c | 6873 | * callback. */ |
wolfSSL | 15:117db924cf7c | 6874 | if (critical) |
wolfSSL | 15:117db924cf7c | 6875 | criticalFail = 1; |
wolfSSL | 15:117db924cf7c | 6876 | break; |
wolfSSL | 15:117db924cf7c | 6877 | } |
wolfSSL | 15:117db924cf7c | 6878 | idx += length; |
wolfSSL | 15:117db924cf7c | 6879 | } |
wolfSSL | 15:117db924cf7c | 6880 | |
wolfSSL | 15:117db924cf7c | 6881 | return criticalFail ? ASN_CRIT_EXT_E : 0; |
wolfSSL | 15:117db924cf7c | 6882 | } |
wolfSSL | 15:117db924cf7c | 6883 | |
wolfSSL | 15:117db924cf7c | 6884 | int ParseCert(DecodedCert* cert, int type, int verify, void* cm) |
wolfSSL | 15:117db924cf7c | 6885 | { |
wolfSSL | 15:117db924cf7c | 6886 | int ret; |
wolfSSL | 15:117db924cf7c | 6887 | char* ptr; |
wolfSSL | 15:117db924cf7c | 6888 | |
wolfSSL | 15:117db924cf7c | 6889 | ret = ParseCertRelative(cert, type, verify, cm); |
wolfSSL | 15:117db924cf7c | 6890 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 6891 | return ret; |
wolfSSL | 15:117db924cf7c | 6892 | |
wolfSSL | 15:117db924cf7c | 6893 | if (cert->subjectCNLen > 0) { |
wolfSSL | 15:117db924cf7c | 6894 | ptr = (char*) XMALLOC(cert->subjectCNLen + 1, cert->heap, |
wolfSSL | 15:117db924cf7c | 6895 | DYNAMIC_TYPE_SUBJECT_CN); |
wolfSSL | 15:117db924cf7c | 6896 | if (ptr == NULL) |
wolfSSL | 15:117db924cf7c | 6897 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 6898 | XMEMCPY(ptr, cert->subjectCN, cert->subjectCNLen); |
wolfSSL | 15:117db924cf7c | 6899 | ptr[cert->subjectCNLen] = '\0'; |
wolfSSL | 15:117db924cf7c | 6900 | cert->subjectCN = ptr; |
wolfSSL | 15:117db924cf7c | 6901 | cert->subjectCNStored = 1; |
wolfSSL | 15:117db924cf7c | 6902 | } |
wolfSSL | 15:117db924cf7c | 6903 | |
wolfSSL | 15:117db924cf7c | 6904 | if (cert->keyOID == RSAk && |
wolfSSL | 15:117db924cf7c | 6905 | cert->publicKey != NULL && cert->pubKeySize > 0) { |
wolfSSL | 15:117db924cf7c | 6906 | ptr = (char*) XMALLOC(cert->pubKeySize, cert->heap, |
wolfSSL | 15:117db924cf7c | 6907 | DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 15:117db924cf7c | 6908 | if (ptr == NULL) |
wolfSSL | 15:117db924cf7c | 6909 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 6910 | XMEMCPY(ptr, cert->publicKey, cert->pubKeySize); |
wolfSSL | 15:117db924cf7c | 6911 | cert->publicKey = (byte *)ptr; |
wolfSSL | 15:117db924cf7c | 6912 | cert->pubKeyStored = 1; |
wolfSSL | 15:117db924cf7c | 6913 | } |
wolfSSL | 15:117db924cf7c | 6914 | |
wolfSSL | 15:117db924cf7c | 6915 | return ret; |
wolfSSL | 15:117db924cf7c | 6916 | } |
wolfSSL | 15:117db924cf7c | 6917 | |
wolfSSL | 15:117db924cf7c | 6918 | /* from SSL proper, for locking can't do find here anymore */ |
wolfSSL | 15:117db924cf7c | 6919 | #ifdef __cplusplus |
wolfSSL | 15:117db924cf7c | 6920 | extern "C" { |
wolfSSL | 15:117db924cf7c | 6921 | #endif |
wolfSSL | 15:117db924cf7c | 6922 | WOLFSSL_LOCAL Signer* GetCA(void* signers, byte* hash); |
wolfSSL | 15:117db924cf7c | 6923 | #ifndef NO_SKID |
wolfSSL | 15:117db924cf7c | 6924 | WOLFSSL_LOCAL Signer* GetCAByName(void* signers, byte* hash); |
wolfSSL | 15:117db924cf7c | 6925 | #endif |
wolfSSL | 15:117db924cf7c | 6926 | #ifdef __cplusplus |
wolfSSL | 15:117db924cf7c | 6927 | } |
wolfSSL | 15:117db924cf7c | 6928 | #endif |
wolfSSL | 15:117db924cf7c | 6929 | |
wolfSSL | 15:117db924cf7c | 6930 | |
wolfSSL | 15:117db924cf7c | 6931 | #if defined(WOLFCRYPT_ONLY) || defined(NO_CERTS) |
wolfSSL | 15:117db924cf7c | 6932 | |
wolfSSL | 15:117db924cf7c | 6933 | /* dummy functions, not using wolfSSL so don't need actual ones */ |
wolfSSL | 15:117db924cf7c | 6934 | Signer* GetCA(void* signers, byte* hash) |
wolfSSL | 15:117db924cf7c | 6935 | { |
wolfSSL | 15:117db924cf7c | 6936 | (void)hash; |
wolfSSL | 15:117db924cf7c | 6937 | |
wolfSSL | 15:117db924cf7c | 6938 | return (Signer*)signers; |
wolfSSL | 15:117db924cf7c | 6939 | } |
wolfSSL | 15:117db924cf7c | 6940 | |
wolfSSL | 15:117db924cf7c | 6941 | #ifndef NO_SKID |
wolfSSL | 15:117db924cf7c | 6942 | Signer* GetCAByName(void* signers, byte* hash) |
wolfSSL | 15:117db924cf7c | 6943 | { |
wolfSSL | 15:117db924cf7c | 6944 | (void)hash; |
wolfSSL | 15:117db924cf7c | 6945 | |
wolfSSL | 15:117db924cf7c | 6946 | return (Signer*)signers; |
wolfSSL | 15:117db924cf7c | 6947 | } |
wolfSSL | 15:117db924cf7c | 6948 | #endif /* NO_SKID */ |
wolfSSL | 15:117db924cf7c | 6949 | |
wolfSSL | 15:117db924cf7c | 6950 | #endif /* WOLFCRYPT_ONLY || NO_CERTS */ |
wolfSSL | 15:117db924cf7c | 6951 | |
wolfSSL | 15:117db924cf7c | 6952 | #if (defined(WOLFSSL_ALT_CERT_CHAINS) || \ |
wolfSSL | 15:117db924cf7c | 6953 | defined(WOLFSSL_NO_TRUSTED_CERTS_VERIFY)) && !defined(NO_SKID) |
wolfSSL | 15:117db924cf7c | 6954 | static Signer* GetCABySubjectAndPubKey(DecodedCert* cert, void* cm) |
wolfSSL | 15:117db924cf7c | 6955 | { |
wolfSSL | 15:117db924cf7c | 6956 | Signer* ca = NULL; |
wolfSSL | 15:117db924cf7c | 6957 | if (cert->extSubjKeyIdSet) |
wolfSSL | 15:117db924cf7c | 6958 | ca = GetCA(cm, cert->extSubjKeyId); |
wolfSSL | 15:117db924cf7c | 6959 | if (ca == NULL) |
wolfSSL | 15:117db924cf7c | 6960 | ca = GetCAByName(cm, cert->subjectHash); |
wolfSSL | 15:117db924cf7c | 6961 | if (ca) { |
wolfSSL | 15:117db924cf7c | 6962 | if ((ca->pubKeySize == cert->pubKeySize) && |
wolfSSL | 15:117db924cf7c | 6963 | (XMEMCMP(ca->publicKey, cert->publicKey, ca->pubKeySize) == 0)) { |
wolfSSL | 15:117db924cf7c | 6964 | return ca; |
wolfSSL | 15:117db924cf7c | 6965 | } |
wolfSSL | 15:117db924cf7c | 6966 | } |
wolfSSL | 15:117db924cf7c | 6967 | return NULL; |
wolfSSL | 15:117db924cf7c | 6968 | } |
wolfSSL | 15:117db924cf7c | 6969 | #endif |
wolfSSL | 15:117db924cf7c | 6970 | |
wolfSSL | 15:117db924cf7c | 6971 | int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm) |
wolfSSL | 15:117db924cf7c | 6972 | { |
wolfSSL | 15:117db924cf7c | 6973 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 6974 | int badDate = 0; |
wolfSSL | 15:117db924cf7c | 6975 | int criticalExt = 0; |
wolfSSL | 15:117db924cf7c | 6976 | word32 confirmOID; |
wolfSSL | 15:117db924cf7c | 6977 | int selfSigned = 0; |
wolfSSL | 15:117db924cf7c | 6978 | |
wolfSSL | 15:117db924cf7c | 6979 | if (cert == NULL) { |
wolfSSL | 15:117db924cf7c | 6980 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 6981 | } |
wolfSSL | 15:117db924cf7c | 6982 | |
wolfSSL | 15:117db924cf7c | 6983 | if (cert->sigCtx.state == SIG_STATE_BEGIN) { |
wolfSSL | 15:117db924cf7c | 6984 | if ((ret = DecodeToKey(cert, verify)) < 0) { |
wolfSSL | 15:117db924cf7c | 6985 | if (ret == ASN_BEFORE_DATE_E || ret == ASN_AFTER_DATE_E) |
wolfSSL | 15:117db924cf7c | 6986 | badDate = ret; |
wolfSSL | 15:117db924cf7c | 6987 | else |
wolfSSL | 15:117db924cf7c | 6988 | return ret; |
wolfSSL | 15:117db924cf7c | 6989 | } |
wolfSSL | 15:117db924cf7c | 6990 | |
wolfSSL | 15:117db924cf7c | 6991 | WOLFSSL_MSG("Parsed Past Key"); |
wolfSSL | 15:117db924cf7c | 6992 | |
wolfSSL | 15:117db924cf7c | 6993 | if (cert->srcIdx < cert->sigIndex) { |
wolfSSL | 15:117db924cf7c | 6994 | #ifndef ALLOW_V1_EXTENSIONS |
wolfSSL | 15:117db924cf7c | 6995 | if (cert->version < 2) { |
wolfSSL | 15:117db924cf7c | 6996 | WOLFSSL_MSG("\tv1 and v2 certs not allowed extensions"); |
wolfSSL | 15:117db924cf7c | 6997 | return ASN_VERSION_E; |
wolfSSL | 15:117db924cf7c | 6998 | } |
wolfSSL | 15:117db924cf7c | 6999 | #endif |
wolfSSL | 15:117db924cf7c | 7000 | |
wolfSSL | 15:117db924cf7c | 7001 | /* save extensions */ |
wolfSSL | 15:117db924cf7c | 7002 | cert->extensions = &cert->source[cert->srcIdx]; |
wolfSSL | 15:117db924cf7c | 7003 | cert->extensionsSz = cert->sigIndex - cert->srcIdx; |
wolfSSL | 15:117db924cf7c | 7004 | cert->extensionsIdx = cert->srcIdx; /* for potential later use */ |
wolfSSL | 15:117db924cf7c | 7005 | |
wolfSSL | 15:117db924cf7c | 7006 | if ((ret = DecodeCertExtensions(cert)) < 0) { |
wolfSSL | 15:117db924cf7c | 7007 | if (ret == ASN_CRIT_EXT_E) |
wolfSSL | 15:117db924cf7c | 7008 | criticalExt = ret; |
wolfSSL | 15:117db924cf7c | 7009 | else |
wolfSSL | 15:117db924cf7c | 7010 | return ret; |
wolfSSL | 15:117db924cf7c | 7011 | } |
wolfSSL | 15:117db924cf7c | 7012 | |
wolfSSL | 15:117db924cf7c | 7013 | /* advance past extensions */ |
wolfSSL | 15:117db924cf7c | 7014 | cert->srcIdx = cert->sigIndex; |
wolfSSL | 15:117db924cf7c | 7015 | } |
wolfSSL | 15:117db924cf7c | 7016 | |
wolfSSL | 15:117db924cf7c | 7017 | if ((ret = GetAlgoId(cert->source, &cert->srcIdx, &confirmOID, |
wolfSSL | 15:117db924cf7c | 7018 | oidSigType, cert->maxIdx)) < 0) |
wolfSSL | 15:117db924cf7c | 7019 | return ret; |
wolfSSL | 15:117db924cf7c | 7020 | |
wolfSSL | 15:117db924cf7c | 7021 | if ((ret = GetSignature(cert)) < 0) |
wolfSSL | 15:117db924cf7c | 7022 | return ret; |
wolfSSL | 15:117db924cf7c | 7023 | |
wolfSSL | 15:117db924cf7c | 7024 | if (confirmOID != cert->signatureOID) |
wolfSSL | 15:117db924cf7c | 7025 | return ASN_SIG_OID_E; |
wolfSSL | 15:117db924cf7c | 7026 | |
wolfSSL | 15:117db924cf7c | 7027 | #ifndef NO_SKID |
wolfSSL | 15:117db924cf7c | 7028 | if (cert->extSubjKeyIdSet == 0 && cert->publicKey != NULL && |
wolfSSL | 15:117db924cf7c | 7029 | cert->pubKeySize > 0) { |
wolfSSL | 15:117db924cf7c | 7030 | #ifdef NO_SHA |
wolfSSL | 15:117db924cf7c | 7031 | ret = wc_Sha256Hash(cert->publicKey, cert->pubKeySize, |
wolfSSL | 15:117db924cf7c | 7032 | cert->extSubjKeyId); |
wolfSSL | 15:117db924cf7c | 7033 | #else |
wolfSSL | 15:117db924cf7c | 7034 | ret = wc_ShaHash(cert->publicKey, cert->pubKeySize, |
wolfSSL | 15:117db924cf7c | 7035 | cert->extSubjKeyId); |
wolfSSL | 15:117db924cf7c | 7036 | #endif /* NO_SHA */ |
wolfSSL | 15:117db924cf7c | 7037 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 7038 | return ret; |
wolfSSL | 15:117db924cf7c | 7039 | } |
wolfSSL | 15:117db924cf7c | 7040 | #endif /* !NO_SKID */ |
wolfSSL | 15:117db924cf7c | 7041 | |
wolfSSL | 15:117db924cf7c | 7042 | if (verify != NO_VERIFY && type != CA_TYPE && type != TRUSTED_PEER_TYPE) { |
wolfSSL | 15:117db924cf7c | 7043 | cert->ca = NULL; |
wolfSSL | 15:117db924cf7c | 7044 | #ifndef NO_SKID |
wolfSSL | 15:117db924cf7c | 7045 | if (cert->extAuthKeyIdSet) |
wolfSSL | 15:117db924cf7c | 7046 | cert->ca = GetCA(cm, cert->extAuthKeyId); |
wolfSSL | 15:117db924cf7c | 7047 | if (cert->ca == NULL) |
wolfSSL | 15:117db924cf7c | 7048 | cert->ca = GetCAByName(cm, cert->issuerHash); |
wolfSSL | 15:117db924cf7c | 7049 | |
wolfSSL | 15:117db924cf7c | 7050 | /* OCSP Only: alt lookup using subject and pub key w/o sig check */ |
wolfSSL | 15:117db924cf7c | 7051 | #ifdef WOLFSSL_NO_TRUSTED_CERTS_VERIFY |
wolfSSL | 15:117db924cf7c | 7052 | if (cert->ca == NULL && verify == VERIFY_OCSP) { |
wolfSSL | 15:117db924cf7c | 7053 | cert->ca = GetCABySubjectAndPubKey(cert, cm); |
wolfSSL | 15:117db924cf7c | 7054 | if (cert->ca) { |
wolfSSL | 15:117db924cf7c | 7055 | ret = 0; /* success */ |
wolfSSL | 15:117db924cf7c | 7056 | goto exit_pcr; |
wolfSSL | 15:117db924cf7c | 7057 | } |
wolfSSL | 15:117db924cf7c | 7058 | } |
wolfSSL | 15:117db924cf7c | 7059 | #endif /* WOLFSSL_NO_TRUSTED_CERTS_VERIFY */ |
wolfSSL | 15:117db924cf7c | 7060 | |
wolfSSL | 15:117db924cf7c | 7061 | /* alt lookup using subject and public key */ |
wolfSSL | 15:117db924cf7c | 7062 | #ifdef WOLFSSL_ALT_CERT_CHAINS |
wolfSSL | 15:117db924cf7c | 7063 | if (cert->ca == NULL) |
wolfSSL | 15:117db924cf7c | 7064 | cert->ca = GetCABySubjectAndPubKey(cert, cm); |
wolfSSL | 15:117db924cf7c | 7065 | #endif |
wolfSSL | 15:117db924cf7c | 7066 | #else |
wolfSSL | 15:117db924cf7c | 7067 | cert->ca = GetCA(cm, cert->issuerHash); |
wolfSSL | 15:117db924cf7c | 7068 | if (XMEMCMP(cert->issuerHash, cert->subjectHash, KEYID_SIZE) == 0) |
wolfSSL | 15:117db924cf7c | 7069 | selfSigned = 1; |
wolfSSL | 15:117db924cf7c | 7070 | #endif /* !NO_SKID */ |
wolfSSL | 15:117db924cf7c | 7071 | |
wolfSSL | 15:117db924cf7c | 7072 | WOLFSSL_MSG("About to verify certificate signature"); |
wolfSSL | 15:117db924cf7c | 7073 | if (cert->ca) { |
wolfSSL | 15:117db924cf7c | 7074 | if (cert->isCA && cert->ca->pathLengthSet) { |
wolfSSL | 15:117db924cf7c | 7075 | if (selfSigned) { |
wolfSSL | 15:117db924cf7c | 7076 | if (cert->ca->pathLength != 0) { |
wolfSSL | 15:117db924cf7c | 7077 | WOLFSSL_MSG("Root CA with path length > 0"); |
wolfSSL | 15:117db924cf7c | 7078 | return ASN_PATHLEN_INV_E; |
wolfSSL | 15:117db924cf7c | 7079 | } |
wolfSSL | 15:117db924cf7c | 7080 | } |
wolfSSL | 15:117db924cf7c | 7081 | else { |
wolfSSL | 15:117db924cf7c | 7082 | if (cert->ca->pathLength == 0) { |
wolfSSL | 15:117db924cf7c | 7083 | WOLFSSL_MSG("CA with path length 0 signing a CA"); |
wolfSSL | 15:117db924cf7c | 7084 | return ASN_PATHLEN_INV_E; |
wolfSSL | 15:117db924cf7c | 7085 | } |
wolfSSL | 15:117db924cf7c | 7086 | else if (cert->pathLength >= cert->ca->pathLength) { |
wolfSSL | 15:117db924cf7c | 7087 | |
wolfSSL | 15:117db924cf7c | 7088 | WOLFSSL_MSG("CA signing CA with longer path length"); |
wolfSSL | 15:117db924cf7c | 7089 | return ASN_PATHLEN_INV_E; |
wolfSSL | 15:117db924cf7c | 7090 | } |
wolfSSL | 15:117db924cf7c | 7091 | } |
wolfSSL | 15:117db924cf7c | 7092 | } |
wolfSSL | 15:117db924cf7c | 7093 | |
wolfSSL | 15:117db924cf7c | 7094 | #ifdef HAVE_OCSP |
wolfSSL | 15:117db924cf7c | 7095 | /* Need the CA's public key hash for OCSP */ |
wolfSSL | 15:117db924cf7c | 7096 | #ifdef NO_SHA |
wolfSSL | 15:117db924cf7c | 7097 | ret = wc_Sha256Hash(cert->ca->publicKey, cert->ca->pubKeySize, |
wolfSSL | 15:117db924cf7c | 7098 | cert->issuerKeyHash); |
wolfSSL | 15:117db924cf7c | 7099 | #else |
wolfSSL | 15:117db924cf7c | 7100 | ret = wc_ShaHash(cert->ca->publicKey, cert->ca->pubKeySize, |
wolfSSL | 15:117db924cf7c | 7101 | cert->issuerKeyHash); |
wolfSSL | 15:117db924cf7c | 7102 | #endif /* NO_SHA */ |
wolfSSL | 15:117db924cf7c | 7103 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 7104 | return ret; |
wolfSSL | 15:117db924cf7c | 7105 | #endif /* HAVE_OCSP */ |
wolfSSL | 15:117db924cf7c | 7106 | } |
wolfSSL | 15:117db924cf7c | 7107 | } |
wolfSSL | 15:117db924cf7c | 7108 | } |
wolfSSL | 15:117db924cf7c | 7109 | |
wolfSSL | 15:117db924cf7c | 7110 | if (verify != NO_VERIFY && type != CA_TYPE && type != TRUSTED_PEER_TYPE) { |
wolfSSL | 15:117db924cf7c | 7111 | if (cert->ca) { |
wolfSSL | 15:117db924cf7c | 7112 | if (verify == VERIFY || verify == VERIFY_OCSP) { |
wolfSSL | 15:117db924cf7c | 7113 | /* try to confirm/verify signature */ |
wolfSSL | 15:117db924cf7c | 7114 | if ((ret = ConfirmSignature(&cert->sigCtx, |
wolfSSL | 15:117db924cf7c | 7115 | cert->source + cert->certBegin, |
wolfSSL | 15:117db924cf7c | 7116 | cert->sigIndex - cert->certBegin, |
wolfSSL | 15:117db924cf7c | 7117 | cert->ca->publicKey, cert->ca->pubKeySize, |
wolfSSL | 15:117db924cf7c | 7118 | cert->ca->keyOID, cert->signature, |
wolfSSL | 15:117db924cf7c | 7119 | cert->sigLength, cert->signatureOID)) != 0) { |
wolfSSL | 15:117db924cf7c | 7120 | if (ret != WC_PENDING_E) { |
wolfSSL | 15:117db924cf7c | 7121 | WOLFSSL_MSG("Confirm signature failed"); |
wolfSSL | 15:117db924cf7c | 7122 | } |
wolfSSL | 15:117db924cf7c | 7123 | return ret; |
wolfSSL | 15:117db924cf7c | 7124 | } |
wolfSSL | 15:117db924cf7c | 7125 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 7126 | /* check that this cert's name is permitted by the signer's |
wolfSSL | 15:117db924cf7c | 7127 | * name constraints */ |
wolfSSL | 15:117db924cf7c | 7128 | if (!ConfirmNameConstraints(cert->ca, cert)) { |
wolfSSL | 15:117db924cf7c | 7129 | WOLFSSL_MSG("Confirm name constraint failed"); |
wolfSSL | 15:117db924cf7c | 7130 | return ASN_NAME_INVALID_E; |
wolfSSL | 15:117db924cf7c | 7131 | } |
wolfSSL | 15:117db924cf7c | 7132 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 7133 | } |
wolfSSL | 15:117db924cf7c | 7134 | } |
wolfSSL | 15:117db924cf7c | 7135 | else { |
wolfSSL | 15:117db924cf7c | 7136 | /* no signer */ |
wolfSSL | 15:117db924cf7c | 7137 | WOLFSSL_MSG("No CA signer to verify with"); |
wolfSSL | 15:117db924cf7c | 7138 | return ASN_NO_SIGNER_E; |
wolfSSL | 15:117db924cf7c | 7139 | } |
wolfSSL | 15:117db924cf7c | 7140 | } |
wolfSSL | 15:117db924cf7c | 7141 | |
wolfSSL | 15:117db924cf7c | 7142 | #if defined(WOLFSSL_NO_TRUSTED_CERTS_VERIFY) && !defined(NO_SKID) |
wolfSSL | 15:117db924cf7c | 7143 | exit_pcr: |
wolfSSL | 15:117db924cf7c | 7144 | #endif |
wolfSSL | 15:117db924cf7c | 7145 | |
wolfSSL | 15:117db924cf7c | 7146 | if (badDate != 0) |
wolfSSL | 15:117db924cf7c | 7147 | return badDate; |
wolfSSL | 15:117db924cf7c | 7148 | |
wolfSSL | 15:117db924cf7c | 7149 | if (criticalExt != 0) |
wolfSSL | 15:117db924cf7c | 7150 | return criticalExt; |
wolfSSL | 15:117db924cf7c | 7151 | |
wolfSSL | 15:117db924cf7c | 7152 | return ret; |
wolfSSL | 15:117db924cf7c | 7153 | } |
wolfSSL | 15:117db924cf7c | 7154 | |
wolfSSL | 15:117db924cf7c | 7155 | /* Create and init an new signer */ |
wolfSSL | 15:117db924cf7c | 7156 | Signer* MakeSigner(void* heap) |
wolfSSL | 15:117db924cf7c | 7157 | { |
wolfSSL | 15:117db924cf7c | 7158 | Signer* signer = (Signer*) XMALLOC(sizeof(Signer), heap, |
wolfSSL | 15:117db924cf7c | 7159 | DYNAMIC_TYPE_SIGNER); |
wolfSSL | 15:117db924cf7c | 7160 | if (signer) { |
wolfSSL | 15:117db924cf7c | 7161 | signer->pubKeySize = 0; |
wolfSSL | 15:117db924cf7c | 7162 | signer->keyOID = 0; |
wolfSSL | 15:117db924cf7c | 7163 | signer->publicKey = NULL; |
wolfSSL | 15:117db924cf7c | 7164 | signer->nameLen = 0; |
wolfSSL | 15:117db924cf7c | 7165 | signer->name = NULL; |
wolfSSL | 15:117db924cf7c | 7166 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 7167 | signer->permittedNames = NULL; |
wolfSSL | 15:117db924cf7c | 7168 | signer->excludedNames = NULL; |
wolfSSL | 15:117db924cf7c | 7169 | #endif /* IGNORE_NAME_CONSTRAINTS */ |
wolfSSL | 15:117db924cf7c | 7170 | signer->pathLengthSet = 0; |
wolfSSL | 15:117db924cf7c | 7171 | signer->pathLength = 0; |
wolfSSL | 15:117db924cf7c | 7172 | #ifdef WOLFSSL_SIGNER_DER_CERT |
wolfSSL | 15:117db924cf7c | 7173 | signer->derCert = NULL; |
wolfSSL | 15:117db924cf7c | 7174 | #endif |
wolfSSL | 15:117db924cf7c | 7175 | signer->next = NULL; |
wolfSSL | 15:117db924cf7c | 7176 | } |
wolfSSL | 15:117db924cf7c | 7177 | (void)heap; |
wolfSSL | 15:117db924cf7c | 7178 | |
wolfSSL | 15:117db924cf7c | 7179 | return signer; |
wolfSSL | 15:117db924cf7c | 7180 | } |
wolfSSL | 15:117db924cf7c | 7181 | |
wolfSSL | 15:117db924cf7c | 7182 | |
wolfSSL | 15:117db924cf7c | 7183 | /* Free an individual signer */ |
wolfSSL | 15:117db924cf7c | 7184 | void FreeSigner(Signer* signer, void* heap) |
wolfSSL | 15:117db924cf7c | 7185 | { |
wolfSSL | 15:117db924cf7c | 7186 | XFREE(signer->name, heap, DYNAMIC_TYPE_SUBJECT_CN); |
wolfSSL | 15:117db924cf7c | 7187 | XFREE(signer->publicKey, heap, DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 15:117db924cf7c | 7188 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 7189 | if (signer->permittedNames) |
wolfSSL | 15:117db924cf7c | 7190 | FreeNameSubtrees(signer->permittedNames, heap); |
wolfSSL | 15:117db924cf7c | 7191 | if (signer->excludedNames) |
wolfSSL | 15:117db924cf7c | 7192 | FreeNameSubtrees(signer->excludedNames, heap); |
wolfSSL | 15:117db924cf7c | 7193 | #endif |
wolfSSL | 15:117db924cf7c | 7194 | #ifdef WOLFSSL_SIGNER_DER_CERT |
wolfSSL | 15:117db924cf7c | 7195 | FreeDer(&signer->derCert); |
wolfSSL | 15:117db924cf7c | 7196 | #endif |
wolfSSL | 15:117db924cf7c | 7197 | XFREE(signer, heap, DYNAMIC_TYPE_SIGNER); |
wolfSSL | 15:117db924cf7c | 7198 | |
wolfSSL | 15:117db924cf7c | 7199 | (void)heap; |
wolfSSL | 15:117db924cf7c | 7200 | } |
wolfSSL | 15:117db924cf7c | 7201 | |
wolfSSL | 15:117db924cf7c | 7202 | |
wolfSSL | 15:117db924cf7c | 7203 | /* Free the whole singer table with number of rows */ |
wolfSSL | 15:117db924cf7c | 7204 | void FreeSignerTable(Signer** table, int rows, void* heap) |
wolfSSL | 15:117db924cf7c | 7205 | { |
wolfSSL | 15:117db924cf7c | 7206 | int i; |
wolfSSL | 15:117db924cf7c | 7207 | |
wolfSSL | 15:117db924cf7c | 7208 | for (i = 0; i < rows; i++) { |
wolfSSL | 15:117db924cf7c | 7209 | Signer* signer = table[i]; |
wolfSSL | 15:117db924cf7c | 7210 | while (signer) { |
wolfSSL | 15:117db924cf7c | 7211 | Signer* next = signer->next; |
wolfSSL | 15:117db924cf7c | 7212 | FreeSigner(signer, heap); |
wolfSSL | 15:117db924cf7c | 7213 | signer = next; |
wolfSSL | 15:117db924cf7c | 7214 | } |
wolfSSL | 15:117db924cf7c | 7215 | table[i] = NULL; |
wolfSSL | 15:117db924cf7c | 7216 | } |
wolfSSL | 15:117db924cf7c | 7217 | } |
wolfSSL | 15:117db924cf7c | 7218 | |
wolfSSL | 15:117db924cf7c | 7219 | #ifdef WOLFSSL_TRUST_PEER_CERT |
wolfSSL | 15:117db924cf7c | 7220 | /* Free an individual trusted peer cert */ |
wolfSSL | 15:117db924cf7c | 7221 | void FreeTrustedPeer(TrustedPeerCert* tp, void* heap) |
wolfSSL | 15:117db924cf7c | 7222 | { |
wolfSSL | 15:117db924cf7c | 7223 | if (tp == NULL) { |
wolfSSL | 15:117db924cf7c | 7224 | return; |
wolfSSL | 15:117db924cf7c | 7225 | } |
wolfSSL | 15:117db924cf7c | 7226 | |
wolfSSL | 15:117db924cf7c | 7227 | if (tp->name) { |
wolfSSL | 15:117db924cf7c | 7228 | XFREE(tp->name, heap, DYNAMIC_TYPE_SUBJECT_CN); |
wolfSSL | 15:117db924cf7c | 7229 | } |
wolfSSL | 15:117db924cf7c | 7230 | |
wolfSSL | 15:117db924cf7c | 7231 | if (tp->sig) { |
wolfSSL | 15:117db924cf7c | 7232 | XFREE(tp->sig, heap, DYNAMIC_TYPE_SIGNATURE); |
wolfSSL | 15:117db924cf7c | 7233 | } |
wolfSSL | 15:117db924cf7c | 7234 | #ifndef IGNORE_NAME_CONSTRAINTS |
wolfSSL | 15:117db924cf7c | 7235 | if (tp->permittedNames) |
wolfSSL | 15:117db924cf7c | 7236 | FreeNameSubtrees(tp->permittedNames, heap); |
wolfSSL | 15:117db924cf7c | 7237 | if (tp->excludedNames) |
wolfSSL | 15:117db924cf7c | 7238 | FreeNameSubtrees(tp->excludedNames, heap); |
wolfSSL | 15:117db924cf7c | 7239 | #endif |
wolfSSL | 15:117db924cf7c | 7240 | XFREE(tp, heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 7241 | |
wolfSSL | 15:117db924cf7c | 7242 | (void)heap; |
wolfSSL | 15:117db924cf7c | 7243 | } |
wolfSSL | 15:117db924cf7c | 7244 | |
wolfSSL | 15:117db924cf7c | 7245 | /* Free the whole Trusted Peer linked list */ |
wolfSSL | 15:117db924cf7c | 7246 | void FreeTrustedPeerTable(TrustedPeerCert** table, int rows, void* heap) |
wolfSSL | 15:117db924cf7c | 7247 | { |
wolfSSL | 15:117db924cf7c | 7248 | int i; |
wolfSSL | 15:117db924cf7c | 7249 | |
wolfSSL | 15:117db924cf7c | 7250 | for (i = 0; i < rows; i++) { |
wolfSSL | 15:117db924cf7c | 7251 | TrustedPeerCert* tp = table[i]; |
wolfSSL | 15:117db924cf7c | 7252 | while (tp) { |
wolfSSL | 15:117db924cf7c | 7253 | TrustedPeerCert* next = tp->next; |
wolfSSL | 15:117db924cf7c | 7254 | FreeTrustedPeer(tp, heap); |
wolfSSL | 15:117db924cf7c | 7255 | tp = next; |
wolfSSL | 15:117db924cf7c | 7256 | } |
wolfSSL | 15:117db924cf7c | 7257 | table[i] = NULL; |
wolfSSL | 15:117db924cf7c | 7258 | } |
wolfSSL | 15:117db924cf7c | 7259 | } |
wolfSSL | 15:117db924cf7c | 7260 | #endif /* WOLFSSL_TRUST_PEER_CERT */ |
wolfSSL | 15:117db924cf7c | 7261 | |
wolfSSL | 15:117db924cf7c | 7262 | WOLFSSL_LOCAL int SetMyVersion(word32 version, byte* output, int header) |
wolfSSL | 15:117db924cf7c | 7263 | { |
wolfSSL | 15:117db924cf7c | 7264 | int i = 0; |
wolfSSL | 15:117db924cf7c | 7265 | |
wolfSSL | 15:117db924cf7c | 7266 | if (output == NULL) |
wolfSSL | 15:117db924cf7c | 7267 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7268 | |
wolfSSL | 15:117db924cf7c | 7269 | if (header) { |
wolfSSL | 15:117db924cf7c | 7270 | output[i++] = ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED; |
wolfSSL | 15:117db924cf7c | 7271 | output[i++] = 3; |
wolfSSL | 15:117db924cf7c | 7272 | } |
wolfSSL | 15:117db924cf7c | 7273 | output[i++] = ASN_INTEGER; |
wolfSSL | 15:117db924cf7c | 7274 | output[i++] = 0x01; |
wolfSSL | 15:117db924cf7c | 7275 | output[i++] = (byte)version; |
wolfSSL | 15:117db924cf7c | 7276 | |
wolfSSL | 15:117db924cf7c | 7277 | return i; |
wolfSSL | 15:117db924cf7c | 7278 | } |
wolfSSL | 15:117db924cf7c | 7279 | |
wolfSSL | 15:117db924cf7c | 7280 | |
wolfSSL | 15:117db924cf7c | 7281 | WOLFSSL_LOCAL int SetSerialNumber(const byte* sn, word32 snSz, byte* output, |
wolfSSL | 15:117db924cf7c | 7282 | int maxSnSz) |
wolfSSL | 15:117db924cf7c | 7283 | { |
wolfSSL | 15:117db924cf7c | 7284 | int i = 0; |
wolfSSL | 15:117db924cf7c | 7285 | int snSzInt = (int)snSz; |
wolfSSL | 15:117db924cf7c | 7286 | |
wolfSSL | 15:117db924cf7c | 7287 | if (sn == NULL || output == NULL || snSzInt < 0) |
wolfSSL | 15:117db924cf7c | 7288 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7289 | |
wolfSSL | 15:117db924cf7c | 7290 | /* remove leading zeros */ |
wolfSSL | 15:117db924cf7c | 7291 | while (snSzInt > 0 && sn[0] == 0) { |
wolfSSL | 15:117db924cf7c | 7292 | snSzInt--; |
wolfSSL | 15:117db924cf7c | 7293 | sn++; |
wolfSSL | 15:117db924cf7c | 7294 | } |
wolfSSL | 15:117db924cf7c | 7295 | |
wolfSSL | 15:117db924cf7c | 7296 | /* truncate if input is too long */ |
wolfSSL | 15:117db924cf7c | 7297 | if (snSzInt > maxSnSz) |
wolfSSL | 15:117db924cf7c | 7298 | snSzInt = maxSnSz; |
wolfSSL | 15:117db924cf7c | 7299 | |
wolfSSL | 15:117db924cf7c | 7300 | /* encode ASN Integer, with length and value */ |
wolfSSL | 15:117db924cf7c | 7301 | output[i++] = ASN_INTEGER; |
wolfSSL | 15:117db924cf7c | 7302 | |
wolfSSL | 15:117db924cf7c | 7303 | /* handle MSB, to make sure value is positive */ |
wolfSSL | 15:117db924cf7c | 7304 | if (sn[0] & 0x80) { |
wolfSSL | 15:117db924cf7c | 7305 | /* make room for zero pad */ |
wolfSSL | 15:117db924cf7c | 7306 | if (snSzInt > maxSnSz-1) |
wolfSSL | 15:117db924cf7c | 7307 | snSzInt = maxSnSz-1; |
wolfSSL | 15:117db924cf7c | 7308 | |
wolfSSL | 15:117db924cf7c | 7309 | /* add zero pad */ |
wolfSSL | 15:117db924cf7c | 7310 | i += SetLength(snSzInt+1, &output[i]); |
wolfSSL | 15:117db924cf7c | 7311 | output[i++] = 0x00; |
wolfSSL | 15:117db924cf7c | 7312 | XMEMCPY(&output[i], sn, snSzInt); |
wolfSSL | 15:117db924cf7c | 7313 | } |
wolfSSL | 15:117db924cf7c | 7314 | else { |
wolfSSL | 15:117db924cf7c | 7315 | i += SetLength(snSzInt, &output[i]); |
wolfSSL | 15:117db924cf7c | 7316 | XMEMCPY(&output[i], sn, snSzInt); |
wolfSSL | 15:117db924cf7c | 7317 | } |
wolfSSL | 15:117db924cf7c | 7318 | |
wolfSSL | 15:117db924cf7c | 7319 | /* compute final length */ |
wolfSSL | 15:117db924cf7c | 7320 | i += snSzInt; |
wolfSSL | 15:117db924cf7c | 7321 | |
wolfSSL | 15:117db924cf7c | 7322 | return i; |
wolfSSL | 15:117db924cf7c | 7323 | } |
wolfSSL | 15:117db924cf7c | 7324 | |
wolfSSL | 15:117db924cf7c | 7325 | WOLFSSL_LOCAL int GetSerialNumber(const byte* input, word32* inOutIdx, |
wolfSSL | 15:117db924cf7c | 7326 | byte* serial, int* serialSz, word32 maxIdx) |
wolfSSL | 15:117db924cf7c | 7327 | { |
wolfSSL | 15:117db924cf7c | 7328 | int result = 0; |
wolfSSL | 15:117db924cf7c | 7329 | int ret; |
wolfSSL | 15:117db924cf7c | 7330 | |
wolfSSL | 15:117db924cf7c | 7331 | WOLFSSL_ENTER("GetSerialNumber"); |
wolfSSL | 15:117db924cf7c | 7332 | |
wolfSSL | 15:117db924cf7c | 7333 | if (serial == NULL || input == NULL || serialSz == NULL) { |
wolfSSL | 15:117db924cf7c | 7334 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7335 | } |
wolfSSL | 15:117db924cf7c | 7336 | |
wolfSSL | 15:117db924cf7c | 7337 | /* First byte is ASN type */ |
wolfSSL | 15:117db924cf7c | 7338 | if ((*inOutIdx+1) > maxIdx) { |
wolfSSL | 15:117db924cf7c | 7339 | WOLFSSL_MSG("Bad idx first"); |
wolfSSL | 15:117db924cf7c | 7340 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7341 | } |
wolfSSL | 15:117db924cf7c | 7342 | |
wolfSSL | 15:117db924cf7c | 7343 | ret = GetASNInt(input, inOutIdx, serialSz, maxIdx); |
wolfSSL | 15:117db924cf7c | 7344 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 7345 | return ret; |
wolfSSL | 15:117db924cf7c | 7346 | |
wolfSSL | 15:117db924cf7c | 7347 | if (*serialSz > EXTERNAL_SERIAL_SIZE) { |
wolfSSL | 15:117db924cf7c | 7348 | WOLFSSL_MSG("Serial size bad"); |
wolfSSL | 15:117db924cf7c | 7349 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 7350 | } |
wolfSSL | 15:117db924cf7c | 7351 | |
wolfSSL | 15:117db924cf7c | 7352 | /* return serial */ |
wolfSSL | 15:117db924cf7c | 7353 | XMEMCPY(serial, &input[*inOutIdx], *serialSz); |
wolfSSL | 15:117db924cf7c | 7354 | *inOutIdx += *serialSz; |
wolfSSL | 15:117db924cf7c | 7355 | |
wolfSSL | 15:117db924cf7c | 7356 | return result; |
wolfSSL | 15:117db924cf7c | 7357 | } |
wolfSSL | 15:117db924cf7c | 7358 | |
wolfSSL | 15:117db924cf7c | 7359 | |
wolfSSL | 15:117db924cf7c | 7360 | int AllocDer(DerBuffer** pDer, word32 length, int type, void* heap) |
wolfSSL | 15:117db924cf7c | 7361 | { |
wolfSSL | 15:117db924cf7c | 7362 | int ret = BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7363 | if (pDer) { |
wolfSSL | 15:117db924cf7c | 7364 | int dynType = 0; |
wolfSSL | 15:117db924cf7c | 7365 | DerBuffer* der; |
wolfSSL | 15:117db924cf7c | 7366 | |
wolfSSL | 15:117db924cf7c | 7367 | /* Determine dynamic type */ |
wolfSSL | 15:117db924cf7c | 7368 | switch (type) { |
wolfSSL | 15:117db924cf7c | 7369 | case CA_TYPE: dynType = DYNAMIC_TYPE_CA; break; |
wolfSSL | 15:117db924cf7c | 7370 | case CERT_TYPE: dynType = DYNAMIC_TYPE_CERT; break; |
wolfSSL | 15:117db924cf7c | 7371 | case CRL_TYPE: dynType = DYNAMIC_TYPE_CRL; break; |
wolfSSL | 15:117db924cf7c | 7372 | case DSA_TYPE: dynType = DYNAMIC_TYPE_DSA; break; |
wolfSSL | 15:117db924cf7c | 7373 | case ECC_TYPE: dynType = DYNAMIC_TYPE_ECC; break; |
wolfSSL | 15:117db924cf7c | 7374 | case RSA_TYPE: dynType = DYNAMIC_TYPE_RSA; break; |
wolfSSL | 15:117db924cf7c | 7375 | default: dynType = DYNAMIC_TYPE_KEY; break; |
wolfSSL | 15:117db924cf7c | 7376 | } |
wolfSSL | 15:117db924cf7c | 7377 | |
wolfSSL | 15:117db924cf7c | 7378 | /* Setup new buffer */ |
wolfSSL | 15:117db924cf7c | 7379 | *pDer = (DerBuffer*)XMALLOC(sizeof(DerBuffer) + length, heap, dynType); |
wolfSSL | 15:117db924cf7c | 7380 | if (*pDer == NULL) { |
wolfSSL | 15:117db924cf7c | 7381 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 7382 | } |
wolfSSL | 15:117db924cf7c | 7383 | XMEMSET(*pDer, 0, sizeof(DerBuffer) + length); |
wolfSSL | 15:117db924cf7c | 7384 | |
wolfSSL | 15:117db924cf7c | 7385 | der = *pDer; |
wolfSSL | 15:117db924cf7c | 7386 | der->type = type; |
wolfSSL | 15:117db924cf7c | 7387 | der->dynType = dynType; /* Cache this for FreeDer */ |
wolfSSL | 15:117db924cf7c | 7388 | der->heap = heap; |
wolfSSL | 15:117db924cf7c | 7389 | der->buffer = (byte*)der + sizeof(DerBuffer); |
wolfSSL | 15:117db924cf7c | 7390 | der->length = length; |
wolfSSL | 15:117db924cf7c | 7391 | ret = 0; /* Success */ |
wolfSSL | 15:117db924cf7c | 7392 | } |
wolfSSL | 15:117db924cf7c | 7393 | return ret; |
wolfSSL | 15:117db924cf7c | 7394 | } |
wolfSSL | 15:117db924cf7c | 7395 | |
wolfSSL | 15:117db924cf7c | 7396 | void FreeDer(DerBuffer** pDer) |
wolfSSL | 15:117db924cf7c | 7397 | { |
wolfSSL | 15:117db924cf7c | 7398 | if (pDer && *pDer) |
wolfSSL | 15:117db924cf7c | 7399 | { |
wolfSSL | 15:117db924cf7c | 7400 | DerBuffer* der = (DerBuffer*)*pDer; |
wolfSSL | 15:117db924cf7c | 7401 | |
wolfSSL | 15:117db924cf7c | 7402 | /* ForceZero private keys */ |
wolfSSL | 15:117db924cf7c | 7403 | if (der->type == PRIVATEKEY_TYPE) { |
wolfSSL | 15:117db924cf7c | 7404 | ForceZero(der->buffer, der->length); |
wolfSSL | 15:117db924cf7c | 7405 | } |
wolfSSL | 15:117db924cf7c | 7406 | der->buffer = NULL; |
wolfSSL | 15:117db924cf7c | 7407 | der->length = 0; |
wolfSSL | 15:117db924cf7c | 7408 | XFREE(der, der->heap, der->dynType); |
wolfSSL | 15:117db924cf7c | 7409 | |
wolfSSL | 15:117db924cf7c | 7410 | *pDer = NULL; |
wolfSSL | 15:117db924cf7c | 7411 | } |
wolfSSL | 15:117db924cf7c | 7412 | } |
wolfSSL | 15:117db924cf7c | 7413 | |
wolfSSL | 15:117db924cf7c | 7414 | |
wolfSSL | 15:117db924cf7c | 7415 | #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM) |
wolfSSL | 15:117db924cf7c | 7416 | |
wolfSSL | 15:117db924cf7c | 7417 | /* Max X509 header length indicates the max length + 2 ('\n', '\0') */ |
wolfSSL | 15:117db924cf7c | 7418 | #define MAX_X509_HEADER_SZ (37 + 2) |
wolfSSL | 15:117db924cf7c | 7419 | |
wolfSSL | 15:117db924cf7c | 7420 | const char* const BEGIN_CERT = "-----BEGIN CERTIFICATE-----"; |
wolfSSL | 15:117db924cf7c | 7421 | const char* const END_CERT = "-----END CERTIFICATE-----"; |
wolfSSL | 15:117db924cf7c | 7422 | #ifdef WOLFSSL_CERT_REQ |
wolfSSL | 15:117db924cf7c | 7423 | const char* const BEGIN_CERT_REQ = "-----BEGIN CERTIFICATE REQUEST-----"; |
wolfSSL | 15:117db924cf7c | 7424 | const char* const END_CERT_REQ = "-----END CERTIFICATE REQUEST-----"; |
wolfSSL | 15:117db924cf7c | 7425 | #endif |
wolfSSL | 15:117db924cf7c | 7426 | #ifndef NO_DH |
wolfSSL | 15:117db924cf7c | 7427 | const char* const BEGIN_DH_PARAM = "-----BEGIN DH PARAMETERS-----"; |
wolfSSL | 15:117db924cf7c | 7428 | const char* const END_DH_PARAM = "-----END DH PARAMETERS-----"; |
wolfSSL | 15:117db924cf7c | 7429 | #endif |
wolfSSL | 15:117db924cf7c | 7430 | #ifndef NO_DSA |
wolfSSL | 15:117db924cf7c | 7431 | const char* const BEGIN_DSA_PARAM = "-----BEGIN DSA PARAMETERS-----"; |
wolfSSL | 15:117db924cf7c | 7432 | const char* const END_DSA_PARAM = "-----END DSA PARAMETERS-----"; |
wolfSSL | 15:117db924cf7c | 7433 | #endif |
wolfSSL | 15:117db924cf7c | 7434 | const char* const BEGIN_X509_CRL = "-----BEGIN X509 CRL-----"; |
wolfSSL | 15:117db924cf7c | 7435 | const char* const END_X509_CRL = "-----END X509 CRL-----"; |
wolfSSL | 15:117db924cf7c | 7436 | const char* const BEGIN_RSA_PRIV = "-----BEGIN RSA PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7437 | const char* const END_RSA_PRIV = "-----END RSA PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7438 | const char* const BEGIN_PRIV_KEY = "-----BEGIN PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7439 | const char* const END_PRIV_KEY = "-----END PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7440 | const char* const BEGIN_ENC_PRIV_KEY = "-----BEGIN ENCRYPTED PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7441 | const char* const END_ENC_PRIV_KEY = "-----END ENCRYPTED PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7442 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 7443 | const char* const BEGIN_EC_PRIV = "-----BEGIN EC PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7444 | const char* const END_EC_PRIV = "-----END EC PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7445 | #endif |
wolfSSL | 15:117db924cf7c | 7446 | #if defined(HAVE_ECC) || defined(HAVE_ED25519) || !defined(NO_DSA) |
wolfSSL | 15:117db924cf7c | 7447 | const char* const BEGIN_DSA_PRIV = "-----BEGIN DSA PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7448 | const char* const END_DSA_PRIV = "-----END DSA PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7449 | #endif |
wolfSSL | 15:117db924cf7c | 7450 | const char* const BEGIN_PUB_KEY = "-----BEGIN PUBLIC KEY-----"; |
wolfSSL | 15:117db924cf7c | 7451 | const char* const END_PUB_KEY = "-----END PUBLIC KEY-----"; |
wolfSSL | 15:117db924cf7c | 7452 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 7453 | const char* const BEGIN_EDDSA_PRIV = "-----BEGIN EDDSA PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7454 | const char* const END_EDDSA_PRIV = "-----END EDDSA PRIVATE KEY-----"; |
wolfSSL | 15:117db924cf7c | 7455 | #endif |
wolfSSL | 15:117db924cf7c | 7456 | #ifdef HAVE_CRL |
wolfSSL | 15:117db924cf7c | 7457 | const char *const BEGIN_CRL = "-----BEGIN X509 CRL-----"; |
wolfSSL | 15:117db924cf7c | 7458 | const char* const END_CRL = "-----END X509 CRL-----"; |
wolfSSL | 15:117db924cf7c | 7459 | #endif |
wolfSSL | 15:117db924cf7c | 7460 | |
wolfSSL | 15:117db924cf7c | 7461 | |
wolfSSL | 15:117db924cf7c | 7462 | |
wolfSSL | 15:117db924cf7c | 7463 | int wc_PemGetHeaderFooter(int type, const char** header, const char** footer) |
wolfSSL | 15:117db924cf7c | 7464 | { |
wolfSSL | 15:117db924cf7c | 7465 | int ret = BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7466 | |
wolfSSL | 15:117db924cf7c | 7467 | switch (type) { |
wolfSSL | 15:117db924cf7c | 7468 | case CA_TYPE: /* same as below */ |
wolfSSL | 15:117db924cf7c | 7469 | case TRUSTED_PEER_TYPE: |
wolfSSL | 15:117db924cf7c | 7470 | case CERT_TYPE: |
wolfSSL | 15:117db924cf7c | 7471 | if (header) *header = BEGIN_CERT; |
wolfSSL | 15:117db924cf7c | 7472 | if (footer) *footer = END_CERT; |
wolfSSL | 15:117db924cf7c | 7473 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7474 | break; |
wolfSSL | 15:117db924cf7c | 7475 | |
wolfSSL | 15:117db924cf7c | 7476 | case CRL_TYPE: |
wolfSSL | 15:117db924cf7c | 7477 | if (header) *header = BEGIN_X509_CRL; |
wolfSSL | 15:117db924cf7c | 7478 | if (footer) *footer = END_X509_CRL; |
wolfSSL | 15:117db924cf7c | 7479 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7480 | break; |
wolfSSL | 15:117db924cf7c | 7481 | #ifndef NO_DH |
wolfSSL | 15:117db924cf7c | 7482 | case DH_PARAM_TYPE: |
wolfSSL | 15:117db924cf7c | 7483 | if (header) *header = BEGIN_DH_PARAM; |
wolfSSL | 15:117db924cf7c | 7484 | if (footer) *footer = END_DH_PARAM; |
wolfSSL | 15:117db924cf7c | 7485 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7486 | break; |
wolfSSL | 15:117db924cf7c | 7487 | #endif |
wolfSSL | 15:117db924cf7c | 7488 | #ifndef NO_DSA |
wolfSSL | 15:117db924cf7c | 7489 | case DSA_PARAM_TYPE: |
wolfSSL | 15:117db924cf7c | 7490 | if (header) *header = BEGIN_DSA_PARAM; |
wolfSSL | 15:117db924cf7c | 7491 | if (footer) *footer = END_DSA_PARAM; |
wolfSSL | 15:117db924cf7c | 7492 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7493 | break; |
wolfSSL | 15:117db924cf7c | 7494 | #endif |
wolfSSL | 15:117db924cf7c | 7495 | #ifdef WOLFSSL_CERT_REQ |
wolfSSL | 15:117db924cf7c | 7496 | case CERTREQ_TYPE: |
wolfSSL | 15:117db924cf7c | 7497 | if (header) *header = BEGIN_CERT_REQ; |
wolfSSL | 15:117db924cf7c | 7498 | if (footer) *footer = END_CERT_REQ; |
wolfSSL | 15:117db924cf7c | 7499 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7500 | break; |
wolfSSL | 15:117db924cf7c | 7501 | #endif |
wolfSSL | 15:117db924cf7c | 7502 | #ifndef NO_DSA |
wolfSSL | 15:117db924cf7c | 7503 | case DSA_TYPE: |
wolfSSL | 15:117db924cf7c | 7504 | case DSA_PRIVATEKEY_TYPE: |
wolfSSL | 15:117db924cf7c | 7505 | if (header) *header = BEGIN_DSA_PRIV; |
wolfSSL | 15:117db924cf7c | 7506 | if (footer) *footer = END_DSA_PRIV; |
wolfSSL | 15:117db924cf7c | 7507 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7508 | break; |
wolfSSL | 15:117db924cf7c | 7509 | #endif |
wolfSSL | 15:117db924cf7c | 7510 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 7511 | case ECC_TYPE: |
wolfSSL | 15:117db924cf7c | 7512 | case ECC_PRIVATEKEY_TYPE: |
wolfSSL | 15:117db924cf7c | 7513 | if (header) *header = BEGIN_EC_PRIV; |
wolfSSL | 15:117db924cf7c | 7514 | if (footer) *footer = END_EC_PRIV; |
wolfSSL | 15:117db924cf7c | 7515 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7516 | break; |
wolfSSL | 15:117db924cf7c | 7517 | #endif |
wolfSSL | 15:117db924cf7c | 7518 | case RSA_TYPE: |
wolfSSL | 15:117db924cf7c | 7519 | case PRIVATEKEY_TYPE: |
wolfSSL | 15:117db924cf7c | 7520 | if (header) *header = BEGIN_RSA_PRIV; |
wolfSSL | 15:117db924cf7c | 7521 | if (footer) *footer = END_RSA_PRIV; |
wolfSSL | 15:117db924cf7c | 7522 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7523 | break; |
wolfSSL | 15:117db924cf7c | 7524 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 7525 | case ED25519_TYPE: |
wolfSSL | 15:117db924cf7c | 7526 | case EDDSA_PRIVATEKEY_TYPE: |
wolfSSL | 15:117db924cf7c | 7527 | if (header) *header = BEGIN_EDDSA_PRIV; |
wolfSSL | 15:117db924cf7c | 7528 | if (footer) *footer = END_EDDSA_PRIV; |
wolfSSL | 15:117db924cf7c | 7529 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7530 | break; |
wolfSSL | 15:117db924cf7c | 7531 | #endif |
wolfSSL | 15:117db924cf7c | 7532 | case PUBLICKEY_TYPE: |
wolfSSL | 15:117db924cf7c | 7533 | if (header) *header = BEGIN_PUB_KEY; |
wolfSSL | 15:117db924cf7c | 7534 | if (footer) *footer = END_PUB_KEY; |
wolfSSL | 15:117db924cf7c | 7535 | ret = 0; |
wolfSSL | 15:117db924cf7c | 7536 | break; |
wolfSSL | 15:117db924cf7c | 7537 | default: |
wolfSSL | 15:117db924cf7c | 7538 | break; |
wolfSSL | 15:117db924cf7c | 7539 | } |
wolfSSL | 15:117db924cf7c | 7540 | return ret; |
wolfSSL | 15:117db924cf7c | 7541 | } |
wolfSSL | 15:117db924cf7c | 7542 | |
wolfSSL | 15:117db924cf7c | 7543 | #ifdef WOLFSSL_ENCRYPTED_KEYS |
wolfSSL | 15:117db924cf7c | 7544 | |
wolfSSL | 15:117db924cf7c | 7545 | static const char* const kProcTypeHeader = "Proc-Type"; |
wolfSSL | 15:117db924cf7c | 7546 | static const char* const kDecInfoHeader = "DEK-Info"; |
wolfSSL | 15:117db924cf7c | 7547 | |
wolfSSL | 15:117db924cf7c | 7548 | #ifdef WOLFSSL_PEM_TO_DER |
wolfSSL | 15:117db924cf7c | 7549 | #ifndef NO_DES3 |
wolfSSL | 15:117db924cf7c | 7550 | static const char* const kEncTypeDes = "DES-CBC"; |
wolfSSL | 15:117db924cf7c | 7551 | static const char* const kEncTypeDes3 = "DES-EDE3-CBC"; |
wolfSSL | 15:117db924cf7c | 7552 | #endif |
wolfSSL | 15:117db924cf7c | 7553 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) |
wolfSSL | 15:117db924cf7c | 7554 | static const char* const kEncTypeAesCbc128 = "AES-128-CBC"; |
wolfSSL | 15:117db924cf7c | 7555 | #endif |
wolfSSL | 15:117db924cf7c | 7556 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_192) |
wolfSSL | 15:117db924cf7c | 7557 | static const char* const kEncTypeAesCbc192 = "AES-192-CBC"; |
wolfSSL | 15:117db924cf7c | 7558 | #endif |
wolfSSL | 15:117db924cf7c | 7559 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) |
wolfSSL | 15:117db924cf7c | 7560 | static const char* const kEncTypeAesCbc256 = "AES-256-CBC"; |
wolfSSL | 15:117db924cf7c | 7561 | #endif |
wolfSSL | 15:117db924cf7c | 7562 | |
wolfSSL | 15:117db924cf7c | 7563 | int wc_EncryptedInfoGet(EncryptedInfo* info, const char* cipherInfo) |
wolfSSL | 15:117db924cf7c | 7564 | { |
wolfSSL | 15:117db924cf7c | 7565 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 7566 | |
wolfSSL | 15:117db924cf7c | 7567 | if (info == NULL || cipherInfo == NULL) |
wolfSSL | 15:117db924cf7c | 7568 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7569 | |
wolfSSL | 15:117db924cf7c | 7570 | /* determine cipher information */ |
wolfSSL | 15:117db924cf7c | 7571 | #ifndef NO_DES3 |
wolfSSL | 15:117db924cf7c | 7572 | if (XSTRNCMP(cipherInfo, kEncTypeDes, XSTRLEN(kEncTypeDes)) == 0) { |
wolfSSL | 15:117db924cf7c | 7573 | info->cipherType = WC_CIPHER_DES; |
wolfSSL | 15:117db924cf7c | 7574 | info->keySz = DES_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 7575 | if (info->ivSz == 0) info->ivSz = DES_IV_SIZE; |
wolfSSL | 15:117db924cf7c | 7576 | } |
wolfSSL | 15:117db924cf7c | 7577 | else if (XSTRNCMP(cipherInfo, kEncTypeDes3, XSTRLEN(kEncTypeDes3)) == 0) { |
wolfSSL | 15:117db924cf7c | 7578 | info->cipherType = WC_CIPHER_DES3; |
wolfSSL | 15:117db924cf7c | 7579 | info->keySz = DES3_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 7580 | if (info->ivSz == 0) info->ivSz = DES_IV_SIZE; |
wolfSSL | 15:117db924cf7c | 7581 | } |
wolfSSL | 15:117db924cf7c | 7582 | else |
wolfSSL | 15:117db924cf7c | 7583 | #endif /* !NO_DES3 */ |
wolfSSL | 15:117db924cf7c | 7584 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128) |
wolfSSL | 15:117db924cf7c | 7585 | if (XSTRNCMP(cipherInfo, kEncTypeAesCbc128, XSTRLEN(kEncTypeAesCbc128)) == 0) { |
wolfSSL | 15:117db924cf7c | 7586 | info->cipherType = WC_CIPHER_AES_CBC; |
wolfSSL | 15:117db924cf7c | 7587 | info->keySz = AES_128_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 7588 | if (info->ivSz == 0) info->ivSz = AES_IV_SIZE; |
wolfSSL | 15:117db924cf7c | 7589 | } |
wolfSSL | 15:117db924cf7c | 7590 | else |
wolfSSL | 15:117db924cf7c | 7591 | #endif |
wolfSSL | 15:117db924cf7c | 7592 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_192) |
wolfSSL | 15:117db924cf7c | 7593 | if (XSTRNCMP(cipherInfo, kEncTypeAesCbc192, XSTRLEN(kEncTypeAesCbc192)) == 0) { |
wolfSSL | 15:117db924cf7c | 7594 | info->cipherType = WC_CIPHER_AES_CBC; |
wolfSSL | 15:117db924cf7c | 7595 | info->keySz = AES_192_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 7596 | if (info->ivSz == 0) info->ivSz = AES_IV_SIZE; |
wolfSSL | 15:117db924cf7c | 7597 | } |
wolfSSL | 15:117db924cf7c | 7598 | else |
wolfSSL | 15:117db924cf7c | 7599 | #endif |
wolfSSL | 15:117db924cf7c | 7600 | #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256) |
wolfSSL | 15:117db924cf7c | 7601 | if (XSTRNCMP(cipherInfo, kEncTypeAesCbc256, XSTRLEN(kEncTypeAesCbc256)) == 0) { |
wolfSSL | 15:117db924cf7c | 7602 | info->cipherType = WC_CIPHER_AES_CBC; |
wolfSSL | 15:117db924cf7c | 7603 | info->keySz = AES_256_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 7604 | if (info->ivSz == 0) info->ivSz = AES_IV_SIZE; |
wolfSSL | 15:117db924cf7c | 7605 | } |
wolfSSL | 15:117db924cf7c | 7606 | else |
wolfSSL | 15:117db924cf7c | 7607 | #endif |
wolfSSL | 15:117db924cf7c | 7608 | { |
wolfSSL | 15:117db924cf7c | 7609 | ret = NOT_COMPILED_IN; |
wolfSSL | 15:117db924cf7c | 7610 | } |
wolfSSL | 15:117db924cf7c | 7611 | return ret; |
wolfSSL | 15:117db924cf7c | 7612 | } |
wolfSSL | 15:117db924cf7c | 7613 | |
wolfSSL | 15:117db924cf7c | 7614 | static int wc_EncryptedInfoParse(EncryptedInfo* info, |
wolfSSL | 15:117db924cf7c | 7615 | char** pBuffer, size_t bufSz) |
wolfSSL | 15:117db924cf7c | 7616 | { |
wolfSSL | 15:117db924cf7c | 7617 | int err = 0; |
wolfSSL | 15:117db924cf7c | 7618 | char* bufferStart; |
wolfSSL | 15:117db924cf7c | 7619 | char* bufferEnd; |
wolfSSL | 15:117db924cf7c | 7620 | char* line; |
wolfSSL | 15:117db924cf7c | 7621 | word32 lineSz; |
wolfSSL | 15:117db924cf7c | 7622 | char* finish; |
wolfSSL | 15:117db924cf7c | 7623 | word32 finishSz; |
wolfSSL | 15:117db924cf7c | 7624 | char* start = NULL; |
wolfSSL | 15:117db924cf7c | 7625 | word32 startSz; |
wolfSSL | 15:117db924cf7c | 7626 | char* newline = NULL; |
wolfSSL | 15:117db924cf7c | 7627 | |
wolfSSL | 15:117db924cf7c | 7628 | if (info == NULL || pBuffer == NULL || bufSz == 0) |
wolfSSL | 15:117db924cf7c | 7629 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7630 | |
wolfSSL | 15:117db924cf7c | 7631 | bufferStart = *pBuffer; |
wolfSSL | 15:117db924cf7c | 7632 | bufferEnd = bufferStart + bufSz; |
wolfSSL | 15:117db924cf7c | 7633 | |
wolfSSL | 15:117db924cf7c | 7634 | /* find encrypted info marker */ |
wolfSSL | 15:117db924cf7c | 7635 | line = XSTRNSTR(bufferStart, kProcTypeHeader, |
wolfSSL | 15:117db924cf7c | 7636 | min((word32)bufSz, PEM_LINE_LEN)); |
wolfSSL | 15:117db924cf7c | 7637 | if (line != NULL) { |
wolfSSL | 15:117db924cf7c | 7638 | if (line >= bufferEnd) { |
wolfSSL | 15:117db924cf7c | 7639 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7640 | } |
wolfSSL | 15:117db924cf7c | 7641 | |
wolfSSL | 15:117db924cf7c | 7642 | lineSz = (word32)(bufferEnd - line); |
wolfSSL | 15:117db924cf7c | 7643 | |
wolfSSL | 15:117db924cf7c | 7644 | /* find DEC-Info marker */ |
wolfSSL | 15:117db924cf7c | 7645 | start = XSTRNSTR(line, kDecInfoHeader, min(lineSz, PEM_LINE_LEN)); |
wolfSSL | 15:117db924cf7c | 7646 | |
wolfSSL | 15:117db924cf7c | 7647 | if (start == NULL) |
wolfSSL | 15:117db924cf7c | 7648 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7649 | |
wolfSSL | 15:117db924cf7c | 7650 | /* skip dec-info and ": " */ |
wolfSSL | 15:117db924cf7c | 7651 | start += XSTRLEN(kDecInfoHeader); |
wolfSSL | 15:117db924cf7c | 7652 | if (start >= bufferEnd) |
wolfSSL | 15:117db924cf7c | 7653 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7654 | |
wolfSSL | 15:117db924cf7c | 7655 | if (start[0] == ':') { |
wolfSSL | 15:117db924cf7c | 7656 | start++; |
wolfSSL | 15:117db924cf7c | 7657 | if (start >= bufferEnd) |
wolfSSL | 15:117db924cf7c | 7658 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7659 | } |
wolfSSL | 15:117db924cf7c | 7660 | if (start[0] == ' ') |
wolfSSL | 15:117db924cf7c | 7661 | start++; |
wolfSSL | 15:117db924cf7c | 7662 | |
wolfSSL | 15:117db924cf7c | 7663 | startSz = (word32)(bufferEnd - start); |
wolfSSL | 15:117db924cf7c | 7664 | finish = XSTRNSTR(start, ",", min(startSz, PEM_LINE_LEN)); |
wolfSSL | 15:117db924cf7c | 7665 | |
wolfSSL | 15:117db924cf7c | 7666 | if ((start != NULL) && (finish != NULL) && (start < finish)) { |
wolfSSL | 15:117db924cf7c | 7667 | if (finish >= bufferEnd) { |
wolfSSL | 15:117db924cf7c | 7668 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7669 | } |
wolfSSL | 15:117db924cf7c | 7670 | |
wolfSSL | 15:117db924cf7c | 7671 | finishSz = (word32)(bufferEnd - finish); |
wolfSSL | 15:117db924cf7c | 7672 | newline = XSTRNSTR(finish, "\r", min(finishSz, PEM_LINE_LEN)); |
wolfSSL | 15:117db924cf7c | 7673 | |
wolfSSL | 15:117db924cf7c | 7674 | /* get cipher name */ |
wolfSSL | 15:117db924cf7c | 7675 | if (NAME_SZ < (finish - start)) /* buffer size of info->name */ |
wolfSSL | 15:117db924cf7c | 7676 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7677 | if (XMEMCPY(info->name, start, finish - start) == NULL) |
wolfSSL | 15:117db924cf7c | 7678 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7679 | info->name[finish - start] = '\0'; /* null term */ |
wolfSSL | 15:117db924cf7c | 7680 | |
wolfSSL | 15:117db924cf7c | 7681 | /* get IV */ |
wolfSSL | 15:117db924cf7c | 7682 | if (finishSz < sizeof(info->iv) + 1) |
wolfSSL | 15:117db924cf7c | 7683 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7684 | if (XMEMCPY(info->iv, finish + 1, sizeof(info->iv)) == NULL) |
wolfSSL | 15:117db924cf7c | 7685 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7686 | |
wolfSSL | 15:117db924cf7c | 7687 | if (newline == NULL) |
wolfSSL | 15:117db924cf7c | 7688 | newline = XSTRNSTR(finish, "\n", min(finishSz, |
wolfSSL | 15:117db924cf7c | 7689 | PEM_LINE_LEN)); |
wolfSSL | 15:117db924cf7c | 7690 | if ((newline != NULL) && (newline > finish)) { |
wolfSSL | 15:117db924cf7c | 7691 | info->ivSz = (word32)(newline - (finish + 1)); |
wolfSSL | 15:117db924cf7c | 7692 | info->set = 1; |
wolfSSL | 15:117db924cf7c | 7693 | } |
wolfSSL | 15:117db924cf7c | 7694 | else |
wolfSSL | 15:117db924cf7c | 7695 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7696 | } |
wolfSSL | 15:117db924cf7c | 7697 | else |
wolfSSL | 15:117db924cf7c | 7698 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7699 | |
wolfSSL | 15:117db924cf7c | 7700 | /* eat blank line */ |
wolfSSL | 15:117db924cf7c | 7701 | while (newline < bufferEnd && |
wolfSSL | 15:117db924cf7c | 7702 | (*newline == '\r' || *newline == '\n')) { |
wolfSSL | 15:117db924cf7c | 7703 | newline++; |
wolfSSL | 15:117db924cf7c | 7704 | } |
wolfSSL | 15:117db924cf7c | 7705 | |
wolfSSL | 15:117db924cf7c | 7706 | /* return new headerEnd */ |
wolfSSL | 15:117db924cf7c | 7707 | if (pBuffer) |
wolfSSL | 15:117db924cf7c | 7708 | *pBuffer = newline; |
wolfSSL | 15:117db924cf7c | 7709 | |
wolfSSL | 15:117db924cf7c | 7710 | /* populate info */ |
wolfSSL | 15:117db924cf7c | 7711 | err = wc_EncryptedInfoGet(info, info->name); |
wolfSSL | 15:117db924cf7c | 7712 | } |
wolfSSL | 15:117db924cf7c | 7713 | |
wolfSSL | 15:117db924cf7c | 7714 | return err; |
wolfSSL | 15:117db924cf7c | 7715 | } |
wolfSSL | 15:117db924cf7c | 7716 | #endif /* WOLFSSL_PEM_TO_DER */ |
wolfSSL | 15:117db924cf7c | 7717 | |
wolfSSL | 15:117db924cf7c | 7718 | #ifdef WOLFSSL_DER_TO_PEM |
wolfSSL | 15:117db924cf7c | 7719 | static int wc_EncryptedInfoAppend(char* dest, char* cipherInfo) |
wolfSSL | 15:117db924cf7c | 7720 | { |
wolfSSL | 15:117db924cf7c | 7721 | if (cipherInfo != NULL) { |
wolfSSL | 15:117db924cf7c | 7722 | size_t cipherInfoStrLen = XSTRLEN(cipherInfo); |
wolfSSL | 15:117db924cf7c | 7723 | if (cipherInfoStrLen > HEADER_ENCRYPTED_KEY_SIZE - (9+14+10+3)) |
wolfSSL | 15:117db924cf7c | 7724 | cipherInfoStrLen = HEADER_ENCRYPTED_KEY_SIZE - (9+14+10+3); |
wolfSSL | 15:117db924cf7c | 7725 | |
wolfSSL | 15:117db924cf7c | 7726 | XSTRNCAT(dest, kProcTypeHeader, 9); |
wolfSSL | 15:117db924cf7c | 7727 | XSTRNCAT(dest, ": 4,ENCRYPTED\n", 14); |
wolfSSL | 15:117db924cf7c | 7728 | XSTRNCAT(dest, kDecInfoHeader, 8); |
wolfSSL | 15:117db924cf7c | 7729 | XSTRNCAT(dest, ": ", 2); |
wolfSSL | 15:117db924cf7c | 7730 | XSTRNCAT(dest, cipherInfo, cipherInfoStrLen); |
wolfSSL | 15:117db924cf7c | 7731 | XSTRNCAT(dest, "\n\n", 3); |
wolfSSL | 15:117db924cf7c | 7732 | } |
wolfSSL | 15:117db924cf7c | 7733 | return 0; |
wolfSSL | 15:117db924cf7c | 7734 | } |
wolfSSL | 15:117db924cf7c | 7735 | #endif /* WOLFSSL_DER_TO_PEM */ |
wolfSSL | 15:117db924cf7c | 7736 | #endif /* WOLFSSL_ENCRYPTED_KEYS */ |
wolfSSL | 15:117db924cf7c | 7737 | |
wolfSSL | 15:117db924cf7c | 7738 | #ifdef WOLFSSL_DER_TO_PEM |
wolfSSL | 15:117db924cf7c | 7739 | |
wolfSSL | 15:117db924cf7c | 7740 | /* Used for compatibility API */ |
wolfSSL | 15:117db924cf7c | 7741 | int wc_DerToPem(const byte* der, word32 derSz, |
wolfSSL | 15:117db924cf7c | 7742 | byte* output, word32 outSz, int type) |
wolfSSL | 15:117db924cf7c | 7743 | { |
wolfSSL | 15:117db924cf7c | 7744 | return wc_DerToPemEx(der, derSz, output, outSz, NULL, type); |
wolfSSL | 15:117db924cf7c | 7745 | } |
wolfSSL | 15:117db924cf7c | 7746 | |
wolfSSL | 15:117db924cf7c | 7747 | /* convert der buffer to pem into output, can't do inplace, der and output |
wolfSSL | 15:117db924cf7c | 7748 | need to be different */ |
wolfSSL | 15:117db924cf7c | 7749 | int wc_DerToPemEx(const byte* der, word32 derSz, byte* output, word32 outSz, |
wolfSSL | 15:117db924cf7c | 7750 | byte *cipher_info, int type) |
wolfSSL | 15:117db924cf7c | 7751 | { |
wolfSSL | 15:117db924cf7c | 7752 | const char* headerStr = NULL; |
wolfSSL | 15:117db924cf7c | 7753 | const char* footerStr = NULL; |
wolfSSL | 15:117db924cf7c | 7754 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7755 | char* header = NULL; |
wolfSSL | 15:117db924cf7c | 7756 | char* footer = NULL; |
wolfSSL | 15:117db924cf7c | 7757 | #else |
wolfSSL | 15:117db924cf7c | 7758 | char header[MAX_X509_HEADER_SZ + HEADER_ENCRYPTED_KEY_SIZE]; |
wolfSSL | 15:117db924cf7c | 7759 | char footer[MAX_X509_HEADER_SZ]; |
wolfSSL | 15:117db924cf7c | 7760 | #endif |
wolfSSL | 15:117db924cf7c | 7761 | int headerLen = MAX_X509_HEADER_SZ + HEADER_ENCRYPTED_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 7762 | int footerLen = MAX_X509_HEADER_SZ; |
wolfSSL | 15:117db924cf7c | 7763 | int i; |
wolfSSL | 15:117db924cf7c | 7764 | int err; |
wolfSSL | 15:117db924cf7c | 7765 | int outLen; /* return length or error */ |
wolfSSL | 15:117db924cf7c | 7766 | |
wolfSSL | 15:117db924cf7c | 7767 | (void)cipher_info; |
wolfSSL | 15:117db924cf7c | 7768 | |
wolfSSL | 15:117db924cf7c | 7769 | if (der == output) /* no in place conversion */ |
wolfSSL | 15:117db924cf7c | 7770 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7771 | |
wolfSSL | 15:117db924cf7c | 7772 | err = wc_PemGetHeaderFooter(type, &headerStr, &footerStr); |
wolfSSL | 15:117db924cf7c | 7773 | if (err != 0) |
wolfSSL | 15:117db924cf7c | 7774 | return err; |
wolfSSL | 15:117db924cf7c | 7775 | |
wolfSSL | 15:117db924cf7c | 7776 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7777 | header = (char*)XMALLOC(headerLen, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7778 | if (header == NULL) |
wolfSSL | 15:117db924cf7c | 7779 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 7780 | |
wolfSSL | 15:117db924cf7c | 7781 | footer = (char*)XMALLOC(footerLen, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7782 | if (footer == NULL) { |
wolfSSL | 15:117db924cf7c | 7783 | XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7784 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 7785 | } |
wolfSSL | 15:117db924cf7c | 7786 | #endif |
wolfSSL | 15:117db924cf7c | 7787 | |
wolfSSL | 15:117db924cf7c | 7788 | /* null term and leave room for newline */ |
wolfSSL | 15:117db924cf7c | 7789 | header[--headerLen] = '\0'; header[--headerLen] = '\0'; |
wolfSSL | 15:117db924cf7c | 7790 | footer[--footerLen] = '\0'; footer[--footerLen] = '\0'; |
wolfSSL | 15:117db924cf7c | 7791 | |
wolfSSL | 15:117db924cf7c | 7792 | /* build header and footer based on type */ |
wolfSSL | 15:117db924cf7c | 7793 | XSTRNCPY(header, headerStr, headerLen); |
wolfSSL | 15:117db924cf7c | 7794 | XSTRNCPY(footer, footerStr, footerLen); |
wolfSSL | 15:117db924cf7c | 7795 | |
wolfSSL | 15:117db924cf7c | 7796 | /* add new line to end */ |
wolfSSL | 15:117db924cf7c | 7797 | XSTRNCAT(header, "\n", 2); |
wolfSSL | 15:117db924cf7c | 7798 | XSTRNCAT(footer, "\n", 2); |
wolfSSL | 15:117db924cf7c | 7799 | |
wolfSSL | 15:117db924cf7c | 7800 | #ifdef WOLFSSL_ENCRYPTED_KEYS |
wolfSSL | 15:117db924cf7c | 7801 | err = wc_EncryptedInfoAppend(header, (char*)cipher_info); |
wolfSSL | 15:117db924cf7c | 7802 | if (err != 0) { |
wolfSSL | 15:117db924cf7c | 7803 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7804 | XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7805 | XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7806 | #endif |
wolfSSL | 15:117db924cf7c | 7807 | return err; |
wolfSSL | 15:117db924cf7c | 7808 | } |
wolfSSL | 15:117db924cf7c | 7809 | #endif |
wolfSSL | 15:117db924cf7c | 7810 | |
wolfSSL | 15:117db924cf7c | 7811 | headerLen = (int)XSTRLEN(header); |
wolfSSL | 15:117db924cf7c | 7812 | footerLen = (int)XSTRLEN(footer); |
wolfSSL | 15:117db924cf7c | 7813 | |
wolfSSL | 15:117db924cf7c | 7814 | /* if null output and 0 size passed in then return size needed */ |
wolfSSL | 15:117db924cf7c | 7815 | if (!output && outSz == 0) { |
wolfSSL | 15:117db924cf7c | 7816 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7817 | XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7818 | XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7819 | #endif |
wolfSSL | 15:117db924cf7c | 7820 | outLen = 0; |
wolfSSL | 15:117db924cf7c | 7821 | if ((err = Base64_Encode(der, derSz, NULL, (word32*)&outLen)) |
wolfSSL | 15:117db924cf7c | 7822 | != LENGTH_ONLY_E) { |
wolfSSL | 15:117db924cf7c | 7823 | return err; |
wolfSSL | 15:117db924cf7c | 7824 | } |
wolfSSL | 15:117db924cf7c | 7825 | return headerLen + footerLen + outLen; |
wolfSSL | 15:117db924cf7c | 7826 | } |
wolfSSL | 15:117db924cf7c | 7827 | |
wolfSSL | 15:117db924cf7c | 7828 | if (!der || !output) { |
wolfSSL | 15:117db924cf7c | 7829 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7830 | XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7831 | XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7832 | #endif |
wolfSSL | 15:117db924cf7c | 7833 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7834 | } |
wolfSSL | 15:117db924cf7c | 7835 | |
wolfSSL | 15:117db924cf7c | 7836 | /* don't even try if outSz too short */ |
wolfSSL | 15:117db924cf7c | 7837 | if (outSz < headerLen + footerLen + derSz) { |
wolfSSL | 15:117db924cf7c | 7838 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7839 | XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7840 | XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7841 | #endif |
wolfSSL | 15:117db924cf7c | 7842 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7843 | } |
wolfSSL | 15:117db924cf7c | 7844 | |
wolfSSL | 15:117db924cf7c | 7845 | /* header */ |
wolfSSL | 15:117db924cf7c | 7846 | XMEMCPY(output, header, headerLen); |
wolfSSL | 15:117db924cf7c | 7847 | i = headerLen; |
wolfSSL | 15:117db924cf7c | 7848 | |
wolfSSL | 15:117db924cf7c | 7849 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7850 | XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7851 | #endif |
wolfSSL | 15:117db924cf7c | 7852 | |
wolfSSL | 15:117db924cf7c | 7853 | /* body */ |
wolfSSL | 15:117db924cf7c | 7854 | outLen = outSz - (headerLen + footerLen); /* input to Base64_Encode */ |
wolfSSL | 15:117db924cf7c | 7855 | if ( (err = Base64_Encode(der, derSz, output + i, (word32*)&outLen)) < 0) { |
wolfSSL | 15:117db924cf7c | 7856 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7857 | XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7858 | #endif |
wolfSSL | 15:117db924cf7c | 7859 | return err; |
wolfSSL | 15:117db924cf7c | 7860 | } |
wolfSSL | 15:117db924cf7c | 7861 | i += outLen; |
wolfSSL | 15:117db924cf7c | 7862 | |
wolfSSL | 15:117db924cf7c | 7863 | /* footer */ |
wolfSSL | 15:117db924cf7c | 7864 | if ( (i + footerLen) > (int)outSz) { |
wolfSSL | 15:117db924cf7c | 7865 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7866 | XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7867 | #endif |
wolfSSL | 15:117db924cf7c | 7868 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 7869 | } |
wolfSSL | 15:117db924cf7c | 7870 | XMEMCPY(output + i, footer, footerLen); |
wolfSSL | 15:117db924cf7c | 7871 | |
wolfSSL | 15:117db924cf7c | 7872 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 7873 | XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 7874 | #endif |
wolfSSL | 15:117db924cf7c | 7875 | |
wolfSSL | 15:117db924cf7c | 7876 | return outLen + headerLen + footerLen; |
wolfSSL | 15:117db924cf7c | 7877 | } |
wolfSSL | 15:117db924cf7c | 7878 | |
wolfSSL | 15:117db924cf7c | 7879 | #endif /* WOLFSSL_DER_TO_PEM */ |
wolfSSL | 15:117db924cf7c | 7880 | |
wolfSSL | 15:117db924cf7c | 7881 | #ifdef WOLFSSL_PEM_TO_DER |
wolfSSL | 15:117db924cf7c | 7882 | |
wolfSSL | 15:117db924cf7c | 7883 | /* Remove PEM header/footer, convert to ASN1, store any encrypted data |
wolfSSL | 15:117db924cf7c | 7884 | info->consumed tracks of PEM bytes consumed in case multiple parts */ |
wolfSSL | 15:117db924cf7c | 7885 | int PemToDer(const unsigned char* buff, long longSz, int type, |
wolfSSL | 15:117db924cf7c | 7886 | DerBuffer** pDer, void* heap, EncryptedInfo* info, int* eccKey) |
wolfSSL | 15:117db924cf7c | 7887 | { |
wolfSSL | 15:117db924cf7c | 7888 | const char* header = NULL; |
wolfSSL | 15:117db924cf7c | 7889 | const char* footer = NULL; |
wolfSSL | 15:117db924cf7c | 7890 | char* headerEnd; |
wolfSSL | 15:117db924cf7c | 7891 | char* footerEnd; |
wolfSSL | 15:117db924cf7c | 7892 | char* consumedEnd; |
wolfSSL | 15:117db924cf7c | 7893 | char* bufferEnd = (char*)(buff + longSz); |
wolfSSL | 15:117db924cf7c | 7894 | long neededSz; |
wolfSSL | 15:117db924cf7c | 7895 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 7896 | int sz = (int)longSz; |
wolfSSL | 15:117db924cf7c | 7897 | int encrypted_key = 0; |
wolfSSL | 15:117db924cf7c | 7898 | DerBuffer* der; |
wolfSSL | 15:117db924cf7c | 7899 | |
wolfSSL | 15:117db924cf7c | 7900 | WOLFSSL_ENTER("PemToDer"); |
wolfSSL | 15:117db924cf7c | 7901 | |
wolfSSL | 15:117db924cf7c | 7902 | /* get PEM header and footer based on type */ |
wolfSSL | 15:117db924cf7c | 7903 | ret = wc_PemGetHeaderFooter(type, &header, &footer); |
wolfSSL | 15:117db924cf7c | 7904 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 7905 | return ret; |
wolfSSL | 15:117db924cf7c | 7906 | |
wolfSSL | 15:117db924cf7c | 7907 | /* map header if not found for type */ |
wolfSSL | 15:117db924cf7c | 7908 | for (;;) { |
wolfSSL | 15:117db924cf7c | 7909 | headerEnd = XSTRNSTR((char*)buff, header, sz); |
wolfSSL | 15:117db924cf7c | 7910 | |
wolfSSL | 15:117db924cf7c | 7911 | if (headerEnd || type != PRIVATEKEY_TYPE) { |
wolfSSL | 15:117db924cf7c | 7912 | break; |
wolfSSL | 15:117db924cf7c | 7913 | } else |
wolfSSL | 15:117db924cf7c | 7914 | if (header == BEGIN_RSA_PRIV) { |
wolfSSL | 15:117db924cf7c | 7915 | header = BEGIN_PRIV_KEY; footer = END_PRIV_KEY; |
wolfSSL | 15:117db924cf7c | 7916 | } else |
wolfSSL | 15:117db924cf7c | 7917 | if (header == BEGIN_PRIV_KEY) { |
wolfSSL | 15:117db924cf7c | 7918 | header = BEGIN_ENC_PRIV_KEY; footer = END_ENC_PRIV_KEY; |
wolfSSL | 15:117db924cf7c | 7919 | } else |
wolfSSL | 15:117db924cf7c | 7920 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 7921 | if (header == BEGIN_ENC_PRIV_KEY) { |
wolfSSL | 15:117db924cf7c | 7922 | header = BEGIN_EC_PRIV; footer = END_EC_PRIV; |
wolfSSL | 15:117db924cf7c | 7923 | } else |
wolfSSL | 15:117db924cf7c | 7924 | if (header == BEGIN_EC_PRIV) { |
wolfSSL | 15:117db924cf7c | 7925 | header = BEGIN_DSA_PRIV; footer = END_DSA_PRIV; |
wolfSSL | 15:117db924cf7c | 7926 | } else |
wolfSSL | 15:117db924cf7c | 7927 | #endif |
wolfSSL | 15:117db924cf7c | 7928 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 7929 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 7930 | if (header == BEGIN_DSA_PRIV) |
wolfSSL | 15:117db924cf7c | 7931 | #else |
wolfSSL | 15:117db924cf7c | 7932 | if (header == BEGIN_ENC_PRIV_KEY) |
wolfSSL | 15:117db924cf7c | 7933 | #endif |
wolfSSL | 15:117db924cf7c | 7934 | { |
wolfSSL | 15:117db924cf7c | 7935 | header = BEGIN_EDDSA_PRIV; footer = END_EDDSA_PRIV; |
wolfSSL | 15:117db924cf7c | 7936 | } else |
wolfSSL | 15:117db924cf7c | 7937 | #endif |
wolfSSL | 15:117db924cf7c | 7938 | #ifdef HAVE_CRL |
wolfSSL | 15:117db924cf7c | 7939 | if (type == CRL_TYPE) { |
wolfSSL | 15:117db924cf7c | 7940 | header = BEGIN_CRL; footer = END_CRL; |
wolfSSL | 15:117db924cf7c | 7941 | } else |
wolfSSL | 15:117db924cf7c | 7942 | #endif |
wolfSSL | 15:117db924cf7c | 7943 | { |
wolfSSL | 15:117db924cf7c | 7944 | break; |
wolfSSL | 15:117db924cf7c | 7945 | } |
wolfSSL | 15:117db924cf7c | 7946 | } |
wolfSSL | 15:117db924cf7c | 7947 | |
wolfSSL | 15:117db924cf7c | 7948 | if (!headerEnd) { |
wolfSSL | 15:117db924cf7c | 7949 | WOLFSSL_MSG("Couldn't find PEM header"); |
wolfSSL | 15:117db924cf7c | 7950 | return ASN_NO_PEM_HEADER; |
wolfSSL | 15:117db924cf7c | 7951 | } |
wolfSSL | 15:117db924cf7c | 7952 | |
wolfSSL | 15:117db924cf7c | 7953 | headerEnd += XSTRLEN(header); |
wolfSSL | 15:117db924cf7c | 7954 | |
wolfSSL | 15:117db924cf7c | 7955 | if ((headerEnd + 1) >= bufferEnd) |
wolfSSL | 15:117db924cf7c | 7956 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7957 | |
wolfSSL | 15:117db924cf7c | 7958 | /* eat end of line */ |
wolfSSL | 15:117db924cf7c | 7959 | if (headerEnd[0] == '\n') |
wolfSSL | 15:117db924cf7c | 7960 | headerEnd++; |
wolfSSL | 15:117db924cf7c | 7961 | else if (headerEnd[1] == '\n') |
wolfSSL | 15:117db924cf7c | 7962 | headerEnd += 2; |
wolfSSL | 15:117db924cf7c | 7963 | else { |
wolfSSL | 15:117db924cf7c | 7964 | if (info) |
wolfSSL | 15:117db924cf7c | 7965 | info->consumed = (long)(headerEnd+2 - (char*)buff); |
wolfSSL | 15:117db924cf7c | 7966 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7967 | } |
wolfSSL | 15:117db924cf7c | 7968 | |
wolfSSL | 15:117db924cf7c | 7969 | if (type == PRIVATEKEY_TYPE) { |
wolfSSL | 15:117db924cf7c | 7970 | if (eccKey) { |
wolfSSL | 15:117db924cf7c | 7971 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 7972 | *eccKey = (header == BEGIN_EC_PRIV) ? 1 : 0; |
wolfSSL | 15:117db924cf7c | 7973 | #else |
wolfSSL | 15:117db924cf7c | 7974 | *eccKey = 0; |
wolfSSL | 15:117db924cf7c | 7975 | #endif |
wolfSSL | 15:117db924cf7c | 7976 | } |
wolfSSL | 15:117db924cf7c | 7977 | } |
wolfSSL | 15:117db924cf7c | 7978 | |
wolfSSL | 15:117db924cf7c | 7979 | #ifdef WOLFSSL_ENCRYPTED_KEYS |
wolfSSL | 15:117db924cf7c | 7980 | if (info) { |
wolfSSL | 15:117db924cf7c | 7981 | ret = wc_EncryptedInfoParse(info, &headerEnd, bufferEnd - headerEnd); |
wolfSSL | 15:117db924cf7c | 7982 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 7983 | return ret; |
wolfSSL | 15:117db924cf7c | 7984 | if (info->set) |
wolfSSL | 15:117db924cf7c | 7985 | encrypted_key = 1; |
wolfSSL | 15:117db924cf7c | 7986 | } |
wolfSSL | 15:117db924cf7c | 7987 | #endif /* WOLFSSL_ENCRYPTED_KEYS */ |
wolfSSL | 15:117db924cf7c | 7988 | |
wolfSSL | 15:117db924cf7c | 7989 | /* find footer */ |
wolfSSL | 15:117db924cf7c | 7990 | footerEnd = XSTRNSTR((char*)buff, footer, sz); |
wolfSSL | 15:117db924cf7c | 7991 | if (!footerEnd) { |
wolfSSL | 15:117db924cf7c | 7992 | if (info) |
wolfSSL | 15:117db924cf7c | 7993 | info->consumed = longSz; /* No more certs if no footer */ |
wolfSSL | 15:117db924cf7c | 7994 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 7995 | } |
wolfSSL | 15:117db924cf7c | 7996 | |
wolfSSL | 15:117db924cf7c | 7997 | consumedEnd = footerEnd + XSTRLEN(footer); |
wolfSSL | 15:117db924cf7c | 7998 | |
wolfSSL | 15:117db924cf7c | 7999 | if (consumedEnd < bufferEnd) { /* handle no end of line on last line */ |
wolfSSL | 15:117db924cf7c | 8000 | /* eat end of line */ |
wolfSSL | 15:117db924cf7c | 8001 | if (consumedEnd[0] == '\n') |
wolfSSL | 15:117db924cf7c | 8002 | consumedEnd++; |
wolfSSL | 15:117db924cf7c | 8003 | else if ((consumedEnd + 1 < bufferEnd) && consumedEnd[1] == '\n') |
wolfSSL | 15:117db924cf7c | 8004 | consumedEnd += 2; |
wolfSSL | 15:117db924cf7c | 8005 | else { |
wolfSSL | 15:117db924cf7c | 8006 | if (info) |
wolfSSL | 15:117db924cf7c | 8007 | info->consumed = (long)(consumedEnd+2 - (char*)buff); |
wolfSSL | 15:117db924cf7c | 8008 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8009 | } |
wolfSSL | 15:117db924cf7c | 8010 | } |
wolfSSL | 15:117db924cf7c | 8011 | |
wolfSSL | 15:117db924cf7c | 8012 | if (info) |
wolfSSL | 15:117db924cf7c | 8013 | info->consumed = (long)(consumedEnd - (char*)buff); |
wolfSSL | 15:117db924cf7c | 8014 | |
wolfSSL | 15:117db924cf7c | 8015 | /* set up der buffer */ |
wolfSSL | 15:117db924cf7c | 8016 | neededSz = (long)(footerEnd - headerEnd); |
wolfSSL | 15:117db924cf7c | 8017 | if (neededSz > sz || neededSz <= 0) |
wolfSSL | 15:117db924cf7c | 8018 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8019 | |
wolfSSL | 15:117db924cf7c | 8020 | ret = AllocDer(pDer, (word32)neededSz, type, heap); |
wolfSSL | 15:117db924cf7c | 8021 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 8022 | return ret; |
wolfSSL | 15:117db924cf7c | 8023 | } |
wolfSSL | 15:117db924cf7c | 8024 | der = *pDer; |
wolfSSL | 15:117db924cf7c | 8025 | |
wolfSSL | 15:117db924cf7c | 8026 | if (Base64_Decode((byte*)headerEnd, (word32)neededSz, |
wolfSSL | 15:117db924cf7c | 8027 | der->buffer, &der->length) < 0) |
wolfSSL | 15:117db924cf7c | 8028 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8029 | |
wolfSSL | 15:117db924cf7c | 8030 | if (header == BEGIN_PRIV_KEY && !encrypted_key) { |
wolfSSL | 15:117db924cf7c | 8031 | /* pkcs8 key, convert and adjust length */ |
wolfSSL | 15:117db924cf7c | 8032 | if ((ret = ToTraditional(der->buffer, der->length)) < 0) |
wolfSSL | 15:117db924cf7c | 8033 | return ret; |
wolfSSL | 15:117db924cf7c | 8034 | |
wolfSSL | 15:117db924cf7c | 8035 | der->length = ret; |
wolfSSL | 15:117db924cf7c | 8036 | return 0; |
wolfSSL | 15:117db924cf7c | 8037 | } |
wolfSSL | 15:117db924cf7c | 8038 | |
wolfSSL | 15:117db924cf7c | 8039 | #ifdef WOLFSSL_ENCRYPTED_KEYS |
wolfSSL | 15:117db924cf7c | 8040 | if (encrypted_key || header == BEGIN_ENC_PRIV_KEY) { |
wolfSSL | 15:117db924cf7c | 8041 | int passwordSz = NAME_SZ; |
wolfSSL | 15:117db924cf7c | 8042 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8043 | char* password = NULL; |
wolfSSL | 15:117db924cf7c | 8044 | #else |
wolfSSL | 15:117db924cf7c | 8045 | char password[NAME_SZ]; |
wolfSSL | 15:117db924cf7c | 8046 | #endif |
wolfSSL | 15:117db924cf7c | 8047 | |
wolfSSL | 15:117db924cf7c | 8048 | if (!info || !info->passwd_cb) { |
wolfSSL | 15:117db924cf7c | 8049 | WOLFSSL_MSG("No password callback set"); |
wolfSSL | 15:117db924cf7c | 8050 | return NO_PASSWORD; |
wolfSSL | 15:117db924cf7c | 8051 | } |
wolfSSL | 15:117db924cf7c | 8052 | |
wolfSSL | 15:117db924cf7c | 8053 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8054 | password = (char*)XMALLOC(passwordSz, heap, DYNAMIC_TYPE_STRING); |
wolfSSL | 15:117db924cf7c | 8055 | if (password == NULL) |
wolfSSL | 15:117db924cf7c | 8056 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8057 | #endif |
wolfSSL | 15:117db924cf7c | 8058 | |
wolfSSL | 15:117db924cf7c | 8059 | /* get password */ |
wolfSSL | 15:117db924cf7c | 8060 | ret = info->passwd_cb(password, passwordSz, PEM_PASS_READ, |
wolfSSL | 15:117db924cf7c | 8061 | info->passwd_userdata); |
wolfSSL | 15:117db924cf7c | 8062 | if (ret >= 0) { |
wolfSSL | 15:117db924cf7c | 8063 | passwordSz = ret; |
wolfSSL | 15:117db924cf7c | 8064 | |
wolfSSL | 15:117db924cf7c | 8065 | /* convert and adjust length */ |
wolfSSL | 15:117db924cf7c | 8066 | if (header == BEGIN_ENC_PRIV_KEY) { |
wolfSSL | 15:117db924cf7c | 8067 | #ifndef NO_PWDBASED |
wolfSSL | 15:117db924cf7c | 8068 | ret = ToTraditionalEnc(der->buffer, der->length, |
wolfSSL | 15:117db924cf7c | 8069 | password, passwordSz); |
wolfSSL | 15:117db924cf7c | 8070 | |
wolfSSL | 15:117db924cf7c | 8071 | if (ret >= 0) { |
wolfSSL | 15:117db924cf7c | 8072 | der->length = ret; |
wolfSSL | 15:117db924cf7c | 8073 | } |
wolfSSL | 15:117db924cf7c | 8074 | #else |
wolfSSL | 15:117db924cf7c | 8075 | ret = NOT_COMPILED_IN; |
wolfSSL | 15:117db924cf7c | 8076 | #endif |
wolfSSL | 15:117db924cf7c | 8077 | } |
wolfSSL | 15:117db924cf7c | 8078 | /* decrypt the key */ |
wolfSSL | 15:117db924cf7c | 8079 | else { |
wolfSSL | 15:117db924cf7c | 8080 | ret = wc_BufferKeyDecrypt(info, der->buffer, der->length, |
wolfSSL | 15:117db924cf7c | 8081 | (byte*)password, passwordSz, WC_MD5); |
wolfSSL | 15:117db924cf7c | 8082 | } |
wolfSSL | 15:117db924cf7c | 8083 | ForceZero(password, passwordSz); |
wolfSSL | 15:117db924cf7c | 8084 | } |
wolfSSL | 15:117db924cf7c | 8085 | |
wolfSSL | 15:117db924cf7c | 8086 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8087 | XFREE(password, heap, DYNAMIC_TYPE_STRING); |
wolfSSL | 15:117db924cf7c | 8088 | #endif |
wolfSSL | 15:117db924cf7c | 8089 | } |
wolfSSL | 15:117db924cf7c | 8090 | #endif /* WOLFSSL_ENCRYPTED_KEYS */ |
wolfSSL | 15:117db924cf7c | 8091 | |
wolfSSL | 15:117db924cf7c | 8092 | return ret; |
wolfSSL | 15:117db924cf7c | 8093 | } |
wolfSSL | 15:117db924cf7c | 8094 | |
wolfSSL | 15:117db924cf7c | 8095 | int wc_PemToDer(const unsigned char* buff, long longSz, int type, |
wolfSSL | 15:117db924cf7c | 8096 | DerBuffer** pDer, void* heap, EncryptedInfo* info, int* eccKey) |
wolfSSL | 15:117db924cf7c | 8097 | { |
wolfSSL | 15:117db924cf7c | 8098 | return PemToDer(buff, longSz, type, pDer, heap, info, eccKey); |
wolfSSL | 15:117db924cf7c | 8099 | } |
wolfSSL | 15:117db924cf7c | 8100 | |
wolfSSL | 15:117db924cf7c | 8101 | |
wolfSSL | 15:117db924cf7c | 8102 | /* our KeyPemToDer password callback, password in userData */ |
wolfSSL | 15:117db924cf7c | 8103 | static WC_INLINE int OurPasswordCb(char* passwd, int sz, int rw, void* userdata) |
wolfSSL | 15:117db924cf7c | 8104 | { |
wolfSSL | 15:117db924cf7c | 8105 | (void)rw; |
wolfSSL | 15:117db924cf7c | 8106 | |
wolfSSL | 15:117db924cf7c | 8107 | if (userdata == NULL) |
wolfSSL | 15:117db924cf7c | 8108 | return 0; |
wolfSSL | 15:117db924cf7c | 8109 | |
wolfSSL | 15:117db924cf7c | 8110 | XSTRNCPY(passwd, (char*)userdata, sz); |
wolfSSL | 15:117db924cf7c | 8111 | return min((word32)sz, (word32)XSTRLEN((char*)userdata)); |
wolfSSL | 15:117db924cf7c | 8112 | } |
wolfSSL | 15:117db924cf7c | 8113 | |
wolfSSL | 15:117db924cf7c | 8114 | /* Return bytes written to buff or < 0 for error */ |
wolfSSL | 15:117db924cf7c | 8115 | int wc_KeyPemToDer(const unsigned char* pem, int pemSz, |
wolfSSL | 15:117db924cf7c | 8116 | unsigned char* buff, int buffSz, const char* pass) |
wolfSSL | 15:117db924cf7c | 8117 | { |
wolfSSL | 15:117db924cf7c | 8118 | int eccKey = 0; |
wolfSSL | 15:117db924cf7c | 8119 | int ret; |
wolfSSL | 15:117db924cf7c | 8120 | DerBuffer* der = NULL; |
wolfSSL | 15:117db924cf7c | 8121 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8122 | EncryptedInfo* info = NULL; |
wolfSSL | 15:117db924cf7c | 8123 | #else |
wolfSSL | 15:117db924cf7c | 8124 | EncryptedInfo info[1]; |
wolfSSL | 15:117db924cf7c | 8125 | #endif |
wolfSSL | 15:117db924cf7c | 8126 | |
wolfSSL | 15:117db924cf7c | 8127 | WOLFSSL_ENTER("wc_KeyPemToDer"); |
wolfSSL | 15:117db924cf7c | 8128 | |
wolfSSL | 15:117db924cf7c | 8129 | if (pem == NULL || buff == NULL || buffSz <= 0) { |
wolfSSL | 15:117db924cf7c | 8130 | WOLFSSL_MSG("Bad pem der args"); |
wolfSSL | 15:117db924cf7c | 8131 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8132 | } |
wolfSSL | 15:117db924cf7c | 8133 | |
wolfSSL | 15:117db924cf7c | 8134 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8135 | info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL, |
wolfSSL | 15:117db924cf7c | 8136 | DYNAMIC_TYPE_ENCRYPTEDINFO); |
wolfSSL | 15:117db924cf7c | 8137 | if (info == NULL) |
wolfSSL | 15:117db924cf7c | 8138 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8139 | #endif |
wolfSSL | 15:117db924cf7c | 8140 | |
wolfSSL | 15:117db924cf7c | 8141 | XMEMSET(info, 0, sizeof(EncryptedInfo)); |
wolfSSL | 15:117db924cf7c | 8142 | info->passwd_cb = OurPasswordCb; |
wolfSSL | 15:117db924cf7c | 8143 | info->passwd_userdata = (void*)pass; |
wolfSSL | 15:117db924cf7c | 8144 | |
wolfSSL | 15:117db924cf7c | 8145 | ret = PemToDer(pem, pemSz, PRIVATEKEY_TYPE, &der, NULL, info, &eccKey); |
wolfSSL | 15:117db924cf7c | 8146 | |
wolfSSL | 15:117db924cf7c | 8147 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8148 | XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO); |
wolfSSL | 15:117db924cf7c | 8149 | #endif |
wolfSSL | 15:117db924cf7c | 8150 | |
wolfSSL | 15:117db924cf7c | 8151 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 8152 | WOLFSSL_MSG("Bad Pem To Der"); |
wolfSSL | 15:117db924cf7c | 8153 | } |
wolfSSL | 15:117db924cf7c | 8154 | else { |
wolfSSL | 15:117db924cf7c | 8155 | if (der->length <= (word32)buffSz) { |
wolfSSL | 15:117db924cf7c | 8156 | XMEMCPY(buff, der->buffer, der->length); |
wolfSSL | 15:117db924cf7c | 8157 | ret = der->length; |
wolfSSL | 15:117db924cf7c | 8158 | } |
wolfSSL | 15:117db924cf7c | 8159 | else { |
wolfSSL | 15:117db924cf7c | 8160 | WOLFSSL_MSG("Bad der length"); |
wolfSSL | 15:117db924cf7c | 8161 | ret = BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8162 | } |
wolfSSL | 15:117db924cf7c | 8163 | } |
wolfSSL | 15:117db924cf7c | 8164 | |
wolfSSL | 15:117db924cf7c | 8165 | FreeDer(&der); |
wolfSSL | 15:117db924cf7c | 8166 | return ret; |
wolfSSL | 15:117db924cf7c | 8167 | } |
wolfSSL | 15:117db924cf7c | 8168 | |
wolfSSL | 15:117db924cf7c | 8169 | |
wolfSSL | 15:117db924cf7c | 8170 | /* Return bytes written to buff or < 0 for error */ |
wolfSSL | 15:117db924cf7c | 8171 | int wc_CertPemToDer(const unsigned char* pem, int pemSz, |
wolfSSL | 15:117db924cf7c | 8172 | unsigned char* buff, int buffSz, int type) |
wolfSSL | 15:117db924cf7c | 8173 | { |
wolfSSL | 15:117db924cf7c | 8174 | int eccKey = 0; |
wolfSSL | 15:117db924cf7c | 8175 | int ret; |
wolfSSL | 15:117db924cf7c | 8176 | DerBuffer* der = NULL; |
wolfSSL | 15:117db924cf7c | 8177 | |
wolfSSL | 15:117db924cf7c | 8178 | WOLFSSL_ENTER("wc_CertPemToDer"); |
wolfSSL | 15:117db924cf7c | 8179 | |
wolfSSL | 15:117db924cf7c | 8180 | if (pem == NULL || buff == NULL || buffSz <= 0) { |
wolfSSL | 15:117db924cf7c | 8181 | WOLFSSL_MSG("Bad pem der args"); |
wolfSSL | 15:117db924cf7c | 8182 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8183 | } |
wolfSSL | 15:117db924cf7c | 8184 | |
wolfSSL | 15:117db924cf7c | 8185 | if (type != CERT_TYPE && type != CA_TYPE && type != CERTREQ_TYPE) { |
wolfSSL | 15:117db924cf7c | 8186 | WOLFSSL_MSG("Bad cert type"); |
wolfSSL | 15:117db924cf7c | 8187 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8188 | } |
wolfSSL | 15:117db924cf7c | 8189 | |
wolfSSL | 15:117db924cf7c | 8190 | |
wolfSSL | 15:117db924cf7c | 8191 | ret = PemToDer(pem, pemSz, type, &der, NULL, NULL, &eccKey); |
wolfSSL | 15:117db924cf7c | 8192 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 8193 | WOLFSSL_MSG("Bad Pem To Der"); |
wolfSSL | 15:117db924cf7c | 8194 | } |
wolfSSL | 15:117db924cf7c | 8195 | else { |
wolfSSL | 15:117db924cf7c | 8196 | if (der->length <= (word32)buffSz) { |
wolfSSL | 15:117db924cf7c | 8197 | XMEMCPY(buff, der->buffer, der->length); |
wolfSSL | 15:117db924cf7c | 8198 | ret = der->length; |
wolfSSL | 15:117db924cf7c | 8199 | } |
wolfSSL | 15:117db924cf7c | 8200 | else { |
wolfSSL | 15:117db924cf7c | 8201 | WOLFSSL_MSG("Bad der length"); |
wolfSSL | 15:117db924cf7c | 8202 | ret = BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8203 | } |
wolfSSL | 15:117db924cf7c | 8204 | } |
wolfSSL | 15:117db924cf7c | 8205 | |
wolfSSL | 15:117db924cf7c | 8206 | FreeDer(&der); |
wolfSSL | 15:117db924cf7c | 8207 | return ret; |
wolfSSL | 15:117db924cf7c | 8208 | } |
wolfSSL | 15:117db924cf7c | 8209 | |
wolfSSL | 15:117db924cf7c | 8210 | #endif /* WOLFSSL_PEM_TO_DER */ |
wolfSSL | 15:117db924cf7c | 8211 | #endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */ |
wolfSSL | 15:117db924cf7c | 8212 | |
wolfSSL | 15:117db924cf7c | 8213 | |
wolfSSL | 15:117db924cf7c | 8214 | #ifdef WOLFSSL_PEM_TO_DER |
wolfSSL | 15:117db924cf7c | 8215 | #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER) |
wolfSSL | 15:117db924cf7c | 8216 | /* Return bytes written to buff or < 0 for error */ |
wolfSSL | 15:117db924cf7c | 8217 | int wc_PubKeyPemToDer(const unsigned char* pem, int pemSz, |
wolfSSL | 15:117db924cf7c | 8218 | unsigned char* buff, int buffSz) |
wolfSSL | 15:117db924cf7c | 8219 | { |
wolfSSL | 15:117db924cf7c | 8220 | int ret; |
wolfSSL | 15:117db924cf7c | 8221 | DerBuffer* der = NULL; |
wolfSSL | 15:117db924cf7c | 8222 | |
wolfSSL | 15:117db924cf7c | 8223 | WOLFSSL_ENTER("wc_PubKeyPemToDer"); |
wolfSSL | 15:117db924cf7c | 8224 | |
wolfSSL | 15:117db924cf7c | 8225 | if (pem == NULL || buff == NULL || buffSz <= 0) { |
wolfSSL | 15:117db924cf7c | 8226 | WOLFSSL_MSG("Bad pem der args"); |
wolfSSL | 15:117db924cf7c | 8227 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8228 | } |
wolfSSL | 15:117db924cf7c | 8229 | |
wolfSSL | 15:117db924cf7c | 8230 | ret = PemToDer(pem, pemSz, PUBLICKEY_TYPE, &der, NULL, NULL, NULL); |
wolfSSL | 15:117db924cf7c | 8231 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 8232 | WOLFSSL_MSG("Bad Pem To Der"); |
wolfSSL | 15:117db924cf7c | 8233 | } |
wolfSSL | 15:117db924cf7c | 8234 | else { |
wolfSSL | 15:117db924cf7c | 8235 | if (der->length <= (word32)buffSz) { |
wolfSSL | 15:117db924cf7c | 8236 | XMEMCPY(buff, der->buffer, der->length); |
wolfSSL | 15:117db924cf7c | 8237 | ret = der->length; |
wolfSSL | 15:117db924cf7c | 8238 | } |
wolfSSL | 15:117db924cf7c | 8239 | else { |
wolfSSL | 15:117db924cf7c | 8240 | WOLFSSL_MSG("Bad der length"); |
wolfSSL | 15:117db924cf7c | 8241 | ret = BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8242 | } |
wolfSSL | 15:117db924cf7c | 8243 | } |
wolfSSL | 15:117db924cf7c | 8244 | |
wolfSSL | 15:117db924cf7c | 8245 | FreeDer(&der); |
wolfSSL | 15:117db924cf7c | 8246 | return ret; |
wolfSSL | 15:117db924cf7c | 8247 | } |
wolfSSL | 15:117db924cf7c | 8248 | #endif /* WOLFSSL_CERT_EXT || WOLFSSL_PUB_PEM_TO_DER */ |
wolfSSL | 15:117db924cf7c | 8249 | #endif /* WOLFSSL_PEM_TO_DER */ |
wolfSSL | 15:117db924cf7c | 8250 | |
wolfSSL | 15:117db924cf7c | 8251 | #ifndef NO_FILESYSTEM |
wolfSSL | 15:117db924cf7c | 8252 | |
wolfSSL | 15:117db924cf7c | 8253 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 8254 | /* load pem cert from file into der buffer, return der size or error */ |
wolfSSL | 15:117db924cf7c | 8255 | int wc_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz) |
wolfSSL | 15:117db924cf7c | 8256 | { |
wolfSSL | 15:117db924cf7c | 8257 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8258 | byte staticBuffer[1]; /* force XMALLOC */ |
wolfSSL | 15:117db924cf7c | 8259 | #else |
wolfSSL | 15:117db924cf7c | 8260 | byte staticBuffer[FILE_BUFFER_SIZE]; |
wolfSSL | 15:117db924cf7c | 8261 | #endif |
wolfSSL | 15:117db924cf7c | 8262 | byte* fileBuf = staticBuffer; |
wolfSSL | 15:117db924cf7c | 8263 | int dynamic = 0; |
wolfSSL | 15:117db924cf7c | 8264 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 8265 | long sz = 0; |
wolfSSL | 15:117db924cf7c | 8266 | XFILE file = XFOPEN(fileName, "rb"); |
wolfSSL | 15:117db924cf7c | 8267 | DerBuffer* converted = NULL; |
wolfSSL | 15:117db924cf7c | 8268 | |
wolfSSL | 15:117db924cf7c | 8269 | WOLFSSL_ENTER("wc_PemCertToDer"); |
wolfSSL | 15:117db924cf7c | 8270 | |
wolfSSL | 15:117db924cf7c | 8271 | if (file == XBADFILE) { |
wolfSSL | 15:117db924cf7c | 8272 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8273 | } |
wolfSSL | 15:117db924cf7c | 8274 | else { |
wolfSSL | 15:117db924cf7c | 8275 | XFSEEK(file, 0, XSEEK_END); |
wolfSSL | 15:117db924cf7c | 8276 | sz = XFTELL(file); |
wolfSSL | 15:117db924cf7c | 8277 | XREWIND(file); |
wolfSSL | 15:117db924cf7c | 8278 | |
wolfSSL | 15:117db924cf7c | 8279 | if (sz <= 0) { |
wolfSSL | 15:117db924cf7c | 8280 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8281 | } |
wolfSSL | 15:117db924cf7c | 8282 | else if (sz > (long)sizeof(staticBuffer)) { |
wolfSSL | 15:117db924cf7c | 8283 | #ifdef WOLFSSL_STATIC_MEMORY |
wolfSSL | 15:117db924cf7c | 8284 | WOLFSSL_MSG("File was larger then static buffer"); |
wolfSSL | 15:117db924cf7c | 8285 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8286 | #endif |
wolfSSL | 15:117db924cf7c | 8287 | fileBuf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE); |
wolfSSL | 15:117db924cf7c | 8288 | if (fileBuf == NULL) |
wolfSSL | 15:117db924cf7c | 8289 | ret = MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8290 | else |
wolfSSL | 15:117db924cf7c | 8291 | dynamic = 1; |
wolfSSL | 15:117db924cf7c | 8292 | } |
wolfSSL | 15:117db924cf7c | 8293 | |
wolfSSL | 15:117db924cf7c | 8294 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 8295 | if ( (ret = (int)XFREAD(fileBuf, 1, sz, file)) != sz) { |
wolfSSL | 15:117db924cf7c | 8296 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8297 | } |
wolfSSL | 15:117db924cf7c | 8298 | #ifdef WOLFSSL_PEM_TO_DER |
wolfSSL | 15:117db924cf7c | 8299 | else { |
wolfSSL | 15:117db924cf7c | 8300 | ret = PemToDer(fileBuf, sz, CA_TYPE, &converted, 0, NULL,NULL); |
wolfSSL | 15:117db924cf7c | 8301 | } |
wolfSSL | 15:117db924cf7c | 8302 | #endif |
wolfSSL | 15:117db924cf7c | 8303 | |
wolfSSL | 15:117db924cf7c | 8304 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 8305 | if (converted->length < (word32)derSz) { |
wolfSSL | 15:117db924cf7c | 8306 | XMEMCPY(derBuf, converted->buffer, converted->length); |
wolfSSL | 15:117db924cf7c | 8307 | ret = converted->length; |
wolfSSL | 15:117db924cf7c | 8308 | } |
wolfSSL | 15:117db924cf7c | 8309 | else |
wolfSSL | 15:117db924cf7c | 8310 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8311 | } |
wolfSSL | 15:117db924cf7c | 8312 | |
wolfSSL | 15:117db924cf7c | 8313 | FreeDer(&converted); |
wolfSSL | 15:117db924cf7c | 8314 | } |
wolfSSL | 15:117db924cf7c | 8315 | |
wolfSSL | 15:117db924cf7c | 8316 | XFCLOSE(file); |
wolfSSL | 15:117db924cf7c | 8317 | if (dynamic) |
wolfSSL | 15:117db924cf7c | 8318 | XFREE(fileBuf, NULL, DYNAMIC_TYPE_FILE); |
wolfSSL | 15:117db924cf7c | 8319 | } |
wolfSSL | 15:117db924cf7c | 8320 | |
wolfSSL | 15:117db924cf7c | 8321 | return ret; |
wolfSSL | 15:117db924cf7c | 8322 | } |
wolfSSL | 15:117db924cf7c | 8323 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 8324 | |
wolfSSL | 15:117db924cf7c | 8325 | #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER) |
wolfSSL | 15:117db924cf7c | 8326 | /* load pem public key from file into der buffer, return der size or error */ |
wolfSSL | 15:117db924cf7c | 8327 | int wc_PemPubKeyToDer(const char* fileName, |
wolfSSL | 15:117db924cf7c | 8328 | unsigned char* derBuf, int derSz) |
wolfSSL | 15:117db924cf7c | 8329 | { |
wolfSSL | 15:117db924cf7c | 8330 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8331 | byte staticBuffer[1]; /* force XMALLOC */ |
wolfSSL | 15:117db924cf7c | 8332 | #else |
wolfSSL | 15:117db924cf7c | 8333 | byte staticBuffer[FILE_BUFFER_SIZE]; |
wolfSSL | 15:117db924cf7c | 8334 | #endif |
wolfSSL | 15:117db924cf7c | 8335 | byte* fileBuf = staticBuffer; |
wolfSSL | 15:117db924cf7c | 8336 | int dynamic = 0; |
wolfSSL | 15:117db924cf7c | 8337 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 8338 | long sz = 0; |
wolfSSL | 15:117db924cf7c | 8339 | XFILE file = XFOPEN(fileName, "rb"); |
wolfSSL | 15:117db924cf7c | 8340 | DerBuffer* converted = NULL; |
wolfSSL | 15:117db924cf7c | 8341 | |
wolfSSL | 15:117db924cf7c | 8342 | WOLFSSL_ENTER("wc_PemPubKeyToDer"); |
wolfSSL | 15:117db924cf7c | 8343 | |
wolfSSL | 15:117db924cf7c | 8344 | if (file == XBADFILE) { |
wolfSSL | 15:117db924cf7c | 8345 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8346 | } |
wolfSSL | 15:117db924cf7c | 8347 | else { |
wolfSSL | 15:117db924cf7c | 8348 | XFSEEK(file, 0, XSEEK_END); |
wolfSSL | 15:117db924cf7c | 8349 | sz = XFTELL(file); |
wolfSSL | 15:117db924cf7c | 8350 | XREWIND(file); |
wolfSSL | 15:117db924cf7c | 8351 | |
wolfSSL | 15:117db924cf7c | 8352 | if (sz <= 0) { |
wolfSSL | 15:117db924cf7c | 8353 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8354 | } |
wolfSSL | 15:117db924cf7c | 8355 | else if (sz > (long)sizeof(staticBuffer)) { |
wolfSSL | 15:117db924cf7c | 8356 | #ifdef WOLFSSL_STATIC_MEMORY |
wolfSSL | 15:117db924cf7c | 8357 | WOLFSSL_MSG("File was larger then static buffer"); |
wolfSSL | 15:117db924cf7c | 8358 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8359 | #endif |
wolfSSL | 15:117db924cf7c | 8360 | fileBuf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE); |
wolfSSL | 15:117db924cf7c | 8361 | if (fileBuf == NULL) |
wolfSSL | 15:117db924cf7c | 8362 | ret = MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8363 | else |
wolfSSL | 15:117db924cf7c | 8364 | dynamic = 1; |
wolfSSL | 15:117db924cf7c | 8365 | } |
wolfSSL | 15:117db924cf7c | 8366 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 8367 | if ( (ret = (int)XFREAD(fileBuf, 1, sz, file)) != sz) { |
wolfSSL | 15:117db924cf7c | 8368 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8369 | } |
wolfSSL | 15:117db924cf7c | 8370 | #ifdef WOLFSSL_PEM_TO_DER |
wolfSSL | 15:117db924cf7c | 8371 | else { |
wolfSSL | 15:117db924cf7c | 8372 | ret = PemToDer(fileBuf, sz, PUBLICKEY_TYPE, &converted, |
wolfSSL | 15:117db924cf7c | 8373 | 0, NULL, NULL); |
wolfSSL | 15:117db924cf7c | 8374 | } |
wolfSSL | 15:117db924cf7c | 8375 | #endif |
wolfSSL | 15:117db924cf7c | 8376 | |
wolfSSL | 15:117db924cf7c | 8377 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 8378 | if (converted->length < (word32)derSz) { |
wolfSSL | 15:117db924cf7c | 8379 | XMEMCPY(derBuf, converted->buffer, converted->length); |
wolfSSL | 15:117db924cf7c | 8380 | ret = converted->length; |
wolfSSL | 15:117db924cf7c | 8381 | } |
wolfSSL | 15:117db924cf7c | 8382 | else |
wolfSSL | 15:117db924cf7c | 8383 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8384 | } |
wolfSSL | 15:117db924cf7c | 8385 | |
wolfSSL | 15:117db924cf7c | 8386 | FreeDer(&converted); |
wolfSSL | 15:117db924cf7c | 8387 | } |
wolfSSL | 15:117db924cf7c | 8388 | |
wolfSSL | 15:117db924cf7c | 8389 | XFCLOSE(file); |
wolfSSL | 15:117db924cf7c | 8390 | if (dynamic) |
wolfSSL | 15:117db924cf7c | 8391 | XFREE(fileBuf, NULL, DYNAMIC_TYPE_FILE); |
wolfSSL | 15:117db924cf7c | 8392 | } |
wolfSSL | 15:117db924cf7c | 8393 | |
wolfSSL | 15:117db924cf7c | 8394 | return ret; |
wolfSSL | 15:117db924cf7c | 8395 | } |
wolfSSL | 15:117db924cf7c | 8396 | #endif /* WOLFSSL_CERT_EXT || WOLFSSL_PUB_PEM_TO_DER */ |
wolfSSL | 15:117db924cf7c | 8397 | |
wolfSSL | 15:117db924cf7c | 8398 | #endif /* !NO_FILESYSTEM */ |
wolfSSL | 15:117db924cf7c | 8399 | |
wolfSSL | 15:117db924cf7c | 8400 | |
wolfSSL | 15:117db924cf7c | 8401 | #if !defined(NO_RSA) && (defined(WOLFSSL_CERT_GEN) || \ |
wolfSSL | 15:117db924cf7c | 8402 | ((defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)) && !defined(HAVE_USER_RSA))) |
wolfSSL | 15:117db924cf7c | 8403 | /* USER RSA ifdef portions used instead of refactor in consideration for |
wolfSSL | 15:117db924cf7c | 8404 | possible fips build */ |
wolfSSL | 15:117db924cf7c | 8405 | /* Write a public RSA key to output */ |
wolfSSL | 15:117db924cf7c | 8406 | static int SetRsaPublicKey(byte* output, RsaKey* key, |
wolfSSL | 15:117db924cf7c | 8407 | int outLen, int with_header) |
wolfSSL | 15:117db924cf7c | 8408 | { |
wolfSSL | 15:117db924cf7c | 8409 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8410 | byte* n = NULL; |
wolfSSL | 15:117db924cf7c | 8411 | byte* e = NULL; |
wolfSSL | 15:117db924cf7c | 8412 | #else |
wolfSSL | 15:117db924cf7c | 8413 | byte n[MAX_RSA_INT_SZ]; |
wolfSSL | 15:117db924cf7c | 8414 | byte e[MAX_RSA_E_SZ]; |
wolfSSL | 15:117db924cf7c | 8415 | #endif |
wolfSSL | 15:117db924cf7c | 8416 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 8417 | byte bitString[1 + MAX_LENGTH_SZ + 1]; |
wolfSSL | 15:117db924cf7c | 8418 | int nSz; |
wolfSSL | 15:117db924cf7c | 8419 | int eSz; |
wolfSSL | 15:117db924cf7c | 8420 | int seqSz; |
wolfSSL | 15:117db924cf7c | 8421 | int bitStringSz; |
wolfSSL | 15:117db924cf7c | 8422 | int idx; |
wolfSSL | 15:117db924cf7c | 8423 | |
wolfSSL | 15:117db924cf7c | 8424 | if (output == NULL || key == NULL || outLen < MAX_SEQ_SZ) |
wolfSSL | 15:117db924cf7c | 8425 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8426 | |
wolfSSL | 15:117db924cf7c | 8427 | /* n */ |
wolfSSL | 15:117db924cf7c | 8428 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8429 | n = (byte*)XMALLOC(MAX_RSA_INT_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8430 | if (n == NULL) |
wolfSSL | 15:117db924cf7c | 8431 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8432 | #endif |
wolfSSL | 15:117db924cf7c | 8433 | |
wolfSSL | 15:117db924cf7c | 8434 | #ifdef HAVE_USER_RSA |
wolfSSL | 15:117db924cf7c | 8435 | nSz = SetASNIntRSA(key->n, n); |
wolfSSL | 15:117db924cf7c | 8436 | #else |
wolfSSL | 15:117db924cf7c | 8437 | nSz = SetASNIntMP(&key->n, MAX_RSA_INT_SZ, n); |
wolfSSL | 15:117db924cf7c | 8438 | #endif |
wolfSSL | 15:117db924cf7c | 8439 | if (nSz < 0) { |
wolfSSL | 15:117db924cf7c | 8440 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8441 | XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8442 | #endif |
wolfSSL | 15:117db924cf7c | 8443 | return nSz; |
wolfSSL | 15:117db924cf7c | 8444 | } |
wolfSSL | 15:117db924cf7c | 8445 | |
wolfSSL | 15:117db924cf7c | 8446 | /* e */ |
wolfSSL | 15:117db924cf7c | 8447 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8448 | e = (byte*)XMALLOC(MAX_RSA_E_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8449 | if (e == NULL) { |
wolfSSL | 15:117db924cf7c | 8450 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8451 | XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8452 | #endif |
wolfSSL | 15:117db924cf7c | 8453 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8454 | } |
wolfSSL | 15:117db924cf7c | 8455 | #endif |
wolfSSL | 15:117db924cf7c | 8456 | |
wolfSSL | 15:117db924cf7c | 8457 | #ifdef HAVE_USER_RSA |
wolfSSL | 15:117db924cf7c | 8458 | eSz = SetASNIntRSA(key->e, e); |
wolfSSL | 15:117db924cf7c | 8459 | #else |
wolfSSL | 15:117db924cf7c | 8460 | eSz = SetASNIntMP(&key->e, MAX_RSA_INT_SZ, e); |
wolfSSL | 15:117db924cf7c | 8461 | #endif |
wolfSSL | 15:117db924cf7c | 8462 | if (eSz < 0) { |
wolfSSL | 15:117db924cf7c | 8463 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8464 | XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8465 | XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8466 | #endif |
wolfSSL | 15:117db924cf7c | 8467 | return eSz; |
wolfSSL | 15:117db924cf7c | 8468 | } |
wolfSSL | 15:117db924cf7c | 8469 | |
wolfSSL | 15:117db924cf7c | 8470 | seqSz = SetSequence(nSz + eSz, seq); |
wolfSSL | 15:117db924cf7c | 8471 | |
wolfSSL | 15:117db924cf7c | 8472 | /* check output size */ |
wolfSSL | 15:117db924cf7c | 8473 | if ( (seqSz + nSz + eSz) > outLen) { |
wolfSSL | 15:117db924cf7c | 8474 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8475 | XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8476 | XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8477 | #endif |
wolfSSL | 15:117db924cf7c | 8478 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8479 | } |
wolfSSL | 15:117db924cf7c | 8480 | |
wolfSSL | 15:117db924cf7c | 8481 | /* headers */ |
wolfSSL | 15:117db924cf7c | 8482 | if (with_header) { |
wolfSSL | 15:117db924cf7c | 8483 | int algoSz; |
wolfSSL | 15:117db924cf7c | 8484 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8485 | byte* algo = NULL; |
wolfSSL | 15:117db924cf7c | 8486 | |
wolfSSL | 15:117db924cf7c | 8487 | algo = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8488 | if (algo == NULL) { |
wolfSSL | 15:117db924cf7c | 8489 | XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8490 | XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8491 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8492 | } |
wolfSSL | 15:117db924cf7c | 8493 | #else |
wolfSSL | 15:117db924cf7c | 8494 | byte algo[MAX_ALGO_SZ]; |
wolfSSL | 15:117db924cf7c | 8495 | #endif |
wolfSSL | 15:117db924cf7c | 8496 | algoSz = SetAlgoID(RSAk, algo, oidKeyType, 0); |
wolfSSL | 15:117db924cf7c | 8497 | bitStringSz = SetBitString(seqSz + nSz + eSz, 0, bitString); |
wolfSSL | 15:117db924cf7c | 8498 | |
wolfSSL | 15:117db924cf7c | 8499 | idx = SetSequence(nSz + eSz + seqSz + bitStringSz + algoSz, output); |
wolfSSL | 15:117db924cf7c | 8500 | |
wolfSSL | 15:117db924cf7c | 8501 | /* check output size */ |
wolfSSL | 15:117db924cf7c | 8502 | if ( (idx + algoSz + bitStringSz + seqSz + nSz + eSz) > outLen) { |
wolfSSL | 15:117db924cf7c | 8503 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8504 | XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8505 | XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8506 | XFREE(algo, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8507 | #endif |
wolfSSL | 15:117db924cf7c | 8508 | |
wolfSSL | 15:117db924cf7c | 8509 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 8510 | } |
wolfSSL | 15:117db924cf7c | 8511 | |
wolfSSL | 15:117db924cf7c | 8512 | /* algo */ |
wolfSSL | 15:117db924cf7c | 8513 | XMEMCPY(output + idx, algo, algoSz); |
wolfSSL | 15:117db924cf7c | 8514 | idx += algoSz; |
wolfSSL | 15:117db924cf7c | 8515 | /* bit string */ |
wolfSSL | 15:117db924cf7c | 8516 | XMEMCPY(output + idx, bitString, bitStringSz); |
wolfSSL | 15:117db924cf7c | 8517 | idx += bitStringSz; |
wolfSSL | 15:117db924cf7c | 8518 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8519 | XFREE(algo, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8520 | #endif |
wolfSSL | 15:117db924cf7c | 8521 | } |
wolfSSL | 15:117db924cf7c | 8522 | else |
wolfSSL | 15:117db924cf7c | 8523 | idx = 0; |
wolfSSL | 15:117db924cf7c | 8524 | |
wolfSSL | 15:117db924cf7c | 8525 | /* seq */ |
wolfSSL | 15:117db924cf7c | 8526 | XMEMCPY(output + idx, seq, seqSz); |
wolfSSL | 15:117db924cf7c | 8527 | idx += seqSz; |
wolfSSL | 15:117db924cf7c | 8528 | /* n */ |
wolfSSL | 15:117db924cf7c | 8529 | XMEMCPY(output + idx, n, nSz); |
wolfSSL | 15:117db924cf7c | 8530 | idx += nSz; |
wolfSSL | 15:117db924cf7c | 8531 | /* e */ |
wolfSSL | 15:117db924cf7c | 8532 | XMEMCPY(output + idx, e, eSz); |
wolfSSL | 15:117db924cf7c | 8533 | idx += eSz; |
wolfSSL | 15:117db924cf7c | 8534 | |
wolfSSL | 15:117db924cf7c | 8535 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8536 | XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8537 | XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8538 | #endif |
wolfSSL | 15:117db924cf7c | 8539 | |
wolfSSL | 15:117db924cf7c | 8540 | return idx; |
wolfSSL | 15:117db924cf7c | 8541 | } |
wolfSSL | 15:117db924cf7c | 8542 | |
wolfSSL | 15:117db924cf7c | 8543 | int RsaPublicKeyDerSize(RsaKey* key, int with_header) |
wolfSSL | 15:117db924cf7c | 8544 | { |
wolfSSL | 15:117db924cf7c | 8545 | byte* dummy = NULL; |
wolfSSL | 15:117db924cf7c | 8546 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 8547 | byte bitString[1 + MAX_LENGTH_SZ + 1]; |
wolfSSL | 15:117db924cf7c | 8548 | int nSz; |
wolfSSL | 15:117db924cf7c | 8549 | int eSz; |
wolfSSL | 15:117db924cf7c | 8550 | int seqSz; |
wolfSSL | 15:117db924cf7c | 8551 | int bitStringSz; |
wolfSSL | 15:117db924cf7c | 8552 | int idx; |
wolfSSL | 15:117db924cf7c | 8553 | |
wolfSSL | 15:117db924cf7c | 8554 | if (key == NULL) |
wolfSSL | 15:117db924cf7c | 8555 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8556 | |
wolfSSL | 15:117db924cf7c | 8557 | /* n */ |
wolfSSL | 15:117db924cf7c | 8558 | dummy = (byte*)XMALLOC(MAX_RSA_INT_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8559 | if (dummy == NULL) |
wolfSSL | 15:117db924cf7c | 8560 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8561 | |
wolfSSL | 15:117db924cf7c | 8562 | #ifdef HAVE_USER_RSA |
wolfSSL | 15:117db924cf7c | 8563 | nSz = SetASNIntRSA(key->n, dummy); |
wolfSSL | 15:117db924cf7c | 8564 | #else |
wolfSSL | 15:117db924cf7c | 8565 | nSz = SetASNIntMP(&key->n, MAX_RSA_INT_SZ, dummy); |
wolfSSL | 15:117db924cf7c | 8566 | #endif |
wolfSSL | 15:117db924cf7c | 8567 | XFREE(dummy, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8568 | if (nSz < 0) { |
wolfSSL | 15:117db924cf7c | 8569 | return nSz; |
wolfSSL | 15:117db924cf7c | 8570 | } |
wolfSSL | 15:117db924cf7c | 8571 | |
wolfSSL | 15:117db924cf7c | 8572 | /* e */ |
wolfSSL | 15:117db924cf7c | 8573 | dummy = (byte*)XMALLOC(MAX_RSA_E_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8574 | if (dummy == NULL) { |
wolfSSL | 15:117db924cf7c | 8575 | XFREE(dummy, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8576 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8577 | } |
wolfSSL | 15:117db924cf7c | 8578 | |
wolfSSL | 15:117db924cf7c | 8579 | #ifdef HAVE_USER_RSA |
wolfSSL | 15:117db924cf7c | 8580 | eSz = SetASNIntRSA(key->e, dummy); |
wolfSSL | 15:117db924cf7c | 8581 | #else |
wolfSSL | 15:117db924cf7c | 8582 | eSz = SetASNIntMP(&key->e, MAX_RSA_INT_SZ, dummy); |
wolfSSL | 15:117db924cf7c | 8583 | #endif |
wolfSSL | 15:117db924cf7c | 8584 | XFREE(dummy, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8585 | if (eSz < 0) { |
wolfSSL | 15:117db924cf7c | 8586 | return eSz; |
wolfSSL | 15:117db924cf7c | 8587 | } |
wolfSSL | 15:117db924cf7c | 8588 | |
wolfSSL | 15:117db924cf7c | 8589 | seqSz = SetSequence(nSz + eSz, seq); |
wolfSSL | 15:117db924cf7c | 8590 | |
wolfSSL | 15:117db924cf7c | 8591 | /* headers */ |
wolfSSL | 15:117db924cf7c | 8592 | if (with_header) { |
wolfSSL | 15:117db924cf7c | 8593 | int algoSz; |
wolfSSL | 15:117db924cf7c | 8594 | dummy = (byte*)XMALLOC(MAX_RSA_INT_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8595 | if (dummy == NULL) |
wolfSSL | 15:117db924cf7c | 8596 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8597 | |
wolfSSL | 15:117db924cf7c | 8598 | algoSz = SetAlgoID(RSAk, dummy, oidKeyType, 0); |
wolfSSL | 15:117db924cf7c | 8599 | bitStringSz = SetBitString(seqSz + nSz + eSz, 0, bitString); |
wolfSSL | 15:117db924cf7c | 8600 | |
wolfSSL | 15:117db924cf7c | 8601 | idx = SetSequence(nSz + eSz + seqSz + bitStringSz + algoSz, dummy); |
wolfSSL | 15:117db924cf7c | 8602 | XFREE(dummy, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8603 | |
wolfSSL | 15:117db924cf7c | 8604 | /* algo */ |
wolfSSL | 15:117db924cf7c | 8605 | idx += algoSz; |
wolfSSL | 15:117db924cf7c | 8606 | /* bit string */ |
wolfSSL | 15:117db924cf7c | 8607 | idx += bitStringSz; |
wolfSSL | 15:117db924cf7c | 8608 | } |
wolfSSL | 15:117db924cf7c | 8609 | else |
wolfSSL | 15:117db924cf7c | 8610 | idx = 0; |
wolfSSL | 15:117db924cf7c | 8611 | |
wolfSSL | 15:117db924cf7c | 8612 | /* seq */ |
wolfSSL | 15:117db924cf7c | 8613 | idx += seqSz; |
wolfSSL | 15:117db924cf7c | 8614 | /* n */ |
wolfSSL | 15:117db924cf7c | 8615 | idx += nSz; |
wolfSSL | 15:117db924cf7c | 8616 | /* e */ |
wolfSSL | 15:117db924cf7c | 8617 | idx += eSz; |
wolfSSL | 15:117db924cf7c | 8618 | |
wolfSSL | 15:117db924cf7c | 8619 | return idx; |
wolfSSL | 15:117db924cf7c | 8620 | } |
wolfSSL | 15:117db924cf7c | 8621 | #endif /* !NO_RSA && (WOLFSSL_CERT_GEN || (WOLFSSL_KEY_GEN && |
wolfSSL | 15:117db924cf7c | 8622 | !HAVE_USER_RSA))) */ |
wolfSSL | 15:117db924cf7c | 8623 | |
wolfSSL | 15:117db924cf7c | 8624 | |
wolfSSL | 15:117db924cf7c | 8625 | #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) |
wolfSSL | 15:117db924cf7c | 8626 | |
wolfSSL | 15:117db924cf7c | 8627 | |
wolfSSL | 15:117db924cf7c | 8628 | static mp_int* GetRsaInt(RsaKey* key, int idx) |
wolfSSL | 15:117db924cf7c | 8629 | { |
wolfSSL | 15:117db924cf7c | 8630 | if (idx == 0) |
wolfSSL | 15:117db924cf7c | 8631 | return &key->n; |
wolfSSL | 15:117db924cf7c | 8632 | if (idx == 1) |
wolfSSL | 15:117db924cf7c | 8633 | return &key->e; |
wolfSSL | 15:117db924cf7c | 8634 | if (idx == 2) |
wolfSSL | 15:117db924cf7c | 8635 | return &key->d; |
wolfSSL | 15:117db924cf7c | 8636 | if (idx == 3) |
wolfSSL | 15:117db924cf7c | 8637 | return &key->p; |
wolfSSL | 15:117db924cf7c | 8638 | if (idx == 4) |
wolfSSL | 15:117db924cf7c | 8639 | return &key->q; |
wolfSSL | 15:117db924cf7c | 8640 | if (idx == 5) |
wolfSSL | 15:117db924cf7c | 8641 | return &key->dP; |
wolfSSL | 15:117db924cf7c | 8642 | if (idx == 6) |
wolfSSL | 15:117db924cf7c | 8643 | return &key->dQ; |
wolfSSL | 15:117db924cf7c | 8644 | if (idx == 7) |
wolfSSL | 15:117db924cf7c | 8645 | return &key->u; |
wolfSSL | 15:117db924cf7c | 8646 | |
wolfSSL | 15:117db924cf7c | 8647 | return NULL; |
wolfSSL | 15:117db924cf7c | 8648 | } |
wolfSSL | 15:117db924cf7c | 8649 | |
wolfSSL | 15:117db924cf7c | 8650 | |
wolfSSL | 15:117db924cf7c | 8651 | /* Release Tmp RSA resources */ |
wolfSSL | 15:117db924cf7c | 8652 | static WC_INLINE void FreeTmpRsas(byte** tmps, void* heap) |
wolfSSL | 15:117db924cf7c | 8653 | { |
wolfSSL | 15:117db924cf7c | 8654 | int i; |
wolfSSL | 15:117db924cf7c | 8655 | |
wolfSSL | 15:117db924cf7c | 8656 | (void)heap; |
wolfSSL | 15:117db924cf7c | 8657 | |
wolfSSL | 15:117db924cf7c | 8658 | for (i = 0; i < RSA_INTS; i++) |
wolfSSL | 15:117db924cf7c | 8659 | XFREE(tmps[i], heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 8660 | } |
wolfSSL | 15:117db924cf7c | 8661 | |
wolfSSL | 15:117db924cf7c | 8662 | |
wolfSSL | 15:117db924cf7c | 8663 | /* Convert RsaKey key to DER format, write to output (inLen), return bytes |
wolfSSL | 15:117db924cf7c | 8664 | written */ |
wolfSSL | 15:117db924cf7c | 8665 | int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen) |
wolfSSL | 15:117db924cf7c | 8666 | { |
wolfSSL | 15:117db924cf7c | 8667 | word32 seqSz, verSz, rawLen, intTotalLen = 0; |
wolfSSL | 15:117db924cf7c | 8668 | word32 sizes[RSA_INTS]; |
wolfSSL | 15:117db924cf7c | 8669 | int i, j, outLen, ret = 0, mpSz; |
wolfSSL | 15:117db924cf7c | 8670 | |
wolfSSL | 15:117db924cf7c | 8671 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 8672 | byte ver[MAX_VERSION_SZ]; |
wolfSSL | 15:117db924cf7c | 8673 | byte* tmps[RSA_INTS]; |
wolfSSL | 15:117db924cf7c | 8674 | |
wolfSSL | 15:117db924cf7c | 8675 | if (!key || !output) |
wolfSSL | 15:117db924cf7c | 8676 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8677 | |
wolfSSL | 15:117db924cf7c | 8678 | if (key->type != RSA_PRIVATE) |
wolfSSL | 15:117db924cf7c | 8679 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8680 | |
wolfSSL | 15:117db924cf7c | 8681 | for (i = 0; i < RSA_INTS; i++) |
wolfSSL | 15:117db924cf7c | 8682 | tmps[i] = NULL; |
wolfSSL | 15:117db924cf7c | 8683 | |
wolfSSL | 15:117db924cf7c | 8684 | /* write all big ints from key to DER tmps */ |
wolfSSL | 15:117db924cf7c | 8685 | for (i = 0; i < RSA_INTS; i++) { |
wolfSSL | 15:117db924cf7c | 8686 | mp_int* keyInt = GetRsaInt(key, i); |
wolfSSL | 15:117db924cf7c | 8687 | |
wolfSSL | 15:117db924cf7c | 8688 | rawLen = mp_unsigned_bin_size(keyInt) + 1; |
wolfSSL | 15:117db924cf7c | 8689 | tmps[i] = (byte*)XMALLOC(rawLen + MAX_SEQ_SZ, key->heap, |
wolfSSL | 15:117db924cf7c | 8690 | DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 8691 | if (tmps[i] == NULL) { |
wolfSSL | 15:117db924cf7c | 8692 | ret = MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8693 | break; |
wolfSSL | 15:117db924cf7c | 8694 | } |
wolfSSL | 15:117db924cf7c | 8695 | |
wolfSSL | 15:117db924cf7c | 8696 | mpSz = SetASNIntMP(keyInt, MAX_RSA_INT_SZ, tmps[i]); |
wolfSSL | 15:117db924cf7c | 8697 | if (mpSz < 0) { |
wolfSSL | 15:117db924cf7c | 8698 | ret = mpSz; |
wolfSSL | 15:117db924cf7c | 8699 | break; |
wolfSSL | 15:117db924cf7c | 8700 | } |
wolfSSL | 15:117db924cf7c | 8701 | intTotalLen += (sizes[i] = mpSz); |
wolfSSL | 15:117db924cf7c | 8702 | } |
wolfSSL | 15:117db924cf7c | 8703 | |
wolfSSL | 15:117db924cf7c | 8704 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 8705 | FreeTmpRsas(tmps, key->heap); |
wolfSSL | 15:117db924cf7c | 8706 | return ret; |
wolfSSL | 15:117db924cf7c | 8707 | } |
wolfSSL | 15:117db924cf7c | 8708 | |
wolfSSL | 15:117db924cf7c | 8709 | /* make headers */ |
wolfSSL | 15:117db924cf7c | 8710 | verSz = SetMyVersion(0, ver, FALSE); |
wolfSSL | 15:117db924cf7c | 8711 | seqSz = SetSequence(verSz + intTotalLen, seq); |
wolfSSL | 15:117db924cf7c | 8712 | |
wolfSSL | 15:117db924cf7c | 8713 | outLen = seqSz + verSz + intTotalLen; |
wolfSSL | 15:117db924cf7c | 8714 | if (outLen > (int)inLen) { |
wolfSSL | 15:117db924cf7c | 8715 | FreeTmpRsas(tmps, key->heap); |
wolfSSL | 15:117db924cf7c | 8716 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8717 | } |
wolfSSL | 15:117db924cf7c | 8718 | |
wolfSSL | 15:117db924cf7c | 8719 | /* write to output */ |
wolfSSL | 15:117db924cf7c | 8720 | XMEMCPY(output, seq, seqSz); |
wolfSSL | 15:117db924cf7c | 8721 | j = seqSz; |
wolfSSL | 15:117db924cf7c | 8722 | XMEMCPY(output + j, ver, verSz); |
wolfSSL | 15:117db924cf7c | 8723 | j += verSz; |
wolfSSL | 15:117db924cf7c | 8724 | |
wolfSSL | 15:117db924cf7c | 8725 | for (i = 0; i < RSA_INTS; i++) { |
wolfSSL | 15:117db924cf7c | 8726 | XMEMCPY(output + j, tmps[i], sizes[i]); |
wolfSSL | 15:117db924cf7c | 8727 | j += sizes[i]; |
wolfSSL | 15:117db924cf7c | 8728 | } |
wolfSSL | 15:117db924cf7c | 8729 | FreeTmpRsas(tmps, key->heap); |
wolfSSL | 15:117db924cf7c | 8730 | |
wolfSSL | 15:117db924cf7c | 8731 | return outLen; |
wolfSSL | 15:117db924cf7c | 8732 | } |
wolfSSL | 15:117db924cf7c | 8733 | #endif |
wolfSSL | 15:117db924cf7c | 8734 | |
wolfSSL | 15:117db924cf7c | 8735 | #if (defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) |
wolfSSL | 15:117db924cf7c | 8736 | /* Convert Rsa Public key to DER format, write to output (inLen), return bytes |
wolfSSL | 15:117db924cf7c | 8737 | written */ |
wolfSSL | 15:117db924cf7c | 8738 | int wc_RsaKeyToPublicDer(RsaKey* key, byte* output, word32 inLen) |
wolfSSL | 15:117db924cf7c | 8739 | { |
wolfSSL | 15:117db924cf7c | 8740 | return SetRsaPublicKey(output, key, inLen, 1); |
wolfSSL | 15:117db924cf7c | 8741 | } |
wolfSSL | 15:117db924cf7c | 8742 | |
wolfSSL | 15:117db924cf7c | 8743 | #endif /* WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA */ |
wolfSSL | 15:117db924cf7c | 8744 | |
wolfSSL | 15:117db924cf7c | 8745 | |
wolfSSL | 15:117db924cf7c | 8746 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 8747 | |
wolfSSL | 15:117db924cf7c | 8748 | /* Initialize and Set Certificate defaults: |
wolfSSL | 15:117db924cf7c | 8749 | version = 3 (0x2) |
wolfSSL | 15:117db924cf7c | 8750 | serial = 0 |
wolfSSL | 15:117db924cf7c | 8751 | sigType = SHA_WITH_RSA |
wolfSSL | 15:117db924cf7c | 8752 | issuer = blank |
wolfSSL | 15:117db924cf7c | 8753 | daysValid = 500 |
wolfSSL | 15:117db924cf7c | 8754 | selfSigned = 1 (true) use subject as issuer |
wolfSSL | 15:117db924cf7c | 8755 | subject = blank |
wolfSSL | 15:117db924cf7c | 8756 | */ |
wolfSSL | 15:117db924cf7c | 8757 | int wc_InitCert(Cert* cert) |
wolfSSL | 15:117db924cf7c | 8758 | { |
wolfSSL | 15:117db924cf7c | 8759 | #ifdef WOLFSSL_MULTI_ATTRIB |
wolfSSL | 15:117db924cf7c | 8760 | int i = 0; |
wolfSSL | 15:117db924cf7c | 8761 | #endif |
wolfSSL | 15:117db924cf7c | 8762 | if (cert == NULL) { |
wolfSSL | 15:117db924cf7c | 8763 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8764 | } |
wolfSSL | 15:117db924cf7c | 8765 | |
wolfSSL | 15:117db924cf7c | 8766 | XMEMSET(cert, 0, sizeof(Cert)); |
wolfSSL | 15:117db924cf7c | 8767 | |
wolfSSL | 15:117db924cf7c | 8768 | cert->version = 2; /* version 3 is hex 2 */ |
wolfSSL | 15:117db924cf7c | 8769 | #ifndef NO_SHA |
wolfSSL | 15:117db924cf7c | 8770 | cert->sigType = CTC_SHAwRSA; |
wolfSSL | 15:117db924cf7c | 8771 | #elif !defined(NO_SHA256) |
wolfSSL | 15:117db924cf7c | 8772 | cert->sigType = CTC_SHA256wRSA; |
wolfSSL | 15:117db924cf7c | 8773 | #else |
wolfSSL | 15:117db924cf7c | 8774 | cert->sigType = 0; |
wolfSSL | 15:117db924cf7c | 8775 | #endif |
wolfSSL | 15:117db924cf7c | 8776 | cert->daysValid = 500; |
wolfSSL | 15:117db924cf7c | 8777 | cert->selfSigned = 1; |
wolfSSL | 15:117db924cf7c | 8778 | cert->keyType = RSA_KEY; |
wolfSSL | 15:117db924cf7c | 8779 | |
wolfSSL | 15:117db924cf7c | 8780 | cert->issuer.countryEnc = CTC_PRINTABLE; |
wolfSSL | 15:117db924cf7c | 8781 | cert->issuer.stateEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8782 | cert->issuer.localityEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8783 | cert->issuer.surEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8784 | cert->issuer.orgEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8785 | cert->issuer.unitEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8786 | cert->issuer.commonNameEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8787 | |
wolfSSL | 15:117db924cf7c | 8788 | cert->subject.countryEnc = CTC_PRINTABLE; |
wolfSSL | 15:117db924cf7c | 8789 | cert->subject.stateEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8790 | cert->subject.localityEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8791 | cert->subject.surEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8792 | cert->subject.orgEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8793 | cert->subject.unitEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8794 | cert->subject.commonNameEnc = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8795 | |
wolfSSL | 15:117db924cf7c | 8796 | #ifdef WOLFSSL_MULTI_ATTRIB |
wolfSSL | 15:117db924cf7c | 8797 | for (i = 0; i < CTC_MAX_ATTRIB; i++) { |
wolfSSL | 15:117db924cf7c | 8798 | cert->issuer.name[i].type = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8799 | cert->subject.name[i].type = CTC_UTF8; |
wolfSSL | 15:117db924cf7c | 8800 | } |
wolfSSL | 15:117db924cf7c | 8801 | #endif /* WOLFSSL_MULTI_ATTRIB */ |
wolfSSL | 15:117db924cf7c | 8802 | |
wolfSSL | 15:117db924cf7c | 8803 | #ifdef WOLFSSL_HEAP_TEST |
wolfSSL | 15:117db924cf7c | 8804 | cert->heap = (void*)WOLFSSL_HEAP_TEST; |
wolfSSL | 15:117db924cf7c | 8805 | #endif |
wolfSSL | 15:117db924cf7c | 8806 | |
wolfSSL | 15:117db924cf7c | 8807 | return 0; |
wolfSSL | 15:117db924cf7c | 8808 | } |
wolfSSL | 15:117db924cf7c | 8809 | |
wolfSSL | 15:117db924cf7c | 8810 | |
wolfSSL | 15:117db924cf7c | 8811 | /* DER encoded x509 Certificate */ |
wolfSSL | 15:117db924cf7c | 8812 | typedef struct DerCert { |
wolfSSL | 15:117db924cf7c | 8813 | byte size[MAX_LENGTH_SZ]; /* length encoded */ |
wolfSSL | 15:117db924cf7c | 8814 | byte version[MAX_VERSION_SZ]; /* version encoded */ |
wolfSSL | 15:117db924cf7c | 8815 | byte serial[(int)CTC_SERIAL_SIZE + (int)MAX_LENGTH_SZ]; /* serial number encoded */ |
wolfSSL | 15:117db924cf7c | 8816 | byte sigAlgo[MAX_ALGO_SZ]; /* signature algo encoded */ |
wolfSSL | 15:117db924cf7c | 8817 | byte issuer[ASN_NAME_MAX]; /* issuer encoded */ |
wolfSSL | 15:117db924cf7c | 8818 | byte subject[ASN_NAME_MAX]; /* subject encoded */ |
wolfSSL | 15:117db924cf7c | 8819 | byte validity[MAX_DATE_SIZE*2 + MAX_SEQ_SZ*2]; /* before and after dates */ |
wolfSSL | 15:117db924cf7c | 8820 | byte publicKey[MAX_PUBLIC_KEY_SZ]; /* rsa / ntru public key encoded */ |
wolfSSL | 15:117db924cf7c | 8821 | byte ca[MAX_CA_SZ]; /* basic constraint CA true size */ |
wolfSSL | 15:117db924cf7c | 8822 | byte extensions[MAX_EXTENSIONS_SZ]; /* all extensions */ |
wolfSSL | 15:117db924cf7c | 8823 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 8824 | byte skid[MAX_KID_SZ]; /* Subject Key Identifier extension */ |
wolfSSL | 15:117db924cf7c | 8825 | byte akid[MAX_KID_SZ]; /* Authority Key Identifier extension */ |
wolfSSL | 15:117db924cf7c | 8826 | byte keyUsage[MAX_KEYUSAGE_SZ]; /* Key Usage extension */ |
wolfSSL | 15:117db924cf7c | 8827 | byte extKeyUsage[MAX_EXTKEYUSAGE_SZ]; /* Extended Key Usage extension */ |
wolfSSL | 15:117db924cf7c | 8828 | byte certPolicies[MAX_CERTPOL_NB*MAX_CERTPOL_SZ]; /* Certificate Policies */ |
wolfSSL | 15:117db924cf7c | 8829 | #endif |
wolfSSL | 15:117db924cf7c | 8830 | #ifdef WOLFSSL_CERT_REQ |
wolfSSL | 15:117db924cf7c | 8831 | byte attrib[MAX_ATTRIB_SZ]; /* Cert req attributes encoded */ |
wolfSSL | 15:117db924cf7c | 8832 | #endif |
wolfSSL | 15:117db924cf7c | 8833 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 8834 | byte altNames[CTC_MAX_ALT_SIZE]; /* Alternative Names encoded */ |
wolfSSL | 15:117db924cf7c | 8835 | #endif |
wolfSSL | 15:117db924cf7c | 8836 | int sizeSz; /* encoded size length */ |
wolfSSL | 15:117db924cf7c | 8837 | int versionSz; /* encoded version length */ |
wolfSSL | 15:117db924cf7c | 8838 | int serialSz; /* encoded serial length */ |
wolfSSL | 15:117db924cf7c | 8839 | int sigAlgoSz; /* encoded sig alog length */ |
wolfSSL | 15:117db924cf7c | 8840 | int issuerSz; /* encoded issuer length */ |
wolfSSL | 15:117db924cf7c | 8841 | int subjectSz; /* encoded subject length */ |
wolfSSL | 15:117db924cf7c | 8842 | int validitySz; /* encoded validity length */ |
wolfSSL | 15:117db924cf7c | 8843 | int publicKeySz; /* encoded public key length */ |
wolfSSL | 15:117db924cf7c | 8844 | int caSz; /* encoded CA extension length */ |
wolfSSL | 15:117db924cf7c | 8845 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 8846 | int skidSz; /* encoded SKID extension length */ |
wolfSSL | 15:117db924cf7c | 8847 | int akidSz; /* encoded SKID extension length */ |
wolfSSL | 15:117db924cf7c | 8848 | int keyUsageSz; /* encoded KeyUsage extension length */ |
wolfSSL | 15:117db924cf7c | 8849 | int extKeyUsageSz; /* encoded ExtendedKeyUsage extension length */ |
wolfSSL | 15:117db924cf7c | 8850 | int certPoliciesSz; /* encoded CertPolicies extension length*/ |
wolfSSL | 15:117db924cf7c | 8851 | #endif |
wolfSSL | 15:117db924cf7c | 8852 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 8853 | int altNamesSz; /* encoded AltNames extension length */ |
wolfSSL | 15:117db924cf7c | 8854 | #endif |
wolfSSL | 15:117db924cf7c | 8855 | int extensionsSz; /* encoded extensions total length */ |
wolfSSL | 15:117db924cf7c | 8856 | int total; /* total encoded lengths */ |
wolfSSL | 15:117db924cf7c | 8857 | #ifdef WOLFSSL_CERT_REQ |
wolfSSL | 15:117db924cf7c | 8858 | int attribSz; |
wolfSSL | 15:117db924cf7c | 8859 | #endif |
wolfSSL | 15:117db924cf7c | 8860 | } DerCert; |
wolfSSL | 15:117db924cf7c | 8861 | |
wolfSSL | 15:117db924cf7c | 8862 | |
wolfSSL | 15:117db924cf7c | 8863 | #ifdef WOLFSSL_CERT_REQ |
wolfSSL | 15:117db924cf7c | 8864 | |
wolfSSL | 15:117db924cf7c | 8865 | /* Write a set header to output */ |
wolfSSL | 15:117db924cf7c | 8866 | static word32 SetUTF8String(word32 len, byte* output) |
wolfSSL | 15:117db924cf7c | 8867 | { |
wolfSSL | 15:117db924cf7c | 8868 | output[0] = ASN_UTF8STRING; |
wolfSSL | 15:117db924cf7c | 8869 | return SetLength(len, output + 1) + 1; |
wolfSSL | 15:117db924cf7c | 8870 | } |
wolfSSL | 15:117db924cf7c | 8871 | |
wolfSSL | 15:117db924cf7c | 8872 | #endif /* WOLFSSL_CERT_REQ */ |
wolfSSL | 15:117db924cf7c | 8873 | |
wolfSSL | 15:117db924cf7c | 8874 | #endif /*WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 8875 | |
wolfSSL | 15:117db924cf7c | 8876 | #if defined(HAVE_ECC) |
wolfSSL | 15:117db924cf7c | 8877 | |
wolfSSL | 15:117db924cf7c | 8878 | /* Write a public ECC key to output */ |
wolfSSL | 15:117db924cf7c | 8879 | static int SetEccPublicKey(byte* output, ecc_key* key, int with_header) |
wolfSSL | 15:117db924cf7c | 8880 | { |
wolfSSL | 15:117db924cf7c | 8881 | byte bitString[1 + MAX_LENGTH_SZ + 1]; |
wolfSSL | 15:117db924cf7c | 8882 | int algoSz; |
wolfSSL | 15:117db924cf7c | 8883 | int curveSz; |
wolfSSL | 15:117db924cf7c | 8884 | int bitStringSz; |
wolfSSL | 15:117db924cf7c | 8885 | int idx; |
wolfSSL | 15:117db924cf7c | 8886 | word32 pubSz = ECC_BUFSIZE; |
wolfSSL | 15:117db924cf7c | 8887 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8888 | byte* algo = NULL; |
wolfSSL | 15:117db924cf7c | 8889 | byte* curve = NULL; |
wolfSSL | 15:117db924cf7c | 8890 | byte* pub = NULL; |
wolfSSL | 15:117db924cf7c | 8891 | #else |
wolfSSL | 15:117db924cf7c | 8892 | byte algo[MAX_ALGO_SZ]; |
wolfSSL | 15:117db924cf7c | 8893 | byte curve[MAX_ALGO_SZ]; |
wolfSSL | 15:117db924cf7c | 8894 | byte pub[ECC_BUFSIZE]; |
wolfSSL | 15:117db924cf7c | 8895 | #endif |
wolfSSL | 15:117db924cf7c | 8896 | int ret; |
wolfSSL | 15:117db924cf7c | 8897 | |
wolfSSL | 15:117db924cf7c | 8898 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8899 | pub = (byte*)XMALLOC(ECC_BUFSIZE, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8900 | if (pub == NULL) |
wolfSSL | 15:117db924cf7c | 8901 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8902 | #endif |
wolfSSL | 15:117db924cf7c | 8903 | |
wolfSSL | 15:117db924cf7c | 8904 | ret = wc_ecc_export_x963(key, pub, &pubSz); |
wolfSSL | 15:117db924cf7c | 8905 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 8906 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8907 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8908 | #endif |
wolfSSL | 15:117db924cf7c | 8909 | return ret; |
wolfSSL | 15:117db924cf7c | 8910 | } |
wolfSSL | 15:117db924cf7c | 8911 | |
wolfSSL | 15:117db924cf7c | 8912 | /* headers */ |
wolfSSL | 15:117db924cf7c | 8913 | if (with_header) { |
wolfSSL | 15:117db924cf7c | 8914 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8915 | curve = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8916 | if (curve == NULL) { |
wolfSSL | 15:117db924cf7c | 8917 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8918 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8919 | } |
wolfSSL | 15:117db924cf7c | 8920 | #endif |
wolfSSL | 15:117db924cf7c | 8921 | curveSz = SetCurve(key, curve); |
wolfSSL | 15:117db924cf7c | 8922 | if (curveSz <= 0) { |
wolfSSL | 15:117db924cf7c | 8923 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8924 | XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8925 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8926 | #endif |
wolfSSL | 15:117db924cf7c | 8927 | return curveSz; |
wolfSSL | 15:117db924cf7c | 8928 | } |
wolfSSL | 15:117db924cf7c | 8929 | |
wolfSSL | 15:117db924cf7c | 8930 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8931 | algo = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8932 | if (algo == NULL) { |
wolfSSL | 15:117db924cf7c | 8933 | XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8934 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8935 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 8936 | } |
wolfSSL | 15:117db924cf7c | 8937 | #endif |
wolfSSL | 15:117db924cf7c | 8938 | algoSz = SetAlgoID(ECDSAk, algo, oidKeyType, curveSz); |
wolfSSL | 15:117db924cf7c | 8939 | |
wolfSSL | 15:117db924cf7c | 8940 | bitStringSz = SetBitString(pubSz, 0, bitString); |
wolfSSL | 15:117db924cf7c | 8941 | |
wolfSSL | 15:117db924cf7c | 8942 | idx = SetSequence(pubSz + curveSz + bitStringSz + algoSz, output); |
wolfSSL | 15:117db924cf7c | 8943 | /* algo */ |
wolfSSL | 15:117db924cf7c | 8944 | XMEMCPY(output + idx, algo, algoSz); |
wolfSSL | 15:117db924cf7c | 8945 | idx += algoSz; |
wolfSSL | 15:117db924cf7c | 8946 | /* curve */ |
wolfSSL | 15:117db924cf7c | 8947 | XMEMCPY(output + idx, curve, curveSz); |
wolfSSL | 15:117db924cf7c | 8948 | idx += curveSz; |
wolfSSL | 15:117db924cf7c | 8949 | /* bit string */ |
wolfSSL | 15:117db924cf7c | 8950 | XMEMCPY(output + idx, bitString, bitStringSz); |
wolfSSL | 15:117db924cf7c | 8951 | idx += bitStringSz; |
wolfSSL | 15:117db924cf7c | 8952 | } |
wolfSSL | 15:117db924cf7c | 8953 | else |
wolfSSL | 15:117db924cf7c | 8954 | idx = 0; |
wolfSSL | 15:117db924cf7c | 8955 | |
wolfSSL | 15:117db924cf7c | 8956 | /* pub */ |
wolfSSL | 15:117db924cf7c | 8957 | XMEMCPY(output + idx, pub, pubSz); |
wolfSSL | 15:117db924cf7c | 8958 | idx += pubSz; |
wolfSSL | 15:117db924cf7c | 8959 | |
wolfSSL | 15:117db924cf7c | 8960 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 8961 | if (with_header) { |
wolfSSL | 15:117db924cf7c | 8962 | XFREE(algo, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8963 | XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8964 | } |
wolfSSL | 15:117db924cf7c | 8965 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 8966 | #endif |
wolfSSL | 15:117db924cf7c | 8967 | |
wolfSSL | 15:117db924cf7c | 8968 | return idx; |
wolfSSL | 15:117db924cf7c | 8969 | } |
wolfSSL | 15:117db924cf7c | 8970 | |
wolfSSL | 15:117db924cf7c | 8971 | |
wolfSSL | 15:117db924cf7c | 8972 | /* returns the size of buffer used, the public ECC key in DER format is stored |
wolfSSL | 15:117db924cf7c | 8973 | in output buffer |
wolfSSL | 15:117db924cf7c | 8974 | with_AlgCurve is a flag for when to include a header that has the Algorithm |
wolfSSL | 15:117db924cf7c | 8975 | and Curve infromation */ |
wolfSSL | 15:117db924cf7c | 8976 | int wc_EccPublicKeyToDer(ecc_key* key, byte* output, word32 inLen, |
wolfSSL | 15:117db924cf7c | 8977 | int with_AlgCurve) |
wolfSSL | 15:117db924cf7c | 8978 | { |
wolfSSL | 15:117db924cf7c | 8979 | word32 infoSz = 0; |
wolfSSL | 15:117db924cf7c | 8980 | word32 keySz = 0; |
wolfSSL | 15:117db924cf7c | 8981 | int ret; |
wolfSSL | 15:117db924cf7c | 8982 | |
wolfSSL | 15:117db924cf7c | 8983 | if (output == NULL || key == NULL) { |
wolfSSL | 15:117db924cf7c | 8984 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 8985 | } |
wolfSSL | 15:117db924cf7c | 8986 | |
wolfSSL | 15:117db924cf7c | 8987 | if (with_AlgCurve) { |
wolfSSL | 15:117db924cf7c | 8988 | /* buffer space for algorithm/curve */ |
wolfSSL | 15:117db924cf7c | 8989 | infoSz += MAX_SEQ_SZ; |
wolfSSL | 15:117db924cf7c | 8990 | infoSz += 2 * MAX_ALGO_SZ; |
wolfSSL | 15:117db924cf7c | 8991 | |
wolfSSL | 15:117db924cf7c | 8992 | /* buffer space for public key sequence */ |
wolfSSL | 15:117db924cf7c | 8993 | infoSz += MAX_SEQ_SZ; |
wolfSSL | 15:117db924cf7c | 8994 | infoSz += TRAILING_ZERO; |
wolfSSL | 15:117db924cf7c | 8995 | } |
wolfSSL | 15:117db924cf7c | 8996 | |
wolfSSL | 15:117db924cf7c | 8997 | if ((ret = wc_ecc_export_x963(key, NULL, &keySz)) != LENGTH_ONLY_E) { |
wolfSSL | 15:117db924cf7c | 8998 | WOLFSSL_MSG("Error in getting ECC public key size"); |
wolfSSL | 15:117db924cf7c | 8999 | return ret; |
wolfSSL | 15:117db924cf7c | 9000 | } |
wolfSSL | 15:117db924cf7c | 9001 | |
wolfSSL | 15:117db924cf7c | 9002 | if (inLen < keySz + infoSz) { |
wolfSSL | 15:117db924cf7c | 9003 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9004 | } |
wolfSSL | 15:117db924cf7c | 9005 | |
wolfSSL | 15:117db924cf7c | 9006 | return SetEccPublicKey(output, key, with_AlgCurve); |
wolfSSL | 15:117db924cf7c | 9007 | } |
wolfSSL | 15:117db924cf7c | 9008 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 9009 | |
wolfSSL | 15:117db924cf7c | 9010 | #if defined(HAVE_ED25519) && (defined(WOLFSSL_CERT_GEN) || \ |
wolfSSL | 15:117db924cf7c | 9011 | defined(WOLFSSL_KEY_GEN)) |
wolfSSL | 15:117db924cf7c | 9012 | |
wolfSSL | 15:117db924cf7c | 9013 | /* Write a public ECC key to output */ |
wolfSSL | 15:117db924cf7c | 9014 | static int SetEd25519PublicKey(byte* output, ed25519_key* key, int with_header) |
wolfSSL | 15:117db924cf7c | 9015 | { |
wolfSSL | 15:117db924cf7c | 9016 | byte bitString[1 + MAX_LENGTH_SZ + 1]; |
wolfSSL | 15:117db924cf7c | 9017 | int algoSz; |
wolfSSL | 15:117db924cf7c | 9018 | int bitStringSz; |
wolfSSL | 15:117db924cf7c | 9019 | int idx; |
wolfSSL | 15:117db924cf7c | 9020 | word32 pubSz = ED25519_PUB_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 9021 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9022 | byte* algo = NULL; |
wolfSSL | 15:117db924cf7c | 9023 | byte* pub = NULL; |
wolfSSL | 15:117db924cf7c | 9024 | #else |
wolfSSL | 15:117db924cf7c | 9025 | byte algo[MAX_ALGO_SZ]; |
wolfSSL | 15:117db924cf7c | 9026 | byte pub[ED25519_PUB_KEY_SIZE]; |
wolfSSL | 15:117db924cf7c | 9027 | #endif |
wolfSSL | 15:117db924cf7c | 9028 | |
wolfSSL | 15:117db924cf7c | 9029 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9030 | pub = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9031 | if (pub == NULL) |
wolfSSL | 15:117db924cf7c | 9032 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 9033 | #endif |
wolfSSL | 15:117db924cf7c | 9034 | |
wolfSSL | 15:117db924cf7c | 9035 | int ret = wc_ed25519_export_public(key, pub, &pubSz); |
wolfSSL | 15:117db924cf7c | 9036 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 9037 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9038 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9039 | #endif |
wolfSSL | 15:117db924cf7c | 9040 | return ret; |
wolfSSL | 15:117db924cf7c | 9041 | } |
wolfSSL | 15:117db924cf7c | 9042 | |
wolfSSL | 15:117db924cf7c | 9043 | /* headers */ |
wolfSSL | 15:117db924cf7c | 9044 | if (with_header) { |
wolfSSL | 15:117db924cf7c | 9045 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9046 | algo = (byte*)XMALLOC(MAX_ALGO_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9047 | if (algo == NULL) { |
wolfSSL | 15:117db924cf7c | 9048 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9049 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 9050 | } |
wolfSSL | 15:117db924cf7c | 9051 | #endif |
wolfSSL | 15:117db924cf7c | 9052 | algoSz = SetAlgoID(ED25519k, algo, oidKeyType, 0); |
wolfSSL | 15:117db924cf7c | 9053 | |
wolfSSL | 15:117db924cf7c | 9054 | bitStringSz = SetBitString(pubSz, 0, bitString); |
wolfSSL | 15:117db924cf7c | 9055 | |
wolfSSL | 15:117db924cf7c | 9056 | idx = SetSequence(pubSz + bitStringSz + algoSz, output); |
wolfSSL | 15:117db924cf7c | 9057 | /* algo */ |
wolfSSL | 15:117db924cf7c | 9058 | XMEMCPY(output + idx, algo, algoSz); |
wolfSSL | 15:117db924cf7c | 9059 | idx += algoSz; |
wolfSSL | 15:117db924cf7c | 9060 | /* bit string */ |
wolfSSL | 15:117db924cf7c | 9061 | XMEMCPY(output + idx, bitString, bitStringSz); |
wolfSSL | 15:117db924cf7c | 9062 | idx += bitStringSz; |
wolfSSL | 15:117db924cf7c | 9063 | } |
wolfSSL | 15:117db924cf7c | 9064 | else |
wolfSSL | 15:117db924cf7c | 9065 | idx = 0; |
wolfSSL | 15:117db924cf7c | 9066 | |
wolfSSL | 15:117db924cf7c | 9067 | /* pub */ |
wolfSSL | 15:117db924cf7c | 9068 | XMEMCPY(output + idx, pub, pubSz); |
wolfSSL | 15:117db924cf7c | 9069 | idx += pubSz; |
wolfSSL | 15:117db924cf7c | 9070 | |
wolfSSL | 15:117db924cf7c | 9071 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9072 | if (with_header) { |
wolfSSL | 15:117db924cf7c | 9073 | XFREE(algo, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9074 | } |
wolfSSL | 15:117db924cf7c | 9075 | XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9076 | #endif |
wolfSSL | 15:117db924cf7c | 9077 | |
wolfSSL | 15:117db924cf7c | 9078 | return idx; |
wolfSSL | 15:117db924cf7c | 9079 | } |
wolfSSL | 15:117db924cf7c | 9080 | |
wolfSSL | 15:117db924cf7c | 9081 | int wc_Ed25519PublicKeyToDer(ed25519_key* key, byte* output, word32 inLen, |
wolfSSL | 15:117db924cf7c | 9082 | int withAlg) |
wolfSSL | 15:117db924cf7c | 9083 | { |
wolfSSL | 15:117db924cf7c | 9084 | word32 infoSz = 0; |
wolfSSL | 15:117db924cf7c | 9085 | word32 keySz = 0; |
wolfSSL | 15:117db924cf7c | 9086 | int ret; |
wolfSSL | 15:117db924cf7c | 9087 | |
wolfSSL | 15:117db924cf7c | 9088 | if (output == NULL || key == NULL) { |
wolfSSL | 15:117db924cf7c | 9089 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9090 | } |
wolfSSL | 15:117db924cf7c | 9091 | |
wolfSSL | 15:117db924cf7c | 9092 | if (withAlg) { |
wolfSSL | 15:117db924cf7c | 9093 | /* buffer space for algorithm */ |
wolfSSL | 15:117db924cf7c | 9094 | infoSz += MAX_SEQ_SZ; |
wolfSSL | 15:117db924cf7c | 9095 | infoSz += MAX_ALGO_SZ; |
wolfSSL | 15:117db924cf7c | 9096 | |
wolfSSL | 15:117db924cf7c | 9097 | /* buffer space for public key sequence */ |
wolfSSL | 15:117db924cf7c | 9098 | infoSz += MAX_SEQ_SZ; |
wolfSSL | 15:117db924cf7c | 9099 | infoSz += TRAILING_ZERO; |
wolfSSL | 15:117db924cf7c | 9100 | } |
wolfSSL | 15:117db924cf7c | 9101 | |
wolfSSL | 15:117db924cf7c | 9102 | if ((ret = wc_ed25519_export_public(key, output, &keySz)) != BUFFER_E) { |
wolfSSL | 15:117db924cf7c | 9103 | WOLFSSL_MSG("Error in getting ECC public key size"); |
wolfSSL | 15:117db924cf7c | 9104 | return ret; |
wolfSSL | 15:117db924cf7c | 9105 | } |
wolfSSL | 15:117db924cf7c | 9106 | |
wolfSSL | 15:117db924cf7c | 9107 | if (inLen < keySz + infoSz) { |
wolfSSL | 15:117db924cf7c | 9108 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9109 | } |
wolfSSL | 15:117db924cf7c | 9110 | |
wolfSSL | 15:117db924cf7c | 9111 | return SetEd25519PublicKey(output, key, withAlg); |
wolfSSL | 15:117db924cf7c | 9112 | } |
wolfSSL | 15:117db924cf7c | 9113 | #endif /* HAVE_ED25519 && (WOLFSSL_CERT_GEN || WOLFSSL_KEY_GEN) */ |
wolfSSL | 15:117db924cf7c | 9114 | |
wolfSSL | 15:117db924cf7c | 9115 | |
wolfSSL | 15:117db924cf7c | 9116 | #ifdef WOLFSSL_CERT_GEN |
wolfSSL | 15:117db924cf7c | 9117 | |
wolfSSL | 15:117db924cf7c | 9118 | static WC_INLINE byte itob(int number) |
wolfSSL | 15:117db924cf7c | 9119 | { |
wolfSSL | 15:117db924cf7c | 9120 | return (byte)number + 0x30; |
wolfSSL | 15:117db924cf7c | 9121 | } |
wolfSSL | 15:117db924cf7c | 9122 | |
wolfSSL | 15:117db924cf7c | 9123 | |
wolfSSL | 15:117db924cf7c | 9124 | /* write time to output, format */ |
wolfSSL | 15:117db924cf7c | 9125 | static void SetTime(struct tm* date, byte* output) |
wolfSSL | 15:117db924cf7c | 9126 | { |
wolfSSL | 15:117db924cf7c | 9127 | int i = 0; |
wolfSSL | 15:117db924cf7c | 9128 | |
wolfSSL | 15:117db924cf7c | 9129 | output[i++] = itob((date->tm_year % 10000) / 1000); |
wolfSSL | 15:117db924cf7c | 9130 | output[i++] = itob((date->tm_year % 1000) / 100); |
wolfSSL | 15:117db924cf7c | 9131 | output[i++] = itob((date->tm_year % 100) / 10); |
wolfSSL | 15:117db924cf7c | 9132 | output[i++] = itob( date->tm_year % 10); |
wolfSSL | 15:117db924cf7c | 9133 | |
wolfSSL | 15:117db924cf7c | 9134 | output[i++] = itob(date->tm_mon / 10); |
wolfSSL | 15:117db924cf7c | 9135 | output[i++] = itob(date->tm_mon % 10); |
wolfSSL | 15:117db924cf7c | 9136 | |
wolfSSL | 15:117db924cf7c | 9137 | output[i++] = itob(date->tm_mday / 10); |
wolfSSL | 15:117db924cf7c | 9138 | output[i++] = itob(date->tm_mday % 10); |
wolfSSL | 15:117db924cf7c | 9139 | |
wolfSSL | 15:117db924cf7c | 9140 | output[i++] = itob(date->tm_hour / 10); |
wolfSSL | 15:117db924cf7c | 9141 | output[i++] = itob(date->tm_hour % 10); |
wolfSSL | 15:117db924cf7c | 9142 | |
wolfSSL | 15:117db924cf7c | 9143 | output[i++] = itob(date->tm_min / 10); |
wolfSSL | 15:117db924cf7c | 9144 | output[i++] = itob(date->tm_min % 10); |
wolfSSL | 15:117db924cf7c | 9145 | |
wolfSSL | 15:117db924cf7c | 9146 | output[i++] = itob(date->tm_sec / 10); |
wolfSSL | 15:117db924cf7c | 9147 | output[i++] = itob(date->tm_sec % 10); |
wolfSSL | 15:117db924cf7c | 9148 | |
wolfSSL | 15:117db924cf7c | 9149 | output[i] = 'Z'; /* Zulu profile */ |
wolfSSL | 15:117db924cf7c | 9150 | } |
wolfSSL | 15:117db924cf7c | 9151 | |
wolfSSL | 15:117db924cf7c | 9152 | |
wolfSSL | 15:117db924cf7c | 9153 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 9154 | |
wolfSSL | 15:117db924cf7c | 9155 | /* Copy Dates from cert, return bytes written */ |
wolfSSL | 15:117db924cf7c | 9156 | static int CopyValidity(byte* output, Cert* cert) |
wolfSSL | 15:117db924cf7c | 9157 | { |
wolfSSL | 15:117db924cf7c | 9158 | int seqSz; |
wolfSSL | 15:117db924cf7c | 9159 | |
wolfSSL | 15:117db924cf7c | 9160 | WOLFSSL_ENTER("CopyValidity"); |
wolfSSL | 15:117db924cf7c | 9161 | |
wolfSSL | 15:117db924cf7c | 9162 | /* headers and output */ |
wolfSSL | 15:117db924cf7c | 9163 | seqSz = SetSequence(cert->beforeDateSz + cert->afterDateSz, output); |
wolfSSL | 15:117db924cf7c | 9164 | XMEMCPY(output + seqSz, cert->beforeDate, cert->beforeDateSz); |
wolfSSL | 15:117db924cf7c | 9165 | XMEMCPY(output + seqSz + cert->beforeDateSz, cert->afterDate, |
wolfSSL | 15:117db924cf7c | 9166 | cert->afterDateSz); |
wolfSSL | 15:117db924cf7c | 9167 | return seqSz + cert->beforeDateSz + cert->afterDateSz; |
wolfSSL | 15:117db924cf7c | 9168 | } |
wolfSSL | 15:117db924cf7c | 9169 | |
wolfSSL | 15:117db924cf7c | 9170 | #endif |
wolfSSL | 15:117db924cf7c | 9171 | |
wolfSSL | 15:117db924cf7c | 9172 | |
wolfSSL | 15:117db924cf7c | 9173 | /* Set Date validity from now until now + daysValid |
wolfSSL | 15:117db924cf7c | 9174 | * return size in bytes written to output, 0 on error */ |
wolfSSL | 15:117db924cf7c | 9175 | static int SetValidity(byte* output, int daysValid) |
wolfSSL | 15:117db924cf7c | 9176 | { |
wolfSSL | 15:117db924cf7c | 9177 | byte before[MAX_DATE_SIZE]; |
wolfSSL | 15:117db924cf7c | 9178 | byte after[MAX_DATE_SIZE]; |
wolfSSL | 15:117db924cf7c | 9179 | |
wolfSSL | 15:117db924cf7c | 9180 | int beforeSz; |
wolfSSL | 15:117db924cf7c | 9181 | int afterSz; |
wolfSSL | 15:117db924cf7c | 9182 | int seqSz; |
wolfSSL | 15:117db924cf7c | 9183 | |
wolfSSL | 15:117db924cf7c | 9184 | time_t now; |
wolfSSL | 15:117db924cf7c | 9185 | time_t then; |
wolfSSL | 15:117db924cf7c | 9186 | struct tm* tmpTime = NULL; |
wolfSSL | 15:117db924cf7c | 9187 | struct tm* expandedTime; |
wolfSSL | 15:117db924cf7c | 9188 | struct tm localTime; |
wolfSSL | 15:117db924cf7c | 9189 | |
wolfSSL | 15:117db924cf7c | 9190 | #if defined(NEED_TMP_TIME) |
wolfSSL | 15:117db924cf7c | 9191 | /* for use with gmtime_r */ |
wolfSSL | 15:117db924cf7c | 9192 | struct tm tmpTimeStorage; |
wolfSSL | 15:117db924cf7c | 9193 | tmpTime = &tmpTimeStorage; |
wolfSSL | 15:117db924cf7c | 9194 | #else |
wolfSSL | 15:117db924cf7c | 9195 | (void)tmpTime; |
wolfSSL | 15:117db924cf7c | 9196 | #endif |
wolfSSL | 15:117db924cf7c | 9197 | |
wolfSSL | 15:117db924cf7c | 9198 | now = XTIME(0); |
wolfSSL | 15:117db924cf7c | 9199 | |
wolfSSL | 15:117db924cf7c | 9200 | /* before now */ |
wolfSSL | 15:117db924cf7c | 9201 | before[0] = ASN_GENERALIZED_TIME; |
wolfSSL | 15:117db924cf7c | 9202 | beforeSz = SetLength(ASN_GEN_TIME_SZ, before + 1) + 1; /* gen tag */ |
wolfSSL | 15:117db924cf7c | 9203 | |
wolfSSL | 15:117db924cf7c | 9204 | /* subtract 1 day of seconds for more compliance */ |
wolfSSL | 15:117db924cf7c | 9205 | then = now - 86400; |
wolfSSL | 15:117db924cf7c | 9206 | expandedTime = XGMTIME(&then, tmpTime); |
wolfSSL | 15:117db924cf7c | 9207 | if (expandedTime == NULL) { |
wolfSSL | 15:117db924cf7c | 9208 | WOLFSSL_MSG("XGMTIME failed"); |
wolfSSL | 15:117db924cf7c | 9209 | return 0; /* error */ |
wolfSSL | 15:117db924cf7c | 9210 | } |
wolfSSL | 15:117db924cf7c | 9211 | localTime = *expandedTime; |
wolfSSL | 15:117db924cf7c | 9212 | |
wolfSSL | 15:117db924cf7c | 9213 | /* adjust */ |
wolfSSL | 15:117db924cf7c | 9214 | localTime.tm_year += 1900; |
wolfSSL | 15:117db924cf7c | 9215 | localTime.tm_mon += 1; |
wolfSSL | 15:117db924cf7c | 9216 | |
wolfSSL | 15:117db924cf7c | 9217 | SetTime(&localTime, before + beforeSz); |
wolfSSL | 15:117db924cf7c | 9218 | beforeSz += ASN_GEN_TIME_SZ; |
wolfSSL | 15:117db924cf7c | 9219 | |
wolfSSL | 15:117db924cf7c | 9220 | after[0] = ASN_GENERALIZED_TIME; |
wolfSSL | 15:117db924cf7c | 9221 | afterSz = SetLength(ASN_GEN_TIME_SZ, after + 1) + 1; /* gen tag */ |
wolfSSL | 15:117db924cf7c | 9222 | |
wolfSSL | 15:117db924cf7c | 9223 | /* add daysValid of seconds */ |
wolfSSL | 15:117db924cf7c | 9224 | then = now + (daysValid * 3600); |
wolfSSL | 15:117db924cf7c | 9225 | expandedTime = XGMTIME(&then, tmpTime); |
wolfSSL | 15:117db924cf7c | 9226 | if (expandedTime == NULL) { |
wolfSSL | 15:117db924cf7c | 9227 | WOLFSSL_MSG("XGMTIME failed"); |
wolfSSL | 15:117db924cf7c | 9228 | return 0; /* error */ |
wolfSSL | 15:117db924cf7c | 9229 | } |
wolfSSL | 15:117db924cf7c | 9230 | localTime = *expandedTime; |
wolfSSL | 15:117db924cf7c | 9231 | |
wolfSSL | 15:117db924cf7c | 9232 | /* adjust */ |
wolfSSL | 15:117db924cf7c | 9233 | localTime.tm_year += 1900; |
wolfSSL | 15:117db924cf7c | 9234 | localTime.tm_mon += 1; |
wolfSSL | 15:117db924cf7c | 9235 | |
wolfSSL | 15:117db924cf7c | 9236 | SetTime(&localTime, after + afterSz); |
wolfSSL | 15:117db924cf7c | 9237 | afterSz += ASN_GEN_TIME_SZ; |
wolfSSL | 15:117db924cf7c | 9238 | |
wolfSSL | 15:117db924cf7c | 9239 | /* headers and output */ |
wolfSSL | 15:117db924cf7c | 9240 | seqSz = SetSequence(beforeSz + afterSz, output); |
wolfSSL | 15:117db924cf7c | 9241 | XMEMCPY(output + seqSz, before, beforeSz); |
wolfSSL | 15:117db924cf7c | 9242 | XMEMCPY(output + seqSz + beforeSz, after, afterSz); |
wolfSSL | 15:117db924cf7c | 9243 | |
wolfSSL | 15:117db924cf7c | 9244 | return seqSz + beforeSz + afterSz; |
wolfSSL | 15:117db924cf7c | 9245 | } |
wolfSSL | 15:117db924cf7c | 9246 | |
wolfSSL | 15:117db924cf7c | 9247 | |
wolfSSL | 15:117db924cf7c | 9248 | /* ASN Encoded Name field */ |
wolfSSL | 15:117db924cf7c | 9249 | typedef struct EncodedName { |
wolfSSL | 15:117db924cf7c | 9250 | int nameLen; /* actual string value length */ |
wolfSSL | 15:117db924cf7c | 9251 | int totalLen; /* total encoded length */ |
wolfSSL | 15:117db924cf7c | 9252 | int type; /* type of name */ |
wolfSSL | 15:117db924cf7c | 9253 | int used; /* are we actually using this one */ |
wolfSSL | 15:117db924cf7c | 9254 | byte encoded[CTC_NAME_SIZE * 2]; /* encoding */ |
wolfSSL | 15:117db924cf7c | 9255 | } EncodedName; |
wolfSSL | 15:117db924cf7c | 9256 | |
wolfSSL | 15:117db924cf7c | 9257 | |
wolfSSL | 15:117db924cf7c | 9258 | /* Get Which Name from index */ |
wolfSSL | 15:117db924cf7c | 9259 | static const char* GetOneName(CertName* name, int idx) |
wolfSSL | 15:117db924cf7c | 9260 | { |
wolfSSL | 15:117db924cf7c | 9261 | switch (idx) { |
wolfSSL | 15:117db924cf7c | 9262 | case 0: |
wolfSSL | 15:117db924cf7c | 9263 | return name->country; |
wolfSSL | 15:117db924cf7c | 9264 | |
wolfSSL | 15:117db924cf7c | 9265 | case 1: |
wolfSSL | 15:117db924cf7c | 9266 | return name->state; |
wolfSSL | 15:117db924cf7c | 9267 | |
wolfSSL | 15:117db924cf7c | 9268 | case 2: |
wolfSSL | 15:117db924cf7c | 9269 | return name->locality; |
wolfSSL | 15:117db924cf7c | 9270 | |
wolfSSL | 15:117db924cf7c | 9271 | case 3: |
wolfSSL | 15:117db924cf7c | 9272 | return name->sur; |
wolfSSL | 15:117db924cf7c | 9273 | |
wolfSSL | 15:117db924cf7c | 9274 | case 4: |
wolfSSL | 15:117db924cf7c | 9275 | return name->org; |
wolfSSL | 15:117db924cf7c | 9276 | |
wolfSSL | 15:117db924cf7c | 9277 | case 5: |
wolfSSL | 15:117db924cf7c | 9278 | return name->unit; |
wolfSSL | 15:117db924cf7c | 9279 | |
wolfSSL | 15:117db924cf7c | 9280 | case 6: |
wolfSSL | 15:117db924cf7c | 9281 | return name->commonName; |
wolfSSL | 15:117db924cf7c | 9282 | |
wolfSSL | 15:117db924cf7c | 9283 | case 7: |
wolfSSL | 15:117db924cf7c | 9284 | return name->email; |
wolfSSL | 15:117db924cf7c | 9285 | |
wolfSSL | 15:117db924cf7c | 9286 | default: |
wolfSSL | 15:117db924cf7c | 9287 | return 0; |
wolfSSL | 15:117db924cf7c | 9288 | } |
wolfSSL | 15:117db924cf7c | 9289 | } |
wolfSSL | 15:117db924cf7c | 9290 | |
wolfSSL | 15:117db924cf7c | 9291 | |
wolfSSL | 15:117db924cf7c | 9292 | /* Get Which Name Encoding from index */ |
wolfSSL | 15:117db924cf7c | 9293 | static char GetNameType(CertName* name, int idx) |
wolfSSL | 15:117db924cf7c | 9294 | { |
wolfSSL | 15:117db924cf7c | 9295 | switch (idx) { |
wolfSSL | 15:117db924cf7c | 9296 | case 0: |
wolfSSL | 15:117db924cf7c | 9297 | return name->countryEnc; |
wolfSSL | 15:117db924cf7c | 9298 | |
wolfSSL | 15:117db924cf7c | 9299 | case 1: |
wolfSSL | 15:117db924cf7c | 9300 | return name->stateEnc; |
wolfSSL | 15:117db924cf7c | 9301 | |
wolfSSL | 15:117db924cf7c | 9302 | case 2: |
wolfSSL | 15:117db924cf7c | 9303 | return name->localityEnc; |
wolfSSL | 15:117db924cf7c | 9304 | |
wolfSSL | 15:117db924cf7c | 9305 | case 3: |
wolfSSL | 15:117db924cf7c | 9306 | return name->surEnc; |
wolfSSL | 15:117db924cf7c | 9307 | |
wolfSSL | 15:117db924cf7c | 9308 | case 4: |
wolfSSL | 15:117db924cf7c | 9309 | return name->orgEnc; |
wolfSSL | 15:117db924cf7c | 9310 | |
wolfSSL | 15:117db924cf7c | 9311 | case 5: |
wolfSSL | 15:117db924cf7c | 9312 | return name->unitEnc; |
wolfSSL | 15:117db924cf7c | 9313 | |
wolfSSL | 15:117db924cf7c | 9314 | case 6: |
wolfSSL | 15:117db924cf7c | 9315 | return name->commonNameEnc; |
wolfSSL | 15:117db924cf7c | 9316 | |
wolfSSL | 15:117db924cf7c | 9317 | default: |
wolfSSL | 15:117db924cf7c | 9318 | return 0; |
wolfSSL | 15:117db924cf7c | 9319 | } |
wolfSSL | 15:117db924cf7c | 9320 | } |
wolfSSL | 15:117db924cf7c | 9321 | |
wolfSSL | 15:117db924cf7c | 9322 | |
wolfSSL | 15:117db924cf7c | 9323 | /* Get ASN Name from index */ |
wolfSSL | 15:117db924cf7c | 9324 | static byte GetNameId(int idx) |
wolfSSL | 15:117db924cf7c | 9325 | { |
wolfSSL | 15:117db924cf7c | 9326 | switch (idx) { |
wolfSSL | 15:117db924cf7c | 9327 | case 0: |
wolfSSL | 15:117db924cf7c | 9328 | return ASN_COUNTRY_NAME; |
wolfSSL | 15:117db924cf7c | 9329 | |
wolfSSL | 15:117db924cf7c | 9330 | case 1: |
wolfSSL | 15:117db924cf7c | 9331 | return ASN_STATE_NAME; |
wolfSSL | 15:117db924cf7c | 9332 | |
wolfSSL | 15:117db924cf7c | 9333 | case 2: |
wolfSSL | 15:117db924cf7c | 9334 | return ASN_LOCALITY_NAME; |
wolfSSL | 15:117db924cf7c | 9335 | |
wolfSSL | 15:117db924cf7c | 9336 | case 3: |
wolfSSL | 15:117db924cf7c | 9337 | return ASN_SUR_NAME; |
wolfSSL | 15:117db924cf7c | 9338 | |
wolfSSL | 15:117db924cf7c | 9339 | case 4: |
wolfSSL | 15:117db924cf7c | 9340 | return ASN_ORG_NAME; |
wolfSSL | 15:117db924cf7c | 9341 | |
wolfSSL | 15:117db924cf7c | 9342 | case 5: |
wolfSSL | 15:117db924cf7c | 9343 | return ASN_ORGUNIT_NAME; |
wolfSSL | 15:117db924cf7c | 9344 | |
wolfSSL | 15:117db924cf7c | 9345 | case 6: |
wolfSSL | 15:117db924cf7c | 9346 | return ASN_COMMON_NAME; |
wolfSSL | 15:117db924cf7c | 9347 | |
wolfSSL | 15:117db924cf7c | 9348 | case 7: |
wolfSSL | 15:117db924cf7c | 9349 | return ASN_EMAIL_NAME; |
wolfSSL | 15:117db924cf7c | 9350 | |
wolfSSL | 15:117db924cf7c | 9351 | default: |
wolfSSL | 15:117db924cf7c | 9352 | return 0; |
wolfSSL | 15:117db924cf7c | 9353 | } |
wolfSSL | 15:117db924cf7c | 9354 | } |
wolfSSL | 15:117db924cf7c | 9355 | |
wolfSSL | 15:117db924cf7c | 9356 | /* |
wolfSSL | 15:117db924cf7c | 9357 | Extensions ::= SEQUENCE OF Extension |
wolfSSL | 15:117db924cf7c | 9358 | |
wolfSSL | 15:117db924cf7c | 9359 | Extension ::= SEQUENCE { |
wolfSSL | 15:117db924cf7c | 9360 | extnId OBJECT IDENTIFIER, |
wolfSSL | 15:117db924cf7c | 9361 | critical BOOLEAN DEFAULT FALSE, |
wolfSSL | 15:117db924cf7c | 9362 | extnValue OCTET STRING } |
wolfSSL | 15:117db924cf7c | 9363 | */ |
wolfSSL | 15:117db924cf7c | 9364 | |
wolfSSL | 15:117db924cf7c | 9365 | /* encode all extensions, return total bytes written */ |
wolfSSL | 15:117db924cf7c | 9366 | static int SetExtensions(byte* out, word32 outSz, int *IdxInOut, |
wolfSSL | 15:117db924cf7c | 9367 | const byte* ext, int extSz) |
wolfSSL | 15:117db924cf7c | 9368 | { |
wolfSSL | 15:117db924cf7c | 9369 | if (out == NULL || IdxInOut == NULL || ext == NULL) |
wolfSSL | 15:117db924cf7c | 9370 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9371 | |
wolfSSL | 15:117db924cf7c | 9372 | if (outSz < (word32)(*IdxInOut+extSz)) |
wolfSSL | 15:117db924cf7c | 9373 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9374 | |
wolfSSL | 15:117db924cf7c | 9375 | XMEMCPY(&out[*IdxInOut], ext, extSz); /* extensions */ |
wolfSSL | 15:117db924cf7c | 9376 | *IdxInOut += extSz; |
wolfSSL | 15:117db924cf7c | 9377 | |
wolfSSL | 15:117db924cf7c | 9378 | return *IdxInOut; |
wolfSSL | 15:117db924cf7c | 9379 | } |
wolfSSL | 15:117db924cf7c | 9380 | |
wolfSSL | 15:117db924cf7c | 9381 | /* encode extensions header, return total bytes written */ |
wolfSSL | 15:117db924cf7c | 9382 | static int SetExtensionsHeader(byte* out, word32 outSz, int extSz) |
wolfSSL | 15:117db924cf7c | 9383 | { |
wolfSSL | 15:117db924cf7c | 9384 | byte sequence[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 9385 | byte len[MAX_LENGTH_SZ]; |
wolfSSL | 15:117db924cf7c | 9386 | int seqSz, lenSz, idx = 0; |
wolfSSL | 15:117db924cf7c | 9387 | |
wolfSSL | 15:117db924cf7c | 9388 | if (out == NULL) |
wolfSSL | 15:117db924cf7c | 9389 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9390 | |
wolfSSL | 15:117db924cf7c | 9391 | if (outSz < 3) |
wolfSSL | 15:117db924cf7c | 9392 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9393 | |
wolfSSL | 15:117db924cf7c | 9394 | seqSz = SetSequence(extSz, sequence); |
wolfSSL | 15:117db924cf7c | 9395 | |
wolfSSL | 15:117db924cf7c | 9396 | /* encode extensions length provided */ |
wolfSSL | 15:117db924cf7c | 9397 | lenSz = SetLength(extSz+seqSz, len); |
wolfSSL | 15:117db924cf7c | 9398 | |
wolfSSL | 15:117db924cf7c | 9399 | if (outSz < (word32)(lenSz+seqSz+1)) |
wolfSSL | 15:117db924cf7c | 9400 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9401 | |
wolfSSL | 15:117db924cf7c | 9402 | out[idx++] = ASN_EXTENSIONS; /* extensions id */ |
wolfSSL | 15:117db924cf7c | 9403 | XMEMCPY(&out[idx], len, lenSz); /* length */ |
wolfSSL | 15:117db924cf7c | 9404 | idx += lenSz; |
wolfSSL | 15:117db924cf7c | 9405 | |
wolfSSL | 15:117db924cf7c | 9406 | XMEMCPY(&out[idx], sequence, seqSz); /* sequence */ |
wolfSSL | 15:117db924cf7c | 9407 | idx += seqSz; |
wolfSSL | 15:117db924cf7c | 9408 | |
wolfSSL | 15:117db924cf7c | 9409 | return idx; |
wolfSSL | 15:117db924cf7c | 9410 | } |
wolfSSL | 15:117db924cf7c | 9411 | |
wolfSSL | 15:117db924cf7c | 9412 | |
wolfSSL | 15:117db924cf7c | 9413 | /* encode CA basic constraint true, return total bytes written */ |
wolfSSL | 15:117db924cf7c | 9414 | static int SetCa(byte* out, word32 outSz) |
wolfSSL | 15:117db924cf7c | 9415 | { |
wolfSSL | 15:117db924cf7c | 9416 | static const byte ca[] = { 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, |
wolfSSL | 15:117db924cf7c | 9417 | 0x05, 0x30, 0x03, 0x01, 0x01, 0xff }; |
wolfSSL | 15:117db924cf7c | 9418 | |
wolfSSL | 15:117db924cf7c | 9419 | if (out == NULL) |
wolfSSL | 15:117db924cf7c | 9420 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9421 | |
wolfSSL | 15:117db924cf7c | 9422 | if (outSz < sizeof(ca)) |
wolfSSL | 15:117db924cf7c | 9423 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9424 | |
wolfSSL | 15:117db924cf7c | 9425 | XMEMCPY(out, ca, sizeof(ca)); |
wolfSSL | 15:117db924cf7c | 9426 | |
wolfSSL | 15:117db924cf7c | 9427 | return (int)sizeof(ca); |
wolfSSL | 15:117db924cf7c | 9428 | } |
wolfSSL | 15:117db924cf7c | 9429 | |
wolfSSL | 15:117db924cf7c | 9430 | |
wolfSSL | 15:117db924cf7c | 9431 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 9432 | /* encode OID and associated value, return total bytes written */ |
wolfSSL | 15:117db924cf7c | 9433 | static int SetOidValue(byte* out, word32 outSz, const byte *oid, word32 oidSz, |
wolfSSL | 15:117db924cf7c | 9434 | byte *in, word32 inSz) |
wolfSSL | 15:117db924cf7c | 9435 | { |
wolfSSL | 15:117db924cf7c | 9436 | int idx = 0; |
wolfSSL | 15:117db924cf7c | 9437 | |
wolfSSL | 15:117db924cf7c | 9438 | if (out == NULL || oid == NULL || in == NULL) |
wolfSSL | 15:117db924cf7c | 9439 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9440 | |
wolfSSL | 15:117db924cf7c | 9441 | if (outSz < 3) |
wolfSSL | 15:117db924cf7c | 9442 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9443 | |
wolfSSL | 15:117db924cf7c | 9444 | /* sequence, + 1 => byte to put value size */ |
wolfSSL | 15:117db924cf7c | 9445 | idx = SetSequence(inSz + oidSz + 1, out); |
wolfSSL | 15:117db924cf7c | 9446 | |
wolfSSL | 15:117db924cf7c | 9447 | if ((idx + inSz + oidSz + 1) > outSz) |
wolfSSL | 15:117db924cf7c | 9448 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9449 | |
wolfSSL | 15:117db924cf7c | 9450 | XMEMCPY(out+idx, oid, oidSz); |
wolfSSL | 15:117db924cf7c | 9451 | idx += oidSz; |
wolfSSL | 15:117db924cf7c | 9452 | out[idx++] = (byte)inSz; |
wolfSSL | 15:117db924cf7c | 9453 | XMEMCPY(out+idx, in, inSz); |
wolfSSL | 15:117db924cf7c | 9454 | |
wolfSSL | 15:117db924cf7c | 9455 | return (idx+inSz); |
wolfSSL | 15:117db924cf7c | 9456 | } |
wolfSSL | 15:117db924cf7c | 9457 | |
wolfSSL | 15:117db924cf7c | 9458 | /* encode Subject Key Identifier, return total bytes written |
wolfSSL | 15:117db924cf7c | 9459 | * RFC5280 : non-critical */ |
wolfSSL | 15:117db924cf7c | 9460 | static int SetSKID(byte* output, word32 outSz, const byte *input, word32 length) |
wolfSSL | 15:117db924cf7c | 9461 | { |
wolfSSL | 15:117db924cf7c | 9462 | byte skid_len[1 + MAX_LENGTH_SZ]; |
wolfSSL | 15:117db924cf7c | 9463 | byte skid_enc_len[MAX_LENGTH_SZ]; |
wolfSSL | 15:117db924cf7c | 9464 | int idx = 0, skid_lenSz, skid_enc_lenSz; |
wolfSSL | 15:117db924cf7c | 9465 | static const byte skid_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04 }; |
wolfSSL | 15:117db924cf7c | 9466 | |
wolfSSL | 15:117db924cf7c | 9467 | if (output == NULL || input == NULL) |
wolfSSL | 15:117db924cf7c | 9468 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9469 | |
wolfSSL | 15:117db924cf7c | 9470 | /* Octet String header */ |
wolfSSL | 15:117db924cf7c | 9471 | skid_lenSz = SetOctetString(length, skid_len); |
wolfSSL | 15:117db924cf7c | 9472 | |
wolfSSL | 15:117db924cf7c | 9473 | /* length of encoded value */ |
wolfSSL | 15:117db924cf7c | 9474 | skid_enc_lenSz = SetLength(length + skid_lenSz, skid_enc_len); |
wolfSSL | 15:117db924cf7c | 9475 | |
wolfSSL | 15:117db924cf7c | 9476 | if (outSz < 3) |
wolfSSL | 15:117db924cf7c | 9477 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9478 | |
wolfSSL | 15:117db924cf7c | 9479 | idx = SetSequence(length + sizeof(skid_oid) + skid_lenSz + skid_enc_lenSz, |
wolfSSL | 15:117db924cf7c | 9480 | output); |
wolfSSL | 15:117db924cf7c | 9481 | |
wolfSSL | 15:117db924cf7c | 9482 | if ((length + sizeof(skid_oid) + skid_lenSz + skid_enc_lenSz) > outSz) |
wolfSSL | 15:117db924cf7c | 9483 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9484 | |
wolfSSL | 15:117db924cf7c | 9485 | /* put oid */ |
wolfSSL | 15:117db924cf7c | 9486 | XMEMCPY(output+idx, skid_oid, sizeof(skid_oid)); |
wolfSSL | 15:117db924cf7c | 9487 | idx += sizeof(skid_oid); |
wolfSSL | 15:117db924cf7c | 9488 | |
wolfSSL | 15:117db924cf7c | 9489 | /* put encoded len */ |
wolfSSL | 15:117db924cf7c | 9490 | XMEMCPY(output+idx, skid_enc_len, skid_enc_lenSz); |
wolfSSL | 15:117db924cf7c | 9491 | idx += skid_enc_lenSz; |
wolfSSL | 15:117db924cf7c | 9492 | |
wolfSSL | 15:117db924cf7c | 9493 | /* put octet header */ |
wolfSSL | 15:117db924cf7c | 9494 | XMEMCPY(output+idx, skid_len, skid_lenSz); |
wolfSSL | 15:117db924cf7c | 9495 | idx += skid_lenSz; |
wolfSSL | 15:117db924cf7c | 9496 | |
wolfSSL | 15:117db924cf7c | 9497 | /* put value */ |
wolfSSL | 15:117db924cf7c | 9498 | XMEMCPY(output+idx, input, length); |
wolfSSL | 15:117db924cf7c | 9499 | idx += length; |
wolfSSL | 15:117db924cf7c | 9500 | |
wolfSSL | 15:117db924cf7c | 9501 | return idx; |
wolfSSL | 15:117db924cf7c | 9502 | } |
wolfSSL | 15:117db924cf7c | 9503 | |
wolfSSL | 15:117db924cf7c | 9504 | /* encode Authority Key Identifier, return total bytes written |
wolfSSL | 15:117db924cf7c | 9505 | * RFC5280 : non-critical */ |
wolfSSL | 15:117db924cf7c | 9506 | static int SetAKID(byte* output, word32 outSz, |
wolfSSL | 15:117db924cf7c | 9507 | byte *input, word32 length, void* heap) |
wolfSSL | 15:117db924cf7c | 9508 | { |
wolfSSL | 15:117db924cf7c | 9509 | byte *enc_val; |
wolfSSL | 15:117db924cf7c | 9510 | int ret, enc_valSz; |
wolfSSL | 15:117db924cf7c | 9511 | static const byte akid_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04 }; |
wolfSSL | 15:117db924cf7c | 9512 | static const byte akid_cs[] = { 0x80 }; |
wolfSSL | 15:117db924cf7c | 9513 | |
wolfSSL | 15:117db924cf7c | 9514 | if (output == NULL || input == NULL) |
wolfSSL | 15:117db924cf7c | 9515 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9516 | |
wolfSSL | 15:117db924cf7c | 9517 | enc_valSz = length + 3 + sizeof(akid_cs); |
wolfSSL | 15:117db924cf7c | 9518 | enc_val = (byte *)XMALLOC(enc_valSz, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9519 | if (enc_val == NULL) |
wolfSSL | 15:117db924cf7c | 9520 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 9521 | |
wolfSSL | 15:117db924cf7c | 9522 | /* sequence for ContentSpec & value */ |
wolfSSL | 15:117db924cf7c | 9523 | ret = SetOidValue(enc_val, enc_valSz, akid_cs, sizeof(akid_cs), |
wolfSSL | 15:117db924cf7c | 9524 | input, length); |
wolfSSL | 15:117db924cf7c | 9525 | if (ret > 0) { |
wolfSSL | 15:117db924cf7c | 9526 | enc_valSz = ret; |
wolfSSL | 15:117db924cf7c | 9527 | |
wolfSSL | 15:117db924cf7c | 9528 | ret = SetOidValue(output, outSz, akid_oid, sizeof(akid_oid), |
wolfSSL | 15:117db924cf7c | 9529 | enc_val, enc_valSz); |
wolfSSL | 15:117db924cf7c | 9530 | } |
wolfSSL | 15:117db924cf7c | 9531 | |
wolfSSL | 15:117db924cf7c | 9532 | XFREE(enc_val, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9533 | return ret; |
wolfSSL | 15:117db924cf7c | 9534 | } |
wolfSSL | 15:117db924cf7c | 9535 | |
wolfSSL | 15:117db924cf7c | 9536 | /* encode Key Usage, return total bytes written |
wolfSSL | 15:117db924cf7c | 9537 | * RFC5280 : critical */ |
wolfSSL | 15:117db924cf7c | 9538 | static int SetKeyUsage(byte* output, word32 outSz, word16 input) |
wolfSSL | 15:117db924cf7c | 9539 | { |
wolfSSL | 15:117db924cf7c | 9540 | byte ku[5]; |
wolfSSL | 15:117db924cf7c | 9541 | int idx; |
wolfSSL | 15:117db924cf7c | 9542 | static const byte keyusage_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x0f, |
wolfSSL | 15:117db924cf7c | 9543 | 0x01, 0x01, 0xff, 0x04}; |
wolfSSL | 15:117db924cf7c | 9544 | if (output == NULL) |
wolfSSL | 15:117db924cf7c | 9545 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9546 | |
wolfSSL | 15:117db924cf7c | 9547 | idx = SetBitString16Bit(input, ku); |
wolfSSL | 15:117db924cf7c | 9548 | return SetOidValue(output, outSz, keyusage_oid, sizeof(keyusage_oid), |
wolfSSL | 15:117db924cf7c | 9549 | ku, idx); |
wolfSSL | 15:117db924cf7c | 9550 | } |
wolfSSL | 15:117db924cf7c | 9551 | |
wolfSSL | 15:117db924cf7c | 9552 | static int SetOjectIdValue(byte* output, word32 outSz, int* idx, |
wolfSSL | 15:117db924cf7c | 9553 | const byte* oid, word32 oidSz) |
wolfSSL | 15:117db924cf7c | 9554 | { |
wolfSSL | 15:117db924cf7c | 9555 | /* verify room */ |
wolfSSL | 15:117db924cf7c | 9556 | if (*idx + 2 + oidSz >= outSz) |
wolfSSL | 15:117db924cf7c | 9557 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 9558 | |
wolfSSL | 15:117db924cf7c | 9559 | *idx += SetObjectId(oidSz, &output[*idx]); |
wolfSSL | 15:117db924cf7c | 9560 | XMEMCPY(&output[*idx], oid, oidSz); |
wolfSSL | 15:117db924cf7c | 9561 | *idx += oidSz; |
wolfSSL | 15:117db924cf7c | 9562 | |
wolfSSL | 15:117db924cf7c | 9563 | return 0; |
wolfSSL | 15:117db924cf7c | 9564 | } |
wolfSSL | 15:117db924cf7c | 9565 | |
wolfSSL | 15:117db924cf7c | 9566 | /* encode Extended Key Usage (RFC 5280 4.2.1.12), return total bytes written */ |
wolfSSL | 15:117db924cf7c | 9567 | static int SetExtKeyUsage(Cert* cert, byte* output, word32 outSz, byte input) |
wolfSSL | 15:117db924cf7c | 9568 | { |
wolfSSL | 15:117db924cf7c | 9569 | int idx = 0, oidListSz = 0, totalSz, ret = 0; |
wolfSSL | 15:117db924cf7c | 9570 | static const byte extkeyusage_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x25 }; |
wolfSSL | 15:117db924cf7c | 9571 | |
wolfSSL | 15:117db924cf7c | 9572 | if (output == NULL) |
wolfSSL | 15:117db924cf7c | 9573 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9574 | |
wolfSSL | 15:117db924cf7c | 9575 | /* Skip to OID List */ |
wolfSSL | 15:117db924cf7c | 9576 | totalSz = 2 + sizeof(extkeyusage_oid) + 4; |
wolfSSL | 15:117db924cf7c | 9577 | idx = totalSz; |
wolfSSL | 15:117db924cf7c | 9578 | |
wolfSSL | 15:117db924cf7c | 9579 | /* Build OID List */ |
wolfSSL | 15:117db924cf7c | 9580 | /* If any set, then just use it */ |
wolfSSL | 15:117db924cf7c | 9581 | if (input & EXTKEYUSE_ANY) { |
wolfSSL | 15:117db924cf7c | 9582 | ret |= SetOjectIdValue(output, outSz, &idx, |
wolfSSL | 15:117db924cf7c | 9583 | extExtKeyUsageAnyOid, sizeof(extExtKeyUsageAnyOid)); |
wolfSSL | 15:117db924cf7c | 9584 | } |
wolfSSL | 15:117db924cf7c | 9585 | else { |
wolfSSL | 15:117db924cf7c | 9586 | if (input & EXTKEYUSE_SERVER_AUTH) |
wolfSSL | 15:117db924cf7c | 9587 | ret |= SetOjectIdValue(output, outSz, &idx, |
wolfSSL | 15:117db924cf7c | 9588 | extExtKeyUsageServerAuthOid, sizeof(extExtKeyUsageServerAuthOid)); |
wolfSSL | 15:117db924cf7c | 9589 | if (input & EXTKEYUSE_CLIENT_AUTH) |
wolfSSL | 15:117db924cf7c | 9590 | ret |= SetOjectIdValue(output, outSz, &idx, |
wolfSSL | 15:117db924cf7c | 9591 | extExtKeyUsageClientAuthOid, sizeof(extExtKeyUsageClientAuthOid)); |
wolfSSL | 15:117db924cf7c | 9592 | if (input & EXTKEYUSE_CODESIGN) |
wolfSSL | 15:117db924cf7c | 9593 | ret |= SetOjectIdValue(output, outSz, &idx, |
wolfSSL | 15:117db924cf7c | 9594 | extExtKeyUsageCodeSigningOid, sizeof(extExtKeyUsageCodeSigningOid)); |
wolfSSL | 15:117db924cf7c | 9595 | if (input & EXTKEYUSE_EMAILPROT) |
wolfSSL | 15:117db924cf7c | 9596 | ret |= SetOjectIdValue(output, outSz, &idx, |
wolfSSL | 15:117db924cf7c | 9597 | extExtKeyUsageEmailProtectOid, sizeof(extExtKeyUsageEmailProtectOid)); |
wolfSSL | 15:117db924cf7c | 9598 | if (input & EXTKEYUSE_TIMESTAMP) |
wolfSSL | 15:117db924cf7c | 9599 | ret |= SetOjectIdValue(output, outSz, &idx, |
wolfSSL | 15:117db924cf7c | 9600 | extExtKeyUsageTimestampOid, sizeof(extExtKeyUsageTimestampOid)); |
wolfSSL | 15:117db924cf7c | 9601 | if (input & EXTKEYUSE_OCSP_SIGN) |
wolfSSL | 15:117db924cf7c | 9602 | ret |= SetOjectIdValue(output, outSz, &idx, |
wolfSSL | 15:117db924cf7c | 9603 | extExtKeyUsageOcspSignOid, sizeof(extExtKeyUsageOcspSignOid)); |
wolfSSL | 15:117db924cf7c | 9604 | #ifdef WOLFSSL_EKU_OID |
wolfSSL | 15:117db924cf7c | 9605 | /* iterate through OID values */ |
wolfSSL | 15:117db924cf7c | 9606 | if (input & EXTKEYUSE_USER) { |
wolfSSL | 15:117db924cf7c | 9607 | int i, sz; |
wolfSSL | 15:117db924cf7c | 9608 | for (i = 0; i < CTC_MAX_EKU_NB; i++) { |
wolfSSL | 15:117db924cf7c | 9609 | sz = cert->extKeyUsageOIDSz[i]; |
wolfSSL | 15:117db924cf7c | 9610 | if (sz > 0) { |
wolfSSL | 15:117db924cf7c | 9611 | ret |= SetOjectIdValue(output, outSz, &idx, |
wolfSSL | 15:117db924cf7c | 9612 | cert->extKeyUsageOID[i], sz); |
wolfSSL | 15:117db924cf7c | 9613 | } |
wolfSSL | 15:117db924cf7c | 9614 | } |
wolfSSL | 15:117db924cf7c | 9615 | } |
wolfSSL | 15:117db924cf7c | 9616 | #endif /* WOLFSSL_EKU_OID */ |
wolfSSL | 15:117db924cf7c | 9617 | } |
wolfSSL | 15:117db924cf7c | 9618 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 9619 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 9620 | |
wolfSSL | 15:117db924cf7c | 9621 | /* Calculate Sizes */ |
wolfSSL | 15:117db924cf7c | 9622 | oidListSz = idx - totalSz; |
wolfSSL | 15:117db924cf7c | 9623 | totalSz = idx - 2; /* exclude first seq/len (2) */ |
wolfSSL | 15:117db924cf7c | 9624 | |
wolfSSL | 15:117db924cf7c | 9625 | /* 1. Seq + Total Len (2) */ |
wolfSSL | 15:117db924cf7c | 9626 | idx = SetSequence(totalSz, output); |
wolfSSL | 15:117db924cf7c | 9627 | |
wolfSSL | 15:117db924cf7c | 9628 | /* 2. Object ID (2) */ |
wolfSSL | 15:117db924cf7c | 9629 | XMEMCPY(&output[idx], extkeyusage_oid, sizeof(extkeyusage_oid)); |
wolfSSL | 15:117db924cf7c | 9630 | idx += sizeof(extkeyusage_oid); |
wolfSSL | 15:117db924cf7c | 9631 | |
wolfSSL | 15:117db924cf7c | 9632 | /* 3. Octect String (2) */ |
wolfSSL | 15:117db924cf7c | 9633 | idx += SetOctetString(totalSz - idx, &output[idx]); |
wolfSSL | 15:117db924cf7c | 9634 | |
wolfSSL | 15:117db924cf7c | 9635 | /* 4. Seq + OidListLen (2) */ |
wolfSSL | 15:117db924cf7c | 9636 | idx += SetSequence(oidListSz, &output[idx]); |
wolfSSL | 15:117db924cf7c | 9637 | |
wolfSSL | 15:117db924cf7c | 9638 | /* 5. Oid List (already set in-place above) */ |
wolfSSL | 15:117db924cf7c | 9639 | idx += oidListSz; |
wolfSSL | 15:117db924cf7c | 9640 | |
wolfSSL | 15:117db924cf7c | 9641 | (void)cert; |
wolfSSL | 15:117db924cf7c | 9642 | return idx; |
wolfSSL | 15:117db924cf7c | 9643 | } |
wolfSSL | 15:117db924cf7c | 9644 | |
wolfSSL | 15:117db924cf7c | 9645 | /* Encode OID string representation to ITU-T X.690 format */ |
wolfSSL | 15:117db924cf7c | 9646 | static int EncodePolicyOID(byte *out, word32 *outSz, const char *in, void* heap) |
wolfSSL | 15:117db924cf7c | 9647 | { |
wolfSSL | 15:117db924cf7c | 9648 | word32 val, idx = 0, nb_val; |
wolfSSL | 15:117db924cf7c | 9649 | char *token, *str, *ptr; |
wolfSSL | 15:117db924cf7c | 9650 | word32 len; |
wolfSSL | 15:117db924cf7c | 9651 | |
wolfSSL | 15:117db924cf7c | 9652 | if (out == NULL || outSz == NULL || *outSz < 2 || in == NULL) |
wolfSSL | 15:117db924cf7c | 9653 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9654 | |
wolfSSL | 15:117db924cf7c | 9655 | len = (word32)XSTRLEN(in); |
wolfSSL | 15:117db924cf7c | 9656 | |
wolfSSL | 15:117db924cf7c | 9657 | str = (char *)XMALLOC(len+1, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9658 | if (str == NULL) |
wolfSSL | 15:117db924cf7c | 9659 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 9660 | |
wolfSSL | 15:117db924cf7c | 9661 | XSTRNCPY(str, in, len); |
wolfSSL | 15:117db924cf7c | 9662 | str[len] = '\0'; |
wolfSSL | 15:117db924cf7c | 9663 | |
wolfSSL | 15:117db924cf7c | 9664 | nb_val = 0; |
wolfSSL | 15:117db924cf7c | 9665 | |
wolfSSL | 15:117db924cf7c | 9666 | /* parse value, and set corresponding Policy OID value */ |
wolfSSL | 15:117db924cf7c | 9667 | token = XSTRTOK(str, ".", &ptr); |
wolfSSL | 15:117db924cf7c | 9668 | while (token != NULL) |
wolfSSL | 15:117db924cf7c | 9669 | { |
wolfSSL | 15:117db924cf7c | 9670 | val = (word32)atoi(token); |
wolfSSL | 15:117db924cf7c | 9671 | |
wolfSSL | 15:117db924cf7c | 9672 | if (nb_val == 0) { |
wolfSSL | 15:117db924cf7c | 9673 | if (val > 2) { |
wolfSSL | 15:117db924cf7c | 9674 | XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9675 | return ASN_OBJECT_ID_E; |
wolfSSL | 15:117db924cf7c | 9676 | } |
wolfSSL | 15:117db924cf7c | 9677 | |
wolfSSL | 15:117db924cf7c | 9678 | out[idx] = (byte)(40 * val); |
wolfSSL | 15:117db924cf7c | 9679 | } |
wolfSSL | 15:117db924cf7c | 9680 | else if (nb_val == 1) { |
wolfSSL | 15:117db924cf7c | 9681 | if (val > 127) { |
wolfSSL | 15:117db924cf7c | 9682 | XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9683 | return ASN_OBJECT_ID_E; |
wolfSSL | 15:117db924cf7c | 9684 | } |
wolfSSL | 15:117db924cf7c | 9685 | |
wolfSSL | 15:117db924cf7c | 9686 | if (idx > *outSz) { |
wolfSSL | 15:117db924cf7c | 9687 | XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9688 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9689 | } |
wolfSSL | 15:117db924cf7c | 9690 | |
wolfSSL | 15:117db924cf7c | 9691 | out[idx++] += (byte)val; |
wolfSSL | 15:117db924cf7c | 9692 | } |
wolfSSL | 15:117db924cf7c | 9693 | else { |
wolfSSL | 15:117db924cf7c | 9694 | word32 tb = 0, x; |
wolfSSL | 15:117db924cf7c | 9695 | int i = 0; |
wolfSSL | 15:117db924cf7c | 9696 | byte oid[MAX_OID_SZ]; |
wolfSSL | 15:117db924cf7c | 9697 | |
wolfSSL | 15:117db924cf7c | 9698 | while (val >= 128) { |
wolfSSL | 15:117db924cf7c | 9699 | x = val % 128; |
wolfSSL | 15:117db924cf7c | 9700 | val /= 128; |
wolfSSL | 15:117db924cf7c | 9701 | oid[i++] = (byte) (((tb++) ? 0x80 : 0) | x); |
wolfSSL | 15:117db924cf7c | 9702 | } |
wolfSSL | 15:117db924cf7c | 9703 | |
wolfSSL | 15:117db924cf7c | 9704 | if ((idx+(word32)i) > *outSz) { |
wolfSSL | 15:117db924cf7c | 9705 | XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9706 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9707 | } |
wolfSSL | 15:117db924cf7c | 9708 | |
wolfSSL | 15:117db924cf7c | 9709 | oid[i] = (byte) (((tb++) ? 0x80 : 0) | val); |
wolfSSL | 15:117db924cf7c | 9710 | |
wolfSSL | 15:117db924cf7c | 9711 | /* push value in the right order */ |
wolfSSL | 15:117db924cf7c | 9712 | while (i >= 0) |
wolfSSL | 15:117db924cf7c | 9713 | out[idx++] = oid[i--]; |
wolfSSL | 15:117db924cf7c | 9714 | } |
wolfSSL | 15:117db924cf7c | 9715 | |
wolfSSL | 15:117db924cf7c | 9716 | token = XSTRTOK(NULL, ".", &ptr); |
wolfSSL | 15:117db924cf7c | 9717 | nb_val++; |
wolfSSL | 15:117db924cf7c | 9718 | } |
wolfSSL | 15:117db924cf7c | 9719 | |
wolfSSL | 15:117db924cf7c | 9720 | *outSz = idx; |
wolfSSL | 15:117db924cf7c | 9721 | |
wolfSSL | 15:117db924cf7c | 9722 | XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9723 | return 0; |
wolfSSL | 15:117db924cf7c | 9724 | } |
wolfSSL | 15:117db924cf7c | 9725 | |
wolfSSL | 15:117db924cf7c | 9726 | /* encode Certificate Policies, return total bytes written |
wolfSSL | 15:117db924cf7c | 9727 | * each input value must be ITU-T X.690 formatted : a.b.c... |
wolfSSL | 15:117db924cf7c | 9728 | * input must be an array of values with a NULL terminated for the latest |
wolfSSL | 15:117db924cf7c | 9729 | * RFC5280 : non-critical */ |
wolfSSL | 15:117db924cf7c | 9730 | static int SetCertificatePolicies(byte *output, |
wolfSSL | 15:117db924cf7c | 9731 | word32 outputSz, |
wolfSSL | 15:117db924cf7c | 9732 | char input[MAX_CERTPOL_NB][MAX_CERTPOL_SZ], |
wolfSSL | 15:117db924cf7c | 9733 | word16 nb_certpol, |
wolfSSL | 15:117db924cf7c | 9734 | void* heap) |
wolfSSL | 15:117db924cf7c | 9735 | { |
wolfSSL | 15:117db924cf7c | 9736 | byte oid[MAX_OID_SZ], |
wolfSSL | 15:117db924cf7c | 9737 | der_oid[MAX_CERTPOL_NB][MAX_OID_SZ], |
wolfSSL | 15:117db924cf7c | 9738 | out[MAX_CERTPOL_SZ]; |
wolfSSL | 15:117db924cf7c | 9739 | word32 oidSz; |
wolfSSL | 15:117db924cf7c | 9740 | word32 outSz, i = 0, der_oidSz[MAX_CERTPOL_NB]; |
wolfSSL | 15:117db924cf7c | 9741 | int ret; |
wolfSSL | 15:117db924cf7c | 9742 | |
wolfSSL | 15:117db924cf7c | 9743 | static const byte certpol_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x20, 0x04 }; |
wolfSSL | 15:117db924cf7c | 9744 | static const byte oid_oid[] = { 0x06 }; |
wolfSSL | 15:117db924cf7c | 9745 | |
wolfSSL | 15:117db924cf7c | 9746 | if (output == NULL || input == NULL || nb_certpol > MAX_CERTPOL_NB) |
wolfSSL | 15:117db924cf7c | 9747 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9748 | |
wolfSSL | 15:117db924cf7c | 9749 | for (i = 0; i < nb_certpol; i++) { |
wolfSSL | 15:117db924cf7c | 9750 | oidSz = sizeof(oid); |
wolfSSL | 15:117db924cf7c | 9751 | XMEMSET(oid, 0, oidSz); |
wolfSSL | 15:117db924cf7c | 9752 | |
wolfSSL | 15:117db924cf7c | 9753 | ret = EncodePolicyOID(oid, &oidSz, input[i], heap); |
wolfSSL | 15:117db924cf7c | 9754 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 9755 | return ret; |
wolfSSL | 15:117db924cf7c | 9756 | |
wolfSSL | 15:117db924cf7c | 9757 | /* compute sequence value for the oid */ |
wolfSSL | 15:117db924cf7c | 9758 | ret = SetOidValue(der_oid[i], MAX_OID_SZ, oid_oid, |
wolfSSL | 15:117db924cf7c | 9759 | sizeof(oid_oid), oid, oidSz); |
wolfSSL | 15:117db924cf7c | 9760 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 9761 | return ret; |
wolfSSL | 15:117db924cf7c | 9762 | else |
wolfSSL | 15:117db924cf7c | 9763 | der_oidSz[i] = (word32)ret; |
wolfSSL | 15:117db924cf7c | 9764 | } |
wolfSSL | 15:117db924cf7c | 9765 | |
wolfSSL | 15:117db924cf7c | 9766 | /* concatenate oid, keep two byte for sequence/size of the created value */ |
wolfSSL | 15:117db924cf7c | 9767 | for (i = 0, outSz = 2; i < nb_certpol; i++) { |
wolfSSL | 15:117db924cf7c | 9768 | XMEMCPY(out+outSz, der_oid[i], der_oidSz[i]); |
wolfSSL | 15:117db924cf7c | 9769 | outSz += der_oidSz[i]; |
wolfSSL | 15:117db924cf7c | 9770 | } |
wolfSSL | 15:117db924cf7c | 9771 | |
wolfSSL | 15:117db924cf7c | 9772 | /* add sequence */ |
wolfSSL | 15:117db924cf7c | 9773 | ret = SetSequence(outSz-2, out); |
wolfSSL | 15:117db924cf7c | 9774 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 9775 | return ret; |
wolfSSL | 15:117db924cf7c | 9776 | |
wolfSSL | 15:117db924cf7c | 9777 | /* add Policy OID to compute final value */ |
wolfSSL | 15:117db924cf7c | 9778 | return SetOidValue(output, outputSz, certpol_oid, sizeof(certpol_oid), |
wolfSSL | 15:117db924cf7c | 9779 | out, outSz); |
wolfSSL | 15:117db924cf7c | 9780 | } |
wolfSSL | 15:117db924cf7c | 9781 | #endif /* WOLFSSL_CERT_EXT */ |
wolfSSL | 15:117db924cf7c | 9782 | |
wolfSSL | 15:117db924cf7c | 9783 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 9784 | /* encode Alternative Names, return total bytes written */ |
wolfSSL | 15:117db924cf7c | 9785 | static int SetAltNames(byte *out, word32 outSz, byte *input, word32 length) |
wolfSSL | 15:117db924cf7c | 9786 | { |
wolfSSL | 15:117db924cf7c | 9787 | if (out == NULL || input == NULL) |
wolfSSL | 15:117db924cf7c | 9788 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9789 | |
wolfSSL | 15:117db924cf7c | 9790 | if (outSz < length) |
wolfSSL | 15:117db924cf7c | 9791 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9792 | |
wolfSSL | 15:117db924cf7c | 9793 | /* Alternative Names come from certificate or computed by |
wolfSSL | 15:117db924cf7c | 9794 | * external function, so already encoded. Just copy value */ |
wolfSSL | 15:117db924cf7c | 9795 | XMEMCPY(out, input, length); |
wolfSSL | 15:117db924cf7c | 9796 | return length; |
wolfSSL | 15:117db924cf7c | 9797 | } |
wolfSSL | 15:117db924cf7c | 9798 | #endif /* WOLFSL_ALT_NAMES */ |
wolfSSL | 15:117db924cf7c | 9799 | |
wolfSSL | 15:117db924cf7c | 9800 | /* Encodes one attribute of the name (issuer/subject) |
wolfSSL | 15:117db924cf7c | 9801 | * |
wolfSSL | 15:117db924cf7c | 9802 | * name structure to hold result of encoding |
wolfSSL | 15:117db924cf7c | 9803 | * nameStr value to be encoded |
wolfSSL | 15:117db924cf7c | 9804 | * nameType type of encoding i.e CTC_UTF8 |
wolfSSL | 15:117db924cf7c | 9805 | * type id of attribute i.e ASN_COMMON_NAME |
wolfSSL | 15:117db924cf7c | 9806 | * |
wolfSSL | 15:117db924cf7c | 9807 | * returns length on success |
wolfSSL | 15:117db924cf7c | 9808 | */ |
wolfSSL | 15:117db924cf7c | 9809 | static int wc_EncodeName(EncodedName* name, const char* nameStr, char nameType, |
wolfSSL | 15:117db924cf7c | 9810 | byte type) |
wolfSSL | 15:117db924cf7c | 9811 | { |
wolfSSL | 15:117db924cf7c | 9812 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 9813 | |
wolfSSL | 15:117db924cf7c | 9814 | if (nameStr) { |
wolfSSL | 15:117db924cf7c | 9815 | /* bottom up */ |
wolfSSL | 15:117db924cf7c | 9816 | byte firstLen[1 + MAX_LENGTH_SZ]; |
wolfSSL | 15:117db924cf7c | 9817 | byte secondLen[MAX_LENGTH_SZ]; |
wolfSSL | 15:117db924cf7c | 9818 | byte sequence[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 9819 | byte set[MAX_SET_SZ]; |
wolfSSL | 15:117db924cf7c | 9820 | |
wolfSSL | 15:117db924cf7c | 9821 | int strLen = (int)XSTRLEN(nameStr); |
wolfSSL | 15:117db924cf7c | 9822 | int thisLen = strLen; |
wolfSSL | 15:117db924cf7c | 9823 | int firstSz, secondSz, seqSz, setSz; |
wolfSSL | 15:117db924cf7c | 9824 | |
wolfSSL | 15:117db924cf7c | 9825 | if (strLen == 0) { /* no user data for this item */ |
wolfSSL | 15:117db924cf7c | 9826 | name->used = 0; |
wolfSSL | 15:117db924cf7c | 9827 | return 0; |
wolfSSL | 15:117db924cf7c | 9828 | } |
wolfSSL | 15:117db924cf7c | 9829 | |
wolfSSL | 15:117db924cf7c | 9830 | /* Restrict country code size */ |
wolfSSL | 15:117db924cf7c | 9831 | if (ASN_COUNTRY_NAME == type && strLen != CTC_COUNTRY_SIZE) { |
wolfSSL | 15:117db924cf7c | 9832 | return ASN_COUNTRY_SIZE_E; |
wolfSSL | 15:117db924cf7c | 9833 | } |
wolfSSL | 15:117db924cf7c | 9834 | |
wolfSSL | 15:117db924cf7c | 9835 | secondSz = SetLength(strLen, secondLen); |
wolfSSL | 15:117db924cf7c | 9836 | thisLen += secondSz; |
wolfSSL | 15:117db924cf7c | 9837 | switch (type) { |
wolfSSL | 15:117db924cf7c | 9838 | case ASN_EMAIL_NAME: /* email */ |
wolfSSL | 15:117db924cf7c | 9839 | thisLen += EMAIL_JOINT_LEN; |
wolfSSL | 15:117db924cf7c | 9840 | firstSz = EMAIL_JOINT_LEN; |
wolfSSL | 15:117db924cf7c | 9841 | break; |
wolfSSL | 15:117db924cf7c | 9842 | |
wolfSSL | 15:117db924cf7c | 9843 | case ASN_DOMAIN_COMPONENT: |
wolfSSL | 15:117db924cf7c | 9844 | thisLen += PILOT_JOINT_LEN; |
wolfSSL | 15:117db924cf7c | 9845 | firstSz = PILOT_JOINT_LEN; |
wolfSSL | 15:117db924cf7c | 9846 | break; |
wolfSSL | 15:117db924cf7c | 9847 | |
wolfSSL | 15:117db924cf7c | 9848 | default: |
wolfSSL | 15:117db924cf7c | 9849 | thisLen++; /* str type */ |
wolfSSL | 15:117db924cf7c | 9850 | thisLen += JOINT_LEN; |
wolfSSL | 15:117db924cf7c | 9851 | firstSz = JOINT_LEN + 1; |
wolfSSL | 15:117db924cf7c | 9852 | } |
wolfSSL | 15:117db924cf7c | 9853 | thisLen++; /* id type */ |
wolfSSL | 15:117db924cf7c | 9854 | firstSz = SetObjectId(firstSz, firstLen); |
wolfSSL | 15:117db924cf7c | 9855 | thisLen += firstSz; |
wolfSSL | 15:117db924cf7c | 9856 | |
wolfSSL | 15:117db924cf7c | 9857 | seqSz = SetSequence(thisLen, sequence); |
wolfSSL | 15:117db924cf7c | 9858 | thisLen += seqSz; |
wolfSSL | 15:117db924cf7c | 9859 | setSz = SetSet(thisLen, set); |
wolfSSL | 15:117db924cf7c | 9860 | thisLen += setSz; |
wolfSSL | 15:117db924cf7c | 9861 | |
wolfSSL | 15:117db924cf7c | 9862 | if (thisLen > (int)sizeof(name->encoded)) { |
wolfSSL | 15:117db924cf7c | 9863 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9864 | } |
wolfSSL | 15:117db924cf7c | 9865 | |
wolfSSL | 15:117db924cf7c | 9866 | /* store it */ |
wolfSSL | 15:117db924cf7c | 9867 | idx = 0; |
wolfSSL | 15:117db924cf7c | 9868 | /* set */ |
wolfSSL | 15:117db924cf7c | 9869 | XMEMCPY(name->encoded, set, setSz); |
wolfSSL | 15:117db924cf7c | 9870 | idx += setSz; |
wolfSSL | 15:117db924cf7c | 9871 | /* seq */ |
wolfSSL | 15:117db924cf7c | 9872 | XMEMCPY(name->encoded + idx, sequence, seqSz); |
wolfSSL | 15:117db924cf7c | 9873 | idx += seqSz; |
wolfSSL | 15:117db924cf7c | 9874 | /* asn object id */ |
wolfSSL | 15:117db924cf7c | 9875 | XMEMCPY(name->encoded + idx, firstLen, firstSz); |
wolfSSL | 15:117db924cf7c | 9876 | idx += firstSz; |
wolfSSL | 15:117db924cf7c | 9877 | switch (type) { |
wolfSSL | 15:117db924cf7c | 9878 | case ASN_EMAIL_NAME: |
wolfSSL | 15:117db924cf7c | 9879 | { |
wolfSSL | 15:117db924cf7c | 9880 | const byte EMAIL_OID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
wolfSSL | 15:117db924cf7c | 9881 | 0x01, 0x09, 0x01, 0x16 }; |
wolfSSL | 15:117db924cf7c | 9882 | /* email joint id */ |
wolfSSL | 15:117db924cf7c | 9883 | XMEMCPY(name->encoded + idx, EMAIL_OID, sizeof(EMAIL_OID)); |
wolfSSL | 15:117db924cf7c | 9884 | idx += (int)sizeof(EMAIL_OID); |
wolfSSL | 15:117db924cf7c | 9885 | } |
wolfSSL | 15:117db924cf7c | 9886 | break; |
wolfSSL | 15:117db924cf7c | 9887 | |
wolfSSL | 15:117db924cf7c | 9888 | case ASN_DOMAIN_COMPONENT: |
wolfSSL | 15:117db924cf7c | 9889 | { |
wolfSSL | 15:117db924cf7c | 9890 | const byte PILOT_OID[] = { 0x09, 0x92, 0x26, 0x89, |
wolfSSL | 15:117db924cf7c | 9891 | 0x93, 0xF2, 0x2C, 0x64, 0x01 |
wolfSSL | 15:117db924cf7c | 9892 | }; |
wolfSSL | 15:117db924cf7c | 9893 | |
wolfSSL | 15:117db924cf7c | 9894 | XMEMCPY(name->encoded + idx, PILOT_OID, |
wolfSSL | 15:117db924cf7c | 9895 | sizeof(PILOT_OID)); |
wolfSSL | 15:117db924cf7c | 9896 | idx += (int)sizeof(PILOT_OID); |
wolfSSL | 15:117db924cf7c | 9897 | /* id type */ |
wolfSSL | 15:117db924cf7c | 9898 | name->encoded[idx++] = type; |
wolfSSL | 15:117db924cf7c | 9899 | /* str type */ |
wolfSSL | 15:117db924cf7c | 9900 | name->encoded[idx++] = nameType; |
wolfSSL | 15:117db924cf7c | 9901 | } |
wolfSSL | 15:117db924cf7c | 9902 | break; |
wolfSSL | 15:117db924cf7c | 9903 | |
wolfSSL | 15:117db924cf7c | 9904 | default: |
wolfSSL | 15:117db924cf7c | 9905 | name->encoded[idx++] = 0x55; |
wolfSSL | 15:117db924cf7c | 9906 | name->encoded[idx++] = 0x04; |
wolfSSL | 15:117db924cf7c | 9907 | /* id type */ |
wolfSSL | 15:117db924cf7c | 9908 | name->encoded[idx++] = type; |
wolfSSL | 15:117db924cf7c | 9909 | /* str type */ |
wolfSSL | 15:117db924cf7c | 9910 | name->encoded[idx++] = nameType; |
wolfSSL | 15:117db924cf7c | 9911 | } |
wolfSSL | 15:117db924cf7c | 9912 | /* second length */ |
wolfSSL | 15:117db924cf7c | 9913 | XMEMCPY(name->encoded + idx, secondLen, secondSz); |
wolfSSL | 15:117db924cf7c | 9914 | idx += secondSz; |
wolfSSL | 15:117db924cf7c | 9915 | /* str value */ |
wolfSSL | 15:117db924cf7c | 9916 | XMEMCPY(name->encoded + idx, nameStr, strLen); |
wolfSSL | 15:117db924cf7c | 9917 | idx += strLen; |
wolfSSL | 15:117db924cf7c | 9918 | |
wolfSSL | 15:117db924cf7c | 9919 | name->type = type; |
wolfSSL | 15:117db924cf7c | 9920 | name->totalLen = idx; |
wolfSSL | 15:117db924cf7c | 9921 | name->used = 1; |
wolfSSL | 15:117db924cf7c | 9922 | } |
wolfSSL | 15:117db924cf7c | 9923 | else |
wolfSSL | 15:117db924cf7c | 9924 | name->used = 0; |
wolfSSL | 15:117db924cf7c | 9925 | |
wolfSSL | 15:117db924cf7c | 9926 | return idx; |
wolfSSL | 15:117db924cf7c | 9927 | } |
wolfSSL | 15:117db924cf7c | 9928 | |
wolfSSL | 15:117db924cf7c | 9929 | /* encode CertName into output, return total bytes written */ |
wolfSSL | 15:117db924cf7c | 9930 | int SetName(byte* output, word32 outputSz, CertName* name) |
wolfSSL | 15:117db924cf7c | 9931 | { |
wolfSSL | 15:117db924cf7c | 9932 | int totalBytes = 0, i, idx; |
wolfSSL | 15:117db924cf7c | 9933 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9934 | EncodedName* names = NULL; |
wolfSSL | 15:117db924cf7c | 9935 | #else |
wolfSSL | 15:117db924cf7c | 9936 | EncodedName names[NAME_ENTRIES]; |
wolfSSL | 15:117db924cf7c | 9937 | #endif |
wolfSSL | 15:117db924cf7c | 9938 | #ifdef WOLFSSL_MULTI_ATTRIB |
wolfSSL | 15:117db924cf7c | 9939 | EncodedName addNames[CTC_MAX_ATTRIB]; |
wolfSSL | 15:117db924cf7c | 9940 | int j, type; |
wolfSSL | 15:117db924cf7c | 9941 | #endif |
wolfSSL | 15:117db924cf7c | 9942 | |
wolfSSL | 15:117db924cf7c | 9943 | if (output == NULL || name == NULL) |
wolfSSL | 15:117db924cf7c | 9944 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 9945 | |
wolfSSL | 15:117db924cf7c | 9946 | if (outputSz < 3) |
wolfSSL | 15:117db924cf7c | 9947 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9948 | |
wolfSSL | 15:117db924cf7c | 9949 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9950 | names = (EncodedName*)XMALLOC(sizeof(EncodedName) * NAME_ENTRIES, NULL, |
wolfSSL | 15:117db924cf7c | 9951 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9952 | if (names == NULL) |
wolfSSL | 15:117db924cf7c | 9953 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 9954 | #endif |
wolfSSL | 15:117db924cf7c | 9955 | |
wolfSSL | 15:117db924cf7c | 9956 | for (i = 0; i < NAME_ENTRIES; i++) { |
wolfSSL | 15:117db924cf7c | 9957 | int ret; |
wolfSSL | 15:117db924cf7c | 9958 | const char* nameStr = GetOneName(name, i); |
wolfSSL | 15:117db924cf7c | 9959 | |
wolfSSL | 15:117db924cf7c | 9960 | ret = wc_EncodeName(&names[i], nameStr, GetNameType(name, i), |
wolfSSL | 15:117db924cf7c | 9961 | GetNameId(i)); |
wolfSSL | 15:117db924cf7c | 9962 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 9963 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9964 | XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9965 | #endif |
wolfSSL | 15:117db924cf7c | 9966 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9967 | } |
wolfSSL | 15:117db924cf7c | 9968 | totalBytes += ret; |
wolfSSL | 15:117db924cf7c | 9969 | } |
wolfSSL | 15:117db924cf7c | 9970 | #ifdef WOLFSSL_MULTI_ATTRIB |
wolfSSL | 15:117db924cf7c | 9971 | for (i = 0; i < CTC_MAX_ATTRIB; i++) { |
wolfSSL | 15:117db924cf7c | 9972 | if (name->name[i].sz > 0) { |
wolfSSL | 15:117db924cf7c | 9973 | int ret; |
wolfSSL | 15:117db924cf7c | 9974 | ret = wc_EncodeName(&addNames[i], name->name[i].value, |
wolfSSL | 15:117db924cf7c | 9975 | name->name[i].type, name->name[i].id); |
wolfSSL | 15:117db924cf7c | 9976 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 9977 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9978 | XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9979 | #endif |
wolfSSL | 15:117db924cf7c | 9980 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9981 | } |
wolfSSL | 15:117db924cf7c | 9982 | totalBytes += ret; |
wolfSSL | 15:117db924cf7c | 9983 | } |
wolfSSL | 15:117db924cf7c | 9984 | else { |
wolfSSL | 15:117db924cf7c | 9985 | addNames[i].used = 0; |
wolfSSL | 15:117db924cf7c | 9986 | } |
wolfSSL | 15:117db924cf7c | 9987 | } |
wolfSSL | 15:117db924cf7c | 9988 | #endif /* WOLFSSL_MULTI_ATTRIB */ |
wolfSSL | 15:117db924cf7c | 9989 | |
wolfSSL | 15:117db924cf7c | 9990 | /* header */ |
wolfSSL | 15:117db924cf7c | 9991 | idx = SetSequence(totalBytes, output); |
wolfSSL | 15:117db924cf7c | 9992 | totalBytes += idx; |
wolfSSL | 15:117db924cf7c | 9993 | if (totalBytes > ASN_NAME_MAX) { |
wolfSSL | 15:117db924cf7c | 9994 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 9995 | XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 9996 | #endif |
wolfSSL | 15:117db924cf7c | 9997 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 9998 | } |
wolfSSL | 15:117db924cf7c | 9999 | |
wolfSSL | 15:117db924cf7c | 10000 | for (i = 0; i < NAME_ENTRIES; i++) { |
wolfSSL | 15:117db924cf7c | 10001 | #ifdef WOLFSSL_MULTI_ATTRIB |
wolfSSL | 15:117db924cf7c | 10002 | type = GetNameId(i); |
wolfSSL | 15:117db924cf7c | 10003 | |
wolfSSL | 15:117db924cf7c | 10004 | /* list all DC values before OUs */ |
wolfSSL | 15:117db924cf7c | 10005 | if (type == ASN_ORGUNIT_NAME) { |
wolfSSL | 15:117db924cf7c | 10006 | type = ASN_DOMAIN_COMPONENT; |
wolfSSL | 15:117db924cf7c | 10007 | for (j = 0; j < CTC_MAX_ATTRIB; j++) { |
wolfSSL | 15:117db924cf7c | 10008 | if (name->name[j].sz > 0 && type == name->name[j].id) { |
wolfSSL | 15:117db924cf7c | 10009 | if (outputSz < (word32)(idx+addNames[j].totalLen)) { |
wolfSSL | 15:117db924cf7c | 10010 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10011 | XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10012 | #endif |
wolfSSL | 15:117db924cf7c | 10013 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 10014 | } |
wolfSSL | 15:117db924cf7c | 10015 | |
wolfSSL | 15:117db924cf7c | 10016 | XMEMCPY(output + idx, addNames[j].encoded, |
wolfSSL | 15:117db924cf7c | 10017 | addNames[j].totalLen); |
wolfSSL | 15:117db924cf7c | 10018 | idx += addNames[j].totalLen; |
wolfSSL | 15:117db924cf7c | 10019 | } |
wolfSSL | 15:117db924cf7c | 10020 | } |
wolfSSL | 15:117db924cf7c | 10021 | type = ASN_ORGUNIT_NAME; |
wolfSSL | 15:117db924cf7c | 10022 | } |
wolfSSL | 15:117db924cf7c | 10023 | |
wolfSSL | 15:117db924cf7c | 10024 | /* write all similar types to the buffer */ |
wolfSSL | 15:117db924cf7c | 10025 | for (j = 0; j < CTC_MAX_ATTRIB; j++) { |
wolfSSL | 15:117db924cf7c | 10026 | if (name->name[j].sz > 0 && type == name->name[j].id) { |
wolfSSL | 15:117db924cf7c | 10027 | if (outputSz < (word32)(idx+addNames[j].totalLen)) { |
wolfSSL | 15:117db924cf7c | 10028 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10029 | XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10030 | #endif |
wolfSSL | 15:117db924cf7c | 10031 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 10032 | } |
wolfSSL | 15:117db924cf7c | 10033 | |
wolfSSL | 15:117db924cf7c | 10034 | XMEMCPY(output + idx, addNames[j].encoded, |
wolfSSL | 15:117db924cf7c | 10035 | addNames[j].totalLen); |
wolfSSL | 15:117db924cf7c | 10036 | idx += addNames[j].totalLen; |
wolfSSL | 15:117db924cf7c | 10037 | } |
wolfSSL | 15:117db924cf7c | 10038 | } |
wolfSSL | 15:117db924cf7c | 10039 | #endif /* WOLFSSL_MULTI_ATTRIB */ |
wolfSSL | 15:117db924cf7c | 10040 | |
wolfSSL | 15:117db924cf7c | 10041 | if (names[i].used) { |
wolfSSL | 15:117db924cf7c | 10042 | if (outputSz < (word32)(idx+names[i].totalLen)) { |
wolfSSL | 15:117db924cf7c | 10043 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10044 | XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10045 | #endif |
wolfSSL | 15:117db924cf7c | 10046 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 10047 | } |
wolfSSL | 15:117db924cf7c | 10048 | |
wolfSSL | 15:117db924cf7c | 10049 | XMEMCPY(output + idx, names[i].encoded, names[i].totalLen); |
wolfSSL | 15:117db924cf7c | 10050 | idx += names[i].totalLen; |
wolfSSL | 15:117db924cf7c | 10051 | } |
wolfSSL | 15:117db924cf7c | 10052 | } |
wolfSSL | 15:117db924cf7c | 10053 | |
wolfSSL | 15:117db924cf7c | 10054 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10055 | XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10056 | #endif |
wolfSSL | 15:117db924cf7c | 10057 | |
wolfSSL | 15:117db924cf7c | 10058 | return totalBytes; |
wolfSSL | 15:117db924cf7c | 10059 | } |
wolfSSL | 15:117db924cf7c | 10060 | |
wolfSSL | 15:117db924cf7c | 10061 | /* encode info from cert into DER encoded format */ |
wolfSSL | 15:117db924cf7c | 10062 | static int EncodeCert(Cert* cert, DerCert* der, RsaKey* rsaKey, ecc_key* eccKey, |
wolfSSL | 15:117db924cf7c | 10063 | WC_RNG* rng, const byte* ntruKey, word16 ntruSz, |
wolfSSL | 15:117db924cf7c | 10064 | ed25519_key* ed25519Key) |
wolfSSL | 15:117db924cf7c | 10065 | { |
wolfSSL | 15:117db924cf7c | 10066 | int ret; |
wolfSSL | 15:117db924cf7c | 10067 | |
wolfSSL | 15:117db924cf7c | 10068 | if (cert == NULL || der == NULL || rng == NULL) |
wolfSSL | 15:117db924cf7c | 10069 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 10070 | |
wolfSSL | 15:117db924cf7c | 10071 | /* make sure at least one key type is provided */ |
wolfSSL | 15:117db924cf7c | 10072 | if (rsaKey == NULL && eccKey == NULL && ed25519Key == NULL && ntruKey == NULL) |
wolfSSL | 15:117db924cf7c | 10073 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10074 | |
wolfSSL | 15:117db924cf7c | 10075 | /* init */ |
wolfSSL | 15:117db924cf7c | 10076 | XMEMSET(der, 0, sizeof(DerCert)); |
wolfSSL | 15:117db924cf7c | 10077 | |
wolfSSL | 15:117db924cf7c | 10078 | /* version */ |
wolfSSL | 15:117db924cf7c | 10079 | der->versionSz = SetMyVersion(cert->version, der->version, TRUE); |
wolfSSL | 15:117db924cf7c | 10080 | |
wolfSSL | 15:117db924cf7c | 10081 | /* serial number (must be positive) */ |
wolfSSL | 15:117db924cf7c | 10082 | if (cert->serialSz == 0) { |
wolfSSL | 15:117db924cf7c | 10083 | /* generate random serial */ |
wolfSSL | 15:117db924cf7c | 10084 | cert->serialSz = CTC_SERIAL_SIZE; |
wolfSSL | 15:117db924cf7c | 10085 | ret = wc_RNG_GenerateBlock(rng, cert->serial, cert->serialSz); |
wolfSSL | 15:117db924cf7c | 10086 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 10087 | return ret; |
wolfSSL | 15:117db924cf7c | 10088 | } |
wolfSSL | 15:117db924cf7c | 10089 | der->serialSz = SetSerialNumber(cert->serial, cert->serialSz, der->serial, |
wolfSSL | 15:117db924cf7c | 10090 | CTC_SERIAL_SIZE); |
wolfSSL | 15:117db924cf7c | 10091 | if (der->serialSz < 0) |
wolfSSL | 15:117db924cf7c | 10092 | return der->serialSz; |
wolfSSL | 15:117db924cf7c | 10093 | |
wolfSSL | 15:117db924cf7c | 10094 | /* signature algo */ |
wolfSSL | 15:117db924cf7c | 10095 | der->sigAlgoSz = SetAlgoID(cert->sigType, der->sigAlgo, oidSigType, 0); |
wolfSSL | 15:117db924cf7c | 10096 | if (der->sigAlgoSz <= 0) |
wolfSSL | 15:117db924cf7c | 10097 | return ALGO_ID_E; |
wolfSSL | 15:117db924cf7c | 10098 | |
wolfSSL | 15:117db924cf7c | 10099 | /* public key */ |
wolfSSL | 15:117db924cf7c | 10100 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 10101 | if (cert->keyType == RSA_KEY) { |
wolfSSL | 15:117db924cf7c | 10102 | if (rsaKey == NULL) |
wolfSSL | 15:117db924cf7c | 10103 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10104 | der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey, |
wolfSSL | 15:117db924cf7c | 10105 | sizeof(der->publicKey), 1); |
wolfSSL | 15:117db924cf7c | 10106 | } |
wolfSSL | 15:117db924cf7c | 10107 | #endif |
wolfSSL | 15:117db924cf7c | 10108 | |
wolfSSL | 15:117db924cf7c | 10109 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 10110 | if (cert->keyType == ECC_KEY) { |
wolfSSL | 15:117db924cf7c | 10111 | if (eccKey == NULL) |
wolfSSL | 15:117db924cf7c | 10112 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10113 | der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey, 1); |
wolfSSL | 15:117db924cf7c | 10114 | } |
wolfSSL | 15:117db924cf7c | 10115 | #endif |
wolfSSL | 15:117db924cf7c | 10116 | |
wolfSSL | 15:117db924cf7c | 10117 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 10118 | if (cert->keyType == ED25519_KEY) { |
wolfSSL | 15:117db924cf7c | 10119 | if (ed25519Key == NULL) |
wolfSSL | 15:117db924cf7c | 10120 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10121 | der->publicKeySz = SetEd25519PublicKey(der->publicKey, ed25519Key, 1); |
wolfSSL | 15:117db924cf7c | 10122 | } |
wolfSSL | 15:117db924cf7c | 10123 | #endif |
wolfSSL | 15:117db924cf7c | 10124 | |
wolfSSL | 15:117db924cf7c | 10125 | #ifdef HAVE_NTRU |
wolfSSL | 15:117db924cf7c | 10126 | if (cert->keyType == NTRU_KEY) { |
wolfSSL | 15:117db924cf7c | 10127 | word32 rc; |
wolfSSL | 15:117db924cf7c | 10128 | word16 encodedSz; |
wolfSSL | 15:117db924cf7c | 10129 | |
wolfSSL | 15:117db924cf7c | 10130 | if (ntruKey == NULL) |
wolfSSL | 15:117db924cf7c | 10131 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10132 | |
wolfSSL | 15:117db924cf7c | 10133 | rc = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(ntruSz, |
wolfSSL | 15:117db924cf7c | 10134 | ntruKey, &encodedSz, NULL); |
wolfSSL | 15:117db924cf7c | 10135 | if (rc != NTRU_OK) |
wolfSSL | 15:117db924cf7c | 10136 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10137 | if (encodedSz > MAX_PUBLIC_KEY_SZ) |
wolfSSL | 15:117db924cf7c | 10138 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10139 | |
wolfSSL | 15:117db924cf7c | 10140 | rc = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(ntruSz, |
wolfSSL | 15:117db924cf7c | 10141 | ntruKey, &encodedSz, der->publicKey); |
wolfSSL | 15:117db924cf7c | 10142 | if (rc != NTRU_OK) |
wolfSSL | 15:117db924cf7c | 10143 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10144 | |
wolfSSL | 15:117db924cf7c | 10145 | der->publicKeySz = encodedSz; |
wolfSSL | 15:117db924cf7c | 10146 | } |
wolfSSL | 15:117db924cf7c | 10147 | #else |
wolfSSL | 15:117db924cf7c | 10148 | (void)ntruSz; |
wolfSSL | 15:117db924cf7c | 10149 | #endif /* HAVE_NTRU */ |
wolfSSL | 15:117db924cf7c | 10150 | |
wolfSSL | 15:117db924cf7c | 10151 | if (der->publicKeySz <= 0) |
wolfSSL | 15:117db924cf7c | 10152 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10153 | |
wolfSSL | 15:117db924cf7c | 10154 | der->validitySz = 0; |
wolfSSL | 15:117db924cf7c | 10155 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 10156 | /* date validity copy ? */ |
wolfSSL | 15:117db924cf7c | 10157 | if (cert->beforeDateSz && cert->afterDateSz) { |
wolfSSL | 15:117db924cf7c | 10158 | der->validitySz = CopyValidity(der->validity, cert); |
wolfSSL | 15:117db924cf7c | 10159 | if (der->validitySz <= 0) |
wolfSSL | 15:117db924cf7c | 10160 | return DATE_E; |
wolfSSL | 15:117db924cf7c | 10161 | } |
wolfSSL | 15:117db924cf7c | 10162 | #endif |
wolfSSL | 15:117db924cf7c | 10163 | |
wolfSSL | 15:117db924cf7c | 10164 | /* date validity */ |
wolfSSL | 15:117db924cf7c | 10165 | if (der->validitySz == 0) { |
wolfSSL | 15:117db924cf7c | 10166 | der->validitySz = SetValidity(der->validity, cert->daysValid); |
wolfSSL | 15:117db924cf7c | 10167 | if (der->validitySz <= 0) |
wolfSSL | 15:117db924cf7c | 10168 | return DATE_E; |
wolfSSL | 15:117db924cf7c | 10169 | } |
wolfSSL | 15:117db924cf7c | 10170 | |
wolfSSL | 15:117db924cf7c | 10171 | /* subject name */ |
wolfSSL | 15:117db924cf7c | 10172 | der->subjectSz = SetName(der->subject, sizeof(der->subject), &cert->subject); |
wolfSSL | 15:117db924cf7c | 10173 | if (der->subjectSz <= 0) |
wolfSSL | 15:117db924cf7c | 10174 | return SUBJECT_E; |
wolfSSL | 15:117db924cf7c | 10175 | |
wolfSSL | 15:117db924cf7c | 10176 | /* issuer name */ |
wolfSSL | 15:117db924cf7c | 10177 | der->issuerSz = SetName(der->issuer, sizeof(der->issuer), cert->selfSigned ? |
wolfSSL | 15:117db924cf7c | 10178 | &cert->subject : &cert->issuer); |
wolfSSL | 15:117db924cf7c | 10179 | if (der->issuerSz <= 0) |
wolfSSL | 15:117db924cf7c | 10180 | return ISSUER_E; |
wolfSSL | 15:117db924cf7c | 10181 | |
wolfSSL | 15:117db924cf7c | 10182 | /* set the extensions */ |
wolfSSL | 15:117db924cf7c | 10183 | der->extensionsSz = 0; |
wolfSSL | 15:117db924cf7c | 10184 | |
wolfSSL | 15:117db924cf7c | 10185 | /* CA */ |
wolfSSL | 15:117db924cf7c | 10186 | if (cert->isCA) { |
wolfSSL | 15:117db924cf7c | 10187 | der->caSz = SetCa(der->ca, sizeof(der->ca)); |
wolfSSL | 15:117db924cf7c | 10188 | if (der->caSz <= 0) |
wolfSSL | 15:117db924cf7c | 10189 | return CA_TRUE_E; |
wolfSSL | 15:117db924cf7c | 10190 | |
wolfSSL | 15:117db924cf7c | 10191 | der->extensionsSz += der->caSz; |
wolfSSL | 15:117db924cf7c | 10192 | } |
wolfSSL | 15:117db924cf7c | 10193 | else |
wolfSSL | 15:117db924cf7c | 10194 | der->caSz = 0; |
wolfSSL | 15:117db924cf7c | 10195 | |
wolfSSL | 15:117db924cf7c | 10196 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 10197 | /* Alternative Name */ |
wolfSSL | 15:117db924cf7c | 10198 | if (cert->altNamesSz) { |
wolfSSL | 15:117db924cf7c | 10199 | der->altNamesSz = SetAltNames(der->altNames, sizeof(der->altNames), |
wolfSSL | 15:117db924cf7c | 10200 | cert->altNames, cert->altNamesSz); |
wolfSSL | 15:117db924cf7c | 10201 | if (der->altNamesSz <= 0) |
wolfSSL | 15:117db924cf7c | 10202 | return ALT_NAME_E; |
wolfSSL | 15:117db924cf7c | 10203 | |
wolfSSL | 15:117db924cf7c | 10204 | der->extensionsSz += der->altNamesSz; |
wolfSSL | 15:117db924cf7c | 10205 | } |
wolfSSL | 15:117db924cf7c | 10206 | else |
wolfSSL | 15:117db924cf7c | 10207 | der->altNamesSz = 0; |
wolfSSL | 15:117db924cf7c | 10208 | #endif |
wolfSSL | 15:117db924cf7c | 10209 | |
wolfSSL | 15:117db924cf7c | 10210 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 10211 | /* SKID */ |
wolfSSL | 15:117db924cf7c | 10212 | if (cert->skidSz) { |
wolfSSL | 15:117db924cf7c | 10213 | /* check the provided SKID size */ |
wolfSSL | 15:117db924cf7c | 10214 | if (cert->skidSz > (int)min(CTC_MAX_SKID_SIZE, sizeof(der->skid))) |
wolfSSL | 15:117db924cf7c | 10215 | return SKID_E; |
wolfSSL | 15:117db924cf7c | 10216 | |
wolfSSL | 15:117db924cf7c | 10217 | /* Note: different skid buffers sizes for der (MAX_KID_SZ) and |
wolfSSL | 15:117db924cf7c | 10218 | cert (CTC_MAX_SKID_SIZE). */ |
wolfSSL | 15:117db924cf7c | 10219 | der->skidSz = SetSKID(der->skid, sizeof(der->skid), |
wolfSSL | 15:117db924cf7c | 10220 | cert->skid, cert->skidSz); |
wolfSSL | 15:117db924cf7c | 10221 | if (der->skidSz <= 0) |
wolfSSL | 15:117db924cf7c | 10222 | return SKID_E; |
wolfSSL | 15:117db924cf7c | 10223 | |
wolfSSL | 15:117db924cf7c | 10224 | der->extensionsSz += der->skidSz; |
wolfSSL | 15:117db924cf7c | 10225 | } |
wolfSSL | 15:117db924cf7c | 10226 | else |
wolfSSL | 15:117db924cf7c | 10227 | der->skidSz = 0; |
wolfSSL | 15:117db924cf7c | 10228 | |
wolfSSL | 15:117db924cf7c | 10229 | /* AKID */ |
wolfSSL | 15:117db924cf7c | 10230 | if (cert->akidSz) { |
wolfSSL | 15:117db924cf7c | 10231 | /* check the provided AKID size */ |
wolfSSL | 15:117db924cf7c | 10232 | if (cert->akidSz > (int)min(CTC_MAX_AKID_SIZE, sizeof(der->akid))) |
wolfSSL | 15:117db924cf7c | 10233 | return AKID_E; |
wolfSSL | 15:117db924cf7c | 10234 | |
wolfSSL | 15:117db924cf7c | 10235 | der->akidSz = SetAKID(der->akid, sizeof(der->akid), |
wolfSSL | 15:117db924cf7c | 10236 | cert->akid, cert->akidSz, cert->heap); |
wolfSSL | 15:117db924cf7c | 10237 | if (der->akidSz <= 0) |
wolfSSL | 15:117db924cf7c | 10238 | return AKID_E; |
wolfSSL | 15:117db924cf7c | 10239 | |
wolfSSL | 15:117db924cf7c | 10240 | der->extensionsSz += der->akidSz; |
wolfSSL | 15:117db924cf7c | 10241 | } |
wolfSSL | 15:117db924cf7c | 10242 | else |
wolfSSL | 15:117db924cf7c | 10243 | der->akidSz = 0; |
wolfSSL | 15:117db924cf7c | 10244 | |
wolfSSL | 15:117db924cf7c | 10245 | /* Key Usage */ |
wolfSSL | 15:117db924cf7c | 10246 | if (cert->keyUsage != 0){ |
wolfSSL | 15:117db924cf7c | 10247 | der->keyUsageSz = SetKeyUsage(der->keyUsage, sizeof(der->keyUsage), |
wolfSSL | 15:117db924cf7c | 10248 | cert->keyUsage); |
wolfSSL | 15:117db924cf7c | 10249 | if (der->keyUsageSz <= 0) |
wolfSSL | 15:117db924cf7c | 10250 | return KEYUSAGE_E; |
wolfSSL | 15:117db924cf7c | 10251 | |
wolfSSL | 15:117db924cf7c | 10252 | der->extensionsSz += der->keyUsageSz; |
wolfSSL | 15:117db924cf7c | 10253 | } |
wolfSSL | 15:117db924cf7c | 10254 | else |
wolfSSL | 15:117db924cf7c | 10255 | der->keyUsageSz = 0; |
wolfSSL | 15:117db924cf7c | 10256 | |
wolfSSL | 15:117db924cf7c | 10257 | /* Extended Key Usage */ |
wolfSSL | 15:117db924cf7c | 10258 | if (cert->extKeyUsage != 0){ |
wolfSSL | 15:117db924cf7c | 10259 | der->extKeyUsageSz = SetExtKeyUsage(cert, der->extKeyUsage, |
wolfSSL | 15:117db924cf7c | 10260 | sizeof(der->extKeyUsage), cert->extKeyUsage); |
wolfSSL | 15:117db924cf7c | 10261 | if (der->extKeyUsageSz <= 0) |
wolfSSL | 15:117db924cf7c | 10262 | return EXTKEYUSAGE_E; |
wolfSSL | 15:117db924cf7c | 10263 | |
wolfSSL | 15:117db924cf7c | 10264 | der->extensionsSz += der->extKeyUsageSz; |
wolfSSL | 15:117db924cf7c | 10265 | } |
wolfSSL | 15:117db924cf7c | 10266 | else |
wolfSSL | 15:117db924cf7c | 10267 | der->extKeyUsageSz = 0; |
wolfSSL | 15:117db924cf7c | 10268 | |
wolfSSL | 15:117db924cf7c | 10269 | /* Certificate Policies */ |
wolfSSL | 15:117db924cf7c | 10270 | if (cert->certPoliciesNb != 0) { |
wolfSSL | 15:117db924cf7c | 10271 | der->certPoliciesSz = SetCertificatePolicies(der->certPolicies, |
wolfSSL | 15:117db924cf7c | 10272 | sizeof(der->certPolicies), |
wolfSSL | 15:117db924cf7c | 10273 | cert->certPolicies, |
wolfSSL | 15:117db924cf7c | 10274 | cert->certPoliciesNb, |
wolfSSL | 15:117db924cf7c | 10275 | cert->heap); |
wolfSSL | 15:117db924cf7c | 10276 | if (der->certPoliciesSz <= 0) |
wolfSSL | 15:117db924cf7c | 10277 | return CERTPOLICIES_E; |
wolfSSL | 15:117db924cf7c | 10278 | |
wolfSSL | 15:117db924cf7c | 10279 | der->extensionsSz += der->certPoliciesSz; |
wolfSSL | 15:117db924cf7c | 10280 | } |
wolfSSL | 15:117db924cf7c | 10281 | else |
wolfSSL | 15:117db924cf7c | 10282 | der->certPoliciesSz = 0; |
wolfSSL | 15:117db924cf7c | 10283 | #endif /* WOLFSSL_CERT_EXT */ |
wolfSSL | 15:117db924cf7c | 10284 | |
wolfSSL | 15:117db924cf7c | 10285 | /* put extensions */ |
wolfSSL | 15:117db924cf7c | 10286 | if (der->extensionsSz > 0) { |
wolfSSL | 15:117db924cf7c | 10287 | |
wolfSSL | 15:117db924cf7c | 10288 | /* put the start of extensions sequence (ID, Size) */ |
wolfSSL | 15:117db924cf7c | 10289 | der->extensionsSz = SetExtensionsHeader(der->extensions, |
wolfSSL | 15:117db924cf7c | 10290 | sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10291 | der->extensionsSz); |
wolfSSL | 15:117db924cf7c | 10292 | if (der->extensionsSz <= 0) |
wolfSSL | 15:117db924cf7c | 10293 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10294 | |
wolfSSL | 15:117db924cf7c | 10295 | /* put CA */ |
wolfSSL | 15:117db924cf7c | 10296 | if (der->caSz) { |
wolfSSL | 15:117db924cf7c | 10297 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10298 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10299 | der->ca, der->caSz); |
wolfSSL | 15:117db924cf7c | 10300 | if (ret == 0) |
wolfSSL | 15:117db924cf7c | 10301 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10302 | } |
wolfSSL | 15:117db924cf7c | 10303 | |
wolfSSL | 15:117db924cf7c | 10304 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 10305 | /* put Alternative Names */ |
wolfSSL | 15:117db924cf7c | 10306 | if (der->altNamesSz) { |
wolfSSL | 15:117db924cf7c | 10307 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10308 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10309 | der->altNames, der->altNamesSz); |
wolfSSL | 15:117db924cf7c | 10310 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10311 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10312 | } |
wolfSSL | 15:117db924cf7c | 10313 | #endif |
wolfSSL | 15:117db924cf7c | 10314 | |
wolfSSL | 15:117db924cf7c | 10315 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 10316 | /* put SKID */ |
wolfSSL | 15:117db924cf7c | 10317 | if (der->skidSz) { |
wolfSSL | 15:117db924cf7c | 10318 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10319 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10320 | der->skid, der->skidSz); |
wolfSSL | 15:117db924cf7c | 10321 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10322 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10323 | } |
wolfSSL | 15:117db924cf7c | 10324 | |
wolfSSL | 15:117db924cf7c | 10325 | /* put AKID */ |
wolfSSL | 15:117db924cf7c | 10326 | if (der->akidSz) { |
wolfSSL | 15:117db924cf7c | 10327 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10328 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10329 | der->akid, der->akidSz); |
wolfSSL | 15:117db924cf7c | 10330 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10331 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10332 | } |
wolfSSL | 15:117db924cf7c | 10333 | |
wolfSSL | 15:117db924cf7c | 10334 | /* put KeyUsage */ |
wolfSSL | 15:117db924cf7c | 10335 | if (der->keyUsageSz) { |
wolfSSL | 15:117db924cf7c | 10336 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10337 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10338 | der->keyUsage, der->keyUsageSz); |
wolfSSL | 15:117db924cf7c | 10339 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10340 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10341 | } |
wolfSSL | 15:117db924cf7c | 10342 | |
wolfSSL | 15:117db924cf7c | 10343 | /* put ExtendedKeyUsage */ |
wolfSSL | 15:117db924cf7c | 10344 | if (der->extKeyUsageSz) { |
wolfSSL | 15:117db924cf7c | 10345 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10346 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10347 | der->extKeyUsage, der->extKeyUsageSz); |
wolfSSL | 15:117db924cf7c | 10348 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10349 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10350 | } |
wolfSSL | 15:117db924cf7c | 10351 | |
wolfSSL | 15:117db924cf7c | 10352 | /* put Certificate Policies */ |
wolfSSL | 15:117db924cf7c | 10353 | if (der->certPoliciesSz) { |
wolfSSL | 15:117db924cf7c | 10354 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10355 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10356 | der->certPolicies, der->certPoliciesSz); |
wolfSSL | 15:117db924cf7c | 10357 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10358 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10359 | } |
wolfSSL | 15:117db924cf7c | 10360 | #endif /* WOLFSSL_CERT_EXT */ |
wolfSSL | 15:117db924cf7c | 10361 | } |
wolfSSL | 15:117db924cf7c | 10362 | |
wolfSSL | 15:117db924cf7c | 10363 | der->total = der->versionSz + der->serialSz + der->sigAlgoSz + |
wolfSSL | 15:117db924cf7c | 10364 | der->publicKeySz + der->validitySz + der->subjectSz + der->issuerSz + |
wolfSSL | 15:117db924cf7c | 10365 | der->extensionsSz; |
wolfSSL | 15:117db924cf7c | 10366 | |
wolfSSL | 15:117db924cf7c | 10367 | return 0; |
wolfSSL | 15:117db924cf7c | 10368 | } |
wolfSSL | 15:117db924cf7c | 10369 | |
wolfSSL | 15:117db924cf7c | 10370 | |
wolfSSL | 15:117db924cf7c | 10371 | /* write DER encoded cert to buffer, size already checked */ |
wolfSSL | 15:117db924cf7c | 10372 | static int WriteCertBody(DerCert* der, byte* buffer) |
wolfSSL | 15:117db924cf7c | 10373 | { |
wolfSSL | 15:117db924cf7c | 10374 | int idx; |
wolfSSL | 15:117db924cf7c | 10375 | |
wolfSSL | 15:117db924cf7c | 10376 | /* signed part header */ |
wolfSSL | 15:117db924cf7c | 10377 | idx = SetSequence(der->total, buffer); |
wolfSSL | 15:117db924cf7c | 10378 | /* version */ |
wolfSSL | 15:117db924cf7c | 10379 | XMEMCPY(buffer + idx, der->version, der->versionSz); |
wolfSSL | 15:117db924cf7c | 10380 | idx += der->versionSz; |
wolfSSL | 15:117db924cf7c | 10381 | /* serial */ |
wolfSSL | 15:117db924cf7c | 10382 | XMEMCPY(buffer + idx, der->serial, der->serialSz); |
wolfSSL | 15:117db924cf7c | 10383 | idx += der->serialSz; |
wolfSSL | 15:117db924cf7c | 10384 | /* sig algo */ |
wolfSSL | 15:117db924cf7c | 10385 | XMEMCPY(buffer + idx, der->sigAlgo, der->sigAlgoSz); |
wolfSSL | 15:117db924cf7c | 10386 | idx += der->sigAlgoSz; |
wolfSSL | 15:117db924cf7c | 10387 | /* issuer */ |
wolfSSL | 15:117db924cf7c | 10388 | XMEMCPY(buffer + idx, der->issuer, der->issuerSz); |
wolfSSL | 15:117db924cf7c | 10389 | idx += der->issuerSz; |
wolfSSL | 15:117db924cf7c | 10390 | /* validity */ |
wolfSSL | 15:117db924cf7c | 10391 | XMEMCPY(buffer + idx, der->validity, der->validitySz); |
wolfSSL | 15:117db924cf7c | 10392 | idx += der->validitySz; |
wolfSSL | 15:117db924cf7c | 10393 | /* subject */ |
wolfSSL | 15:117db924cf7c | 10394 | XMEMCPY(buffer + idx, der->subject, der->subjectSz); |
wolfSSL | 15:117db924cf7c | 10395 | idx += der->subjectSz; |
wolfSSL | 15:117db924cf7c | 10396 | /* public key */ |
wolfSSL | 15:117db924cf7c | 10397 | XMEMCPY(buffer + idx, der->publicKey, der->publicKeySz); |
wolfSSL | 15:117db924cf7c | 10398 | idx += der->publicKeySz; |
wolfSSL | 15:117db924cf7c | 10399 | if (der->extensionsSz) { |
wolfSSL | 15:117db924cf7c | 10400 | /* extensions */ |
wolfSSL | 15:117db924cf7c | 10401 | XMEMCPY(buffer + idx, der->extensions, min(der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10402 | (int)sizeof(der->extensions))); |
wolfSSL | 15:117db924cf7c | 10403 | idx += der->extensionsSz; |
wolfSSL | 15:117db924cf7c | 10404 | } |
wolfSSL | 15:117db924cf7c | 10405 | |
wolfSSL | 15:117db924cf7c | 10406 | return idx; |
wolfSSL | 15:117db924cf7c | 10407 | } |
wolfSSL | 15:117db924cf7c | 10408 | |
wolfSSL | 15:117db924cf7c | 10409 | |
wolfSSL | 15:117db924cf7c | 10410 | /* Make RSA signature from buffer (sz), write to sig (sigSz) */ |
wolfSSL | 15:117db924cf7c | 10411 | static int MakeSignature(CertSignCtx* certSignCtx, const byte* buffer, int sz, |
wolfSSL | 15:117db924cf7c | 10412 | byte* sig, int sigSz, RsaKey* rsaKey, ecc_key* eccKey, |
wolfSSL | 15:117db924cf7c | 10413 | ed25519_key* ed25519Key, WC_RNG* rng, int sigAlgoType, void* heap) |
wolfSSL | 15:117db924cf7c | 10414 | { |
wolfSSL | 15:117db924cf7c | 10415 | int digestSz = 0, typeH = 0, ret = 0; |
wolfSSL | 15:117db924cf7c | 10416 | |
wolfSSL | 15:117db924cf7c | 10417 | (void)digestSz; |
wolfSSL | 15:117db924cf7c | 10418 | (void)typeH; |
wolfSSL | 15:117db924cf7c | 10419 | (void)buffer; |
wolfSSL | 15:117db924cf7c | 10420 | (void)sz; |
wolfSSL | 15:117db924cf7c | 10421 | (void)sig; |
wolfSSL | 15:117db924cf7c | 10422 | (void)sigSz; |
wolfSSL | 15:117db924cf7c | 10423 | (void)rsaKey; |
wolfSSL | 15:117db924cf7c | 10424 | (void)eccKey; |
wolfSSL | 15:117db924cf7c | 10425 | (void)ed25519Key; |
wolfSSL | 15:117db924cf7c | 10426 | (void)rng; |
wolfSSL | 15:117db924cf7c | 10427 | |
wolfSSL | 15:117db924cf7c | 10428 | switch (certSignCtx->state) { |
wolfSSL | 15:117db924cf7c | 10429 | case CERTSIGN_STATE_BEGIN: |
wolfSSL | 15:117db924cf7c | 10430 | case CERTSIGN_STATE_DIGEST: |
wolfSSL | 15:117db924cf7c | 10431 | |
wolfSSL | 15:117db924cf7c | 10432 | certSignCtx->state = CERTSIGN_STATE_DIGEST; |
wolfSSL | 15:117db924cf7c | 10433 | certSignCtx->digest = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, |
wolfSSL | 15:117db924cf7c | 10434 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10435 | if (certSignCtx->digest == NULL) { |
wolfSSL | 15:117db924cf7c | 10436 | ret = MEMORY_E; goto exit_ms; |
wolfSSL | 15:117db924cf7c | 10437 | } |
wolfSSL | 15:117db924cf7c | 10438 | |
wolfSSL | 15:117db924cf7c | 10439 | ret = HashForSignature(buffer, sz, sigAlgoType, certSignCtx->digest, |
wolfSSL | 15:117db924cf7c | 10440 | &typeH, &digestSz, 0); |
wolfSSL | 15:117db924cf7c | 10441 | /* set next state, since WC_PENDING rentry for these are not "call again" */ |
wolfSSL | 15:117db924cf7c | 10442 | certSignCtx->state = CERTSIGN_STATE_ENCODE; |
wolfSSL | 15:117db924cf7c | 10443 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 10444 | goto exit_ms; |
wolfSSL | 15:117db924cf7c | 10445 | } |
wolfSSL | 15:117db924cf7c | 10446 | FALL_THROUGH; |
wolfSSL | 15:117db924cf7c | 10447 | |
wolfSSL | 15:117db924cf7c | 10448 | case CERTSIGN_STATE_ENCODE: |
wolfSSL | 15:117db924cf7c | 10449 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 10450 | if (rsaKey) { |
wolfSSL | 15:117db924cf7c | 10451 | certSignCtx->encSig = (byte*)XMALLOC(MAX_DER_DIGEST_SZ, heap, |
wolfSSL | 15:117db924cf7c | 10452 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10453 | if (certSignCtx->encSig == NULL) { |
wolfSSL | 15:117db924cf7c | 10454 | ret = MEMORY_E; goto exit_ms; |
wolfSSL | 15:117db924cf7c | 10455 | } |
wolfSSL | 15:117db924cf7c | 10456 | |
wolfSSL | 15:117db924cf7c | 10457 | /* signature */ |
wolfSSL | 15:117db924cf7c | 10458 | certSignCtx->encSigSz = wc_EncodeSignature(certSignCtx->encSig, |
wolfSSL | 15:117db924cf7c | 10459 | certSignCtx->digest, digestSz, typeH); |
wolfSSL | 15:117db924cf7c | 10460 | } |
wolfSSL | 15:117db924cf7c | 10461 | #endif /* !NO_RSA */ |
wolfSSL | 15:117db924cf7c | 10462 | FALL_THROUGH; |
wolfSSL | 15:117db924cf7c | 10463 | |
wolfSSL | 15:117db924cf7c | 10464 | case CERTSIGN_STATE_DO: |
wolfSSL | 15:117db924cf7c | 10465 | certSignCtx->state = CERTSIGN_STATE_DO; |
wolfSSL | 15:117db924cf7c | 10466 | ret = ALGO_ID_E; /* default to error */ |
wolfSSL | 15:117db924cf7c | 10467 | |
wolfSSL | 15:117db924cf7c | 10468 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 10469 | if (rsaKey) { |
wolfSSL | 15:117db924cf7c | 10470 | /* signature */ |
wolfSSL | 15:117db924cf7c | 10471 | ret = wc_RsaSSL_Sign(certSignCtx->encSig, certSignCtx->encSigSz, |
wolfSSL | 15:117db924cf7c | 10472 | sig, sigSz, rsaKey, rng); |
wolfSSL | 15:117db924cf7c | 10473 | } |
wolfSSL | 15:117db924cf7c | 10474 | #endif /* !NO_RSA */ |
wolfSSL | 15:117db924cf7c | 10475 | |
wolfSSL | 15:117db924cf7c | 10476 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 10477 | if (!rsaKey && eccKey) { |
wolfSSL | 15:117db924cf7c | 10478 | word32 outSz = sigSz; |
wolfSSL | 15:117db924cf7c | 10479 | |
wolfSSL | 15:117db924cf7c | 10480 | ret = wc_ecc_sign_hash(certSignCtx->digest, digestSz, |
wolfSSL | 15:117db924cf7c | 10481 | sig, &outSz, rng, eccKey); |
wolfSSL | 15:117db924cf7c | 10482 | if (ret == 0) |
wolfSSL | 15:117db924cf7c | 10483 | ret = outSz; |
wolfSSL | 15:117db924cf7c | 10484 | } |
wolfSSL | 15:117db924cf7c | 10485 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 10486 | |
wolfSSL | 15:117db924cf7c | 10487 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 10488 | if (!rsaKey && !eccKey && ed25519Key) { |
wolfSSL | 15:117db924cf7c | 10489 | word32 outSz = sigSz; |
wolfSSL | 15:117db924cf7c | 10490 | |
wolfSSL | 15:117db924cf7c | 10491 | ret = wc_ed25519_sign_msg(buffer, sz, sig, &outSz, ed25519Key); |
wolfSSL | 15:117db924cf7c | 10492 | if (ret == 0) |
wolfSSL | 15:117db924cf7c | 10493 | ret = outSz; |
wolfSSL | 15:117db924cf7c | 10494 | } |
wolfSSL | 15:117db924cf7c | 10495 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 10496 | break; |
wolfSSL | 15:117db924cf7c | 10497 | } |
wolfSSL | 15:117db924cf7c | 10498 | |
wolfSSL | 15:117db924cf7c | 10499 | exit_ms: |
wolfSSL | 15:117db924cf7c | 10500 | |
wolfSSL | 15:117db924cf7c | 10501 | if (ret == WC_PENDING_E) { |
wolfSSL | 15:117db924cf7c | 10502 | return ret; |
wolfSSL | 15:117db924cf7c | 10503 | } |
wolfSSL | 15:117db924cf7c | 10504 | |
wolfSSL | 15:117db924cf7c | 10505 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 10506 | if (rsaKey) { |
wolfSSL | 15:117db924cf7c | 10507 | XFREE(certSignCtx->encSig, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10508 | } |
wolfSSL | 15:117db924cf7c | 10509 | #endif /* !NO_RSA */ |
wolfSSL | 15:117db924cf7c | 10510 | |
wolfSSL | 15:117db924cf7c | 10511 | XFREE(certSignCtx->digest, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10512 | certSignCtx->digest = NULL; |
wolfSSL | 15:117db924cf7c | 10513 | |
wolfSSL | 15:117db924cf7c | 10514 | /* reset state */ |
wolfSSL | 15:117db924cf7c | 10515 | certSignCtx->state = CERTSIGN_STATE_BEGIN; |
wolfSSL | 15:117db924cf7c | 10516 | |
wolfSSL | 15:117db924cf7c | 10517 | return ret; |
wolfSSL | 15:117db924cf7c | 10518 | } |
wolfSSL | 15:117db924cf7c | 10519 | |
wolfSSL | 15:117db924cf7c | 10520 | |
wolfSSL | 15:117db924cf7c | 10521 | /* add signature to end of buffer, size of buffer assumed checked, return |
wolfSSL | 15:117db924cf7c | 10522 | new length */ |
wolfSSL | 15:117db924cf7c | 10523 | static int AddSignature(byte* buffer, int bodySz, const byte* sig, int sigSz, |
wolfSSL | 15:117db924cf7c | 10524 | int sigAlgoType) |
wolfSSL | 15:117db924cf7c | 10525 | { |
wolfSSL | 15:117db924cf7c | 10526 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 10527 | int idx = bodySz, seqSz; |
wolfSSL | 15:117db924cf7c | 10528 | |
wolfSSL | 15:117db924cf7c | 10529 | /* algo */ |
wolfSSL | 15:117db924cf7c | 10530 | idx += SetAlgoID(sigAlgoType, buffer + idx, oidSigType, 0); |
wolfSSL | 15:117db924cf7c | 10531 | /* bit string */ |
wolfSSL | 15:117db924cf7c | 10532 | idx += SetBitString(sigSz, 0, buffer + idx); |
wolfSSL | 15:117db924cf7c | 10533 | /* signature */ |
wolfSSL | 15:117db924cf7c | 10534 | XMEMCPY(buffer + idx, sig, sigSz); |
wolfSSL | 15:117db924cf7c | 10535 | idx += sigSz; |
wolfSSL | 15:117db924cf7c | 10536 | |
wolfSSL | 15:117db924cf7c | 10537 | /* make room for overall header */ |
wolfSSL | 15:117db924cf7c | 10538 | seqSz = SetSequence(idx, seq); |
wolfSSL | 15:117db924cf7c | 10539 | XMEMMOVE(buffer + seqSz, buffer, idx); |
wolfSSL | 15:117db924cf7c | 10540 | XMEMCPY(buffer, seq, seqSz); |
wolfSSL | 15:117db924cf7c | 10541 | |
wolfSSL | 15:117db924cf7c | 10542 | return idx + seqSz; |
wolfSSL | 15:117db924cf7c | 10543 | } |
wolfSSL | 15:117db924cf7c | 10544 | |
wolfSSL | 15:117db924cf7c | 10545 | |
wolfSSL | 15:117db924cf7c | 10546 | /* Make an x509 Certificate v3 any key type from cert input, write to buffer */ |
wolfSSL | 15:117db924cf7c | 10547 | static int MakeAnyCert(Cert* cert, byte* derBuffer, word32 derSz, |
wolfSSL | 15:117db924cf7c | 10548 | RsaKey* rsaKey, ecc_key* eccKey, WC_RNG* rng, |
wolfSSL | 15:117db924cf7c | 10549 | const byte* ntruKey, word16 ntruSz, |
wolfSSL | 15:117db924cf7c | 10550 | ed25519_key* ed25519Key) |
wolfSSL | 15:117db924cf7c | 10551 | { |
wolfSSL | 15:117db924cf7c | 10552 | int ret; |
wolfSSL | 15:117db924cf7c | 10553 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10554 | DerCert* der; |
wolfSSL | 15:117db924cf7c | 10555 | #else |
wolfSSL | 15:117db924cf7c | 10556 | DerCert der[1]; |
wolfSSL | 15:117db924cf7c | 10557 | #endif |
wolfSSL | 15:117db924cf7c | 10558 | |
wolfSSL | 15:117db924cf7c | 10559 | cert->keyType = eccKey ? ECC_KEY : (rsaKey ? RSA_KEY : |
wolfSSL | 15:117db924cf7c | 10560 | (ed25519Key ? ED25519_KEY : NTRU_KEY)); |
wolfSSL | 15:117db924cf7c | 10561 | |
wolfSSL | 15:117db924cf7c | 10562 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10563 | der = (DerCert*)XMALLOC(sizeof(DerCert), cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10564 | if (der == NULL) |
wolfSSL | 15:117db924cf7c | 10565 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 10566 | #endif |
wolfSSL | 15:117db924cf7c | 10567 | |
wolfSSL | 15:117db924cf7c | 10568 | ret = EncodeCert(cert, der, rsaKey, eccKey, rng, ntruKey, ntruSz, |
wolfSSL | 15:117db924cf7c | 10569 | ed25519Key); |
wolfSSL | 15:117db924cf7c | 10570 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 10571 | if (der->total + MAX_SEQ_SZ * 2 > (int)derSz) |
wolfSSL | 15:117db924cf7c | 10572 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 10573 | else |
wolfSSL | 15:117db924cf7c | 10574 | ret = cert->bodySz = WriteCertBody(der, derBuffer); |
wolfSSL | 15:117db924cf7c | 10575 | } |
wolfSSL | 15:117db924cf7c | 10576 | |
wolfSSL | 15:117db924cf7c | 10577 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10578 | XFREE(der, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10579 | #endif |
wolfSSL | 15:117db924cf7c | 10580 | |
wolfSSL | 15:117db924cf7c | 10581 | return ret; |
wolfSSL | 15:117db924cf7c | 10582 | } |
wolfSSL | 15:117db924cf7c | 10583 | |
wolfSSL | 15:117db924cf7c | 10584 | |
wolfSSL | 15:117db924cf7c | 10585 | /* Make an x509 Certificate v3 RSA or ECC from cert input, write to buffer */ |
wolfSSL | 15:117db924cf7c | 10586 | int wc_MakeCert_ex(Cert* cert, byte* derBuffer, word32 derSz, int keyType, |
wolfSSL | 15:117db924cf7c | 10587 | void* key, WC_RNG* rng) |
wolfSSL | 15:117db924cf7c | 10588 | { |
wolfSSL | 15:117db924cf7c | 10589 | RsaKey* rsaKey = NULL; |
wolfSSL | 15:117db924cf7c | 10590 | ecc_key* eccKey = NULL; |
wolfSSL | 15:117db924cf7c | 10591 | ed25519_key* ed25519Key = NULL; |
wolfSSL | 15:117db924cf7c | 10592 | |
wolfSSL | 15:117db924cf7c | 10593 | if (keyType == RSA_TYPE) |
wolfSSL | 15:117db924cf7c | 10594 | rsaKey = (RsaKey*)key; |
wolfSSL | 15:117db924cf7c | 10595 | else if (keyType == ECC_TYPE) |
wolfSSL | 15:117db924cf7c | 10596 | eccKey = (ecc_key*)key; |
wolfSSL | 15:117db924cf7c | 10597 | else if (keyType == ED25519_TYPE) |
wolfSSL | 15:117db924cf7c | 10598 | ed25519Key = (ed25519_key*)key; |
wolfSSL | 15:117db924cf7c | 10599 | |
wolfSSL | 15:117db924cf7c | 10600 | return MakeAnyCert(cert, derBuffer, derSz, rsaKey, eccKey, rng, NULL, 0, |
wolfSSL | 15:117db924cf7c | 10601 | ed25519Key); |
wolfSSL | 15:117db924cf7c | 10602 | } |
wolfSSL | 15:117db924cf7c | 10603 | /* Make an x509 Certificate v3 RSA or ECC from cert input, write to buffer */ |
wolfSSL | 15:117db924cf7c | 10604 | int wc_MakeCert(Cert* cert, byte* derBuffer, word32 derSz, RsaKey* rsaKey, |
wolfSSL | 15:117db924cf7c | 10605 | ecc_key* eccKey, WC_RNG* rng) |
wolfSSL | 15:117db924cf7c | 10606 | { |
wolfSSL | 15:117db924cf7c | 10607 | return MakeAnyCert(cert, derBuffer, derSz, rsaKey, eccKey, rng, NULL, 0, |
wolfSSL | 15:117db924cf7c | 10608 | NULL); |
wolfSSL | 15:117db924cf7c | 10609 | } |
wolfSSL | 15:117db924cf7c | 10610 | |
wolfSSL | 15:117db924cf7c | 10611 | |
wolfSSL | 15:117db924cf7c | 10612 | #ifdef HAVE_NTRU |
wolfSSL | 15:117db924cf7c | 10613 | |
wolfSSL | 15:117db924cf7c | 10614 | int wc_MakeNtruCert(Cert* cert, byte* derBuffer, word32 derSz, |
wolfSSL | 15:117db924cf7c | 10615 | const byte* ntruKey, word16 keySz, WC_RNG* rng) |
wolfSSL | 15:117db924cf7c | 10616 | { |
wolfSSL | 15:117db924cf7c | 10617 | return MakeAnyCert(cert, derBuffer, derSz, NULL, NULL, rng, ntruKey, keySz, NULL); |
wolfSSL | 15:117db924cf7c | 10618 | } |
wolfSSL | 15:117db924cf7c | 10619 | |
wolfSSL | 15:117db924cf7c | 10620 | #endif /* HAVE_NTRU */ |
wolfSSL | 15:117db924cf7c | 10621 | |
wolfSSL | 15:117db924cf7c | 10622 | |
wolfSSL | 15:117db924cf7c | 10623 | #ifdef WOLFSSL_CERT_REQ |
wolfSSL | 15:117db924cf7c | 10624 | |
wolfSSL | 15:117db924cf7c | 10625 | static int SetReqAttrib(byte* output, char* pw, int extSz) |
wolfSSL | 15:117db924cf7c | 10626 | { |
wolfSSL | 15:117db924cf7c | 10627 | static const byte cpOid[] = |
wolfSSL | 15:117db924cf7c | 10628 | { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, |
wolfSSL | 15:117db924cf7c | 10629 | 0x09, 0x07 }; |
wolfSSL | 15:117db924cf7c | 10630 | static const byte erOid[] = |
wolfSSL | 15:117db924cf7c | 10631 | { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, |
wolfSSL | 15:117db924cf7c | 10632 | 0x09, 0x0e }; |
wolfSSL | 15:117db924cf7c | 10633 | |
wolfSSL | 15:117db924cf7c | 10634 | int sz = 0; /* overall size */ |
wolfSSL | 15:117db924cf7c | 10635 | int cpSz = 0; /* Challenge Password section size */ |
wolfSSL | 15:117db924cf7c | 10636 | int cpSeqSz = 0; |
wolfSSL | 15:117db924cf7c | 10637 | int cpSetSz = 0; |
wolfSSL | 15:117db924cf7c | 10638 | int cpStrSz = 0; |
wolfSSL | 15:117db924cf7c | 10639 | int pwSz = 0; |
wolfSSL | 15:117db924cf7c | 10640 | int erSz = 0; /* Extension Request section size */ |
wolfSSL | 15:117db924cf7c | 10641 | int erSeqSz = 0; |
wolfSSL | 15:117db924cf7c | 10642 | int erSetSz = 0; |
wolfSSL | 15:117db924cf7c | 10643 | byte cpSeq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 10644 | byte cpSet[MAX_SET_SZ]; |
wolfSSL | 15:117db924cf7c | 10645 | byte cpStr[MAX_PRSTR_SZ]; |
wolfSSL | 15:117db924cf7c | 10646 | byte erSeq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 10647 | byte erSet[MAX_SET_SZ]; |
wolfSSL | 15:117db924cf7c | 10648 | |
wolfSSL | 15:117db924cf7c | 10649 | output[0] = 0xa0; |
wolfSSL | 15:117db924cf7c | 10650 | sz++; |
wolfSSL | 15:117db924cf7c | 10651 | |
wolfSSL | 15:117db924cf7c | 10652 | if (pw && pw[0]) { |
wolfSSL | 15:117db924cf7c | 10653 | pwSz = (int)XSTRLEN(pw); |
wolfSSL | 15:117db924cf7c | 10654 | cpStrSz = SetUTF8String(pwSz, cpStr); |
wolfSSL | 15:117db924cf7c | 10655 | cpSetSz = SetSet(cpStrSz + pwSz, cpSet); |
wolfSSL | 15:117db924cf7c | 10656 | cpSeqSz = SetSequence(sizeof(cpOid) + cpSetSz + cpStrSz + pwSz, cpSeq); |
wolfSSL | 15:117db924cf7c | 10657 | cpSz = cpSeqSz + sizeof(cpOid) + cpSetSz + cpStrSz + pwSz; |
wolfSSL | 15:117db924cf7c | 10658 | } |
wolfSSL | 15:117db924cf7c | 10659 | |
wolfSSL | 15:117db924cf7c | 10660 | if (extSz) { |
wolfSSL | 15:117db924cf7c | 10661 | erSetSz = SetSet(extSz, erSet); |
wolfSSL | 15:117db924cf7c | 10662 | erSeqSz = SetSequence(erSetSz + sizeof(erOid) + extSz, erSeq); |
wolfSSL | 15:117db924cf7c | 10663 | erSz = extSz + erSetSz + erSeqSz + sizeof(erOid); |
wolfSSL | 15:117db924cf7c | 10664 | } |
wolfSSL | 15:117db924cf7c | 10665 | |
wolfSSL | 15:117db924cf7c | 10666 | /* Put the pieces together. */ |
wolfSSL | 15:117db924cf7c | 10667 | sz += SetLength(cpSz + erSz, &output[sz]); |
wolfSSL | 15:117db924cf7c | 10668 | |
wolfSSL | 15:117db924cf7c | 10669 | if (cpSz) { |
wolfSSL | 15:117db924cf7c | 10670 | XMEMCPY(&output[sz], cpSeq, cpSeqSz); |
wolfSSL | 15:117db924cf7c | 10671 | sz += cpSeqSz; |
wolfSSL | 15:117db924cf7c | 10672 | XMEMCPY(&output[sz], cpOid, sizeof(cpOid)); |
wolfSSL | 15:117db924cf7c | 10673 | sz += sizeof(cpOid); |
wolfSSL | 15:117db924cf7c | 10674 | XMEMCPY(&output[sz], cpSet, cpSetSz); |
wolfSSL | 15:117db924cf7c | 10675 | sz += cpSetSz; |
wolfSSL | 15:117db924cf7c | 10676 | XMEMCPY(&output[sz], cpStr, cpStrSz); |
wolfSSL | 15:117db924cf7c | 10677 | sz += cpStrSz; |
wolfSSL | 15:117db924cf7c | 10678 | XMEMCPY(&output[sz], pw, pwSz); |
wolfSSL | 15:117db924cf7c | 10679 | sz += pwSz; |
wolfSSL | 15:117db924cf7c | 10680 | } |
wolfSSL | 15:117db924cf7c | 10681 | |
wolfSSL | 15:117db924cf7c | 10682 | if (erSz) { |
wolfSSL | 15:117db924cf7c | 10683 | XMEMCPY(&output[sz], erSeq, erSeqSz); |
wolfSSL | 15:117db924cf7c | 10684 | sz += erSeqSz; |
wolfSSL | 15:117db924cf7c | 10685 | XMEMCPY(&output[sz], erOid, sizeof(erOid)); |
wolfSSL | 15:117db924cf7c | 10686 | sz += sizeof(erOid); |
wolfSSL | 15:117db924cf7c | 10687 | XMEMCPY(&output[sz], erSet, erSetSz); |
wolfSSL | 15:117db924cf7c | 10688 | sz += erSetSz; |
wolfSSL | 15:117db924cf7c | 10689 | /* The actual extension data will be tacked onto the output later. */ |
wolfSSL | 15:117db924cf7c | 10690 | } |
wolfSSL | 15:117db924cf7c | 10691 | |
wolfSSL | 15:117db924cf7c | 10692 | return sz; |
wolfSSL | 15:117db924cf7c | 10693 | } |
wolfSSL | 15:117db924cf7c | 10694 | |
wolfSSL | 15:117db924cf7c | 10695 | |
wolfSSL | 15:117db924cf7c | 10696 | /* encode info from cert into DER encoded format */ |
wolfSSL | 15:117db924cf7c | 10697 | static int EncodeCertReq(Cert* cert, DerCert* der, RsaKey* rsaKey, |
wolfSSL | 15:117db924cf7c | 10698 | ecc_key* eccKey, ed25519_key* ed25519Key) |
wolfSSL | 15:117db924cf7c | 10699 | { |
wolfSSL | 15:117db924cf7c | 10700 | (void)eccKey; |
wolfSSL | 15:117db924cf7c | 10701 | (void)ed25519Key; |
wolfSSL | 15:117db924cf7c | 10702 | |
wolfSSL | 15:117db924cf7c | 10703 | if (cert == NULL || der == NULL) |
wolfSSL | 15:117db924cf7c | 10704 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 10705 | |
wolfSSL | 15:117db924cf7c | 10706 | if (rsaKey == NULL && eccKey == NULL && ed25519Key == NULL) |
wolfSSL | 15:117db924cf7c | 10707 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10708 | |
wolfSSL | 15:117db924cf7c | 10709 | /* init */ |
wolfSSL | 15:117db924cf7c | 10710 | XMEMSET(der, 0, sizeof(DerCert)); |
wolfSSL | 15:117db924cf7c | 10711 | |
wolfSSL | 15:117db924cf7c | 10712 | /* version */ |
wolfSSL | 15:117db924cf7c | 10713 | der->versionSz = SetMyVersion(cert->version, der->version, FALSE); |
wolfSSL | 15:117db924cf7c | 10714 | |
wolfSSL | 15:117db924cf7c | 10715 | /* subject name */ |
wolfSSL | 15:117db924cf7c | 10716 | der->subjectSz = SetName(der->subject, sizeof(der->subject), &cert->subject); |
wolfSSL | 15:117db924cf7c | 10717 | if (der->subjectSz <= 0) |
wolfSSL | 15:117db924cf7c | 10718 | return SUBJECT_E; |
wolfSSL | 15:117db924cf7c | 10719 | |
wolfSSL | 15:117db924cf7c | 10720 | /* public key */ |
wolfSSL | 15:117db924cf7c | 10721 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 10722 | if (cert->keyType == RSA_KEY) { |
wolfSSL | 15:117db924cf7c | 10723 | if (rsaKey == NULL) |
wolfSSL | 15:117db924cf7c | 10724 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10725 | der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey, |
wolfSSL | 15:117db924cf7c | 10726 | sizeof(der->publicKey), 1); |
wolfSSL | 15:117db924cf7c | 10727 | } |
wolfSSL | 15:117db924cf7c | 10728 | #endif |
wolfSSL | 15:117db924cf7c | 10729 | |
wolfSSL | 15:117db924cf7c | 10730 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 10731 | if (cert->keyType == ECC_KEY) { |
wolfSSL | 15:117db924cf7c | 10732 | der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey, 1); |
wolfSSL | 15:117db924cf7c | 10733 | } |
wolfSSL | 15:117db924cf7c | 10734 | #endif |
wolfSSL | 15:117db924cf7c | 10735 | |
wolfSSL | 15:117db924cf7c | 10736 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 10737 | if (cert->keyType == ED25519_KEY) { |
wolfSSL | 15:117db924cf7c | 10738 | if (ed25519Key == NULL) |
wolfSSL | 15:117db924cf7c | 10739 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10740 | der->publicKeySz = SetEd25519PublicKey(der->publicKey, ed25519Key, 1); |
wolfSSL | 15:117db924cf7c | 10741 | } |
wolfSSL | 15:117db924cf7c | 10742 | #endif |
wolfSSL | 15:117db924cf7c | 10743 | |
wolfSSL | 15:117db924cf7c | 10744 | if (der->publicKeySz <= 0) |
wolfSSL | 15:117db924cf7c | 10745 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 10746 | |
wolfSSL | 15:117db924cf7c | 10747 | /* set the extensions */ |
wolfSSL | 15:117db924cf7c | 10748 | der->extensionsSz = 0; |
wolfSSL | 15:117db924cf7c | 10749 | |
wolfSSL | 15:117db924cf7c | 10750 | /* CA */ |
wolfSSL | 15:117db924cf7c | 10751 | if (cert->isCA) { |
wolfSSL | 15:117db924cf7c | 10752 | der->caSz = SetCa(der->ca, sizeof(der->ca)); |
wolfSSL | 15:117db924cf7c | 10753 | if (der->caSz <= 0) |
wolfSSL | 15:117db924cf7c | 10754 | return CA_TRUE_E; |
wolfSSL | 15:117db924cf7c | 10755 | |
wolfSSL | 15:117db924cf7c | 10756 | der->extensionsSz += der->caSz; |
wolfSSL | 15:117db924cf7c | 10757 | } |
wolfSSL | 15:117db924cf7c | 10758 | else |
wolfSSL | 15:117db924cf7c | 10759 | der->caSz = 0; |
wolfSSL | 15:117db924cf7c | 10760 | |
wolfSSL | 15:117db924cf7c | 10761 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 10762 | /* SKID */ |
wolfSSL | 15:117db924cf7c | 10763 | if (cert->skidSz) { |
wolfSSL | 15:117db924cf7c | 10764 | /* check the provided SKID size */ |
wolfSSL | 15:117db924cf7c | 10765 | if (cert->skidSz > (int)min(CTC_MAX_SKID_SIZE, sizeof(der->skid))) |
wolfSSL | 15:117db924cf7c | 10766 | return SKID_E; |
wolfSSL | 15:117db924cf7c | 10767 | |
wolfSSL | 15:117db924cf7c | 10768 | der->skidSz = SetSKID(der->skid, sizeof(der->skid), |
wolfSSL | 15:117db924cf7c | 10769 | cert->skid, cert->skidSz); |
wolfSSL | 15:117db924cf7c | 10770 | if (der->skidSz <= 0) |
wolfSSL | 15:117db924cf7c | 10771 | return SKID_E; |
wolfSSL | 15:117db924cf7c | 10772 | |
wolfSSL | 15:117db924cf7c | 10773 | der->extensionsSz += der->skidSz; |
wolfSSL | 15:117db924cf7c | 10774 | } |
wolfSSL | 15:117db924cf7c | 10775 | else |
wolfSSL | 15:117db924cf7c | 10776 | der->skidSz = 0; |
wolfSSL | 15:117db924cf7c | 10777 | |
wolfSSL | 15:117db924cf7c | 10778 | /* Key Usage */ |
wolfSSL | 15:117db924cf7c | 10779 | if (cert->keyUsage != 0){ |
wolfSSL | 15:117db924cf7c | 10780 | der->keyUsageSz = SetKeyUsage(der->keyUsage, sizeof(der->keyUsage), |
wolfSSL | 15:117db924cf7c | 10781 | cert->keyUsage); |
wolfSSL | 15:117db924cf7c | 10782 | if (der->keyUsageSz <= 0) |
wolfSSL | 15:117db924cf7c | 10783 | return KEYUSAGE_E; |
wolfSSL | 15:117db924cf7c | 10784 | |
wolfSSL | 15:117db924cf7c | 10785 | der->extensionsSz += der->keyUsageSz; |
wolfSSL | 15:117db924cf7c | 10786 | } |
wolfSSL | 15:117db924cf7c | 10787 | else |
wolfSSL | 15:117db924cf7c | 10788 | der->keyUsageSz = 0; |
wolfSSL | 15:117db924cf7c | 10789 | |
wolfSSL | 15:117db924cf7c | 10790 | /* Extended Key Usage */ |
wolfSSL | 15:117db924cf7c | 10791 | if (cert->extKeyUsage != 0){ |
wolfSSL | 15:117db924cf7c | 10792 | der->extKeyUsageSz = SetExtKeyUsage(cert, der->extKeyUsage, |
wolfSSL | 15:117db924cf7c | 10793 | sizeof(der->extKeyUsage), cert->extKeyUsage); |
wolfSSL | 15:117db924cf7c | 10794 | if (der->extKeyUsageSz <= 0) |
wolfSSL | 15:117db924cf7c | 10795 | return EXTKEYUSAGE_E; |
wolfSSL | 15:117db924cf7c | 10796 | |
wolfSSL | 15:117db924cf7c | 10797 | der->extensionsSz += der->extKeyUsageSz; |
wolfSSL | 15:117db924cf7c | 10798 | } |
wolfSSL | 15:117db924cf7c | 10799 | else |
wolfSSL | 15:117db924cf7c | 10800 | der->extKeyUsageSz = 0; |
wolfSSL | 15:117db924cf7c | 10801 | |
wolfSSL | 15:117db924cf7c | 10802 | #endif /* WOLFSSL_CERT_EXT */ |
wolfSSL | 15:117db924cf7c | 10803 | |
wolfSSL | 15:117db924cf7c | 10804 | /* put extensions */ |
wolfSSL | 15:117db924cf7c | 10805 | if (der->extensionsSz > 0) { |
wolfSSL | 15:117db924cf7c | 10806 | int ret; |
wolfSSL | 15:117db924cf7c | 10807 | |
wolfSSL | 15:117db924cf7c | 10808 | /* put the start of sequence (ID, Size) */ |
wolfSSL | 15:117db924cf7c | 10809 | der->extensionsSz = SetSequence(der->extensionsSz, der->extensions); |
wolfSSL | 15:117db924cf7c | 10810 | if (der->extensionsSz <= 0) |
wolfSSL | 15:117db924cf7c | 10811 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10812 | |
wolfSSL | 15:117db924cf7c | 10813 | /* put CA */ |
wolfSSL | 15:117db924cf7c | 10814 | if (der->caSz) { |
wolfSSL | 15:117db924cf7c | 10815 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10816 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10817 | der->ca, der->caSz); |
wolfSSL | 15:117db924cf7c | 10818 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10819 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10820 | } |
wolfSSL | 15:117db924cf7c | 10821 | |
wolfSSL | 15:117db924cf7c | 10822 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 10823 | /* put SKID */ |
wolfSSL | 15:117db924cf7c | 10824 | if (der->skidSz) { |
wolfSSL | 15:117db924cf7c | 10825 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10826 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10827 | der->skid, der->skidSz); |
wolfSSL | 15:117db924cf7c | 10828 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10829 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10830 | } |
wolfSSL | 15:117db924cf7c | 10831 | |
wolfSSL | 15:117db924cf7c | 10832 | /* put AKID */ |
wolfSSL | 15:117db924cf7c | 10833 | if (der->akidSz) { |
wolfSSL | 15:117db924cf7c | 10834 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10835 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10836 | der->akid, der->akidSz); |
wolfSSL | 15:117db924cf7c | 10837 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10838 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10839 | } |
wolfSSL | 15:117db924cf7c | 10840 | |
wolfSSL | 15:117db924cf7c | 10841 | /* put KeyUsage */ |
wolfSSL | 15:117db924cf7c | 10842 | if (der->keyUsageSz) { |
wolfSSL | 15:117db924cf7c | 10843 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10844 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10845 | der->keyUsage, der->keyUsageSz); |
wolfSSL | 15:117db924cf7c | 10846 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10847 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10848 | } |
wolfSSL | 15:117db924cf7c | 10849 | |
wolfSSL | 15:117db924cf7c | 10850 | /* put ExtendedKeyUsage */ |
wolfSSL | 15:117db924cf7c | 10851 | if (der->extKeyUsageSz) { |
wolfSSL | 15:117db924cf7c | 10852 | ret = SetExtensions(der->extensions, sizeof(der->extensions), |
wolfSSL | 15:117db924cf7c | 10853 | &der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10854 | der->extKeyUsage, der->extKeyUsageSz); |
wolfSSL | 15:117db924cf7c | 10855 | if (ret <= 0) |
wolfSSL | 15:117db924cf7c | 10856 | return EXTENSIONS_E; |
wolfSSL | 15:117db924cf7c | 10857 | } |
wolfSSL | 15:117db924cf7c | 10858 | |
wolfSSL | 15:117db924cf7c | 10859 | #endif /* WOLFSSL_CERT_EXT */ |
wolfSSL | 15:117db924cf7c | 10860 | } |
wolfSSL | 15:117db924cf7c | 10861 | |
wolfSSL | 15:117db924cf7c | 10862 | der->attribSz = SetReqAttrib(der->attrib, |
wolfSSL | 15:117db924cf7c | 10863 | cert->challengePw, der->extensionsSz); |
wolfSSL | 15:117db924cf7c | 10864 | if (der->attribSz <= 0) |
wolfSSL | 15:117db924cf7c | 10865 | return REQ_ATTRIBUTE_E; |
wolfSSL | 15:117db924cf7c | 10866 | |
wolfSSL | 15:117db924cf7c | 10867 | der->total = der->versionSz + der->subjectSz + der->publicKeySz + |
wolfSSL | 15:117db924cf7c | 10868 | der->extensionsSz + der->attribSz; |
wolfSSL | 15:117db924cf7c | 10869 | |
wolfSSL | 15:117db924cf7c | 10870 | return 0; |
wolfSSL | 15:117db924cf7c | 10871 | } |
wolfSSL | 15:117db924cf7c | 10872 | |
wolfSSL | 15:117db924cf7c | 10873 | |
wolfSSL | 15:117db924cf7c | 10874 | /* write DER encoded cert req to buffer, size already checked */ |
wolfSSL | 15:117db924cf7c | 10875 | static int WriteCertReqBody(DerCert* der, byte* buffer) |
wolfSSL | 15:117db924cf7c | 10876 | { |
wolfSSL | 15:117db924cf7c | 10877 | int idx; |
wolfSSL | 15:117db924cf7c | 10878 | |
wolfSSL | 15:117db924cf7c | 10879 | /* signed part header */ |
wolfSSL | 15:117db924cf7c | 10880 | idx = SetSequence(der->total, buffer); |
wolfSSL | 15:117db924cf7c | 10881 | /* version */ |
wolfSSL | 15:117db924cf7c | 10882 | XMEMCPY(buffer + idx, der->version, der->versionSz); |
wolfSSL | 15:117db924cf7c | 10883 | idx += der->versionSz; |
wolfSSL | 15:117db924cf7c | 10884 | /* subject */ |
wolfSSL | 15:117db924cf7c | 10885 | XMEMCPY(buffer + idx, der->subject, der->subjectSz); |
wolfSSL | 15:117db924cf7c | 10886 | idx += der->subjectSz; |
wolfSSL | 15:117db924cf7c | 10887 | /* public key */ |
wolfSSL | 15:117db924cf7c | 10888 | XMEMCPY(buffer + idx, der->publicKey, der->publicKeySz); |
wolfSSL | 15:117db924cf7c | 10889 | idx += der->publicKeySz; |
wolfSSL | 15:117db924cf7c | 10890 | /* attributes */ |
wolfSSL | 15:117db924cf7c | 10891 | XMEMCPY(buffer + idx, der->attrib, der->attribSz); |
wolfSSL | 15:117db924cf7c | 10892 | idx += der->attribSz; |
wolfSSL | 15:117db924cf7c | 10893 | /* extensions */ |
wolfSSL | 15:117db924cf7c | 10894 | if (der->extensionsSz) { |
wolfSSL | 15:117db924cf7c | 10895 | XMEMCPY(buffer + idx, der->extensions, min(der->extensionsSz, |
wolfSSL | 15:117db924cf7c | 10896 | (int)sizeof(der->extensions))); |
wolfSSL | 15:117db924cf7c | 10897 | idx += der->extensionsSz; |
wolfSSL | 15:117db924cf7c | 10898 | } |
wolfSSL | 15:117db924cf7c | 10899 | |
wolfSSL | 15:117db924cf7c | 10900 | return idx; |
wolfSSL | 15:117db924cf7c | 10901 | } |
wolfSSL | 15:117db924cf7c | 10902 | |
wolfSSL | 15:117db924cf7c | 10903 | |
wolfSSL | 15:117db924cf7c | 10904 | static int MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz, |
wolfSSL | 15:117db924cf7c | 10905 | RsaKey* rsaKey, ecc_key* eccKey, ed25519_key* ed25519Key) |
wolfSSL | 15:117db924cf7c | 10906 | { |
wolfSSL | 15:117db924cf7c | 10907 | int ret; |
wolfSSL | 15:117db924cf7c | 10908 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10909 | DerCert* der; |
wolfSSL | 15:117db924cf7c | 10910 | #else |
wolfSSL | 15:117db924cf7c | 10911 | DerCert der[1]; |
wolfSSL | 15:117db924cf7c | 10912 | #endif |
wolfSSL | 15:117db924cf7c | 10913 | |
wolfSSL | 15:117db924cf7c | 10914 | cert->keyType = eccKey ? ECC_KEY : (ed25519Key ? ED25519_KEY : RSA_KEY); |
wolfSSL | 15:117db924cf7c | 10915 | |
wolfSSL | 15:117db924cf7c | 10916 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10917 | der = (DerCert*)XMALLOC(sizeof(DerCert), cert->heap, |
wolfSSL | 15:117db924cf7c | 10918 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10919 | if (der == NULL) |
wolfSSL | 15:117db924cf7c | 10920 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 10921 | #endif |
wolfSSL | 15:117db924cf7c | 10922 | |
wolfSSL | 15:117db924cf7c | 10923 | ret = EncodeCertReq(cert, der, rsaKey, eccKey, ed25519Key); |
wolfSSL | 15:117db924cf7c | 10924 | |
wolfSSL | 15:117db924cf7c | 10925 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 10926 | if (der->total + MAX_SEQ_SZ * 2 > (int)derSz) |
wolfSSL | 15:117db924cf7c | 10927 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 10928 | else |
wolfSSL | 15:117db924cf7c | 10929 | ret = cert->bodySz = WriteCertReqBody(der, derBuffer); |
wolfSSL | 15:117db924cf7c | 10930 | } |
wolfSSL | 15:117db924cf7c | 10931 | |
wolfSSL | 15:117db924cf7c | 10932 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 10933 | XFREE(der, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 10934 | #endif |
wolfSSL | 15:117db924cf7c | 10935 | |
wolfSSL | 15:117db924cf7c | 10936 | return ret; |
wolfSSL | 15:117db924cf7c | 10937 | } |
wolfSSL | 15:117db924cf7c | 10938 | |
wolfSSL | 15:117db924cf7c | 10939 | int wc_MakeCertReq_ex(Cert* cert, byte* derBuffer, word32 derSz, int keyType, |
wolfSSL | 15:117db924cf7c | 10940 | void* key) |
wolfSSL | 15:117db924cf7c | 10941 | { |
wolfSSL | 15:117db924cf7c | 10942 | RsaKey* rsaKey = NULL; |
wolfSSL | 15:117db924cf7c | 10943 | ecc_key* eccKey = NULL; |
wolfSSL | 15:117db924cf7c | 10944 | ed25519_key* ed25519Key = NULL; |
wolfSSL | 15:117db924cf7c | 10945 | |
wolfSSL | 15:117db924cf7c | 10946 | if (keyType == RSA_TYPE) |
wolfSSL | 15:117db924cf7c | 10947 | rsaKey = (RsaKey*)key; |
wolfSSL | 15:117db924cf7c | 10948 | else if (keyType == ECC_TYPE) |
wolfSSL | 15:117db924cf7c | 10949 | eccKey = (ecc_key*)key; |
wolfSSL | 15:117db924cf7c | 10950 | else if (keyType == ED25519_TYPE) |
wolfSSL | 15:117db924cf7c | 10951 | ed25519Key = (ed25519_key*)key; |
wolfSSL | 15:117db924cf7c | 10952 | |
wolfSSL | 15:117db924cf7c | 10953 | return MakeCertReq(cert, derBuffer, derSz, rsaKey, eccKey, ed25519Key); |
wolfSSL | 15:117db924cf7c | 10954 | } |
wolfSSL | 15:117db924cf7c | 10955 | |
wolfSSL | 15:117db924cf7c | 10956 | int wc_MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz, |
wolfSSL | 15:117db924cf7c | 10957 | RsaKey* rsaKey, ecc_key* eccKey) |
wolfSSL | 15:117db924cf7c | 10958 | { |
wolfSSL | 15:117db924cf7c | 10959 | return MakeCertReq(cert, derBuffer, derSz, rsaKey, eccKey, NULL); |
wolfSSL | 15:117db924cf7c | 10960 | } |
wolfSSL | 15:117db924cf7c | 10961 | #endif /* WOLFSSL_CERT_REQ */ |
wolfSSL | 15:117db924cf7c | 10962 | |
wolfSSL | 15:117db924cf7c | 10963 | |
wolfSSL | 15:117db924cf7c | 10964 | static int SignCert(int requestSz, int sType, byte* buffer, word32 buffSz, |
wolfSSL | 15:117db924cf7c | 10965 | RsaKey* rsaKey, ecc_key* eccKey, ed25519_key* ed25519Key, |
wolfSSL | 15:117db924cf7c | 10966 | WC_RNG* rng) |
wolfSSL | 15:117db924cf7c | 10967 | { |
wolfSSL | 15:117db924cf7c | 10968 | int sigSz = 0; |
wolfSSL | 15:117db924cf7c | 10969 | void* heap = NULL; |
wolfSSL | 15:117db924cf7c | 10970 | CertSignCtx* certSignCtx = NULL; |
wolfSSL | 15:117db924cf7c | 10971 | #ifndef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 10972 | CertSignCtx certSignCtx_lcl; |
wolfSSL | 15:117db924cf7c | 10973 | certSignCtx = &certSignCtx_lcl; |
wolfSSL | 15:117db924cf7c | 10974 | XMEMSET(certSignCtx, 0, sizeof(CertSignCtx)); |
wolfSSL | 15:117db924cf7c | 10975 | #endif |
wolfSSL | 15:117db924cf7c | 10976 | |
wolfSSL | 15:117db924cf7c | 10977 | if (requestSz < 0) |
wolfSSL | 15:117db924cf7c | 10978 | return requestSz; |
wolfSSL | 15:117db924cf7c | 10979 | |
wolfSSL | 15:117db924cf7c | 10980 | /* locate ctx */ |
wolfSSL | 15:117db924cf7c | 10981 | if (rsaKey) { |
wolfSSL | 15:117db924cf7c | 10982 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 10983 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 10984 | certSignCtx = &rsaKey->certSignCtx; |
wolfSSL | 15:117db924cf7c | 10985 | #endif |
wolfSSL | 15:117db924cf7c | 10986 | heap = rsaKey->heap; |
wolfSSL | 15:117db924cf7c | 10987 | #else |
wolfSSL | 15:117db924cf7c | 10988 | return NOT_COMPILED_IN; |
wolfSSL | 15:117db924cf7c | 10989 | #endif /* NO_RSA */ |
wolfSSL | 15:117db924cf7c | 10990 | } |
wolfSSL | 15:117db924cf7c | 10991 | else if (eccKey) { |
wolfSSL | 15:117db924cf7c | 10992 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 10993 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 10994 | certSignCtx = &eccKey->certSignCtx; |
wolfSSL | 15:117db924cf7c | 10995 | #endif |
wolfSSL | 15:117db924cf7c | 10996 | heap = eccKey->heap; |
wolfSSL | 15:117db924cf7c | 10997 | #else |
wolfSSL | 15:117db924cf7c | 10998 | return NOT_COMPILED_IN; |
wolfSSL | 15:117db924cf7c | 10999 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 11000 | } |
wolfSSL | 15:117db924cf7c | 11001 | |
wolfSSL | 15:117db924cf7c | 11002 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 15:117db924cf7c | 11003 | if (certSignCtx == NULL) { |
wolfSSL | 15:117db924cf7c | 11004 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11005 | } |
wolfSSL | 15:117db924cf7c | 11006 | #endif |
wolfSSL | 15:117db924cf7c | 11007 | |
wolfSSL | 15:117db924cf7c | 11008 | if (certSignCtx->sig == NULL) { |
wolfSSL | 15:117db924cf7c | 11009 | certSignCtx->sig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, heap, |
wolfSSL | 15:117db924cf7c | 11010 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11011 | if (certSignCtx->sig == NULL) |
wolfSSL | 15:117db924cf7c | 11012 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11013 | } |
wolfSSL | 15:117db924cf7c | 11014 | |
wolfSSL | 15:117db924cf7c | 11015 | sigSz = MakeSignature(certSignCtx, buffer, requestSz, certSignCtx->sig, |
wolfSSL | 15:117db924cf7c | 11016 | MAX_ENCODED_SIG_SZ, rsaKey, eccKey, ed25519Key, rng, sType, heap); |
wolfSSL | 15:117db924cf7c | 11017 | if (sigSz == WC_PENDING_E) { |
wolfSSL | 15:117db924cf7c | 11018 | /* Not free'ing certSignCtx->sig here because it could still be in use |
wolfSSL | 15:117db924cf7c | 11019 | * with async operations. */ |
wolfSSL | 15:117db924cf7c | 11020 | return sigSz; |
wolfSSL | 15:117db924cf7c | 11021 | } |
wolfSSL | 15:117db924cf7c | 11022 | |
wolfSSL | 15:117db924cf7c | 11023 | if (sigSz >= 0) { |
wolfSSL | 15:117db924cf7c | 11024 | if (requestSz + MAX_SEQ_SZ * 2 + sigSz > (int)buffSz) |
wolfSSL | 15:117db924cf7c | 11025 | sigSz = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 11026 | else |
wolfSSL | 15:117db924cf7c | 11027 | sigSz = AddSignature(buffer, requestSz, certSignCtx->sig, sigSz, |
wolfSSL | 15:117db924cf7c | 11028 | sType); |
wolfSSL | 15:117db924cf7c | 11029 | } |
wolfSSL | 15:117db924cf7c | 11030 | |
wolfSSL | 15:117db924cf7c | 11031 | XFREE(certSignCtx->sig, heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11032 | certSignCtx->sig = NULL; |
wolfSSL | 15:117db924cf7c | 11033 | |
wolfSSL | 15:117db924cf7c | 11034 | return sigSz; |
wolfSSL | 15:117db924cf7c | 11035 | } |
wolfSSL | 15:117db924cf7c | 11036 | |
wolfSSL | 15:117db924cf7c | 11037 | int wc_SignCert_ex(int requestSz, int sType, byte* buffer, word32 buffSz, |
wolfSSL | 15:117db924cf7c | 11038 | int keyType, void* key, WC_RNG* rng) |
wolfSSL | 15:117db924cf7c | 11039 | { |
wolfSSL | 15:117db924cf7c | 11040 | RsaKey* rsaKey = NULL; |
wolfSSL | 15:117db924cf7c | 11041 | ecc_key* eccKey = NULL; |
wolfSSL | 15:117db924cf7c | 11042 | ed25519_key* ed25519Key = NULL; |
wolfSSL | 15:117db924cf7c | 11043 | |
wolfSSL | 15:117db924cf7c | 11044 | if (keyType == RSA_TYPE) |
wolfSSL | 15:117db924cf7c | 11045 | rsaKey = (RsaKey*)key; |
wolfSSL | 15:117db924cf7c | 11046 | else if (keyType == ECC_TYPE) |
wolfSSL | 15:117db924cf7c | 11047 | eccKey = (ecc_key*)key; |
wolfSSL | 15:117db924cf7c | 11048 | else if (keyType == ED25519_TYPE) |
wolfSSL | 15:117db924cf7c | 11049 | ed25519Key = (ed25519_key*)key; |
wolfSSL | 15:117db924cf7c | 11050 | |
wolfSSL | 15:117db924cf7c | 11051 | return SignCert(requestSz, sType, buffer, buffSz, rsaKey, eccKey, |
wolfSSL | 15:117db924cf7c | 11052 | ed25519Key, rng); |
wolfSSL | 15:117db924cf7c | 11053 | } |
wolfSSL | 15:117db924cf7c | 11054 | |
wolfSSL | 15:117db924cf7c | 11055 | int wc_SignCert(int requestSz, int sType, byte* buffer, word32 buffSz, |
wolfSSL | 15:117db924cf7c | 11056 | RsaKey* rsaKey, ecc_key* eccKey, WC_RNG* rng) |
wolfSSL | 15:117db924cf7c | 11057 | { |
wolfSSL | 15:117db924cf7c | 11058 | return SignCert(requestSz, sType, buffer, buffSz, rsaKey, eccKey, NULL, |
wolfSSL | 15:117db924cf7c | 11059 | rng); |
wolfSSL | 15:117db924cf7c | 11060 | } |
wolfSSL | 15:117db924cf7c | 11061 | |
wolfSSL | 15:117db924cf7c | 11062 | int wc_MakeSelfCert(Cert* cert, byte* buffer, word32 buffSz, |
wolfSSL | 15:117db924cf7c | 11063 | RsaKey* key, WC_RNG* rng) |
wolfSSL | 15:117db924cf7c | 11064 | { |
wolfSSL | 15:117db924cf7c | 11065 | int ret; |
wolfSSL | 15:117db924cf7c | 11066 | |
wolfSSL | 15:117db924cf7c | 11067 | ret = wc_MakeCert(cert, buffer, buffSz, key, NULL, rng); |
wolfSSL | 15:117db924cf7c | 11068 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 11069 | return ret; |
wolfSSL | 15:117db924cf7c | 11070 | |
wolfSSL | 15:117db924cf7c | 11071 | return wc_SignCert(cert->bodySz, cert->sigType, |
wolfSSL | 15:117db924cf7c | 11072 | buffer, buffSz, key, NULL, rng); |
wolfSSL | 15:117db924cf7c | 11073 | } |
wolfSSL | 15:117db924cf7c | 11074 | |
wolfSSL | 15:117db924cf7c | 11075 | |
wolfSSL | 15:117db924cf7c | 11076 | #ifdef WOLFSSL_CERT_EXT |
wolfSSL | 15:117db924cf7c | 11077 | |
wolfSSL | 15:117db924cf7c | 11078 | /* Set KID from public key */ |
wolfSSL | 15:117db924cf7c | 11079 | static int SetKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey, ecc_key *eckey, |
wolfSSL | 15:117db924cf7c | 11080 | byte *ntruKey, word16 ntruKeySz, |
wolfSSL | 15:117db924cf7c | 11081 | ed25519_key* ed25519Key, int kid_type) |
wolfSSL | 15:117db924cf7c | 11082 | { |
wolfSSL | 15:117db924cf7c | 11083 | byte *buffer; |
wolfSSL | 15:117db924cf7c | 11084 | int bufferSz, ret; |
wolfSSL | 15:117db924cf7c | 11085 | |
wolfSSL | 15:117db924cf7c | 11086 | if (cert == NULL || |
wolfSSL | 15:117db924cf7c | 11087 | (rsakey == NULL && eckey == NULL && ntruKey == NULL && |
wolfSSL | 15:117db924cf7c | 11088 | ed25519Key == NULL) || |
wolfSSL | 15:117db924cf7c | 11089 | (kid_type != SKID_TYPE && kid_type != AKID_TYPE)) |
wolfSSL | 15:117db924cf7c | 11090 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11091 | |
wolfSSL | 15:117db924cf7c | 11092 | buffer = (byte *)XMALLOC(MAX_PUBLIC_KEY_SZ, cert->heap, |
wolfSSL | 15:117db924cf7c | 11093 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11094 | if (buffer == NULL) |
wolfSSL | 15:117db924cf7c | 11095 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11096 | |
wolfSSL | 15:117db924cf7c | 11097 | /* Public Key */ |
wolfSSL | 15:117db924cf7c | 11098 | bufferSz = -1; |
wolfSSL | 15:117db924cf7c | 11099 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 11100 | /* RSA public key */ |
wolfSSL | 15:117db924cf7c | 11101 | if (rsakey != NULL) |
wolfSSL | 15:117db924cf7c | 11102 | bufferSz = SetRsaPublicKey(buffer, rsakey, MAX_PUBLIC_KEY_SZ, 0); |
wolfSSL | 15:117db924cf7c | 11103 | #endif |
wolfSSL | 15:117db924cf7c | 11104 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 11105 | /* ECC public key */ |
wolfSSL | 15:117db924cf7c | 11106 | if (eckey != NULL) |
wolfSSL | 15:117db924cf7c | 11107 | bufferSz = SetEccPublicKey(buffer, eckey, 0); |
wolfSSL | 15:117db924cf7c | 11108 | #endif |
wolfSSL | 15:117db924cf7c | 11109 | #ifdef HAVE_NTRU |
wolfSSL | 15:117db924cf7c | 11110 | /* NTRU public key */ |
wolfSSL | 15:117db924cf7c | 11111 | if (ntruKey != NULL) { |
wolfSSL | 15:117db924cf7c | 11112 | bufferSz = MAX_PUBLIC_KEY_SZ; |
wolfSSL | 15:117db924cf7c | 11113 | ret = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo( |
wolfSSL | 15:117db924cf7c | 11114 | ntruKeySz, ntruKey, (word16 *)(&bufferSz), buffer); |
wolfSSL | 15:117db924cf7c | 11115 | if (ret != NTRU_OK) |
wolfSSL | 15:117db924cf7c | 11116 | bufferSz = -1; |
wolfSSL | 15:117db924cf7c | 11117 | } |
wolfSSL | 15:117db924cf7c | 11118 | #else |
wolfSSL | 15:117db924cf7c | 11119 | (void)ntruKeySz; |
wolfSSL | 15:117db924cf7c | 11120 | #endif |
wolfSSL | 15:117db924cf7c | 11121 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 11122 | /* ED25519 public key */ |
wolfSSL | 15:117db924cf7c | 11123 | if (ed25519Key != NULL) |
wolfSSL | 15:117db924cf7c | 11124 | bufferSz = SetEd25519PublicKey(buffer, ed25519Key, 0); |
wolfSSL | 15:117db924cf7c | 11125 | #endif |
wolfSSL | 15:117db924cf7c | 11126 | |
wolfSSL | 15:117db924cf7c | 11127 | if (bufferSz <= 0) { |
wolfSSL | 15:117db924cf7c | 11128 | XFREE(buffer, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11129 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 11130 | } |
wolfSSL | 15:117db924cf7c | 11131 | |
wolfSSL | 15:117db924cf7c | 11132 | /* Compute SKID by hashing public key */ |
wolfSSL | 15:117db924cf7c | 11133 | #ifdef NO_SHA |
wolfSSL | 15:117db924cf7c | 11134 | if (kid_type == SKID_TYPE) { |
wolfSSL | 15:117db924cf7c | 11135 | ret = wc_Sha256Hash(buffer, bufferSz, cert->skid); |
wolfSSL | 15:117db924cf7c | 11136 | cert->skidSz = WC_SHA256_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 11137 | } |
wolfSSL | 15:117db924cf7c | 11138 | else if (kid_type == AKID_TYPE) { |
wolfSSL | 15:117db924cf7c | 11139 | ret = wc_Sha256Hash(buffer, bufferSz, cert->akid); |
wolfSSL | 15:117db924cf7c | 11140 | cert->akidSz = WC_SHA256_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 11141 | } |
wolfSSL | 15:117db924cf7c | 11142 | else |
wolfSSL | 15:117db924cf7c | 11143 | ret = BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11144 | #else /* NO_SHA */ |
wolfSSL | 15:117db924cf7c | 11145 | if (kid_type == SKID_TYPE) { |
wolfSSL | 15:117db924cf7c | 11146 | ret = wc_ShaHash(buffer, bufferSz, cert->skid); |
wolfSSL | 15:117db924cf7c | 11147 | cert->skidSz = WC_SHA_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 11148 | } |
wolfSSL | 15:117db924cf7c | 11149 | else if (kid_type == AKID_TYPE) { |
wolfSSL | 15:117db924cf7c | 11150 | ret = wc_ShaHash(buffer, bufferSz, cert->akid); |
wolfSSL | 15:117db924cf7c | 11151 | cert->akidSz = WC_SHA_DIGEST_SIZE; |
wolfSSL | 15:117db924cf7c | 11152 | } |
wolfSSL | 15:117db924cf7c | 11153 | else |
wolfSSL | 15:117db924cf7c | 11154 | ret = BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11155 | #endif /* NO_SHA */ |
wolfSSL | 15:117db924cf7c | 11156 | |
wolfSSL | 15:117db924cf7c | 11157 | XFREE(buffer, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11158 | return ret; |
wolfSSL | 15:117db924cf7c | 11159 | } |
wolfSSL | 15:117db924cf7c | 11160 | |
wolfSSL | 15:117db924cf7c | 11161 | int wc_SetSubjectKeyIdFromPublicKey_ex(Cert *cert, int keyType, void* key) |
wolfSSL | 15:117db924cf7c | 11162 | { |
wolfSSL | 15:117db924cf7c | 11163 | RsaKey* rsaKey = NULL; |
wolfSSL | 15:117db924cf7c | 11164 | ecc_key* eccKey = NULL; |
wolfSSL | 15:117db924cf7c | 11165 | ed25519_key* ed25519Key = NULL; |
wolfSSL | 15:117db924cf7c | 11166 | |
wolfSSL | 15:117db924cf7c | 11167 | if (keyType == RSA_TYPE) |
wolfSSL | 15:117db924cf7c | 11168 | rsaKey = (RsaKey*)key; |
wolfSSL | 15:117db924cf7c | 11169 | else if (keyType == ECC_TYPE) |
wolfSSL | 15:117db924cf7c | 11170 | eccKey = (ecc_key*)key; |
wolfSSL | 15:117db924cf7c | 11171 | else if (keyType == ED25519_TYPE) |
wolfSSL | 15:117db924cf7c | 11172 | ed25519Key = (ed25519_key*)key; |
wolfSSL | 15:117db924cf7c | 11173 | |
wolfSSL | 15:117db924cf7c | 11174 | return SetKeyIdFromPublicKey(cert, rsaKey, eccKey, NULL, 0, ed25519Key, |
wolfSSL | 15:117db924cf7c | 11175 | SKID_TYPE); |
wolfSSL | 15:117db924cf7c | 11176 | } |
wolfSSL | 15:117db924cf7c | 11177 | |
wolfSSL | 15:117db924cf7c | 11178 | /* Set SKID from RSA or ECC public key */ |
wolfSSL | 15:117db924cf7c | 11179 | int wc_SetSubjectKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey, ecc_key *eckey) |
wolfSSL | 15:117db924cf7c | 11180 | { |
wolfSSL | 15:117db924cf7c | 11181 | return SetKeyIdFromPublicKey(cert, rsakey, eckey, NULL, 0, NULL, SKID_TYPE); |
wolfSSL | 15:117db924cf7c | 11182 | } |
wolfSSL | 15:117db924cf7c | 11183 | |
wolfSSL | 15:117db924cf7c | 11184 | #ifdef HAVE_NTRU |
wolfSSL | 15:117db924cf7c | 11185 | /* Set SKID from NTRU public key */ |
wolfSSL | 15:117db924cf7c | 11186 | int wc_SetSubjectKeyIdFromNtruPublicKey(Cert *cert, |
wolfSSL | 15:117db924cf7c | 11187 | byte *ntruKey, word16 ntruKeySz) |
wolfSSL | 15:117db924cf7c | 11188 | { |
wolfSSL | 15:117db924cf7c | 11189 | return SetKeyIdFromPublicKey(cert, NULL,NULL,ntruKey, ntruKeySz, NULL, |
wolfSSL | 15:117db924cf7c | 11190 | SKID_TYPE); |
wolfSSL | 15:117db924cf7c | 11191 | } |
wolfSSL | 15:117db924cf7c | 11192 | #endif |
wolfSSL | 15:117db924cf7c | 11193 | |
wolfSSL | 15:117db924cf7c | 11194 | int wc_SetAuthKeyIdFromPublicKey_ex(Cert *cert, int keyType, void* key) |
wolfSSL | 15:117db924cf7c | 11195 | { |
wolfSSL | 15:117db924cf7c | 11196 | RsaKey* rsaKey = NULL; |
wolfSSL | 15:117db924cf7c | 11197 | ecc_key* eccKey = NULL; |
wolfSSL | 15:117db924cf7c | 11198 | ed25519_key* ed25519Key = NULL; |
wolfSSL | 15:117db924cf7c | 11199 | |
wolfSSL | 15:117db924cf7c | 11200 | if (keyType == RSA_TYPE) |
wolfSSL | 15:117db924cf7c | 11201 | rsaKey = (RsaKey*)key; |
wolfSSL | 15:117db924cf7c | 11202 | else if (keyType == ECC_TYPE) |
wolfSSL | 15:117db924cf7c | 11203 | eccKey = (ecc_key*)key; |
wolfSSL | 15:117db924cf7c | 11204 | else if (keyType == ED25519_TYPE) |
wolfSSL | 15:117db924cf7c | 11205 | ed25519Key = (ed25519_key*)key; |
wolfSSL | 15:117db924cf7c | 11206 | |
wolfSSL | 15:117db924cf7c | 11207 | return SetKeyIdFromPublicKey(cert, rsaKey, eccKey, NULL, 0, ed25519Key, |
wolfSSL | 15:117db924cf7c | 11208 | AKID_TYPE); |
wolfSSL | 15:117db924cf7c | 11209 | } |
wolfSSL | 15:117db924cf7c | 11210 | |
wolfSSL | 15:117db924cf7c | 11211 | /* Set SKID from RSA or ECC public key */ |
wolfSSL | 15:117db924cf7c | 11212 | int wc_SetAuthKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey, ecc_key *eckey) |
wolfSSL | 15:117db924cf7c | 11213 | { |
wolfSSL | 15:117db924cf7c | 11214 | return SetKeyIdFromPublicKey(cert, rsakey, eckey, NULL, 0, NULL, AKID_TYPE); |
wolfSSL | 15:117db924cf7c | 11215 | } |
wolfSSL | 15:117db924cf7c | 11216 | |
wolfSSL | 15:117db924cf7c | 11217 | |
wolfSSL | 15:117db924cf7c | 11218 | #ifndef NO_FILESYSTEM |
wolfSSL | 15:117db924cf7c | 11219 | |
wolfSSL | 15:117db924cf7c | 11220 | /* Set SKID from public key file in PEM */ |
wolfSSL | 15:117db924cf7c | 11221 | int wc_SetSubjectKeyId(Cert *cert, const char* file) |
wolfSSL | 15:117db924cf7c | 11222 | { |
wolfSSL | 15:117db924cf7c | 11223 | int ret, derSz; |
wolfSSL | 15:117db924cf7c | 11224 | byte* der; |
wolfSSL | 15:117db924cf7c | 11225 | word32 idx; |
wolfSSL | 15:117db924cf7c | 11226 | RsaKey *rsakey = NULL; |
wolfSSL | 15:117db924cf7c | 11227 | ecc_key *eckey = NULL; |
wolfSSL | 15:117db924cf7c | 11228 | |
wolfSSL | 15:117db924cf7c | 11229 | if (cert == NULL || file == NULL) |
wolfSSL | 15:117db924cf7c | 11230 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11231 | |
wolfSSL | 15:117db924cf7c | 11232 | der = (byte*)XMALLOC(MAX_PUBLIC_KEY_SZ, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11233 | if (der == NULL) { |
wolfSSL | 15:117db924cf7c | 11234 | WOLFSSL_MSG("wc_SetSubjectKeyId memory Problem"); |
wolfSSL | 15:117db924cf7c | 11235 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11236 | } |
wolfSSL | 15:117db924cf7c | 11237 | |
wolfSSL | 15:117db924cf7c | 11238 | derSz = wc_PemPubKeyToDer(file, der, MAX_PUBLIC_KEY_SZ); |
wolfSSL | 15:117db924cf7c | 11239 | if (derSz <= 0) |
wolfSSL | 15:117db924cf7c | 11240 | { |
wolfSSL | 15:117db924cf7c | 11241 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11242 | return derSz; |
wolfSSL | 15:117db924cf7c | 11243 | } |
wolfSSL | 15:117db924cf7c | 11244 | |
wolfSSL | 15:117db924cf7c | 11245 | /* Load PubKey in internal structure */ |
wolfSSL | 15:117db924cf7c | 11246 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 11247 | rsakey = (RsaKey*) XMALLOC(sizeof(RsaKey), cert->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 11248 | if (rsakey == NULL) { |
wolfSSL | 15:117db924cf7c | 11249 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11250 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11251 | } |
wolfSSL | 15:117db924cf7c | 11252 | |
wolfSSL | 15:117db924cf7c | 11253 | if (wc_InitRsaKey(rsakey, cert->heap) != 0) { |
wolfSSL | 15:117db924cf7c | 11254 | WOLFSSL_MSG("wc_InitRsaKey failure"); |
wolfSSL | 15:117db924cf7c | 11255 | XFREE(rsakey, cert->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 11256 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11257 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11258 | } |
wolfSSL | 15:117db924cf7c | 11259 | |
wolfSSL | 15:117db924cf7c | 11260 | idx = 0; |
wolfSSL | 15:117db924cf7c | 11261 | ret = wc_RsaPublicKeyDecode(der, &idx, rsakey, derSz); |
wolfSSL | 15:117db924cf7c | 11262 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 11263 | #endif |
wolfSSL | 15:117db924cf7c | 11264 | { |
wolfSSL | 15:117db924cf7c | 11265 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 11266 | WOLFSSL_MSG("wc_RsaPublicKeyDecode failed"); |
wolfSSL | 15:117db924cf7c | 11267 | wc_FreeRsaKey(rsakey); |
wolfSSL | 15:117db924cf7c | 11268 | XFREE(rsakey, cert->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 11269 | rsakey = NULL; |
wolfSSL | 15:117db924cf7c | 11270 | #endif |
wolfSSL | 15:117db924cf7c | 11271 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 11272 | /* Check to load ecc public key */ |
wolfSSL | 15:117db924cf7c | 11273 | eckey = (ecc_key*) XMALLOC(sizeof(ecc_key), cert->heap, |
wolfSSL | 15:117db924cf7c | 11274 | DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 11275 | if (eckey == NULL) { |
wolfSSL | 15:117db924cf7c | 11276 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11277 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11278 | } |
wolfSSL | 15:117db924cf7c | 11279 | |
wolfSSL | 15:117db924cf7c | 11280 | if (wc_ecc_init(eckey) != 0) { |
wolfSSL | 15:117db924cf7c | 11281 | WOLFSSL_MSG("wc_ecc_init failure"); |
wolfSSL | 15:117db924cf7c | 11282 | wc_ecc_free(eckey); |
wolfSSL | 15:117db924cf7c | 11283 | XFREE(eckey, cert->heap, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 11284 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11285 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11286 | } |
wolfSSL | 15:117db924cf7c | 11287 | |
wolfSSL | 15:117db924cf7c | 11288 | idx = 0; |
wolfSSL | 15:117db924cf7c | 11289 | ret = wc_EccPublicKeyDecode(der, &idx, eckey, derSz); |
wolfSSL | 15:117db924cf7c | 11290 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 11291 | WOLFSSL_MSG("wc_EccPublicKeyDecode failed"); |
wolfSSL | 15:117db924cf7c | 11292 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11293 | wc_ecc_free(eckey); |
wolfSSL | 15:117db924cf7c | 11294 | XFREE(eckey, cert->heap, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 11295 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 11296 | } |
wolfSSL | 15:117db924cf7c | 11297 | #else |
wolfSSL | 15:117db924cf7c | 11298 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11299 | return PUBLIC_KEY_E; |
wolfSSL | 15:117db924cf7c | 11300 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 11301 | } |
wolfSSL | 15:117db924cf7c | 11302 | |
wolfSSL | 15:117db924cf7c | 11303 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11304 | |
wolfSSL | 15:117db924cf7c | 11305 | ret = wc_SetSubjectKeyIdFromPublicKey(cert, rsakey, eckey); |
wolfSSL | 15:117db924cf7c | 11306 | |
wolfSSL | 15:117db924cf7c | 11307 | #ifndef NO_RSA |
wolfSSL | 15:117db924cf7c | 11308 | wc_FreeRsaKey(rsakey); |
wolfSSL | 15:117db924cf7c | 11309 | XFREE(rsakey, cert->heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 15:117db924cf7c | 11310 | #endif |
wolfSSL | 15:117db924cf7c | 11311 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 11312 | wc_ecc_free(eckey); |
wolfSSL | 15:117db924cf7c | 11313 | XFREE(eckey, cert->heap, DYNAMIC_TYPE_ECC); |
wolfSSL | 15:117db924cf7c | 11314 | #endif |
wolfSSL | 15:117db924cf7c | 11315 | return ret; |
wolfSSL | 15:117db924cf7c | 11316 | } |
wolfSSL | 15:117db924cf7c | 11317 | |
wolfSSL | 15:117db924cf7c | 11318 | #endif /* NO_FILESYSTEM */ |
wolfSSL | 15:117db924cf7c | 11319 | |
wolfSSL | 15:117db924cf7c | 11320 | /* Set AKID from certificate contains in buffer (DER encoded) */ |
wolfSSL | 15:117db924cf7c | 11321 | int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz) |
wolfSSL | 15:117db924cf7c | 11322 | { |
wolfSSL | 15:117db924cf7c | 11323 | int ret; |
wolfSSL | 15:117db924cf7c | 11324 | |
wolfSSL | 15:117db924cf7c | 11325 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11326 | DecodedCert* decoded; |
wolfSSL | 15:117db924cf7c | 11327 | #else |
wolfSSL | 15:117db924cf7c | 11328 | DecodedCert decoded[1]; |
wolfSSL | 15:117db924cf7c | 11329 | #endif |
wolfSSL | 15:117db924cf7c | 11330 | |
wolfSSL | 15:117db924cf7c | 11331 | if (cert == NULL || der == NULL || derSz <= 0) |
wolfSSL | 15:117db924cf7c | 11332 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11333 | |
wolfSSL | 15:117db924cf7c | 11334 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11335 | decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), |
wolfSSL | 15:117db924cf7c | 11336 | cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11337 | if (decoded == NULL) |
wolfSSL | 15:117db924cf7c | 11338 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11339 | #endif |
wolfSSL | 15:117db924cf7c | 11340 | |
wolfSSL | 15:117db924cf7c | 11341 | /* decode certificate and get SKID that will be AKID of current cert */ |
wolfSSL | 15:117db924cf7c | 11342 | InitDecodedCert(decoded, (byte*)der, derSz, NULL); |
wolfSSL | 15:117db924cf7c | 11343 | ret = ParseCert(decoded, CERT_TYPE, NO_VERIFY, 0); |
wolfSSL | 15:117db924cf7c | 11344 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 11345 | FreeDecodedCert(decoded); |
wolfSSL | 15:117db924cf7c | 11346 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11347 | XFREE(decoded, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11348 | #endif |
wolfSSL | 15:117db924cf7c | 11349 | return ret; |
wolfSSL | 15:117db924cf7c | 11350 | } |
wolfSSL | 15:117db924cf7c | 11351 | |
wolfSSL | 15:117db924cf7c | 11352 | /* Subject Key Id not found !! */ |
wolfSSL | 15:117db924cf7c | 11353 | if (decoded->extSubjKeyIdSet == 0) { |
wolfSSL | 15:117db924cf7c | 11354 | FreeDecodedCert(decoded); |
wolfSSL | 15:117db924cf7c | 11355 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11356 | XFREE(decoded, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11357 | #endif |
wolfSSL | 15:117db924cf7c | 11358 | return ASN_NO_SKID; |
wolfSSL | 15:117db924cf7c | 11359 | } |
wolfSSL | 15:117db924cf7c | 11360 | |
wolfSSL | 15:117db924cf7c | 11361 | /* SKID invalid size */ |
wolfSSL | 15:117db924cf7c | 11362 | if (sizeof(cert->akid) < sizeof(decoded->extSubjKeyId)) { |
wolfSSL | 15:117db924cf7c | 11363 | FreeDecodedCert(decoded); |
wolfSSL | 15:117db924cf7c | 11364 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11365 | XFREE(decoded, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11366 | #endif |
wolfSSL | 15:117db924cf7c | 11367 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11368 | } |
wolfSSL | 15:117db924cf7c | 11369 | |
wolfSSL | 15:117db924cf7c | 11370 | /* Put the SKID of CA to AKID of certificate */ |
wolfSSL | 15:117db924cf7c | 11371 | XMEMCPY(cert->akid, decoded->extSubjKeyId, KEYID_SIZE); |
wolfSSL | 15:117db924cf7c | 11372 | cert->akidSz = KEYID_SIZE; |
wolfSSL | 15:117db924cf7c | 11373 | |
wolfSSL | 15:117db924cf7c | 11374 | FreeDecodedCert(decoded); |
wolfSSL | 15:117db924cf7c | 11375 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11376 | XFREE(decoded, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11377 | #endif |
wolfSSL | 15:117db924cf7c | 11378 | |
wolfSSL | 15:117db924cf7c | 11379 | return 0; |
wolfSSL | 15:117db924cf7c | 11380 | } |
wolfSSL | 15:117db924cf7c | 11381 | |
wolfSSL | 15:117db924cf7c | 11382 | |
wolfSSL | 15:117db924cf7c | 11383 | #ifndef NO_FILESYSTEM |
wolfSSL | 15:117db924cf7c | 11384 | |
wolfSSL | 15:117db924cf7c | 11385 | /* Set AKID from certificate file in PEM */ |
wolfSSL | 15:117db924cf7c | 11386 | int wc_SetAuthKeyId(Cert *cert, const char* file) |
wolfSSL | 15:117db924cf7c | 11387 | { |
wolfSSL | 15:117db924cf7c | 11388 | int ret; |
wolfSSL | 15:117db924cf7c | 11389 | int derSz; |
wolfSSL | 15:117db924cf7c | 11390 | byte* der; |
wolfSSL | 15:117db924cf7c | 11391 | |
wolfSSL | 15:117db924cf7c | 11392 | if (cert == NULL || file == NULL) |
wolfSSL | 15:117db924cf7c | 11393 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11394 | |
wolfSSL | 15:117db924cf7c | 11395 | der = (byte*)XMALLOC(EIGHTK_BUF, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11396 | if (der == NULL) { |
wolfSSL | 15:117db924cf7c | 11397 | WOLFSSL_MSG("wc_SetAuthKeyId OOF Problem"); |
wolfSSL | 15:117db924cf7c | 11398 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11399 | } |
wolfSSL | 15:117db924cf7c | 11400 | |
wolfSSL | 15:117db924cf7c | 11401 | derSz = wc_PemCertToDer(file, der, EIGHTK_BUF); |
wolfSSL | 15:117db924cf7c | 11402 | if (derSz <= 0) |
wolfSSL | 15:117db924cf7c | 11403 | { |
wolfSSL | 15:117db924cf7c | 11404 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11405 | return derSz; |
wolfSSL | 15:117db924cf7c | 11406 | } |
wolfSSL | 15:117db924cf7c | 11407 | |
wolfSSL | 15:117db924cf7c | 11408 | ret = wc_SetAuthKeyIdFromCert(cert, der, derSz); |
wolfSSL | 15:117db924cf7c | 11409 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11410 | |
wolfSSL | 15:117db924cf7c | 11411 | return ret; |
wolfSSL | 15:117db924cf7c | 11412 | } |
wolfSSL | 15:117db924cf7c | 11413 | |
wolfSSL | 15:117db924cf7c | 11414 | #endif /* NO_FILESYSTEM */ |
wolfSSL | 15:117db924cf7c | 11415 | |
wolfSSL | 15:117db924cf7c | 11416 | /* Set KeyUsage from human readable string */ |
wolfSSL | 15:117db924cf7c | 11417 | int wc_SetKeyUsage(Cert *cert, const char *value) |
wolfSSL | 15:117db924cf7c | 11418 | { |
wolfSSL | 15:117db924cf7c | 11419 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 11420 | char *token, *str, *ptr; |
wolfSSL | 15:117db924cf7c | 11421 | word32 len; |
wolfSSL | 15:117db924cf7c | 11422 | |
wolfSSL | 15:117db924cf7c | 11423 | if (cert == NULL || value == NULL) |
wolfSSL | 15:117db924cf7c | 11424 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11425 | |
wolfSSL | 15:117db924cf7c | 11426 | cert->keyUsage = 0; |
wolfSSL | 15:117db924cf7c | 11427 | |
wolfSSL | 15:117db924cf7c | 11428 | len = (word32)XSTRLEN(value); |
wolfSSL | 15:117db924cf7c | 11429 | str = (char*)XMALLOC(len+1, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11430 | if (str == NULL) |
wolfSSL | 15:117db924cf7c | 11431 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11432 | |
wolfSSL | 15:117db924cf7c | 11433 | XSTRNCPY(str, value, len); |
wolfSSL | 15:117db924cf7c | 11434 | str[len] = '\0'; |
wolfSSL | 15:117db924cf7c | 11435 | |
wolfSSL | 15:117db924cf7c | 11436 | /* parse value, and set corresponding Key Usage value */ |
wolfSSL | 15:117db924cf7c | 11437 | if ((token = XSTRTOK(str, ",", &ptr)) == NULL) { |
wolfSSL | 15:117db924cf7c | 11438 | XFREE(str, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11439 | return KEYUSAGE_E; |
wolfSSL | 15:117db924cf7c | 11440 | } |
wolfSSL | 15:117db924cf7c | 11441 | while (token != NULL) |
wolfSSL | 15:117db924cf7c | 11442 | { |
wolfSSL | 15:117db924cf7c | 11443 | len = (word32)XSTRLEN(token); |
wolfSSL | 15:117db924cf7c | 11444 | |
wolfSSL | 15:117db924cf7c | 11445 | if (!XSTRNCASECMP(token, "digitalSignature", len)) |
wolfSSL | 15:117db924cf7c | 11446 | cert->keyUsage |= KEYUSE_DIGITAL_SIG; |
wolfSSL | 15:117db924cf7c | 11447 | else if (!XSTRNCASECMP(token, "nonRepudiation", len) || |
wolfSSL | 15:117db924cf7c | 11448 | !XSTRNCASECMP(token, "contentCommitment", len)) |
wolfSSL | 15:117db924cf7c | 11449 | cert->keyUsage |= KEYUSE_CONTENT_COMMIT; |
wolfSSL | 15:117db924cf7c | 11450 | else if (!XSTRNCASECMP(token, "keyEncipherment", len)) |
wolfSSL | 15:117db924cf7c | 11451 | cert->keyUsage |= KEYUSE_KEY_ENCIPHER; |
wolfSSL | 15:117db924cf7c | 11452 | else if (!XSTRNCASECMP(token, "dataEncipherment", len)) |
wolfSSL | 15:117db924cf7c | 11453 | cert->keyUsage |= KEYUSE_DATA_ENCIPHER; |
wolfSSL | 15:117db924cf7c | 11454 | else if (!XSTRNCASECMP(token, "keyAgreement", len)) |
wolfSSL | 15:117db924cf7c | 11455 | cert->keyUsage |= KEYUSE_KEY_AGREE; |
wolfSSL | 15:117db924cf7c | 11456 | else if (!XSTRNCASECMP(token, "keyCertSign", len)) |
wolfSSL | 15:117db924cf7c | 11457 | cert->keyUsage |= KEYUSE_KEY_CERT_SIGN; |
wolfSSL | 15:117db924cf7c | 11458 | else if (!XSTRNCASECMP(token, "cRLSign", len)) |
wolfSSL | 15:117db924cf7c | 11459 | cert->keyUsage |= KEYUSE_CRL_SIGN; |
wolfSSL | 15:117db924cf7c | 11460 | else if (!XSTRNCASECMP(token, "encipherOnly", len)) |
wolfSSL | 15:117db924cf7c | 11461 | cert->keyUsage |= KEYUSE_ENCIPHER_ONLY; |
wolfSSL | 15:117db924cf7c | 11462 | else if (!XSTRNCASECMP(token, "decipherOnly", len)) |
wolfSSL | 15:117db924cf7c | 11463 | cert->keyUsage |= KEYUSE_DECIPHER_ONLY; |
wolfSSL | 15:117db924cf7c | 11464 | else { |
wolfSSL | 15:117db924cf7c | 11465 | ret = KEYUSAGE_E; |
wolfSSL | 15:117db924cf7c | 11466 | break; |
wolfSSL | 15:117db924cf7c | 11467 | } |
wolfSSL | 15:117db924cf7c | 11468 | |
wolfSSL | 15:117db924cf7c | 11469 | token = XSTRTOK(NULL, ",", &ptr); |
wolfSSL | 15:117db924cf7c | 11470 | } |
wolfSSL | 15:117db924cf7c | 11471 | |
wolfSSL | 15:117db924cf7c | 11472 | XFREE(str, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11473 | return ret; |
wolfSSL | 15:117db924cf7c | 11474 | } |
wolfSSL | 15:117db924cf7c | 11475 | |
wolfSSL | 15:117db924cf7c | 11476 | /* Set ExtendedKeyUsage from human readable string */ |
wolfSSL | 15:117db924cf7c | 11477 | int wc_SetExtKeyUsage(Cert *cert, const char *value) |
wolfSSL | 15:117db924cf7c | 11478 | { |
wolfSSL | 15:117db924cf7c | 11479 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 11480 | char *token, *str, *ptr; |
wolfSSL | 15:117db924cf7c | 11481 | word32 len; |
wolfSSL | 15:117db924cf7c | 11482 | |
wolfSSL | 15:117db924cf7c | 11483 | if (cert == NULL || value == NULL) |
wolfSSL | 15:117db924cf7c | 11484 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11485 | |
wolfSSL | 15:117db924cf7c | 11486 | cert->extKeyUsage = 0; |
wolfSSL | 15:117db924cf7c | 11487 | |
wolfSSL | 15:117db924cf7c | 11488 | len = (word32)XSTRLEN(value); |
wolfSSL | 15:117db924cf7c | 11489 | str = (char*)XMALLOC(len+1, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11490 | if (str == NULL) |
wolfSSL | 15:117db924cf7c | 11491 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11492 | |
wolfSSL | 15:117db924cf7c | 11493 | XSTRNCPY(str, value, len); |
wolfSSL | 15:117db924cf7c | 11494 | str[len] = '\0'; |
wolfSSL | 15:117db924cf7c | 11495 | |
wolfSSL | 15:117db924cf7c | 11496 | /* parse value, and set corresponding Key Usage value */ |
wolfSSL | 15:117db924cf7c | 11497 | if ((token = XSTRTOK(str, ",", &ptr)) == NULL) { |
wolfSSL | 15:117db924cf7c | 11498 | XFREE(str, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11499 | return EXTKEYUSAGE_E; |
wolfSSL | 15:117db924cf7c | 11500 | } |
wolfSSL | 15:117db924cf7c | 11501 | |
wolfSSL | 15:117db924cf7c | 11502 | while (token != NULL) |
wolfSSL | 15:117db924cf7c | 11503 | { |
wolfSSL | 15:117db924cf7c | 11504 | len = (word32)XSTRLEN(token); |
wolfSSL | 15:117db924cf7c | 11505 | |
wolfSSL | 15:117db924cf7c | 11506 | if (!XSTRNCASECMP(token, "any", len)) |
wolfSSL | 15:117db924cf7c | 11507 | cert->extKeyUsage |= EXTKEYUSE_ANY; |
wolfSSL | 15:117db924cf7c | 11508 | else if (!XSTRNCASECMP(token, "serverAuth", len)) |
wolfSSL | 15:117db924cf7c | 11509 | cert->extKeyUsage |= EXTKEYUSE_SERVER_AUTH; |
wolfSSL | 15:117db924cf7c | 11510 | else if (!XSTRNCASECMP(token, "clientAuth", len)) |
wolfSSL | 15:117db924cf7c | 11511 | cert->extKeyUsage |= EXTKEYUSE_CLIENT_AUTH; |
wolfSSL | 15:117db924cf7c | 11512 | else if (!XSTRNCASECMP(token, "codeSigning", len)) |
wolfSSL | 15:117db924cf7c | 11513 | cert->extKeyUsage |= EXTKEYUSE_CODESIGN; |
wolfSSL | 15:117db924cf7c | 11514 | else if (!XSTRNCASECMP(token, "emailProtection", len)) |
wolfSSL | 15:117db924cf7c | 11515 | cert->extKeyUsage |= EXTKEYUSE_EMAILPROT; |
wolfSSL | 15:117db924cf7c | 11516 | else if (!XSTRNCASECMP(token, "timeStamping", len)) |
wolfSSL | 15:117db924cf7c | 11517 | cert->extKeyUsage |= EXTKEYUSE_TIMESTAMP; |
wolfSSL | 15:117db924cf7c | 11518 | else if (!XSTRNCASECMP(token, "OCSPSigning", len)) |
wolfSSL | 15:117db924cf7c | 11519 | cert->extKeyUsage |= EXTKEYUSE_OCSP_SIGN; |
wolfSSL | 15:117db924cf7c | 11520 | else { |
wolfSSL | 15:117db924cf7c | 11521 | ret = EXTKEYUSAGE_E; |
wolfSSL | 15:117db924cf7c | 11522 | break; |
wolfSSL | 15:117db924cf7c | 11523 | } |
wolfSSL | 15:117db924cf7c | 11524 | |
wolfSSL | 15:117db924cf7c | 11525 | token = XSTRTOK(NULL, ",", &ptr); |
wolfSSL | 15:117db924cf7c | 11526 | } |
wolfSSL | 15:117db924cf7c | 11527 | |
wolfSSL | 15:117db924cf7c | 11528 | XFREE(str, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11529 | return ret; |
wolfSSL | 15:117db924cf7c | 11530 | } |
wolfSSL | 15:117db924cf7c | 11531 | |
wolfSSL | 15:117db924cf7c | 11532 | #ifdef WOLFSSL_EKU_OID |
wolfSSL | 15:117db924cf7c | 11533 | /* |
wolfSSL | 15:117db924cf7c | 11534 | * cert structure to set EKU oid in |
wolfSSL | 15:117db924cf7c | 11535 | * oid the oid in byte representation |
wolfSSL | 15:117db924cf7c | 11536 | * sz size of oid buffer |
wolfSSL | 15:117db924cf7c | 11537 | * idx index of array to place oid |
wolfSSL | 15:117db924cf7c | 11538 | * |
wolfSSL | 15:117db924cf7c | 11539 | * returns 0 on success |
wolfSSL | 15:117db924cf7c | 11540 | */ |
wolfSSL | 15:117db924cf7c | 11541 | int wc_SetExtKeyUsageOID(Cert *cert, const char *in, word32 sz, byte idx, |
wolfSSL | 15:117db924cf7c | 11542 | void* heap) |
wolfSSL | 15:117db924cf7c | 11543 | { |
wolfSSL | 15:117db924cf7c | 11544 | byte oid[MAX_OID_SZ]; |
wolfSSL | 15:117db924cf7c | 11545 | word32 oidSz = MAX_OID_SZ; |
wolfSSL | 15:117db924cf7c | 11546 | |
wolfSSL | 15:117db924cf7c | 11547 | if (idx >= CTC_MAX_EKU_NB || sz >= CTC_MAX_EKU_OID_SZ) { |
wolfSSL | 15:117db924cf7c | 11548 | WOLFSSL_MSG("Either idx or sz was too large"); |
wolfSSL | 15:117db924cf7c | 11549 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11550 | } |
wolfSSL | 15:117db924cf7c | 11551 | |
wolfSSL | 15:117db924cf7c | 11552 | if (EncodePolicyOID(oid, &oidSz, in, heap) != 0) { |
wolfSSL | 15:117db924cf7c | 11553 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 11554 | } |
wolfSSL | 15:117db924cf7c | 11555 | |
wolfSSL | 15:117db924cf7c | 11556 | XMEMCPY(cert->extKeyUsageOID[idx], oid, oidSz); |
wolfSSL | 15:117db924cf7c | 11557 | cert->extKeyUsageOIDSz[idx] = oidSz; |
wolfSSL | 15:117db924cf7c | 11558 | cert->extKeyUsage |= EXTKEYUSE_USER; |
wolfSSL | 15:117db924cf7c | 11559 | |
wolfSSL | 15:117db924cf7c | 11560 | return 0; |
wolfSSL | 15:117db924cf7c | 11561 | } |
wolfSSL | 15:117db924cf7c | 11562 | #endif /* WOLFSSL_EKU_OID */ |
wolfSSL | 15:117db924cf7c | 11563 | #endif /* WOLFSSL_CERT_EXT */ |
wolfSSL | 15:117db924cf7c | 11564 | |
wolfSSL | 15:117db924cf7c | 11565 | |
wolfSSL | 15:117db924cf7c | 11566 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 11567 | |
wolfSSL | 15:117db924cf7c | 11568 | /* Set Alt Names from der cert, return 0 on success */ |
wolfSSL | 15:117db924cf7c | 11569 | static int SetAltNamesFromCert(Cert* cert, const byte* der, int derSz) |
wolfSSL | 15:117db924cf7c | 11570 | { |
wolfSSL | 15:117db924cf7c | 11571 | int ret; |
wolfSSL | 15:117db924cf7c | 11572 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11573 | DecodedCert* decoded; |
wolfSSL | 15:117db924cf7c | 11574 | #else |
wolfSSL | 15:117db924cf7c | 11575 | DecodedCert decoded[1]; |
wolfSSL | 15:117db924cf7c | 11576 | #endif |
wolfSSL | 15:117db924cf7c | 11577 | |
wolfSSL | 15:117db924cf7c | 11578 | if (derSz < 0) |
wolfSSL | 15:117db924cf7c | 11579 | return derSz; |
wolfSSL | 15:117db924cf7c | 11580 | |
wolfSSL | 15:117db924cf7c | 11581 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11582 | decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cert->heap, |
wolfSSL | 15:117db924cf7c | 11583 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11584 | if (decoded == NULL) |
wolfSSL | 15:117db924cf7c | 11585 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11586 | #endif |
wolfSSL | 15:117db924cf7c | 11587 | |
wolfSSL | 15:117db924cf7c | 11588 | InitDecodedCert(decoded, (byte*)der, derSz, NULL); |
wolfSSL | 15:117db924cf7c | 11589 | ret = ParseCertRelative(decoded, CA_TYPE, NO_VERIFY, 0); |
wolfSSL | 15:117db924cf7c | 11590 | |
wolfSSL | 15:117db924cf7c | 11591 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 11592 | WOLFSSL_MSG("ParseCertRelative error"); |
wolfSSL | 15:117db924cf7c | 11593 | } |
wolfSSL | 15:117db924cf7c | 11594 | else if (decoded->extensions) { |
wolfSSL | 15:117db924cf7c | 11595 | byte b; |
wolfSSL | 15:117db924cf7c | 11596 | int length; |
wolfSSL | 15:117db924cf7c | 11597 | word32 maxExtensionsIdx; |
wolfSSL | 15:117db924cf7c | 11598 | |
wolfSSL | 15:117db924cf7c | 11599 | decoded->srcIdx = decoded->extensionsIdx; |
wolfSSL | 15:117db924cf7c | 11600 | b = decoded->source[decoded->srcIdx++]; |
wolfSSL | 15:117db924cf7c | 11601 | |
wolfSSL | 15:117db924cf7c | 11602 | if (b != ASN_EXTENSIONS) { |
wolfSSL | 15:117db924cf7c | 11603 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 11604 | } |
wolfSSL | 15:117db924cf7c | 11605 | else if (GetLength(decoded->source, &decoded->srcIdx, &length, |
wolfSSL | 15:117db924cf7c | 11606 | decoded->maxIdx) < 0) { |
wolfSSL | 15:117db924cf7c | 11607 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 11608 | } |
wolfSSL | 15:117db924cf7c | 11609 | else if (GetSequence(decoded->source, &decoded->srcIdx, &length, |
wolfSSL | 15:117db924cf7c | 11610 | decoded->maxIdx) < 0) { |
wolfSSL | 15:117db924cf7c | 11611 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 11612 | } |
wolfSSL | 15:117db924cf7c | 11613 | else { |
wolfSSL | 15:117db924cf7c | 11614 | maxExtensionsIdx = decoded->srcIdx + length; |
wolfSSL | 15:117db924cf7c | 11615 | |
wolfSSL | 15:117db924cf7c | 11616 | while (decoded->srcIdx < maxExtensionsIdx) { |
wolfSSL | 15:117db924cf7c | 11617 | word32 oid; |
wolfSSL | 15:117db924cf7c | 11618 | word32 startIdx = decoded->srcIdx; |
wolfSSL | 15:117db924cf7c | 11619 | word32 tmpIdx; |
wolfSSL | 15:117db924cf7c | 11620 | |
wolfSSL | 15:117db924cf7c | 11621 | if (GetSequence(decoded->source, &decoded->srcIdx, &length, |
wolfSSL | 15:117db924cf7c | 11622 | decoded->maxIdx) < 0) { |
wolfSSL | 15:117db924cf7c | 11623 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 11624 | break; |
wolfSSL | 15:117db924cf7c | 11625 | } |
wolfSSL | 15:117db924cf7c | 11626 | |
wolfSSL | 15:117db924cf7c | 11627 | tmpIdx = decoded->srcIdx; |
wolfSSL | 15:117db924cf7c | 11628 | decoded->srcIdx = startIdx; |
wolfSSL | 15:117db924cf7c | 11629 | |
wolfSSL | 15:117db924cf7c | 11630 | if (GetAlgoId(decoded->source, &decoded->srcIdx, &oid, |
wolfSSL | 15:117db924cf7c | 11631 | oidCertExtType, decoded->maxIdx) < 0) { |
wolfSSL | 15:117db924cf7c | 11632 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 11633 | break; |
wolfSSL | 15:117db924cf7c | 11634 | } |
wolfSSL | 15:117db924cf7c | 11635 | |
wolfSSL | 15:117db924cf7c | 11636 | if (oid == ALT_NAMES_OID) { |
wolfSSL | 15:117db924cf7c | 11637 | cert->altNamesSz = length + (tmpIdx - startIdx); |
wolfSSL | 15:117db924cf7c | 11638 | |
wolfSSL | 15:117db924cf7c | 11639 | if (cert->altNamesSz < (int)sizeof(cert->altNames)) |
wolfSSL | 15:117db924cf7c | 11640 | XMEMCPY(cert->altNames, &decoded->source[startIdx], |
wolfSSL | 15:117db924cf7c | 11641 | cert->altNamesSz); |
wolfSSL | 15:117db924cf7c | 11642 | else { |
wolfSSL | 15:117db924cf7c | 11643 | cert->altNamesSz = 0; |
wolfSSL | 15:117db924cf7c | 11644 | WOLFSSL_MSG("AltNames extensions too big"); |
wolfSSL | 15:117db924cf7c | 11645 | ret = ALT_NAME_E; |
wolfSSL | 15:117db924cf7c | 11646 | break; |
wolfSSL | 15:117db924cf7c | 11647 | } |
wolfSSL | 15:117db924cf7c | 11648 | } |
wolfSSL | 15:117db924cf7c | 11649 | decoded->srcIdx = tmpIdx + length; |
wolfSSL | 15:117db924cf7c | 11650 | } |
wolfSSL | 15:117db924cf7c | 11651 | } |
wolfSSL | 15:117db924cf7c | 11652 | } |
wolfSSL | 15:117db924cf7c | 11653 | |
wolfSSL | 15:117db924cf7c | 11654 | FreeDecodedCert(decoded); |
wolfSSL | 15:117db924cf7c | 11655 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11656 | XFREE(decoded, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11657 | #endif |
wolfSSL | 15:117db924cf7c | 11658 | |
wolfSSL | 15:117db924cf7c | 11659 | return ret < 0 ? ret : 0; |
wolfSSL | 15:117db924cf7c | 11660 | } |
wolfSSL | 15:117db924cf7c | 11661 | |
wolfSSL | 15:117db924cf7c | 11662 | |
wolfSSL | 15:117db924cf7c | 11663 | /* Set Dates from der cert, return 0 on success */ |
wolfSSL | 15:117db924cf7c | 11664 | static int SetDatesFromCert(Cert* cert, const byte* der, int derSz) |
wolfSSL | 15:117db924cf7c | 11665 | { |
wolfSSL | 15:117db924cf7c | 11666 | int ret; |
wolfSSL | 15:117db924cf7c | 11667 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11668 | DecodedCert* decoded; |
wolfSSL | 15:117db924cf7c | 11669 | #else |
wolfSSL | 15:117db924cf7c | 11670 | DecodedCert decoded[1]; |
wolfSSL | 15:117db924cf7c | 11671 | #endif |
wolfSSL | 15:117db924cf7c | 11672 | |
wolfSSL | 15:117db924cf7c | 11673 | WOLFSSL_ENTER("SetDatesFromCert"); |
wolfSSL | 15:117db924cf7c | 11674 | if (derSz < 0) |
wolfSSL | 15:117db924cf7c | 11675 | return derSz; |
wolfSSL | 15:117db924cf7c | 11676 | |
wolfSSL | 15:117db924cf7c | 11677 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11678 | decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cert->heap, |
wolfSSL | 15:117db924cf7c | 11679 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11680 | if (decoded == NULL) |
wolfSSL | 15:117db924cf7c | 11681 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11682 | #endif |
wolfSSL | 15:117db924cf7c | 11683 | |
wolfSSL | 15:117db924cf7c | 11684 | InitDecodedCert(decoded, (byte*)der, derSz, NULL); |
wolfSSL | 15:117db924cf7c | 11685 | ret = ParseCertRelative(decoded, CA_TYPE, NO_VERIFY, 0); |
wolfSSL | 15:117db924cf7c | 11686 | |
wolfSSL | 15:117db924cf7c | 11687 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 11688 | WOLFSSL_MSG("ParseCertRelative error"); |
wolfSSL | 15:117db924cf7c | 11689 | } |
wolfSSL | 15:117db924cf7c | 11690 | else if (decoded->beforeDate == NULL || decoded->afterDate == NULL) { |
wolfSSL | 15:117db924cf7c | 11691 | WOLFSSL_MSG("Couldn't extract dates"); |
wolfSSL | 15:117db924cf7c | 11692 | ret = -1; |
wolfSSL | 15:117db924cf7c | 11693 | } |
wolfSSL | 15:117db924cf7c | 11694 | else if (decoded->beforeDateLen > MAX_DATE_SIZE || |
wolfSSL | 15:117db924cf7c | 11695 | decoded->afterDateLen > MAX_DATE_SIZE) { |
wolfSSL | 15:117db924cf7c | 11696 | WOLFSSL_MSG("Bad date size"); |
wolfSSL | 15:117db924cf7c | 11697 | ret = -1; |
wolfSSL | 15:117db924cf7c | 11698 | } |
wolfSSL | 15:117db924cf7c | 11699 | else { |
wolfSSL | 15:117db924cf7c | 11700 | XMEMCPY(cert->beforeDate, decoded->beforeDate, decoded->beforeDateLen); |
wolfSSL | 15:117db924cf7c | 11701 | XMEMCPY(cert->afterDate, decoded->afterDate, decoded->afterDateLen); |
wolfSSL | 15:117db924cf7c | 11702 | |
wolfSSL | 15:117db924cf7c | 11703 | cert->beforeDateSz = decoded->beforeDateLen; |
wolfSSL | 15:117db924cf7c | 11704 | cert->afterDateSz = decoded->afterDateLen; |
wolfSSL | 15:117db924cf7c | 11705 | } |
wolfSSL | 15:117db924cf7c | 11706 | |
wolfSSL | 15:117db924cf7c | 11707 | FreeDecodedCert(decoded); |
wolfSSL | 15:117db924cf7c | 11708 | |
wolfSSL | 15:117db924cf7c | 11709 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11710 | XFREE(decoded, cert->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11711 | #endif |
wolfSSL | 15:117db924cf7c | 11712 | |
wolfSSL | 15:117db924cf7c | 11713 | return ret < 0 ? ret : 0; |
wolfSSL | 15:117db924cf7c | 11714 | } |
wolfSSL | 15:117db924cf7c | 11715 | |
wolfSSL | 15:117db924cf7c | 11716 | #endif /* WOLFSSL_ALT_NAMES */ |
wolfSSL | 15:117db924cf7c | 11717 | |
wolfSSL | 15:117db924cf7c | 11718 | /* Set cn name from der buffer, return 0 on success */ |
wolfSSL | 15:117db924cf7c | 11719 | static int SetNameFromCert(CertName* cn, const byte* der, int derSz) |
wolfSSL | 15:117db924cf7c | 11720 | { |
wolfSSL | 15:117db924cf7c | 11721 | int ret, sz; |
wolfSSL | 15:117db924cf7c | 11722 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11723 | DecodedCert* decoded; |
wolfSSL | 15:117db924cf7c | 11724 | #else |
wolfSSL | 15:117db924cf7c | 11725 | DecodedCert decoded[1]; |
wolfSSL | 15:117db924cf7c | 11726 | #endif |
wolfSSL | 15:117db924cf7c | 11727 | |
wolfSSL | 15:117db924cf7c | 11728 | if (derSz < 0) |
wolfSSL | 15:117db924cf7c | 11729 | return derSz; |
wolfSSL | 15:117db924cf7c | 11730 | |
wolfSSL | 15:117db924cf7c | 11731 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11732 | decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, |
wolfSSL | 15:117db924cf7c | 11733 | DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11734 | if (decoded == NULL) |
wolfSSL | 15:117db924cf7c | 11735 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11736 | #endif |
wolfSSL | 15:117db924cf7c | 11737 | |
wolfSSL | 15:117db924cf7c | 11738 | InitDecodedCert(decoded, (byte*)der, derSz, NULL); |
wolfSSL | 15:117db924cf7c | 11739 | ret = ParseCertRelative(decoded, CA_TYPE, NO_VERIFY, 0); |
wolfSSL | 15:117db924cf7c | 11740 | |
wolfSSL | 15:117db924cf7c | 11741 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 11742 | WOLFSSL_MSG("ParseCertRelative error"); |
wolfSSL | 15:117db924cf7c | 11743 | } |
wolfSSL | 15:117db924cf7c | 11744 | else { |
wolfSSL | 15:117db924cf7c | 11745 | if (decoded->subjectCN) { |
wolfSSL | 15:117db924cf7c | 11746 | sz = (decoded->subjectCNLen < CTC_NAME_SIZE) ? decoded->subjectCNLen |
wolfSSL | 15:117db924cf7c | 11747 | : CTC_NAME_SIZE - 1; |
wolfSSL | 15:117db924cf7c | 11748 | XSTRNCPY(cn->commonName, decoded->subjectCN, CTC_NAME_SIZE); |
wolfSSL | 15:117db924cf7c | 11749 | cn->commonName[sz] = '\0'; |
wolfSSL | 15:117db924cf7c | 11750 | cn->commonNameEnc = decoded->subjectCNEnc; |
wolfSSL | 15:117db924cf7c | 11751 | } |
wolfSSL | 15:117db924cf7c | 11752 | if (decoded->subjectC) { |
wolfSSL | 15:117db924cf7c | 11753 | sz = (decoded->subjectCLen < CTC_NAME_SIZE) ? decoded->subjectCLen |
wolfSSL | 15:117db924cf7c | 11754 | : CTC_NAME_SIZE - 1; |
wolfSSL | 15:117db924cf7c | 11755 | XSTRNCPY(cn->country, decoded->subjectC, CTC_NAME_SIZE); |
wolfSSL | 15:117db924cf7c | 11756 | cn->country[sz] = '\0'; |
wolfSSL | 15:117db924cf7c | 11757 | cn->countryEnc = decoded->subjectCEnc; |
wolfSSL | 15:117db924cf7c | 11758 | } |
wolfSSL | 15:117db924cf7c | 11759 | if (decoded->subjectST) { |
wolfSSL | 15:117db924cf7c | 11760 | sz = (decoded->subjectSTLen < CTC_NAME_SIZE) ? decoded->subjectSTLen |
wolfSSL | 15:117db924cf7c | 11761 | : CTC_NAME_SIZE - 1; |
wolfSSL | 15:117db924cf7c | 11762 | XSTRNCPY(cn->state, decoded->subjectST, CTC_NAME_SIZE); |
wolfSSL | 15:117db924cf7c | 11763 | cn->state[sz] = '\0'; |
wolfSSL | 15:117db924cf7c | 11764 | cn->stateEnc = decoded->subjectSTEnc; |
wolfSSL | 15:117db924cf7c | 11765 | } |
wolfSSL | 15:117db924cf7c | 11766 | if (decoded->subjectL) { |
wolfSSL | 15:117db924cf7c | 11767 | sz = (decoded->subjectLLen < CTC_NAME_SIZE) ? decoded->subjectLLen |
wolfSSL | 15:117db924cf7c | 11768 | : CTC_NAME_SIZE - 1; |
wolfSSL | 15:117db924cf7c | 11769 | XSTRNCPY(cn->locality, decoded->subjectL, CTC_NAME_SIZE); |
wolfSSL | 15:117db924cf7c | 11770 | cn->locality[sz] = '\0'; |
wolfSSL | 15:117db924cf7c | 11771 | cn->localityEnc = decoded->subjectLEnc; |
wolfSSL | 15:117db924cf7c | 11772 | } |
wolfSSL | 15:117db924cf7c | 11773 | if (decoded->subjectO) { |
wolfSSL | 15:117db924cf7c | 11774 | sz = (decoded->subjectOLen < CTC_NAME_SIZE) ? decoded->subjectOLen |
wolfSSL | 15:117db924cf7c | 11775 | : CTC_NAME_SIZE - 1; |
wolfSSL | 15:117db924cf7c | 11776 | XSTRNCPY(cn->org, decoded->subjectO, CTC_NAME_SIZE); |
wolfSSL | 15:117db924cf7c | 11777 | cn->org[sz] = '\0'; |
wolfSSL | 15:117db924cf7c | 11778 | cn->orgEnc = decoded->subjectOEnc; |
wolfSSL | 15:117db924cf7c | 11779 | } |
wolfSSL | 15:117db924cf7c | 11780 | if (decoded->subjectOU) { |
wolfSSL | 15:117db924cf7c | 11781 | sz = (decoded->subjectOULen < CTC_NAME_SIZE) ? decoded->subjectOULen |
wolfSSL | 15:117db924cf7c | 11782 | : CTC_NAME_SIZE - 1; |
wolfSSL | 15:117db924cf7c | 11783 | XSTRNCPY(cn->unit, decoded->subjectOU, CTC_NAME_SIZE); |
wolfSSL | 15:117db924cf7c | 11784 | cn->unit[sz] = '\0'; |
wolfSSL | 15:117db924cf7c | 11785 | cn->unitEnc = decoded->subjectOUEnc; |
wolfSSL | 15:117db924cf7c | 11786 | } |
wolfSSL | 15:117db924cf7c | 11787 | if (decoded->subjectSN) { |
wolfSSL | 15:117db924cf7c | 11788 | sz = (decoded->subjectSNLen < CTC_NAME_SIZE) ? decoded->subjectSNLen |
wolfSSL | 15:117db924cf7c | 11789 | : CTC_NAME_SIZE - 1; |
wolfSSL | 15:117db924cf7c | 11790 | XSTRNCPY(cn->sur, decoded->subjectSN, CTC_NAME_SIZE); |
wolfSSL | 15:117db924cf7c | 11791 | cn->sur[sz] = '\0'; |
wolfSSL | 15:117db924cf7c | 11792 | cn->surEnc = decoded->subjectSNEnc; |
wolfSSL | 15:117db924cf7c | 11793 | } |
wolfSSL | 15:117db924cf7c | 11794 | if (decoded->subjectEmail) { |
wolfSSL | 15:117db924cf7c | 11795 | sz = (decoded->subjectEmailLen < CTC_NAME_SIZE) |
wolfSSL | 15:117db924cf7c | 11796 | ? decoded->subjectEmailLen : CTC_NAME_SIZE - 1; |
wolfSSL | 15:117db924cf7c | 11797 | XSTRNCPY(cn->email, decoded->subjectEmail, CTC_NAME_SIZE); |
wolfSSL | 15:117db924cf7c | 11798 | cn->email[sz] = '\0'; |
wolfSSL | 15:117db924cf7c | 11799 | } |
wolfSSL | 15:117db924cf7c | 11800 | } |
wolfSSL | 15:117db924cf7c | 11801 | |
wolfSSL | 15:117db924cf7c | 11802 | FreeDecodedCert(decoded); |
wolfSSL | 15:117db924cf7c | 11803 | |
wolfSSL | 15:117db924cf7c | 11804 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11805 | XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 11806 | #endif |
wolfSSL | 15:117db924cf7c | 11807 | |
wolfSSL | 15:117db924cf7c | 11808 | return ret < 0 ? ret : 0; |
wolfSSL | 15:117db924cf7c | 11809 | } |
wolfSSL | 15:117db924cf7c | 11810 | |
wolfSSL | 15:117db924cf7c | 11811 | |
wolfSSL | 15:117db924cf7c | 11812 | #ifndef NO_FILESYSTEM |
wolfSSL | 15:117db924cf7c | 11813 | |
wolfSSL | 15:117db924cf7c | 11814 | /* Set cert issuer from issuerFile in PEM */ |
wolfSSL | 15:117db924cf7c | 11815 | int wc_SetIssuer(Cert* cert, const char* issuerFile) |
wolfSSL | 15:117db924cf7c | 11816 | { |
wolfSSL | 15:117db924cf7c | 11817 | int ret; |
wolfSSL | 15:117db924cf7c | 11818 | int derSz; |
wolfSSL | 15:117db924cf7c | 11819 | byte* der = (byte*)XMALLOC(EIGHTK_BUF, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11820 | |
wolfSSL | 15:117db924cf7c | 11821 | if (der == NULL) { |
wolfSSL | 15:117db924cf7c | 11822 | WOLFSSL_MSG("wc_SetIssuer OOF Problem"); |
wolfSSL | 15:117db924cf7c | 11823 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11824 | } |
wolfSSL | 15:117db924cf7c | 11825 | derSz = wc_PemCertToDer(issuerFile, der, EIGHTK_BUF); |
wolfSSL | 15:117db924cf7c | 11826 | cert->selfSigned = 0; |
wolfSSL | 15:117db924cf7c | 11827 | ret = SetNameFromCert(&cert->issuer, der, derSz); |
wolfSSL | 15:117db924cf7c | 11828 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11829 | |
wolfSSL | 15:117db924cf7c | 11830 | return ret; |
wolfSSL | 15:117db924cf7c | 11831 | } |
wolfSSL | 15:117db924cf7c | 11832 | |
wolfSSL | 15:117db924cf7c | 11833 | |
wolfSSL | 15:117db924cf7c | 11834 | /* Set cert subject from subjectFile in PEM */ |
wolfSSL | 15:117db924cf7c | 11835 | int wc_SetSubject(Cert* cert, const char* subjectFile) |
wolfSSL | 15:117db924cf7c | 11836 | { |
wolfSSL | 15:117db924cf7c | 11837 | int ret; |
wolfSSL | 15:117db924cf7c | 11838 | int derSz; |
wolfSSL | 15:117db924cf7c | 11839 | byte* der = (byte*)XMALLOC(EIGHTK_BUF, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11840 | |
wolfSSL | 15:117db924cf7c | 11841 | if (der == NULL) { |
wolfSSL | 15:117db924cf7c | 11842 | WOLFSSL_MSG("wc_SetSubject OOF Problem"); |
wolfSSL | 15:117db924cf7c | 11843 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11844 | } |
wolfSSL | 15:117db924cf7c | 11845 | derSz = wc_PemCertToDer(subjectFile, der, EIGHTK_BUF); |
wolfSSL | 15:117db924cf7c | 11846 | ret = SetNameFromCert(&cert->subject, der, derSz); |
wolfSSL | 15:117db924cf7c | 11847 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11848 | |
wolfSSL | 15:117db924cf7c | 11849 | return ret; |
wolfSSL | 15:117db924cf7c | 11850 | } |
wolfSSL | 15:117db924cf7c | 11851 | |
wolfSSL | 15:117db924cf7c | 11852 | |
wolfSSL | 15:117db924cf7c | 11853 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 11854 | |
wolfSSL | 15:117db924cf7c | 11855 | /* Set alt names from file in PEM */ |
wolfSSL | 15:117db924cf7c | 11856 | int wc_SetAltNames(Cert* cert, const char* file) |
wolfSSL | 15:117db924cf7c | 11857 | { |
wolfSSL | 15:117db924cf7c | 11858 | int ret; |
wolfSSL | 15:117db924cf7c | 11859 | int derSz; |
wolfSSL | 15:117db924cf7c | 11860 | byte* der = (byte*)XMALLOC(EIGHTK_BUF, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11861 | |
wolfSSL | 15:117db924cf7c | 11862 | if (der == NULL) { |
wolfSSL | 15:117db924cf7c | 11863 | WOLFSSL_MSG("wc_SetAltNames OOF Problem"); |
wolfSSL | 15:117db924cf7c | 11864 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 11865 | } |
wolfSSL | 15:117db924cf7c | 11866 | derSz = wc_PemCertToDer(file, der, EIGHTK_BUF); |
wolfSSL | 15:117db924cf7c | 11867 | ret = SetAltNamesFromCert(cert, der, derSz); |
wolfSSL | 15:117db924cf7c | 11868 | XFREE(der, cert->heap, DYNAMIC_TYPE_CERT); |
wolfSSL | 15:117db924cf7c | 11869 | |
wolfSSL | 15:117db924cf7c | 11870 | return ret; |
wolfSSL | 15:117db924cf7c | 11871 | } |
wolfSSL | 15:117db924cf7c | 11872 | |
wolfSSL | 15:117db924cf7c | 11873 | #endif /* WOLFSSL_ALT_NAMES */ |
wolfSSL | 15:117db924cf7c | 11874 | |
wolfSSL | 15:117db924cf7c | 11875 | #endif /* NO_FILESYSTEM */ |
wolfSSL | 15:117db924cf7c | 11876 | |
wolfSSL | 15:117db924cf7c | 11877 | /* Set cert issuer from DER buffer */ |
wolfSSL | 15:117db924cf7c | 11878 | int wc_SetIssuerBuffer(Cert* cert, const byte* der, int derSz) |
wolfSSL | 15:117db924cf7c | 11879 | { |
wolfSSL | 15:117db924cf7c | 11880 | cert->selfSigned = 0; |
wolfSSL | 15:117db924cf7c | 11881 | return SetNameFromCert(&cert->issuer, der, derSz); |
wolfSSL | 15:117db924cf7c | 11882 | } |
wolfSSL | 15:117db924cf7c | 11883 | |
wolfSSL | 15:117db924cf7c | 11884 | |
wolfSSL | 15:117db924cf7c | 11885 | /* Set cert subject from DER buffer */ |
wolfSSL | 15:117db924cf7c | 11886 | int wc_SetSubjectBuffer(Cert* cert, const byte* der, int derSz) |
wolfSSL | 15:117db924cf7c | 11887 | { |
wolfSSL | 15:117db924cf7c | 11888 | return SetNameFromCert(&cert->subject, der, derSz); |
wolfSSL | 15:117db924cf7c | 11889 | } |
wolfSSL | 15:117db924cf7c | 11890 | |
wolfSSL | 15:117db924cf7c | 11891 | |
wolfSSL | 15:117db924cf7c | 11892 | #ifdef WOLFSSL_ALT_NAMES |
wolfSSL | 15:117db924cf7c | 11893 | |
wolfSSL | 15:117db924cf7c | 11894 | /* Set cert alt names from DER buffer */ |
wolfSSL | 15:117db924cf7c | 11895 | int wc_SetAltNamesBuffer(Cert* cert, const byte* der, int derSz) |
wolfSSL | 15:117db924cf7c | 11896 | { |
wolfSSL | 15:117db924cf7c | 11897 | return SetAltNamesFromCert(cert, der, derSz); |
wolfSSL | 15:117db924cf7c | 11898 | } |
wolfSSL | 15:117db924cf7c | 11899 | |
wolfSSL | 15:117db924cf7c | 11900 | /* Set cert dates from DER buffer */ |
wolfSSL | 15:117db924cf7c | 11901 | int wc_SetDatesBuffer(Cert* cert, const byte* der, int derSz) |
wolfSSL | 15:117db924cf7c | 11902 | { |
wolfSSL | 15:117db924cf7c | 11903 | return SetDatesFromCert(cert, der, derSz); |
wolfSSL | 15:117db924cf7c | 11904 | } |
wolfSSL | 15:117db924cf7c | 11905 | |
wolfSSL | 15:117db924cf7c | 11906 | #endif /* WOLFSSL_ALT_NAMES */ |
wolfSSL | 15:117db924cf7c | 11907 | |
wolfSSL | 15:117db924cf7c | 11908 | #endif /* WOLFSSL_CERT_GEN */ |
wolfSSL | 15:117db924cf7c | 11909 | |
wolfSSL | 15:117db924cf7c | 11910 | |
wolfSSL | 15:117db924cf7c | 11911 | #ifdef HAVE_ECC |
wolfSSL | 15:117db924cf7c | 11912 | |
wolfSSL | 15:117db924cf7c | 11913 | /* Der Encode r & s ints into out, outLen is (in/out) size */ |
wolfSSL | 15:117db924cf7c | 11914 | int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r, mp_int* s) |
wolfSSL | 15:117db924cf7c | 11915 | { |
wolfSSL | 15:117db924cf7c | 11916 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 11917 | int rSz; /* encoding size */ |
wolfSSL | 15:117db924cf7c | 11918 | int sSz; |
wolfSSL | 15:117db924cf7c | 11919 | word32 headerSz = 4; /* 2*ASN_TAG + 2*LEN(ENUM) */ |
wolfSSL | 15:117db924cf7c | 11920 | |
wolfSSL | 15:117db924cf7c | 11921 | /* If the leading bit on the INTEGER is a 1, add a leading zero */ |
wolfSSL | 15:117db924cf7c | 11922 | int rLeadingZero = mp_leading_bit(r); |
wolfSSL | 15:117db924cf7c | 11923 | int sLeadingZero = mp_leading_bit(s); |
wolfSSL | 15:117db924cf7c | 11924 | int rLen = mp_unsigned_bin_size(r); /* big int size */ |
wolfSSL | 15:117db924cf7c | 11925 | int sLen = mp_unsigned_bin_size(s); |
wolfSSL | 15:117db924cf7c | 11926 | |
wolfSSL | 15:117db924cf7c | 11927 | if (*outLen < (rLen + rLeadingZero + sLen + sLeadingZero + |
wolfSSL | 15:117db924cf7c | 11928 | headerSz + 2)) /* SEQ_TAG + LEN(ENUM) */ |
wolfSSL | 15:117db924cf7c | 11929 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 11930 | |
wolfSSL | 15:117db924cf7c | 11931 | idx = SetSequence(rLen + rLeadingZero + sLen+sLeadingZero + headerSz, out); |
wolfSSL | 15:117db924cf7c | 11932 | |
wolfSSL | 15:117db924cf7c | 11933 | /* store r */ |
wolfSSL | 15:117db924cf7c | 11934 | rSz = SetASNIntMP(r, -1, &out[idx]); |
wolfSSL | 15:117db924cf7c | 11935 | if (rSz < 0) |
wolfSSL | 15:117db924cf7c | 11936 | return rSz; |
wolfSSL | 15:117db924cf7c | 11937 | idx += rSz; |
wolfSSL | 15:117db924cf7c | 11938 | |
wolfSSL | 15:117db924cf7c | 11939 | /* store s */ |
wolfSSL | 15:117db924cf7c | 11940 | sSz = SetASNIntMP(s, -1, &out[idx]); |
wolfSSL | 15:117db924cf7c | 11941 | if (sSz < 0) |
wolfSSL | 15:117db924cf7c | 11942 | return sSz; |
wolfSSL | 15:117db924cf7c | 11943 | idx += sSz; |
wolfSSL | 15:117db924cf7c | 11944 | |
wolfSSL | 15:117db924cf7c | 11945 | *outLen = idx; |
wolfSSL | 15:117db924cf7c | 11946 | |
wolfSSL | 15:117db924cf7c | 11947 | return 0; |
wolfSSL | 15:117db924cf7c | 11948 | } |
wolfSSL | 15:117db924cf7c | 11949 | |
wolfSSL | 15:117db924cf7c | 11950 | |
wolfSSL | 15:117db924cf7c | 11951 | /* Der Decode ECC-DSA Signature, r & s stored as big ints */ |
wolfSSL | 15:117db924cf7c | 11952 | int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen, mp_int* r, mp_int* s) |
wolfSSL | 15:117db924cf7c | 11953 | { |
wolfSSL | 15:117db924cf7c | 11954 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 11955 | int len = 0; |
wolfSSL | 15:117db924cf7c | 11956 | |
wolfSSL | 15:117db924cf7c | 11957 | if (GetSequence(sig, &idx, &len, sigLen) < 0) { |
wolfSSL | 15:117db924cf7c | 11958 | return ASN_ECC_KEY_E; |
wolfSSL | 15:117db924cf7c | 11959 | } |
wolfSSL | 15:117db924cf7c | 11960 | |
wolfSSL | 15:117db924cf7c | 11961 | if ((word32)len > (sigLen - idx)) { |
wolfSSL | 15:117db924cf7c | 11962 | return ASN_ECC_KEY_E; |
wolfSSL | 15:117db924cf7c | 11963 | } |
wolfSSL | 15:117db924cf7c | 11964 | |
wolfSSL | 15:117db924cf7c | 11965 | if (GetInt(r, sig, &idx, sigLen) < 0) { |
wolfSSL | 15:117db924cf7c | 11966 | return ASN_ECC_KEY_E; |
wolfSSL | 15:117db924cf7c | 11967 | } |
wolfSSL | 15:117db924cf7c | 11968 | |
wolfSSL | 15:117db924cf7c | 11969 | if (GetInt(s, sig, &idx, sigLen) < 0) { |
wolfSSL | 15:117db924cf7c | 11970 | return ASN_ECC_KEY_E; |
wolfSSL | 15:117db924cf7c | 11971 | } |
wolfSSL | 15:117db924cf7c | 11972 | |
wolfSSL | 15:117db924cf7c | 11973 | return 0; |
wolfSSL | 15:117db924cf7c | 11974 | } |
wolfSSL | 15:117db924cf7c | 11975 | |
wolfSSL | 15:117db924cf7c | 11976 | |
wolfSSL | 15:117db924cf7c | 11977 | int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, |
wolfSSL | 15:117db924cf7c | 11978 | word32 inSz) |
wolfSSL | 15:117db924cf7c | 11979 | { |
wolfSSL | 15:117db924cf7c | 11980 | word32 oidSum; |
wolfSSL | 15:117db924cf7c | 11981 | int version, length; |
wolfSSL | 15:117db924cf7c | 11982 | int privSz, pubSz = 0; |
wolfSSL | 15:117db924cf7c | 11983 | byte b; |
wolfSSL | 15:117db924cf7c | 11984 | int ret = 0; |
wolfSSL | 15:117db924cf7c | 11985 | int curve_id = ECC_CURVE_DEF; |
wolfSSL | 15:117db924cf7c | 11986 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 11987 | byte* priv; |
wolfSSL | 15:117db924cf7c | 11988 | byte* pub; |
wolfSSL | 15:117db924cf7c | 11989 | #else |
wolfSSL | 15:117db924cf7c | 11990 | byte priv[ECC_MAXSIZE+1]; |
wolfSSL | 15:117db924cf7c | 11991 | byte pub[2*(ECC_MAXSIZE+1)]; /* public key has two parts plus header */ |
wolfSSL | 15:117db924cf7c | 11992 | #endif |
wolfSSL | 15:117db924cf7c | 11993 | byte* pubData = NULL; |
wolfSSL | 15:117db924cf7c | 11994 | |
wolfSSL | 15:117db924cf7c | 11995 | if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) |
wolfSSL | 15:117db924cf7c | 11996 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 11997 | |
wolfSSL | 15:117db924cf7c | 11998 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 11999 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12000 | |
wolfSSL | 15:117db924cf7c | 12001 | if (GetMyVersion(input, inOutIdx, &version, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12002 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12003 | |
wolfSSL | 15:117db924cf7c | 12004 | if (*inOutIdx >= inSz) |
wolfSSL | 15:117db924cf7c | 12005 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12006 | |
wolfSSL | 15:117db924cf7c | 12007 | b = input[*inOutIdx]; |
wolfSSL | 15:117db924cf7c | 12008 | *inOutIdx += 1; |
wolfSSL | 15:117db924cf7c | 12009 | |
wolfSSL | 15:117db924cf7c | 12010 | /* priv type */ |
wolfSSL | 15:117db924cf7c | 12011 | if (b != 4 && b != 6 && b != 7) |
wolfSSL | 15:117db924cf7c | 12012 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12013 | |
wolfSSL | 15:117db924cf7c | 12014 | if (GetLength(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12015 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12016 | |
wolfSSL | 15:117db924cf7c | 12017 | if (length > ECC_MAXSIZE) |
wolfSSL | 15:117db924cf7c | 12018 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 12019 | |
wolfSSL | 15:117db924cf7c | 12020 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 12021 | priv = (byte*)XMALLOC(ECC_MAXSIZE+1, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12022 | if (priv == NULL) |
wolfSSL | 15:117db924cf7c | 12023 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 12024 | |
wolfSSL | 15:117db924cf7c | 12025 | pub = (byte*)XMALLOC(2*(ECC_MAXSIZE+1), key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12026 | if (pub == NULL) { |
wolfSSL | 15:117db924cf7c | 12027 | XFREE(priv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12028 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 12029 | } |
wolfSSL | 15:117db924cf7c | 12030 | #endif |
wolfSSL | 15:117db924cf7c | 12031 | |
wolfSSL | 15:117db924cf7c | 12032 | /* priv key */ |
wolfSSL | 15:117db924cf7c | 12033 | privSz = length; |
wolfSSL | 15:117db924cf7c | 12034 | XMEMCPY(priv, &input[*inOutIdx], privSz); |
wolfSSL | 15:117db924cf7c | 12035 | *inOutIdx += length; |
wolfSSL | 15:117db924cf7c | 12036 | |
wolfSSL | 15:117db924cf7c | 12037 | if (ret == 0 && (*inOutIdx + 1) < inSz) { |
wolfSSL | 15:117db924cf7c | 12038 | /* prefix 0, may have */ |
wolfSSL | 15:117db924cf7c | 12039 | b = input[*inOutIdx]; |
wolfSSL | 15:117db924cf7c | 12040 | if (b == ECC_PREFIX_0) { |
wolfSSL | 15:117db924cf7c | 12041 | *inOutIdx += 1; |
wolfSSL | 15:117db924cf7c | 12042 | |
wolfSSL | 15:117db924cf7c | 12043 | if (GetLength(input, inOutIdx, &length, inSz) <= 0) |
wolfSSL | 15:117db924cf7c | 12044 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12045 | else { |
wolfSSL | 15:117db924cf7c | 12046 | ret = GetObjectId(input, inOutIdx, &oidSum, oidIgnoreType, |
wolfSSL | 15:117db924cf7c | 12047 | inSz); |
wolfSSL | 15:117db924cf7c | 12048 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12049 | if ((ret = CheckCurve(oidSum)) < 0) |
wolfSSL | 15:117db924cf7c | 12050 | ret = ECC_CURVE_OID_E; |
wolfSSL | 15:117db924cf7c | 12051 | else { |
wolfSSL | 15:117db924cf7c | 12052 | curve_id = ret; |
wolfSSL | 15:117db924cf7c | 12053 | ret = 0; |
wolfSSL | 15:117db924cf7c | 12054 | } |
wolfSSL | 15:117db924cf7c | 12055 | } |
wolfSSL | 15:117db924cf7c | 12056 | } |
wolfSSL | 15:117db924cf7c | 12057 | } |
wolfSSL | 15:117db924cf7c | 12058 | } |
wolfSSL | 15:117db924cf7c | 12059 | |
wolfSSL | 15:117db924cf7c | 12060 | if (ret == 0 && (*inOutIdx + 1) < inSz) { |
wolfSSL | 15:117db924cf7c | 12061 | /* prefix 1 */ |
wolfSSL | 15:117db924cf7c | 12062 | b = input[*inOutIdx]; |
wolfSSL | 15:117db924cf7c | 12063 | *inOutIdx += 1; |
wolfSSL | 15:117db924cf7c | 12064 | |
wolfSSL | 15:117db924cf7c | 12065 | if (b != ECC_PREFIX_1) { |
wolfSSL | 15:117db924cf7c | 12066 | ret = ASN_ECC_KEY_E; |
wolfSSL | 15:117db924cf7c | 12067 | } |
wolfSSL | 15:117db924cf7c | 12068 | else if (GetLength(input, inOutIdx, &length, inSz) <= 0) { |
wolfSSL | 15:117db924cf7c | 12069 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12070 | } |
wolfSSL | 15:117db924cf7c | 12071 | else { |
wolfSSL | 15:117db924cf7c | 12072 | /* key header */ |
wolfSSL | 15:117db924cf7c | 12073 | ret = CheckBitString(input, inOutIdx, &length, inSz, 0, NULL); |
wolfSSL | 15:117db924cf7c | 12074 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12075 | /* pub key */ |
wolfSSL | 15:117db924cf7c | 12076 | pubSz = length; |
wolfSSL | 15:117db924cf7c | 12077 | if (pubSz < 2*(ECC_MAXSIZE+1)) { |
wolfSSL | 15:117db924cf7c | 12078 | XMEMCPY(pub, &input[*inOutIdx], pubSz); |
wolfSSL | 15:117db924cf7c | 12079 | *inOutIdx += length; |
wolfSSL | 15:117db924cf7c | 12080 | pubData = pub; |
wolfSSL | 15:117db924cf7c | 12081 | } |
wolfSSL | 15:117db924cf7c | 12082 | else |
wolfSSL | 15:117db924cf7c | 12083 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 12084 | } |
wolfSSL | 15:117db924cf7c | 12085 | } |
wolfSSL | 15:117db924cf7c | 12086 | } |
wolfSSL | 15:117db924cf7c | 12087 | |
wolfSSL | 15:117db924cf7c | 12088 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12089 | ret = wc_ecc_import_private_key_ex(priv, privSz, pubData, pubSz, key, |
wolfSSL | 15:117db924cf7c | 12090 | curve_id); |
wolfSSL | 15:117db924cf7c | 12091 | } |
wolfSSL | 15:117db924cf7c | 12092 | |
wolfSSL | 15:117db924cf7c | 12093 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 15:117db924cf7c | 12094 | XFREE(priv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12095 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12096 | #endif |
wolfSSL | 15:117db924cf7c | 12097 | |
wolfSSL | 15:117db924cf7c | 12098 | return ret; |
wolfSSL | 15:117db924cf7c | 12099 | } |
wolfSSL | 15:117db924cf7c | 12100 | |
wolfSSL | 15:117db924cf7c | 12101 | |
wolfSSL | 15:117db924cf7c | 12102 | #ifdef WOLFSSL_CUSTOM_CURVES |
wolfSSL | 15:117db924cf7c | 12103 | static void ByteToHex(byte n, char* str) |
wolfSSL | 15:117db924cf7c | 12104 | { |
wolfSSL | 15:117db924cf7c | 12105 | static const char hexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7', |
wolfSSL | 15:117db924cf7c | 12106 | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
wolfSSL | 15:117db924cf7c | 12107 | |
wolfSSL | 15:117db924cf7c | 12108 | str[0] = hexChar[n >> 4]; |
wolfSSL | 15:117db924cf7c | 12109 | str[1] = hexChar[n & 0xf]; |
wolfSSL | 15:117db924cf7c | 12110 | } |
wolfSSL | 15:117db924cf7c | 12111 | |
wolfSSL | 15:117db924cf7c | 12112 | /* returns 0 on success */ |
wolfSSL | 15:117db924cf7c | 12113 | static int ASNToHexString(const byte* input, word32* inOutIdx, char** out, |
wolfSSL | 15:117db924cf7c | 12114 | word32 inSz, void* heap, int heapType) |
wolfSSL | 15:117db924cf7c | 12115 | { |
wolfSSL | 15:117db924cf7c | 12116 | int len; |
wolfSSL | 15:117db924cf7c | 12117 | int i; |
wolfSSL | 15:117db924cf7c | 12118 | char* str; |
wolfSSL | 15:117db924cf7c | 12119 | |
wolfSSL | 15:117db924cf7c | 12120 | if (*inOutIdx >= inSz) { |
wolfSSL | 15:117db924cf7c | 12121 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 12122 | } |
wolfSSL | 15:117db924cf7c | 12123 | |
wolfSSL | 15:117db924cf7c | 12124 | if (input[*inOutIdx] == ASN_INTEGER) { |
wolfSSL | 15:117db924cf7c | 12125 | if (GetASNInt(input, inOutIdx, &len, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12126 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12127 | } |
wolfSSL | 15:117db924cf7c | 12128 | else { |
wolfSSL | 15:117db924cf7c | 12129 | if (GetOctetString(input, inOutIdx, &len, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12130 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12131 | } |
wolfSSL | 15:117db924cf7c | 12132 | |
wolfSSL | 15:117db924cf7c | 12133 | str = (char*)XMALLOC(len * 2 + 1, heap, heapType); |
wolfSSL | 15:117db924cf7c | 12134 | for (i=0; i<len; i++) |
wolfSSL | 15:117db924cf7c | 12135 | ByteToHex(input[*inOutIdx + i], str + i*2); |
wolfSSL | 15:117db924cf7c | 12136 | str[len*2] = '\0'; |
wolfSSL | 15:117db924cf7c | 12137 | |
wolfSSL | 15:117db924cf7c | 12138 | *inOutIdx += len; |
wolfSSL | 15:117db924cf7c | 12139 | *out = str; |
wolfSSL | 15:117db924cf7c | 12140 | |
wolfSSL | 15:117db924cf7c | 12141 | return 0; |
wolfSSL | 15:117db924cf7c | 12142 | } |
wolfSSL | 15:117db924cf7c | 12143 | #endif |
wolfSSL | 15:117db924cf7c | 12144 | |
wolfSSL | 15:117db924cf7c | 12145 | int wc_EccPublicKeyDecode(const byte* input, word32* inOutIdx, |
wolfSSL | 15:117db924cf7c | 12146 | ecc_key* key, word32 inSz) |
wolfSSL | 15:117db924cf7c | 12147 | { |
wolfSSL | 15:117db924cf7c | 12148 | int length; |
wolfSSL | 15:117db924cf7c | 12149 | int ret; |
wolfSSL | 15:117db924cf7c | 12150 | int curve_id = ECC_CURVE_DEF; |
wolfSSL | 15:117db924cf7c | 12151 | word32 oidSum; |
wolfSSL | 15:117db924cf7c | 12152 | |
wolfSSL | 15:117db924cf7c | 12153 | if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) |
wolfSSL | 15:117db924cf7c | 12154 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 12155 | |
wolfSSL | 15:117db924cf7c | 12156 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12157 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12158 | |
wolfSSL | 15:117db924cf7c | 12159 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12160 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12161 | |
wolfSSL | 15:117db924cf7c | 12162 | ret = SkipObjectId(input, inOutIdx, inSz); |
wolfSSL | 15:117db924cf7c | 12163 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 12164 | return ret; |
wolfSSL | 15:117db924cf7c | 12165 | |
wolfSSL | 15:117db924cf7c | 12166 | if (*inOutIdx >= inSz) { |
wolfSSL | 15:117db924cf7c | 12167 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 12168 | } |
wolfSSL | 15:117db924cf7c | 12169 | |
wolfSSL | 15:117db924cf7c | 12170 | if (input[*inOutIdx] == (ASN_SEQUENCE | ASN_CONSTRUCTED)) { |
wolfSSL | 15:117db924cf7c | 12171 | #ifdef WOLFSSL_CUSTOM_CURVES |
wolfSSL | 15:117db924cf7c | 12172 | ecc_set_type* curve; |
wolfSSL | 15:117db924cf7c | 12173 | int len; |
wolfSSL | 15:117db924cf7c | 12174 | char* point; |
wolfSSL | 15:117db924cf7c | 12175 | |
wolfSSL | 15:117db924cf7c | 12176 | ret = 0; |
wolfSSL | 15:117db924cf7c | 12177 | |
wolfSSL | 15:117db924cf7c | 12178 | curve = (ecc_set_type*)XMALLOC(sizeof(*curve), key->heap, |
wolfSSL | 15:117db924cf7c | 12179 | DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12180 | if (curve == NULL) |
wolfSSL | 15:117db924cf7c | 12181 | ret = MEMORY_E; |
wolfSSL | 15:117db924cf7c | 12182 | |
wolfSSL | 15:117db924cf7c | 12183 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12184 | XMEMSET(curve, 0, sizeof(*curve)); |
wolfSSL | 15:117db924cf7c | 12185 | curve->name = "Custom"; |
wolfSSL | 15:117db924cf7c | 12186 | curve->id = ECC_CURVE_CUSTOM; |
wolfSSL | 15:117db924cf7c | 12187 | |
wolfSSL | 15:117db924cf7c | 12188 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12189 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12190 | } |
wolfSSL | 15:117db924cf7c | 12191 | |
wolfSSL | 15:117db924cf7c | 12192 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12193 | GetInteger7Bit(input, inOutIdx, inSz); |
wolfSSL | 15:117db924cf7c | 12194 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12195 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12196 | } |
wolfSSL | 15:117db924cf7c | 12197 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12198 | SkipObjectId(input, inOutIdx, inSz); |
wolfSSL | 15:117db924cf7c | 12199 | ret = ASNToHexString(input, inOutIdx, (char**)&curve->prime, inSz, |
wolfSSL | 15:117db924cf7c | 12200 | key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12201 | } |
wolfSSL | 15:117db924cf7c | 12202 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12203 | curve->size = (int)XSTRLEN(curve->prime) / 2; |
wolfSSL | 15:117db924cf7c | 12204 | |
wolfSSL | 15:117db924cf7c | 12205 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12206 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12207 | } |
wolfSSL | 15:117db924cf7c | 12208 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12209 | ret = ASNToHexString(input, inOutIdx, (char**)&curve->Af, inSz, |
wolfSSL | 15:117db924cf7c | 12210 | key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12211 | } |
wolfSSL | 15:117db924cf7c | 12212 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12213 | ret = ASNToHexString(input, inOutIdx, (char**)&curve->Bf, inSz, |
wolfSSL | 15:117db924cf7c | 12214 | key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12215 | } |
wolfSSL | 15:117db924cf7c | 12216 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12217 | if (*inOutIdx < inSz && input[*inOutIdx] == ASN_BIT_STRING) { |
wolfSSL | 15:117db924cf7c | 12218 | len = 0; |
wolfSSL | 15:117db924cf7c | 12219 | ret = GetASNHeader(input, ASN_BIT_STRING, inOutIdx, &len, inSz); |
wolfSSL | 15:117db924cf7c | 12220 | *inOutIdx += len; |
wolfSSL | 15:117db924cf7c | 12221 | } |
wolfSSL | 15:117db924cf7c | 12222 | } |
wolfSSL | 15:117db924cf7c | 12223 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12224 | ret = ASNToHexString(input, inOutIdx, (char**)&point, inSz, |
wolfSSL | 15:117db924cf7c | 12225 | key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12226 | |
wolfSSL | 15:117db924cf7c | 12227 | /* sanity check that point buffer is not smaller than the expected |
wolfSSL | 15:117db924cf7c | 12228 | * size to hold ( 0 4 || Gx || Gy ) |
wolfSSL | 15:117db924cf7c | 12229 | * where Gx and Gy are each the size of curve->size * 2 */ |
wolfSSL | 15:117db924cf7c | 12230 | if (ret == 0 && (int)XSTRLEN(point) < (curve->size * 4) + 2) { |
wolfSSL | 15:117db924cf7c | 12231 | XFREE(point, key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12232 | ret = BUFFER_E; |
wolfSSL | 15:117db924cf7c | 12233 | } |
wolfSSL | 15:117db924cf7c | 12234 | } |
wolfSSL | 15:117db924cf7c | 12235 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12236 | curve->Gx = (const char*)XMALLOC(curve->size * 2 + 2, key->heap, |
wolfSSL | 15:117db924cf7c | 12237 | DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12238 | curve->Gy = (const char*)XMALLOC(curve->size * 2 + 2, key->heap, |
wolfSSL | 15:117db924cf7c | 12239 | DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12240 | if (curve->Gx == NULL || curve->Gy == NULL) { |
wolfSSL | 15:117db924cf7c | 12241 | XFREE(point, key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12242 | ret = MEMORY_E; |
wolfSSL | 15:117db924cf7c | 12243 | } |
wolfSSL | 15:117db924cf7c | 12244 | } |
wolfSSL | 15:117db924cf7c | 12245 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12246 | XMEMCPY((char*)curve->Gx, point + 2, curve->size * 2); |
wolfSSL | 15:117db924cf7c | 12247 | XMEMCPY((char*)curve->Gy, point + curve->size * 2 + 2, |
wolfSSL | 15:117db924cf7c | 12248 | curve->size * 2); |
wolfSSL | 15:117db924cf7c | 12249 | ((char*)curve->Gx)[curve->size * 2] = '\0'; |
wolfSSL | 15:117db924cf7c | 12250 | ((char*)curve->Gy)[curve->size * 2] = '\0'; |
wolfSSL | 15:117db924cf7c | 12251 | XFREE(point, key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12252 | ret = ASNToHexString(input, inOutIdx, (char**)&curve->order, inSz, |
wolfSSL | 15:117db924cf7c | 12253 | key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
wolfSSL | 15:117db924cf7c | 12254 | } |
wolfSSL | 15:117db924cf7c | 12255 | if (ret == 0) { |
wolfSSL | 15:117db924cf7c | 12256 | curve->cofactor = GetInteger7Bit(input, inOutIdx, inSz); |
wolfSSL | 15:117db924cf7c | 12257 | |
wolfSSL | 15:117db924cf7c | 12258 | curve->oid = NULL; |
wolfSSL | 15:117db924cf7c | 12259 | curve->oidSz = 0; |
wolfSSL | 15:117db924cf7c | 12260 | curve->oidSum = 0; |
wolfSSL | 15:117db924cf7c | 12261 | |
wolfSSL | 15:117db924cf7c | 12262 | if (wc_ecc_set_custom_curve(key, curve) < 0) { |
wolfSSL | 15:117db924cf7c | 12263 | ret = ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12264 | } |
wolfSSL | 15:117db924cf7c | 12265 | key->deallocSet = 1; |
wolfSSL | 15:117db924cf7c | 12266 | curve = NULL; |
wolfSSL | 15:117db924cf7c | 12267 | } |
wolfSSL | 15:117db924cf7c | 12268 | if (curve != NULL) |
wolfSSL | 15:117db924cf7c | 12269 | wc_ecc_free_curve(curve, key->heap); |
wolfSSL | 15:117db924cf7c | 12270 | |
wolfSSL | 15:117db924cf7c | 12271 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 12272 | return ret; |
wolfSSL | 15:117db924cf7c | 12273 | #else |
wolfSSL | 15:117db924cf7c | 12274 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12275 | #endif |
wolfSSL | 15:117db924cf7c | 12276 | } |
wolfSSL | 15:117db924cf7c | 12277 | else { |
wolfSSL | 15:117db924cf7c | 12278 | /* ecc params information */ |
wolfSSL | 15:117db924cf7c | 12279 | ret = GetObjectId(input, inOutIdx, &oidSum, oidIgnoreType, inSz); |
wolfSSL | 15:117db924cf7c | 12280 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 12281 | return ret; |
wolfSSL | 15:117db924cf7c | 12282 | |
wolfSSL | 15:117db924cf7c | 12283 | /* get curve id */ |
wolfSSL | 15:117db924cf7c | 12284 | curve_id = wc_ecc_get_oid(oidSum, NULL, 0); |
wolfSSL | 15:117db924cf7c | 12285 | if (curve_id < 0) |
wolfSSL | 15:117db924cf7c | 12286 | return ECC_CURVE_OID_E; |
wolfSSL | 15:117db924cf7c | 12287 | } |
wolfSSL | 15:117db924cf7c | 12288 | |
wolfSSL | 15:117db924cf7c | 12289 | /* key header */ |
wolfSSL | 15:117db924cf7c | 12290 | ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL); |
wolfSSL | 15:117db924cf7c | 12291 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 12292 | return ret; |
wolfSSL | 15:117db924cf7c | 12293 | |
wolfSSL | 15:117db924cf7c | 12294 | /* This is the raw point data compressed or uncompressed. */ |
wolfSSL | 15:117db924cf7c | 12295 | if (wc_ecc_import_x963_ex(input + *inOutIdx, inSz - *inOutIdx, key, |
wolfSSL | 15:117db924cf7c | 12296 | curve_id) != 0) { |
wolfSSL | 15:117db924cf7c | 12297 | return ASN_ECC_KEY_E; |
wolfSSL | 15:117db924cf7c | 12298 | } |
wolfSSL | 15:117db924cf7c | 12299 | |
wolfSSL | 15:117db924cf7c | 12300 | return 0; |
wolfSSL | 15:117db924cf7c | 12301 | } |
wolfSSL | 15:117db924cf7c | 12302 | |
wolfSSL | 15:117db924cf7c | 12303 | |
wolfSSL | 15:117db924cf7c | 12304 | /* build DER formatted ECC key, include optional public key if requested, |
wolfSSL | 15:117db924cf7c | 12305 | * return length on success, negative on error */ |
wolfSSL | 15:117db924cf7c | 12306 | static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen, |
wolfSSL | 15:117db924cf7c | 12307 | int pubIn) |
wolfSSL | 15:117db924cf7c | 12308 | { |
wolfSSL | 15:117db924cf7c | 12309 | byte curve[MAX_ALGO_SZ+2]; |
wolfSSL | 15:117db924cf7c | 12310 | byte ver[MAX_VERSION_SZ]; |
wolfSSL | 15:117db924cf7c | 12311 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 12312 | byte *prv = NULL, *pub = NULL; |
wolfSSL | 15:117db924cf7c | 12313 | int ret, totalSz, curveSz, verSz; |
wolfSSL | 15:117db924cf7c | 12314 | int privHdrSz = ASN_ECC_HEADER_SZ; |
wolfSSL | 15:117db924cf7c | 12315 | int pubHdrSz = ASN_ECC_CONTEXT_SZ + ASN_ECC_HEADER_SZ; |
wolfSSL | 15:117db924cf7c | 12316 | |
wolfSSL | 15:117db924cf7c | 12317 | word32 idx = 0, prvidx = 0, pubidx = 0, curveidx = 0; |
wolfSSL | 15:117db924cf7c | 12318 | word32 seqSz, privSz, pubSz = ECC_BUFSIZE; |
wolfSSL | 15:117db924cf7c | 12319 | |
wolfSSL | 15:117db924cf7c | 12320 | if (key == NULL || output == NULL || inLen == 0) |
wolfSSL | 15:117db924cf7c | 12321 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 12322 | |
wolfSSL | 15:117db924cf7c | 12323 | /* curve */ |
wolfSSL | 15:117db924cf7c | 12324 | curve[curveidx++] = ECC_PREFIX_0; |
wolfSSL | 15:117db924cf7c | 12325 | curveidx++ /* to put the size after computation */; |
wolfSSL | 15:117db924cf7c | 12326 | curveSz = SetCurve(key, curve+curveidx); |
wolfSSL | 15:117db924cf7c | 12327 | if (curveSz < 0) |
wolfSSL | 15:117db924cf7c | 12328 | return curveSz; |
wolfSSL | 15:117db924cf7c | 12329 | /* set computed size */ |
wolfSSL | 15:117db924cf7c | 12330 | curve[1] = (byte)curveSz; |
wolfSSL | 15:117db924cf7c | 12331 | curveidx += curveSz; |
wolfSSL | 15:117db924cf7c | 12332 | |
wolfSSL | 15:117db924cf7c | 12333 | /* private */ |
wolfSSL | 15:117db924cf7c | 12334 | privSz = key->dp->size; |
wolfSSL | 15:117db924cf7c | 12335 | prv = (byte*)XMALLOC(privSz + privHdrSz + MAX_SEQ_SZ, |
wolfSSL | 15:117db924cf7c | 12336 | key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12337 | if (prv == NULL) { |
wolfSSL | 15:117db924cf7c | 12338 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 12339 | } |
wolfSSL | 15:117db924cf7c | 12340 | prvidx += SetOctetString8Bit(key->dp->size, &prv[prvidx]); |
wolfSSL | 15:117db924cf7c | 12341 | ret = wc_ecc_export_private_only(key, prv + prvidx, &privSz); |
wolfSSL | 15:117db924cf7c | 12342 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 12343 | XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12344 | return ret; |
wolfSSL | 15:117db924cf7c | 12345 | } |
wolfSSL | 15:117db924cf7c | 12346 | prvidx += privSz; |
wolfSSL | 15:117db924cf7c | 12347 | |
wolfSSL | 15:117db924cf7c | 12348 | /* pubIn */ |
wolfSSL | 15:117db924cf7c | 12349 | if (pubIn) { |
wolfSSL | 15:117db924cf7c | 12350 | ret = wc_ecc_export_x963(key, NULL, &pubSz); |
wolfSSL | 15:117db924cf7c | 12351 | if (ret != LENGTH_ONLY_E) { |
wolfSSL | 15:117db924cf7c | 12352 | XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12353 | return ret; |
wolfSSL | 15:117db924cf7c | 12354 | } |
wolfSSL | 15:117db924cf7c | 12355 | |
wolfSSL | 15:117db924cf7c | 12356 | pub = (byte*)XMALLOC(pubSz + pubHdrSz + MAX_SEQ_SZ, |
wolfSSL | 15:117db924cf7c | 12357 | key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12358 | if (pub == NULL) { |
wolfSSL | 15:117db924cf7c | 12359 | XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12360 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 12361 | } |
wolfSSL | 15:117db924cf7c | 12362 | |
wolfSSL | 15:117db924cf7c | 12363 | pub[pubidx++] = ECC_PREFIX_1; |
wolfSSL | 15:117db924cf7c | 12364 | if (pubSz > 128) /* leading zero + extra size byte */ |
wolfSSL | 15:117db924cf7c | 12365 | pubidx += SetLength(pubSz + ASN_ECC_CONTEXT_SZ + 2, pub+pubidx); |
wolfSSL | 15:117db924cf7c | 12366 | else /* leading zero */ |
wolfSSL | 15:117db924cf7c | 12367 | pubidx += SetLength(pubSz + ASN_ECC_CONTEXT_SZ + 1, pub+pubidx); |
wolfSSL | 15:117db924cf7c | 12368 | |
wolfSSL | 15:117db924cf7c | 12369 | /* SetBitString adds leading zero */ |
wolfSSL | 15:117db924cf7c | 12370 | pubidx += SetBitString(pubSz, 0, pub + pubidx); |
wolfSSL | 15:117db924cf7c | 12371 | ret = wc_ecc_export_x963(key, pub + pubidx, &pubSz); |
wolfSSL | 15:117db924cf7c | 12372 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 12373 | XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12374 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12375 | return ret; |
wolfSSL | 15:117db924cf7c | 12376 | } |
wolfSSL | 15:117db924cf7c | 12377 | pubidx += pubSz; |
wolfSSL | 15:117db924cf7c | 12378 | } |
wolfSSL | 15:117db924cf7c | 12379 | |
wolfSSL | 15:117db924cf7c | 12380 | /* make headers */ |
wolfSSL | 15:117db924cf7c | 12381 | verSz = SetMyVersion(1, ver, FALSE); |
wolfSSL | 15:117db924cf7c | 12382 | seqSz = SetSequence(verSz + prvidx + pubidx + curveidx, seq); |
wolfSSL | 15:117db924cf7c | 12383 | |
wolfSSL | 15:117db924cf7c | 12384 | totalSz = prvidx + pubidx + curveidx + verSz + seqSz; |
wolfSSL | 15:117db924cf7c | 12385 | if (totalSz > (int)inLen) { |
wolfSSL | 15:117db924cf7c | 12386 | XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12387 | if (pubIn) { |
wolfSSL | 15:117db924cf7c | 12388 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12389 | } |
wolfSSL | 15:117db924cf7c | 12390 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 12391 | } |
wolfSSL | 15:117db924cf7c | 12392 | |
wolfSSL | 15:117db924cf7c | 12393 | /* write out */ |
wolfSSL | 15:117db924cf7c | 12394 | /* seq */ |
wolfSSL | 15:117db924cf7c | 12395 | XMEMCPY(output + idx, seq, seqSz); |
wolfSSL | 15:117db924cf7c | 12396 | idx = seqSz; |
wolfSSL | 15:117db924cf7c | 12397 | |
wolfSSL | 15:117db924cf7c | 12398 | /* ver */ |
wolfSSL | 15:117db924cf7c | 12399 | XMEMCPY(output + idx, ver, verSz); |
wolfSSL | 15:117db924cf7c | 12400 | idx += verSz; |
wolfSSL | 15:117db924cf7c | 12401 | |
wolfSSL | 15:117db924cf7c | 12402 | /* private */ |
wolfSSL | 15:117db924cf7c | 12403 | XMEMCPY(output + idx, prv, prvidx); |
wolfSSL | 15:117db924cf7c | 12404 | idx += prvidx; |
wolfSSL | 15:117db924cf7c | 12405 | XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12406 | |
wolfSSL | 15:117db924cf7c | 12407 | /* curve */ |
wolfSSL | 15:117db924cf7c | 12408 | XMEMCPY(output + idx, curve, curveidx); |
wolfSSL | 15:117db924cf7c | 12409 | idx += curveidx; |
wolfSSL | 15:117db924cf7c | 12410 | |
wolfSSL | 15:117db924cf7c | 12411 | /* pubIn */ |
wolfSSL | 15:117db924cf7c | 12412 | if (pubIn) { |
wolfSSL | 15:117db924cf7c | 12413 | XMEMCPY(output + idx, pub, pubidx); |
wolfSSL | 15:117db924cf7c | 12414 | /* idx += pubidx; not used after write, if more data remove comment */ |
wolfSSL | 15:117db924cf7c | 12415 | XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12416 | } |
wolfSSL | 15:117db924cf7c | 12417 | |
wolfSSL | 15:117db924cf7c | 12418 | return totalSz; |
wolfSSL | 15:117db924cf7c | 12419 | } |
wolfSSL | 15:117db924cf7c | 12420 | |
wolfSSL | 15:117db924cf7c | 12421 | |
wolfSSL | 15:117db924cf7c | 12422 | /* Write a Private ecc key, including public to DER format, |
wolfSSL | 15:117db924cf7c | 12423 | * length on success else < 0 */ |
wolfSSL | 15:117db924cf7c | 12424 | int wc_EccKeyToDer(ecc_key* key, byte* output, word32 inLen) |
wolfSSL | 15:117db924cf7c | 12425 | { |
wolfSSL | 15:117db924cf7c | 12426 | return wc_BuildEccKeyDer(key, output, inLen, 1); |
wolfSSL | 15:117db924cf7c | 12427 | } |
wolfSSL | 15:117db924cf7c | 12428 | |
wolfSSL | 15:117db924cf7c | 12429 | |
wolfSSL | 15:117db924cf7c | 12430 | /* Write only private ecc key to DER format, |
wolfSSL | 15:117db924cf7c | 12431 | * length on success else < 0 */ |
wolfSSL | 15:117db924cf7c | 12432 | int wc_EccPrivateKeyToDer(ecc_key* key, byte* output, word32 inLen) |
wolfSSL | 15:117db924cf7c | 12433 | { |
wolfSSL | 15:117db924cf7c | 12434 | return wc_BuildEccKeyDer(key, output, inLen, 0); |
wolfSSL | 15:117db924cf7c | 12435 | } |
wolfSSL | 15:117db924cf7c | 12436 | |
wolfSSL | 15:117db924cf7c | 12437 | /* Write only private ecc key to unencrypted PKCS#8 format. |
wolfSSL | 15:117db924cf7c | 12438 | * |
wolfSSL | 15:117db924cf7c | 12439 | * If output is NULL, places required PKCS#8 buffer size in outLen and |
wolfSSL | 15:117db924cf7c | 12440 | * returns LENGTH_ONLY_E. |
wolfSSL | 15:117db924cf7c | 12441 | * |
wolfSSL | 15:117db924cf7c | 12442 | * return length on success else < 0 */ |
wolfSSL | 15:117db924cf7c | 12443 | int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen) |
wolfSSL | 15:117db924cf7c | 12444 | { |
wolfSSL | 15:117db924cf7c | 12445 | int ret, tmpDerSz; |
wolfSSL | 15:117db924cf7c | 12446 | int algoID = 0; |
wolfSSL | 15:117db924cf7c | 12447 | word32 oidSz = 0; |
wolfSSL | 15:117db924cf7c | 12448 | word32 pkcs8Sz = 0; |
wolfSSL | 15:117db924cf7c | 12449 | const byte* curveOID = NULL; |
wolfSSL | 15:117db924cf7c | 12450 | byte* tmpDer = NULL; |
wolfSSL | 15:117db924cf7c | 12451 | |
wolfSSL | 15:117db924cf7c | 12452 | if (key == NULL || outLen == NULL) |
wolfSSL | 15:117db924cf7c | 12453 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 12454 | |
wolfSSL | 15:117db924cf7c | 12455 | /* set algoID, get curve OID */ |
wolfSSL | 15:117db924cf7c | 12456 | algoID = ECDSAk; |
wolfSSL | 15:117db924cf7c | 12457 | ret = wc_ecc_get_oid(key->dp->oidSum, &curveOID, &oidSz); |
wolfSSL | 15:117db924cf7c | 12458 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 12459 | return ret; |
wolfSSL | 15:117db924cf7c | 12460 | |
wolfSSL | 15:117db924cf7c | 12461 | /* temp buffer for plain DER key */ |
wolfSSL | 15:117db924cf7c | 12462 | tmpDer = (byte*)XMALLOC(ECC_BUFSIZE, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12463 | if (tmpDer == NULL) |
wolfSSL | 15:117db924cf7c | 12464 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 12465 | |
wolfSSL | 15:117db924cf7c | 12466 | XMEMSET(tmpDer, 0, ECC_BUFSIZE); |
wolfSSL | 15:117db924cf7c | 12467 | |
wolfSSL | 15:117db924cf7c | 12468 | tmpDerSz = wc_BuildEccKeyDer(key, tmpDer, ECC_BUFSIZE, 0); |
wolfSSL | 15:117db924cf7c | 12469 | if (tmpDerSz < 0) { |
wolfSSL | 15:117db924cf7c | 12470 | XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12471 | return tmpDerSz; |
wolfSSL | 15:117db924cf7c | 12472 | } |
wolfSSL | 15:117db924cf7c | 12473 | |
wolfSSL | 15:117db924cf7c | 12474 | /* get pkcs8 expected output size */ |
wolfSSL | 15:117db924cf7c | 12475 | ret = wc_CreatePKCS8Key(NULL, &pkcs8Sz, tmpDer, tmpDerSz, algoID, |
wolfSSL | 15:117db924cf7c | 12476 | curveOID, oidSz); |
wolfSSL | 15:117db924cf7c | 12477 | if (ret != LENGTH_ONLY_E) { |
wolfSSL | 15:117db924cf7c | 12478 | XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12479 | return ret; |
wolfSSL | 15:117db924cf7c | 12480 | } |
wolfSSL | 15:117db924cf7c | 12481 | |
wolfSSL | 15:117db924cf7c | 12482 | if (output == NULL) { |
wolfSSL | 15:117db924cf7c | 12483 | XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12484 | *outLen = pkcs8Sz; |
wolfSSL | 15:117db924cf7c | 12485 | return LENGTH_ONLY_E; |
wolfSSL | 15:117db924cf7c | 12486 | |
wolfSSL | 15:117db924cf7c | 12487 | } else if (*outLen < pkcs8Sz) { |
wolfSSL | 15:117db924cf7c | 12488 | XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12489 | WOLFSSL_MSG("Input buffer too small for ECC PKCS#8 key"); |
wolfSSL | 15:117db924cf7c | 12490 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 12491 | } |
wolfSSL | 15:117db924cf7c | 12492 | |
wolfSSL | 15:117db924cf7c | 12493 | ret = wc_CreatePKCS8Key(output, &pkcs8Sz, tmpDer, tmpDerSz, |
wolfSSL | 15:117db924cf7c | 12494 | algoID, curveOID, oidSz); |
wolfSSL | 15:117db924cf7c | 12495 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 12496 | XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12497 | return ret; |
wolfSSL | 15:117db924cf7c | 12498 | } |
wolfSSL | 15:117db924cf7c | 12499 | |
wolfSSL | 15:117db924cf7c | 12500 | XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 15:117db924cf7c | 12501 | |
wolfSSL | 15:117db924cf7c | 12502 | *outLen = ret; |
wolfSSL | 15:117db924cf7c | 12503 | return ret; |
wolfSSL | 15:117db924cf7c | 12504 | } |
wolfSSL | 15:117db924cf7c | 12505 | |
wolfSSL | 15:117db924cf7c | 12506 | #endif /* HAVE_ECC */ |
wolfSSL | 15:117db924cf7c | 12507 | |
wolfSSL | 15:117db924cf7c | 12508 | |
wolfSSL | 15:117db924cf7c | 12509 | #ifdef HAVE_ED25519 |
wolfSSL | 15:117db924cf7c | 12510 | |
wolfSSL | 15:117db924cf7c | 12511 | int wc_Ed25519PrivateKeyDecode(const byte* input, word32* inOutIdx, |
wolfSSL | 15:117db924cf7c | 12512 | ed25519_key* key, word32 inSz) |
wolfSSL | 15:117db924cf7c | 12513 | { |
wolfSSL | 15:117db924cf7c | 12514 | word32 oid; |
wolfSSL | 15:117db924cf7c | 12515 | int ret, version, length, endKeyIdx, privSz, pubSz; |
wolfSSL | 15:117db924cf7c | 12516 | const byte* priv; |
wolfSSL | 15:117db924cf7c | 12517 | const byte* pub; |
wolfSSL | 15:117db924cf7c | 12518 | |
wolfSSL | 15:117db924cf7c | 12519 | if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) |
wolfSSL | 15:117db924cf7c | 12520 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 12521 | |
wolfSSL | 15:117db924cf7c | 12522 | if (GetSequence(input, inOutIdx, &length, inSz) >= 0) { |
wolfSSL | 15:117db924cf7c | 12523 | endKeyIdx = *inOutIdx + length; |
wolfSSL | 15:117db924cf7c | 12524 | |
wolfSSL | 15:117db924cf7c | 12525 | if (GetMyVersion(input, inOutIdx, &version, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12526 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12527 | if (version != 0) { |
wolfSSL | 15:117db924cf7c | 12528 | WOLFSSL_MSG("Unrecognized version of ED25519 private key"); |
wolfSSL | 15:117db924cf7c | 12529 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12530 | } |
wolfSSL | 15:117db924cf7c | 12531 | |
wolfSSL | 15:117db924cf7c | 12532 | if (GetAlgoId(input, inOutIdx, &oid, oidKeyType, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12533 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12534 | if (oid != ED25519k) |
wolfSSL | 15:117db924cf7c | 12535 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12536 | |
wolfSSL | 15:117db924cf7c | 12537 | if (GetOctetString(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12538 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12539 | |
wolfSSL | 15:117db924cf7c | 12540 | if (GetOctetString(input, inOutIdx, &privSz, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12541 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12542 | |
wolfSSL | 15:117db924cf7c | 12543 | priv = input + *inOutIdx; |
wolfSSL | 15:117db924cf7c | 12544 | *inOutIdx += privSz; |
wolfSSL | 15:117db924cf7c | 12545 | } |
wolfSSL | 15:117db924cf7c | 12546 | else { |
wolfSSL | 15:117db924cf7c | 12547 | if (GetOctetString(input, inOutIdx, &privSz, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12548 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12549 | |
wolfSSL | 15:117db924cf7c | 12550 | priv = input + *inOutIdx; |
wolfSSL | 15:117db924cf7c | 12551 | *inOutIdx += privSz; |
wolfSSL | 15:117db924cf7c | 12552 | endKeyIdx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 12553 | } |
wolfSSL | 15:117db924cf7c | 12554 | |
wolfSSL | 15:117db924cf7c | 12555 | if (endKeyIdx == (int)*inOutIdx) { |
wolfSSL | 15:117db924cf7c | 12556 | ret = wc_ed25519_import_private_only(priv, privSz, key); |
wolfSSL | 15:117db924cf7c | 12557 | } |
wolfSSL | 15:117db924cf7c | 12558 | else { |
wolfSSL | 15:117db924cf7c | 12559 | if (GetASNHeader(input, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1, |
wolfSSL | 15:117db924cf7c | 12560 | inOutIdx, &length, inSz) < 0) { |
wolfSSL | 15:117db924cf7c | 12561 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12562 | } |
wolfSSL | 15:117db924cf7c | 12563 | if (GetOctetString(input, inOutIdx, &pubSz, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12564 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12565 | pub = input + *inOutIdx; |
wolfSSL | 15:117db924cf7c | 12566 | *inOutIdx += pubSz; |
wolfSSL | 15:117db924cf7c | 12567 | |
wolfSSL | 15:117db924cf7c | 12568 | ret = wc_ed25519_import_private_key(priv, privSz, pub, pubSz, key); |
wolfSSL | 15:117db924cf7c | 12569 | } |
wolfSSL | 15:117db924cf7c | 12570 | if (ret == 0 && endKeyIdx != (int)*inOutIdx) |
wolfSSL | 15:117db924cf7c | 12571 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12572 | |
wolfSSL | 15:117db924cf7c | 12573 | return ret; |
wolfSSL | 15:117db924cf7c | 12574 | } |
wolfSSL | 15:117db924cf7c | 12575 | |
wolfSSL | 15:117db924cf7c | 12576 | |
wolfSSL | 15:117db924cf7c | 12577 | int wc_Ed25519PublicKeyDecode(const byte* input, word32* inOutIdx, |
wolfSSL | 15:117db924cf7c | 12578 | ed25519_key* key, word32 inSz) |
wolfSSL | 15:117db924cf7c | 12579 | { |
wolfSSL | 15:117db924cf7c | 12580 | int length; |
wolfSSL | 15:117db924cf7c | 12581 | int ret; |
wolfSSL | 15:117db924cf7c | 12582 | |
wolfSSL | 15:117db924cf7c | 12583 | if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) |
wolfSSL | 15:117db924cf7c | 12584 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 12585 | |
wolfSSL | 15:117db924cf7c | 12586 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12587 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12588 | |
wolfSSL | 15:117db924cf7c | 12589 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 15:117db924cf7c | 12590 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12591 | |
wolfSSL | 15:117db924cf7c | 12592 | ret = SkipObjectId(input, inOutIdx, inSz); |
wolfSSL | 15:117db924cf7c | 12593 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 12594 | return ret; |
wolfSSL | 15:117db924cf7c | 12595 | |
wolfSSL | 15:117db924cf7c | 12596 | /* key header */ |
wolfSSL | 15:117db924cf7c | 12597 | ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL); |
wolfSSL | 15:117db924cf7c | 12598 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 12599 | return ret; |
wolfSSL | 15:117db924cf7c | 12600 | |
wolfSSL | 15:117db924cf7c | 12601 | /* This is the raw point data compressed or uncompressed. */ |
wolfSSL | 15:117db924cf7c | 12602 | if (wc_ed25519_import_public(input + *inOutIdx, inSz - *inOutIdx, key) != 0) |
wolfSSL | 15:117db924cf7c | 12603 | return ASN_ECC_KEY_E; |
wolfSSL | 15:117db924cf7c | 12604 | |
wolfSSL | 15:117db924cf7c | 12605 | return 0; |
wolfSSL | 15:117db924cf7c | 12606 | } |
wolfSSL | 15:117db924cf7c | 12607 | |
wolfSSL | 15:117db924cf7c | 12608 | |
wolfSSL | 15:117db924cf7c | 12609 | #ifdef WOLFSSL_KEY_GEN |
wolfSSL | 15:117db924cf7c | 12610 | |
wolfSSL | 15:117db924cf7c | 12611 | /* build DER formatted ED25519 key, |
wolfSSL | 15:117db924cf7c | 12612 | * return length on success, negative on error */ |
wolfSSL | 15:117db924cf7c | 12613 | static int wc_BuildEd25519KeyDer(ed25519_key* key, byte* output, word32 inLen, |
wolfSSL | 15:117db924cf7c | 12614 | int pubOut) |
wolfSSL | 15:117db924cf7c | 12615 | { |
wolfSSL | 15:117db924cf7c | 12616 | byte algoArray[MAX_ALGO_SZ]; |
wolfSSL | 15:117db924cf7c | 12617 | byte ver[MAX_VERSION_SZ]; |
wolfSSL | 15:117db924cf7c | 12618 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 12619 | int ret; |
wolfSSL | 15:117db924cf7c | 12620 | word32 idx = 0, seqSz, verSz, algoSz, privSz, pubSz = 0; |
wolfSSL | 15:117db924cf7c | 12621 | |
wolfSSL | 15:117db924cf7c | 12622 | if (key == NULL || output == NULL || inLen == 0) |
wolfSSL | 15:117db924cf7c | 12623 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 12624 | |
wolfSSL | 15:117db924cf7c | 12625 | if (pubOut) |
wolfSSL | 15:117db924cf7c | 12626 | pubSz = 2 + 2 + ED25519_PUB_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 12627 | privSz = 2 + 2 + ED25519_KEY_SIZE; |
wolfSSL | 15:117db924cf7c | 12628 | algoSz = SetAlgoID(ED25519k, algoArray, oidKeyType, 0); |
wolfSSL | 15:117db924cf7c | 12629 | verSz = SetMyVersion(0, ver, FALSE); |
wolfSSL | 15:117db924cf7c | 12630 | seqSz = SetSequence(verSz + algoSz + privSz + pubSz, seq); |
wolfSSL | 15:117db924cf7c | 12631 | |
wolfSSL | 15:117db924cf7c | 12632 | if (seqSz + verSz + algoSz + privSz + pubSz > inLen) |
wolfSSL | 15:117db924cf7c | 12633 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 12634 | |
wolfSSL | 15:117db924cf7c | 12635 | /* write out */ |
wolfSSL | 15:117db924cf7c | 12636 | /* seq */ |
wolfSSL | 15:117db924cf7c | 12637 | XMEMCPY(output + idx, seq, seqSz); |
wolfSSL | 15:117db924cf7c | 12638 | idx = seqSz; |
wolfSSL | 15:117db924cf7c | 12639 | /* ver */ |
wolfSSL | 15:117db924cf7c | 12640 | XMEMCPY(output + idx, ver, verSz); |
wolfSSL | 15:117db924cf7c | 12641 | idx += verSz; |
wolfSSL | 15:117db924cf7c | 12642 | /* algo */ |
wolfSSL | 15:117db924cf7c | 12643 | XMEMCPY(output + idx, algoArray, algoSz); |
wolfSSL | 15:117db924cf7c | 12644 | idx += algoSz; |
wolfSSL | 15:117db924cf7c | 12645 | /* privKey */ |
wolfSSL | 15:117db924cf7c | 12646 | idx += SetOctetString(2 + ED25519_KEY_SIZE, output + idx); |
wolfSSL | 15:117db924cf7c | 12647 | idx += SetOctetString(ED25519_KEY_SIZE, output + idx); |
wolfSSL | 15:117db924cf7c | 12648 | ret = wc_ed25519_export_private_only(key, output + idx, &privSz); |
wolfSSL | 15:117db924cf7c | 12649 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 12650 | return ret; |
wolfSSL | 15:117db924cf7c | 12651 | idx += privSz; |
wolfSSL | 15:117db924cf7c | 12652 | /* pubKey */ |
wolfSSL | 15:117db924cf7c | 12653 | if (pubOut) { |
wolfSSL | 15:117db924cf7c | 12654 | idx += SetExplicit(1, 2 + ED25519_PUB_KEY_SIZE, output + idx); |
wolfSSL | 15:117db924cf7c | 12655 | idx += SetOctetString(ED25519_KEY_SIZE, output + idx); |
wolfSSL | 15:117db924cf7c | 12656 | ret = wc_ed25519_export_public(key, output + idx, &pubSz); |
wolfSSL | 15:117db924cf7c | 12657 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 12658 | return ret; |
wolfSSL | 15:117db924cf7c | 12659 | idx += pubSz; |
wolfSSL | 15:117db924cf7c | 12660 | } |
wolfSSL | 15:117db924cf7c | 12661 | |
wolfSSL | 15:117db924cf7c | 12662 | return idx; |
wolfSSL | 15:117db924cf7c | 12663 | } |
wolfSSL | 15:117db924cf7c | 12664 | |
wolfSSL | 15:117db924cf7c | 12665 | /* Write a Private ecc key, including public to DER format, |
wolfSSL | 15:117db924cf7c | 12666 | * length on success else < 0 */ |
wolfSSL | 15:117db924cf7c | 12667 | int wc_Ed25519KeyToDer(ed25519_key* key, byte* output, word32 inLen) |
wolfSSL | 15:117db924cf7c | 12668 | { |
wolfSSL | 15:117db924cf7c | 12669 | return wc_BuildEd25519KeyDer(key, output, inLen, 1); |
wolfSSL | 15:117db924cf7c | 12670 | } |
wolfSSL | 15:117db924cf7c | 12671 | |
wolfSSL | 15:117db924cf7c | 12672 | |
wolfSSL | 15:117db924cf7c | 12673 | |
wolfSSL | 15:117db924cf7c | 12674 | /* Write only private ecc key to DER format, |
wolfSSL | 15:117db924cf7c | 12675 | * length on success else < 0 */ |
wolfSSL | 15:117db924cf7c | 12676 | int wc_Ed25519PrivateKeyToDer(ed25519_key* key, byte* output, word32 inLen) |
wolfSSL | 15:117db924cf7c | 12677 | { |
wolfSSL | 15:117db924cf7c | 12678 | return wc_BuildEd25519KeyDer(key, output, inLen, 0); |
wolfSSL | 15:117db924cf7c | 12679 | } |
wolfSSL | 15:117db924cf7c | 12680 | |
wolfSSL | 15:117db924cf7c | 12681 | #endif /* WOLFSSL_KEY_GEN */ |
wolfSSL | 15:117db924cf7c | 12682 | |
wolfSSL | 15:117db924cf7c | 12683 | #endif /* HAVE_ED25519 */ |
wolfSSL | 15:117db924cf7c | 12684 | |
wolfSSL | 15:117db924cf7c | 12685 | |
wolfSSL | 15:117db924cf7c | 12686 | #if defined(HAVE_OCSP) || defined(HAVE_CRL) |
wolfSSL | 15:117db924cf7c | 12687 | |
wolfSSL | 15:117db924cf7c | 12688 | /* Get raw Date only, no processing, 0 on success */ |
wolfSSL | 15:117db924cf7c | 12689 | static int GetBasicDate(const byte* source, word32* idx, byte* date, |
wolfSSL | 15:117db924cf7c | 12690 | byte* format, int maxIdx) |
wolfSSL | 15:117db924cf7c | 12691 | { |
wolfSSL | 15:117db924cf7c | 12692 | int ret, length; |
wolfSSL | 15:117db924cf7c | 12693 | const byte *datePtr = NULL; |
wolfSSL | 15:117db924cf7c | 12694 | |
wolfSSL | 15:117db924cf7c | 12695 | WOLFSSL_ENTER("GetBasicDate"); |
wolfSSL | 15:117db924cf7c | 12696 | |
wolfSSL | 15:117db924cf7c | 12697 | ret = GetDateInfo(source, idx, &datePtr, format, &length, maxIdx); |
wolfSSL | 15:117db924cf7c | 12698 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 12699 | return ret; |
wolfSSL | 15:117db924cf7c | 12700 | |
wolfSSL | 15:117db924cf7c | 12701 | XMEMCPY(date, datePtr, length); |
wolfSSL | 15:117db924cf7c | 12702 | |
wolfSSL | 15:117db924cf7c | 12703 | return 0; |
wolfSSL | 15:117db924cf7c | 12704 | } |
wolfSSL | 15:117db924cf7c | 12705 | |
wolfSSL | 15:117db924cf7c | 12706 | #endif |
wolfSSL | 15:117db924cf7c | 12707 | |
wolfSSL | 15:117db924cf7c | 12708 | |
wolfSSL | 15:117db924cf7c | 12709 | #ifdef HAVE_OCSP |
wolfSSL | 15:117db924cf7c | 12710 | |
wolfSSL | 15:117db924cf7c | 12711 | static int GetEnumerated(const byte* input, word32* inOutIdx, int *value) |
wolfSSL | 15:117db924cf7c | 12712 | { |
wolfSSL | 15:117db924cf7c | 12713 | word32 idx = *inOutIdx; |
wolfSSL | 15:117db924cf7c | 12714 | word32 len; |
wolfSSL | 15:117db924cf7c | 12715 | |
wolfSSL | 15:117db924cf7c | 12716 | WOLFSSL_ENTER("GetEnumerated"); |
wolfSSL | 15:117db924cf7c | 12717 | |
wolfSSL | 15:117db924cf7c | 12718 | *value = 0; |
wolfSSL | 15:117db924cf7c | 12719 | |
wolfSSL | 15:117db924cf7c | 12720 | if (input[idx++] != ASN_ENUMERATED) |
wolfSSL | 15:117db924cf7c | 12721 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12722 | |
wolfSSL | 15:117db924cf7c | 12723 | len = input[idx++]; |
wolfSSL | 15:117db924cf7c | 12724 | if (len > 4) |
wolfSSL | 15:117db924cf7c | 12725 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12726 | |
wolfSSL | 15:117db924cf7c | 12727 | while (len--) { |
wolfSSL | 15:117db924cf7c | 12728 | *value = *value << 8 | input[idx++]; |
wolfSSL | 15:117db924cf7c | 12729 | } |
wolfSSL | 15:117db924cf7c | 12730 | |
wolfSSL | 15:117db924cf7c | 12731 | *inOutIdx = idx; |
wolfSSL | 15:117db924cf7c | 12732 | |
wolfSSL | 15:117db924cf7c | 12733 | return *value; |
wolfSSL | 15:117db924cf7c | 12734 | } |
wolfSSL | 15:117db924cf7c | 12735 | |
wolfSSL | 15:117db924cf7c | 12736 | |
wolfSSL | 15:117db924cf7c | 12737 | static int DecodeSingleResponse(byte* source, |
wolfSSL | 15:117db924cf7c | 12738 | word32* ioIndex, OcspResponse* resp, word32 size) |
wolfSSL | 15:117db924cf7c | 12739 | { |
wolfSSL | 15:117db924cf7c | 12740 | word32 idx = *ioIndex, prevIndex, oid; |
wolfSSL | 15:117db924cf7c | 12741 | int length, wrapperSz; |
wolfSSL | 15:117db924cf7c | 12742 | CertStatus* cs = resp->status; |
wolfSSL | 15:117db924cf7c | 12743 | int ret; |
wolfSSL | 15:117db924cf7c | 12744 | |
wolfSSL | 15:117db924cf7c | 12745 | WOLFSSL_ENTER("DecodeSingleResponse"); |
wolfSSL | 15:117db924cf7c | 12746 | |
wolfSSL | 15:117db924cf7c | 12747 | /* Outer wrapper of the SEQUENCE OF Single Responses. */ |
wolfSSL | 15:117db924cf7c | 12748 | if (GetSequence(source, &idx, &wrapperSz, size) < 0) |
wolfSSL | 15:117db924cf7c | 12749 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12750 | |
wolfSSL | 15:117db924cf7c | 12751 | prevIndex = idx; |
wolfSSL | 15:117db924cf7c | 12752 | |
wolfSSL | 15:117db924cf7c | 12753 | /* When making a request, we only request one status on one certificate |
wolfSSL | 15:117db924cf7c | 12754 | * at a time. There should only be one SingleResponse */ |
wolfSSL | 15:117db924cf7c | 12755 | |
wolfSSL | 15:117db924cf7c | 12756 | /* Wrapper around the Single Response */ |
wolfSSL | 15:117db924cf7c | 12757 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12758 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12759 | |
wolfSSL | 15:117db924cf7c | 12760 | /* Wrapper around the CertID */ |
wolfSSL | 15:117db924cf7c | 12761 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12762 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12763 | /* Skip the hash algorithm */ |
wolfSSL | 15:117db924cf7c | 12764 | if (GetAlgoId(source, &idx, &oid, oidIgnoreType, size) < 0) |
wolfSSL | 15:117db924cf7c | 12765 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12766 | /* Save reference to the hash of CN */ |
wolfSSL | 15:117db924cf7c | 12767 | ret = GetOctetString(source, &idx, &length, size); |
wolfSSL | 15:117db924cf7c | 12768 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 12769 | return ret; |
wolfSSL | 15:117db924cf7c | 12770 | resp->issuerHash = source + idx; |
wolfSSL | 15:117db924cf7c | 12771 | idx += length; |
wolfSSL | 15:117db924cf7c | 12772 | /* Save reference to the hash of the issuer public key */ |
wolfSSL | 15:117db924cf7c | 12773 | ret = GetOctetString(source, &idx, &length, size); |
wolfSSL | 15:117db924cf7c | 12774 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 12775 | return ret; |
wolfSSL | 15:117db924cf7c | 12776 | resp->issuerKeyHash = source + idx; |
wolfSSL | 15:117db924cf7c | 12777 | idx += length; |
wolfSSL | 15:117db924cf7c | 12778 | |
wolfSSL | 15:117db924cf7c | 12779 | /* Get serial number */ |
wolfSSL | 15:117db924cf7c | 12780 | if (GetSerialNumber(source, &idx, cs->serial, &cs->serialSz, size) < 0) |
wolfSSL | 15:117db924cf7c | 12781 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12782 | |
wolfSSL | 15:117db924cf7c | 12783 | /* CertStatus */ |
wolfSSL | 15:117db924cf7c | 12784 | switch (source[idx++]) |
wolfSSL | 15:117db924cf7c | 12785 | { |
wolfSSL | 15:117db924cf7c | 12786 | case (ASN_CONTEXT_SPECIFIC | CERT_GOOD): |
wolfSSL | 15:117db924cf7c | 12787 | cs->status = CERT_GOOD; |
wolfSSL | 15:117db924cf7c | 12788 | idx++; |
wolfSSL | 15:117db924cf7c | 12789 | break; |
wolfSSL | 15:117db924cf7c | 12790 | case (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | CERT_REVOKED): |
wolfSSL | 15:117db924cf7c | 12791 | cs->status = CERT_REVOKED; |
wolfSSL | 15:117db924cf7c | 12792 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12793 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12794 | idx += length; |
wolfSSL | 15:117db924cf7c | 12795 | break; |
wolfSSL | 15:117db924cf7c | 12796 | case (ASN_CONTEXT_SPECIFIC | CERT_UNKNOWN): |
wolfSSL | 15:117db924cf7c | 12797 | cs->status = CERT_UNKNOWN; |
wolfSSL | 15:117db924cf7c | 12798 | idx++; |
wolfSSL | 15:117db924cf7c | 12799 | break; |
wolfSSL | 15:117db924cf7c | 12800 | default: |
wolfSSL | 15:117db924cf7c | 12801 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12802 | } |
wolfSSL | 15:117db924cf7c | 12803 | |
wolfSSL | 15:117db924cf7c | 12804 | #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) |
wolfSSL | 15:117db924cf7c | 12805 | cs->thisDateAsn = source + idx; |
wolfSSL | 15:117db924cf7c | 12806 | #endif |
wolfSSL | 15:117db924cf7c | 12807 | if (GetBasicDate(source, &idx, cs->thisDate, |
wolfSSL | 15:117db924cf7c | 12808 | &cs->thisDateFormat, size) < 0) |
wolfSSL | 15:117db924cf7c | 12809 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12810 | |
wolfSSL | 15:117db924cf7c | 12811 | #ifndef NO_ASN_TIME |
wolfSSL | 15:117db924cf7c | 12812 | if (!XVALIDATE_DATE(cs->thisDate, cs->thisDateFormat, BEFORE)) |
wolfSSL | 15:117db924cf7c | 12813 | return ASN_BEFORE_DATE_E; |
wolfSSL | 15:117db924cf7c | 12814 | #endif |
wolfSSL | 15:117db924cf7c | 12815 | |
wolfSSL | 15:117db924cf7c | 12816 | /* The following items are optional. Only check for them if there is more |
wolfSSL | 15:117db924cf7c | 12817 | * unprocessed data in the singleResponse wrapper. */ |
wolfSSL | 15:117db924cf7c | 12818 | |
wolfSSL | 15:117db924cf7c | 12819 | if (((int)(idx - prevIndex) < wrapperSz) && |
wolfSSL | 15:117db924cf7c | 12820 | (source[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0))) |
wolfSSL | 15:117db924cf7c | 12821 | { |
wolfSSL | 15:117db924cf7c | 12822 | idx++; |
wolfSSL | 15:117db924cf7c | 12823 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12824 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12825 | #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) |
wolfSSL | 15:117db924cf7c | 12826 | cs->nextDateAsn = source + idx; |
wolfSSL | 15:117db924cf7c | 12827 | #endif |
wolfSSL | 15:117db924cf7c | 12828 | if (GetBasicDate(source, &idx, cs->nextDate, |
wolfSSL | 15:117db924cf7c | 12829 | &cs->nextDateFormat, size) < 0) |
wolfSSL | 15:117db924cf7c | 12830 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12831 | |
wolfSSL | 15:117db924cf7c | 12832 | #ifndef NO_ASN_TIME |
wolfSSL | 15:117db924cf7c | 12833 | if (!XVALIDATE_DATE(cs->nextDate, cs->nextDateFormat, AFTER)) |
wolfSSL | 15:117db924cf7c | 12834 | return ASN_AFTER_DATE_E; |
wolfSSL | 15:117db924cf7c | 12835 | #endif |
wolfSSL | 15:117db924cf7c | 12836 | } |
wolfSSL | 15:117db924cf7c | 12837 | if (((int)(idx - prevIndex) < wrapperSz) && |
wolfSSL | 15:117db924cf7c | 12838 | (source[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1))) |
wolfSSL | 15:117db924cf7c | 12839 | { |
wolfSSL | 15:117db924cf7c | 12840 | idx++; |
wolfSSL | 15:117db924cf7c | 12841 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12842 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12843 | idx += length; |
wolfSSL | 15:117db924cf7c | 12844 | } |
wolfSSL | 15:117db924cf7c | 12845 | |
wolfSSL | 15:117db924cf7c | 12846 | *ioIndex = idx; |
wolfSSL | 15:117db924cf7c | 12847 | |
wolfSSL | 15:117db924cf7c | 12848 | return 0; |
wolfSSL | 15:117db924cf7c | 12849 | } |
wolfSSL | 15:117db924cf7c | 12850 | |
wolfSSL | 15:117db924cf7c | 12851 | static int DecodeOcspRespExtensions(byte* source, |
wolfSSL | 15:117db924cf7c | 12852 | word32* ioIndex, OcspResponse* resp, word32 sz) |
wolfSSL | 15:117db924cf7c | 12853 | { |
wolfSSL | 15:117db924cf7c | 12854 | word32 idx = *ioIndex; |
wolfSSL | 15:117db924cf7c | 12855 | int length; |
wolfSSL | 15:117db924cf7c | 12856 | int ext_bound; /* boundary index for the sequence of extensions */ |
wolfSSL | 15:117db924cf7c | 12857 | word32 oid; |
wolfSSL | 15:117db924cf7c | 12858 | int ret; |
wolfSSL | 15:117db924cf7c | 12859 | |
wolfSSL | 15:117db924cf7c | 12860 | WOLFSSL_ENTER("DecodeOcspRespExtensions"); |
wolfSSL | 15:117db924cf7c | 12861 | |
wolfSSL | 15:117db924cf7c | 12862 | if ((idx + 1) > sz) |
wolfSSL | 15:117db924cf7c | 12863 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 12864 | |
wolfSSL | 15:117db924cf7c | 12865 | if (source[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1)) |
wolfSSL | 15:117db924cf7c | 12866 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12867 | |
wolfSSL | 15:117db924cf7c | 12868 | if (GetLength(source, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 12869 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12870 | |
wolfSSL | 15:117db924cf7c | 12871 | if (GetSequence(source, &idx, &length, sz) < 0) |
wolfSSL | 15:117db924cf7c | 12872 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12873 | |
wolfSSL | 15:117db924cf7c | 12874 | ext_bound = idx + length; |
wolfSSL | 15:117db924cf7c | 12875 | |
wolfSSL | 15:117db924cf7c | 12876 | while (idx < (word32)ext_bound) { |
wolfSSL | 15:117db924cf7c | 12877 | if (GetSequence(source, &idx, &length, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 12878 | WOLFSSL_MSG("\tfail: should be a SEQUENCE"); |
wolfSSL | 15:117db924cf7c | 12879 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12880 | } |
wolfSSL | 15:117db924cf7c | 12881 | |
wolfSSL | 15:117db924cf7c | 12882 | oid = 0; |
wolfSSL | 15:117db924cf7c | 12883 | if (GetObjectId(source, &idx, &oid, oidOcspType, sz) < 0) { |
wolfSSL | 15:117db924cf7c | 12884 | WOLFSSL_MSG("\tfail: OBJECT ID"); |
wolfSSL | 15:117db924cf7c | 12885 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12886 | } |
wolfSSL | 15:117db924cf7c | 12887 | |
wolfSSL | 15:117db924cf7c | 12888 | /* check for critical flag */ |
wolfSSL | 15:117db924cf7c | 12889 | if (source[idx] == ASN_BOOLEAN) { |
wolfSSL | 15:117db924cf7c | 12890 | WOLFSSL_MSG("\tfound optional critical flag, moving past"); |
wolfSSL | 15:117db924cf7c | 12891 | ret = GetBoolean(source, &idx, sz); |
wolfSSL | 15:117db924cf7c | 12892 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 12893 | return ret; |
wolfSSL | 15:117db924cf7c | 12894 | } |
wolfSSL | 15:117db924cf7c | 12895 | |
wolfSSL | 15:117db924cf7c | 12896 | ret = GetOctetString(source, &idx, &length, sz); |
wolfSSL | 15:117db924cf7c | 12897 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 12898 | return ret; |
wolfSSL | 15:117db924cf7c | 12899 | |
wolfSSL | 15:117db924cf7c | 12900 | if (oid == OCSP_NONCE_OID) { |
wolfSSL | 15:117db924cf7c | 12901 | /* get data inside extra OCTET_STRING */ |
wolfSSL | 15:117db924cf7c | 12902 | ret = GetOctetString(source, &idx, &length, sz); |
wolfSSL | 15:117db924cf7c | 12903 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 12904 | return ret; |
wolfSSL | 15:117db924cf7c | 12905 | |
wolfSSL | 15:117db924cf7c | 12906 | resp->nonce = source + idx; |
wolfSSL | 15:117db924cf7c | 12907 | resp->nonceSz = length; |
wolfSSL | 15:117db924cf7c | 12908 | } |
wolfSSL | 15:117db924cf7c | 12909 | |
wolfSSL | 15:117db924cf7c | 12910 | idx += length; |
wolfSSL | 15:117db924cf7c | 12911 | } |
wolfSSL | 15:117db924cf7c | 12912 | |
wolfSSL | 15:117db924cf7c | 12913 | *ioIndex = idx; |
wolfSSL | 15:117db924cf7c | 12914 | return 0; |
wolfSSL | 15:117db924cf7c | 12915 | } |
wolfSSL | 15:117db924cf7c | 12916 | |
wolfSSL | 15:117db924cf7c | 12917 | |
wolfSSL | 15:117db924cf7c | 12918 | static int DecodeResponseData(byte* source, |
wolfSSL | 15:117db924cf7c | 12919 | word32* ioIndex, OcspResponse* resp, word32 size) |
wolfSSL | 15:117db924cf7c | 12920 | { |
wolfSSL | 15:117db924cf7c | 12921 | word32 idx = *ioIndex, prev_idx; |
wolfSSL | 15:117db924cf7c | 12922 | int length; |
wolfSSL | 15:117db924cf7c | 12923 | int version; |
wolfSSL | 15:117db924cf7c | 12924 | word32 responderId = 0; |
wolfSSL | 15:117db924cf7c | 12925 | |
wolfSSL | 15:117db924cf7c | 12926 | WOLFSSL_ENTER("DecodeResponseData"); |
wolfSSL | 15:117db924cf7c | 12927 | |
wolfSSL | 15:117db924cf7c | 12928 | resp->response = source + idx; |
wolfSSL | 15:117db924cf7c | 12929 | prev_idx = idx; |
wolfSSL | 15:117db924cf7c | 12930 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12931 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12932 | resp->responseSz = length + idx - prev_idx; |
wolfSSL | 15:117db924cf7c | 12933 | |
wolfSSL | 15:117db924cf7c | 12934 | /* Get version. It is an EXPLICIT[0] DEFAULT(0) value. If this |
wolfSSL | 15:117db924cf7c | 12935 | * item isn't an EXPLICIT[0], then set version to zero and move |
wolfSSL | 15:117db924cf7c | 12936 | * onto the next item. |
wolfSSL | 15:117db924cf7c | 12937 | */ |
wolfSSL | 15:117db924cf7c | 12938 | if (source[idx] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) |
wolfSSL | 15:117db924cf7c | 12939 | { |
wolfSSL | 15:117db924cf7c | 12940 | idx += 2; /* Eat the value and length */ |
wolfSSL | 15:117db924cf7c | 12941 | if (GetMyVersion(source, &idx, &version, size) < 0) |
wolfSSL | 15:117db924cf7c | 12942 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12943 | } else |
wolfSSL | 15:117db924cf7c | 12944 | version = 0; |
wolfSSL | 15:117db924cf7c | 12945 | |
wolfSSL | 15:117db924cf7c | 12946 | responderId = source[idx++]; |
wolfSSL | 15:117db924cf7c | 12947 | if ((responderId == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1)) || |
wolfSSL | 15:117db924cf7c | 12948 | (responderId == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 2))) |
wolfSSL | 15:117db924cf7c | 12949 | { |
wolfSSL | 15:117db924cf7c | 12950 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12951 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12952 | idx += length; |
wolfSSL | 15:117db924cf7c | 12953 | } |
wolfSSL | 15:117db924cf7c | 12954 | else |
wolfSSL | 15:117db924cf7c | 12955 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12956 | |
wolfSSL | 15:117db924cf7c | 12957 | /* save pointer to the producedAt time */ |
wolfSSL | 15:117db924cf7c | 12958 | if (GetBasicDate(source, &idx, resp->producedDate, |
wolfSSL | 15:117db924cf7c | 12959 | &resp->producedDateFormat, size) < 0) |
wolfSSL | 15:117db924cf7c | 12960 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12961 | |
wolfSSL | 15:117db924cf7c | 12962 | if (DecodeSingleResponse(source, &idx, resp, size) < 0) |
wolfSSL | 15:117db924cf7c | 12963 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12964 | |
wolfSSL | 15:117db924cf7c | 12965 | /* |
wolfSSL | 15:117db924cf7c | 12966 | * Check the length of the ResponseData against the current index to |
wolfSSL | 15:117db924cf7c | 12967 | * see if there are extensions, they are optional. |
wolfSSL | 15:117db924cf7c | 12968 | */ |
wolfSSL | 15:117db924cf7c | 12969 | if (idx - prev_idx < resp->responseSz) |
wolfSSL | 15:117db924cf7c | 12970 | if (DecodeOcspRespExtensions(source, &idx, resp, size) < 0) |
wolfSSL | 15:117db924cf7c | 12971 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12972 | |
wolfSSL | 15:117db924cf7c | 12973 | *ioIndex = idx; |
wolfSSL | 15:117db924cf7c | 12974 | return 0; |
wolfSSL | 15:117db924cf7c | 12975 | } |
wolfSSL | 15:117db924cf7c | 12976 | |
wolfSSL | 15:117db924cf7c | 12977 | |
wolfSSL | 15:117db924cf7c | 12978 | #ifndef WOLFSSL_NO_OCSP_OPTIONAL_CERTS |
wolfSSL | 15:117db924cf7c | 12979 | |
wolfSSL | 15:117db924cf7c | 12980 | static int DecodeCerts(byte* source, |
wolfSSL | 15:117db924cf7c | 12981 | word32* ioIndex, OcspResponse* resp, word32 size) |
wolfSSL | 15:117db924cf7c | 12982 | { |
wolfSSL | 15:117db924cf7c | 12983 | word32 idx = *ioIndex; |
wolfSSL | 15:117db924cf7c | 12984 | |
wolfSSL | 15:117db924cf7c | 12985 | WOLFSSL_ENTER("DecodeCerts"); |
wolfSSL | 15:117db924cf7c | 12986 | |
wolfSSL | 15:117db924cf7c | 12987 | if (source[idx++] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) |
wolfSSL | 15:117db924cf7c | 12988 | { |
wolfSSL | 15:117db924cf7c | 12989 | int length; |
wolfSSL | 15:117db924cf7c | 12990 | |
wolfSSL | 15:117db924cf7c | 12991 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12992 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12993 | |
wolfSSL | 15:117db924cf7c | 12994 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 12995 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 12996 | |
wolfSSL | 15:117db924cf7c | 12997 | resp->cert = source + idx; |
wolfSSL | 15:117db924cf7c | 12998 | resp->certSz = length; |
wolfSSL | 15:117db924cf7c | 12999 | |
wolfSSL | 15:117db924cf7c | 13000 | idx += length; |
wolfSSL | 15:117db924cf7c | 13001 | } |
wolfSSL | 15:117db924cf7c | 13002 | *ioIndex = idx; |
wolfSSL | 15:117db924cf7c | 13003 | return 0; |
wolfSSL | 15:117db924cf7c | 13004 | } |
wolfSSL | 15:117db924cf7c | 13005 | |
wolfSSL | 15:117db924cf7c | 13006 | #endif /* WOLFSSL_NO_OCSP_OPTIONAL_CERTS */ |
wolfSSL | 15:117db924cf7c | 13007 | |
wolfSSL | 15:117db924cf7c | 13008 | |
wolfSSL | 15:117db924cf7c | 13009 | static int DecodeBasicOcspResponse(byte* source, word32* ioIndex, |
wolfSSL | 15:117db924cf7c | 13010 | OcspResponse* resp, word32 size, void* cm, void* heap, int noVerify) |
wolfSSL | 15:117db924cf7c | 13011 | { |
wolfSSL | 15:117db924cf7c | 13012 | int length; |
wolfSSL | 15:117db924cf7c | 13013 | word32 idx = *ioIndex; |
wolfSSL | 15:117db924cf7c | 13014 | word32 end_index; |
wolfSSL | 15:117db924cf7c | 13015 | int ret; |
wolfSSL | 15:117db924cf7c | 13016 | int sigLength; |
wolfSSL | 15:117db924cf7c | 13017 | |
wolfSSL | 15:117db924cf7c | 13018 | WOLFSSL_ENTER("DecodeBasicOcspResponse"); |
wolfSSL | 15:117db924cf7c | 13019 | (void)heap; |
wolfSSL | 15:117db924cf7c | 13020 | |
wolfSSL | 15:117db924cf7c | 13021 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 13022 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13023 | |
wolfSSL | 15:117db924cf7c | 13024 | if (idx + length > size) |
wolfSSL | 15:117db924cf7c | 13025 | return ASN_INPUT_E; |
wolfSSL | 15:117db924cf7c | 13026 | end_index = idx + length; |
wolfSSL | 15:117db924cf7c | 13027 | |
wolfSSL | 15:117db924cf7c | 13028 | if (DecodeResponseData(source, &idx, resp, size) < 0) |
wolfSSL | 15:117db924cf7c | 13029 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13030 | |
wolfSSL | 15:117db924cf7c | 13031 | /* Get the signature algorithm */ |
wolfSSL | 15:117db924cf7c | 13032 | if (GetAlgoId(source, &idx, &resp->sigOID, oidSigType, size) < 0) |
wolfSSL | 15:117db924cf7c | 13033 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13034 | |
wolfSSL | 15:117db924cf7c | 13035 | ret = CheckBitString(source, &idx, &sigLength, size, 1, NULL); |
wolfSSL | 15:117db924cf7c | 13036 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 13037 | return ret; |
wolfSSL | 15:117db924cf7c | 13038 | |
wolfSSL | 15:117db924cf7c | 13039 | resp->sigSz = sigLength; |
wolfSSL | 15:117db924cf7c | 13040 | resp->sig = source + idx; |
wolfSSL | 15:117db924cf7c | 13041 | idx += sigLength; |
wolfSSL | 15:117db924cf7c | 13042 | |
wolfSSL | 15:117db924cf7c | 13043 | /* |
wolfSSL | 15:117db924cf7c | 13044 | * Check the length of the BasicOcspResponse against the current index to |
wolfSSL | 15:117db924cf7c | 13045 | * see if there are certificates, they are optional. |
wolfSSL | 15:117db924cf7c | 13046 | */ |
wolfSSL | 15:117db924cf7c | 13047 | #ifndef WOLFSSL_NO_OCSP_OPTIONAL_CERTS |
wolfSSL | 15:117db924cf7c | 13048 | if (idx < end_index) |
wolfSSL | 15:117db924cf7c | 13049 | { |
wolfSSL | 15:117db924cf7c | 13050 | DecodedCert cert; |
wolfSSL | 15:117db924cf7c | 13051 | |
wolfSSL | 15:117db924cf7c | 13052 | if (DecodeCerts(source, &idx, resp, size) < 0) |
wolfSSL | 15:117db924cf7c | 13053 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13054 | |
wolfSSL | 15:117db924cf7c | 13055 | InitDecodedCert(&cert, resp->cert, resp->certSz, heap); |
wolfSSL | 15:117db924cf7c | 13056 | |
wolfSSL | 15:117db924cf7c | 13057 | /* Don't verify if we don't have access to Cert Manager. */ |
wolfSSL | 15:117db924cf7c | 13058 | ret = ParseCertRelative(&cert, CERT_TYPE, |
wolfSSL | 15:117db924cf7c | 13059 | noVerify ? NO_VERIFY : VERIFY_OCSP, cm); |
wolfSSL | 15:117db924cf7c | 13060 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 13061 | WOLFSSL_MSG("\tOCSP Responder certificate parsing failed"); |
wolfSSL | 15:117db924cf7c | 13062 | FreeDecodedCert(&cert); |
wolfSSL | 15:117db924cf7c | 13063 | return ret; |
wolfSSL | 15:117db924cf7c | 13064 | } |
wolfSSL | 15:117db924cf7c | 13065 | |
wolfSSL | 15:117db924cf7c | 13066 | #ifndef WOLFSSL_NO_OCSP_ISSUER_CHECK |
wolfSSL | 15:117db924cf7c | 13067 | if ((cert.extExtKeyUsage & EXTKEYUSE_OCSP_SIGN) == 0) { |
wolfSSL | 15:117db924cf7c | 13068 | if (XMEMCMP(cert.subjectHash, |
wolfSSL | 15:117db924cf7c | 13069 | resp->issuerHash, KEYID_SIZE) == 0) { |
wolfSSL | 15:117db924cf7c | 13070 | WOLFSSL_MSG("\tOCSP Response signed by issuer"); |
wolfSSL | 15:117db924cf7c | 13071 | } |
wolfSSL | 15:117db924cf7c | 13072 | else { |
wolfSSL | 15:117db924cf7c | 13073 | WOLFSSL_MSG("\tOCSP Responder key usage check failed"); |
wolfSSL | 15:117db924cf7c | 13074 | #ifdef OPENSSL_EXTRA |
wolfSSL | 15:117db924cf7c | 13075 | resp->verifyError = OCSP_BAD_ISSUER; |
wolfSSL | 15:117db924cf7c | 13076 | #else |
wolfSSL | 15:117db924cf7c | 13077 | FreeDecodedCert(&cert); |
wolfSSL | 15:117db924cf7c | 13078 | return BAD_OCSP_RESPONDER; |
wolfSSL | 15:117db924cf7c | 13079 | #endif |
wolfSSL | 15:117db924cf7c | 13080 | } |
wolfSSL | 15:117db924cf7c | 13081 | } |
wolfSSL | 15:117db924cf7c | 13082 | #endif |
wolfSSL | 15:117db924cf7c | 13083 | |
wolfSSL | 15:117db924cf7c | 13084 | /* ConfirmSignature is blocking here */ |
wolfSSL | 15:117db924cf7c | 13085 | ret = ConfirmSignature(&cert.sigCtx, |
wolfSSL | 15:117db924cf7c | 13086 | resp->response, resp->responseSz, |
wolfSSL | 15:117db924cf7c | 13087 | cert.publicKey, cert.pubKeySize, cert.keyOID, |
wolfSSL | 15:117db924cf7c | 13088 | resp->sig, resp->sigSz, resp->sigOID); |
wolfSSL | 15:117db924cf7c | 13089 | FreeDecodedCert(&cert); |
wolfSSL | 15:117db924cf7c | 13090 | |
wolfSSL | 15:117db924cf7c | 13091 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 13092 | WOLFSSL_MSG("\tOCSP Confirm signature failed"); |
wolfSSL | 15:117db924cf7c | 13093 | return ASN_OCSP_CONFIRM_E; |
wolfSSL | 15:117db924cf7c | 13094 | } |
wolfSSL | 15:117db924cf7c | 13095 | } |
wolfSSL | 15:117db924cf7c | 13096 | else |
wolfSSL | 15:117db924cf7c | 13097 | #endif /* WOLFSSL_NO_OCSP_OPTIONAL_CERTS */ |
wolfSSL | 15:117db924cf7c | 13098 | { |
wolfSSL | 15:117db924cf7c | 13099 | Signer* ca = NULL; |
wolfSSL | 15:117db924cf7c | 13100 | int sigValid = -1; |
wolfSSL | 15:117db924cf7c | 13101 | |
wolfSSL | 15:117db924cf7c | 13102 | #ifndef NO_SKID |
wolfSSL | 15:117db924cf7c | 13103 | ca = GetCA(cm, resp->issuerKeyHash); |
wolfSSL | 15:117db924cf7c | 13104 | #else |
wolfSSL | 15:117db924cf7c | 13105 | ca = GetCA(cm, resp->issuerHash); |
wolfSSL | 15:117db924cf7c | 13106 | #endif |
wolfSSL | 15:117db924cf7c | 13107 | |
wolfSSL | 15:117db924cf7c | 13108 | if (ca) { |
wolfSSL | 15:117db924cf7c | 13109 | SignatureCtx sigCtx; |
wolfSSL | 15:117db924cf7c | 13110 | InitSignatureCtx(&sigCtx, heap, INVALID_DEVID); |
wolfSSL | 15:117db924cf7c | 13111 | |
wolfSSL | 15:117db924cf7c | 13112 | /* ConfirmSignature is blocking here */ |
wolfSSL | 15:117db924cf7c | 13113 | sigValid = ConfirmSignature(&sigCtx, resp->response, |
wolfSSL | 15:117db924cf7c | 13114 | resp->responseSz, ca->publicKey, ca->pubKeySize, ca->keyOID, |
wolfSSL | 15:117db924cf7c | 13115 | resp->sig, resp->sigSz, resp->sigOID); |
wolfSSL | 15:117db924cf7c | 13116 | } |
wolfSSL | 15:117db924cf7c | 13117 | if (ca == NULL || sigValid != 0) { |
wolfSSL | 15:117db924cf7c | 13118 | WOLFSSL_MSG("\tOCSP Confirm signature failed"); |
wolfSSL | 15:117db924cf7c | 13119 | return ASN_OCSP_CONFIRM_E; |
wolfSSL | 15:117db924cf7c | 13120 | } |
wolfSSL | 15:117db924cf7c | 13121 | |
wolfSSL | 15:117db924cf7c | 13122 | (void)noVerify; |
wolfSSL | 15:117db924cf7c | 13123 | } |
wolfSSL | 15:117db924cf7c | 13124 | |
wolfSSL | 15:117db924cf7c | 13125 | *ioIndex = idx; |
wolfSSL | 15:117db924cf7c | 13126 | return 0; |
wolfSSL | 15:117db924cf7c | 13127 | } |
wolfSSL | 15:117db924cf7c | 13128 | |
wolfSSL | 15:117db924cf7c | 13129 | |
wolfSSL | 15:117db924cf7c | 13130 | void InitOcspResponse(OcspResponse* resp, CertStatus* status, |
wolfSSL | 15:117db924cf7c | 13131 | byte* source, word32 inSz) |
wolfSSL | 15:117db924cf7c | 13132 | { |
wolfSSL | 15:117db924cf7c | 13133 | WOLFSSL_ENTER("InitOcspResponse"); |
wolfSSL | 15:117db924cf7c | 13134 | |
wolfSSL | 15:117db924cf7c | 13135 | XMEMSET(status, 0, sizeof(CertStatus)); |
wolfSSL | 15:117db924cf7c | 13136 | XMEMSET(resp, 0, sizeof(OcspResponse)); |
wolfSSL | 15:117db924cf7c | 13137 | |
wolfSSL | 15:117db924cf7c | 13138 | resp->responseStatus = -1; |
wolfSSL | 15:117db924cf7c | 13139 | resp->status = status; |
wolfSSL | 15:117db924cf7c | 13140 | resp->source = source; |
wolfSSL | 15:117db924cf7c | 13141 | resp->maxIdx = inSz; |
wolfSSL | 15:117db924cf7c | 13142 | } |
wolfSSL | 15:117db924cf7c | 13143 | |
wolfSSL | 15:117db924cf7c | 13144 | |
wolfSSL | 15:117db924cf7c | 13145 | int OcspResponseDecode(OcspResponse* resp, void* cm, void* heap, int noVerify) |
wolfSSL | 15:117db924cf7c | 13146 | { |
wolfSSL | 15:117db924cf7c | 13147 | int ret; |
wolfSSL | 15:117db924cf7c | 13148 | int length = 0; |
wolfSSL | 15:117db924cf7c | 13149 | word32 idx = 0; |
wolfSSL | 15:117db924cf7c | 13150 | byte* source = resp->source; |
wolfSSL | 15:117db924cf7c | 13151 | word32 size = resp->maxIdx; |
wolfSSL | 15:117db924cf7c | 13152 | word32 oid; |
wolfSSL | 15:117db924cf7c | 13153 | |
wolfSSL | 15:117db924cf7c | 13154 | WOLFSSL_ENTER("OcspResponseDecode"); |
wolfSSL | 15:117db924cf7c | 13155 | |
wolfSSL | 15:117db924cf7c | 13156 | /* peel the outer SEQUENCE wrapper */ |
wolfSSL | 15:117db924cf7c | 13157 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 13158 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13159 | |
wolfSSL | 15:117db924cf7c | 13160 | /* First get the responseStatus, an ENUMERATED */ |
wolfSSL | 15:117db924cf7c | 13161 | if (GetEnumerated(source, &idx, &resp->responseStatus) < 0) |
wolfSSL | 15:117db924cf7c | 13162 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13163 | |
wolfSSL | 15:117db924cf7c | 13164 | if (resp->responseStatus != OCSP_SUCCESSFUL) |
wolfSSL | 15:117db924cf7c | 13165 | return 0; |
wolfSSL | 15:117db924cf7c | 13166 | |
wolfSSL | 15:117db924cf7c | 13167 | /* Next is an EXPLICIT record called ResponseBytes, OPTIONAL */ |
wolfSSL | 15:117db924cf7c | 13168 | if (idx >= size) |
wolfSSL | 15:117db924cf7c | 13169 | return ASN_INPUT_E; |
wolfSSL | 15:117db924cf7c | 13170 | if (source[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) |
wolfSSL | 15:117db924cf7c | 13171 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13172 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 13173 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13174 | |
wolfSSL | 15:117db924cf7c | 13175 | /* Get the responseBytes SEQUENCE */ |
wolfSSL | 15:117db924cf7c | 13176 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 15:117db924cf7c | 13177 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13178 | |
wolfSSL | 15:117db924cf7c | 13179 | /* Check ObjectID for the resposeBytes */ |
wolfSSL | 15:117db924cf7c | 13180 | if (GetObjectId(source, &idx, &oid, oidOcspType, size) < 0) |
wolfSSL | 15:117db924cf7c | 13181 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13182 | if (oid != OCSP_BASIC_OID) |
wolfSSL | 15:117db924cf7c | 13183 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13184 | ret = GetOctetString(source, &idx, &length, size); |
wolfSSL | 15:117db924cf7c | 13185 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 13186 | return ret; |
wolfSSL | 15:117db924cf7c | 13187 | |
wolfSSL | 15:117db924cf7c | 13188 | ret = DecodeBasicOcspResponse(source, &idx, resp, size, cm, heap, noVerify); |
wolfSSL | 15:117db924cf7c | 13189 | if (ret < 0) |
wolfSSL | 15:117db924cf7c | 13190 | return ret; |
wolfSSL | 15:117db924cf7c | 13191 | |
wolfSSL | 15:117db924cf7c | 13192 | return 0; |
wolfSSL | 15:117db924cf7c | 13193 | } |
wolfSSL | 15:117db924cf7c | 13194 | |
wolfSSL | 15:117db924cf7c | 13195 | |
wolfSSL | 15:117db924cf7c | 13196 | word32 EncodeOcspRequestExtensions(OcspRequest* req, byte* output, word32 size) |
wolfSSL | 15:117db924cf7c | 13197 | { |
wolfSSL | 15:117db924cf7c | 13198 | static const byte NonceObjId[] = { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, |
wolfSSL | 15:117db924cf7c | 13199 | 0x30, 0x01, 0x02 }; |
wolfSSL | 15:117db924cf7c | 13200 | byte seqArray[5][MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 13201 | word32 seqSz[5], totalSz = (word32)sizeof(NonceObjId); |
wolfSSL | 15:117db924cf7c | 13202 | |
wolfSSL | 15:117db924cf7c | 13203 | WOLFSSL_ENTER("SetOcspReqExtensions"); |
wolfSSL | 15:117db924cf7c | 13204 | |
wolfSSL | 15:117db924cf7c | 13205 | if (!req || !output || !req->nonceSz) |
wolfSSL | 15:117db924cf7c | 13206 | return 0; |
wolfSSL | 15:117db924cf7c | 13207 | |
wolfSSL | 15:117db924cf7c | 13208 | totalSz += req->nonceSz; |
wolfSSL | 15:117db924cf7c | 13209 | totalSz += seqSz[0] = SetOctetString(req->nonceSz, seqArray[0]); |
wolfSSL | 15:117db924cf7c | 13210 | totalSz += seqSz[1] = SetOctetString(req->nonceSz + seqSz[0], seqArray[1]); |
wolfSSL | 15:117db924cf7c | 13211 | totalSz += seqSz[2] = SetObjectId(sizeof(NonceObjId), seqArray[2]); |
wolfSSL | 15:117db924cf7c | 13212 | totalSz += seqSz[3] = SetSequence(totalSz, seqArray[3]); |
wolfSSL | 15:117db924cf7c | 13213 | totalSz += seqSz[4] = SetSequence(totalSz, seqArray[4]); |
wolfSSL | 15:117db924cf7c | 13214 | |
wolfSSL | 15:117db924cf7c | 13215 | if (totalSz > size) |
wolfSSL | 15:117db924cf7c | 13216 | return 0; |
wolfSSL | 15:117db924cf7c | 13217 | |
wolfSSL | 15:117db924cf7c | 13218 | totalSz = 0; |
wolfSSL | 15:117db924cf7c | 13219 | |
wolfSSL | 15:117db924cf7c | 13220 | XMEMCPY(output + totalSz, seqArray[4], seqSz[4]); |
wolfSSL | 15:117db924cf7c | 13221 | totalSz += seqSz[4]; |
wolfSSL | 15:117db924cf7c | 13222 | |
wolfSSL | 15:117db924cf7c | 13223 | XMEMCPY(output + totalSz, seqArray[3], seqSz[3]); |
wolfSSL | 15:117db924cf7c | 13224 | totalSz += seqSz[3]; |
wolfSSL | 15:117db924cf7c | 13225 | |
wolfSSL | 15:117db924cf7c | 13226 | XMEMCPY(output + totalSz, seqArray[2], seqSz[2]); |
wolfSSL | 15:117db924cf7c | 13227 | totalSz += seqSz[2]; |
wolfSSL | 15:117db924cf7c | 13228 | |
wolfSSL | 15:117db924cf7c | 13229 | XMEMCPY(output + totalSz, NonceObjId, sizeof(NonceObjId)); |
wolfSSL | 15:117db924cf7c | 13230 | totalSz += (word32)sizeof(NonceObjId); |
wolfSSL | 15:117db924cf7c | 13231 | |
wolfSSL | 15:117db924cf7c | 13232 | XMEMCPY(output + totalSz, seqArray[1], seqSz[1]); |
wolfSSL | 15:117db924cf7c | 13233 | totalSz += seqSz[1]; |
wolfSSL | 15:117db924cf7c | 13234 | |
wolfSSL | 15:117db924cf7c | 13235 | XMEMCPY(output + totalSz, seqArray[0], seqSz[0]); |
wolfSSL | 15:117db924cf7c | 13236 | totalSz += seqSz[0]; |
wolfSSL | 15:117db924cf7c | 13237 | |
wolfSSL | 15:117db924cf7c | 13238 | XMEMCPY(output + totalSz, req->nonce, req->nonceSz); |
wolfSSL | 15:117db924cf7c | 13239 | totalSz += req->nonceSz; |
wolfSSL | 15:117db924cf7c | 13240 | |
wolfSSL | 15:117db924cf7c | 13241 | return totalSz; |
wolfSSL | 15:117db924cf7c | 13242 | } |
wolfSSL | 15:117db924cf7c | 13243 | |
wolfSSL | 15:117db924cf7c | 13244 | |
wolfSSL | 15:117db924cf7c | 13245 | int EncodeOcspRequest(OcspRequest* req, byte* output, word32 size) |
wolfSSL | 15:117db924cf7c | 13246 | { |
wolfSSL | 15:117db924cf7c | 13247 | byte seqArray[5][MAX_SEQ_SZ]; |
wolfSSL | 15:117db924cf7c | 13248 | /* The ASN.1 of the OCSP Request is an onion of sequences */ |
wolfSSL | 15:117db924cf7c | 13249 | byte algoArray[MAX_ALGO_SZ]; |
wolfSSL | 15:117db924cf7c | 13250 | byte issuerArray[MAX_ENCODED_DIG_SZ]; |
wolfSSL | 15:117db924cf7c | 13251 | byte issuerKeyArray[MAX_ENCODED_DIG_SZ]; |
wolfSSL | 15:117db924cf7c | 13252 | byte snArray[MAX_SN_SZ]; |
wolfSSL | 15:117db924cf7c | 13253 | byte extArray[MAX_OCSP_EXT_SZ]; |
wolfSSL | 15:117db924cf7c | 13254 | word32 seqSz[5], algoSz, issuerSz, issuerKeySz, extSz, totalSz; |
wolfSSL | 15:117db924cf7c | 13255 | int i, snSz; |
wolfSSL | 15:117db924cf7c | 13256 | |
wolfSSL | 15:117db924cf7c | 13257 | WOLFSSL_ENTER("EncodeOcspRequest"); |
wolfSSL | 15:117db924cf7c | 13258 | |
wolfSSL | 15:117db924cf7c | 13259 | #ifdef NO_SHA |
wolfSSL | 15:117db924cf7c | 13260 | algoSz = SetAlgoID(SHA256h, algoArray, oidHashType, 0); |
wolfSSL | 15:117db924cf7c | 13261 | #else |
wolfSSL | 15:117db924cf7c | 13262 | algoSz = SetAlgoID(SHAh, algoArray, oidHashType, 0); |
wolfSSL | 15:117db924cf7c | 13263 | #endif |
wolfSSL | 15:117db924cf7c | 13264 | |
wolfSSL | 15:117db924cf7c | 13265 | issuerSz = SetDigest(req->issuerHash, KEYID_SIZE, issuerArray); |
wolfSSL | 15:117db924cf7c | 13266 | issuerKeySz = SetDigest(req->issuerKeyHash, KEYID_SIZE, issuerKeyArray); |
wolfSSL | 15:117db924cf7c | 13267 | snSz = SetSerialNumber(req->serial, req->serialSz, snArray, MAX_SN_SZ); |
wolfSSL | 15:117db924cf7c | 13268 | extSz = 0; |
wolfSSL | 15:117db924cf7c | 13269 | |
wolfSSL | 15:117db924cf7c | 13270 | if (snSz < 0) |
wolfSSL | 15:117db924cf7c | 13271 | return snSz; |
wolfSSL | 15:117db924cf7c | 13272 | |
wolfSSL | 15:117db924cf7c | 13273 | if (req->nonceSz) { |
wolfSSL | 15:117db924cf7c | 13274 | /* TLS Extensions use this function too - put extensions after |
wolfSSL | 15:117db924cf7c | 13275 | * ASN.1: Context Specific [2]. |
wolfSSL | 15:117db924cf7c | 13276 | */ |
wolfSSL | 15:117db924cf7c | 13277 | extSz = EncodeOcspRequestExtensions(req, extArray + 2, |
wolfSSL | 15:117db924cf7c | 13278 | OCSP_NONCE_EXT_SZ); |
wolfSSL | 15:117db924cf7c | 13279 | extSz += SetExplicit(2, extSz, extArray); |
wolfSSL | 15:117db924cf7c | 13280 | } |
wolfSSL | 15:117db924cf7c | 13281 | |
wolfSSL | 15:117db924cf7c | 13282 | totalSz = algoSz + issuerSz + issuerKeySz + snSz; |
wolfSSL | 15:117db924cf7c | 13283 | for (i = 4; i >= 0; i--) { |
wolfSSL | 15:117db924cf7c | 13284 | seqSz[i] = SetSequence(totalSz, seqArray[i]); |
wolfSSL | 15:117db924cf7c | 13285 | totalSz += seqSz[i]; |
wolfSSL | 15:117db924cf7c | 13286 | if (i == 2) totalSz += extSz; |
wolfSSL | 15:117db924cf7c | 13287 | } |
wolfSSL | 15:117db924cf7c | 13288 | |
wolfSSL | 15:117db924cf7c | 13289 | if (output == NULL) |
wolfSSL | 15:117db924cf7c | 13290 | return totalSz; |
wolfSSL | 15:117db924cf7c | 13291 | if (totalSz > size) |
wolfSSL | 15:117db924cf7c | 13292 | return BUFFER_E; |
wolfSSL | 15:117db924cf7c | 13293 | |
wolfSSL | 15:117db924cf7c | 13294 | totalSz = 0; |
wolfSSL | 15:117db924cf7c | 13295 | for (i = 0; i < 5; i++) { |
wolfSSL | 15:117db924cf7c | 13296 | XMEMCPY(output + totalSz, seqArray[i], seqSz[i]); |
wolfSSL | 15:117db924cf7c | 13297 | totalSz += seqSz[i]; |
wolfSSL | 15:117db924cf7c | 13298 | } |
wolfSSL | 15:117db924cf7c | 13299 | |
wolfSSL | 15:117db924cf7c | 13300 | XMEMCPY(output + totalSz, algoArray, algoSz); |
wolfSSL | 15:117db924cf7c | 13301 | totalSz += algoSz; |
wolfSSL | 15:117db924cf7c | 13302 | |
wolfSSL | 15:117db924cf7c | 13303 | XMEMCPY(output + totalSz, issuerArray, issuerSz); |
wolfSSL | 15:117db924cf7c | 13304 | totalSz += issuerSz; |
wolfSSL | 15:117db924cf7c | 13305 | |
wolfSSL | 15:117db924cf7c | 13306 | XMEMCPY(output + totalSz, issuerKeyArray, issuerKeySz); |
wolfSSL | 15:117db924cf7c | 13307 | totalSz += issuerKeySz; |
wolfSSL | 15:117db924cf7c | 13308 | |
wolfSSL | 15:117db924cf7c | 13309 | XMEMCPY(output + totalSz, snArray, snSz); |
wolfSSL | 15:117db924cf7c | 13310 | totalSz += snSz; |
wolfSSL | 15:117db924cf7c | 13311 | |
wolfSSL | 15:117db924cf7c | 13312 | if (extSz != 0) { |
wolfSSL | 15:117db924cf7c | 13313 | XMEMCPY(output + totalSz, extArray, extSz); |
wolfSSL | 15:117db924cf7c | 13314 | totalSz += extSz; |
wolfSSL | 15:117db924cf7c | 13315 | } |
wolfSSL | 15:117db924cf7c | 13316 | |
wolfSSL | 15:117db924cf7c | 13317 | return totalSz; |
wolfSSL | 15:117db924cf7c | 13318 | } |
wolfSSL | 15:117db924cf7c | 13319 | |
wolfSSL | 15:117db924cf7c | 13320 | |
wolfSSL | 15:117db924cf7c | 13321 | int InitOcspRequest(OcspRequest* req, DecodedCert* cert, byte useNonce, |
wolfSSL | 15:117db924cf7c | 13322 | void* heap) |
wolfSSL | 15:117db924cf7c | 13323 | { |
wolfSSL | 15:117db924cf7c | 13324 | int ret; |
wolfSSL | 15:117db924cf7c | 13325 | |
wolfSSL | 15:117db924cf7c | 13326 | WOLFSSL_ENTER("InitOcspRequest"); |
wolfSSL | 15:117db924cf7c | 13327 | |
wolfSSL | 15:117db924cf7c | 13328 | if (req == NULL) |
wolfSSL | 15:117db924cf7c | 13329 | return BAD_FUNC_ARG; |
wolfSSL | 15:117db924cf7c | 13330 | |
wolfSSL | 15:117db924cf7c | 13331 | ForceZero(req, sizeof(OcspRequest)); |
wolfSSL | 15:117db924cf7c | 13332 | req->heap = heap; |
wolfSSL | 15:117db924cf7c | 13333 | |
wolfSSL | 15:117db924cf7c | 13334 | if (cert) { |
wolfSSL | 15:117db924cf7c | 13335 | XMEMCPY(req->issuerHash, cert->issuerHash, KEYID_SIZE); |
wolfSSL | 15:117db924cf7c | 13336 | XMEMCPY(req->issuerKeyHash, cert->issuerKeyHash, KEYID_SIZE); |
wolfSSL | 15:117db924cf7c | 13337 | |
wolfSSL | 15:117db924cf7c | 13338 | req->serial = (byte*)XMALLOC(cert->serialSz, req->heap, |
wolfSSL | 15:117db924cf7c | 13339 | DYNAMIC_TYPE_OCSP_REQUEST); |
wolfSSL | 15:117db924cf7c | 13340 | if (req->serial == NULL) |
wolfSSL | 15:117db924cf7c | 13341 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 13342 | |
wolfSSL | 15:117db924cf7c | 13343 | XMEMCPY(req->serial, cert->serial, cert->serialSz); |
wolfSSL | 15:117db924cf7c | 13344 | req->serialSz = cert->serialSz; |
wolfSSL | 15:117db924cf7c | 13345 | |
wolfSSL | 15:117db924cf7c | 13346 | if (cert->extAuthInfoSz != 0 && cert->extAuthInfo != NULL) { |
wolfSSL | 15:117db924cf7c | 13347 | req->url = (byte*)XMALLOC(cert->extAuthInfoSz, req->heap, |
wolfSSL | 15:117db924cf7c | 13348 | DYNAMIC_TYPE_OCSP_REQUEST); |
wolfSSL | 15:117db924cf7c | 13349 | if (req->url == NULL) { |
wolfSSL | 15:117db924cf7c | 13350 | XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP); |
wolfSSL | 15:117db924cf7c | 13351 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 13352 | } |
wolfSSL | 15:117db924cf7c | 13353 | |
wolfSSL | 15:117db924cf7c | 13354 | XMEMCPY(req->url, cert->extAuthInfo, cert->extAuthInfoSz); |
wolfSSL | 15:117db924cf7c | 13355 | req->urlSz = cert->extAuthInfoSz; |
wolfSSL | 15:117db924cf7c | 13356 | } |
wolfSSL | 15:117db924cf7c | 13357 | } |
wolfSSL | 15:117db924cf7c | 13358 | |
wolfSSL | 15:117db924cf7c | 13359 | if (useNonce) { |
wolfSSL | 15:117db924cf7c | 13360 | WC_RNG rng; |
wolfSSL | 15:117db924cf7c | 13361 | |
wolfSSL | 15:117db924cf7c | 13362 | #ifndef HAVE_FIPS |
wolfSSL | 15:117db924cf7c | 13363 | ret = wc_InitRng_ex(&rng, req->heap, INVALID_DEVID); |
wolfSSL | 15:117db924cf7c | 13364 | #else |
wolfSSL | 15:117db924cf7c | 13365 | ret = wc_InitRng(&rng); |
wolfSSL | 15:117db924cf7c | 13366 | #endif |
wolfSSL | 15:117db924cf7c | 13367 | if (ret != 0) { |
wolfSSL | 15:117db924cf7c | 13368 | WOLFSSL_MSG("\tCannot initialize RNG. Skipping the OSCP Nonce."); |
wolfSSL | 15:117db924cf7c | 13369 | } else { |
wolfSSL | 15:117db924cf7c | 13370 | if (wc_RNG_GenerateBlock(&rng, req->nonce, MAX_OCSP_NONCE_SZ) != 0) |
wolfSSL | 15:117db924cf7c | 13371 | WOLFSSL_MSG("\tCannot run RNG. Skipping the OSCP Nonce."); |
wolfSSL | 15:117db924cf7c | 13372 | else |
wolfSSL | 15:117db924cf7c | 13373 | req->nonceSz = MAX_OCSP_NONCE_SZ; |
wolfSSL | 15:117db924cf7c | 13374 | |
wolfSSL | 15:117db924cf7c | 13375 | wc_FreeRng(&rng); |
wolfSSL | 15:117db924cf7c | 13376 | } |
wolfSSL | 15:117db924cf7c | 13377 | } |
wolfSSL | 15:117db924cf7c | 13378 | |
wolfSSL | 15:117db924cf7c | 13379 | return 0; |
wolfSSL | 15:117db924cf7c | 13380 | } |
wolfSSL | 15:117db924cf7c | 13381 | |
wolfSSL | 15:117db924cf7c | 13382 | void FreeOcspRequest(OcspRequest* req) |
wolfSSL | 15:117db924cf7c | 13383 | { |
wolfSSL | 15:117db924cf7c | 13384 | WOLFSSL_ENTER("FreeOcspRequest"); |
wolfSSL | 15:117db924cf7c | 13385 | |
wolfSSL | 15:117db924cf7c | 13386 | if (req) { |
wolfSSL | 15:117db924cf7c | 13387 | if (req->serial) |
wolfSSL | 15:117db924cf7c | 13388 | XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP_REQUEST); |
wolfSSL | 15:117db924cf7c | 13389 | |
wolfSSL | 15:117db924cf7c | 13390 | if (req->url) |
wolfSSL | 15:117db924cf7c | 13391 | XFREE(req->url, req->heap, DYNAMIC_TYPE_OCSP_REQUEST); |
wolfSSL | 15:117db924cf7c | 13392 | } |
wolfSSL | 15:117db924cf7c | 13393 | } |
wolfSSL | 15:117db924cf7c | 13394 | |
wolfSSL | 15:117db924cf7c | 13395 | |
wolfSSL | 15:117db924cf7c | 13396 | int CompareOcspReqResp(OcspRequest* req, OcspResponse* resp) |
wolfSSL | 15:117db924cf7c | 13397 | { |
wolfSSL | 15:117db924cf7c | 13398 | int cmp; |
wolfSSL | 15:117db924cf7c | 13399 | |
wolfSSL | 15:117db924cf7c | 13400 | WOLFSSL_ENTER("CompareOcspReqResp"); |
wolfSSL | 15:117db924cf7c | 13401 | |
wolfSSL | 15:117db924cf7c | 13402 | if (req == NULL) |
wolfSSL | 15:117db924cf7c | 13403 | { |
wolfSSL | 15:117db924cf7c | 13404 | WOLFSSL_MSG("\tReq missing"); |
wolfSSL | 15:117db924cf7c | 13405 | return -1; |
wolfSSL | 15:117db924cf7c | 13406 | } |
wolfSSL | 15:117db924cf7c | 13407 | |
wolfSSL | 15:117db924cf7c | 13408 | if (resp == NULL) |
wolfSSL | 15:117db924cf7c | 13409 | { |
wolfSSL | 15:117db924cf7c | 13410 | WOLFSSL_MSG("\tResp missing"); |
wolfSSL | 15:117db924cf7c | 13411 | return 1; |
wolfSSL | 15:117db924cf7c | 13412 | } |
wolfSSL | 15:117db924cf7c | 13413 | |
wolfSSL | 15:117db924cf7c | 13414 | /* Nonces are not critical. The responder may not necessarily add |
wolfSSL | 15:117db924cf7c | 13415 | * the nonce to the response. */ |
wolfSSL | 15:117db924cf7c | 13416 | if (resp->nonceSz != 0) { |
wolfSSL | 15:117db924cf7c | 13417 | cmp = req->nonceSz - resp->nonceSz; |
wolfSSL | 15:117db924cf7c | 13418 | if (cmp != 0) |
wolfSSL | 15:117db924cf7c | 13419 | { |
wolfSSL | 15:117db924cf7c | 13420 | WOLFSSL_MSG("\tnonceSz mismatch"); |
wolfSSL | 15:117db924cf7c | 13421 | return cmp; |
wolfSSL | 15:117db924cf7c | 13422 | } |
wolfSSL | 15:117db924cf7c | 13423 | |
wolfSSL | 15:117db924cf7c | 13424 | cmp = XMEMCMP(req->nonce, resp->nonce, req->nonceSz); |
wolfSSL | 15:117db924cf7c | 13425 | if (cmp != 0) |
wolfSSL | 15:117db924cf7c | 13426 | { |
wolfSSL | 15:117db924cf7c | 13427 | WOLFSSL_MSG("\tnonce mismatch"); |
wolfSSL | 15:117db924cf7c | 13428 | return cmp; |
wolfSSL | 15:117db924cf7c | 13429 | } |
wolfSSL | 15:117db924cf7c | 13430 | } |
wolfSSL | 15:117db924cf7c | 13431 | |
wolfSSL | 15:117db924cf7c | 13432 | cmp = XMEMCMP(req->issuerHash, resp->issuerHash, KEYID_SIZE); |
wolfSSL | 15:117db924cf7c | 13433 | if (cmp != 0) |
wolfSSL | 15:117db924cf7c | 13434 | { |
wolfSSL | 15:117db924cf7c | 13435 | WOLFSSL_MSG("\tissuerHash mismatch"); |
wolfSSL | 15:117db924cf7c | 13436 | return cmp; |
wolfSSL | 15:117db924cf7c | 13437 | } |
wolfSSL | 15:117db924cf7c | 13438 | |
wolfSSL | 15:117db924cf7c | 13439 | cmp = XMEMCMP(req->issuerKeyHash, resp->issuerKeyHash, KEYID_SIZE); |
wolfSSL | 15:117db924cf7c | 13440 | if (cmp != 0) |
wolfSSL | 15:117db924cf7c | 13441 | { |
wolfSSL | 15:117db924cf7c | 13442 | WOLFSSL_MSG("\tissuerKeyHash mismatch"); |
wolfSSL | 15:117db924cf7c | 13443 | return cmp; |
wolfSSL | 15:117db924cf7c | 13444 | } |
wolfSSL | 15:117db924cf7c | 13445 | |
wolfSSL | 15:117db924cf7c | 13446 | cmp = req->serialSz - resp->status->serialSz; |
wolfSSL | 15:117db924cf7c | 13447 | if (cmp != 0) |
wolfSSL | 15:117db924cf7c | 13448 | { |
wolfSSL | 15:117db924cf7c | 13449 | WOLFSSL_MSG("\tserialSz mismatch"); |
wolfSSL | 15:117db924cf7c | 13450 | return cmp; |
wolfSSL | 15:117db924cf7c | 13451 | } |
wolfSSL | 15:117db924cf7c | 13452 | |
wolfSSL | 15:117db924cf7c | 13453 | cmp = XMEMCMP(req->serial, resp->status->serial, req->serialSz); |
wolfSSL | 15:117db924cf7c | 13454 | if (cmp != 0) |
wolfSSL | 15:117db924cf7c | 13455 | { |
wolfSSL | 15:117db924cf7c | 13456 | WOLFSSL_MSG("\tserial mismatch"); |
wolfSSL | 15:117db924cf7c | 13457 | return cmp; |
wolfSSL | 15:117db924cf7c | 13458 | } |
wolfSSL | 15:117db924cf7c | 13459 | |
wolfSSL | 15:117db924cf7c | 13460 | return 0; |
wolfSSL | 15:117db924cf7c | 13461 | } |
wolfSSL | 15:117db924cf7c | 13462 | |
wolfSSL | 15:117db924cf7c | 13463 | #endif |
wolfSSL | 15:117db924cf7c | 13464 | |
wolfSSL | 15:117db924cf7c | 13465 | |
wolfSSL | 15:117db924cf7c | 13466 | /* store WC_SHA hash of NAME */ |
wolfSSL | 15:117db924cf7c | 13467 | WOLFSSL_LOCAL int GetNameHash(const byte* source, word32* idx, byte* hash, |
wolfSSL | 15:117db924cf7c | 13468 | int maxIdx) |
wolfSSL | 15:117db924cf7c | 13469 | { |
wolfSSL | 15:117db924cf7c | 13470 | int length; /* length of all distinguished names */ |
wolfSSL | 15:117db924cf7c | 13471 | int ret; |
wolfSSL | 15:117db924cf7c | 13472 | word32 dummy; |
wolfSSL | 15:117db924cf7c | 13473 | |
wolfSSL | 15:117db924cf7c | 13474 | WOLFSSL_ENTER("GetNameHash"); |
wolfSSL | 15:117db924cf7c | 13475 | |
wolfSSL | 15:117db924cf7c | 13476 | if (source[*idx] == ASN_OBJECT_ID) { |
wolfSSL | 15:117db924cf7c | 13477 | WOLFSSL_MSG("Trying optional prefix..."); |
wolfSSL | 15:117db924cf7c | 13478 | |
wolfSSL | 15:117db924cf7c | 13479 | if (GetLength(source, idx, &length, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 13480 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13481 | |
wolfSSL | 15:117db924cf7c | 13482 | *idx += length; |
wolfSSL | 15:117db924cf7c | 13483 | WOLFSSL_MSG("Got optional prefix"); |
wolfSSL | 15:117db924cf7c | 13484 | } |
wolfSSL | 15:117db924cf7c | 13485 | |
wolfSSL | 15:117db924cf7c | 13486 | /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be |
wolfSSL | 15:117db924cf7c | 13487 | * calculated over the entire DER encoding of the Name field, including |
wolfSSL | 15:117db924cf7c | 13488 | * the tag and length. */ |
wolfSSL | 15:117db924cf7c | 13489 | dummy = *idx; |
wolfSSL | 15:117db924cf7c | 13490 | if (GetSequence(source, idx, &length, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 13491 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13492 | |
wolfSSL | 15:117db924cf7c | 13493 | #ifdef NO_SHA |
wolfSSL | 15:117db924cf7c | 13494 | ret = wc_Sha256Hash(source + dummy, length + *idx - dummy, hash); |
wolfSSL | 15:117db924cf7c | 13495 | #else |
wolfSSL | 15:117db924cf7c | 13496 | ret = wc_ShaHash(source + dummy, length + *idx - dummy, hash); |
wolfSSL | 15:117db924cf7c | 13497 | #endif |
wolfSSL | 15:117db924cf7c | 13498 | |
wolfSSL | 15:117db924cf7c | 13499 | *idx += length; |
wolfSSL | 15:117db924cf7c | 13500 | |
wolfSSL | 15:117db924cf7c | 13501 | return ret; |
wolfSSL | 15:117db924cf7c | 13502 | } |
wolfSSL | 15:117db924cf7c | 13503 | |
wolfSSL | 15:117db924cf7c | 13504 | |
wolfSSL | 15:117db924cf7c | 13505 | #ifdef HAVE_CRL |
wolfSSL | 15:117db924cf7c | 13506 | |
wolfSSL | 15:117db924cf7c | 13507 | /* initialize decoded CRL */ |
wolfSSL | 15:117db924cf7c | 13508 | void InitDecodedCRL(DecodedCRL* dcrl, void* heap) |
wolfSSL | 15:117db924cf7c | 13509 | { |
wolfSSL | 15:117db924cf7c | 13510 | WOLFSSL_MSG("InitDecodedCRL"); |
wolfSSL | 15:117db924cf7c | 13511 | |
wolfSSL | 15:117db924cf7c | 13512 | dcrl->certBegin = 0; |
wolfSSL | 15:117db924cf7c | 13513 | dcrl->sigIndex = 0; |
wolfSSL | 15:117db924cf7c | 13514 | dcrl->sigLength = 0; |
wolfSSL | 15:117db924cf7c | 13515 | dcrl->signatureOID = 0; |
wolfSSL | 15:117db924cf7c | 13516 | dcrl->certs = NULL; |
wolfSSL | 15:117db924cf7c | 13517 | dcrl->totalCerts = 0; |
wolfSSL | 15:117db924cf7c | 13518 | dcrl->heap = heap; |
wolfSSL | 15:117db924cf7c | 13519 | #ifdef WOLFSSL_HEAP_TEST |
wolfSSL | 15:117db924cf7c | 13520 | dcrl->heap = (void*)WOLFSSL_HEAP_TEST; |
wolfSSL | 15:117db924cf7c | 13521 | #endif |
wolfSSL | 15:117db924cf7c | 13522 | } |
wolfSSL | 15:117db924cf7c | 13523 | |
wolfSSL | 15:117db924cf7c | 13524 | |
wolfSSL | 15:117db924cf7c | 13525 | /* free decoded CRL resources */ |
wolfSSL | 15:117db924cf7c | 13526 | void FreeDecodedCRL(DecodedCRL* dcrl) |
wolfSSL | 15:117db924cf7c | 13527 | { |
wolfSSL | 15:117db924cf7c | 13528 | RevokedCert* tmp = dcrl->certs; |
wolfSSL | 15:117db924cf7c | 13529 | |
wolfSSL | 15:117db924cf7c | 13530 | WOLFSSL_MSG("FreeDecodedCRL"); |
wolfSSL | 15:117db924cf7c | 13531 | |
wolfSSL | 15:117db924cf7c | 13532 | while(tmp) { |
wolfSSL | 15:117db924cf7c | 13533 | RevokedCert* next = tmp->next; |
wolfSSL | 15:117db924cf7c | 13534 | XFREE(tmp, dcrl->heap, DYNAMIC_TYPE_REVOKED); |
wolfSSL | 15:117db924cf7c | 13535 | tmp = next; |
wolfSSL | 15:117db924cf7c | 13536 | } |
wolfSSL | 15:117db924cf7c | 13537 | } |
wolfSSL | 15:117db924cf7c | 13538 | |
wolfSSL | 15:117db924cf7c | 13539 | |
wolfSSL | 15:117db924cf7c | 13540 | /* Get Revoked Cert list, 0 on success */ |
wolfSSL | 15:117db924cf7c | 13541 | static int GetRevoked(const byte* buff, word32* idx, DecodedCRL* dcrl, |
wolfSSL | 15:117db924cf7c | 13542 | int maxIdx) |
wolfSSL | 15:117db924cf7c | 13543 | { |
wolfSSL | 15:117db924cf7c | 13544 | int ret, len; |
wolfSSL | 15:117db924cf7c | 13545 | word32 end; |
wolfSSL | 15:117db924cf7c | 13546 | byte b; |
wolfSSL | 15:117db924cf7c | 13547 | RevokedCert* rc; |
wolfSSL | 15:117db924cf7c | 13548 | |
wolfSSL | 15:117db924cf7c | 13549 | WOLFSSL_ENTER("GetRevoked"); |
wolfSSL | 15:117db924cf7c | 13550 | |
wolfSSL | 15:117db924cf7c | 13551 | if (GetSequence(buff, idx, &len, maxIdx) < 0) |
wolfSSL | 15:117db924cf7c | 13552 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13553 | |
wolfSSL | 15:117db924cf7c | 13554 | end = *idx + len; |
wolfSSL | 15:117db924cf7c | 13555 | |
wolfSSL | 15:117db924cf7c | 13556 | rc = (RevokedCert*)XMALLOC(sizeof(RevokedCert), dcrl->heap, |
wolfSSL | 15:117db924cf7c | 13557 | DYNAMIC_TYPE_REVOKED); |
wolfSSL | 15:117db924cf7c | 13558 | if (rc == NULL) { |
wolfSSL | 15:117db924cf7c | 13559 | WOLFSSL_MSG("Alloc Revoked Cert failed"); |
wolfSSL | 15:117db924cf7c | 13560 | return MEMORY_E; |
wolfSSL | 15:117db924cf7c | 13561 | } |
wolfSSL | 15:117db924cf7c | 13562 | |
wolfSSL | 15:117db924cf7c | 13563 | if (GetSerialNumber(buff, idx, rc->serialNumber, &rc->serialSz, |
wolfSSL | 15:117db924cf7c | 13564 | maxIdx) < 0) { |
wolfSSL | 15:117db924cf7c | 13565 | XFREE(rc, dcrl->heap, DYNAMIC_TYPE_REVOKED); |
wolfSSL | 15:117db924cf7c | 13566 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13567 | } |
wolfSSL | 15:117db924cf7c | 13568 | |
wolfSSL | 15:117db924cf7c | 13569 | /* add to list */ |
wolfSSL | 15:117db924cf7c | 13570 | rc->next = dcrl->certs; |
wolfSSL | 15:117db924cf7c | 13571 | dcrl->certs = rc; |
wolfSSL | 15:117db924cf7c | 13572 | dcrl->totalCerts++; |
wolfSSL | 15:117db924cf7c | 13573 | |
wolfSSL | 15:117db924cf7c | 13574 | /* get date */ |
wolfSSL | 15:117db924cf7c | 13575 | ret = GetDateInfo(buff, idx, NULL, &b, NULL, maxIdx); |
wolfSSL | 15:117db924cf7c | 13576 | if (ret < 0) { |
wolfSSL | 15:117db924cf7c | 13577 | WOLFSSL_MSG("Expecting Date"); |
wolfSSL | 15:117db924cf7c | 13578 | return ret; |
wolfSSL | 15:117db924cf7c | 13579 | } |
wolfSSL | 15:117db924cf7c | 13580 | |
wolfSSL | 15:117db924cf7c | 13581 | if (*idx != end) /* skip extensions */ |
wolfSSL | 15:117db924cf7c | 13582 | *idx = end; |
wolfSSL | 15:117db924cf7c | 13583 | |
wolfSSL | 15:117db924cf7c | 13584 | return 0; |
wolfSSL | 15:117db924cf7c | 13585 | } |
wolfSSL | 15:117db924cf7c | 13586 | |
wolfSSL | 15:117db924cf7c | 13587 | |
wolfSSL | 15:117db924cf7c | 13588 | /* Get CRL Signature, 0 on success */ |
wolfSSL | 15:117db924cf7c | 13589 | static int GetCRL_Signature(const byte* source, word32* idx, DecodedCRL* dcrl, |
wolfSSL | 15:117db924cf7c | 13590 | int maxIdx) |
wolfSSL | 15:117db924cf7c | 13591 | { |
wolfSSL | 15:117db924cf7c | 13592 | int length; |
wolfSSL | 15:117db924cf7c | 13593 | int ret; |
wolfSSL | 15:117db924cf7c | 13594 | |
wolfSSL | 15:117db924cf7c | 13595 | WOLFSSL_ENTER("GetCRL_Signature"); |
wolfSSL | 15:117db924cf7c | 13596 | |
wolfSSL | 15:117db924cf7c | 13597 | ret = CheckBitString(source, idx, &length, maxIdx, 1, NULL); |
wolfSSL | 15:117db924cf7c | 13598 | if (ret != 0) |
wolfSSL | 15:117db924cf7c | 13599 | return ret; |
wolfSSL | 15:117db924cf7c | 13600 | dcrl->sigLength = length; |
wolfSSL | 15:117db924cf7c | 13601 | |
wolfSSL | 15:117db924cf7c | 13602 | dcrl->signature = (byte*)&source[*idx]; |
wolfSSL | 15:117db924cf7c | 13603 | *idx += dcrl->sigLength; |
wolfSSL | 15:117db924cf7c | 13604 | |
wolfSSL | 15:117db924cf7c | 13605 | return 0; |
wolfSSL | 15:117db924cf7c | 13606 | } |
wolfSSL | 15:117db924cf7c | 13607 | |
wolfSSL | 15:117db924cf7c | 13608 | int VerifyCRL_Signature(SignatureCtx* sigCtx, const byte* toBeSigned, |
wolfSSL | 15:117db924cf7c | 13609 | word32 tbsSz, const byte* signature, word32 sigSz, |
wolfSSL | 15:117db924cf7c | 13610 | word32 signatureOID, Signer *ca, void* heap) |
wolfSSL | 15:117db924cf7c | 13611 | { |
wolfSSL | 15:117db924cf7c | 13612 | /* try to confirm/verify signature */ |
wolfSSL | 15:117db924cf7c | 13613 | #ifndef IGNORE_KEY_EXTENSIONS |
wolfSSL | 15:117db924cf7c | 13614 | if ((ca->keyUsage & KEYUSE_CRL_SIGN) == 0) { |
wolfSSL | 15:117db924cf7c | 13615 | WOLFSSL_MSG("CA cannot sign CRLs"); |
wolfSSL | 15:117db924cf7c | 13616 | return ASN_CRL_NO_SIGNER_E; |
wolfSSL | 15:117db924cf7c | 13617 | } |
wolfSSL | 15:117db924cf7c | 13618 | #endif /* IGNORE_KEY_EXTENSIONS */ |
wolfSSL | 15:117db924cf7c | 13619 | |
wolfSSL | 15:117db924cf7c | 13620 | InitSignatureCtx(sigCtx, heap, INVALID_DEVID); |
wolfSSL | 15:117db924cf7c | 13621 | if (ConfirmSignature(sigCtx, toBeSigned, tbsSz, ca->publicKey, |
wolfSSL | 15:117db924cf7c | 13622 | ca->pubKeySize, ca->keyOID, signature, sigSz, |
wolfSSL | 15:117db924cf7c | 13623 | signatureOID) != 0) { |
wolfSSL | 15:117db924cf7c | 13624 | WOLFSSL_MSG("CRL Confirm signature failed"); |
wolfSSL | 15:117db924cf7c | 13625 | return ASN_CRL_CONFIRM_E; |
wolfSSL | 15:117db924cf7c | 13626 | } |
wolfSSL | 15:117db924cf7c | 13627 | |
wolfSSL | 15:117db924cf7c | 13628 | return 0; |
wolfSSL | 15:117db924cf7c | 13629 | } |
wolfSSL | 15:117db924cf7c | 13630 | |
wolfSSL | 15:117db924cf7c | 13631 | /* prase crl buffer into decoded state, 0 on success */ |
wolfSSL | 15:117db924cf7c | 13632 | int ParseCRL(DecodedCRL* dcrl, const byte* buff, word32 sz, void* cm) |
wolfSSL | 15:117db924cf7c | 13633 | { |
wolfSSL | 15:117db924cf7c | 13634 | int version, len, doNextDate = 1; |
wolfSSL | 15:117db924cf7c | 13635 | word32 oid, idx = 0, dateIdx; |
wolfSSL | 15:117db924cf7c | 13636 | Signer* ca = NULL; |
wolfSSL | 15:117db924cf7c | 13637 | SignatureCtx sigCtx; |
wolfSSL | 15:117db924cf7c | 13638 | |
wolfSSL | 15:117db924cf7c | 13639 | WOLFSSL_MSG("ParseCRL"); |
wolfSSL | 15:117db924cf7c | 13640 | |
wolfSSL | 15:117db924cf7c | 13641 | /* raw crl hash */ |
wolfSSL | 15:117db924cf7c | 13642 | /* hash here if needed for optimized comparisons |
wolfSSL | 15:117db924cf7c | 13643 | * wc_Sha sha; |
wolfSSL | 15:117db924cf7c | 13644 | * wc_InitSha(&sha); |
wolfSSL | 15:117db924cf7c | 13645 | * wc_ShaUpdate(&sha, buff, sz); |
wolfSSL | 15:117db924cf7c | 13646 | * wc_ShaFinal(&sha, dcrl->crlHash); */ |
wolfSSL | 15:117db924cf7c | 13647 | |
wolfSSL | 15:117db924cf7c | 13648 | if (GetSequence(buff, &idx, &len, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13649 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13650 | |
wolfSSL | 15:117db924cf7c | 13651 | dcrl->certBegin = idx; |
wolfSSL | 15:117db924cf7c | 13652 | |
wolfSSL | 15:117db924cf7c | 13653 | if (GetSequence(buff, &idx, &len, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13654 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13655 | dcrl->sigIndex = len + idx; |
wolfSSL | 15:117db924cf7c | 13656 | |
wolfSSL | 15:117db924cf7c | 13657 | /* may have version */ |
wolfSSL | 15:117db924cf7c | 13658 | if (buff[idx] == ASN_INTEGER) { |
wolfSSL | 15:117db924cf7c | 13659 | if (GetMyVersion(buff, &idx, &version, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13660 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13661 | } |
wolfSSL | 15:117db924cf7c | 13662 | |
wolfSSL | 15:117db924cf7c | 13663 | if (GetAlgoId(buff, &idx, &oid, oidIgnoreType, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13664 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13665 | |
wolfSSL | 15:117db924cf7c | 13666 | if (GetNameHash(buff, &idx, dcrl->issuerHash, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13667 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13668 | |
wolfSSL | 15:117db924cf7c | 13669 | if (GetBasicDate(buff, &idx, dcrl->lastDate, &dcrl->lastDateFormat, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13670 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13671 | |
wolfSSL | 15:117db924cf7c | 13672 | dateIdx = idx; |
wolfSSL | 15:117db924cf7c | 13673 | |
wolfSSL | 15:117db924cf7c | 13674 | if (GetBasicDate(buff, &idx, dcrl->nextDate, &dcrl->nextDateFormat, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13675 | { |
wolfSSL | 15:117db924cf7c | 13676 | #ifndef WOLFSSL_NO_CRL_NEXT_DATE |
wolfSSL | 15:117db924cf7c | 13677 | (void)dateIdx; |
wolfSSL | 15:117db924cf7c | 13678 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13679 | #else |
wolfSSL | 15:117db924cf7c | 13680 | dcrl->nextDateFormat = ASN_OTHER_TYPE; /* skip flag */ |
wolfSSL | 15:117db924cf7c | 13681 | doNextDate = 0; |
wolfSSL | 15:117db924cf7c | 13682 | idx = dateIdx; |
wolfSSL | 15:117db924cf7c | 13683 | #endif |
wolfSSL | 15:117db924cf7c | 13684 | } |
wolfSSL | 15:117db924cf7c | 13685 | |
wolfSSL | 15:117db924cf7c | 13686 | if (doNextDate) { |
wolfSSL | 15:117db924cf7c | 13687 | #ifndef NO_ASN_TIME |
wolfSSL | 15:117db924cf7c | 13688 | if (!XVALIDATE_DATE(dcrl->nextDate, dcrl->nextDateFormat, AFTER)) { |
wolfSSL | 15:117db924cf7c | 13689 | WOLFSSL_MSG("CRL after date is no longer valid"); |
wolfSSL | 15:117db924cf7c | 13690 | return ASN_AFTER_DATE_E; |
wolfSSL | 15:117db924cf7c | 13691 | } |
wolfSSL | 15:117db924cf7c | 13692 | #endif |
wolfSSL | 15:117db924cf7c | 13693 | } |
wolfSSL | 15:117db924cf7c | 13694 | |
wolfSSL | 15:117db924cf7c | 13695 | if (idx != dcrl->sigIndex && buff[idx] != CRL_EXTENSIONS) { |
wolfSSL | 15:117db924cf7c | 13696 | if (GetSequence(buff, &idx, &len, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13697 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13698 | |
wolfSSL | 15:117db924cf7c | 13699 | len += idx; |
wolfSSL | 15:117db924cf7c | 13700 | |
wolfSSL | 15:117db924cf7c | 13701 | while (idx < (word32)len) { |
wolfSSL | 15:117db924cf7c | 13702 | if (GetRevoked(buff, &idx, dcrl, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13703 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13704 | } |
wolfSSL | 15:117db924cf7c | 13705 | } |
wolfSSL | 15:117db924cf7c | 13706 | |
wolfSSL | 15:117db924cf7c | 13707 | if (idx != dcrl->sigIndex) |
wolfSSL | 15:117db924cf7c | 13708 | idx = dcrl->sigIndex; /* skip extensions */ |
wolfSSL | 15:117db924cf7c | 13709 | |
wolfSSL | 15:117db924cf7c | 13710 | if (GetAlgoId(buff, &idx, &dcrl->signatureOID, oidSigType, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13711 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13712 | |
wolfSSL | 15:117db924cf7c | 13713 | if (GetCRL_Signature(buff, &idx, dcrl, sz) < 0) |
wolfSSL | 15:117db924cf7c | 13714 | return ASN_PARSE_E; |
wolfSSL | 15:117db924cf7c | 13715 | |
wolfSSL | 15:117db924cf7c | 13716 | /* openssl doesn't add skid by default for CRLs cause firefox chokes |
wolfSSL | 15:117db924cf7c | 13717 | we're not assuming it's available yet */ |
wolfSSL | 15:117db924cf7c | 13718 | #if !defined(NO_SKID) && defined(CRL_SKID_READY) |
wolfSSL | 15:117db924cf7c | 13719 | if (dcrl->extAuthKeyIdSet) |
wolfSSL | 15:117db924cf7c | 13720 | ca = GetCA(cm, dcrl->extAuthKeyId); |
wolfSSL | 15:117db924cf7c | 13721 | if (ca == NULL) |
wolfSSL | 15:117db924cf7c | 13722 | ca = GetCAByName(cm, dcrl->issuerHash); |
wolfSSL | 15:117db924cf7c | 13723 | #else |
wolfSSL | 15:117db924cf7c | 13724 | ca = GetCA(cm, dcrl->issuerHash); |
wolfSSL | 15:117db924cf7c | 13725 | #endif /* !NO_SKID && CRL_SKID_READY */ |
wolfSSL | 15:117db924cf7c | 13726 | WOLFSSL_MSG("About to verify CRL signature"); |
wolfSSL | 15:117db924cf7c | 13727 | |
wolfSSL | 15:117db924cf7c | 13728 | if (ca == NULL) { |
wolfSSL | 15:117db924cf7c | 13729 | WOLFSSL_MSG("Did NOT find CRL issuer CA"); |
wolfSSL | 15:117db924cf7c | 13730 | return ASN_CRL_NO_SIGNER_E; |
wolfSSL | 15:117db924cf7c | 13731 | } |
wolfSSL | 15:117db924cf7c | 13732 | |
wolfSSL | 15:117db924cf7c | 13733 | WOLFSSL_MSG("Found CRL issuer CA"); |
wolfSSL | 15:117db924cf7c | 13734 | return VerifyCRL_Signature(&sigCtx, buff + dcrl->certBegin, |
wolfSSL | 15:117db924cf7c | 13735 | dcrl->sigIndex - dcrl->certBegin, dcrl->signature, dcrl->sigLength, |
wolfSSL | 15:117db924cf7c | 13736 | dcrl->signatureOID, ca, dcrl->heap); |
wolfSSL | 15:117db924cf7c | 13737 | } |
wolfSSL | 15:117db924cf7c | 13738 | |
wolfSSL | 15:117db924cf7c | 13739 | #endif /* HAVE_CRL */ |
wolfSSL | 15:117db924cf7c | 13740 | |
wolfSSL | 15:117db924cf7c | 13741 | #undef ERROR_OUT |
wolfSSL | 15:117db924cf7c | 13742 | |
wolfSSL | 15:117db924cf7c | 13743 | #endif /* !NO_ASN */ |
wolfSSL | 15:117db924cf7c | 13744 | |
wolfSSL | 15:117db924cf7c | 13745 | #ifdef WOLFSSL_SEP |
wolfSSL | 15:117db924cf7c | 13746 | |
wolfSSL | 15:117db924cf7c | 13747 | |
wolfSSL | 15:117db924cf7c | 13748 | #endif /* WOLFSSL_SEP */ |
wolfSSL | 15:117db924cf7c | 13749 |