Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
d0773d
Date:
Tue Mar 03 22:52:52 2015 +0000
Revision:
4:28ac50e1d49c
Parent:
0:1239e9b70ca2
CyaSSL example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:1239e9b70ca2 1 /* asn.c
wolfSSL 0:1239e9b70ca2 2 *
wolfSSL 0:1239e9b70ca2 3 * Copyright (C) 2006-2014 wolfSSL Inc.
wolfSSL 0:1239e9b70ca2 4 *
wolfSSL 0:1239e9b70ca2 5 * This file is part of CyaSSL.
wolfSSL 0:1239e9b70ca2 6 *
wolfSSL 0:1239e9b70ca2 7 * CyaSSL is free software; you can redistribute it and/or modify
wolfSSL 0:1239e9b70ca2 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:1239e9b70ca2 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:1239e9b70ca2 10 * (at your option) any later version.
wolfSSL 0:1239e9b70ca2 11 *
wolfSSL 0:1239e9b70ca2 12 * CyaSSL is distributed in the hope that it will be useful,
wolfSSL 0:1239e9b70ca2 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:1239e9b70ca2 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:1239e9b70ca2 15 * GNU General Public License for more details.
wolfSSL 0:1239e9b70ca2 16 *
wolfSSL 0:1239e9b70ca2 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:1239e9b70ca2 18 * along with this program; if not, write to the Free Software
wolfSSL 0:1239e9b70ca2 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:1239e9b70ca2 20 */
wolfSSL 0:1239e9b70ca2 21
wolfSSL 0:1239e9b70ca2 22 #ifdef HAVE_CONFIG_H
wolfSSL 0:1239e9b70ca2 23 #include <config.h>
wolfSSL 0:1239e9b70ca2 24 #endif
wolfSSL 0:1239e9b70ca2 25
wolfSSL 0:1239e9b70ca2 26 #include <cyassl/ctaocrypt/settings.h>
wolfSSL 0:1239e9b70ca2 27
wolfSSL 0:1239e9b70ca2 28 #ifndef NO_ASN
wolfSSL 0:1239e9b70ca2 29
wolfSSL 0:1239e9b70ca2 30 #ifdef HAVE_RTP_SYS
wolfSSL 0:1239e9b70ca2 31 #include "os.h" /* dc_rtc_api needs */
wolfSSL 0:1239e9b70ca2 32 #include "dc_rtc_api.h" /* to get current time */
wolfSSL 0:1239e9b70ca2 33 #endif
wolfSSL 0:1239e9b70ca2 34
wolfSSL 0:1239e9b70ca2 35 #include <cyassl/ctaocrypt/integer.h>
wolfSSL 0:1239e9b70ca2 36 #include <cyassl/ctaocrypt/asn.h>
wolfSSL 0:1239e9b70ca2 37 #include <cyassl/ctaocrypt/coding.h>
wolfSSL 0:1239e9b70ca2 38 #include <cyassl/ctaocrypt/sha.h>
wolfSSL 0:1239e9b70ca2 39 #include <cyassl/ctaocrypt/md5.h>
wolfSSL 0:1239e9b70ca2 40 #include <cyassl/ctaocrypt/md2.h>
wolfSSL 0:1239e9b70ca2 41 #include <cyassl/ctaocrypt/error-crypt.h>
wolfSSL 0:1239e9b70ca2 42 #include <cyassl/ctaocrypt/pwdbased.h>
wolfSSL 0:1239e9b70ca2 43 #include <cyassl/ctaocrypt/des3.h>
wolfSSL 0:1239e9b70ca2 44 #include <cyassl/ctaocrypt/sha256.h>
wolfSSL 0:1239e9b70ca2 45 #include <cyassl/ctaocrypt/sha512.h>
wolfSSL 0:1239e9b70ca2 46 #include <cyassl/ctaocrypt/logging.h>
wolfSSL 0:1239e9b70ca2 47
wolfSSL 0:1239e9b70ca2 48 #include <cyassl/ctaocrypt/random.h>
wolfSSL 0:1239e9b70ca2 49
wolfSSL 0:1239e9b70ca2 50
wolfSSL 0:1239e9b70ca2 51 #ifndef NO_RC4
wolfSSL 0:1239e9b70ca2 52 #include <cyassl/ctaocrypt/arc4.h>
wolfSSL 0:1239e9b70ca2 53 #endif
wolfSSL 0:1239e9b70ca2 54
wolfSSL 0:1239e9b70ca2 55 #ifdef HAVE_NTRU
wolfSSL 0:1239e9b70ca2 56 #include "crypto_ntru.h"
wolfSSL 0:1239e9b70ca2 57 #endif
wolfSSL 0:1239e9b70ca2 58
wolfSSL 0:1239e9b70ca2 59 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 60 #include <cyassl/ctaocrypt/ecc.h>
wolfSSL 0:1239e9b70ca2 61 #endif
wolfSSL 0:1239e9b70ca2 62
wolfSSL 0:1239e9b70ca2 63 #ifdef CYASSL_DEBUG_ENCODING
wolfSSL 0:1239e9b70ca2 64 #ifdef FREESCALE_MQX
wolfSSL 0:1239e9b70ca2 65 #include <fio.h>
wolfSSL 0:1239e9b70ca2 66 #else
wolfSSL 0:1239e9b70ca2 67 #include <stdio.h>
wolfSSL 0:1239e9b70ca2 68 #endif
wolfSSL 0:1239e9b70ca2 69 #endif
wolfSSL 0:1239e9b70ca2 70
wolfSSL 0:1239e9b70ca2 71 #ifdef _MSC_VER
wolfSSL 0:1239e9b70ca2 72 /* 4996 warning to use MS extensions e.g., strcpy_s instead of XSTRNCPY */
wolfSSL 0:1239e9b70ca2 73 #pragma warning(disable: 4996)
wolfSSL 0:1239e9b70ca2 74 #endif
wolfSSL 0:1239e9b70ca2 75
wolfSSL 0:1239e9b70ca2 76
wolfSSL 0:1239e9b70ca2 77 #ifndef TRUE
wolfSSL 0:1239e9b70ca2 78 #define TRUE 1
wolfSSL 0:1239e9b70ca2 79 #endif
wolfSSL 0:1239e9b70ca2 80 #ifndef FALSE
wolfSSL 0:1239e9b70ca2 81 #define FALSE 0
wolfSSL 0:1239e9b70ca2 82 #endif
wolfSSL 0:1239e9b70ca2 83
wolfSSL 0:1239e9b70ca2 84
wolfSSL 0:1239e9b70ca2 85 #ifdef HAVE_RTP_SYS
wolfSSL 0:1239e9b70ca2 86 /* uses parital <time.h> structures */
wolfSSL 0:1239e9b70ca2 87 #define XTIME(tl) (0)
wolfSSL 0:1239e9b70ca2 88 #define XGMTIME(c) my_gmtime((c))
wolfSSL 0:1239e9b70ca2 89 #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
wolfSSL 0:1239e9b70ca2 90 #elif defined(MICRIUM)
wolfSSL 0:1239e9b70ca2 91 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
wolfSSL 0:1239e9b70ca2 92 #define XVALIDATE_DATE(d,f,t) NetSecure_ValidateDateHandler((d),(f),(t))
wolfSSL 0:1239e9b70ca2 93 #else
wolfSSL 0:1239e9b70ca2 94 #define XVALIDATE_DATE(d, f, t) (0)
wolfSSL 0:1239e9b70ca2 95 #endif
wolfSSL 0:1239e9b70ca2 96 #define NO_TIME_H
wolfSSL 0:1239e9b70ca2 97 /* since Micrium not defining XTIME or XGMTIME, CERT_GEN not available */
wolfSSL 0:1239e9b70ca2 98 #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
wolfSSL 0:1239e9b70ca2 99 #include <time.h>
wolfSSL 0:1239e9b70ca2 100 #define XTIME(t1) pic32_time((t1))
wolfSSL 0:1239e9b70ca2 101 #define XGMTIME(c) gmtime((c))
wolfSSL 0:1239e9b70ca2 102 #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
wolfSSL 0:1239e9b70ca2 103 #elif defined(FREESCALE_MQX)
wolfSSL 0:1239e9b70ca2 104 #include <time.h>
wolfSSL 0:1239e9b70ca2 105 #define XTIME(t1) mqx_time((t1))
wolfSSL 0:1239e9b70ca2 106 #define XGMTIME(c) gmtime((c))
wolfSSL 0:1239e9b70ca2 107 #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
wolfSSL 0:1239e9b70ca2 108 #elif defined(CYASSL_MDK_ARM)
wolfSSL 0:1239e9b70ca2 109 #if defined(CYASSL_MDK5)
wolfSSL 0:1239e9b70ca2 110 #include "cmsis_os.h"
wolfSSL 0:1239e9b70ca2 111 #else
wolfSSL 0:1239e9b70ca2 112 #include <rtl.h>
wolfSSL 0:1239e9b70ca2 113 #endif
wolfSSL 0:1239e9b70ca2 114 #undef RNG
wolfSSL 0:1239e9b70ca2 115 #include "cyassl_MDK_ARM.h"
wolfSSL 0:1239e9b70ca2 116 #undef RNG
wolfSSL 0:1239e9b70ca2 117 #define RNG CyaSSL_RNG /*for avoiding name conflict in "stm32f2xx.h" */
wolfSSL 0:1239e9b70ca2 118 #define XTIME(tl) (0)
wolfSSL 0:1239e9b70ca2 119 #define XGMTIME(c) Cyassl_MDK_gmtime((c))
wolfSSL 0:1239e9b70ca2 120 #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
wolfSSL 0:1239e9b70ca2 121 #elif defined(USER_TIME)
wolfSSL 0:1239e9b70ca2 122 /* user time, and gmtime compatible functions, there is a gmtime
wolfSSL 0:1239e9b70ca2 123 implementation here that WINCE uses, so really just need some ticks
wolfSSL 0:1239e9b70ca2 124 since the EPOCH
wolfSSL 0:1239e9b70ca2 125 */
wolfSSL 0:1239e9b70ca2 126
wolfSSL 0:1239e9b70ca2 127 struct tm {
wolfSSL 0:1239e9b70ca2 128 int tm_sec; /* seconds after the minute [0-60] */
wolfSSL 0:1239e9b70ca2 129 int tm_min; /* minutes after the hour [0-59] */
wolfSSL 0:1239e9b70ca2 130 int tm_hour; /* hours since midnight [0-23] */
wolfSSL 0:1239e9b70ca2 131 int tm_mday; /* day of the month [1-31] */
wolfSSL 0:1239e9b70ca2 132 int tm_mon; /* months since January [0-11] */
wolfSSL 0:1239e9b70ca2 133 int tm_year; /* years since 1900 */
wolfSSL 0:1239e9b70ca2 134 int tm_wday; /* days since Sunday [0-6] */
wolfSSL 0:1239e9b70ca2 135 int tm_yday; /* days since January 1 [0-365] */
wolfSSL 0:1239e9b70ca2 136 int tm_isdst; /* Daylight Savings Time flag */
wolfSSL 0:1239e9b70ca2 137 long tm_gmtoff; /* offset from CUT in seconds */
wolfSSL 0:1239e9b70ca2 138 char *tm_zone; /* timezone abbreviation */
wolfSSL 0:1239e9b70ca2 139 };
wolfSSL 0:1239e9b70ca2 140 typedef long time_t;
wolfSSL 0:1239e9b70ca2 141
wolfSSL 0:1239e9b70ca2 142 /* forward declaration */
wolfSSL 0:1239e9b70ca2 143 struct tm* gmtime(const time_t* timer);
wolfSSL 0:1239e9b70ca2 144 extern time_t XTIME(time_t * timer);
wolfSSL 0:1239e9b70ca2 145
wolfSSL 0:1239e9b70ca2 146 #define XGMTIME(c) gmtime((c))
wolfSSL 0:1239e9b70ca2 147 #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
wolfSSL 0:1239e9b70ca2 148
wolfSSL 0:1239e9b70ca2 149 #ifdef STACK_TRAP
wolfSSL 0:1239e9b70ca2 150 /* for stack trap tracking, don't call os gmtime on OS X/linux,
wolfSSL 0:1239e9b70ca2 151 uses a lot of stack spce */
wolfSSL 0:1239e9b70ca2 152 extern time_t time(time_t * timer);
wolfSSL 0:1239e9b70ca2 153 #define XTIME(tl) time((tl))
wolfSSL 0:1239e9b70ca2 154 #endif /* STACK_TRAP */
wolfSSL 0:1239e9b70ca2 155
wolfSSL 0:1239e9b70ca2 156 #else
wolfSSL 0:1239e9b70ca2 157 /* default */
wolfSSL 0:1239e9b70ca2 158 /* uses complete <time.h> facility */
wolfSSL 0:1239e9b70ca2 159 #include <time.h>
wolfSSL 0:1239e9b70ca2 160 #define XTIME(tl) time((tl))
wolfSSL 0:1239e9b70ca2 161 #define XGMTIME(c) gmtime((c))
wolfSSL 0:1239e9b70ca2 162 #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t))
wolfSSL 0:1239e9b70ca2 163 #endif
wolfSSL 0:1239e9b70ca2 164
wolfSSL 0:1239e9b70ca2 165
wolfSSL 0:1239e9b70ca2 166 #ifdef _WIN32_WCE
wolfSSL 0:1239e9b70ca2 167 /* no time() or gmtime() even though in time.h header?? */
wolfSSL 0:1239e9b70ca2 168
wolfSSL 0:1239e9b70ca2 169 #include <windows.h>
wolfSSL 0:1239e9b70ca2 170
wolfSSL 0:1239e9b70ca2 171
wolfSSL 0:1239e9b70ca2 172 time_t time(time_t* timer)
wolfSSL 0:1239e9b70ca2 173 {
wolfSSL 0:1239e9b70ca2 174 SYSTEMTIME sysTime;
wolfSSL 0:1239e9b70ca2 175 FILETIME fTime;
wolfSSL 0:1239e9b70ca2 176 ULARGE_INTEGER intTime;
wolfSSL 0:1239e9b70ca2 177 time_t localTime;
wolfSSL 0:1239e9b70ca2 178
wolfSSL 0:1239e9b70ca2 179 if (timer == NULL)
wolfSSL 0:1239e9b70ca2 180 timer = &localTime;
wolfSSL 0:1239e9b70ca2 181
wolfSSL 0:1239e9b70ca2 182 GetSystemTime(&sysTime);
wolfSSL 0:1239e9b70ca2 183 SystemTimeToFileTime(&sysTime, &fTime);
wolfSSL 0:1239e9b70ca2 184
wolfSSL 0:1239e9b70ca2 185 XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
wolfSSL 0:1239e9b70ca2 186 /* subtract EPOCH */
wolfSSL 0:1239e9b70ca2 187 intTime.QuadPart -= 0x19db1ded53e8000;
wolfSSL 0:1239e9b70ca2 188 /* to secs */
wolfSSL 0:1239e9b70ca2 189 intTime.QuadPart /= 10000000;
wolfSSL 0:1239e9b70ca2 190 *timer = (time_t)intTime.QuadPart;
wolfSSL 0:1239e9b70ca2 191
wolfSSL 0:1239e9b70ca2 192 return *timer;
wolfSSL 0:1239e9b70ca2 193 }
wolfSSL 0:1239e9b70ca2 194
wolfSSL 0:1239e9b70ca2 195 #endif /* _WIN32_WCE */
wolfSSL 0:1239e9b70ca2 196 #if defined( _WIN32_WCE ) || defined( USER_TIME )
wolfSSL 0:1239e9b70ca2 197
wolfSSL 0:1239e9b70ca2 198 struct tm* gmtime(const time_t* timer)
wolfSSL 0:1239e9b70ca2 199 {
wolfSSL 0:1239e9b70ca2 200 #define YEAR0 1900
wolfSSL 0:1239e9b70ca2 201 #define EPOCH_YEAR 1970
wolfSSL 0:1239e9b70ca2 202 #define SECS_DAY (24L * 60L * 60L)
wolfSSL 0:1239e9b70ca2 203 #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
wolfSSL 0:1239e9b70ca2 204 #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
wolfSSL 0:1239e9b70ca2 205
wolfSSL 0:1239e9b70ca2 206 static const int _ytab[2][12] =
wolfSSL 0:1239e9b70ca2 207 {
wolfSSL 0:1239e9b70ca2 208 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
wolfSSL 0:1239e9b70ca2 209 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
wolfSSL 0:1239e9b70ca2 210 };
wolfSSL 0:1239e9b70ca2 211
wolfSSL 0:1239e9b70ca2 212 static struct tm st_time;
wolfSSL 0:1239e9b70ca2 213 struct tm* ret = &st_time;
wolfSSL 0:1239e9b70ca2 214 time_t secs = *timer;
wolfSSL 0:1239e9b70ca2 215 unsigned long dayclock, dayno;
wolfSSL 0:1239e9b70ca2 216 int year = EPOCH_YEAR;
wolfSSL 0:1239e9b70ca2 217
wolfSSL 0:1239e9b70ca2 218 dayclock = (unsigned long)secs % SECS_DAY;
wolfSSL 0:1239e9b70ca2 219 dayno = (unsigned long)secs / SECS_DAY;
wolfSSL 0:1239e9b70ca2 220
wolfSSL 0:1239e9b70ca2 221 ret->tm_sec = (int) dayclock % 60;
wolfSSL 0:1239e9b70ca2 222 ret->tm_min = (int)(dayclock % 3600) / 60;
wolfSSL 0:1239e9b70ca2 223 ret->tm_hour = (int) dayclock / 3600;
wolfSSL 0:1239e9b70ca2 224 ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */
wolfSSL 0:1239e9b70ca2 225
wolfSSL 0:1239e9b70ca2 226 while(dayno >= (unsigned long)YEARSIZE(year)) {
wolfSSL 0:1239e9b70ca2 227 dayno -= YEARSIZE(year);
wolfSSL 0:1239e9b70ca2 228 year++;
wolfSSL 0:1239e9b70ca2 229 }
wolfSSL 0:1239e9b70ca2 230
wolfSSL 0:1239e9b70ca2 231 ret->tm_year = year - YEAR0;
wolfSSL 0:1239e9b70ca2 232 ret->tm_yday = (int)dayno;
wolfSSL 0:1239e9b70ca2 233 ret->tm_mon = 0;
wolfSSL 0:1239e9b70ca2 234
wolfSSL 0:1239e9b70ca2 235 while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
wolfSSL 0:1239e9b70ca2 236 dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
wolfSSL 0:1239e9b70ca2 237 ret->tm_mon++;
wolfSSL 0:1239e9b70ca2 238 }
wolfSSL 0:1239e9b70ca2 239
wolfSSL 0:1239e9b70ca2 240 ret->tm_mday = (int)++dayno;
wolfSSL 0:1239e9b70ca2 241 ret->tm_isdst = 0;
wolfSSL 0:1239e9b70ca2 242
wolfSSL 0:1239e9b70ca2 243 return ret;
wolfSSL 0:1239e9b70ca2 244 }
wolfSSL 0:1239e9b70ca2 245
wolfSSL 0:1239e9b70ca2 246 #endif /* _WIN32_WCE || USER_TIME */
wolfSSL 0:1239e9b70ca2 247
wolfSSL 0:1239e9b70ca2 248
wolfSSL 0:1239e9b70ca2 249 #ifdef HAVE_RTP_SYS
wolfSSL 0:1239e9b70ca2 250
wolfSSL 0:1239e9b70ca2 251 #define YEAR0 1900
wolfSSL 0:1239e9b70ca2 252
wolfSSL 0:1239e9b70ca2 253 struct tm* my_gmtime(const time_t* timer) /* has a gmtime() but hangs */
wolfSSL 0:1239e9b70ca2 254 {
wolfSSL 0:1239e9b70ca2 255 static struct tm st_time;
wolfSSL 0:1239e9b70ca2 256 struct tm* ret = &st_time;
wolfSSL 0:1239e9b70ca2 257
wolfSSL 0:1239e9b70ca2 258 DC_RTC_CALENDAR cal;
wolfSSL 0:1239e9b70ca2 259 dc_rtc_time_get(&cal, TRUE);
wolfSSL 0:1239e9b70ca2 260
wolfSSL 0:1239e9b70ca2 261 ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
wolfSSL 0:1239e9b70ca2 262 ret->tm_mon = cal.month - 1; /* gm starts at 0 */
wolfSSL 0:1239e9b70ca2 263 ret->tm_mday = cal.day;
wolfSSL 0:1239e9b70ca2 264 ret->tm_hour = cal.hour;
wolfSSL 0:1239e9b70ca2 265 ret->tm_min = cal.minute;
wolfSSL 0:1239e9b70ca2 266 ret->tm_sec = cal.second;
wolfSSL 0:1239e9b70ca2 267
wolfSSL 0:1239e9b70ca2 268 return ret;
wolfSSL 0:1239e9b70ca2 269 }
wolfSSL 0:1239e9b70ca2 270
wolfSSL 0:1239e9b70ca2 271 #endif /* HAVE_RTP_SYS */
wolfSSL 0:1239e9b70ca2 272
wolfSSL 0:1239e9b70ca2 273
wolfSSL 0:1239e9b70ca2 274 #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
wolfSSL 0:1239e9b70ca2 275
wolfSSL 0:1239e9b70ca2 276 /*
wolfSSL 0:1239e9b70ca2 277 * time() is just a stub in Microchip libraries. We need our own
wolfSSL 0:1239e9b70ca2 278 * implementation. Use SNTP client to get seconds since epoch.
wolfSSL 0:1239e9b70ca2 279 */
wolfSSL 0:1239e9b70ca2 280 time_t pic32_time(time_t* timer)
wolfSSL 0:1239e9b70ca2 281 {
wolfSSL 0:1239e9b70ca2 282 #ifdef MICROCHIP_TCPIP_V5
wolfSSL 0:1239e9b70ca2 283 DWORD sec = 0;
wolfSSL 0:1239e9b70ca2 284 #else
wolfSSL 0:1239e9b70ca2 285 uint32_t sec = 0;
wolfSSL 0:1239e9b70ca2 286 #endif
wolfSSL 0:1239e9b70ca2 287 time_t localTime;
wolfSSL 0:1239e9b70ca2 288
wolfSSL 0:1239e9b70ca2 289 if (timer == NULL)
wolfSSL 0:1239e9b70ca2 290 timer = &localTime;
wolfSSL 0:1239e9b70ca2 291
wolfSSL 0:1239e9b70ca2 292 #ifdef MICROCHIP_MPLAB_HARMONY
wolfSSL 0:1239e9b70ca2 293 sec = TCPIP_SNTP_UTCSecondsGet();
wolfSSL 0:1239e9b70ca2 294 #else
wolfSSL 0:1239e9b70ca2 295 sec = SNTPGetUTCSeconds();
wolfSSL 0:1239e9b70ca2 296 #endif
wolfSSL 0:1239e9b70ca2 297 *timer = (time_t) sec;
wolfSSL 0:1239e9b70ca2 298
wolfSSL 0:1239e9b70ca2 299 return *timer;
wolfSSL 0:1239e9b70ca2 300 }
wolfSSL 0:1239e9b70ca2 301
wolfSSL 0:1239e9b70ca2 302 #endif /* MICROCHIP_TCPIP */
wolfSSL 0:1239e9b70ca2 303
wolfSSL 0:1239e9b70ca2 304
wolfSSL 0:1239e9b70ca2 305 #ifdef FREESCALE_MQX
wolfSSL 0:1239e9b70ca2 306
wolfSSL 0:1239e9b70ca2 307 time_t mqx_time(time_t* timer)
wolfSSL 0:1239e9b70ca2 308 {
wolfSSL 0:1239e9b70ca2 309 time_t localTime;
wolfSSL 0:1239e9b70ca2 310 TIME_STRUCT time_s;
wolfSSL 0:1239e9b70ca2 311
wolfSSL 0:1239e9b70ca2 312 if (timer == NULL)
wolfSSL 0:1239e9b70ca2 313 timer = &localTime;
wolfSSL 0:1239e9b70ca2 314
wolfSSL 0:1239e9b70ca2 315 _time_get(&time_s);
wolfSSL 0:1239e9b70ca2 316 *timer = (time_t) time_s.SECONDS;
wolfSSL 0:1239e9b70ca2 317
wolfSSL 0:1239e9b70ca2 318 return *timer;
wolfSSL 0:1239e9b70ca2 319 }
wolfSSL 0:1239e9b70ca2 320
wolfSSL 0:1239e9b70ca2 321 #endif /* FREESCALE_MQX */
wolfSSL 0:1239e9b70ca2 322
wolfSSL 0:1239e9b70ca2 323
wolfSSL 0:1239e9b70ca2 324 static INLINE word32 btoi(byte b)
wolfSSL 0:1239e9b70ca2 325 {
wolfSSL 0:1239e9b70ca2 326 return b - 0x30;
wolfSSL 0:1239e9b70ca2 327 }
wolfSSL 0:1239e9b70ca2 328
wolfSSL 0:1239e9b70ca2 329
wolfSSL 0:1239e9b70ca2 330 /* two byte date/time, add to value */
wolfSSL 0:1239e9b70ca2 331 static INLINE void GetTime(int* value, const byte* date, int* idx)
wolfSSL 0:1239e9b70ca2 332 {
wolfSSL 0:1239e9b70ca2 333 int i = *idx;
wolfSSL 0:1239e9b70ca2 334
wolfSSL 0:1239e9b70ca2 335 *value += btoi(date[i++]) * 10;
wolfSSL 0:1239e9b70ca2 336 *value += btoi(date[i++]);
wolfSSL 0:1239e9b70ca2 337
wolfSSL 0:1239e9b70ca2 338 *idx = i;
wolfSSL 0:1239e9b70ca2 339 }
wolfSSL 0:1239e9b70ca2 340
wolfSSL 0:1239e9b70ca2 341
wolfSSL 0:1239e9b70ca2 342 #if defined(MICRIUM)
wolfSSL 0:1239e9b70ca2 343
wolfSSL 0:1239e9b70ca2 344 CPU_INT32S NetSecure_ValidateDateHandler(CPU_INT08U *date, CPU_INT08U format,
wolfSSL 0:1239e9b70ca2 345 CPU_INT08U dateType)
wolfSSL 0:1239e9b70ca2 346 {
wolfSSL 0:1239e9b70ca2 347 CPU_BOOLEAN rtn_code;
wolfSSL 0:1239e9b70ca2 348 CPU_INT32S i;
wolfSSL 0:1239e9b70ca2 349 CPU_INT32S val;
wolfSSL 0:1239e9b70ca2 350 CPU_INT16U year;
wolfSSL 0:1239e9b70ca2 351 CPU_INT08U month;
wolfSSL 0:1239e9b70ca2 352 CPU_INT16U day;
wolfSSL 0:1239e9b70ca2 353 CPU_INT08U hour;
wolfSSL 0:1239e9b70ca2 354 CPU_INT08U min;
wolfSSL 0:1239e9b70ca2 355 CPU_INT08U sec;
wolfSSL 0:1239e9b70ca2 356
wolfSSL 0:1239e9b70ca2 357 i = 0;
wolfSSL 0:1239e9b70ca2 358 year = 0u;
wolfSSL 0:1239e9b70ca2 359
wolfSSL 0:1239e9b70ca2 360 if (format == ASN_UTC_TIME) {
wolfSSL 0:1239e9b70ca2 361 if (btoi(date[0]) >= 5)
wolfSSL 0:1239e9b70ca2 362 year = 1900;
wolfSSL 0:1239e9b70ca2 363 else
wolfSSL 0:1239e9b70ca2 364 year = 2000;
wolfSSL 0:1239e9b70ca2 365 }
wolfSSL 0:1239e9b70ca2 366 else { /* format == GENERALIZED_TIME */
wolfSSL 0:1239e9b70ca2 367 year += btoi(date[i++]) * 1000;
wolfSSL 0:1239e9b70ca2 368 year += btoi(date[i++]) * 100;
wolfSSL 0:1239e9b70ca2 369 }
wolfSSL 0:1239e9b70ca2 370
wolfSSL 0:1239e9b70ca2 371 val = year;
wolfSSL 0:1239e9b70ca2 372 GetTime(&val, date, &i);
wolfSSL 0:1239e9b70ca2 373 year = (CPU_INT16U)val;
wolfSSL 0:1239e9b70ca2 374
wolfSSL 0:1239e9b70ca2 375 val = 0;
wolfSSL 0:1239e9b70ca2 376 GetTime(&val, date, &i);
wolfSSL 0:1239e9b70ca2 377 month = (CPU_INT08U)val;
wolfSSL 0:1239e9b70ca2 378
wolfSSL 0:1239e9b70ca2 379 val = 0;
wolfSSL 0:1239e9b70ca2 380 GetTime(&val, date, &i);
wolfSSL 0:1239e9b70ca2 381 day = (CPU_INT16U)val;
wolfSSL 0:1239e9b70ca2 382
wolfSSL 0:1239e9b70ca2 383 val = 0;
wolfSSL 0:1239e9b70ca2 384 GetTime(&val, date, &i);
wolfSSL 0:1239e9b70ca2 385 hour = (CPU_INT08U)val;
wolfSSL 0:1239e9b70ca2 386
wolfSSL 0:1239e9b70ca2 387 val = 0;
wolfSSL 0:1239e9b70ca2 388 GetTime(&val, date, &i);
wolfSSL 0:1239e9b70ca2 389 min = (CPU_INT08U)val;
wolfSSL 0:1239e9b70ca2 390
wolfSSL 0:1239e9b70ca2 391 val = 0;
wolfSSL 0:1239e9b70ca2 392 GetTime(&val, date, &i);
wolfSSL 0:1239e9b70ca2 393 sec = (CPU_INT08U)val;
wolfSSL 0:1239e9b70ca2 394
wolfSSL 0:1239e9b70ca2 395 return NetSecure_ValidateDate(year, month, day, hour, min, sec, dateType);
wolfSSL 0:1239e9b70ca2 396 }
wolfSSL 0:1239e9b70ca2 397
wolfSSL 0:1239e9b70ca2 398 #endif /* MICRIUM */
wolfSSL 0:1239e9b70ca2 399
wolfSSL 0:1239e9b70ca2 400
wolfSSL 0:1239e9b70ca2 401 CYASSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len,
wolfSSL 0:1239e9b70ca2 402 word32 maxIdx)
wolfSSL 0:1239e9b70ca2 403 {
wolfSSL 0:1239e9b70ca2 404 int length = 0;
wolfSSL 0:1239e9b70ca2 405 word32 i = *inOutIdx;
wolfSSL 0:1239e9b70ca2 406 byte b;
wolfSSL 0:1239e9b70ca2 407
wolfSSL 0:1239e9b70ca2 408 if ( (i+1) > maxIdx) { /* for first read */
wolfSSL 0:1239e9b70ca2 409 CYASSL_MSG("GetLength bad index on input");
wolfSSL 0:1239e9b70ca2 410 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 411 }
wolfSSL 0:1239e9b70ca2 412
wolfSSL 0:1239e9b70ca2 413 b = input[i++];
wolfSSL 0:1239e9b70ca2 414 if (b >= ASN_LONG_LENGTH) {
wolfSSL 0:1239e9b70ca2 415 word32 bytes = b & 0x7F;
wolfSSL 0:1239e9b70ca2 416
wolfSSL 0:1239e9b70ca2 417 if ( (i+bytes) > maxIdx) { /* for reading bytes */
wolfSSL 0:1239e9b70ca2 418 CYASSL_MSG("GetLength bad long length");
wolfSSL 0:1239e9b70ca2 419 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 420 }
wolfSSL 0:1239e9b70ca2 421
wolfSSL 0:1239e9b70ca2 422 while (bytes--) {
wolfSSL 0:1239e9b70ca2 423 b = input[i++];
wolfSSL 0:1239e9b70ca2 424 length = (length << 8) | b;
wolfSSL 0:1239e9b70ca2 425 }
wolfSSL 0:1239e9b70ca2 426 }
wolfSSL 0:1239e9b70ca2 427 else
wolfSSL 0:1239e9b70ca2 428 length = b;
wolfSSL 0:1239e9b70ca2 429
wolfSSL 0:1239e9b70ca2 430 if ( (i+length) > maxIdx) { /* for user of length */
wolfSSL 0:1239e9b70ca2 431 CYASSL_MSG("GetLength value exceeds buffer length");
wolfSSL 0:1239e9b70ca2 432 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 433 }
wolfSSL 0:1239e9b70ca2 434
wolfSSL 0:1239e9b70ca2 435 *inOutIdx = i;
wolfSSL 0:1239e9b70ca2 436 *len = length;
wolfSSL 0:1239e9b70ca2 437
wolfSSL 0:1239e9b70ca2 438 return length;
wolfSSL 0:1239e9b70ca2 439 }
wolfSSL 0:1239e9b70ca2 440
wolfSSL 0:1239e9b70ca2 441
wolfSSL 0:1239e9b70ca2 442 CYASSL_LOCAL int GetSequence(const byte* input, word32* inOutIdx, int* len,
wolfSSL 0:1239e9b70ca2 443 word32 maxIdx)
wolfSSL 0:1239e9b70ca2 444 {
wolfSSL 0:1239e9b70ca2 445 int length = -1;
wolfSSL 0:1239e9b70ca2 446 word32 idx = *inOutIdx;
wolfSSL 0:1239e9b70ca2 447
wolfSSL 0:1239e9b70ca2 448 if (input[idx++] != (ASN_SEQUENCE | ASN_CONSTRUCTED) ||
wolfSSL 0:1239e9b70ca2 449 GetLength(input, &idx, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 450 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 451
wolfSSL 0:1239e9b70ca2 452 *len = length;
wolfSSL 0:1239e9b70ca2 453 *inOutIdx = idx;
wolfSSL 0:1239e9b70ca2 454
wolfSSL 0:1239e9b70ca2 455 return length;
wolfSSL 0:1239e9b70ca2 456 }
wolfSSL 0:1239e9b70ca2 457
wolfSSL 0:1239e9b70ca2 458
wolfSSL 0:1239e9b70ca2 459 CYASSL_LOCAL int GetSet(const byte* input, word32* inOutIdx, int* len,
wolfSSL 0:1239e9b70ca2 460 word32 maxIdx)
wolfSSL 0:1239e9b70ca2 461 {
wolfSSL 0:1239e9b70ca2 462 int length = -1;
wolfSSL 0:1239e9b70ca2 463 word32 idx = *inOutIdx;
wolfSSL 0:1239e9b70ca2 464
wolfSSL 0:1239e9b70ca2 465 if (input[idx++] != (ASN_SET | ASN_CONSTRUCTED) ||
wolfSSL 0:1239e9b70ca2 466 GetLength(input, &idx, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 467 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 468
wolfSSL 0:1239e9b70ca2 469 *len = length;
wolfSSL 0:1239e9b70ca2 470 *inOutIdx = idx;
wolfSSL 0:1239e9b70ca2 471
wolfSSL 0:1239e9b70ca2 472 return length;
wolfSSL 0:1239e9b70ca2 473 }
wolfSSL 0:1239e9b70ca2 474
wolfSSL 0:1239e9b70ca2 475
wolfSSL 0:1239e9b70ca2 476 /* winodws header clash for WinCE using GetVersion */
wolfSSL 0:1239e9b70ca2 477 CYASSL_LOCAL int GetMyVersion(const byte* input, word32* inOutIdx, int* version)
wolfSSL 0:1239e9b70ca2 478 {
wolfSSL 0:1239e9b70ca2 479 word32 idx = *inOutIdx;
wolfSSL 0:1239e9b70ca2 480
wolfSSL 0:1239e9b70ca2 481 CYASSL_ENTER("GetMyVersion");
wolfSSL 0:1239e9b70ca2 482
wolfSSL 0:1239e9b70ca2 483 if (input[idx++] != ASN_INTEGER)
wolfSSL 0:1239e9b70ca2 484 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 485
wolfSSL 0:1239e9b70ca2 486 if (input[idx++] != 0x01)
wolfSSL 0:1239e9b70ca2 487 return ASN_VERSION_E;
wolfSSL 0:1239e9b70ca2 488
wolfSSL 0:1239e9b70ca2 489 *version = input[idx++];
wolfSSL 0:1239e9b70ca2 490 *inOutIdx = idx;
wolfSSL 0:1239e9b70ca2 491
wolfSSL 0:1239e9b70ca2 492 return *version;
wolfSSL 0:1239e9b70ca2 493 }
wolfSSL 0:1239e9b70ca2 494
wolfSSL 0:1239e9b70ca2 495
wolfSSL 0:1239e9b70ca2 496 #ifndef NO_PWDBASED
wolfSSL 0:1239e9b70ca2 497 /* Get small count integer, 32 bits or less */
wolfSSL 0:1239e9b70ca2 498 static int GetShortInt(const byte* input, word32* inOutIdx, int* number)
wolfSSL 0:1239e9b70ca2 499 {
wolfSSL 0:1239e9b70ca2 500 word32 idx = *inOutIdx;
wolfSSL 0:1239e9b70ca2 501 word32 len;
wolfSSL 0:1239e9b70ca2 502
wolfSSL 0:1239e9b70ca2 503 *number = 0;
wolfSSL 0:1239e9b70ca2 504
wolfSSL 0:1239e9b70ca2 505 if (input[idx++] != ASN_INTEGER)
wolfSSL 0:1239e9b70ca2 506 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 507
wolfSSL 0:1239e9b70ca2 508 len = input[idx++];
wolfSSL 0:1239e9b70ca2 509 if (len > 4)
wolfSSL 0:1239e9b70ca2 510 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 511
wolfSSL 0:1239e9b70ca2 512 while (len--) {
wolfSSL 0:1239e9b70ca2 513 *number = *number << 8 | input[idx++];
wolfSSL 0:1239e9b70ca2 514 }
wolfSSL 0:1239e9b70ca2 515
wolfSSL 0:1239e9b70ca2 516 *inOutIdx = idx;
wolfSSL 0:1239e9b70ca2 517
wolfSSL 0:1239e9b70ca2 518 return *number;
wolfSSL 0:1239e9b70ca2 519 }
wolfSSL 0:1239e9b70ca2 520 #endif /* !NO_PWDBASED */
wolfSSL 0:1239e9b70ca2 521
wolfSSL 0:1239e9b70ca2 522
wolfSSL 0:1239e9b70ca2 523 /* May not have one, not an error */
wolfSSL 0:1239e9b70ca2 524 static int GetExplicitVersion(const byte* input, word32* inOutIdx, int* version)
wolfSSL 0:1239e9b70ca2 525 {
wolfSSL 0:1239e9b70ca2 526 word32 idx = *inOutIdx;
wolfSSL 0:1239e9b70ca2 527
wolfSSL 0:1239e9b70ca2 528 CYASSL_ENTER("GetExplicitVersion");
wolfSSL 0:1239e9b70ca2 529 if (input[idx++] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) {
wolfSSL 0:1239e9b70ca2 530 *inOutIdx = ++idx; /* eat header */
wolfSSL 0:1239e9b70ca2 531 return GetMyVersion(input, inOutIdx, version);
wolfSSL 0:1239e9b70ca2 532 }
wolfSSL 0:1239e9b70ca2 533
wolfSSL 0:1239e9b70ca2 534 /* go back as is */
wolfSSL 0:1239e9b70ca2 535 *version = 0;
wolfSSL 0:1239e9b70ca2 536
wolfSSL 0:1239e9b70ca2 537 return 0;
wolfSSL 0:1239e9b70ca2 538 }
wolfSSL 0:1239e9b70ca2 539
wolfSSL 0:1239e9b70ca2 540
wolfSSL 0:1239e9b70ca2 541 CYASSL_LOCAL int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx,
wolfSSL 0:1239e9b70ca2 542 word32 maxIdx)
wolfSSL 0:1239e9b70ca2 543 {
wolfSSL 0:1239e9b70ca2 544 word32 i = *inOutIdx;
wolfSSL 0:1239e9b70ca2 545 byte b = input[i++];
wolfSSL 0:1239e9b70ca2 546 int length;
wolfSSL 0:1239e9b70ca2 547
wolfSSL 0:1239e9b70ca2 548 if (b != ASN_INTEGER)
wolfSSL 0:1239e9b70ca2 549 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 550
wolfSSL 0:1239e9b70ca2 551 if (GetLength(input, &i, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 552 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 553
wolfSSL 0:1239e9b70ca2 554 if ( (b = input[i++]) == 0x00)
wolfSSL 0:1239e9b70ca2 555 length--;
wolfSSL 0:1239e9b70ca2 556 else
wolfSSL 0:1239e9b70ca2 557 i--;
wolfSSL 0:1239e9b70ca2 558
wolfSSL 0:1239e9b70ca2 559 if (mp_init(mpi) != MP_OKAY)
wolfSSL 0:1239e9b70ca2 560 return MP_INIT_E;
wolfSSL 0:1239e9b70ca2 561
wolfSSL 0:1239e9b70ca2 562 if (mp_read_unsigned_bin(mpi, (byte*)input + i, length) != 0) {
wolfSSL 0:1239e9b70ca2 563 mp_clear(mpi);
wolfSSL 0:1239e9b70ca2 564 return ASN_GETINT_E;
wolfSSL 0:1239e9b70ca2 565 }
wolfSSL 0:1239e9b70ca2 566
wolfSSL 0:1239e9b70ca2 567 *inOutIdx = i + length;
wolfSSL 0:1239e9b70ca2 568 return 0;
wolfSSL 0:1239e9b70ca2 569 }
wolfSSL 0:1239e9b70ca2 570
wolfSSL 0:1239e9b70ca2 571
wolfSSL 0:1239e9b70ca2 572 static int GetObjectId(const byte* input, word32* inOutIdx, word32* oid,
wolfSSL 0:1239e9b70ca2 573 word32 maxIdx)
wolfSSL 0:1239e9b70ca2 574 {
wolfSSL 0:1239e9b70ca2 575 int length;
wolfSSL 0:1239e9b70ca2 576 word32 i = *inOutIdx;
wolfSSL 0:1239e9b70ca2 577 byte b;
wolfSSL 0:1239e9b70ca2 578 *oid = 0;
wolfSSL 0:1239e9b70ca2 579
wolfSSL 0:1239e9b70ca2 580 b = input[i++];
wolfSSL 0:1239e9b70ca2 581 if (b != ASN_OBJECT_ID)
wolfSSL 0:1239e9b70ca2 582 return ASN_OBJECT_ID_E;
wolfSSL 0:1239e9b70ca2 583
wolfSSL 0:1239e9b70ca2 584 if (GetLength(input, &i, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 585 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 586
wolfSSL 0:1239e9b70ca2 587 while(length--)
wolfSSL 0:1239e9b70ca2 588 *oid += input[i++];
wolfSSL 0:1239e9b70ca2 589 /* just sum it up for now */
wolfSSL 0:1239e9b70ca2 590
wolfSSL 0:1239e9b70ca2 591 *inOutIdx = i;
wolfSSL 0:1239e9b70ca2 592
wolfSSL 0:1239e9b70ca2 593 return 0;
wolfSSL 0:1239e9b70ca2 594 }
wolfSSL 0:1239e9b70ca2 595
wolfSSL 0:1239e9b70ca2 596
wolfSSL 0:1239e9b70ca2 597 CYASSL_LOCAL int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid,
wolfSSL 0:1239e9b70ca2 598 word32 maxIdx)
wolfSSL 0:1239e9b70ca2 599 {
wolfSSL 0:1239e9b70ca2 600 int length;
wolfSSL 0:1239e9b70ca2 601 word32 i = *inOutIdx;
wolfSSL 0:1239e9b70ca2 602 byte b;
wolfSSL 0:1239e9b70ca2 603 *oid = 0;
wolfSSL 0:1239e9b70ca2 604
wolfSSL 0:1239e9b70ca2 605 CYASSL_ENTER("GetAlgoId");
wolfSSL 0:1239e9b70ca2 606
wolfSSL 0:1239e9b70ca2 607 if (GetSequence(input, &i, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 608 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 609
wolfSSL 0:1239e9b70ca2 610 b = input[i++];
wolfSSL 0:1239e9b70ca2 611 if (b != ASN_OBJECT_ID)
wolfSSL 0:1239e9b70ca2 612 return ASN_OBJECT_ID_E;
wolfSSL 0:1239e9b70ca2 613
wolfSSL 0:1239e9b70ca2 614 if (GetLength(input, &i, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 615 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 616
wolfSSL 0:1239e9b70ca2 617 while(length--) {
wolfSSL 0:1239e9b70ca2 618 /* odd HC08 compiler behavior here when input[i++] */
wolfSSL 0:1239e9b70ca2 619 *oid += input[i];
wolfSSL 0:1239e9b70ca2 620 i++;
wolfSSL 0:1239e9b70ca2 621 }
wolfSSL 0:1239e9b70ca2 622 /* just sum it up for now */
wolfSSL 0:1239e9b70ca2 623
wolfSSL 0:1239e9b70ca2 624 /* could have NULL tag and 0 terminator, but may not */
wolfSSL 0:1239e9b70ca2 625 b = input[i++];
wolfSSL 0:1239e9b70ca2 626
wolfSSL 0:1239e9b70ca2 627 if (b == ASN_TAG_NULL) {
wolfSSL 0:1239e9b70ca2 628 b = input[i++];
wolfSSL 0:1239e9b70ca2 629 if (b != 0)
wolfSSL 0:1239e9b70ca2 630 return ASN_EXPECT_0_E;
wolfSSL 0:1239e9b70ca2 631 }
wolfSSL 0:1239e9b70ca2 632 else
wolfSSL 0:1239e9b70ca2 633 /* go back, didn't have it */
wolfSSL 0:1239e9b70ca2 634 i--;
wolfSSL 0:1239e9b70ca2 635
wolfSSL 0:1239e9b70ca2 636 *inOutIdx = i;
wolfSSL 0:1239e9b70ca2 637
wolfSSL 0:1239e9b70ca2 638 return 0;
wolfSSL 0:1239e9b70ca2 639 }
wolfSSL 0:1239e9b70ca2 640
wolfSSL 0:1239e9b70ca2 641 #ifndef NO_RSA
wolfSSL 0:1239e9b70ca2 642
wolfSSL 0:1239e9b70ca2 643
wolfSSL 0:1239e9b70ca2 644 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 645
wolfSSL 0:1239e9b70ca2 646 static int GetCaviumInt(byte** buff, word16* buffSz, const byte* input,
wolfSSL 0:1239e9b70ca2 647 word32* inOutIdx, word32 maxIdx, void* heap)
wolfSSL 0:1239e9b70ca2 648 {
wolfSSL 0:1239e9b70ca2 649 word32 i = *inOutIdx;
wolfSSL 0:1239e9b70ca2 650 byte b = input[i++];
wolfSSL 0:1239e9b70ca2 651 int length;
wolfSSL 0:1239e9b70ca2 652
wolfSSL 0:1239e9b70ca2 653 if (b != ASN_INTEGER)
wolfSSL 0:1239e9b70ca2 654 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 655
wolfSSL 0:1239e9b70ca2 656 if (GetLength(input, &i, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 657 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 658
wolfSSL 0:1239e9b70ca2 659 if ( (b = input[i++]) == 0x00)
wolfSSL 0:1239e9b70ca2 660 length--;
wolfSSL 0:1239e9b70ca2 661 else
wolfSSL 0:1239e9b70ca2 662 i--;
wolfSSL 0:1239e9b70ca2 663
wolfSSL 0:1239e9b70ca2 664 *buffSz = (word16)length;
wolfSSL 0:1239e9b70ca2 665 *buff = XMALLOC(*buffSz, heap, DYNAMIC_TYPE_CAVIUM_RSA);
wolfSSL 0:1239e9b70ca2 666 if (*buff == NULL)
wolfSSL 0:1239e9b70ca2 667 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 668
wolfSSL 0:1239e9b70ca2 669 XMEMCPY(*buff, input + i, *buffSz);
wolfSSL 0:1239e9b70ca2 670
wolfSSL 0:1239e9b70ca2 671 *inOutIdx = i + length;
wolfSSL 0:1239e9b70ca2 672 return 0;
wolfSSL 0:1239e9b70ca2 673 }
wolfSSL 0:1239e9b70ca2 674
wolfSSL 0:1239e9b70ca2 675 static int CaviumRsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
wolfSSL 0:1239e9b70ca2 676 RsaKey* key, word32 inSz)
wolfSSL 0:1239e9b70ca2 677 {
wolfSSL 0:1239e9b70ca2 678 int version, length;
wolfSSL 0:1239e9b70ca2 679 void* h = key->heap;
wolfSSL 0:1239e9b70ca2 680
wolfSSL 0:1239e9b70ca2 681 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 682 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 683
wolfSSL 0:1239e9b70ca2 684 if (GetMyVersion(input, inOutIdx, &version) < 0)
wolfSSL 0:1239e9b70ca2 685 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 686
wolfSSL 0:1239e9b70ca2 687 key->type = RSA_PRIVATE;
wolfSSL 0:1239e9b70ca2 688
wolfSSL 0:1239e9b70ca2 689 if (GetCaviumInt(&key->c_n, &key->c_nSz, input, inOutIdx, inSz, h) < 0 ||
wolfSSL 0:1239e9b70ca2 690 GetCaviumInt(&key->c_e, &key->c_eSz, input, inOutIdx, inSz, h) < 0 ||
wolfSSL 0:1239e9b70ca2 691 GetCaviumInt(&key->c_d, &key->c_dSz, input, inOutIdx, inSz, h) < 0 ||
wolfSSL 0:1239e9b70ca2 692 GetCaviumInt(&key->c_p, &key->c_pSz, input, inOutIdx, inSz, h) < 0 ||
wolfSSL 0:1239e9b70ca2 693 GetCaviumInt(&key->c_q, &key->c_qSz, input, inOutIdx, inSz, h) < 0 ||
wolfSSL 0:1239e9b70ca2 694 GetCaviumInt(&key->c_dP, &key->c_dP_Sz, input, inOutIdx, inSz, h) < 0 ||
wolfSSL 0:1239e9b70ca2 695 GetCaviumInt(&key->c_dQ, &key->c_dQ_Sz, input, inOutIdx, inSz, h) < 0 ||
wolfSSL 0:1239e9b70ca2 696 GetCaviumInt(&key->c_u, &key->c_uSz, input, inOutIdx, inSz, h) < 0 )
wolfSSL 0:1239e9b70ca2 697 return ASN_RSA_KEY_E;
wolfSSL 0:1239e9b70ca2 698
wolfSSL 0:1239e9b70ca2 699 return 0;
wolfSSL 0:1239e9b70ca2 700 }
wolfSSL 0:1239e9b70ca2 701
wolfSSL 0:1239e9b70ca2 702
wolfSSL 0:1239e9b70ca2 703 #endif /* HAVE_CAVIUM */
wolfSSL 0:1239e9b70ca2 704
wolfSSL 0:1239e9b70ca2 705 int RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key,
wolfSSL 0:1239e9b70ca2 706 word32 inSz)
wolfSSL 0:1239e9b70ca2 707 {
wolfSSL 0:1239e9b70ca2 708 int version, length;
wolfSSL 0:1239e9b70ca2 709
wolfSSL 0:1239e9b70ca2 710 #ifdef HAVE_CAVIUM
wolfSSL 0:1239e9b70ca2 711 if (key->magic == CYASSL_RSA_CAVIUM_MAGIC)
wolfSSL 0:1239e9b70ca2 712 return CaviumRsaPrivateKeyDecode(input, inOutIdx, key, inSz);
wolfSSL 0:1239e9b70ca2 713 #endif
wolfSSL 0:1239e9b70ca2 714
wolfSSL 0:1239e9b70ca2 715 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 716 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 717
wolfSSL 0:1239e9b70ca2 718 if (GetMyVersion(input, inOutIdx, &version) < 0)
wolfSSL 0:1239e9b70ca2 719 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 720
wolfSSL 0:1239e9b70ca2 721 key->type = RSA_PRIVATE;
wolfSSL 0:1239e9b70ca2 722
wolfSSL 0:1239e9b70ca2 723 if (GetInt(&key->n, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 724 GetInt(&key->e, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 725 GetInt(&key->d, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 726 GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 727 GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 728 GetInt(&key->dP, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 729 GetInt(&key->dQ, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 730 GetInt(&key->u, input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E;
wolfSSL 0:1239e9b70ca2 731
wolfSSL 0:1239e9b70ca2 732 return 0;
wolfSSL 0:1239e9b70ca2 733 }
wolfSSL 0:1239e9b70ca2 734
wolfSSL 0:1239e9b70ca2 735 #endif /* NO_RSA */
wolfSSL 0:1239e9b70ca2 736
wolfSSL 0:1239e9b70ca2 737 /* Remove PKCS8 header, move beginning of traditional to beginning of input */
wolfSSL 0:1239e9b70ca2 738 int ToTraditional(byte* input, word32 sz)
wolfSSL 0:1239e9b70ca2 739 {
wolfSSL 0:1239e9b70ca2 740 word32 inOutIdx = 0, oid;
wolfSSL 0:1239e9b70ca2 741 int version, length;
wolfSSL 0:1239e9b70ca2 742
wolfSSL 0:1239e9b70ca2 743 if (GetSequence(input, &inOutIdx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 744 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 745
wolfSSL 0:1239e9b70ca2 746 if (GetMyVersion(input, &inOutIdx, &version) < 0)
wolfSSL 0:1239e9b70ca2 747 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 748
wolfSSL 0:1239e9b70ca2 749 if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
wolfSSL 0:1239e9b70ca2 750 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 751
wolfSSL 0:1239e9b70ca2 752 if (input[inOutIdx] == ASN_OBJECT_ID) {
wolfSSL 0:1239e9b70ca2 753 /* pkcs8 ecc uses slightly different format */
wolfSSL 0:1239e9b70ca2 754 inOutIdx++; /* past id */
wolfSSL 0:1239e9b70ca2 755 if (GetLength(input, &inOutIdx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 756 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 757 inOutIdx += length; /* over sub id, key input will verify */
wolfSSL 0:1239e9b70ca2 758 }
wolfSSL 0:1239e9b70ca2 759
wolfSSL 0:1239e9b70ca2 760 if (input[inOutIdx++] != ASN_OCTET_STRING)
wolfSSL 0:1239e9b70ca2 761 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 762
wolfSSL 0:1239e9b70ca2 763 if (GetLength(input, &inOutIdx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 764 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 765
wolfSSL 0:1239e9b70ca2 766 XMEMMOVE(input, input + inOutIdx, length);
wolfSSL 0:1239e9b70ca2 767
wolfSSL 0:1239e9b70ca2 768 return length;
wolfSSL 0:1239e9b70ca2 769 }
wolfSSL 0:1239e9b70ca2 770
wolfSSL 0:1239e9b70ca2 771
wolfSSL 0:1239e9b70ca2 772 #ifndef NO_PWDBASED
wolfSSL 0:1239e9b70ca2 773
wolfSSL 0:1239e9b70ca2 774 /* Check To see if PKCS version algo is supported, set id if it is return 0
wolfSSL 0:1239e9b70ca2 775 < 0 on error */
wolfSSL 0:1239e9b70ca2 776 static int CheckAlgo(int first, int second, int* id, int* version)
wolfSSL 0:1239e9b70ca2 777 {
wolfSSL 0:1239e9b70ca2 778 *id = ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 779 *version = PKCS5; /* default */
wolfSSL 0:1239e9b70ca2 780
wolfSSL 0:1239e9b70ca2 781 if (first == 1) {
wolfSSL 0:1239e9b70ca2 782 switch (second) {
wolfSSL 0:1239e9b70ca2 783 case 1:
wolfSSL 0:1239e9b70ca2 784 *id = PBE_SHA1_RC4_128;
wolfSSL 0:1239e9b70ca2 785 *version = PKCS12;
wolfSSL 0:1239e9b70ca2 786 return 0;
wolfSSL 0:1239e9b70ca2 787 case 3:
wolfSSL 0:1239e9b70ca2 788 *id = PBE_SHA1_DES3;
wolfSSL 0:1239e9b70ca2 789 *version = PKCS12;
wolfSSL 0:1239e9b70ca2 790 return 0;
wolfSSL 0:1239e9b70ca2 791 default:
wolfSSL 0:1239e9b70ca2 792 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 793 }
wolfSSL 0:1239e9b70ca2 794 }
wolfSSL 0:1239e9b70ca2 795
wolfSSL 0:1239e9b70ca2 796 if (first != PKCS5)
wolfSSL 0:1239e9b70ca2 797 return ASN_INPUT_E; /* VERSION ERROR */
wolfSSL 0:1239e9b70ca2 798
wolfSSL 0:1239e9b70ca2 799 if (second == PBES2) {
wolfSSL 0:1239e9b70ca2 800 *version = PKCS5v2;
wolfSSL 0:1239e9b70ca2 801 return 0;
wolfSSL 0:1239e9b70ca2 802 }
wolfSSL 0:1239e9b70ca2 803
wolfSSL 0:1239e9b70ca2 804 switch (second) {
wolfSSL 0:1239e9b70ca2 805 case 3: /* see RFC 2898 for ids */
wolfSSL 0:1239e9b70ca2 806 *id = PBE_MD5_DES;
wolfSSL 0:1239e9b70ca2 807 return 0;
wolfSSL 0:1239e9b70ca2 808 case 10:
wolfSSL 0:1239e9b70ca2 809 *id = PBE_SHA1_DES;
wolfSSL 0:1239e9b70ca2 810 return 0;
wolfSSL 0:1239e9b70ca2 811 default:
wolfSSL 0:1239e9b70ca2 812 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 813
wolfSSL 0:1239e9b70ca2 814 }
wolfSSL 0:1239e9b70ca2 815 }
wolfSSL 0:1239e9b70ca2 816
wolfSSL 0:1239e9b70ca2 817
wolfSSL 0:1239e9b70ca2 818 /* Check To see if PKCS v2 algo is supported, set id if it is return 0
wolfSSL 0:1239e9b70ca2 819 < 0 on error */
wolfSSL 0:1239e9b70ca2 820 static int CheckAlgoV2(int oid, int* id)
wolfSSL 0:1239e9b70ca2 821 {
wolfSSL 0:1239e9b70ca2 822 switch (oid) {
wolfSSL 0:1239e9b70ca2 823 case 69:
wolfSSL 0:1239e9b70ca2 824 *id = PBE_SHA1_DES;
wolfSSL 0:1239e9b70ca2 825 return 0;
wolfSSL 0:1239e9b70ca2 826 case 652:
wolfSSL 0:1239e9b70ca2 827 *id = PBE_SHA1_DES3;
wolfSSL 0:1239e9b70ca2 828 return 0;
wolfSSL 0:1239e9b70ca2 829 default:
wolfSSL 0:1239e9b70ca2 830 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 831
wolfSSL 0:1239e9b70ca2 832 }
wolfSSL 0:1239e9b70ca2 833 }
wolfSSL 0:1239e9b70ca2 834
wolfSSL 0:1239e9b70ca2 835
wolfSSL 0:1239e9b70ca2 836 /* Decrypt intput in place from parameters based on id */
wolfSSL 0:1239e9b70ca2 837 static int DecryptKey(const char* password, int passwordSz, byte* salt,
wolfSSL 0:1239e9b70ca2 838 int saltSz, int iterations, int id, byte* input,
wolfSSL 0:1239e9b70ca2 839 int length, int version, byte* cbcIv)
wolfSSL 0:1239e9b70ca2 840 {
wolfSSL 0:1239e9b70ca2 841 byte key[MAX_KEY_SIZE];
wolfSSL 0:1239e9b70ca2 842 int typeH;
wolfSSL 0:1239e9b70ca2 843 int derivedLen;
wolfSSL 0:1239e9b70ca2 844 int decryptionType;
wolfSSL 0:1239e9b70ca2 845 int ret = 0;
wolfSSL 0:1239e9b70ca2 846
wolfSSL 0:1239e9b70ca2 847 switch (id) {
wolfSSL 0:1239e9b70ca2 848 case PBE_MD5_DES:
wolfSSL 0:1239e9b70ca2 849 typeH = MD5;
wolfSSL 0:1239e9b70ca2 850 derivedLen = 16; /* may need iv for v1.5 */
wolfSSL 0:1239e9b70ca2 851 decryptionType = DES_TYPE;
wolfSSL 0:1239e9b70ca2 852 break;
wolfSSL 0:1239e9b70ca2 853
wolfSSL 0:1239e9b70ca2 854 case PBE_SHA1_DES:
wolfSSL 0:1239e9b70ca2 855 typeH = SHA;
wolfSSL 0:1239e9b70ca2 856 derivedLen = 16; /* may need iv for v1.5 */
wolfSSL 0:1239e9b70ca2 857 decryptionType = DES_TYPE;
wolfSSL 0:1239e9b70ca2 858 break;
wolfSSL 0:1239e9b70ca2 859
wolfSSL 0:1239e9b70ca2 860 case PBE_SHA1_DES3:
wolfSSL 0:1239e9b70ca2 861 typeH = SHA;
wolfSSL 0:1239e9b70ca2 862 derivedLen = 32; /* may need iv for v1.5 */
wolfSSL 0:1239e9b70ca2 863 decryptionType = DES3_TYPE;
wolfSSL 0:1239e9b70ca2 864 break;
wolfSSL 0:1239e9b70ca2 865
wolfSSL 0:1239e9b70ca2 866 case PBE_SHA1_RC4_128:
wolfSSL 0:1239e9b70ca2 867 typeH = SHA;
wolfSSL 0:1239e9b70ca2 868 derivedLen = 16;
wolfSSL 0:1239e9b70ca2 869 decryptionType = RC4_TYPE;
wolfSSL 0:1239e9b70ca2 870 break;
wolfSSL 0:1239e9b70ca2 871
wolfSSL 0:1239e9b70ca2 872 default:
wolfSSL 0:1239e9b70ca2 873 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 874 }
wolfSSL 0:1239e9b70ca2 875
wolfSSL 0:1239e9b70ca2 876 if (version == PKCS5v2)
wolfSSL 0:1239e9b70ca2 877 ret = PBKDF2(key, (byte*)password, passwordSz, salt, saltSz, iterations,
wolfSSL 0:1239e9b70ca2 878 derivedLen, typeH);
wolfSSL 0:1239e9b70ca2 879 else if (version == PKCS5)
wolfSSL 0:1239e9b70ca2 880 ret = PBKDF1(key, (byte*)password, passwordSz, salt, saltSz, iterations,
wolfSSL 0:1239e9b70ca2 881 derivedLen, typeH);
wolfSSL 0:1239e9b70ca2 882 else if (version == PKCS12) {
wolfSSL 0:1239e9b70ca2 883 int i, idx = 0;
wolfSSL 0:1239e9b70ca2 884 byte unicodePasswd[MAX_UNICODE_SZ];
wolfSSL 0:1239e9b70ca2 885
wolfSSL 0:1239e9b70ca2 886 if ( (passwordSz * 2 + 2) > (int)sizeof(unicodePasswd))
wolfSSL 0:1239e9b70ca2 887 return UNICODE_SIZE_E;
wolfSSL 0:1239e9b70ca2 888
wolfSSL 0:1239e9b70ca2 889 for (i = 0; i < passwordSz; i++) {
wolfSSL 0:1239e9b70ca2 890 unicodePasswd[idx++] = 0x00;
wolfSSL 0:1239e9b70ca2 891 unicodePasswd[idx++] = (byte)password[i];
wolfSSL 0:1239e9b70ca2 892 }
wolfSSL 0:1239e9b70ca2 893 /* add trailing NULL */
wolfSSL 0:1239e9b70ca2 894 unicodePasswd[idx++] = 0x00;
wolfSSL 0:1239e9b70ca2 895 unicodePasswd[idx++] = 0x00;
wolfSSL 0:1239e9b70ca2 896
wolfSSL 0:1239e9b70ca2 897 ret = PKCS12_PBKDF(key, unicodePasswd, idx, salt, saltSz,
wolfSSL 0:1239e9b70ca2 898 iterations, derivedLen, typeH, 1);
wolfSSL 0:1239e9b70ca2 899 if (decryptionType != RC4_TYPE)
wolfSSL 0:1239e9b70ca2 900 ret += PKCS12_PBKDF(cbcIv, unicodePasswd, idx, salt, saltSz,
wolfSSL 0:1239e9b70ca2 901 iterations, 8, typeH, 2);
wolfSSL 0:1239e9b70ca2 902 }
wolfSSL 0:1239e9b70ca2 903 else
wolfSSL 0:1239e9b70ca2 904 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 905
wolfSSL 0:1239e9b70ca2 906 if (ret != 0)
wolfSSL 0:1239e9b70ca2 907 return ret;
wolfSSL 0:1239e9b70ca2 908
wolfSSL 0:1239e9b70ca2 909 switch (decryptionType) {
wolfSSL 0:1239e9b70ca2 910 #ifndef NO_DES3
wolfSSL 0:1239e9b70ca2 911 case DES_TYPE:
wolfSSL 0:1239e9b70ca2 912 {
wolfSSL 0:1239e9b70ca2 913 Des dec;
wolfSSL 0:1239e9b70ca2 914 byte* desIv = key + 8;
wolfSSL 0:1239e9b70ca2 915
wolfSSL 0:1239e9b70ca2 916 if (version == PKCS5v2 || version == PKCS12)
wolfSSL 0:1239e9b70ca2 917 desIv = cbcIv;
wolfSSL 0:1239e9b70ca2 918
wolfSSL 0:1239e9b70ca2 919 ret = Des_SetKey(&dec, key, desIv, DES_DECRYPTION);
wolfSSL 0:1239e9b70ca2 920 if (ret != 0)
wolfSSL 0:1239e9b70ca2 921 return ret;
wolfSSL 0:1239e9b70ca2 922
wolfSSL 0:1239e9b70ca2 923 Des_CbcDecrypt(&dec, input, input, length);
wolfSSL 0:1239e9b70ca2 924 break;
wolfSSL 0:1239e9b70ca2 925 }
wolfSSL 0:1239e9b70ca2 926
wolfSSL 0:1239e9b70ca2 927 case DES3_TYPE:
wolfSSL 0:1239e9b70ca2 928 {
wolfSSL 0:1239e9b70ca2 929 Des3 dec;
wolfSSL 0:1239e9b70ca2 930 byte* desIv = key + 24;
wolfSSL 0:1239e9b70ca2 931
wolfSSL 0:1239e9b70ca2 932 if (version == PKCS5v2 || version == PKCS12)
wolfSSL 0:1239e9b70ca2 933 desIv = cbcIv;
wolfSSL 0:1239e9b70ca2 934 ret = Des3_SetKey(&dec, key, desIv, DES_DECRYPTION);
wolfSSL 0:1239e9b70ca2 935 if (ret != 0)
wolfSSL 0:1239e9b70ca2 936 return ret;
wolfSSL 0:1239e9b70ca2 937 ret = Des3_CbcDecrypt(&dec, input, input, length);
wolfSSL 0:1239e9b70ca2 938 if (ret != 0)
wolfSSL 0:1239e9b70ca2 939 return ret;
wolfSSL 0:1239e9b70ca2 940 break;
wolfSSL 0:1239e9b70ca2 941 }
wolfSSL 0:1239e9b70ca2 942 #endif
wolfSSL 0:1239e9b70ca2 943 #ifndef NO_RC4
wolfSSL 0:1239e9b70ca2 944 case RC4_TYPE:
wolfSSL 0:1239e9b70ca2 945 {
wolfSSL 0:1239e9b70ca2 946 Arc4 dec;
wolfSSL 0:1239e9b70ca2 947
wolfSSL 0:1239e9b70ca2 948 Arc4SetKey(&dec, key, derivedLen);
wolfSSL 0:1239e9b70ca2 949 Arc4Process(&dec, input, input, length);
wolfSSL 0:1239e9b70ca2 950 break;
wolfSSL 0:1239e9b70ca2 951 }
wolfSSL 0:1239e9b70ca2 952 #endif
wolfSSL 0:1239e9b70ca2 953
wolfSSL 0:1239e9b70ca2 954 default:
wolfSSL 0:1239e9b70ca2 955 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 956 }
wolfSSL 0:1239e9b70ca2 957
wolfSSL 0:1239e9b70ca2 958 return 0;
wolfSSL 0:1239e9b70ca2 959 }
wolfSSL 0:1239e9b70ca2 960
wolfSSL 0:1239e9b70ca2 961
wolfSSL 0:1239e9b70ca2 962 /* Remove Encrypted PKCS8 header, move beginning of traditional to beginning
wolfSSL 0:1239e9b70ca2 963 of input */
wolfSSL 0:1239e9b70ca2 964 int ToTraditionalEnc(byte* input, word32 sz,const char* password,int passwordSz)
wolfSSL 0:1239e9b70ca2 965 {
wolfSSL 0:1239e9b70ca2 966 word32 inOutIdx = 0, oid;
wolfSSL 0:1239e9b70ca2 967 int first, second, length, version, saltSz, id;
wolfSSL 0:1239e9b70ca2 968 int iterations = 0;
wolfSSL 0:1239e9b70ca2 969 byte salt[MAX_SALT_SIZE];
wolfSSL 0:1239e9b70ca2 970 byte cbcIv[MAX_IV_SIZE];
wolfSSL 0:1239e9b70ca2 971
wolfSSL 0:1239e9b70ca2 972 if (GetSequence(input, &inOutIdx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 973 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 974
wolfSSL 0:1239e9b70ca2 975 if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
wolfSSL 0:1239e9b70ca2 976 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 977
wolfSSL 0:1239e9b70ca2 978 first = input[inOutIdx - 2]; /* PKCS version alwyas 2nd to last byte */
wolfSSL 0:1239e9b70ca2 979 second = input[inOutIdx - 1]; /* version.algo, algo id last byte */
wolfSSL 0:1239e9b70ca2 980
wolfSSL 0:1239e9b70ca2 981 if (CheckAlgo(first, second, &id, &version) < 0)
wolfSSL 0:1239e9b70ca2 982 return ASN_INPUT_E; /* Algo ID error */
wolfSSL 0:1239e9b70ca2 983
wolfSSL 0:1239e9b70ca2 984 if (version == PKCS5v2) {
wolfSSL 0:1239e9b70ca2 985
wolfSSL 0:1239e9b70ca2 986 if (GetSequence(input, &inOutIdx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 987 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 988
wolfSSL 0:1239e9b70ca2 989 if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
wolfSSL 0:1239e9b70ca2 990 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 991
wolfSSL 0:1239e9b70ca2 992 if (oid != PBKDF2_OID)
wolfSSL 0:1239e9b70ca2 993 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 994 }
wolfSSL 0:1239e9b70ca2 995
wolfSSL 0:1239e9b70ca2 996 if (GetSequence(input, &inOutIdx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 997 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 998
wolfSSL 0:1239e9b70ca2 999 if (input[inOutIdx++] != ASN_OCTET_STRING)
wolfSSL 0:1239e9b70ca2 1000 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1001
wolfSSL 0:1239e9b70ca2 1002 if (GetLength(input, &inOutIdx, &saltSz, sz) < 0)
wolfSSL 0:1239e9b70ca2 1003 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1004
wolfSSL 0:1239e9b70ca2 1005 if (saltSz > MAX_SALT_SIZE)
wolfSSL 0:1239e9b70ca2 1006 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1007
wolfSSL 0:1239e9b70ca2 1008 XMEMCPY(salt, &input[inOutIdx], saltSz);
wolfSSL 0:1239e9b70ca2 1009 inOutIdx += saltSz;
wolfSSL 0:1239e9b70ca2 1010
wolfSSL 0:1239e9b70ca2 1011 if (GetShortInt(input, &inOutIdx, &iterations) < 0)
wolfSSL 0:1239e9b70ca2 1012 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1013
wolfSSL 0:1239e9b70ca2 1014 if (version == PKCS5v2) {
wolfSSL 0:1239e9b70ca2 1015 /* get encryption algo */
wolfSSL 0:1239e9b70ca2 1016 if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
wolfSSL 0:1239e9b70ca2 1017 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1018
wolfSSL 0:1239e9b70ca2 1019 if (CheckAlgoV2(oid, &id) < 0)
wolfSSL 0:1239e9b70ca2 1020 return ASN_PARSE_E; /* PKCS v2 algo id error */
wolfSSL 0:1239e9b70ca2 1021
wolfSSL 0:1239e9b70ca2 1022 if (input[inOutIdx++] != ASN_OCTET_STRING)
wolfSSL 0:1239e9b70ca2 1023 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1024
wolfSSL 0:1239e9b70ca2 1025 if (GetLength(input, &inOutIdx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 1026 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1027
wolfSSL 0:1239e9b70ca2 1028 XMEMCPY(cbcIv, &input[inOutIdx], length);
wolfSSL 0:1239e9b70ca2 1029 inOutIdx += length;
wolfSSL 0:1239e9b70ca2 1030 }
wolfSSL 0:1239e9b70ca2 1031
wolfSSL 0:1239e9b70ca2 1032 if (input[inOutIdx++] != ASN_OCTET_STRING)
wolfSSL 0:1239e9b70ca2 1033 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1034
wolfSSL 0:1239e9b70ca2 1035 if (GetLength(input, &inOutIdx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 1036 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1037
wolfSSL 0:1239e9b70ca2 1038 if (DecryptKey(password, passwordSz, salt, saltSz, iterations, id,
wolfSSL 0:1239e9b70ca2 1039 input + inOutIdx, length, version, cbcIv) < 0)
wolfSSL 0:1239e9b70ca2 1040 return ASN_INPUT_E; /* decrypt failure */
wolfSSL 0:1239e9b70ca2 1041
wolfSSL 0:1239e9b70ca2 1042 XMEMMOVE(input, input + inOutIdx, length);
wolfSSL 0:1239e9b70ca2 1043 return ToTraditional(input, length);
wolfSSL 0:1239e9b70ca2 1044 }
wolfSSL 0:1239e9b70ca2 1045
wolfSSL 0:1239e9b70ca2 1046 #endif /* NO_PWDBASED */
wolfSSL 0:1239e9b70ca2 1047
wolfSSL 0:1239e9b70ca2 1048 #ifndef NO_RSA
wolfSSL 0:1239e9b70ca2 1049
wolfSSL 0:1239e9b70ca2 1050 int RsaPublicKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key,
wolfSSL 0:1239e9b70ca2 1051 word32 inSz)
wolfSSL 0:1239e9b70ca2 1052 {
wolfSSL 0:1239e9b70ca2 1053 int length;
wolfSSL 0:1239e9b70ca2 1054
wolfSSL 0:1239e9b70ca2 1055 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1056 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1057
wolfSSL 0:1239e9b70ca2 1058 key->type = RSA_PUBLIC;
wolfSSL 0:1239e9b70ca2 1059
wolfSSL 0:1239e9b70ca2 1060 #if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
wolfSSL 0:1239e9b70ca2 1061 {
wolfSSL 0:1239e9b70ca2 1062 byte b = input[*inOutIdx];
wolfSSL 0:1239e9b70ca2 1063 if (b != ASN_INTEGER) {
wolfSSL 0:1239e9b70ca2 1064 /* not from decoded cert, will have algo id, skip past */
wolfSSL 0:1239e9b70ca2 1065 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1066 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1067
wolfSSL 0:1239e9b70ca2 1068 b = input[(*inOutIdx)++];
wolfSSL 0:1239e9b70ca2 1069 if (b != ASN_OBJECT_ID)
wolfSSL 0:1239e9b70ca2 1070 return ASN_OBJECT_ID_E;
wolfSSL 0:1239e9b70ca2 1071
wolfSSL 0:1239e9b70ca2 1072 if (GetLength(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1073 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1074
wolfSSL 0:1239e9b70ca2 1075 *inOutIdx += length; /* skip past */
wolfSSL 0:1239e9b70ca2 1076
wolfSSL 0:1239e9b70ca2 1077 /* could have NULL tag and 0 terminator, but may not */
wolfSSL 0:1239e9b70ca2 1078 b = input[(*inOutIdx)++];
wolfSSL 0:1239e9b70ca2 1079
wolfSSL 0:1239e9b70ca2 1080 if (b == ASN_TAG_NULL) {
wolfSSL 0:1239e9b70ca2 1081 b = input[(*inOutIdx)++];
wolfSSL 0:1239e9b70ca2 1082 if (b != 0)
wolfSSL 0:1239e9b70ca2 1083 return ASN_EXPECT_0_E;
wolfSSL 0:1239e9b70ca2 1084 }
wolfSSL 0:1239e9b70ca2 1085 else
wolfSSL 0:1239e9b70ca2 1086 /* go back, didn't have it */
wolfSSL 0:1239e9b70ca2 1087 (*inOutIdx)--;
wolfSSL 0:1239e9b70ca2 1088
wolfSSL 0:1239e9b70ca2 1089 /* should have bit tag length and seq next */
wolfSSL 0:1239e9b70ca2 1090 b = input[(*inOutIdx)++];
wolfSSL 0:1239e9b70ca2 1091 if (b != ASN_BIT_STRING)
wolfSSL 0:1239e9b70ca2 1092 return ASN_BITSTR_E;
wolfSSL 0:1239e9b70ca2 1093
wolfSSL 0:1239e9b70ca2 1094 if (GetLength(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1095 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1096
wolfSSL 0:1239e9b70ca2 1097 /* could have 0 */
wolfSSL 0:1239e9b70ca2 1098 b = input[(*inOutIdx)++];
wolfSSL 0:1239e9b70ca2 1099 if (b != 0)
wolfSSL 0:1239e9b70ca2 1100 (*inOutIdx)--;
wolfSSL 0:1239e9b70ca2 1101
wolfSSL 0:1239e9b70ca2 1102 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1103 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1104 } /* end if */
wolfSSL 0:1239e9b70ca2 1105 } /* openssl var block */
wolfSSL 0:1239e9b70ca2 1106 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1107
wolfSSL 0:1239e9b70ca2 1108 if (GetInt(&key->n, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1109 GetInt(&key->e, input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E;
wolfSSL 0:1239e9b70ca2 1110
wolfSSL 0:1239e9b70ca2 1111 return 0;
wolfSSL 0:1239e9b70ca2 1112 }
wolfSSL 0:1239e9b70ca2 1113
wolfSSL 0:1239e9b70ca2 1114 #endif
wolfSSL 0:1239e9b70ca2 1115
wolfSSL 0:1239e9b70ca2 1116 #ifndef NO_DH
wolfSSL 0:1239e9b70ca2 1117
wolfSSL 0:1239e9b70ca2 1118 int DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
wolfSSL 0:1239e9b70ca2 1119 {
wolfSSL 0:1239e9b70ca2 1120 int length;
wolfSSL 0:1239e9b70ca2 1121
wolfSSL 0:1239e9b70ca2 1122 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1123 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1124
wolfSSL 0:1239e9b70ca2 1125 if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1126 GetInt(&key->g, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E;
wolfSSL 0:1239e9b70ca2 1127
wolfSSL 0:1239e9b70ca2 1128 return 0;
wolfSSL 0:1239e9b70ca2 1129 }
wolfSSL 0:1239e9b70ca2 1130
wolfSSL 0:1239e9b70ca2 1131 int DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g, word32 gSz)
wolfSSL 0:1239e9b70ca2 1132 {
wolfSSL 0:1239e9b70ca2 1133 if (key == NULL || p == NULL || g == NULL || pSz == 0 || gSz == 0)
wolfSSL 0:1239e9b70ca2 1134 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 1135
wolfSSL 0:1239e9b70ca2 1136 /* may have leading 0 */
wolfSSL 0:1239e9b70ca2 1137 if (p[0] == 0) {
wolfSSL 0:1239e9b70ca2 1138 pSz--; p++;
wolfSSL 0:1239e9b70ca2 1139 }
wolfSSL 0:1239e9b70ca2 1140
wolfSSL 0:1239e9b70ca2 1141 if (g[0] == 0) {
wolfSSL 0:1239e9b70ca2 1142 gSz--; g++;
wolfSSL 0:1239e9b70ca2 1143 }
wolfSSL 0:1239e9b70ca2 1144
wolfSSL 0:1239e9b70ca2 1145 if (mp_init(&key->p) != MP_OKAY)
wolfSSL 0:1239e9b70ca2 1146 return MP_INIT_E;
wolfSSL 0:1239e9b70ca2 1147 if (mp_read_unsigned_bin(&key->p, p, pSz) != 0) {
wolfSSL 0:1239e9b70ca2 1148 mp_clear(&key->p);
wolfSSL 0:1239e9b70ca2 1149 return ASN_DH_KEY_E;
wolfSSL 0:1239e9b70ca2 1150 }
wolfSSL 0:1239e9b70ca2 1151
wolfSSL 0:1239e9b70ca2 1152 if (mp_init(&key->g) != MP_OKAY) {
wolfSSL 0:1239e9b70ca2 1153 mp_clear(&key->p);
wolfSSL 0:1239e9b70ca2 1154 return MP_INIT_E;
wolfSSL 0:1239e9b70ca2 1155 }
wolfSSL 0:1239e9b70ca2 1156 if (mp_read_unsigned_bin(&key->g, g, gSz) != 0) {
wolfSSL 0:1239e9b70ca2 1157 mp_clear(&key->g);
wolfSSL 0:1239e9b70ca2 1158 mp_clear(&key->p);
wolfSSL 0:1239e9b70ca2 1159 return ASN_DH_KEY_E;
wolfSSL 0:1239e9b70ca2 1160 }
wolfSSL 0:1239e9b70ca2 1161
wolfSSL 0:1239e9b70ca2 1162 return 0;
wolfSSL 0:1239e9b70ca2 1163 }
wolfSSL 0:1239e9b70ca2 1164
wolfSSL 0:1239e9b70ca2 1165
wolfSSL 0:1239e9b70ca2 1166 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1167
wolfSSL 0:1239e9b70ca2 1168 int DhParamsLoad(const byte* input, word32 inSz, byte* p, word32* pInOutSz,
wolfSSL 0:1239e9b70ca2 1169 byte* g, word32* gInOutSz)
wolfSSL 0:1239e9b70ca2 1170 {
wolfSSL 0:1239e9b70ca2 1171 word32 i = 0;
wolfSSL 0:1239e9b70ca2 1172 byte b;
wolfSSL 0:1239e9b70ca2 1173 int length;
wolfSSL 0:1239e9b70ca2 1174
wolfSSL 0:1239e9b70ca2 1175 if (GetSequence(input, &i, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1176 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1177
wolfSSL 0:1239e9b70ca2 1178 b = input[i++];
wolfSSL 0:1239e9b70ca2 1179 if (b != ASN_INTEGER)
wolfSSL 0:1239e9b70ca2 1180 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1181
wolfSSL 0:1239e9b70ca2 1182 if (GetLength(input, &i, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1183 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1184
wolfSSL 0:1239e9b70ca2 1185 if ( (b = input[i++]) == 0x00)
wolfSSL 0:1239e9b70ca2 1186 length--;
wolfSSL 0:1239e9b70ca2 1187 else
wolfSSL 0:1239e9b70ca2 1188 i--;
wolfSSL 0:1239e9b70ca2 1189
wolfSSL 0:1239e9b70ca2 1190 if (length <= (int)*pInOutSz) {
wolfSSL 0:1239e9b70ca2 1191 XMEMCPY(p, &input[i], length);
wolfSSL 0:1239e9b70ca2 1192 *pInOutSz = length;
wolfSSL 0:1239e9b70ca2 1193 }
wolfSSL 0:1239e9b70ca2 1194 else
wolfSSL 0:1239e9b70ca2 1195 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 1196
wolfSSL 0:1239e9b70ca2 1197 i += length;
wolfSSL 0:1239e9b70ca2 1198
wolfSSL 0:1239e9b70ca2 1199 b = input[i++];
wolfSSL 0:1239e9b70ca2 1200 if (b != ASN_INTEGER)
wolfSSL 0:1239e9b70ca2 1201 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1202
wolfSSL 0:1239e9b70ca2 1203 if (GetLength(input, &i, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1204 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1205
wolfSSL 0:1239e9b70ca2 1206 if (length <= (int)*gInOutSz) {
wolfSSL 0:1239e9b70ca2 1207 XMEMCPY(g, &input[i], length);
wolfSSL 0:1239e9b70ca2 1208 *gInOutSz = length;
wolfSSL 0:1239e9b70ca2 1209 }
wolfSSL 0:1239e9b70ca2 1210 else
wolfSSL 0:1239e9b70ca2 1211 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 1212
wolfSSL 0:1239e9b70ca2 1213 return 0;
wolfSSL 0:1239e9b70ca2 1214 }
wolfSSL 0:1239e9b70ca2 1215
wolfSSL 0:1239e9b70ca2 1216 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1217 #endif /* NO_DH */
wolfSSL 0:1239e9b70ca2 1218
wolfSSL 0:1239e9b70ca2 1219
wolfSSL 0:1239e9b70ca2 1220 #ifndef NO_DSA
wolfSSL 0:1239e9b70ca2 1221
wolfSSL 0:1239e9b70ca2 1222 int DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
wolfSSL 0:1239e9b70ca2 1223 word32 inSz)
wolfSSL 0:1239e9b70ca2 1224 {
wolfSSL 0:1239e9b70ca2 1225 int length;
wolfSSL 0:1239e9b70ca2 1226
wolfSSL 0:1239e9b70ca2 1227 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1228 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1229
wolfSSL 0:1239e9b70ca2 1230 if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1231 GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1232 GetInt(&key->g, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1233 GetInt(&key->y, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E;
wolfSSL 0:1239e9b70ca2 1234
wolfSSL 0:1239e9b70ca2 1235 key->type = DSA_PUBLIC;
wolfSSL 0:1239e9b70ca2 1236 return 0;
wolfSSL 0:1239e9b70ca2 1237 }
wolfSSL 0:1239e9b70ca2 1238
wolfSSL 0:1239e9b70ca2 1239
wolfSSL 0:1239e9b70ca2 1240 int DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
wolfSSL 0:1239e9b70ca2 1241 word32 inSz)
wolfSSL 0:1239e9b70ca2 1242 {
wolfSSL 0:1239e9b70ca2 1243 int length, version;
wolfSSL 0:1239e9b70ca2 1244
wolfSSL 0:1239e9b70ca2 1245 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 1246 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1247
wolfSSL 0:1239e9b70ca2 1248 if (GetMyVersion(input, inOutIdx, &version) < 0)
wolfSSL 0:1239e9b70ca2 1249 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1250
wolfSSL 0:1239e9b70ca2 1251 if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1252 GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1253 GetInt(&key->g, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1254 GetInt(&key->y, input, inOutIdx, inSz) < 0 ||
wolfSSL 0:1239e9b70ca2 1255 GetInt(&key->x, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E;
wolfSSL 0:1239e9b70ca2 1256
wolfSSL 0:1239e9b70ca2 1257 key->type = DSA_PRIVATE;
wolfSSL 0:1239e9b70ca2 1258 return 0;
wolfSSL 0:1239e9b70ca2 1259 }
wolfSSL 0:1239e9b70ca2 1260
wolfSSL 0:1239e9b70ca2 1261 #endif /* NO_DSA */
wolfSSL 0:1239e9b70ca2 1262
wolfSSL 0:1239e9b70ca2 1263
wolfSSL 0:1239e9b70ca2 1264 void InitDecodedCert(DecodedCert* cert, byte* source, word32 inSz, void* heap)
wolfSSL 0:1239e9b70ca2 1265 {
wolfSSL 0:1239e9b70ca2 1266 cert->publicKey = 0;
wolfSSL 0:1239e9b70ca2 1267 cert->pubKeySize = 0;
wolfSSL 0:1239e9b70ca2 1268 cert->pubKeyStored = 0;
wolfSSL 0:1239e9b70ca2 1269 cert->version = 0;
wolfSSL 0:1239e9b70ca2 1270 cert->signature = 0;
wolfSSL 0:1239e9b70ca2 1271 cert->subjectCN = 0;
wolfSSL 0:1239e9b70ca2 1272 cert->subjectCNLen = 0;
wolfSSL 0:1239e9b70ca2 1273 cert->subjectCNStored = 0;
wolfSSL 0:1239e9b70ca2 1274 cert->altNames = NULL;
wolfSSL 0:1239e9b70ca2 1275 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 1276 cert->altEmailNames = NULL;
wolfSSL 0:1239e9b70ca2 1277 cert->permittedNames = NULL;
wolfSSL 0:1239e9b70ca2 1278 cert->excludedNames = NULL;
wolfSSL 0:1239e9b70ca2 1279 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 1280 cert->issuer[0] = '\0';
wolfSSL 0:1239e9b70ca2 1281 cert->subject[0] = '\0';
wolfSSL 0:1239e9b70ca2 1282 cert->source = source; /* don't own */
wolfSSL 0:1239e9b70ca2 1283 cert->srcIdx = 0;
wolfSSL 0:1239e9b70ca2 1284 cert->maxIdx = inSz; /* can't go over this index */
wolfSSL 0:1239e9b70ca2 1285 cert->heap = heap;
wolfSSL 0:1239e9b70ca2 1286 XMEMSET(cert->serial, 0, EXTERNAL_SERIAL_SIZE);
wolfSSL 0:1239e9b70ca2 1287 cert->serialSz = 0;
wolfSSL 0:1239e9b70ca2 1288 cert->extensions = 0;
wolfSSL 0:1239e9b70ca2 1289 cert->extensionsSz = 0;
wolfSSL 0:1239e9b70ca2 1290 cert->extensionsIdx = 0;
wolfSSL 0:1239e9b70ca2 1291 cert->extAuthInfo = NULL;
wolfSSL 0:1239e9b70ca2 1292 cert->extAuthInfoSz = 0;
wolfSSL 0:1239e9b70ca2 1293 cert->extCrlInfo = NULL;
wolfSSL 0:1239e9b70ca2 1294 cert->extCrlInfoSz = 0;
wolfSSL 0:1239e9b70ca2 1295 XMEMSET(cert->extSubjKeyId, 0, SHA_SIZE);
wolfSSL 0:1239e9b70ca2 1296 cert->extSubjKeyIdSet = 0;
wolfSSL 0:1239e9b70ca2 1297 XMEMSET(cert->extAuthKeyId, 0, SHA_SIZE);
wolfSSL 0:1239e9b70ca2 1298 cert->extAuthKeyIdSet = 0;
wolfSSL 0:1239e9b70ca2 1299 cert->extKeyUsageSet = 0;
wolfSSL 0:1239e9b70ca2 1300 cert->extKeyUsage = 0;
wolfSSL 0:1239e9b70ca2 1301 cert->extExtKeyUsageSet = 0;
wolfSSL 0:1239e9b70ca2 1302 cert->extExtKeyUsage = 0;
wolfSSL 0:1239e9b70ca2 1303 cert->isCA = 0;
wolfSSL 0:1239e9b70ca2 1304 #ifdef HAVE_PKCS7
wolfSSL 0:1239e9b70ca2 1305 cert->issuerRaw = NULL;
wolfSSL 0:1239e9b70ca2 1306 cert->issuerRawLen = 0;
wolfSSL 0:1239e9b70ca2 1307 #endif
wolfSSL 0:1239e9b70ca2 1308 #ifdef CYASSL_CERT_GEN
wolfSSL 0:1239e9b70ca2 1309 cert->subjectSN = 0;
wolfSSL 0:1239e9b70ca2 1310 cert->subjectSNLen = 0;
wolfSSL 0:1239e9b70ca2 1311 cert->subjectC = 0;
wolfSSL 0:1239e9b70ca2 1312 cert->subjectCLen = 0;
wolfSSL 0:1239e9b70ca2 1313 cert->subjectL = 0;
wolfSSL 0:1239e9b70ca2 1314 cert->subjectLLen = 0;
wolfSSL 0:1239e9b70ca2 1315 cert->subjectST = 0;
wolfSSL 0:1239e9b70ca2 1316 cert->subjectSTLen = 0;
wolfSSL 0:1239e9b70ca2 1317 cert->subjectO = 0;
wolfSSL 0:1239e9b70ca2 1318 cert->subjectOLen = 0;
wolfSSL 0:1239e9b70ca2 1319 cert->subjectOU = 0;
wolfSSL 0:1239e9b70ca2 1320 cert->subjectOULen = 0;
wolfSSL 0:1239e9b70ca2 1321 cert->subjectEmail = 0;
wolfSSL 0:1239e9b70ca2 1322 cert->subjectEmailLen = 0;
wolfSSL 0:1239e9b70ca2 1323 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 1324 cert->beforeDate = NULL;
wolfSSL 0:1239e9b70ca2 1325 cert->beforeDateLen = 0;
wolfSSL 0:1239e9b70ca2 1326 cert->afterDate = NULL;
wolfSSL 0:1239e9b70ca2 1327 cert->afterDateLen = 0;
wolfSSL 0:1239e9b70ca2 1328 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1329 XMEMSET(&cert->issuerName, 0, sizeof(DecodedName));
wolfSSL 0:1239e9b70ca2 1330 XMEMSET(&cert->subjectName, 0, sizeof(DecodedName));
wolfSSL 0:1239e9b70ca2 1331 cert->extBasicConstSet = 0;
wolfSSL 0:1239e9b70ca2 1332 cert->extBasicConstCrit = 0;
wolfSSL 0:1239e9b70ca2 1333 cert->extBasicConstPlSet = 0;
wolfSSL 0:1239e9b70ca2 1334 cert->pathLength = 0;
wolfSSL 0:1239e9b70ca2 1335 cert->extSubjAltNameSet = 0;
wolfSSL 0:1239e9b70ca2 1336 cert->extSubjAltNameCrit = 0;
wolfSSL 0:1239e9b70ca2 1337 cert->extAuthKeyIdCrit = 0;
wolfSSL 0:1239e9b70ca2 1338 cert->extSubjKeyIdCrit = 0;
wolfSSL 0:1239e9b70ca2 1339 cert->extKeyUsageCrit = 0;
wolfSSL 0:1239e9b70ca2 1340 cert->extExtKeyUsageCrit = 0;
wolfSSL 0:1239e9b70ca2 1341 cert->extExtKeyUsageSrc = NULL;
wolfSSL 0:1239e9b70ca2 1342 cert->extExtKeyUsageSz = 0;
wolfSSL 0:1239e9b70ca2 1343 cert->extExtKeyUsageCount = 0;
wolfSSL 0:1239e9b70ca2 1344 cert->extAuthKeyIdSrc = NULL;
wolfSSL 0:1239e9b70ca2 1345 cert->extAuthKeyIdSz = 0;
wolfSSL 0:1239e9b70ca2 1346 cert->extSubjKeyIdSrc = NULL;
wolfSSL 0:1239e9b70ca2 1347 cert->extSubjKeyIdSz = 0;
wolfSSL 0:1239e9b70ca2 1348 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1349 #if defined(OPENSSL_EXTRA) || !defined(IGNORE_NAME_CONSTRAINTS)
wolfSSL 0:1239e9b70ca2 1350 cert->extNameConstraintSet = 0;
wolfSSL 0:1239e9b70ca2 1351 #endif /* OPENSSL_EXTRA || !IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 1352 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 1353 cert->pkCurveOID = 0;
wolfSSL 0:1239e9b70ca2 1354 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 1355 #ifdef CYASSL_SEP
wolfSSL 0:1239e9b70ca2 1356 cert->deviceTypeSz = 0;
wolfSSL 0:1239e9b70ca2 1357 cert->deviceType = NULL;
wolfSSL 0:1239e9b70ca2 1358 cert->hwTypeSz = 0;
wolfSSL 0:1239e9b70ca2 1359 cert->hwType = NULL;
wolfSSL 0:1239e9b70ca2 1360 cert->hwSerialNumSz = 0;
wolfSSL 0:1239e9b70ca2 1361 cert->hwSerialNum = NULL;
wolfSSL 0:1239e9b70ca2 1362 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1363 cert->extCertPolicySet = 0;
wolfSSL 0:1239e9b70ca2 1364 cert->extCertPolicyCrit = 0;
wolfSSL 0:1239e9b70ca2 1365 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1366 #endif /* CYASSL_SEP */
wolfSSL 0:1239e9b70ca2 1367 }
wolfSSL 0:1239e9b70ca2 1368
wolfSSL 0:1239e9b70ca2 1369
wolfSSL 0:1239e9b70ca2 1370 void FreeAltNames(DNS_entry* altNames, void* heap)
wolfSSL 0:1239e9b70ca2 1371 {
wolfSSL 0:1239e9b70ca2 1372 (void)heap;
wolfSSL 0:1239e9b70ca2 1373
wolfSSL 0:1239e9b70ca2 1374 while (altNames) {
wolfSSL 0:1239e9b70ca2 1375 DNS_entry* tmp = altNames->next;
wolfSSL 0:1239e9b70ca2 1376
wolfSSL 0:1239e9b70ca2 1377 XFREE(altNames->name, heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 1378 XFREE(altNames, heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 1379 altNames = tmp;
wolfSSL 0:1239e9b70ca2 1380 }
wolfSSL 0:1239e9b70ca2 1381 }
wolfSSL 0:1239e9b70ca2 1382
wolfSSL 0:1239e9b70ca2 1383 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 1384
wolfSSL 0:1239e9b70ca2 1385 void FreeNameSubtrees(Base_entry* names, void* heap)
wolfSSL 0:1239e9b70ca2 1386 {
wolfSSL 0:1239e9b70ca2 1387 (void)heap;
wolfSSL 0:1239e9b70ca2 1388
wolfSSL 0:1239e9b70ca2 1389 while (names) {
wolfSSL 0:1239e9b70ca2 1390 Base_entry* tmp = names->next;
wolfSSL 0:1239e9b70ca2 1391
wolfSSL 0:1239e9b70ca2 1392 XFREE(names->name, heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 1393 XFREE(names, heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 1394 names = tmp;
wolfSSL 0:1239e9b70ca2 1395 }
wolfSSL 0:1239e9b70ca2 1396 }
wolfSSL 0:1239e9b70ca2 1397
wolfSSL 0:1239e9b70ca2 1398 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 1399
wolfSSL 0:1239e9b70ca2 1400 void FreeDecodedCert(DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 1401 {
wolfSSL 0:1239e9b70ca2 1402 if (cert->subjectCNStored == 1)
wolfSSL 0:1239e9b70ca2 1403 XFREE(cert->subjectCN, cert->heap, DYNAMIC_TYPE_SUBJECT_CN);
wolfSSL 0:1239e9b70ca2 1404 if (cert->pubKeyStored == 1)
wolfSSL 0:1239e9b70ca2 1405 XFREE(cert->publicKey, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
wolfSSL 0:1239e9b70ca2 1406 if (cert->altNames)
wolfSSL 0:1239e9b70ca2 1407 FreeAltNames(cert->altNames, cert->heap);
wolfSSL 0:1239e9b70ca2 1408 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 1409 if (cert->altEmailNames)
wolfSSL 0:1239e9b70ca2 1410 FreeAltNames(cert->altEmailNames, cert->heap);
wolfSSL 0:1239e9b70ca2 1411 if (cert->permittedNames)
wolfSSL 0:1239e9b70ca2 1412 FreeNameSubtrees(cert->permittedNames, cert->heap);
wolfSSL 0:1239e9b70ca2 1413 if (cert->excludedNames)
wolfSSL 0:1239e9b70ca2 1414 FreeNameSubtrees(cert->excludedNames, cert->heap);
wolfSSL 0:1239e9b70ca2 1415 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 1416 #ifdef CYASSL_SEP
wolfSSL 0:1239e9b70ca2 1417 XFREE(cert->deviceType, cert->heap, 0);
wolfSSL 0:1239e9b70ca2 1418 XFREE(cert->hwType, cert->heap, 0);
wolfSSL 0:1239e9b70ca2 1419 XFREE(cert->hwSerialNum, cert->heap, 0);
wolfSSL 0:1239e9b70ca2 1420 #endif /* CYASSL_SEP */
wolfSSL 0:1239e9b70ca2 1421 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1422 if (cert->issuerName.fullName != NULL)
wolfSSL 0:1239e9b70ca2 1423 XFREE(cert->issuerName.fullName, NULL, DYNAMIC_TYPE_X509);
wolfSSL 0:1239e9b70ca2 1424 if (cert->subjectName.fullName != NULL)
wolfSSL 0:1239e9b70ca2 1425 XFREE(cert->subjectName.fullName, NULL, DYNAMIC_TYPE_X509);
wolfSSL 0:1239e9b70ca2 1426 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1427 }
wolfSSL 0:1239e9b70ca2 1428
wolfSSL 0:1239e9b70ca2 1429
wolfSSL 0:1239e9b70ca2 1430 static int GetCertHeader(DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 1431 {
wolfSSL 0:1239e9b70ca2 1432 int ret = 0, len;
wolfSSL 0:1239e9b70ca2 1433 byte serialTmp[EXTERNAL_SERIAL_SIZE];
wolfSSL 0:1239e9b70ca2 1434 mp_int mpi;
wolfSSL 0:1239e9b70ca2 1435
wolfSSL 0:1239e9b70ca2 1436 if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1437 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1438
wolfSSL 0:1239e9b70ca2 1439 cert->certBegin = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1440
wolfSSL 0:1239e9b70ca2 1441 if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1442 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1443 cert->sigIndex = len + cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1444
wolfSSL 0:1239e9b70ca2 1445 if (GetExplicitVersion(cert->source, &cert->srcIdx, &cert->version) < 0)
wolfSSL 0:1239e9b70ca2 1446 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1447
wolfSSL 0:1239e9b70ca2 1448 if (GetInt(&mpi, cert->source, &cert->srcIdx, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1449 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1450
wolfSSL 0:1239e9b70ca2 1451 len = mp_unsigned_bin_size(&mpi);
wolfSSL 0:1239e9b70ca2 1452 if (len < (int)sizeof(serialTmp)) {
wolfSSL 0:1239e9b70ca2 1453 if ( (ret = mp_to_unsigned_bin(&mpi, serialTmp)) == MP_OKAY) {
wolfSSL 0:1239e9b70ca2 1454 if (len > EXTERNAL_SERIAL_SIZE)
wolfSSL 0:1239e9b70ca2 1455 len = EXTERNAL_SERIAL_SIZE;
wolfSSL 0:1239e9b70ca2 1456 XMEMCPY(cert->serial, serialTmp, len);
wolfSSL 0:1239e9b70ca2 1457 cert->serialSz = len;
wolfSSL 0:1239e9b70ca2 1458 }
wolfSSL 0:1239e9b70ca2 1459 }
wolfSSL 0:1239e9b70ca2 1460 mp_clear(&mpi);
wolfSSL 0:1239e9b70ca2 1461 return ret;
wolfSSL 0:1239e9b70ca2 1462 }
wolfSSL 0:1239e9b70ca2 1463
wolfSSL 0:1239e9b70ca2 1464 #if !defined(NO_RSA)
wolfSSL 0:1239e9b70ca2 1465 /* Store Rsa Key, may save later, Dsa could use in future */
wolfSSL 0:1239e9b70ca2 1466 static int StoreRsaKey(DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 1467 {
wolfSSL 0:1239e9b70ca2 1468 int length;
wolfSSL 0:1239e9b70ca2 1469 word32 recvd = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1470
wolfSSL 0:1239e9b70ca2 1471 if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1472 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1473
wolfSSL 0:1239e9b70ca2 1474 recvd = cert->srcIdx - recvd;
wolfSSL 0:1239e9b70ca2 1475 length += recvd;
wolfSSL 0:1239e9b70ca2 1476
wolfSSL 0:1239e9b70ca2 1477 while (recvd--)
wolfSSL 0:1239e9b70ca2 1478 cert->srcIdx--;
wolfSSL 0:1239e9b70ca2 1479
wolfSSL 0:1239e9b70ca2 1480 cert->pubKeySize = length;
wolfSSL 0:1239e9b70ca2 1481 cert->publicKey = cert->source + cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1482 cert->srcIdx += length;
wolfSSL 0:1239e9b70ca2 1483
wolfSSL 0:1239e9b70ca2 1484 return 0;
wolfSSL 0:1239e9b70ca2 1485 }
wolfSSL 0:1239e9b70ca2 1486 #endif
wolfSSL 0:1239e9b70ca2 1487
wolfSSL 0:1239e9b70ca2 1488
wolfSSL 0:1239e9b70ca2 1489 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 1490
wolfSSL 0:1239e9b70ca2 1491 /* return 0 on sucess if the ECC curve oid sum is supported */
wolfSSL 0:1239e9b70ca2 1492 static int CheckCurve(word32 oid)
wolfSSL 0:1239e9b70ca2 1493 {
wolfSSL 0:1239e9b70ca2 1494 if (oid != ECC_256R1 && oid != ECC_384R1 && oid != ECC_521R1 && oid !=
wolfSSL 0:1239e9b70ca2 1495 ECC_160R1 && oid != ECC_192R1 && oid != ECC_224R1)
wolfSSL 0:1239e9b70ca2 1496 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 1497
wolfSSL 0:1239e9b70ca2 1498 return 0;
wolfSSL 0:1239e9b70ca2 1499 }
wolfSSL 0:1239e9b70ca2 1500
wolfSSL 0:1239e9b70ca2 1501 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 1502
wolfSSL 0:1239e9b70ca2 1503
wolfSSL 0:1239e9b70ca2 1504 static int GetKey(DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 1505 {
wolfSSL 0:1239e9b70ca2 1506 int length;
wolfSSL 0:1239e9b70ca2 1507 #ifdef HAVE_NTRU
wolfSSL 0:1239e9b70ca2 1508 int tmpIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1509 #endif
wolfSSL 0:1239e9b70ca2 1510
wolfSSL 0:1239e9b70ca2 1511 if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1512 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1513
wolfSSL 0:1239e9b70ca2 1514 if (GetAlgoId(cert->source, &cert->srcIdx, &cert->keyOID, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1515 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1516
wolfSSL 0:1239e9b70ca2 1517 switch (cert->keyOID) {
wolfSSL 0:1239e9b70ca2 1518 #ifndef NO_RSA
wolfSSL 0:1239e9b70ca2 1519 case RSAk:
wolfSSL 0:1239e9b70ca2 1520 {
wolfSSL 0:1239e9b70ca2 1521 byte b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 1522 if (b != ASN_BIT_STRING)
wolfSSL 0:1239e9b70ca2 1523 return ASN_BITSTR_E;
wolfSSL 0:1239e9b70ca2 1524
wolfSSL 0:1239e9b70ca2 1525 if (GetLength(cert->source,&cert->srcIdx,&length,cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1526 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1527 b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 1528 if (b != 0x00)
wolfSSL 0:1239e9b70ca2 1529 return ASN_EXPECT_0_E;
wolfSSL 0:1239e9b70ca2 1530
wolfSSL 0:1239e9b70ca2 1531 return StoreRsaKey(cert);
wolfSSL 0:1239e9b70ca2 1532 }
wolfSSL 0:1239e9b70ca2 1533
wolfSSL 0:1239e9b70ca2 1534 #endif /* NO_RSA */
wolfSSL 0:1239e9b70ca2 1535 #ifdef HAVE_NTRU
wolfSSL 0:1239e9b70ca2 1536 case NTRUk:
wolfSSL 0:1239e9b70ca2 1537 {
wolfSSL 0:1239e9b70ca2 1538 const byte* key = &cert->source[tmpIdx];
wolfSSL 0:1239e9b70ca2 1539 byte* next = (byte*)key;
wolfSSL 0:1239e9b70ca2 1540 word16 keyLen;
wolfSSL 0:1239e9b70ca2 1541 byte keyBlob[MAX_NTRU_KEY_SZ];
wolfSSL 0:1239e9b70ca2 1542
wolfSSL 0:1239e9b70ca2 1543 word32 rc = crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key,
wolfSSL 0:1239e9b70ca2 1544 &keyLen, NULL, &next);
wolfSSL 0:1239e9b70ca2 1545
wolfSSL 0:1239e9b70ca2 1546 if (rc != NTRU_OK)
wolfSSL 0:1239e9b70ca2 1547 return ASN_NTRU_KEY_E;
wolfSSL 0:1239e9b70ca2 1548 if (keyLen > sizeof(keyBlob))
wolfSSL 0:1239e9b70ca2 1549 return ASN_NTRU_KEY_E;
wolfSSL 0:1239e9b70ca2 1550
wolfSSL 0:1239e9b70ca2 1551 rc = crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key,&keyLen,
wolfSSL 0:1239e9b70ca2 1552 keyBlob, &next);
wolfSSL 0:1239e9b70ca2 1553 if (rc != NTRU_OK)
wolfSSL 0:1239e9b70ca2 1554 return ASN_NTRU_KEY_E;
wolfSSL 0:1239e9b70ca2 1555
wolfSSL 0:1239e9b70ca2 1556 if ( (next - key) < 0)
wolfSSL 0:1239e9b70ca2 1557 return ASN_NTRU_KEY_E;
wolfSSL 0:1239e9b70ca2 1558
wolfSSL 0:1239e9b70ca2 1559 cert->srcIdx = tmpIdx + (int)(next - key);
wolfSSL 0:1239e9b70ca2 1560
wolfSSL 0:1239e9b70ca2 1561 cert->publicKey = (byte*) XMALLOC(keyLen, cert->heap,
wolfSSL 0:1239e9b70ca2 1562 DYNAMIC_TYPE_PUBLIC_KEY);
wolfSSL 0:1239e9b70ca2 1563 if (cert->publicKey == NULL)
wolfSSL 0:1239e9b70ca2 1564 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 1565 XMEMCPY(cert->publicKey, keyBlob, keyLen);
wolfSSL 0:1239e9b70ca2 1566 cert->pubKeyStored = 1;
wolfSSL 0:1239e9b70ca2 1567 cert->pubKeySize = keyLen;
wolfSSL 0:1239e9b70ca2 1568
wolfSSL 0:1239e9b70ca2 1569 return 0;
wolfSSL 0:1239e9b70ca2 1570 }
wolfSSL 0:1239e9b70ca2 1571 #endif /* HAVE_NTRU */
wolfSSL 0:1239e9b70ca2 1572 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 1573 case ECDSAk:
wolfSSL 0:1239e9b70ca2 1574 {
wolfSSL 0:1239e9b70ca2 1575 int oidSz = 0;
wolfSSL 0:1239e9b70ca2 1576 byte b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 1577
wolfSSL 0:1239e9b70ca2 1578 if (b != ASN_OBJECT_ID)
wolfSSL 0:1239e9b70ca2 1579 return ASN_OBJECT_ID_E;
wolfSSL 0:1239e9b70ca2 1580
wolfSSL 0:1239e9b70ca2 1581 if (GetLength(cert->source,&cert->srcIdx,&oidSz,cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1582 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1583
wolfSSL 0:1239e9b70ca2 1584 while(oidSz--)
wolfSSL 0:1239e9b70ca2 1585 cert->pkCurveOID += cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 1586
wolfSSL 0:1239e9b70ca2 1587 if (CheckCurve(cert->pkCurveOID) < 0)
wolfSSL 0:1239e9b70ca2 1588 return ECC_CURVE_OID_E;
wolfSSL 0:1239e9b70ca2 1589
wolfSSL 0:1239e9b70ca2 1590 /* key header */
wolfSSL 0:1239e9b70ca2 1591 b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 1592 if (b != ASN_BIT_STRING)
wolfSSL 0:1239e9b70ca2 1593 return ASN_BITSTR_E;
wolfSSL 0:1239e9b70ca2 1594
wolfSSL 0:1239e9b70ca2 1595 if (GetLength(cert->source,&cert->srcIdx,&length,cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1596 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1597 b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 1598 if (b != 0x00)
wolfSSL 0:1239e9b70ca2 1599 return ASN_EXPECT_0_E;
wolfSSL 0:1239e9b70ca2 1600
wolfSSL 0:1239e9b70ca2 1601 /* actual key, use length - 1 since ate preceding 0 */
wolfSSL 0:1239e9b70ca2 1602 length -= 1;
wolfSSL 0:1239e9b70ca2 1603
wolfSSL 0:1239e9b70ca2 1604 cert->publicKey = (byte*) XMALLOC(length, cert->heap,
wolfSSL 0:1239e9b70ca2 1605 DYNAMIC_TYPE_PUBLIC_KEY);
wolfSSL 0:1239e9b70ca2 1606 if (cert->publicKey == NULL)
wolfSSL 0:1239e9b70ca2 1607 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 1608 XMEMCPY(cert->publicKey, &cert->source[cert->srcIdx], length);
wolfSSL 0:1239e9b70ca2 1609 cert->pubKeyStored = 1;
wolfSSL 0:1239e9b70ca2 1610 cert->pubKeySize = length;
wolfSSL 0:1239e9b70ca2 1611
wolfSSL 0:1239e9b70ca2 1612 cert->srcIdx += length;
wolfSSL 0:1239e9b70ca2 1613
wolfSSL 0:1239e9b70ca2 1614 return 0;
wolfSSL 0:1239e9b70ca2 1615 }
wolfSSL 0:1239e9b70ca2 1616 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 1617 default:
wolfSSL 0:1239e9b70ca2 1618 return ASN_UNKNOWN_OID_E;
wolfSSL 0:1239e9b70ca2 1619 }
wolfSSL 0:1239e9b70ca2 1620 }
wolfSSL 0:1239e9b70ca2 1621
wolfSSL 0:1239e9b70ca2 1622
wolfSSL 0:1239e9b70ca2 1623 /* process NAME, either issuer or subject */
wolfSSL 0:1239e9b70ca2 1624 static int GetName(DecodedCert* cert, int nameType)
wolfSSL 0:1239e9b70ca2 1625 {
wolfSSL 0:1239e9b70ca2 1626 Sha sha; /* MUST have SHA-1 hash for cert names */
wolfSSL 0:1239e9b70ca2 1627 int length; /* length of all distinguished names */
wolfSSL 0:1239e9b70ca2 1628 int dummy;
wolfSSL 0:1239e9b70ca2 1629 int ret;
wolfSSL 0:1239e9b70ca2 1630 char* full = (nameType == ISSUER) ? cert->issuer : cert->subject;
wolfSSL 0:1239e9b70ca2 1631 word32 idx;
wolfSSL 0:1239e9b70ca2 1632 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1633 DecodedName* dName =
wolfSSL 0:1239e9b70ca2 1634 (nameType == ISSUER) ? &cert->issuerName : &cert->subjectName;
wolfSSL 0:1239e9b70ca2 1635 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1636
wolfSSL 0:1239e9b70ca2 1637 CYASSL_MSG("Getting Cert Name");
wolfSSL 0:1239e9b70ca2 1638
wolfSSL 0:1239e9b70ca2 1639 if (cert->source[cert->srcIdx] == ASN_OBJECT_ID) {
wolfSSL 0:1239e9b70ca2 1640 CYASSL_MSG("Trying optional prefix...");
wolfSSL 0:1239e9b70ca2 1641
wolfSSL 0:1239e9b70ca2 1642 if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1643 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1644
wolfSSL 0:1239e9b70ca2 1645 cert->srcIdx += length;
wolfSSL 0:1239e9b70ca2 1646 CYASSL_MSG("Got optional prefix");
wolfSSL 0:1239e9b70ca2 1647 }
wolfSSL 0:1239e9b70ca2 1648
wolfSSL 0:1239e9b70ca2 1649 /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be
wolfSSL 0:1239e9b70ca2 1650 * calculated over the entire DER encoding of the Name field, including
wolfSSL 0:1239e9b70ca2 1651 * the tag and length. */
wolfSSL 0:1239e9b70ca2 1652 idx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1653 if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1654 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1655
wolfSSL 0:1239e9b70ca2 1656 ret = InitSha(&sha);
wolfSSL 0:1239e9b70ca2 1657 if (ret != 0)
wolfSSL 0:1239e9b70ca2 1658 return ret;
wolfSSL 0:1239e9b70ca2 1659 ShaUpdate(&sha, &cert->source[idx], length + cert->srcIdx - idx);
wolfSSL 0:1239e9b70ca2 1660 if (nameType == ISSUER)
wolfSSL 0:1239e9b70ca2 1661 ShaFinal(&sha, cert->issuerHash);
wolfSSL 0:1239e9b70ca2 1662 else
wolfSSL 0:1239e9b70ca2 1663 ShaFinal(&sha, cert->subjectHash);
wolfSSL 0:1239e9b70ca2 1664
wolfSSL 0:1239e9b70ca2 1665 length += cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1666 idx = 0;
wolfSSL 0:1239e9b70ca2 1667
wolfSSL 0:1239e9b70ca2 1668 #ifdef HAVE_PKCS7
wolfSSL 0:1239e9b70ca2 1669 /* store pointer to raw issuer */
wolfSSL 0:1239e9b70ca2 1670 if (nameType == ISSUER) {
wolfSSL 0:1239e9b70ca2 1671 cert->issuerRaw = &cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1672 cert->issuerRawLen = length - cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1673 }
wolfSSL 0:1239e9b70ca2 1674 #endif
wolfSSL 0:1239e9b70ca2 1675 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 1676 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1677 cert->subjectRaw = &cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1678 cert->subjectRawLen = length - cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1679 }
wolfSSL 0:1239e9b70ca2 1680 #endif
wolfSSL 0:1239e9b70ca2 1681
wolfSSL 0:1239e9b70ca2 1682 while (cert->srcIdx < (word32)length) {
wolfSSL 0:1239e9b70ca2 1683 byte b;
wolfSSL 0:1239e9b70ca2 1684 byte joint[2];
wolfSSL 0:1239e9b70ca2 1685 byte tooBig = FALSE;
wolfSSL 0:1239e9b70ca2 1686 int oidSz;
wolfSSL 0:1239e9b70ca2 1687
wolfSSL 0:1239e9b70ca2 1688 if (GetSet(cert->source, &cert->srcIdx, &dummy, cert->maxIdx) < 0) {
wolfSSL 0:1239e9b70ca2 1689 CYASSL_MSG("Cert name lacks set header, trying sequence");
wolfSSL 0:1239e9b70ca2 1690 }
wolfSSL 0:1239e9b70ca2 1691
wolfSSL 0:1239e9b70ca2 1692 if (GetSequence(cert->source, &cert->srcIdx, &dummy, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1693 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1694
wolfSSL 0:1239e9b70ca2 1695 b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 1696 if (b != ASN_OBJECT_ID)
wolfSSL 0:1239e9b70ca2 1697 return ASN_OBJECT_ID_E;
wolfSSL 0:1239e9b70ca2 1698
wolfSSL 0:1239e9b70ca2 1699 if (GetLength(cert->source, &cert->srcIdx, &oidSz, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1700 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1701
wolfSSL 0:1239e9b70ca2 1702 XMEMCPY(joint, &cert->source[cert->srcIdx], sizeof(joint));
wolfSSL 0:1239e9b70ca2 1703
wolfSSL 0:1239e9b70ca2 1704 /* v1 name types */
wolfSSL 0:1239e9b70ca2 1705 if (joint[0] == 0x55 && joint[1] == 0x04) {
wolfSSL 0:1239e9b70ca2 1706 byte id;
wolfSSL 0:1239e9b70ca2 1707 byte copy = FALSE;
wolfSSL 0:1239e9b70ca2 1708 int strLen;
wolfSSL 0:1239e9b70ca2 1709
wolfSSL 0:1239e9b70ca2 1710 cert->srcIdx += 2;
wolfSSL 0:1239e9b70ca2 1711 id = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 1712 b = cert->source[cert->srcIdx++]; /* strType */
wolfSSL 0:1239e9b70ca2 1713 (void)b; /* may want to validate? */
wolfSSL 0:1239e9b70ca2 1714
wolfSSL 0:1239e9b70ca2 1715 if (GetLength(cert->source, &cert->srcIdx, &strLen,
wolfSSL 0:1239e9b70ca2 1716 cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1717 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1718
wolfSSL 0:1239e9b70ca2 1719 if ( (strLen + 14) > (int)(ASN_NAME_MAX - idx)) {
wolfSSL 0:1239e9b70ca2 1720 /* include biggest pre fix header too 4 = "/serialNumber=" */
wolfSSL 0:1239e9b70ca2 1721 CYASSL_MSG("ASN Name too big, skipping");
wolfSSL 0:1239e9b70ca2 1722 tooBig = TRUE;
wolfSSL 0:1239e9b70ca2 1723 }
wolfSSL 0:1239e9b70ca2 1724
wolfSSL 0:1239e9b70ca2 1725 if (id == ASN_COMMON_NAME) {
wolfSSL 0:1239e9b70ca2 1726 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1727 cert->subjectCN = (char *)&cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1728 cert->subjectCNLen = strLen;
wolfSSL 0:1239e9b70ca2 1729 }
wolfSSL 0:1239e9b70ca2 1730
wolfSSL 0:1239e9b70ca2 1731 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1732 XMEMCPY(&full[idx], "/CN=", 4);
wolfSSL 0:1239e9b70ca2 1733 idx += 4;
wolfSSL 0:1239e9b70ca2 1734 copy = TRUE;
wolfSSL 0:1239e9b70ca2 1735 }
wolfSSL 0:1239e9b70ca2 1736 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1737 dName->cnIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1738 dName->cnLen = strLen;
wolfSSL 0:1239e9b70ca2 1739 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1740 }
wolfSSL 0:1239e9b70ca2 1741 else if (id == ASN_SUR_NAME) {
wolfSSL 0:1239e9b70ca2 1742 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1743 XMEMCPY(&full[idx], "/SN=", 4);
wolfSSL 0:1239e9b70ca2 1744 idx += 4;
wolfSSL 0:1239e9b70ca2 1745 copy = TRUE;
wolfSSL 0:1239e9b70ca2 1746 }
wolfSSL 0:1239e9b70ca2 1747 #ifdef CYASSL_CERT_GEN
wolfSSL 0:1239e9b70ca2 1748 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1749 cert->subjectSN = (char*)&cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1750 cert->subjectSNLen = strLen;
wolfSSL 0:1239e9b70ca2 1751 }
wolfSSL 0:1239e9b70ca2 1752 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 1753 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1754 dName->snIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1755 dName->snLen = strLen;
wolfSSL 0:1239e9b70ca2 1756 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1757 }
wolfSSL 0:1239e9b70ca2 1758 else if (id == ASN_COUNTRY_NAME) {
wolfSSL 0:1239e9b70ca2 1759 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1760 XMEMCPY(&full[idx], "/C=", 3);
wolfSSL 0:1239e9b70ca2 1761 idx += 3;
wolfSSL 0:1239e9b70ca2 1762 copy = TRUE;
wolfSSL 0:1239e9b70ca2 1763 }
wolfSSL 0:1239e9b70ca2 1764 #ifdef CYASSL_CERT_GEN
wolfSSL 0:1239e9b70ca2 1765 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1766 cert->subjectC = (char*)&cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1767 cert->subjectCLen = strLen;
wolfSSL 0:1239e9b70ca2 1768 }
wolfSSL 0:1239e9b70ca2 1769 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 1770 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1771 dName->cIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1772 dName->cLen = strLen;
wolfSSL 0:1239e9b70ca2 1773 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1774 }
wolfSSL 0:1239e9b70ca2 1775 else if (id == ASN_LOCALITY_NAME) {
wolfSSL 0:1239e9b70ca2 1776 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1777 XMEMCPY(&full[idx], "/L=", 3);
wolfSSL 0:1239e9b70ca2 1778 idx += 3;
wolfSSL 0:1239e9b70ca2 1779 copy = TRUE;
wolfSSL 0:1239e9b70ca2 1780 }
wolfSSL 0:1239e9b70ca2 1781 #ifdef CYASSL_CERT_GEN
wolfSSL 0:1239e9b70ca2 1782 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1783 cert->subjectL = (char*)&cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1784 cert->subjectLLen = strLen;
wolfSSL 0:1239e9b70ca2 1785 }
wolfSSL 0:1239e9b70ca2 1786 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 1787 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1788 dName->lIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1789 dName->lLen = strLen;
wolfSSL 0:1239e9b70ca2 1790 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1791 }
wolfSSL 0:1239e9b70ca2 1792 else if (id == ASN_STATE_NAME) {
wolfSSL 0:1239e9b70ca2 1793 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1794 XMEMCPY(&full[idx], "/ST=", 4);
wolfSSL 0:1239e9b70ca2 1795 idx += 4;
wolfSSL 0:1239e9b70ca2 1796 copy = TRUE;
wolfSSL 0:1239e9b70ca2 1797 }
wolfSSL 0:1239e9b70ca2 1798 #ifdef CYASSL_CERT_GEN
wolfSSL 0:1239e9b70ca2 1799 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1800 cert->subjectST = (char*)&cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1801 cert->subjectSTLen = strLen;
wolfSSL 0:1239e9b70ca2 1802 }
wolfSSL 0:1239e9b70ca2 1803 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 1804 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1805 dName->stIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1806 dName->stLen = strLen;
wolfSSL 0:1239e9b70ca2 1807 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1808 }
wolfSSL 0:1239e9b70ca2 1809 else if (id == ASN_ORG_NAME) {
wolfSSL 0:1239e9b70ca2 1810 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1811 XMEMCPY(&full[idx], "/O=", 3);
wolfSSL 0:1239e9b70ca2 1812 idx += 3;
wolfSSL 0:1239e9b70ca2 1813 copy = TRUE;
wolfSSL 0:1239e9b70ca2 1814 }
wolfSSL 0:1239e9b70ca2 1815 #ifdef CYASSL_CERT_GEN
wolfSSL 0:1239e9b70ca2 1816 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1817 cert->subjectO = (char*)&cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1818 cert->subjectOLen = strLen;
wolfSSL 0:1239e9b70ca2 1819 }
wolfSSL 0:1239e9b70ca2 1820 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 1821 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1822 dName->oIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1823 dName->oLen = strLen;
wolfSSL 0:1239e9b70ca2 1824 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1825 }
wolfSSL 0:1239e9b70ca2 1826 else if (id == ASN_ORGUNIT_NAME) {
wolfSSL 0:1239e9b70ca2 1827 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1828 XMEMCPY(&full[idx], "/OU=", 4);
wolfSSL 0:1239e9b70ca2 1829 idx += 4;
wolfSSL 0:1239e9b70ca2 1830 copy = TRUE;
wolfSSL 0:1239e9b70ca2 1831 }
wolfSSL 0:1239e9b70ca2 1832 #ifdef CYASSL_CERT_GEN
wolfSSL 0:1239e9b70ca2 1833 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1834 cert->subjectOU = (char*)&cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1835 cert->subjectOULen = strLen;
wolfSSL 0:1239e9b70ca2 1836 }
wolfSSL 0:1239e9b70ca2 1837 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 1838 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1839 dName->ouIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1840 dName->ouLen = strLen;
wolfSSL 0:1239e9b70ca2 1841 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1842 }
wolfSSL 0:1239e9b70ca2 1843 else if (id == ASN_SERIAL_NUMBER) {
wolfSSL 0:1239e9b70ca2 1844 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1845 XMEMCPY(&full[idx], "/serialNumber=", 14);
wolfSSL 0:1239e9b70ca2 1846 idx += 14;
wolfSSL 0:1239e9b70ca2 1847 copy = TRUE;
wolfSSL 0:1239e9b70ca2 1848 }
wolfSSL 0:1239e9b70ca2 1849 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1850 dName->snIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1851 dName->snLen = strLen;
wolfSSL 0:1239e9b70ca2 1852 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1853 }
wolfSSL 0:1239e9b70ca2 1854
wolfSSL 0:1239e9b70ca2 1855 if (copy && !tooBig) {
wolfSSL 0:1239e9b70ca2 1856 XMEMCPY(&full[idx], &cert->source[cert->srcIdx], strLen);
wolfSSL 0:1239e9b70ca2 1857 idx += strLen;
wolfSSL 0:1239e9b70ca2 1858 }
wolfSSL 0:1239e9b70ca2 1859
wolfSSL 0:1239e9b70ca2 1860 cert->srcIdx += strLen;
wolfSSL 0:1239e9b70ca2 1861 }
wolfSSL 0:1239e9b70ca2 1862 else {
wolfSSL 0:1239e9b70ca2 1863 /* skip */
wolfSSL 0:1239e9b70ca2 1864 byte email = FALSE;
wolfSSL 0:1239e9b70ca2 1865 byte uid = FALSE;
wolfSSL 0:1239e9b70ca2 1866 int adv;
wolfSSL 0:1239e9b70ca2 1867
wolfSSL 0:1239e9b70ca2 1868 if (joint[0] == 0x2a && joint[1] == 0x86) /* email id hdr */
wolfSSL 0:1239e9b70ca2 1869 email = TRUE;
wolfSSL 0:1239e9b70ca2 1870
wolfSSL 0:1239e9b70ca2 1871 if (joint[0] == 0x9 && joint[1] == 0x92) /* uid id hdr */
wolfSSL 0:1239e9b70ca2 1872 uid = TRUE;
wolfSSL 0:1239e9b70ca2 1873
wolfSSL 0:1239e9b70ca2 1874 cert->srcIdx += oidSz + 1;
wolfSSL 0:1239e9b70ca2 1875
wolfSSL 0:1239e9b70ca2 1876 if (GetLength(cert->source, &cert->srcIdx, &adv, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 1877 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 1878
wolfSSL 0:1239e9b70ca2 1879 if (adv > (int)(ASN_NAME_MAX - idx)) {
wolfSSL 0:1239e9b70ca2 1880 CYASSL_MSG("ASN name too big, skipping");
wolfSSL 0:1239e9b70ca2 1881 tooBig = TRUE;
wolfSSL 0:1239e9b70ca2 1882 }
wolfSSL 0:1239e9b70ca2 1883
wolfSSL 0:1239e9b70ca2 1884 if (email) {
wolfSSL 0:1239e9b70ca2 1885 if ( (14 + adv) > (int)(ASN_NAME_MAX - idx)) {
wolfSSL 0:1239e9b70ca2 1886 CYASSL_MSG("ASN name too big, skipping");
wolfSSL 0:1239e9b70ca2 1887 tooBig = TRUE;
wolfSSL 0:1239e9b70ca2 1888 }
wolfSSL 0:1239e9b70ca2 1889 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1890 XMEMCPY(&full[idx], "/emailAddress=", 14);
wolfSSL 0:1239e9b70ca2 1891 idx += 14;
wolfSSL 0:1239e9b70ca2 1892 }
wolfSSL 0:1239e9b70ca2 1893
wolfSSL 0:1239e9b70ca2 1894 #ifdef CYASSL_CERT_GEN
wolfSSL 0:1239e9b70ca2 1895 if (nameType == SUBJECT) {
wolfSSL 0:1239e9b70ca2 1896 cert->subjectEmail = (char*)&cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 1897 cert->subjectEmailLen = adv;
wolfSSL 0:1239e9b70ca2 1898 }
wolfSSL 0:1239e9b70ca2 1899 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 1900 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1901 dName->emailIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1902 dName->emailLen = adv;
wolfSSL 0:1239e9b70ca2 1903 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1904 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 1905 {
wolfSSL 0:1239e9b70ca2 1906 DNS_entry* emailName = NULL;
wolfSSL 0:1239e9b70ca2 1907
wolfSSL 0:1239e9b70ca2 1908 emailName = (DNS_entry*)XMALLOC(sizeof(DNS_entry),
wolfSSL 0:1239e9b70ca2 1909 cert->heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 1910 if (emailName == NULL) {
wolfSSL 0:1239e9b70ca2 1911 CYASSL_MSG("\tOut of Memory");
wolfSSL 0:1239e9b70ca2 1912 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 1913 }
wolfSSL 0:1239e9b70ca2 1914 emailName->name = (char*)XMALLOC(adv + 1,
wolfSSL 0:1239e9b70ca2 1915 cert->heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 1916 if (emailName->name == NULL) {
wolfSSL 0:1239e9b70ca2 1917 CYASSL_MSG("\tOut of Memory");
wolfSSL 0:1239e9b70ca2 1918 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 1919 }
wolfSSL 0:1239e9b70ca2 1920 XMEMCPY(emailName->name,
wolfSSL 0:1239e9b70ca2 1921 &cert->source[cert->srcIdx], adv);
wolfSSL 0:1239e9b70ca2 1922 emailName->name[adv] = 0;
wolfSSL 0:1239e9b70ca2 1923
wolfSSL 0:1239e9b70ca2 1924 emailName->next = cert->altEmailNames;
wolfSSL 0:1239e9b70ca2 1925 cert->altEmailNames = emailName;
wolfSSL 0:1239e9b70ca2 1926 }
wolfSSL 0:1239e9b70ca2 1927 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 1928 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1929 XMEMCPY(&full[idx], &cert->source[cert->srcIdx], adv);
wolfSSL 0:1239e9b70ca2 1930 idx += adv;
wolfSSL 0:1239e9b70ca2 1931 }
wolfSSL 0:1239e9b70ca2 1932 }
wolfSSL 0:1239e9b70ca2 1933
wolfSSL 0:1239e9b70ca2 1934 if (uid) {
wolfSSL 0:1239e9b70ca2 1935 if ( (5 + adv) > (int)(ASN_NAME_MAX - idx)) {
wolfSSL 0:1239e9b70ca2 1936 CYASSL_MSG("ASN name too big, skipping");
wolfSSL 0:1239e9b70ca2 1937 tooBig = TRUE;
wolfSSL 0:1239e9b70ca2 1938 }
wolfSSL 0:1239e9b70ca2 1939 if (!tooBig) {
wolfSSL 0:1239e9b70ca2 1940 XMEMCPY(&full[idx], "/UID=", 5);
wolfSSL 0:1239e9b70ca2 1941 idx += 5;
wolfSSL 0:1239e9b70ca2 1942
wolfSSL 0:1239e9b70ca2 1943 XMEMCPY(&full[idx], &cert->source[cert->srcIdx], adv);
wolfSSL 0:1239e9b70ca2 1944 idx += adv;
wolfSSL 0:1239e9b70ca2 1945 }
wolfSSL 0:1239e9b70ca2 1946 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1947 dName->uidIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 1948 dName->uidLen = adv;
wolfSSL 0:1239e9b70ca2 1949 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 1950 }
wolfSSL 0:1239e9b70ca2 1951
wolfSSL 0:1239e9b70ca2 1952 cert->srcIdx += adv;
wolfSSL 0:1239e9b70ca2 1953 }
wolfSSL 0:1239e9b70ca2 1954 }
wolfSSL 0:1239e9b70ca2 1955 full[idx++] = 0;
wolfSSL 0:1239e9b70ca2 1956
wolfSSL 0:1239e9b70ca2 1957 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 1958 {
wolfSSL 0:1239e9b70ca2 1959 int totalLen = 0;
wolfSSL 0:1239e9b70ca2 1960
wolfSSL 0:1239e9b70ca2 1961 if (dName->cnLen != 0)
wolfSSL 0:1239e9b70ca2 1962 totalLen += dName->cnLen + 4;
wolfSSL 0:1239e9b70ca2 1963 if (dName->snLen != 0)
wolfSSL 0:1239e9b70ca2 1964 totalLen += dName->snLen + 4;
wolfSSL 0:1239e9b70ca2 1965 if (dName->cLen != 0)
wolfSSL 0:1239e9b70ca2 1966 totalLen += dName->cLen + 3;
wolfSSL 0:1239e9b70ca2 1967 if (dName->lLen != 0)
wolfSSL 0:1239e9b70ca2 1968 totalLen += dName->lLen + 3;
wolfSSL 0:1239e9b70ca2 1969 if (dName->stLen != 0)
wolfSSL 0:1239e9b70ca2 1970 totalLen += dName->stLen + 4;
wolfSSL 0:1239e9b70ca2 1971 if (dName->oLen != 0)
wolfSSL 0:1239e9b70ca2 1972 totalLen += dName->oLen + 3;
wolfSSL 0:1239e9b70ca2 1973 if (dName->ouLen != 0)
wolfSSL 0:1239e9b70ca2 1974 totalLen += dName->ouLen + 4;
wolfSSL 0:1239e9b70ca2 1975 if (dName->emailLen != 0)
wolfSSL 0:1239e9b70ca2 1976 totalLen += dName->emailLen + 14;
wolfSSL 0:1239e9b70ca2 1977 if (dName->uidLen != 0)
wolfSSL 0:1239e9b70ca2 1978 totalLen += dName->uidLen + 5;
wolfSSL 0:1239e9b70ca2 1979 if (dName->serialLen != 0)
wolfSSL 0:1239e9b70ca2 1980 totalLen += dName->serialLen + 14;
wolfSSL 0:1239e9b70ca2 1981
wolfSSL 0:1239e9b70ca2 1982 dName->fullName = (char*)XMALLOC(totalLen + 1, NULL, DYNAMIC_TYPE_X509);
wolfSSL 0:1239e9b70ca2 1983 if (dName->fullName != NULL) {
wolfSSL 0:1239e9b70ca2 1984 idx = 0;
wolfSSL 0:1239e9b70ca2 1985
wolfSSL 0:1239e9b70ca2 1986 if (dName->cnLen != 0) {
wolfSSL 0:1239e9b70ca2 1987 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 1988 XMEMCPY(&dName->fullName[idx], "/CN=", 4);
wolfSSL 0:1239e9b70ca2 1989 idx += 4;
wolfSSL 0:1239e9b70ca2 1990 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 1991 &cert->source[dName->cnIdx], dName->cnLen);
wolfSSL 0:1239e9b70ca2 1992 dName->cnIdx = idx;
wolfSSL 0:1239e9b70ca2 1993 idx += dName->cnLen;
wolfSSL 0:1239e9b70ca2 1994 }
wolfSSL 0:1239e9b70ca2 1995 if (dName->snLen != 0) {
wolfSSL 0:1239e9b70ca2 1996 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 1997 XMEMCPY(&dName->fullName[idx], "/SN=", 4);
wolfSSL 0:1239e9b70ca2 1998 idx += 4;
wolfSSL 0:1239e9b70ca2 1999 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2000 &cert->source[dName->snIdx], dName->snLen);
wolfSSL 0:1239e9b70ca2 2001 dName->snIdx = idx;
wolfSSL 0:1239e9b70ca2 2002 idx += dName->snLen;
wolfSSL 0:1239e9b70ca2 2003 }
wolfSSL 0:1239e9b70ca2 2004 if (dName->cLen != 0) {
wolfSSL 0:1239e9b70ca2 2005 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 2006 XMEMCPY(&dName->fullName[idx], "/C=", 3);
wolfSSL 0:1239e9b70ca2 2007 idx += 3;
wolfSSL 0:1239e9b70ca2 2008 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2009 &cert->source[dName->cIdx], dName->cLen);
wolfSSL 0:1239e9b70ca2 2010 dName->cIdx = idx;
wolfSSL 0:1239e9b70ca2 2011 idx += dName->cLen;
wolfSSL 0:1239e9b70ca2 2012 }
wolfSSL 0:1239e9b70ca2 2013 if (dName->lLen != 0) {
wolfSSL 0:1239e9b70ca2 2014 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 2015 XMEMCPY(&dName->fullName[idx], "/L=", 3);
wolfSSL 0:1239e9b70ca2 2016 idx += 3;
wolfSSL 0:1239e9b70ca2 2017 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2018 &cert->source[dName->lIdx], dName->lLen);
wolfSSL 0:1239e9b70ca2 2019 dName->lIdx = idx;
wolfSSL 0:1239e9b70ca2 2020 idx += dName->lLen;
wolfSSL 0:1239e9b70ca2 2021 }
wolfSSL 0:1239e9b70ca2 2022 if (dName->stLen != 0) {
wolfSSL 0:1239e9b70ca2 2023 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 2024 XMEMCPY(&dName->fullName[idx], "/ST=", 4);
wolfSSL 0:1239e9b70ca2 2025 idx += 4;
wolfSSL 0:1239e9b70ca2 2026 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2027 &cert->source[dName->stIdx], dName->stLen);
wolfSSL 0:1239e9b70ca2 2028 dName->stIdx = idx;
wolfSSL 0:1239e9b70ca2 2029 idx += dName->stLen;
wolfSSL 0:1239e9b70ca2 2030 }
wolfSSL 0:1239e9b70ca2 2031 if (dName->oLen != 0) {
wolfSSL 0:1239e9b70ca2 2032 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 2033 XMEMCPY(&dName->fullName[idx], "/O=", 3);
wolfSSL 0:1239e9b70ca2 2034 idx += 3;
wolfSSL 0:1239e9b70ca2 2035 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2036 &cert->source[dName->oIdx], dName->oLen);
wolfSSL 0:1239e9b70ca2 2037 dName->oIdx = idx;
wolfSSL 0:1239e9b70ca2 2038 idx += dName->oLen;
wolfSSL 0:1239e9b70ca2 2039 }
wolfSSL 0:1239e9b70ca2 2040 if (dName->ouLen != 0) {
wolfSSL 0:1239e9b70ca2 2041 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 2042 XMEMCPY(&dName->fullName[idx], "/OU=", 4);
wolfSSL 0:1239e9b70ca2 2043 idx += 4;
wolfSSL 0:1239e9b70ca2 2044 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2045 &cert->source[dName->ouIdx], dName->ouLen);
wolfSSL 0:1239e9b70ca2 2046 dName->ouIdx = idx;
wolfSSL 0:1239e9b70ca2 2047 idx += dName->ouLen;
wolfSSL 0:1239e9b70ca2 2048 }
wolfSSL 0:1239e9b70ca2 2049 if (dName->emailLen != 0) {
wolfSSL 0:1239e9b70ca2 2050 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 2051 XMEMCPY(&dName->fullName[idx], "/emailAddress=", 14);
wolfSSL 0:1239e9b70ca2 2052 idx += 14;
wolfSSL 0:1239e9b70ca2 2053 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2054 &cert->source[dName->emailIdx], dName->emailLen);
wolfSSL 0:1239e9b70ca2 2055 dName->emailIdx = idx;
wolfSSL 0:1239e9b70ca2 2056 idx += dName->emailLen;
wolfSSL 0:1239e9b70ca2 2057 }
wolfSSL 0:1239e9b70ca2 2058 if (dName->uidLen != 0) {
wolfSSL 0:1239e9b70ca2 2059 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 2060 XMEMCPY(&dName->fullName[idx], "/UID=", 5);
wolfSSL 0:1239e9b70ca2 2061 idx += 5;
wolfSSL 0:1239e9b70ca2 2062 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2063 &cert->source[dName->uidIdx], dName->uidLen);
wolfSSL 0:1239e9b70ca2 2064 dName->uidIdx = idx;
wolfSSL 0:1239e9b70ca2 2065 idx += dName->uidLen;
wolfSSL 0:1239e9b70ca2 2066 }
wolfSSL 0:1239e9b70ca2 2067 if (dName->serialLen != 0) {
wolfSSL 0:1239e9b70ca2 2068 dName->entryCount++;
wolfSSL 0:1239e9b70ca2 2069 XMEMCPY(&dName->fullName[idx], "/serialNumber=", 14);
wolfSSL 0:1239e9b70ca2 2070 idx += 14;
wolfSSL 0:1239e9b70ca2 2071 XMEMCPY(&dName->fullName[idx],
wolfSSL 0:1239e9b70ca2 2072 &cert->source[dName->serialIdx], dName->serialLen);
wolfSSL 0:1239e9b70ca2 2073 dName->serialIdx = idx;
wolfSSL 0:1239e9b70ca2 2074 idx += dName->serialLen;
wolfSSL 0:1239e9b70ca2 2075 }
wolfSSL 0:1239e9b70ca2 2076 dName->fullName[idx] = '\0';
wolfSSL 0:1239e9b70ca2 2077 dName->fullNameLen = totalLen;
wolfSSL 0:1239e9b70ca2 2078 }
wolfSSL 0:1239e9b70ca2 2079 }
wolfSSL 0:1239e9b70ca2 2080 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 2081
wolfSSL 0:1239e9b70ca2 2082 return 0;
wolfSSL 0:1239e9b70ca2 2083 }
wolfSSL 0:1239e9b70ca2 2084
wolfSSL 0:1239e9b70ca2 2085
wolfSSL 0:1239e9b70ca2 2086 #ifndef NO_TIME_H
wolfSSL 0:1239e9b70ca2 2087
wolfSSL 0:1239e9b70ca2 2088 /* to the second */
wolfSSL 0:1239e9b70ca2 2089 static int DateGreaterThan(const struct tm* a, const struct tm* b)
wolfSSL 0:1239e9b70ca2 2090 {
wolfSSL 0:1239e9b70ca2 2091 if (a->tm_year > b->tm_year)
wolfSSL 0:1239e9b70ca2 2092 return 1;
wolfSSL 0:1239e9b70ca2 2093
wolfSSL 0:1239e9b70ca2 2094 if (a->tm_year == b->tm_year && a->tm_mon > b->tm_mon)
wolfSSL 0:1239e9b70ca2 2095 return 1;
wolfSSL 0:1239e9b70ca2 2096
wolfSSL 0:1239e9b70ca2 2097 if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
wolfSSL 0:1239e9b70ca2 2098 a->tm_mday > b->tm_mday)
wolfSSL 0:1239e9b70ca2 2099 return 1;
wolfSSL 0:1239e9b70ca2 2100
wolfSSL 0:1239e9b70ca2 2101 if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
wolfSSL 0:1239e9b70ca2 2102 a->tm_mday == b->tm_mday && a->tm_hour > b->tm_hour)
wolfSSL 0:1239e9b70ca2 2103 return 1;
wolfSSL 0:1239e9b70ca2 2104
wolfSSL 0:1239e9b70ca2 2105 if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
wolfSSL 0:1239e9b70ca2 2106 a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour &&
wolfSSL 0:1239e9b70ca2 2107 a->tm_min > b->tm_min)
wolfSSL 0:1239e9b70ca2 2108 return 1;
wolfSSL 0:1239e9b70ca2 2109
wolfSSL 0:1239e9b70ca2 2110 if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
wolfSSL 0:1239e9b70ca2 2111 a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour &&
wolfSSL 0:1239e9b70ca2 2112 a->tm_min == b->tm_min && a->tm_sec > b->tm_sec)
wolfSSL 0:1239e9b70ca2 2113 return 1;
wolfSSL 0:1239e9b70ca2 2114
wolfSSL 0:1239e9b70ca2 2115 return 0; /* false */
wolfSSL 0:1239e9b70ca2 2116 }
wolfSSL 0:1239e9b70ca2 2117
wolfSSL 0:1239e9b70ca2 2118
wolfSSL 0:1239e9b70ca2 2119 static INLINE int DateLessThan(const struct tm* a, const struct tm* b)
wolfSSL 0:1239e9b70ca2 2120 {
wolfSSL 0:1239e9b70ca2 2121 return !DateGreaterThan(a,b);
wolfSSL 0:1239e9b70ca2 2122 }
wolfSSL 0:1239e9b70ca2 2123
wolfSSL 0:1239e9b70ca2 2124
wolfSSL 0:1239e9b70ca2 2125 /* like atoi but only use first byte */
wolfSSL 0:1239e9b70ca2 2126 /* Make sure before and after dates are valid */
wolfSSL 0:1239e9b70ca2 2127 int ValidateDate(const byte* date, byte format, int dateType)
wolfSSL 0:1239e9b70ca2 2128 {
wolfSSL 0:1239e9b70ca2 2129 time_t ltime;
wolfSSL 0:1239e9b70ca2 2130 struct tm certTime;
wolfSSL 0:1239e9b70ca2 2131 struct tm* localTime;
wolfSSL 0:1239e9b70ca2 2132 int i = 0;
wolfSSL 0:1239e9b70ca2 2133
wolfSSL 0:1239e9b70ca2 2134 ltime = XTIME(0);
wolfSSL 0:1239e9b70ca2 2135 XMEMSET(&certTime, 0, sizeof(certTime));
wolfSSL 0:1239e9b70ca2 2136
wolfSSL 0:1239e9b70ca2 2137 if (format == ASN_UTC_TIME) {
wolfSSL 0:1239e9b70ca2 2138 if (btoi(date[0]) >= 5)
wolfSSL 0:1239e9b70ca2 2139 certTime.tm_year = 1900;
wolfSSL 0:1239e9b70ca2 2140 else
wolfSSL 0:1239e9b70ca2 2141 certTime.tm_year = 2000;
wolfSSL 0:1239e9b70ca2 2142 }
wolfSSL 0:1239e9b70ca2 2143 else { /* format == GENERALIZED_TIME */
wolfSSL 0:1239e9b70ca2 2144 certTime.tm_year += btoi(date[i++]) * 1000;
wolfSSL 0:1239e9b70ca2 2145 certTime.tm_year += btoi(date[i++]) * 100;
wolfSSL 0:1239e9b70ca2 2146 }
wolfSSL 0:1239e9b70ca2 2147
wolfSSL 0:1239e9b70ca2 2148 GetTime(&certTime.tm_year, date, &i); certTime.tm_year -= 1900; /* adjust */
wolfSSL 0:1239e9b70ca2 2149 GetTime(&certTime.tm_mon, date, &i); certTime.tm_mon -= 1; /* adjust */
wolfSSL 0:1239e9b70ca2 2150 GetTime(&certTime.tm_mday, date, &i);
wolfSSL 0:1239e9b70ca2 2151 GetTime(&certTime.tm_hour, date, &i);
wolfSSL 0:1239e9b70ca2 2152 GetTime(&certTime.tm_min, date, &i);
wolfSSL 0:1239e9b70ca2 2153 GetTime(&certTime.tm_sec, date, &i);
wolfSSL 0:1239e9b70ca2 2154
wolfSSL 0:1239e9b70ca2 2155 if (date[i] != 'Z') { /* only Zulu supported for this profile */
wolfSSL 0:1239e9b70ca2 2156 CYASSL_MSG("Only Zulu time supported for this profile");
wolfSSL 0:1239e9b70ca2 2157 return 0;
wolfSSL 0:1239e9b70ca2 2158 }
wolfSSL 0:1239e9b70ca2 2159
wolfSSL 0:1239e9b70ca2 2160 localTime = XGMTIME(&ltime);
wolfSSL 0:1239e9b70ca2 2161
wolfSSL 0:1239e9b70ca2 2162 if (dateType == BEFORE) {
wolfSSL 0:1239e9b70ca2 2163 if (DateLessThan(localTime, &certTime))
wolfSSL 0:1239e9b70ca2 2164 return 0;
wolfSSL 0:1239e9b70ca2 2165 }
wolfSSL 0:1239e9b70ca2 2166 else
wolfSSL 0:1239e9b70ca2 2167 if (DateGreaterThan(localTime, &certTime))
wolfSSL 0:1239e9b70ca2 2168 return 0;
wolfSSL 0:1239e9b70ca2 2169
wolfSSL 0:1239e9b70ca2 2170 return 1;
wolfSSL 0:1239e9b70ca2 2171 }
wolfSSL 0:1239e9b70ca2 2172
wolfSSL 0:1239e9b70ca2 2173 #endif /* NO_TIME_H */
wolfSSL 0:1239e9b70ca2 2174
wolfSSL 0:1239e9b70ca2 2175
wolfSSL 0:1239e9b70ca2 2176 static int GetDate(DecodedCert* cert, int dateType)
wolfSSL 0:1239e9b70ca2 2177 {
wolfSSL 0:1239e9b70ca2 2178 int length;
wolfSSL 0:1239e9b70ca2 2179 byte date[MAX_DATE_SIZE];
wolfSSL 0:1239e9b70ca2 2180 byte b;
wolfSSL 0:1239e9b70ca2 2181 word32 startIdx = 0;
wolfSSL 0:1239e9b70ca2 2182
wolfSSL 0:1239e9b70ca2 2183 if (dateType == BEFORE)
wolfSSL 0:1239e9b70ca2 2184 cert->beforeDate = &cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 2185 else
wolfSSL 0:1239e9b70ca2 2186 cert->afterDate = &cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 2187 startIdx = cert->srcIdx;
wolfSSL 0:1239e9b70ca2 2188
wolfSSL 0:1239e9b70ca2 2189 b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 2190 if (b != ASN_UTC_TIME && b != ASN_GENERALIZED_TIME)
wolfSSL 0:1239e9b70ca2 2191 return ASN_TIME_E;
wolfSSL 0:1239e9b70ca2 2192
wolfSSL 0:1239e9b70ca2 2193 if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 2194 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 2195
wolfSSL 0:1239e9b70ca2 2196 if (length > MAX_DATE_SIZE || length < MIN_DATE_SIZE)
wolfSSL 0:1239e9b70ca2 2197 return ASN_DATE_SZ_E;
wolfSSL 0:1239e9b70ca2 2198
wolfSSL 0:1239e9b70ca2 2199 XMEMCPY(date, &cert->source[cert->srcIdx], length);
wolfSSL 0:1239e9b70ca2 2200 cert->srcIdx += length;
wolfSSL 0:1239e9b70ca2 2201
wolfSSL 0:1239e9b70ca2 2202 if (dateType == BEFORE)
wolfSSL 0:1239e9b70ca2 2203 cert->beforeDateLen = cert->srcIdx - startIdx;
wolfSSL 0:1239e9b70ca2 2204 else
wolfSSL 0:1239e9b70ca2 2205 cert->afterDateLen = cert->srcIdx - startIdx;
wolfSSL 0:1239e9b70ca2 2206
wolfSSL 0:1239e9b70ca2 2207 if (!XVALIDATE_DATE(date, b, dateType)) {
wolfSSL 0:1239e9b70ca2 2208 if (dateType == BEFORE)
wolfSSL 0:1239e9b70ca2 2209 return ASN_BEFORE_DATE_E;
wolfSSL 0:1239e9b70ca2 2210 else
wolfSSL 0:1239e9b70ca2 2211 return ASN_AFTER_DATE_E;
wolfSSL 0:1239e9b70ca2 2212 }
wolfSSL 0:1239e9b70ca2 2213
wolfSSL 0:1239e9b70ca2 2214 return 0;
wolfSSL 0:1239e9b70ca2 2215 }
wolfSSL 0:1239e9b70ca2 2216
wolfSSL 0:1239e9b70ca2 2217
wolfSSL 0:1239e9b70ca2 2218 static int GetValidity(DecodedCert* cert, int verify)
wolfSSL 0:1239e9b70ca2 2219 {
wolfSSL 0:1239e9b70ca2 2220 int length;
wolfSSL 0:1239e9b70ca2 2221 int badDate = 0;
wolfSSL 0:1239e9b70ca2 2222
wolfSSL 0:1239e9b70ca2 2223 if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 2224 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 2225
wolfSSL 0:1239e9b70ca2 2226 if (GetDate(cert, BEFORE) < 0 && verify)
wolfSSL 0:1239e9b70ca2 2227 badDate = ASN_BEFORE_DATE_E; /* continue parsing */
wolfSSL 0:1239e9b70ca2 2228
wolfSSL 0:1239e9b70ca2 2229 if (GetDate(cert, AFTER) < 0 && verify)
wolfSSL 0:1239e9b70ca2 2230 return ASN_AFTER_DATE_E;
wolfSSL 0:1239e9b70ca2 2231
wolfSSL 0:1239e9b70ca2 2232 if (badDate != 0)
wolfSSL 0:1239e9b70ca2 2233 return badDate;
wolfSSL 0:1239e9b70ca2 2234
wolfSSL 0:1239e9b70ca2 2235 return 0;
wolfSSL 0:1239e9b70ca2 2236 }
wolfSSL 0:1239e9b70ca2 2237
wolfSSL 0:1239e9b70ca2 2238
wolfSSL 0:1239e9b70ca2 2239 int DecodeToKey(DecodedCert* cert, int verify)
wolfSSL 0:1239e9b70ca2 2240 {
wolfSSL 0:1239e9b70ca2 2241 int badDate = 0;
wolfSSL 0:1239e9b70ca2 2242 int ret;
wolfSSL 0:1239e9b70ca2 2243
wolfSSL 0:1239e9b70ca2 2244 if ( (ret = GetCertHeader(cert)) < 0)
wolfSSL 0:1239e9b70ca2 2245 return ret;
wolfSSL 0:1239e9b70ca2 2246
wolfSSL 0:1239e9b70ca2 2247 CYASSL_MSG("Got Cert Header");
wolfSSL 0:1239e9b70ca2 2248
wolfSSL 0:1239e9b70ca2 2249 if ( (ret = GetAlgoId(cert->source, &cert->srcIdx, &cert->signatureOID,
wolfSSL 0:1239e9b70ca2 2250 cert->maxIdx)) < 0)
wolfSSL 0:1239e9b70ca2 2251 return ret;
wolfSSL 0:1239e9b70ca2 2252
wolfSSL 0:1239e9b70ca2 2253 CYASSL_MSG("Got Algo ID");
wolfSSL 0:1239e9b70ca2 2254
wolfSSL 0:1239e9b70ca2 2255 if ( (ret = GetName(cert, ISSUER)) < 0)
wolfSSL 0:1239e9b70ca2 2256 return ret;
wolfSSL 0:1239e9b70ca2 2257
wolfSSL 0:1239e9b70ca2 2258 if ( (ret = GetValidity(cert, verify)) < 0)
wolfSSL 0:1239e9b70ca2 2259 badDate = ret;
wolfSSL 0:1239e9b70ca2 2260
wolfSSL 0:1239e9b70ca2 2261 if ( (ret = GetName(cert, SUBJECT)) < 0)
wolfSSL 0:1239e9b70ca2 2262 return ret;
wolfSSL 0:1239e9b70ca2 2263
wolfSSL 0:1239e9b70ca2 2264 CYASSL_MSG("Got Subject Name");
wolfSSL 0:1239e9b70ca2 2265
wolfSSL 0:1239e9b70ca2 2266 if ( (ret = GetKey(cert)) < 0)
wolfSSL 0:1239e9b70ca2 2267 return ret;
wolfSSL 0:1239e9b70ca2 2268
wolfSSL 0:1239e9b70ca2 2269 CYASSL_MSG("Got Key");
wolfSSL 0:1239e9b70ca2 2270
wolfSSL 0:1239e9b70ca2 2271 if (badDate != 0)
wolfSSL 0:1239e9b70ca2 2272 return badDate;
wolfSSL 0:1239e9b70ca2 2273
wolfSSL 0:1239e9b70ca2 2274 return ret;
wolfSSL 0:1239e9b70ca2 2275 }
wolfSSL 0:1239e9b70ca2 2276
wolfSSL 0:1239e9b70ca2 2277
wolfSSL 0:1239e9b70ca2 2278 static int GetSignature(DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 2279 {
wolfSSL 0:1239e9b70ca2 2280 int length;
wolfSSL 0:1239e9b70ca2 2281 byte b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 2282
wolfSSL 0:1239e9b70ca2 2283 if (b != ASN_BIT_STRING)
wolfSSL 0:1239e9b70ca2 2284 return ASN_BITSTR_E;
wolfSSL 0:1239e9b70ca2 2285
wolfSSL 0:1239e9b70ca2 2286 if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 2287 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 2288
wolfSSL 0:1239e9b70ca2 2289 cert->sigLength = length;
wolfSSL 0:1239e9b70ca2 2290
wolfSSL 0:1239e9b70ca2 2291 b = cert->source[cert->srcIdx++];
wolfSSL 0:1239e9b70ca2 2292 if (b != 0x00)
wolfSSL 0:1239e9b70ca2 2293 return ASN_EXPECT_0_E;
wolfSSL 0:1239e9b70ca2 2294
wolfSSL 0:1239e9b70ca2 2295 cert->sigLength--;
wolfSSL 0:1239e9b70ca2 2296 cert->signature = &cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 2297 cert->srcIdx += cert->sigLength;
wolfSSL 0:1239e9b70ca2 2298
wolfSSL 0:1239e9b70ca2 2299 return 0;
wolfSSL 0:1239e9b70ca2 2300 }
wolfSSL 0:1239e9b70ca2 2301
wolfSSL 0:1239e9b70ca2 2302
wolfSSL 0:1239e9b70ca2 2303 static word32 SetDigest(const byte* digest, word32 digSz, byte* output)
wolfSSL 0:1239e9b70ca2 2304 {
wolfSSL 0:1239e9b70ca2 2305 output[0] = ASN_OCTET_STRING;
wolfSSL 0:1239e9b70ca2 2306 output[1] = (byte)digSz;
wolfSSL 0:1239e9b70ca2 2307 XMEMCPY(&output[2], digest, digSz);
wolfSSL 0:1239e9b70ca2 2308
wolfSSL 0:1239e9b70ca2 2309 return digSz + 2;
wolfSSL 0:1239e9b70ca2 2310 }
wolfSSL 0:1239e9b70ca2 2311
wolfSSL 0:1239e9b70ca2 2312
wolfSSL 0:1239e9b70ca2 2313 static word32 BytePrecision(word32 value)
wolfSSL 0:1239e9b70ca2 2314 {
wolfSSL 0:1239e9b70ca2 2315 word32 i;
wolfSSL 0:1239e9b70ca2 2316 for (i = sizeof(value); i; --i)
wolfSSL 0:1239e9b70ca2 2317 if (value >> ((i - 1) * CYASSL_BIT_SIZE))
wolfSSL 0:1239e9b70ca2 2318 break;
wolfSSL 0:1239e9b70ca2 2319
wolfSSL 0:1239e9b70ca2 2320 return i;
wolfSSL 0:1239e9b70ca2 2321 }
wolfSSL 0:1239e9b70ca2 2322
wolfSSL 0:1239e9b70ca2 2323
wolfSSL 0:1239e9b70ca2 2324 CYASSL_LOCAL word32 SetLength(word32 length, byte* output)
wolfSSL 0:1239e9b70ca2 2325 {
wolfSSL 0:1239e9b70ca2 2326 word32 i = 0, j;
wolfSSL 0:1239e9b70ca2 2327
wolfSSL 0:1239e9b70ca2 2328 if (length < ASN_LONG_LENGTH)
wolfSSL 0:1239e9b70ca2 2329 output[i++] = (byte)length;
wolfSSL 0:1239e9b70ca2 2330 else {
wolfSSL 0:1239e9b70ca2 2331 output[i++] = (byte)(BytePrecision(length) | ASN_LONG_LENGTH);
wolfSSL 0:1239e9b70ca2 2332
wolfSSL 0:1239e9b70ca2 2333 for (j = BytePrecision(length); j; --j) {
wolfSSL 0:1239e9b70ca2 2334 output[i] = (byte)(length >> ((j - 1) * CYASSL_BIT_SIZE));
wolfSSL 0:1239e9b70ca2 2335 i++;
wolfSSL 0:1239e9b70ca2 2336 }
wolfSSL 0:1239e9b70ca2 2337 }
wolfSSL 0:1239e9b70ca2 2338
wolfSSL 0:1239e9b70ca2 2339 return i;
wolfSSL 0:1239e9b70ca2 2340 }
wolfSSL 0:1239e9b70ca2 2341
wolfSSL 0:1239e9b70ca2 2342
wolfSSL 0:1239e9b70ca2 2343 CYASSL_LOCAL word32 SetSequence(word32 len, byte* output)
wolfSSL 0:1239e9b70ca2 2344 {
wolfSSL 0:1239e9b70ca2 2345 output[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
wolfSSL 0:1239e9b70ca2 2346 return SetLength(len, output + 1) + 1;
wolfSSL 0:1239e9b70ca2 2347 }
wolfSSL 0:1239e9b70ca2 2348
wolfSSL 0:1239e9b70ca2 2349 CYASSL_LOCAL word32 SetOctetString(word32 len, byte* output)
wolfSSL 0:1239e9b70ca2 2350 {
wolfSSL 0:1239e9b70ca2 2351 output[0] = ASN_OCTET_STRING;
wolfSSL 0:1239e9b70ca2 2352 return SetLength(len, output + 1) + 1;
wolfSSL 0:1239e9b70ca2 2353 }
wolfSSL 0:1239e9b70ca2 2354
wolfSSL 0:1239e9b70ca2 2355 /* Write a set header to output */
wolfSSL 0:1239e9b70ca2 2356 CYASSL_LOCAL word32 SetSet(word32 len, byte* output)
wolfSSL 0:1239e9b70ca2 2357 {
wolfSSL 0:1239e9b70ca2 2358 output[0] = ASN_SET | ASN_CONSTRUCTED;
wolfSSL 0:1239e9b70ca2 2359 return SetLength(len, output + 1) + 1;
wolfSSL 0:1239e9b70ca2 2360 }
wolfSSL 0:1239e9b70ca2 2361
wolfSSL 0:1239e9b70ca2 2362 CYASSL_LOCAL word32 SetImplicit(byte tag, byte number, word32 len, byte* output)
wolfSSL 0:1239e9b70ca2 2363 {
wolfSSL 0:1239e9b70ca2 2364
wolfSSL 0:1239e9b70ca2 2365 output[0] = ((tag == ASN_SEQUENCE || tag == ASN_SET) ? ASN_CONSTRUCTED : 0)
wolfSSL 0:1239e9b70ca2 2366 | ASN_CONTEXT_SPECIFIC | number;
wolfSSL 0:1239e9b70ca2 2367 return SetLength(len, output + 1) + 1;
wolfSSL 0:1239e9b70ca2 2368 }
wolfSSL 0:1239e9b70ca2 2369
wolfSSL 0:1239e9b70ca2 2370 CYASSL_LOCAL word32 SetExplicit(byte number, word32 len, byte* output)
wolfSSL 0:1239e9b70ca2 2371 {
wolfSSL 0:1239e9b70ca2 2372 output[0] = ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | number;
wolfSSL 0:1239e9b70ca2 2373 return SetLength(len, output + 1) + 1;
wolfSSL 0:1239e9b70ca2 2374 }
wolfSSL 0:1239e9b70ca2 2375
wolfSSL 0:1239e9b70ca2 2376
wolfSSL 0:1239e9b70ca2 2377 #if defined(HAVE_ECC) && defined(CYASSL_CERT_GEN)
wolfSSL 0:1239e9b70ca2 2378
wolfSSL 0:1239e9b70ca2 2379 static word32 SetCurve(ecc_key* key, byte* output)
wolfSSL 0:1239e9b70ca2 2380 {
wolfSSL 0:1239e9b70ca2 2381
wolfSSL 0:1239e9b70ca2 2382 /* curve types */
wolfSSL 0:1239e9b70ca2 2383 static const byte ECC_192v1_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE, 0x3d,
wolfSSL 0:1239e9b70ca2 2384 0x03, 0x01, 0x01};
wolfSSL 0:1239e9b70ca2 2385 static const byte ECC_256v1_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE, 0x3d,
wolfSSL 0:1239e9b70ca2 2386 0x03, 0x01, 0x07};
wolfSSL 0:1239e9b70ca2 2387 static const byte ECC_160r1_AlgoID[] = { 0x2b, 0x81, 0x04, 0x00,
wolfSSL 0:1239e9b70ca2 2388 0x02};
wolfSSL 0:1239e9b70ca2 2389 static const byte ECC_224r1_AlgoID[] = { 0x2b, 0x81, 0x04, 0x00,
wolfSSL 0:1239e9b70ca2 2390 0x21};
wolfSSL 0:1239e9b70ca2 2391 static const byte ECC_384r1_AlgoID[] = { 0x2b, 0x81, 0x04, 0x00,
wolfSSL 0:1239e9b70ca2 2392 0x22};
wolfSSL 0:1239e9b70ca2 2393 static const byte ECC_521r1_AlgoID[] = { 0x2b, 0x81, 0x04, 0x00,
wolfSSL 0:1239e9b70ca2 2394 0x23};
wolfSSL 0:1239e9b70ca2 2395
wolfSSL 0:1239e9b70ca2 2396 int oidSz = 0;
wolfSSL 0:1239e9b70ca2 2397 int idx = 0;
wolfSSL 0:1239e9b70ca2 2398 int lenSz = 0;
wolfSSL 0:1239e9b70ca2 2399 const byte* oid = 0;
wolfSSL 0:1239e9b70ca2 2400
wolfSSL 0:1239e9b70ca2 2401 output[0] = ASN_OBJECT_ID;
wolfSSL 0:1239e9b70ca2 2402 idx++;
wolfSSL 0:1239e9b70ca2 2403
wolfSSL 0:1239e9b70ca2 2404 switch (key->dp->size) {
wolfSSL 0:1239e9b70ca2 2405 case 20:
wolfSSL 0:1239e9b70ca2 2406 oidSz = sizeof(ECC_160r1_AlgoID);
wolfSSL 0:1239e9b70ca2 2407 oid = ECC_160r1_AlgoID;
wolfSSL 0:1239e9b70ca2 2408 break;
wolfSSL 0:1239e9b70ca2 2409
wolfSSL 0:1239e9b70ca2 2410 case 24:
wolfSSL 0:1239e9b70ca2 2411 oidSz = sizeof(ECC_192v1_AlgoID);
wolfSSL 0:1239e9b70ca2 2412 oid = ECC_192v1_AlgoID;
wolfSSL 0:1239e9b70ca2 2413 break;
wolfSSL 0:1239e9b70ca2 2414
wolfSSL 0:1239e9b70ca2 2415 case 28:
wolfSSL 0:1239e9b70ca2 2416 oidSz = sizeof(ECC_224r1_AlgoID);
wolfSSL 0:1239e9b70ca2 2417 oid = ECC_224r1_AlgoID;
wolfSSL 0:1239e9b70ca2 2418 break;
wolfSSL 0:1239e9b70ca2 2419
wolfSSL 0:1239e9b70ca2 2420 case 32:
wolfSSL 0:1239e9b70ca2 2421 oidSz = sizeof(ECC_256v1_AlgoID);
wolfSSL 0:1239e9b70ca2 2422 oid = ECC_256v1_AlgoID;
wolfSSL 0:1239e9b70ca2 2423 break;
wolfSSL 0:1239e9b70ca2 2424
wolfSSL 0:1239e9b70ca2 2425 case 48:
wolfSSL 0:1239e9b70ca2 2426 oidSz = sizeof(ECC_384r1_AlgoID);
wolfSSL 0:1239e9b70ca2 2427 oid = ECC_384r1_AlgoID;
wolfSSL 0:1239e9b70ca2 2428 break;
wolfSSL 0:1239e9b70ca2 2429
wolfSSL 0:1239e9b70ca2 2430 case 66:
wolfSSL 0:1239e9b70ca2 2431 oidSz = sizeof(ECC_521r1_AlgoID);
wolfSSL 0:1239e9b70ca2 2432 oid = ECC_521r1_AlgoID;
wolfSSL 0:1239e9b70ca2 2433 break;
wolfSSL 0:1239e9b70ca2 2434
wolfSSL 0:1239e9b70ca2 2435 default:
wolfSSL 0:1239e9b70ca2 2436 return ASN_UNKNOWN_OID_E;
wolfSSL 0:1239e9b70ca2 2437 }
wolfSSL 0:1239e9b70ca2 2438 lenSz = SetLength(oidSz, output+idx);
wolfSSL 0:1239e9b70ca2 2439 idx += lenSz;
wolfSSL 0:1239e9b70ca2 2440
wolfSSL 0:1239e9b70ca2 2441 XMEMCPY(output+idx, oid, oidSz);
wolfSSL 0:1239e9b70ca2 2442 idx += oidSz;
wolfSSL 0:1239e9b70ca2 2443
wolfSSL 0:1239e9b70ca2 2444 return idx;
wolfSSL 0:1239e9b70ca2 2445 }
wolfSSL 0:1239e9b70ca2 2446
wolfSSL 0:1239e9b70ca2 2447 #endif /* HAVE_ECC && CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 2448
wolfSSL 0:1239e9b70ca2 2449
wolfSSL 0:1239e9b70ca2 2450 CYASSL_LOCAL word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz)
wolfSSL 0:1239e9b70ca2 2451 {
wolfSSL 0:1239e9b70ca2 2452 /* adding TAG_NULL and 0 to end */
wolfSSL 0:1239e9b70ca2 2453
wolfSSL 0:1239e9b70ca2 2454 /* hashTypes */
wolfSSL 0:1239e9b70ca2 2455 static const byte shaAlgoID[] = { 0x2b, 0x0e, 0x03, 0x02, 0x1a,
wolfSSL 0:1239e9b70ca2 2456 0x05, 0x00 };
wolfSSL 0:1239e9b70ca2 2457 static const byte sha256AlgoID[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
wolfSSL 0:1239e9b70ca2 2458 0x04, 0x02, 0x01, 0x05, 0x00 };
wolfSSL 0:1239e9b70ca2 2459 static const byte sha384AlgoID[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
wolfSSL 0:1239e9b70ca2 2460 0x04, 0x02, 0x02, 0x05, 0x00 };
wolfSSL 0:1239e9b70ca2 2461 static const byte sha512AlgoID[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
wolfSSL 0:1239e9b70ca2 2462 0x04, 0x02, 0x03, 0x05, 0x00 };
wolfSSL 0:1239e9b70ca2 2463 static const byte md5AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
wolfSSL 0:1239e9b70ca2 2464 0x02, 0x05, 0x05, 0x00 };
wolfSSL 0:1239e9b70ca2 2465 static const byte md2AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
wolfSSL 0:1239e9b70ca2 2466 0x02, 0x02, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2467
wolfSSL 0:1239e9b70ca2 2468 /* blkTypes, no NULL tags because IV is there instead */
wolfSSL 0:1239e9b70ca2 2469 static const byte desCbcAlgoID[] = { 0x2B, 0x0E, 0x03, 0x02, 0x07 };
wolfSSL 0:1239e9b70ca2 2470 static const byte des3CbcAlgoID[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7,
wolfSSL 0:1239e9b70ca2 2471 0x0D, 0x03, 0x07 };
wolfSSL 0:1239e9b70ca2 2472
wolfSSL 0:1239e9b70ca2 2473 /* RSA sigTypes */
wolfSSL 0:1239e9b70ca2 2474 #ifndef NO_RSA
wolfSSL 0:1239e9b70ca2 2475 static const byte md5wRSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7,
wolfSSL 0:1239e9b70ca2 2476 0x0d, 0x01, 0x01, 0x04, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2477 static const byte shawRSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7,
wolfSSL 0:1239e9b70ca2 2478 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2479 static const byte sha256wRSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7,
wolfSSL 0:1239e9b70ca2 2480 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2481 static const byte sha384wRSA_AlgoID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
wolfSSL 0:1239e9b70ca2 2482 0x0d, 0x01, 0x01, 0x0c, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2483 static const byte sha512wRSA_AlgoID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7,
wolfSSL 0:1239e9b70ca2 2484 0x0d, 0x01, 0x01, 0x0d, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2485 #endif /* NO_RSA */
wolfSSL 0:1239e9b70ca2 2486
wolfSSL 0:1239e9b70ca2 2487 /* ECDSA sigTypes */
wolfSSL 0:1239e9b70ca2 2488 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 2489 static const byte shawECDSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE, 0x3d,
wolfSSL 0:1239e9b70ca2 2490 0x04, 0x01, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2491 static const byte sha256wECDSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE,0x3d,
wolfSSL 0:1239e9b70ca2 2492 0x04, 0x03, 0x02, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2493 static const byte sha384wECDSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE,0x3d,
wolfSSL 0:1239e9b70ca2 2494 0x04, 0x03, 0x03, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2495 static const byte sha512wECDSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE,0x3d,
wolfSSL 0:1239e9b70ca2 2496 0x04, 0x03, 0x04, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2497 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 2498
wolfSSL 0:1239e9b70ca2 2499 /* RSA keyType */
wolfSSL 0:1239e9b70ca2 2500 #ifndef NO_RSA
wolfSSL 0:1239e9b70ca2 2501 static const byte RSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
wolfSSL 0:1239e9b70ca2 2502 0x01, 0x01, 0x01, 0x05, 0x00};
wolfSSL 0:1239e9b70ca2 2503 #endif /* NO_RSA */
wolfSSL 0:1239e9b70ca2 2504
wolfSSL 0:1239e9b70ca2 2505 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 2506 /* ECC keyType */
wolfSSL 0:1239e9b70ca2 2507 /* no tags, so set tagSz smaller later */
wolfSSL 0:1239e9b70ca2 2508 static const byte ECC_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE, 0x3d,
wolfSSL 0:1239e9b70ca2 2509 0x02, 0x01};
wolfSSL 0:1239e9b70ca2 2510 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 2511
wolfSSL 0:1239e9b70ca2 2512 int algoSz = 0;
wolfSSL 0:1239e9b70ca2 2513 int tagSz = 2; /* tag null and terminator */
wolfSSL 0:1239e9b70ca2 2514 word32 idSz, seqSz;
wolfSSL 0:1239e9b70ca2 2515 const byte* algoName = 0;
wolfSSL 0:1239e9b70ca2 2516 byte ID_Length[MAX_LENGTH_SZ];
wolfSSL 0:1239e9b70ca2 2517 byte seqArray[MAX_SEQ_SZ + 1]; /* add object_id to end */
wolfSSL 0:1239e9b70ca2 2518
wolfSSL 0:1239e9b70ca2 2519 if (type == hashType) {
wolfSSL 0:1239e9b70ca2 2520 switch (algoOID) {
wolfSSL 0:1239e9b70ca2 2521 case SHAh:
wolfSSL 0:1239e9b70ca2 2522 algoSz = sizeof(shaAlgoID);
wolfSSL 0:1239e9b70ca2 2523 algoName = shaAlgoID;
wolfSSL 0:1239e9b70ca2 2524 break;
wolfSSL 0:1239e9b70ca2 2525
wolfSSL 0:1239e9b70ca2 2526 case SHA256h:
wolfSSL 0:1239e9b70ca2 2527 algoSz = sizeof(sha256AlgoID);
wolfSSL 0:1239e9b70ca2 2528 algoName = sha256AlgoID;
wolfSSL 0:1239e9b70ca2 2529 break;
wolfSSL 0:1239e9b70ca2 2530
wolfSSL 0:1239e9b70ca2 2531 case SHA384h:
wolfSSL 0:1239e9b70ca2 2532 algoSz = sizeof(sha384AlgoID);
wolfSSL 0:1239e9b70ca2 2533 algoName = sha384AlgoID;
wolfSSL 0:1239e9b70ca2 2534 break;
wolfSSL 0:1239e9b70ca2 2535
wolfSSL 0:1239e9b70ca2 2536 case SHA512h:
wolfSSL 0:1239e9b70ca2 2537 algoSz = sizeof(sha512AlgoID);
wolfSSL 0:1239e9b70ca2 2538 algoName = sha512AlgoID;
wolfSSL 0:1239e9b70ca2 2539 break;
wolfSSL 0:1239e9b70ca2 2540
wolfSSL 0:1239e9b70ca2 2541 case MD2h:
wolfSSL 0:1239e9b70ca2 2542 algoSz = sizeof(md2AlgoID);
wolfSSL 0:1239e9b70ca2 2543 algoName = md2AlgoID;
wolfSSL 0:1239e9b70ca2 2544 break;
wolfSSL 0:1239e9b70ca2 2545
wolfSSL 0:1239e9b70ca2 2546 case MD5h:
wolfSSL 0:1239e9b70ca2 2547 algoSz = sizeof(md5AlgoID);
wolfSSL 0:1239e9b70ca2 2548 algoName = md5AlgoID;
wolfSSL 0:1239e9b70ca2 2549 break;
wolfSSL 0:1239e9b70ca2 2550
wolfSSL 0:1239e9b70ca2 2551 default:
wolfSSL 0:1239e9b70ca2 2552 CYASSL_MSG("Unknown Hash Algo");
wolfSSL 0:1239e9b70ca2 2553 return 0; /* UNKOWN_HASH_E; */
wolfSSL 0:1239e9b70ca2 2554 }
wolfSSL 0:1239e9b70ca2 2555 }
wolfSSL 0:1239e9b70ca2 2556 else if (type == blkType) {
wolfSSL 0:1239e9b70ca2 2557 switch (algoOID) {
wolfSSL 0:1239e9b70ca2 2558 case DESb:
wolfSSL 0:1239e9b70ca2 2559 algoSz = sizeof(desCbcAlgoID);
wolfSSL 0:1239e9b70ca2 2560 algoName = desCbcAlgoID;
wolfSSL 0:1239e9b70ca2 2561 tagSz = 0;
wolfSSL 0:1239e9b70ca2 2562 break;
wolfSSL 0:1239e9b70ca2 2563 case DES3b:
wolfSSL 0:1239e9b70ca2 2564 algoSz = sizeof(des3CbcAlgoID);
wolfSSL 0:1239e9b70ca2 2565 algoName = des3CbcAlgoID;
wolfSSL 0:1239e9b70ca2 2566 tagSz = 0;
wolfSSL 0:1239e9b70ca2 2567 break;
wolfSSL 0:1239e9b70ca2 2568 default:
wolfSSL 0:1239e9b70ca2 2569 CYASSL_MSG("Unknown Block Algo");
wolfSSL 0:1239e9b70ca2 2570 return 0;
wolfSSL 0:1239e9b70ca2 2571 }
wolfSSL 0:1239e9b70ca2 2572 }
wolfSSL 0:1239e9b70ca2 2573 else if (type == sigType) { /* sigType */
wolfSSL 0:1239e9b70ca2 2574 switch (algoOID) {
wolfSSL 0:1239e9b70ca2 2575 #ifndef NO_RSA
wolfSSL 0:1239e9b70ca2 2576 case CTC_MD5wRSA:
wolfSSL 0:1239e9b70ca2 2577 algoSz = sizeof(md5wRSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2578 algoName = md5wRSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2579 break;
wolfSSL 0:1239e9b70ca2 2580
wolfSSL 0:1239e9b70ca2 2581 case CTC_SHAwRSA:
wolfSSL 0:1239e9b70ca2 2582 algoSz = sizeof(shawRSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2583 algoName = shawRSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2584 break;
wolfSSL 0:1239e9b70ca2 2585
wolfSSL 0:1239e9b70ca2 2586 case CTC_SHA256wRSA:
wolfSSL 0:1239e9b70ca2 2587 algoSz = sizeof(sha256wRSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2588 algoName = sha256wRSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2589 break;
wolfSSL 0:1239e9b70ca2 2590
wolfSSL 0:1239e9b70ca2 2591 case CTC_SHA384wRSA:
wolfSSL 0:1239e9b70ca2 2592 algoSz = sizeof(sha384wRSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2593 algoName = sha384wRSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2594 break;
wolfSSL 0:1239e9b70ca2 2595
wolfSSL 0:1239e9b70ca2 2596 case CTC_SHA512wRSA:
wolfSSL 0:1239e9b70ca2 2597 algoSz = sizeof(sha512wRSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2598 algoName = sha512wRSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2599 break;
wolfSSL 0:1239e9b70ca2 2600 #endif /* NO_RSA */
wolfSSL 0:1239e9b70ca2 2601 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 2602 case CTC_SHAwECDSA:
wolfSSL 0:1239e9b70ca2 2603 algoSz = sizeof(shawECDSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2604 algoName = shawECDSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2605 break;
wolfSSL 0:1239e9b70ca2 2606
wolfSSL 0:1239e9b70ca2 2607 case CTC_SHA256wECDSA:
wolfSSL 0:1239e9b70ca2 2608 algoSz = sizeof(sha256wECDSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2609 algoName = sha256wECDSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2610 break;
wolfSSL 0:1239e9b70ca2 2611
wolfSSL 0:1239e9b70ca2 2612 case CTC_SHA384wECDSA:
wolfSSL 0:1239e9b70ca2 2613 algoSz = sizeof(sha384wECDSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2614 algoName = sha384wECDSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2615 break;
wolfSSL 0:1239e9b70ca2 2616
wolfSSL 0:1239e9b70ca2 2617 case CTC_SHA512wECDSA:
wolfSSL 0:1239e9b70ca2 2618 algoSz = sizeof(sha512wECDSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2619 algoName = sha512wECDSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2620 break;
wolfSSL 0:1239e9b70ca2 2621 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 2622 default:
wolfSSL 0:1239e9b70ca2 2623 CYASSL_MSG("Unknown Signature Algo");
wolfSSL 0:1239e9b70ca2 2624 return 0;
wolfSSL 0:1239e9b70ca2 2625 }
wolfSSL 0:1239e9b70ca2 2626 }
wolfSSL 0:1239e9b70ca2 2627 else if (type == keyType) { /* keyType */
wolfSSL 0:1239e9b70ca2 2628 switch (algoOID) {
wolfSSL 0:1239e9b70ca2 2629 #ifndef NO_RSA
wolfSSL 0:1239e9b70ca2 2630 case RSAk:
wolfSSL 0:1239e9b70ca2 2631 algoSz = sizeof(RSA_AlgoID);
wolfSSL 0:1239e9b70ca2 2632 algoName = RSA_AlgoID;
wolfSSL 0:1239e9b70ca2 2633 break;
wolfSSL 0:1239e9b70ca2 2634 #endif /* NO_RSA */
wolfSSL 0:1239e9b70ca2 2635 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 2636 case ECDSAk:
wolfSSL 0:1239e9b70ca2 2637 algoSz = sizeof(ECC_AlgoID);
wolfSSL 0:1239e9b70ca2 2638 algoName = ECC_AlgoID;
wolfSSL 0:1239e9b70ca2 2639 tagSz = 0;
wolfSSL 0:1239e9b70ca2 2640 break;
wolfSSL 0:1239e9b70ca2 2641 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 2642 default:
wolfSSL 0:1239e9b70ca2 2643 CYASSL_MSG("Unknown Key Algo");
wolfSSL 0:1239e9b70ca2 2644 return 0;
wolfSSL 0:1239e9b70ca2 2645 }
wolfSSL 0:1239e9b70ca2 2646 }
wolfSSL 0:1239e9b70ca2 2647 else {
wolfSSL 0:1239e9b70ca2 2648 CYASSL_MSG("Unknown Algo type");
wolfSSL 0:1239e9b70ca2 2649 return 0;
wolfSSL 0:1239e9b70ca2 2650 }
wolfSSL 0:1239e9b70ca2 2651
wolfSSL 0:1239e9b70ca2 2652 idSz = SetLength(algoSz - tagSz, ID_Length); /* don't include tags */
wolfSSL 0:1239e9b70ca2 2653 seqSz = SetSequence(idSz + algoSz + 1 + curveSz, seqArray);
wolfSSL 0:1239e9b70ca2 2654 /* +1 for object id, curveID of curveSz follows for ecc */
wolfSSL 0:1239e9b70ca2 2655 seqArray[seqSz++] = ASN_OBJECT_ID;
wolfSSL 0:1239e9b70ca2 2656
wolfSSL 0:1239e9b70ca2 2657 XMEMCPY(output, seqArray, seqSz);
wolfSSL 0:1239e9b70ca2 2658 XMEMCPY(output + seqSz, ID_Length, idSz);
wolfSSL 0:1239e9b70ca2 2659 XMEMCPY(output + seqSz + idSz, algoName, algoSz);
wolfSSL 0:1239e9b70ca2 2660
wolfSSL 0:1239e9b70ca2 2661 return seqSz + idSz + algoSz;
wolfSSL 0:1239e9b70ca2 2662
wolfSSL 0:1239e9b70ca2 2663 }
wolfSSL 0:1239e9b70ca2 2664
wolfSSL 0:1239e9b70ca2 2665
wolfSSL 0:1239e9b70ca2 2666 word32 EncodeSignature(byte* out, const byte* digest, word32 digSz, int hashOID)
wolfSSL 0:1239e9b70ca2 2667 {
wolfSSL 0:1239e9b70ca2 2668 byte digArray[MAX_ENCODED_DIG_SZ];
wolfSSL 0:1239e9b70ca2 2669 byte algoArray[MAX_ALGO_SZ];
wolfSSL 0:1239e9b70ca2 2670 byte seqArray[MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 2671 word32 encDigSz, algoSz, seqSz;
wolfSSL 0:1239e9b70ca2 2672
wolfSSL 0:1239e9b70ca2 2673 encDigSz = SetDigest(digest, digSz, digArray);
wolfSSL 0:1239e9b70ca2 2674 algoSz = SetAlgoID(hashOID, algoArray, hashType, 0);
wolfSSL 0:1239e9b70ca2 2675 seqSz = SetSequence(encDigSz + algoSz, seqArray);
wolfSSL 0:1239e9b70ca2 2676
wolfSSL 0:1239e9b70ca2 2677 XMEMCPY(out, seqArray, seqSz);
wolfSSL 0:1239e9b70ca2 2678 XMEMCPY(out + seqSz, algoArray, algoSz);
wolfSSL 0:1239e9b70ca2 2679 XMEMCPY(out + seqSz + algoSz, digArray, encDigSz);
wolfSSL 0:1239e9b70ca2 2680
wolfSSL 0:1239e9b70ca2 2681 return encDigSz + algoSz + seqSz;
wolfSSL 0:1239e9b70ca2 2682 }
wolfSSL 0:1239e9b70ca2 2683
wolfSSL 0:1239e9b70ca2 2684
wolfSSL 0:1239e9b70ca2 2685 /* return true (1) for Confirmation */
wolfSSL 0:1239e9b70ca2 2686 static int ConfirmSignature(const byte* buf, word32 bufSz,
wolfSSL 0:1239e9b70ca2 2687 const byte* key, word32 keySz, word32 keyOID,
wolfSSL 0:1239e9b70ca2 2688 const byte* sig, word32 sigSz, word32 sigOID,
wolfSSL 0:1239e9b70ca2 2689 void* heap)
wolfSSL 0:1239e9b70ca2 2690 {
wolfSSL 0:1239e9b70ca2 2691 #ifdef CYASSL_SHA512
wolfSSL 0:1239e9b70ca2 2692 byte digest[SHA512_DIGEST_SIZE]; /* max size */
wolfSSL 0:1239e9b70ca2 2693 #elif !defined(NO_SHA256)
wolfSSL 0:1239e9b70ca2 2694 byte digest[SHA256_DIGEST_SIZE]; /* max size */
wolfSSL 0:1239e9b70ca2 2695 #else
wolfSSL 0:1239e9b70ca2 2696 byte digest[SHA_DIGEST_SIZE]; /* max size */
wolfSSL 0:1239e9b70ca2 2697 #endif
wolfSSL 0:1239e9b70ca2 2698 int typeH, digestSz, ret = 0;
wolfSSL 0:1239e9b70ca2 2699
wolfSSL 0:1239e9b70ca2 2700 (void)key;
wolfSSL 0:1239e9b70ca2 2701 (void)keySz;
wolfSSL 0:1239e9b70ca2 2702 (void)sig;
wolfSSL 0:1239e9b70ca2 2703 (void)sigSz;
wolfSSL 0:1239e9b70ca2 2704 (void)heap;
wolfSSL 0:1239e9b70ca2 2705 (void)ret;
wolfSSL 0:1239e9b70ca2 2706
wolfSSL 0:1239e9b70ca2 2707 switch (sigOID) {
wolfSSL 0:1239e9b70ca2 2708 #ifndef NO_MD5
wolfSSL 0:1239e9b70ca2 2709 case CTC_MD5wRSA:
wolfSSL 0:1239e9b70ca2 2710 {
wolfSSL 0:1239e9b70ca2 2711 Md5 md5;
wolfSSL 0:1239e9b70ca2 2712 InitMd5(&md5);
wolfSSL 0:1239e9b70ca2 2713 Md5Update(&md5, buf, bufSz);
wolfSSL 0:1239e9b70ca2 2714 Md5Final(&md5, digest);
wolfSSL 0:1239e9b70ca2 2715 typeH = MD5h;
wolfSSL 0:1239e9b70ca2 2716 digestSz = MD5_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 2717 }
wolfSSL 0:1239e9b70ca2 2718 break;
wolfSSL 0:1239e9b70ca2 2719 #endif
wolfSSL 0:1239e9b70ca2 2720 #if defined(CYASSL_MD2)
wolfSSL 0:1239e9b70ca2 2721 case CTC_MD2wRSA:
wolfSSL 0:1239e9b70ca2 2722 {
wolfSSL 0:1239e9b70ca2 2723 Md2 md2;
wolfSSL 0:1239e9b70ca2 2724 InitMd2(&md2);
wolfSSL 0:1239e9b70ca2 2725 Md2Update(&md2, buf, bufSz);
wolfSSL 0:1239e9b70ca2 2726 Md2Final(&md2, digest);
wolfSSL 0:1239e9b70ca2 2727 typeH = MD2h;
wolfSSL 0:1239e9b70ca2 2728 digestSz = MD2_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 2729 }
wolfSSL 0:1239e9b70ca2 2730 break;
wolfSSL 0:1239e9b70ca2 2731 #endif
wolfSSL 0:1239e9b70ca2 2732 #ifndef NO_SHA
wolfSSL 0:1239e9b70ca2 2733 case CTC_SHAwRSA:
wolfSSL 0:1239e9b70ca2 2734 case CTC_SHAwDSA:
wolfSSL 0:1239e9b70ca2 2735 case CTC_SHAwECDSA:
wolfSSL 0:1239e9b70ca2 2736 {
wolfSSL 0:1239e9b70ca2 2737 Sha sha;
wolfSSL 0:1239e9b70ca2 2738 ret = InitSha(&sha);
wolfSSL 0:1239e9b70ca2 2739 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2740 CYASSL_MSG("InitSha failed");
wolfSSL 0:1239e9b70ca2 2741 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2742 }
wolfSSL 0:1239e9b70ca2 2743 ShaUpdate(&sha, buf, bufSz);
wolfSSL 0:1239e9b70ca2 2744 ShaFinal(&sha, digest);
wolfSSL 0:1239e9b70ca2 2745 typeH = SHAh;
wolfSSL 0:1239e9b70ca2 2746 digestSz = SHA_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 2747 }
wolfSSL 0:1239e9b70ca2 2748 break;
wolfSSL 0:1239e9b70ca2 2749 #endif
wolfSSL 0:1239e9b70ca2 2750 #ifndef NO_SHA256
wolfSSL 0:1239e9b70ca2 2751 case CTC_SHA256wRSA:
wolfSSL 0:1239e9b70ca2 2752 case CTC_SHA256wECDSA:
wolfSSL 0:1239e9b70ca2 2753 {
wolfSSL 0:1239e9b70ca2 2754 Sha256 sha256;
wolfSSL 0:1239e9b70ca2 2755 ret = InitSha256(&sha256);
wolfSSL 0:1239e9b70ca2 2756 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2757 CYASSL_MSG("InitSha256 failed");
wolfSSL 0:1239e9b70ca2 2758 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2759 }
wolfSSL 0:1239e9b70ca2 2760
wolfSSL 0:1239e9b70ca2 2761 ret = Sha256Update(&sha256, buf, bufSz);
wolfSSL 0:1239e9b70ca2 2762 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2763 CYASSL_MSG("Sha256Update failed");
wolfSSL 0:1239e9b70ca2 2764 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2765 }
wolfSSL 0:1239e9b70ca2 2766
wolfSSL 0:1239e9b70ca2 2767 ret = Sha256Final(&sha256, digest);
wolfSSL 0:1239e9b70ca2 2768 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2769 CYASSL_MSG("Sha256Final failed");
wolfSSL 0:1239e9b70ca2 2770 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2771 }
wolfSSL 0:1239e9b70ca2 2772
wolfSSL 0:1239e9b70ca2 2773 typeH = SHA256h;
wolfSSL 0:1239e9b70ca2 2774 digestSz = SHA256_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 2775 }
wolfSSL 0:1239e9b70ca2 2776 break;
wolfSSL 0:1239e9b70ca2 2777 #endif
wolfSSL 0:1239e9b70ca2 2778 #ifdef CYASSL_SHA512
wolfSSL 0:1239e9b70ca2 2779 case CTC_SHA512wRSA:
wolfSSL 0:1239e9b70ca2 2780 case CTC_SHA512wECDSA:
wolfSSL 0:1239e9b70ca2 2781 {
wolfSSL 0:1239e9b70ca2 2782 Sha512 sha512;
wolfSSL 0:1239e9b70ca2 2783 ret = InitSha512(&sha512);
wolfSSL 0:1239e9b70ca2 2784 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2785 CYASSL_MSG("InitSha512 failed");
wolfSSL 0:1239e9b70ca2 2786 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2787 }
wolfSSL 0:1239e9b70ca2 2788
wolfSSL 0:1239e9b70ca2 2789 ret = Sha512Update(&sha512, buf, bufSz);
wolfSSL 0:1239e9b70ca2 2790 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2791 CYASSL_MSG("Sha512Update failed");
wolfSSL 0:1239e9b70ca2 2792 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2793 }
wolfSSL 0:1239e9b70ca2 2794
wolfSSL 0:1239e9b70ca2 2795 ret = Sha512Final(&sha512, digest);
wolfSSL 0:1239e9b70ca2 2796 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2797 CYASSL_MSG("Sha512Final failed");
wolfSSL 0:1239e9b70ca2 2798 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2799 }
wolfSSL 0:1239e9b70ca2 2800
wolfSSL 0:1239e9b70ca2 2801 typeH = SHA512h;
wolfSSL 0:1239e9b70ca2 2802 digestSz = SHA512_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 2803 }
wolfSSL 0:1239e9b70ca2 2804 break;
wolfSSL 0:1239e9b70ca2 2805 #endif
wolfSSL 0:1239e9b70ca2 2806 #ifdef CYASSL_SHA384
wolfSSL 0:1239e9b70ca2 2807 case CTC_SHA384wRSA:
wolfSSL 0:1239e9b70ca2 2808 case CTC_SHA384wECDSA:
wolfSSL 0:1239e9b70ca2 2809 {
wolfSSL 0:1239e9b70ca2 2810 Sha384 sha384;
wolfSSL 0:1239e9b70ca2 2811 ret = InitSha384(&sha384);
wolfSSL 0:1239e9b70ca2 2812 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2813 CYASSL_MSG("InitSha384 failed");
wolfSSL 0:1239e9b70ca2 2814 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2815 }
wolfSSL 0:1239e9b70ca2 2816
wolfSSL 0:1239e9b70ca2 2817 ret = Sha384Update(&sha384, buf, bufSz);
wolfSSL 0:1239e9b70ca2 2818 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2819 CYASSL_MSG("Sha384Update failed");
wolfSSL 0:1239e9b70ca2 2820 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2821 }
wolfSSL 0:1239e9b70ca2 2822
wolfSSL 0:1239e9b70ca2 2823 ret = Sha384Final(&sha384, digest);
wolfSSL 0:1239e9b70ca2 2824 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 2825 CYASSL_MSG("Sha384Final failed");
wolfSSL 0:1239e9b70ca2 2826 return 0; /* not confirmed */
wolfSSL 0:1239e9b70ca2 2827 }
wolfSSL 0:1239e9b70ca2 2828
wolfSSL 0:1239e9b70ca2 2829 typeH = SHA384h;
wolfSSL 0:1239e9b70ca2 2830 digestSz = SHA384_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 2831 }
wolfSSL 0:1239e9b70ca2 2832 break;
wolfSSL 0:1239e9b70ca2 2833 #endif
wolfSSL 0:1239e9b70ca2 2834 default:
wolfSSL 0:1239e9b70ca2 2835 CYASSL_MSG("Verify Signautre has unsupported type");
wolfSSL 0:1239e9b70ca2 2836 return 0;
wolfSSL 0:1239e9b70ca2 2837 }
wolfSSL 0:1239e9b70ca2 2838 (void)typeH; /* some builds won't read */
wolfSSL 0:1239e9b70ca2 2839
wolfSSL 0:1239e9b70ca2 2840 switch (keyOID) {
wolfSSL 0:1239e9b70ca2 2841 #ifndef NO_RSA
wolfSSL 0:1239e9b70ca2 2842 case RSAk:
wolfSSL 0:1239e9b70ca2 2843 {
wolfSSL 0:1239e9b70ca2 2844 RsaKey pubKey;
wolfSSL 0:1239e9b70ca2 2845 byte encodedSig[MAX_ENCODED_SIG_SZ];
wolfSSL 0:1239e9b70ca2 2846 byte plain[MAX_ENCODED_SIG_SZ];
wolfSSL 0:1239e9b70ca2 2847 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 2848 int encodedSigSz, verifySz;
wolfSSL 0:1239e9b70ca2 2849 byte* out;
wolfSSL 0:1239e9b70ca2 2850
wolfSSL 0:1239e9b70ca2 2851 if (sigSz > MAX_ENCODED_SIG_SZ) {
wolfSSL 0:1239e9b70ca2 2852 CYASSL_MSG("Verify Signautre is too big");
wolfSSL 0:1239e9b70ca2 2853 return 0;
wolfSSL 0:1239e9b70ca2 2854 }
wolfSSL 0:1239e9b70ca2 2855
wolfSSL 0:1239e9b70ca2 2856 ret = InitRsaKey(&pubKey, heap);
wolfSSL 0:1239e9b70ca2 2857 if (ret != 0) return ret;
wolfSSL 0:1239e9b70ca2 2858 if (RsaPublicKeyDecode(key, &idx, &pubKey, keySz) < 0) {
wolfSSL 0:1239e9b70ca2 2859 CYASSL_MSG("ASN Key decode error RSA");
wolfSSL 0:1239e9b70ca2 2860 ret = 0;
wolfSSL 0:1239e9b70ca2 2861 }
wolfSSL 0:1239e9b70ca2 2862 else {
wolfSSL 0:1239e9b70ca2 2863 XMEMCPY(plain, sig, sigSz);
wolfSSL 0:1239e9b70ca2 2864 if ( (verifySz = RsaSSL_VerifyInline(plain, sigSz, &out,
wolfSSL 0:1239e9b70ca2 2865 &pubKey)) < 0) {
wolfSSL 0:1239e9b70ca2 2866 CYASSL_MSG("Rsa SSL verify error");
wolfSSL 0:1239e9b70ca2 2867 ret = 0;
wolfSSL 0:1239e9b70ca2 2868 }
wolfSSL 0:1239e9b70ca2 2869 else {
wolfSSL 0:1239e9b70ca2 2870 /* make sure we're right justified */
wolfSSL 0:1239e9b70ca2 2871 encodedSigSz =
wolfSSL 0:1239e9b70ca2 2872 EncodeSignature(encodedSig, digest, digestSz, typeH);
wolfSSL 0:1239e9b70ca2 2873 if (encodedSigSz != verifySz ||
wolfSSL 0:1239e9b70ca2 2874 XMEMCMP(out, encodedSig, encodedSigSz) != 0) {
wolfSSL 0:1239e9b70ca2 2875 CYASSL_MSG("Rsa SSL verify match encode error");
wolfSSL 0:1239e9b70ca2 2876 ret = 0;
wolfSSL 0:1239e9b70ca2 2877 }
wolfSSL 0:1239e9b70ca2 2878 else
wolfSSL 0:1239e9b70ca2 2879 ret = 1; /* match */
wolfSSL 0:1239e9b70ca2 2880
wolfSSL 0:1239e9b70ca2 2881 #ifdef CYASSL_DEBUG_ENCODING
wolfSSL 0:1239e9b70ca2 2882 {
wolfSSL 0:1239e9b70ca2 2883 int x;
wolfSSL 0:1239e9b70ca2 2884 printf("cyassl encodedSig:\n");
wolfSSL 0:1239e9b70ca2 2885 for (x = 0; x < encodedSigSz; x++) {
wolfSSL 0:1239e9b70ca2 2886 printf("%02x ", encodedSig[x]);
wolfSSL 0:1239e9b70ca2 2887 if ( (x % 16) == 15)
wolfSSL 0:1239e9b70ca2 2888 printf("\n");
wolfSSL 0:1239e9b70ca2 2889 }
wolfSSL 0:1239e9b70ca2 2890 printf("\n");
wolfSSL 0:1239e9b70ca2 2891 printf("actual digest:\n");
wolfSSL 0:1239e9b70ca2 2892 for (x = 0; x < verifySz; x++) {
wolfSSL 0:1239e9b70ca2 2893 printf("%02x ", out[x]);
wolfSSL 0:1239e9b70ca2 2894 if ( (x % 16) == 15)
wolfSSL 0:1239e9b70ca2 2895 printf("\n");
wolfSSL 0:1239e9b70ca2 2896 }
wolfSSL 0:1239e9b70ca2 2897 printf("\n");
wolfSSL 0:1239e9b70ca2 2898 }
wolfSSL 0:1239e9b70ca2 2899 #endif /* CYASSL_DEBUG_ENCODING */
wolfSSL 0:1239e9b70ca2 2900 }
wolfSSL 0:1239e9b70ca2 2901 }
wolfSSL 0:1239e9b70ca2 2902 FreeRsaKey(&pubKey);
wolfSSL 0:1239e9b70ca2 2903 return ret;
wolfSSL 0:1239e9b70ca2 2904 }
wolfSSL 0:1239e9b70ca2 2905
wolfSSL 0:1239e9b70ca2 2906 #endif /* NO_RSA */
wolfSSL 0:1239e9b70ca2 2907 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 2908 case ECDSAk:
wolfSSL 0:1239e9b70ca2 2909 {
wolfSSL 0:1239e9b70ca2 2910 ecc_key pubKey;
wolfSSL 0:1239e9b70ca2 2911 int verify = 0;
wolfSSL 0:1239e9b70ca2 2912
wolfSSL 0:1239e9b70ca2 2913 if (ecc_import_x963(key, keySz, &pubKey) < 0) {
wolfSSL 0:1239e9b70ca2 2914 CYASSL_MSG("ASN Key import error ECC");
wolfSSL 0:1239e9b70ca2 2915 return 0;
wolfSSL 0:1239e9b70ca2 2916 }
wolfSSL 0:1239e9b70ca2 2917
wolfSSL 0:1239e9b70ca2 2918 ret = ecc_verify_hash(sig,sigSz,digest,digestSz,&verify,&pubKey);
wolfSSL 0:1239e9b70ca2 2919 ecc_free(&pubKey);
wolfSSL 0:1239e9b70ca2 2920 if (ret == 0 && verify == 1)
wolfSSL 0:1239e9b70ca2 2921 return 1; /* match */
wolfSSL 0:1239e9b70ca2 2922
wolfSSL 0:1239e9b70ca2 2923 CYASSL_MSG("ECC Verify didn't match");
wolfSSL 0:1239e9b70ca2 2924 return 0;
wolfSSL 0:1239e9b70ca2 2925 }
wolfSSL 0:1239e9b70ca2 2926 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 2927 default:
wolfSSL 0:1239e9b70ca2 2928 CYASSL_MSG("Verify Key type unknown");
wolfSSL 0:1239e9b70ca2 2929 return 0;
wolfSSL 0:1239e9b70ca2 2930 }
wolfSSL 0:1239e9b70ca2 2931 }
wolfSSL 0:1239e9b70ca2 2932
wolfSSL 0:1239e9b70ca2 2933
wolfSSL 0:1239e9b70ca2 2934 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 2935
wolfSSL 0:1239e9b70ca2 2936 static int MatchBaseName(int type, const char* name, int nameSz,
wolfSSL 0:1239e9b70ca2 2937 const char* base, int baseSz)
wolfSSL 0:1239e9b70ca2 2938 {
wolfSSL 0:1239e9b70ca2 2939 if (base == NULL || baseSz <= 0 || name == NULL || nameSz <= 0 ||
wolfSSL 0:1239e9b70ca2 2940 name[0] == '.' || nameSz < baseSz ||
wolfSSL 0:1239e9b70ca2 2941 (type != ASN_RFC822_TYPE && type != ASN_DNS_TYPE))
wolfSSL 0:1239e9b70ca2 2942 return 0;
wolfSSL 0:1239e9b70ca2 2943
wolfSSL 0:1239e9b70ca2 2944 /* If an email type, handle special cases where the base is only
wolfSSL 0:1239e9b70ca2 2945 * a domain, or is an email address itself. */
wolfSSL 0:1239e9b70ca2 2946 if (type == ASN_RFC822_TYPE) {
wolfSSL 0:1239e9b70ca2 2947 const char* p = NULL;
wolfSSL 0:1239e9b70ca2 2948 int count = 0;
wolfSSL 0:1239e9b70ca2 2949
wolfSSL 0:1239e9b70ca2 2950 if (base[0] != '.') {
wolfSSL 0:1239e9b70ca2 2951 p = base;
wolfSSL 0:1239e9b70ca2 2952 count = 0;
wolfSSL 0:1239e9b70ca2 2953
wolfSSL 0:1239e9b70ca2 2954 /* find the '@' in the base */
wolfSSL 0:1239e9b70ca2 2955 while (*p != '@' && count < baseSz) {
wolfSSL 0:1239e9b70ca2 2956 count++;
wolfSSL 0:1239e9b70ca2 2957 p++;
wolfSSL 0:1239e9b70ca2 2958 }
wolfSSL 0:1239e9b70ca2 2959
wolfSSL 0:1239e9b70ca2 2960 /* No '@' in base, reset p to NULL */
wolfSSL 0:1239e9b70ca2 2961 if (count >= baseSz)
wolfSSL 0:1239e9b70ca2 2962 p = NULL;
wolfSSL 0:1239e9b70ca2 2963 }
wolfSSL 0:1239e9b70ca2 2964
wolfSSL 0:1239e9b70ca2 2965 if (p == NULL) {
wolfSSL 0:1239e9b70ca2 2966 /* Base isn't an email address, it is a domain name,
wolfSSL 0:1239e9b70ca2 2967 * wind the name forward one character past its '@'. */
wolfSSL 0:1239e9b70ca2 2968 p = name;
wolfSSL 0:1239e9b70ca2 2969 count = 0;
wolfSSL 0:1239e9b70ca2 2970 while (*p != '@' && count < baseSz) {
wolfSSL 0:1239e9b70ca2 2971 count++;
wolfSSL 0:1239e9b70ca2 2972 p++;
wolfSSL 0:1239e9b70ca2 2973 }
wolfSSL 0:1239e9b70ca2 2974
wolfSSL 0:1239e9b70ca2 2975 if (count < baseSz && *p == '@') {
wolfSSL 0:1239e9b70ca2 2976 name = p + 1;
wolfSSL 0:1239e9b70ca2 2977 nameSz -= count + 1;
wolfSSL 0:1239e9b70ca2 2978 }
wolfSSL 0:1239e9b70ca2 2979 }
wolfSSL 0:1239e9b70ca2 2980 }
wolfSSL 0:1239e9b70ca2 2981
wolfSSL 0:1239e9b70ca2 2982 if ((type == ASN_DNS_TYPE || type == ASN_RFC822_TYPE) && base[0] == '.') {
wolfSSL 0:1239e9b70ca2 2983 int szAdjust = nameSz - baseSz;
wolfSSL 0:1239e9b70ca2 2984 name += szAdjust;
wolfSSL 0:1239e9b70ca2 2985 nameSz -= szAdjust;
wolfSSL 0:1239e9b70ca2 2986 }
wolfSSL 0:1239e9b70ca2 2987
wolfSSL 0:1239e9b70ca2 2988 while (nameSz > 0) {
wolfSSL 0:1239e9b70ca2 2989 if (XTOLOWER(*name++) != XTOLOWER(*base++))
wolfSSL 0:1239e9b70ca2 2990 return 0;
wolfSSL 0:1239e9b70ca2 2991 nameSz--;
wolfSSL 0:1239e9b70ca2 2992 }
wolfSSL 0:1239e9b70ca2 2993
wolfSSL 0:1239e9b70ca2 2994 return 1;
wolfSSL 0:1239e9b70ca2 2995 }
wolfSSL 0:1239e9b70ca2 2996
wolfSSL 0:1239e9b70ca2 2997
wolfSSL 0:1239e9b70ca2 2998 static int ConfirmNameConstraints(Signer* signer, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 2999 {
wolfSSL 0:1239e9b70ca2 3000 if (signer == NULL || cert == NULL)
wolfSSL 0:1239e9b70ca2 3001 return 0;
wolfSSL 0:1239e9b70ca2 3002
wolfSSL 0:1239e9b70ca2 3003 /* Check against the excluded list */
wolfSSL 0:1239e9b70ca2 3004 if (signer->excludedNames) {
wolfSSL 0:1239e9b70ca2 3005 Base_entry* base = signer->excludedNames;
wolfSSL 0:1239e9b70ca2 3006
wolfSSL 0:1239e9b70ca2 3007 while (base != NULL) {
wolfSSL 0:1239e9b70ca2 3008 if (base->type == ASN_DNS_TYPE) {
wolfSSL 0:1239e9b70ca2 3009 DNS_entry* name = cert->altNames;
wolfSSL 0:1239e9b70ca2 3010 while (name != NULL) {
wolfSSL 0:1239e9b70ca2 3011 if (MatchBaseName(ASN_DNS_TYPE,
wolfSSL 0:1239e9b70ca2 3012 name->name, (int)XSTRLEN(name->name),
wolfSSL 0:1239e9b70ca2 3013 base->name, base->nameSz))
wolfSSL 0:1239e9b70ca2 3014 return 0;
wolfSSL 0:1239e9b70ca2 3015 name = name->next;
wolfSSL 0:1239e9b70ca2 3016 }
wolfSSL 0:1239e9b70ca2 3017 }
wolfSSL 0:1239e9b70ca2 3018 else if (base->type == ASN_RFC822_TYPE) {
wolfSSL 0:1239e9b70ca2 3019 DNS_entry* name = cert->altEmailNames;
wolfSSL 0:1239e9b70ca2 3020 while (name != NULL) {
wolfSSL 0:1239e9b70ca2 3021 if (MatchBaseName(ASN_RFC822_TYPE,
wolfSSL 0:1239e9b70ca2 3022 name->name, (int)XSTRLEN(name->name),
wolfSSL 0:1239e9b70ca2 3023 base->name, base->nameSz))
wolfSSL 0:1239e9b70ca2 3024 return 0;
wolfSSL 0:1239e9b70ca2 3025
wolfSSL 0:1239e9b70ca2 3026 name = name->next;
wolfSSL 0:1239e9b70ca2 3027 }
wolfSSL 0:1239e9b70ca2 3028 }
wolfSSL 0:1239e9b70ca2 3029 else if (base->type == ASN_DIR_TYPE) {
wolfSSL 0:1239e9b70ca2 3030 if (cert->subjectRawLen == base->nameSz &&
wolfSSL 0:1239e9b70ca2 3031 XMEMCMP(cert->subjectRaw, base->name, base->nameSz) == 0) {
wolfSSL 0:1239e9b70ca2 3032
wolfSSL 0:1239e9b70ca2 3033 return 0;
wolfSSL 0:1239e9b70ca2 3034 }
wolfSSL 0:1239e9b70ca2 3035 }
wolfSSL 0:1239e9b70ca2 3036 base = base->next;
wolfSSL 0:1239e9b70ca2 3037 }
wolfSSL 0:1239e9b70ca2 3038 }
wolfSSL 0:1239e9b70ca2 3039
wolfSSL 0:1239e9b70ca2 3040 /* Check against the permitted list */
wolfSSL 0:1239e9b70ca2 3041 if (signer->permittedNames != NULL) {
wolfSSL 0:1239e9b70ca2 3042 int needDns = 0;
wolfSSL 0:1239e9b70ca2 3043 int matchDns = 0;
wolfSSL 0:1239e9b70ca2 3044 int needEmail = 0;
wolfSSL 0:1239e9b70ca2 3045 int matchEmail = 0;
wolfSSL 0:1239e9b70ca2 3046 int needDir = 0;
wolfSSL 0:1239e9b70ca2 3047 int matchDir = 0;
wolfSSL 0:1239e9b70ca2 3048 Base_entry* base = signer->permittedNames;
wolfSSL 0:1239e9b70ca2 3049
wolfSSL 0:1239e9b70ca2 3050 while (base != NULL) {
wolfSSL 0:1239e9b70ca2 3051 if (base->type == ASN_DNS_TYPE) {
wolfSSL 0:1239e9b70ca2 3052 DNS_entry* name = cert->altNames;
wolfSSL 0:1239e9b70ca2 3053
wolfSSL 0:1239e9b70ca2 3054 if (name != NULL)
wolfSSL 0:1239e9b70ca2 3055 needDns = 1;
wolfSSL 0:1239e9b70ca2 3056
wolfSSL 0:1239e9b70ca2 3057 while (name != NULL) {
wolfSSL 0:1239e9b70ca2 3058 matchDns = MatchBaseName(ASN_DNS_TYPE,
wolfSSL 0:1239e9b70ca2 3059 name->name, (int)XSTRLEN(name->name),
wolfSSL 0:1239e9b70ca2 3060 base->name, base->nameSz);
wolfSSL 0:1239e9b70ca2 3061 name = name->next;
wolfSSL 0:1239e9b70ca2 3062 }
wolfSSL 0:1239e9b70ca2 3063 }
wolfSSL 0:1239e9b70ca2 3064 else if (base->type == ASN_RFC822_TYPE) {
wolfSSL 0:1239e9b70ca2 3065 DNS_entry* name = cert->altEmailNames;
wolfSSL 0:1239e9b70ca2 3066
wolfSSL 0:1239e9b70ca2 3067 if (name != NULL)
wolfSSL 0:1239e9b70ca2 3068 needEmail = 1;
wolfSSL 0:1239e9b70ca2 3069
wolfSSL 0:1239e9b70ca2 3070 while (name != NULL) {
wolfSSL 0:1239e9b70ca2 3071 matchEmail = MatchBaseName(ASN_DNS_TYPE,
wolfSSL 0:1239e9b70ca2 3072 name->name, (int)XSTRLEN(name->name),
wolfSSL 0:1239e9b70ca2 3073 base->name, base->nameSz);
wolfSSL 0:1239e9b70ca2 3074 name = name->next;
wolfSSL 0:1239e9b70ca2 3075 }
wolfSSL 0:1239e9b70ca2 3076 }
wolfSSL 0:1239e9b70ca2 3077 else if (base->type == ASN_DIR_TYPE) {
wolfSSL 0:1239e9b70ca2 3078 needDir = 1;
wolfSSL 0:1239e9b70ca2 3079 if (cert->subjectRaw != NULL &&
wolfSSL 0:1239e9b70ca2 3080 cert->subjectRawLen == base->nameSz &&
wolfSSL 0:1239e9b70ca2 3081 XMEMCMP(cert->subjectRaw, base->name, base->nameSz) == 0) {
wolfSSL 0:1239e9b70ca2 3082
wolfSSL 0:1239e9b70ca2 3083 matchDir = 1;
wolfSSL 0:1239e9b70ca2 3084 }
wolfSSL 0:1239e9b70ca2 3085 }
wolfSSL 0:1239e9b70ca2 3086 base = base->next;
wolfSSL 0:1239e9b70ca2 3087 }
wolfSSL 0:1239e9b70ca2 3088
wolfSSL 0:1239e9b70ca2 3089 if ((needDns && !matchDns) || (needEmail && !matchEmail) ||
wolfSSL 0:1239e9b70ca2 3090 (needDir && !matchDir)) {
wolfSSL 0:1239e9b70ca2 3091
wolfSSL 0:1239e9b70ca2 3092 return 0;
wolfSSL 0:1239e9b70ca2 3093 }
wolfSSL 0:1239e9b70ca2 3094 }
wolfSSL 0:1239e9b70ca2 3095
wolfSSL 0:1239e9b70ca2 3096 return 1;
wolfSSL 0:1239e9b70ca2 3097 }
wolfSSL 0:1239e9b70ca2 3098
wolfSSL 0:1239e9b70ca2 3099 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 3100
wolfSSL 0:1239e9b70ca2 3101
wolfSSL 0:1239e9b70ca2 3102 static int DecodeAltNames(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3103 {
wolfSSL 0:1239e9b70ca2 3104 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3105 int length = 0;
wolfSSL 0:1239e9b70ca2 3106
wolfSSL 0:1239e9b70ca2 3107 CYASSL_ENTER("DecodeAltNames");
wolfSSL 0:1239e9b70ca2 3108
wolfSSL 0:1239e9b70ca2 3109 if (GetSequence(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3110 CYASSL_MSG("\tBad Sequence");
wolfSSL 0:1239e9b70ca2 3111 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3112 }
wolfSSL 0:1239e9b70ca2 3113
wolfSSL 0:1239e9b70ca2 3114 while (length > 0) {
wolfSSL 0:1239e9b70ca2 3115 byte b = input[idx++];
wolfSSL 0:1239e9b70ca2 3116
wolfSSL 0:1239e9b70ca2 3117 length--;
wolfSSL 0:1239e9b70ca2 3118
wolfSSL 0:1239e9b70ca2 3119 /* Save DNS Type names in the altNames list. */
wolfSSL 0:1239e9b70ca2 3120 /* Save Other Type names in the cert's OidMap */
wolfSSL 0:1239e9b70ca2 3121 if (b == (ASN_CONTEXT_SPECIFIC | ASN_DNS_TYPE)) {
wolfSSL 0:1239e9b70ca2 3122 DNS_entry* dnsEntry;
wolfSSL 0:1239e9b70ca2 3123 int strLen;
wolfSSL 0:1239e9b70ca2 3124 word32 lenStartIdx = idx;
wolfSSL 0:1239e9b70ca2 3125
wolfSSL 0:1239e9b70ca2 3126 if (GetLength(input, &idx, &strLen, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3127 CYASSL_MSG("\tfail: str length");
wolfSSL 0:1239e9b70ca2 3128 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3129 }
wolfSSL 0:1239e9b70ca2 3130 length -= (idx - lenStartIdx);
wolfSSL 0:1239e9b70ca2 3131
wolfSSL 0:1239e9b70ca2 3132 dnsEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap,
wolfSSL 0:1239e9b70ca2 3133 DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 3134 if (dnsEntry == NULL) {
wolfSSL 0:1239e9b70ca2 3135 CYASSL_MSG("\tOut of Memory");
wolfSSL 0:1239e9b70ca2 3136 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3137 }
wolfSSL 0:1239e9b70ca2 3138
wolfSSL 0:1239e9b70ca2 3139 dnsEntry->name = (char*)XMALLOC(strLen + 1, cert->heap,
wolfSSL 0:1239e9b70ca2 3140 DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 3141 if (dnsEntry->name == NULL) {
wolfSSL 0:1239e9b70ca2 3142 CYASSL_MSG("\tOut of Memory");
wolfSSL 0:1239e9b70ca2 3143 XFREE(dnsEntry, cert->heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 3144 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3145 }
wolfSSL 0:1239e9b70ca2 3146
wolfSSL 0:1239e9b70ca2 3147 XMEMCPY(dnsEntry->name, &input[idx], strLen);
wolfSSL 0:1239e9b70ca2 3148 dnsEntry->name[strLen] = '\0';
wolfSSL 0:1239e9b70ca2 3149
wolfSSL 0:1239e9b70ca2 3150 dnsEntry->next = cert->altNames;
wolfSSL 0:1239e9b70ca2 3151 cert->altNames = dnsEntry;
wolfSSL 0:1239e9b70ca2 3152
wolfSSL 0:1239e9b70ca2 3153 length -= strLen;
wolfSSL 0:1239e9b70ca2 3154 idx += strLen;
wolfSSL 0:1239e9b70ca2 3155 }
wolfSSL 0:1239e9b70ca2 3156 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 3157 else if (b == (ASN_CONTEXT_SPECIFIC | ASN_RFC822_TYPE)) {
wolfSSL 0:1239e9b70ca2 3158 DNS_entry* emailEntry;
wolfSSL 0:1239e9b70ca2 3159 int strLen;
wolfSSL 0:1239e9b70ca2 3160 word32 lenStartIdx = idx;
wolfSSL 0:1239e9b70ca2 3161
wolfSSL 0:1239e9b70ca2 3162 if (GetLength(input, &idx, &strLen, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3163 CYASSL_MSG("\tfail: str length");
wolfSSL 0:1239e9b70ca2 3164 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3165 }
wolfSSL 0:1239e9b70ca2 3166 length -= (idx - lenStartIdx);
wolfSSL 0:1239e9b70ca2 3167
wolfSSL 0:1239e9b70ca2 3168 emailEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap,
wolfSSL 0:1239e9b70ca2 3169 DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 3170 if (emailEntry == NULL) {
wolfSSL 0:1239e9b70ca2 3171 CYASSL_MSG("\tOut of Memory");
wolfSSL 0:1239e9b70ca2 3172 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3173 }
wolfSSL 0:1239e9b70ca2 3174
wolfSSL 0:1239e9b70ca2 3175 emailEntry->name = (char*)XMALLOC(strLen + 1, cert->heap,
wolfSSL 0:1239e9b70ca2 3176 DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 3177 if (emailEntry->name == NULL) {
wolfSSL 0:1239e9b70ca2 3178 CYASSL_MSG("\tOut of Memory");
wolfSSL 0:1239e9b70ca2 3179 XFREE(emailEntry, cert->heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 3180 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3181 }
wolfSSL 0:1239e9b70ca2 3182
wolfSSL 0:1239e9b70ca2 3183 XMEMCPY(emailEntry->name, &input[idx], strLen);
wolfSSL 0:1239e9b70ca2 3184 emailEntry->name[strLen] = '\0';
wolfSSL 0:1239e9b70ca2 3185
wolfSSL 0:1239e9b70ca2 3186 emailEntry->next = cert->altEmailNames;
wolfSSL 0:1239e9b70ca2 3187 cert->altEmailNames = emailEntry;
wolfSSL 0:1239e9b70ca2 3188
wolfSSL 0:1239e9b70ca2 3189 length -= strLen;
wolfSSL 0:1239e9b70ca2 3190 idx += strLen;
wolfSSL 0:1239e9b70ca2 3191 }
wolfSSL 0:1239e9b70ca2 3192 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 3193 #ifdef CYASSL_SEP
wolfSSL 0:1239e9b70ca2 3194 else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_OTHER_TYPE))
wolfSSL 0:1239e9b70ca2 3195 {
wolfSSL 0:1239e9b70ca2 3196 int strLen;
wolfSSL 0:1239e9b70ca2 3197 word32 lenStartIdx = idx;
wolfSSL 0:1239e9b70ca2 3198 word32 oid = 0;
wolfSSL 0:1239e9b70ca2 3199
wolfSSL 0:1239e9b70ca2 3200 if (GetLength(input, &idx, &strLen, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3201 CYASSL_MSG("\tfail: other name length");
wolfSSL 0:1239e9b70ca2 3202 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3203 }
wolfSSL 0:1239e9b70ca2 3204 /* Consume the rest of this sequence. */
wolfSSL 0:1239e9b70ca2 3205 length -= (strLen + idx - lenStartIdx);
wolfSSL 0:1239e9b70ca2 3206
wolfSSL 0:1239e9b70ca2 3207 if (GetObjectId(input, &idx, &oid, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3208 CYASSL_MSG("\tbad OID");
wolfSSL 0:1239e9b70ca2 3209 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3210 }
wolfSSL 0:1239e9b70ca2 3211
wolfSSL 0:1239e9b70ca2 3212 if (oid != HW_NAME_OID) {
wolfSSL 0:1239e9b70ca2 3213 CYASSL_MSG("\tincorrect OID");
wolfSSL 0:1239e9b70ca2 3214 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3215 }
wolfSSL 0:1239e9b70ca2 3216
wolfSSL 0:1239e9b70ca2 3217 if (input[idx++] != (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) {
wolfSSL 0:1239e9b70ca2 3218 CYASSL_MSG("\twrong type");
wolfSSL 0:1239e9b70ca2 3219 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3220 }
wolfSSL 0:1239e9b70ca2 3221
wolfSSL 0:1239e9b70ca2 3222 if (GetLength(input, &idx, &strLen, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3223 CYASSL_MSG("\tfail: str len");
wolfSSL 0:1239e9b70ca2 3224 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3225 }
wolfSSL 0:1239e9b70ca2 3226
wolfSSL 0:1239e9b70ca2 3227 if (GetSequence(input, &idx, &strLen, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3228 CYASSL_MSG("\tBad Sequence");
wolfSSL 0:1239e9b70ca2 3229 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3230 }
wolfSSL 0:1239e9b70ca2 3231
wolfSSL 0:1239e9b70ca2 3232 if (input[idx++] != ASN_OBJECT_ID) {
wolfSSL 0:1239e9b70ca2 3233 CYASSL_MSG("\texpected OID");
wolfSSL 0:1239e9b70ca2 3234 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3235 }
wolfSSL 0:1239e9b70ca2 3236
wolfSSL 0:1239e9b70ca2 3237 if (GetLength(input, &idx, &strLen, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3238 CYASSL_MSG("\tfailed: str len");
wolfSSL 0:1239e9b70ca2 3239 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3240 }
wolfSSL 0:1239e9b70ca2 3241
wolfSSL 0:1239e9b70ca2 3242 cert->hwType = (byte*)XMALLOC(strLen, cert->heap, 0);
wolfSSL 0:1239e9b70ca2 3243 if (cert->hwType == NULL) {
wolfSSL 0:1239e9b70ca2 3244 CYASSL_MSG("\tOut of Memory");
wolfSSL 0:1239e9b70ca2 3245 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 3246 }
wolfSSL 0:1239e9b70ca2 3247
wolfSSL 0:1239e9b70ca2 3248 XMEMCPY(cert->hwType, &input[idx], strLen);
wolfSSL 0:1239e9b70ca2 3249 cert->hwTypeSz = strLen;
wolfSSL 0:1239e9b70ca2 3250 idx += strLen;
wolfSSL 0:1239e9b70ca2 3251
wolfSSL 0:1239e9b70ca2 3252 if (input[idx++] != ASN_OCTET_STRING) {
wolfSSL 0:1239e9b70ca2 3253 CYASSL_MSG("\texpected Octet String");
wolfSSL 0:1239e9b70ca2 3254 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3255 }
wolfSSL 0:1239e9b70ca2 3256
wolfSSL 0:1239e9b70ca2 3257 if (GetLength(input, &idx, &strLen, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3258 CYASSL_MSG("\tfailed: str len");
wolfSSL 0:1239e9b70ca2 3259 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3260 }
wolfSSL 0:1239e9b70ca2 3261
wolfSSL 0:1239e9b70ca2 3262 cert->hwSerialNum = (byte*)XMALLOC(strLen + 1, cert->heap, 0);
wolfSSL 0:1239e9b70ca2 3263 if (cert->hwSerialNum == NULL) {
wolfSSL 0:1239e9b70ca2 3264 CYASSL_MSG("\tOut of Memory");
wolfSSL 0:1239e9b70ca2 3265 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 3266 }
wolfSSL 0:1239e9b70ca2 3267
wolfSSL 0:1239e9b70ca2 3268 XMEMCPY(cert->hwSerialNum, &input[idx], strLen);
wolfSSL 0:1239e9b70ca2 3269 cert->hwSerialNum[strLen] = '\0';
wolfSSL 0:1239e9b70ca2 3270 cert->hwSerialNumSz = strLen;
wolfSSL 0:1239e9b70ca2 3271 idx += strLen;
wolfSSL 0:1239e9b70ca2 3272 }
wolfSSL 0:1239e9b70ca2 3273 #endif /* CYASSL_SEP */
wolfSSL 0:1239e9b70ca2 3274 else {
wolfSSL 0:1239e9b70ca2 3275 int strLen;
wolfSSL 0:1239e9b70ca2 3276 word32 lenStartIdx = idx;
wolfSSL 0:1239e9b70ca2 3277
wolfSSL 0:1239e9b70ca2 3278 CYASSL_MSG("\tUnsupported name type, skipping");
wolfSSL 0:1239e9b70ca2 3279
wolfSSL 0:1239e9b70ca2 3280 if (GetLength(input, &idx, &strLen, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3281 CYASSL_MSG("\tfail: unsupported name length");
wolfSSL 0:1239e9b70ca2 3282 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3283 }
wolfSSL 0:1239e9b70ca2 3284 length -= (strLen + idx - lenStartIdx);
wolfSSL 0:1239e9b70ca2 3285 idx += strLen;
wolfSSL 0:1239e9b70ca2 3286 }
wolfSSL 0:1239e9b70ca2 3287 }
wolfSSL 0:1239e9b70ca2 3288 return 0;
wolfSSL 0:1239e9b70ca2 3289 }
wolfSSL 0:1239e9b70ca2 3290
wolfSSL 0:1239e9b70ca2 3291
wolfSSL 0:1239e9b70ca2 3292 static int DecodeBasicCaConstraint(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3293 {
wolfSSL 0:1239e9b70ca2 3294 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3295 int length = 0;
wolfSSL 0:1239e9b70ca2 3296
wolfSSL 0:1239e9b70ca2 3297 CYASSL_ENTER("DecodeBasicCaConstraint");
wolfSSL 0:1239e9b70ca2 3298 if (GetSequence(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3299 CYASSL_MSG("\tfail: bad SEQUENCE");
wolfSSL 0:1239e9b70ca2 3300 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3301 }
wolfSSL 0:1239e9b70ca2 3302
wolfSSL 0:1239e9b70ca2 3303 if (length == 0)
wolfSSL 0:1239e9b70ca2 3304 return 0;
wolfSSL 0:1239e9b70ca2 3305
wolfSSL 0:1239e9b70ca2 3306 /* If the basic ca constraint is false, this extension may be named, but
wolfSSL 0:1239e9b70ca2 3307 * left empty. So, if the length is 0, just return. */
wolfSSL 0:1239e9b70ca2 3308
wolfSSL 0:1239e9b70ca2 3309 if (input[idx++] != ASN_BOOLEAN)
wolfSSL 0:1239e9b70ca2 3310 {
wolfSSL 0:1239e9b70ca2 3311 CYASSL_MSG("\tfail: constraint not BOOLEAN");
wolfSSL 0:1239e9b70ca2 3312 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3313 }
wolfSSL 0:1239e9b70ca2 3314
wolfSSL 0:1239e9b70ca2 3315 if (GetLength(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3316 {
wolfSSL 0:1239e9b70ca2 3317 CYASSL_MSG("\tfail: length");
wolfSSL 0:1239e9b70ca2 3318 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3319 }
wolfSSL 0:1239e9b70ca2 3320
wolfSSL 0:1239e9b70ca2 3321 if (input[idx++])
wolfSSL 0:1239e9b70ca2 3322 cert->isCA = 1;
wolfSSL 0:1239e9b70ca2 3323
wolfSSL 0:1239e9b70ca2 3324 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3325 /* If there isn't any more data, return. */
wolfSSL 0:1239e9b70ca2 3326 if (idx >= (word32)sz)
wolfSSL 0:1239e9b70ca2 3327 return 0;
wolfSSL 0:1239e9b70ca2 3328
wolfSSL 0:1239e9b70ca2 3329 /* Anything left should be the optional pathlength */
wolfSSL 0:1239e9b70ca2 3330 if (input[idx++] != ASN_INTEGER) {
wolfSSL 0:1239e9b70ca2 3331 CYASSL_MSG("\tfail: pathlen not INTEGER");
wolfSSL 0:1239e9b70ca2 3332 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3333 }
wolfSSL 0:1239e9b70ca2 3334
wolfSSL 0:1239e9b70ca2 3335 if (input[idx++] != 1) {
wolfSSL 0:1239e9b70ca2 3336 CYASSL_MSG("\tfail: pathlen too long");
wolfSSL 0:1239e9b70ca2 3337 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3338 }
wolfSSL 0:1239e9b70ca2 3339
wolfSSL 0:1239e9b70ca2 3340 cert->pathLength = input[idx];
wolfSSL 0:1239e9b70ca2 3341 cert->extBasicConstPlSet = 1;
wolfSSL 0:1239e9b70ca2 3342 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 3343
wolfSSL 0:1239e9b70ca2 3344 return 0;
wolfSSL 0:1239e9b70ca2 3345 }
wolfSSL 0:1239e9b70ca2 3346
wolfSSL 0:1239e9b70ca2 3347
wolfSSL 0:1239e9b70ca2 3348 #define CRLDP_FULL_NAME 0
wolfSSL 0:1239e9b70ca2 3349 /* From RFC3280 SS4.2.1.14, Distribution Point Name*/
wolfSSL 0:1239e9b70ca2 3350 #define GENERALNAME_URI 6
wolfSSL 0:1239e9b70ca2 3351 /* From RFC3280 SS4.2.1.7, GeneralName */
wolfSSL 0:1239e9b70ca2 3352
wolfSSL 0:1239e9b70ca2 3353 static int DecodeCrlDist(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3354 {
wolfSSL 0:1239e9b70ca2 3355 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3356 int length = 0;
wolfSSL 0:1239e9b70ca2 3357
wolfSSL 0:1239e9b70ca2 3358 CYASSL_ENTER("DecodeCrlDist");
wolfSSL 0:1239e9b70ca2 3359
wolfSSL 0:1239e9b70ca2 3360 /* Unwrap the list of Distribution Points*/
wolfSSL 0:1239e9b70ca2 3361 if (GetSequence(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3362 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3363
wolfSSL 0:1239e9b70ca2 3364 /* Unwrap a single Distribution Point */
wolfSSL 0:1239e9b70ca2 3365 if (GetSequence(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3366 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3367
wolfSSL 0:1239e9b70ca2 3368 /* The Distribution Point has three explicit optional members
wolfSSL 0:1239e9b70ca2 3369 * First check for a DistributionPointName
wolfSSL 0:1239e9b70ca2 3370 */
wolfSSL 0:1239e9b70ca2 3371 if (input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0))
wolfSSL 0:1239e9b70ca2 3372 {
wolfSSL 0:1239e9b70ca2 3373 idx++;
wolfSSL 0:1239e9b70ca2 3374 if (GetLength(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3375 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3376
wolfSSL 0:1239e9b70ca2 3377 if (input[idx] ==
wolfSSL 0:1239e9b70ca2 3378 (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | CRLDP_FULL_NAME))
wolfSSL 0:1239e9b70ca2 3379 {
wolfSSL 0:1239e9b70ca2 3380 idx++;
wolfSSL 0:1239e9b70ca2 3381 if (GetLength(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3382 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3383
wolfSSL 0:1239e9b70ca2 3384 if (input[idx] == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI))
wolfSSL 0:1239e9b70ca2 3385 {
wolfSSL 0:1239e9b70ca2 3386 idx++;
wolfSSL 0:1239e9b70ca2 3387 if (GetLength(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3388 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3389
wolfSSL 0:1239e9b70ca2 3390 cert->extCrlInfoSz = length;
wolfSSL 0:1239e9b70ca2 3391 cert->extCrlInfo = input + idx;
wolfSSL 0:1239e9b70ca2 3392 idx += length;
wolfSSL 0:1239e9b70ca2 3393 }
wolfSSL 0:1239e9b70ca2 3394 else
wolfSSL 0:1239e9b70ca2 3395 /* This isn't a URI, skip it. */
wolfSSL 0:1239e9b70ca2 3396 idx += length;
wolfSSL 0:1239e9b70ca2 3397 }
wolfSSL 0:1239e9b70ca2 3398 else
wolfSSL 0:1239e9b70ca2 3399 /* This isn't a FULLNAME, skip it. */
wolfSSL 0:1239e9b70ca2 3400 idx += length;
wolfSSL 0:1239e9b70ca2 3401 }
wolfSSL 0:1239e9b70ca2 3402
wolfSSL 0:1239e9b70ca2 3403 /* Check for reasonFlags */
wolfSSL 0:1239e9b70ca2 3404 if (idx < (word32)sz &&
wolfSSL 0:1239e9b70ca2 3405 input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1))
wolfSSL 0:1239e9b70ca2 3406 {
wolfSSL 0:1239e9b70ca2 3407 idx++;
wolfSSL 0:1239e9b70ca2 3408 if (GetLength(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3409 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3410 idx += length;
wolfSSL 0:1239e9b70ca2 3411 }
wolfSSL 0:1239e9b70ca2 3412
wolfSSL 0:1239e9b70ca2 3413 /* Check for cRLIssuer */
wolfSSL 0:1239e9b70ca2 3414 if (idx < (word32)sz &&
wolfSSL 0:1239e9b70ca2 3415 input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 2))
wolfSSL 0:1239e9b70ca2 3416 {
wolfSSL 0:1239e9b70ca2 3417 idx++;
wolfSSL 0:1239e9b70ca2 3418 if (GetLength(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3419 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3420 idx += length;
wolfSSL 0:1239e9b70ca2 3421 }
wolfSSL 0:1239e9b70ca2 3422
wolfSSL 0:1239e9b70ca2 3423 if (idx < (word32)sz)
wolfSSL 0:1239e9b70ca2 3424 {
wolfSSL 0:1239e9b70ca2 3425 CYASSL_MSG("\tThere are more CRL Distribution Point records, "
wolfSSL 0:1239e9b70ca2 3426 "but we only use the first one.");
wolfSSL 0:1239e9b70ca2 3427 }
wolfSSL 0:1239e9b70ca2 3428
wolfSSL 0:1239e9b70ca2 3429 return 0;
wolfSSL 0:1239e9b70ca2 3430 }
wolfSSL 0:1239e9b70ca2 3431
wolfSSL 0:1239e9b70ca2 3432
wolfSSL 0:1239e9b70ca2 3433 static int DecodeAuthInfo(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3434 /*
wolfSSL 0:1239e9b70ca2 3435 * Read the first of the Authority Information Access records. If there are
wolfSSL 0:1239e9b70ca2 3436 * any issues, return without saving the record.
wolfSSL 0:1239e9b70ca2 3437 */
wolfSSL 0:1239e9b70ca2 3438 {
wolfSSL 0:1239e9b70ca2 3439 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3440 int length = 0;
wolfSSL 0:1239e9b70ca2 3441 byte b;
wolfSSL 0:1239e9b70ca2 3442 word32 oid;
wolfSSL 0:1239e9b70ca2 3443
wolfSSL 0:1239e9b70ca2 3444 CYASSL_ENTER("DecodeAuthInfo");
wolfSSL 0:1239e9b70ca2 3445
wolfSSL 0:1239e9b70ca2 3446 /* Unwrap the list of AIAs */
wolfSSL 0:1239e9b70ca2 3447 if (GetSequence(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3448 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3449
wolfSSL 0:1239e9b70ca2 3450 while (idx < (word32)sz) {
wolfSSL 0:1239e9b70ca2 3451 /* Unwrap a single AIA */
wolfSSL 0:1239e9b70ca2 3452 if (GetSequence(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3453 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3454
wolfSSL 0:1239e9b70ca2 3455 oid = 0;
wolfSSL 0:1239e9b70ca2 3456 if (GetObjectId(input, &idx, &oid, sz) < 0)
wolfSSL 0:1239e9b70ca2 3457 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3458
wolfSSL 0:1239e9b70ca2 3459 /* Only supporting URIs right now. */
wolfSSL 0:1239e9b70ca2 3460 b = input[idx++];
wolfSSL 0:1239e9b70ca2 3461 if (GetLength(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3462 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3463
wolfSSL 0:1239e9b70ca2 3464 if (b == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI) &&
wolfSSL 0:1239e9b70ca2 3465 oid == AIA_OCSP_OID)
wolfSSL 0:1239e9b70ca2 3466 {
wolfSSL 0:1239e9b70ca2 3467 cert->extAuthInfoSz = length;
wolfSSL 0:1239e9b70ca2 3468 cert->extAuthInfo = input + idx;
wolfSSL 0:1239e9b70ca2 3469 break;
wolfSSL 0:1239e9b70ca2 3470 }
wolfSSL 0:1239e9b70ca2 3471 idx += length;
wolfSSL 0:1239e9b70ca2 3472 }
wolfSSL 0:1239e9b70ca2 3473
wolfSSL 0:1239e9b70ca2 3474 return 0;
wolfSSL 0:1239e9b70ca2 3475 }
wolfSSL 0:1239e9b70ca2 3476
wolfSSL 0:1239e9b70ca2 3477
wolfSSL 0:1239e9b70ca2 3478 static int DecodeAuthKeyId(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3479 {
wolfSSL 0:1239e9b70ca2 3480 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3481 int length = 0, ret = 0;
wolfSSL 0:1239e9b70ca2 3482
wolfSSL 0:1239e9b70ca2 3483 CYASSL_ENTER("DecodeAuthKeyId");
wolfSSL 0:1239e9b70ca2 3484
wolfSSL 0:1239e9b70ca2 3485 if (GetSequence(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3486 CYASSL_MSG("\tfail: should be a SEQUENCE\n");
wolfSSL 0:1239e9b70ca2 3487 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3488 }
wolfSSL 0:1239e9b70ca2 3489
wolfSSL 0:1239e9b70ca2 3490 if (input[idx++] != (ASN_CONTEXT_SPECIFIC | 0)) {
wolfSSL 0:1239e9b70ca2 3491 CYASSL_MSG("\tfail: wanted OPTIONAL item 0, not available\n");
wolfSSL 0:1239e9b70ca2 3492 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3493 }
wolfSSL 0:1239e9b70ca2 3494
wolfSSL 0:1239e9b70ca2 3495 if (GetLength(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3496 CYASSL_MSG("\tfail: extension data length");
wolfSSL 0:1239e9b70ca2 3497 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3498 }
wolfSSL 0:1239e9b70ca2 3499
wolfSSL 0:1239e9b70ca2 3500 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3501 cert->extAuthKeyIdSrc = &input[idx];
wolfSSL 0:1239e9b70ca2 3502 cert->extAuthKeyIdSz = length;
wolfSSL 0:1239e9b70ca2 3503 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 3504
wolfSSL 0:1239e9b70ca2 3505 if (length == SHA_SIZE) {
wolfSSL 0:1239e9b70ca2 3506 XMEMCPY(cert->extAuthKeyId, input + idx, length);
wolfSSL 0:1239e9b70ca2 3507 }
wolfSSL 0:1239e9b70ca2 3508 else {
wolfSSL 0:1239e9b70ca2 3509 Sha sha;
wolfSSL 0:1239e9b70ca2 3510 ret = InitSha(&sha);
wolfSSL 0:1239e9b70ca2 3511 if (ret != 0)
wolfSSL 0:1239e9b70ca2 3512 return ret;
wolfSSL 0:1239e9b70ca2 3513 ShaUpdate(&sha, input + idx, length);
wolfSSL 0:1239e9b70ca2 3514 ShaFinal(&sha, cert->extAuthKeyId);
wolfSSL 0:1239e9b70ca2 3515 }
wolfSSL 0:1239e9b70ca2 3516
wolfSSL 0:1239e9b70ca2 3517 return 0;
wolfSSL 0:1239e9b70ca2 3518 }
wolfSSL 0:1239e9b70ca2 3519
wolfSSL 0:1239e9b70ca2 3520
wolfSSL 0:1239e9b70ca2 3521 static int DecodeSubjKeyId(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3522 {
wolfSSL 0:1239e9b70ca2 3523 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3524 int length = 0, ret = 0;
wolfSSL 0:1239e9b70ca2 3525
wolfSSL 0:1239e9b70ca2 3526 CYASSL_ENTER("DecodeSubjKeyId");
wolfSSL 0:1239e9b70ca2 3527
wolfSSL 0:1239e9b70ca2 3528 if (input[idx++] != ASN_OCTET_STRING) {
wolfSSL 0:1239e9b70ca2 3529 CYASSL_MSG("\tfail: should be an OCTET STRING");
wolfSSL 0:1239e9b70ca2 3530 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3531 }
wolfSSL 0:1239e9b70ca2 3532
wolfSSL 0:1239e9b70ca2 3533 if (GetLength(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3534 CYASSL_MSG("\tfail: extension data length");
wolfSSL 0:1239e9b70ca2 3535 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3536 }
wolfSSL 0:1239e9b70ca2 3537
wolfSSL 0:1239e9b70ca2 3538 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3539 cert->extSubjKeyIdSrc = &input[idx];
wolfSSL 0:1239e9b70ca2 3540 cert->extSubjKeyIdSz = length;
wolfSSL 0:1239e9b70ca2 3541 #endif /* OPENSSL_EXTRA */
wolfSSL 0:1239e9b70ca2 3542
wolfSSL 0:1239e9b70ca2 3543 if (length == SIGNER_DIGEST_SIZE) {
wolfSSL 0:1239e9b70ca2 3544 XMEMCPY(cert->extSubjKeyId, input + idx, length);
wolfSSL 0:1239e9b70ca2 3545 }
wolfSSL 0:1239e9b70ca2 3546 else {
wolfSSL 0:1239e9b70ca2 3547 Sha sha;
wolfSSL 0:1239e9b70ca2 3548 ret = InitSha(&sha);
wolfSSL 0:1239e9b70ca2 3549 if (ret != 0)
wolfSSL 0:1239e9b70ca2 3550 return ret;
wolfSSL 0:1239e9b70ca2 3551 ShaUpdate(&sha, input + idx, length);
wolfSSL 0:1239e9b70ca2 3552 ShaFinal(&sha, cert->extSubjKeyId);
wolfSSL 0:1239e9b70ca2 3553 }
wolfSSL 0:1239e9b70ca2 3554
wolfSSL 0:1239e9b70ca2 3555 return ret;
wolfSSL 0:1239e9b70ca2 3556 }
wolfSSL 0:1239e9b70ca2 3557
wolfSSL 0:1239e9b70ca2 3558
wolfSSL 0:1239e9b70ca2 3559 static int DecodeKeyUsage(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3560 {
wolfSSL 0:1239e9b70ca2 3561 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3562 int length;
wolfSSL 0:1239e9b70ca2 3563 byte unusedBits;
wolfSSL 0:1239e9b70ca2 3564 CYASSL_ENTER("DecodeKeyUsage");
wolfSSL 0:1239e9b70ca2 3565
wolfSSL 0:1239e9b70ca2 3566 if (input[idx++] != ASN_BIT_STRING) {
wolfSSL 0:1239e9b70ca2 3567 CYASSL_MSG("\tfail: key usage expected bit string");
wolfSSL 0:1239e9b70ca2 3568 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3569 }
wolfSSL 0:1239e9b70ca2 3570
wolfSSL 0:1239e9b70ca2 3571 if (GetLength(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3572 CYASSL_MSG("\tfail: key usage bad length");
wolfSSL 0:1239e9b70ca2 3573 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3574 }
wolfSSL 0:1239e9b70ca2 3575
wolfSSL 0:1239e9b70ca2 3576 unusedBits = input[idx++];
wolfSSL 0:1239e9b70ca2 3577 length--;
wolfSSL 0:1239e9b70ca2 3578
wolfSSL 0:1239e9b70ca2 3579 if (length == 2) {
wolfSSL 0:1239e9b70ca2 3580 cert->extKeyUsage = (word16)((input[idx] << 8) | input[idx+1]);
wolfSSL 0:1239e9b70ca2 3581 cert->extKeyUsage >>= unusedBits;
wolfSSL 0:1239e9b70ca2 3582 }
wolfSSL 0:1239e9b70ca2 3583 else if (length == 1)
wolfSSL 0:1239e9b70ca2 3584 cert->extKeyUsage = (word16)(input[idx] << 1);
wolfSSL 0:1239e9b70ca2 3585
wolfSSL 0:1239e9b70ca2 3586 return 0;
wolfSSL 0:1239e9b70ca2 3587 }
wolfSSL 0:1239e9b70ca2 3588
wolfSSL 0:1239e9b70ca2 3589
wolfSSL 0:1239e9b70ca2 3590 static int DecodeExtKeyUsage(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3591 {
wolfSSL 0:1239e9b70ca2 3592 word32 idx = 0, oid;
wolfSSL 0:1239e9b70ca2 3593 int length;
wolfSSL 0:1239e9b70ca2 3594
wolfSSL 0:1239e9b70ca2 3595 CYASSL_ENTER("DecodeExtKeyUsage");
wolfSSL 0:1239e9b70ca2 3596
wolfSSL 0:1239e9b70ca2 3597 if (GetSequence(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3598 CYASSL_MSG("\tfail: should be a SEQUENCE");
wolfSSL 0:1239e9b70ca2 3599 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3600 }
wolfSSL 0:1239e9b70ca2 3601
wolfSSL 0:1239e9b70ca2 3602 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3603 cert->extExtKeyUsageSrc = input + idx;
wolfSSL 0:1239e9b70ca2 3604 cert->extExtKeyUsageSz = length;
wolfSSL 0:1239e9b70ca2 3605 #endif
wolfSSL 0:1239e9b70ca2 3606
wolfSSL 0:1239e9b70ca2 3607 while (idx < (word32)sz) {
wolfSSL 0:1239e9b70ca2 3608 if (GetObjectId(input, &idx, &oid, sz) < 0)
wolfSSL 0:1239e9b70ca2 3609 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3610
wolfSSL 0:1239e9b70ca2 3611 switch (oid) {
wolfSSL 0:1239e9b70ca2 3612 case EKU_ANY_OID:
wolfSSL 0:1239e9b70ca2 3613 cert->extExtKeyUsage |= EXTKEYUSE_ANY;
wolfSSL 0:1239e9b70ca2 3614 break;
wolfSSL 0:1239e9b70ca2 3615 case EKU_SERVER_AUTH_OID:
wolfSSL 0:1239e9b70ca2 3616 cert->extExtKeyUsage |= EXTKEYUSE_SERVER_AUTH;
wolfSSL 0:1239e9b70ca2 3617 break;
wolfSSL 0:1239e9b70ca2 3618 case EKU_CLIENT_AUTH_OID:
wolfSSL 0:1239e9b70ca2 3619 cert->extExtKeyUsage |= EXTKEYUSE_CLIENT_AUTH;
wolfSSL 0:1239e9b70ca2 3620 break;
wolfSSL 0:1239e9b70ca2 3621 case EKU_OCSP_SIGN_OID:
wolfSSL 0:1239e9b70ca2 3622 cert->extExtKeyUsage |= EXTKEYUSE_OCSP_SIGN;
wolfSSL 0:1239e9b70ca2 3623 break;
wolfSSL 0:1239e9b70ca2 3624 }
wolfSSL 0:1239e9b70ca2 3625
wolfSSL 0:1239e9b70ca2 3626 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3627 cert->extExtKeyUsageCount++;
wolfSSL 0:1239e9b70ca2 3628 #endif
wolfSSL 0:1239e9b70ca2 3629 }
wolfSSL 0:1239e9b70ca2 3630
wolfSSL 0:1239e9b70ca2 3631 return 0;
wolfSSL 0:1239e9b70ca2 3632 }
wolfSSL 0:1239e9b70ca2 3633
wolfSSL 0:1239e9b70ca2 3634
wolfSSL 0:1239e9b70ca2 3635 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 3636 static int DecodeSubtree(byte* input, int sz, Base_entry** head, void* heap)
wolfSSL 0:1239e9b70ca2 3637 {
wolfSSL 0:1239e9b70ca2 3638 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3639
wolfSSL 0:1239e9b70ca2 3640 (void)heap;
wolfSSL 0:1239e9b70ca2 3641
wolfSSL 0:1239e9b70ca2 3642 while (idx < (word32)sz) {
wolfSSL 0:1239e9b70ca2 3643 int seqLength, strLength;
wolfSSL 0:1239e9b70ca2 3644 word32 nameIdx;
wolfSSL 0:1239e9b70ca2 3645 byte b;
wolfSSL 0:1239e9b70ca2 3646
wolfSSL 0:1239e9b70ca2 3647 if (GetSequence(input, &idx, &seqLength, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3648 CYASSL_MSG("\tfail: should be a SEQUENCE");
wolfSSL 0:1239e9b70ca2 3649 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3650 }
wolfSSL 0:1239e9b70ca2 3651
wolfSSL 0:1239e9b70ca2 3652 nameIdx = idx;
wolfSSL 0:1239e9b70ca2 3653 b = input[nameIdx++];
wolfSSL 0:1239e9b70ca2 3654 if (GetLength(input, &nameIdx, &strLength, sz) <= 0) {
wolfSSL 0:1239e9b70ca2 3655 CYASSL_MSG("\tinvalid length");
wolfSSL 0:1239e9b70ca2 3656 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3657 }
wolfSSL 0:1239e9b70ca2 3658
wolfSSL 0:1239e9b70ca2 3659 if (b == (ASN_CONTEXT_SPECIFIC | ASN_DNS_TYPE) ||
wolfSSL 0:1239e9b70ca2 3660 b == (ASN_CONTEXT_SPECIFIC | ASN_RFC822_TYPE) ||
wolfSSL 0:1239e9b70ca2 3661 b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_DIR_TYPE)) {
wolfSSL 0:1239e9b70ca2 3662
wolfSSL 0:1239e9b70ca2 3663 Base_entry* entry = (Base_entry*)XMALLOC(sizeof(Base_entry),
wolfSSL 0:1239e9b70ca2 3664 heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 3665
wolfSSL 0:1239e9b70ca2 3666 if (entry == NULL) {
wolfSSL 0:1239e9b70ca2 3667 CYASSL_MSG("allocate error");
wolfSSL 0:1239e9b70ca2 3668 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 3669 }
wolfSSL 0:1239e9b70ca2 3670
wolfSSL 0:1239e9b70ca2 3671 entry->name = (char*)XMALLOC(strLength, heap, DYNAMIC_TYPE_ALTNAME);
wolfSSL 0:1239e9b70ca2 3672 if (entry->name == NULL) {
wolfSSL 0:1239e9b70ca2 3673 CYASSL_MSG("allocate error");
wolfSSL 0:1239e9b70ca2 3674 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 3675 }
wolfSSL 0:1239e9b70ca2 3676
wolfSSL 0:1239e9b70ca2 3677 XMEMCPY(entry->name, &input[nameIdx], strLength);
wolfSSL 0:1239e9b70ca2 3678 entry->nameSz = strLength;
wolfSSL 0:1239e9b70ca2 3679 entry->type = b & 0x0F;
wolfSSL 0:1239e9b70ca2 3680
wolfSSL 0:1239e9b70ca2 3681 entry->next = *head;
wolfSSL 0:1239e9b70ca2 3682 *head = entry;
wolfSSL 0:1239e9b70ca2 3683 }
wolfSSL 0:1239e9b70ca2 3684
wolfSSL 0:1239e9b70ca2 3685 idx += seqLength;
wolfSSL 0:1239e9b70ca2 3686 }
wolfSSL 0:1239e9b70ca2 3687
wolfSSL 0:1239e9b70ca2 3688 return 0;
wolfSSL 0:1239e9b70ca2 3689 }
wolfSSL 0:1239e9b70ca2 3690
wolfSSL 0:1239e9b70ca2 3691
wolfSSL 0:1239e9b70ca2 3692 static int DecodeNameConstraints(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3693 {
wolfSSL 0:1239e9b70ca2 3694 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3695 int length = 0;
wolfSSL 0:1239e9b70ca2 3696
wolfSSL 0:1239e9b70ca2 3697 CYASSL_ENTER("DecodeNameConstraints");
wolfSSL 0:1239e9b70ca2 3698
wolfSSL 0:1239e9b70ca2 3699 if (GetSequence(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3700 CYASSL_MSG("\tfail: should be a SEQUENCE");
wolfSSL 0:1239e9b70ca2 3701 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3702 }
wolfSSL 0:1239e9b70ca2 3703
wolfSSL 0:1239e9b70ca2 3704 while (idx < (word32)sz) {
wolfSSL 0:1239e9b70ca2 3705 byte b = input[idx++];
wolfSSL 0:1239e9b70ca2 3706 Base_entry** subtree = NULL;
wolfSSL 0:1239e9b70ca2 3707
wolfSSL 0:1239e9b70ca2 3708 if (GetLength(input, &idx, &length, sz) <= 0) {
wolfSSL 0:1239e9b70ca2 3709 CYASSL_MSG("\tinvalid length");
wolfSSL 0:1239e9b70ca2 3710 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3711 }
wolfSSL 0:1239e9b70ca2 3712
wolfSSL 0:1239e9b70ca2 3713 if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0))
wolfSSL 0:1239e9b70ca2 3714 subtree = &cert->permittedNames;
wolfSSL 0:1239e9b70ca2 3715 else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1))
wolfSSL 0:1239e9b70ca2 3716 subtree = &cert->excludedNames;
wolfSSL 0:1239e9b70ca2 3717 else {
wolfSSL 0:1239e9b70ca2 3718 CYASSL_MSG("\tinvalid subtree");
wolfSSL 0:1239e9b70ca2 3719 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3720 }
wolfSSL 0:1239e9b70ca2 3721
wolfSSL 0:1239e9b70ca2 3722 DecodeSubtree(input + idx, length, subtree, cert->heap);
wolfSSL 0:1239e9b70ca2 3723
wolfSSL 0:1239e9b70ca2 3724 idx += length;
wolfSSL 0:1239e9b70ca2 3725 }
wolfSSL 0:1239e9b70ca2 3726
wolfSSL 0:1239e9b70ca2 3727 return 0;
wolfSSL 0:1239e9b70ca2 3728 }
wolfSSL 0:1239e9b70ca2 3729 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 3730
wolfSSL 0:1239e9b70ca2 3731
wolfSSL 0:1239e9b70ca2 3732 #ifdef CYASSL_SEP
wolfSSL 0:1239e9b70ca2 3733 static int DecodeCertPolicy(byte* input, int sz, DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3734 {
wolfSSL 0:1239e9b70ca2 3735 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3736 int length = 0;
wolfSSL 0:1239e9b70ca2 3737
wolfSSL 0:1239e9b70ca2 3738 CYASSL_ENTER("DecodeCertPolicy");
wolfSSL 0:1239e9b70ca2 3739
wolfSSL 0:1239e9b70ca2 3740 /* Unwrap certificatePolicies */
wolfSSL 0:1239e9b70ca2 3741 if (GetSequence(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3742 CYASSL_MSG("\tdeviceType isn't OID");
wolfSSL 0:1239e9b70ca2 3743 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3744 }
wolfSSL 0:1239e9b70ca2 3745
wolfSSL 0:1239e9b70ca2 3746 if (GetSequence(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3747 CYASSL_MSG("\tdeviceType isn't OID");
wolfSSL 0:1239e9b70ca2 3748 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3749 }
wolfSSL 0:1239e9b70ca2 3750
wolfSSL 0:1239e9b70ca2 3751 if (input[idx++] != ASN_OBJECT_ID) {
wolfSSL 0:1239e9b70ca2 3752 CYASSL_MSG("\tdeviceType isn't OID");
wolfSSL 0:1239e9b70ca2 3753 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3754 }
wolfSSL 0:1239e9b70ca2 3755
wolfSSL 0:1239e9b70ca2 3756 if (GetLength(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3757 CYASSL_MSG("\tCouldn't read length of deviceType");
wolfSSL 0:1239e9b70ca2 3758 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3759 }
wolfSSL 0:1239e9b70ca2 3760
wolfSSL 0:1239e9b70ca2 3761 if (length > 0) {
wolfSSL 0:1239e9b70ca2 3762 cert->deviceType = (byte*)XMALLOC(length, cert->heap, 0);
wolfSSL 0:1239e9b70ca2 3763 if (cert->deviceType == NULL) {
wolfSSL 0:1239e9b70ca2 3764 CYASSL_MSG("\tCouldn't alloc memory for deviceType");
wolfSSL 0:1239e9b70ca2 3765 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 3766 }
wolfSSL 0:1239e9b70ca2 3767 cert->deviceTypeSz = length;
wolfSSL 0:1239e9b70ca2 3768 XMEMCPY(cert->deviceType, input + idx, length);
wolfSSL 0:1239e9b70ca2 3769 }
wolfSSL 0:1239e9b70ca2 3770
wolfSSL 0:1239e9b70ca2 3771 CYASSL_LEAVE("DecodeCertPolicy", 0);
wolfSSL 0:1239e9b70ca2 3772 return 0;
wolfSSL 0:1239e9b70ca2 3773 }
wolfSSL 0:1239e9b70ca2 3774 #endif /* CYASSL_SEP */
wolfSSL 0:1239e9b70ca2 3775
wolfSSL 0:1239e9b70ca2 3776
wolfSSL 0:1239e9b70ca2 3777 static int DecodeCertExtensions(DecodedCert* cert)
wolfSSL 0:1239e9b70ca2 3778 /*
wolfSSL 0:1239e9b70ca2 3779 * Processing the Certificate Extensions. This does not modify the current
wolfSSL 0:1239e9b70ca2 3780 * index. It is works starting with the recorded extensions pointer.
wolfSSL 0:1239e9b70ca2 3781 */
wolfSSL 0:1239e9b70ca2 3782 {
wolfSSL 0:1239e9b70ca2 3783 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 3784 int sz = cert->extensionsSz;
wolfSSL 0:1239e9b70ca2 3785 byte* input = cert->extensions;
wolfSSL 0:1239e9b70ca2 3786 int length;
wolfSSL 0:1239e9b70ca2 3787 word32 oid;
wolfSSL 0:1239e9b70ca2 3788 byte critical = 0;
wolfSSL 0:1239e9b70ca2 3789 byte criticalFail = 0;
wolfSSL 0:1239e9b70ca2 3790
wolfSSL 0:1239e9b70ca2 3791 CYASSL_ENTER("DecodeCertExtensions");
wolfSSL 0:1239e9b70ca2 3792
wolfSSL 0:1239e9b70ca2 3793 if (input == NULL || sz == 0)
wolfSSL 0:1239e9b70ca2 3794 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 3795
wolfSSL 0:1239e9b70ca2 3796 if (input[idx++] != ASN_EXTENSIONS)
wolfSSL 0:1239e9b70ca2 3797 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3798
wolfSSL 0:1239e9b70ca2 3799 if (GetLength(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3800 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3801
wolfSSL 0:1239e9b70ca2 3802 if (GetSequence(input, &idx, &length, sz) < 0)
wolfSSL 0:1239e9b70ca2 3803 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3804
wolfSSL 0:1239e9b70ca2 3805 while (idx < (word32)sz) {
wolfSSL 0:1239e9b70ca2 3806 if (GetSequence(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3807 CYASSL_MSG("\tfail: should be a SEQUENCE");
wolfSSL 0:1239e9b70ca2 3808 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3809 }
wolfSSL 0:1239e9b70ca2 3810
wolfSSL 0:1239e9b70ca2 3811 oid = 0;
wolfSSL 0:1239e9b70ca2 3812 if (GetObjectId(input, &idx, &oid, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3813 CYASSL_MSG("\tfail: OBJECT ID");
wolfSSL 0:1239e9b70ca2 3814 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3815 }
wolfSSL 0:1239e9b70ca2 3816
wolfSSL 0:1239e9b70ca2 3817 /* check for critical flag */
wolfSSL 0:1239e9b70ca2 3818 critical = 0;
wolfSSL 0:1239e9b70ca2 3819 if (input[idx] == ASN_BOOLEAN) {
wolfSSL 0:1239e9b70ca2 3820 int boolLength = 0;
wolfSSL 0:1239e9b70ca2 3821 idx++;
wolfSSL 0:1239e9b70ca2 3822 if (GetLength(input, &idx, &boolLength, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3823 CYASSL_MSG("\tfail: critical boolean length");
wolfSSL 0:1239e9b70ca2 3824 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3825 }
wolfSSL 0:1239e9b70ca2 3826 if (input[idx++])
wolfSSL 0:1239e9b70ca2 3827 critical = 1;
wolfSSL 0:1239e9b70ca2 3828 }
wolfSSL 0:1239e9b70ca2 3829
wolfSSL 0:1239e9b70ca2 3830 /* process the extension based on the OID */
wolfSSL 0:1239e9b70ca2 3831 if (input[idx++] != ASN_OCTET_STRING) {
wolfSSL 0:1239e9b70ca2 3832 CYASSL_MSG("\tfail: should be an OCTET STRING");
wolfSSL 0:1239e9b70ca2 3833 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3834 }
wolfSSL 0:1239e9b70ca2 3835
wolfSSL 0:1239e9b70ca2 3836 if (GetLength(input, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 3837 CYASSL_MSG("\tfail: extension data length");
wolfSSL 0:1239e9b70ca2 3838 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3839 }
wolfSSL 0:1239e9b70ca2 3840
wolfSSL 0:1239e9b70ca2 3841 switch (oid) {
wolfSSL 0:1239e9b70ca2 3842 case BASIC_CA_OID:
wolfSSL 0:1239e9b70ca2 3843 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3844 cert->extBasicConstSet = 1;
wolfSSL 0:1239e9b70ca2 3845 cert->extBasicConstCrit = critical;
wolfSSL 0:1239e9b70ca2 3846 #endif
wolfSSL 0:1239e9b70ca2 3847 if (DecodeBasicCaConstraint(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3848 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3849 break;
wolfSSL 0:1239e9b70ca2 3850
wolfSSL 0:1239e9b70ca2 3851 case CRL_DIST_OID:
wolfSSL 0:1239e9b70ca2 3852 if (DecodeCrlDist(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3853 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3854 break;
wolfSSL 0:1239e9b70ca2 3855
wolfSSL 0:1239e9b70ca2 3856 case AUTH_INFO_OID:
wolfSSL 0:1239e9b70ca2 3857 if (DecodeAuthInfo(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3858 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3859 break;
wolfSSL 0:1239e9b70ca2 3860
wolfSSL 0:1239e9b70ca2 3861 case ALT_NAMES_OID:
wolfSSL 0:1239e9b70ca2 3862 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3863 cert->extSubjAltNameSet = 1;
wolfSSL 0:1239e9b70ca2 3864 cert->extSubjAltNameCrit = critical;
wolfSSL 0:1239e9b70ca2 3865 #endif
wolfSSL 0:1239e9b70ca2 3866 if (DecodeAltNames(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3867 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3868 break;
wolfSSL 0:1239e9b70ca2 3869
wolfSSL 0:1239e9b70ca2 3870 case AUTH_KEY_OID:
wolfSSL 0:1239e9b70ca2 3871 cert->extAuthKeyIdSet = 1;
wolfSSL 0:1239e9b70ca2 3872 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3873 cert->extAuthKeyIdCrit = critical;
wolfSSL 0:1239e9b70ca2 3874 #endif
wolfSSL 0:1239e9b70ca2 3875 if (DecodeAuthKeyId(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3876 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3877 break;
wolfSSL 0:1239e9b70ca2 3878
wolfSSL 0:1239e9b70ca2 3879 case SUBJ_KEY_OID:
wolfSSL 0:1239e9b70ca2 3880 cert->extSubjKeyIdSet = 1;
wolfSSL 0:1239e9b70ca2 3881 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3882 cert->extSubjKeyIdCrit = critical;
wolfSSL 0:1239e9b70ca2 3883 #endif
wolfSSL 0:1239e9b70ca2 3884 if (DecodeSubjKeyId(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3885 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3886 break;
wolfSSL 0:1239e9b70ca2 3887
wolfSSL 0:1239e9b70ca2 3888 case CERT_POLICY_OID:
wolfSSL 0:1239e9b70ca2 3889 CYASSL_MSG("Certificate Policy extension not supported yet.");
wolfSSL 0:1239e9b70ca2 3890 #ifdef CYASSL_SEP
wolfSSL 0:1239e9b70ca2 3891 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3892 cert->extCertPolicySet = 1;
wolfSSL 0:1239e9b70ca2 3893 cert->extCertPolicyCrit = critical;
wolfSSL 0:1239e9b70ca2 3894 #endif
wolfSSL 0:1239e9b70ca2 3895 if (DecodeCertPolicy(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3896 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3897 #endif
wolfSSL 0:1239e9b70ca2 3898 break;
wolfSSL 0:1239e9b70ca2 3899
wolfSSL 0:1239e9b70ca2 3900 case KEY_USAGE_OID:
wolfSSL 0:1239e9b70ca2 3901 cert->extKeyUsageSet = 1;
wolfSSL 0:1239e9b70ca2 3902 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3903 cert->extKeyUsageCrit = critical;
wolfSSL 0:1239e9b70ca2 3904 #endif
wolfSSL 0:1239e9b70ca2 3905 if (DecodeKeyUsage(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3906 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3907 break;
wolfSSL 0:1239e9b70ca2 3908
wolfSSL 0:1239e9b70ca2 3909 case EXT_KEY_USAGE_OID:
wolfSSL 0:1239e9b70ca2 3910 cert->extExtKeyUsageSet = 1;
wolfSSL 0:1239e9b70ca2 3911 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3912 cert->extExtKeyUsageCrit = critical;
wolfSSL 0:1239e9b70ca2 3913 #endif
wolfSSL 0:1239e9b70ca2 3914 if (DecodeExtKeyUsage(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3915 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3916 break;
wolfSSL 0:1239e9b70ca2 3917
wolfSSL 0:1239e9b70ca2 3918 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 3919 case NAME_CONS_OID:
wolfSSL 0:1239e9b70ca2 3920 cert->extNameConstraintSet = 1;
wolfSSL 0:1239e9b70ca2 3921 #ifdef OPENSSL_EXTRA
wolfSSL 0:1239e9b70ca2 3922 cert->extNameConstraintCrit = critical;
wolfSSL 0:1239e9b70ca2 3923 #endif
wolfSSL 0:1239e9b70ca2 3924 if (DecodeNameConstraints(&input[idx], length, cert) < 0)
wolfSSL 0:1239e9b70ca2 3925 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 3926 break;
wolfSSL 0:1239e9b70ca2 3927 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 3928
wolfSSL 0:1239e9b70ca2 3929 case INHIBIT_ANY_OID:
wolfSSL 0:1239e9b70ca2 3930 CYASSL_MSG("Inhibit anyPolicy extension not supported yet.");
wolfSSL 0:1239e9b70ca2 3931 break;
wolfSSL 0:1239e9b70ca2 3932
wolfSSL 0:1239e9b70ca2 3933 default:
wolfSSL 0:1239e9b70ca2 3934 /* While it is a failure to not support critical extensions,
wolfSSL 0:1239e9b70ca2 3935 * still parse the certificate ignoring the unsupported
wolfSSL 0:1239e9b70ca2 3936 * extention to allow caller to accept it with the verify
wolfSSL 0:1239e9b70ca2 3937 * callback. */
wolfSSL 0:1239e9b70ca2 3938 if (critical)
wolfSSL 0:1239e9b70ca2 3939 criticalFail = 1;
wolfSSL 0:1239e9b70ca2 3940 break;
wolfSSL 0:1239e9b70ca2 3941 }
wolfSSL 0:1239e9b70ca2 3942 idx += length;
wolfSSL 0:1239e9b70ca2 3943 }
wolfSSL 0:1239e9b70ca2 3944
wolfSSL 0:1239e9b70ca2 3945 return criticalFail ? ASN_CRIT_EXT_E : 0;
wolfSSL 0:1239e9b70ca2 3946 }
wolfSSL 0:1239e9b70ca2 3947
wolfSSL 0:1239e9b70ca2 3948
wolfSSL 0:1239e9b70ca2 3949 int ParseCert(DecodedCert* cert, int type, int verify, void* cm)
wolfSSL 0:1239e9b70ca2 3950 {
wolfSSL 0:1239e9b70ca2 3951 int ret;
wolfSSL 0:1239e9b70ca2 3952 char* ptr;
wolfSSL 0:1239e9b70ca2 3953
wolfSSL 0:1239e9b70ca2 3954 ret = ParseCertRelative(cert, type, verify, cm);
wolfSSL 0:1239e9b70ca2 3955 if (ret < 0)
wolfSSL 0:1239e9b70ca2 3956 return ret;
wolfSSL 0:1239e9b70ca2 3957
wolfSSL 0:1239e9b70ca2 3958 if (cert->subjectCNLen > 0) {
wolfSSL 0:1239e9b70ca2 3959 ptr = (char*) XMALLOC(cert->subjectCNLen + 1, cert->heap,
wolfSSL 0:1239e9b70ca2 3960 DYNAMIC_TYPE_SUBJECT_CN);
wolfSSL 0:1239e9b70ca2 3961 if (ptr == NULL)
wolfSSL 0:1239e9b70ca2 3962 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 3963 XMEMCPY(ptr, cert->subjectCN, cert->subjectCNLen);
wolfSSL 0:1239e9b70ca2 3964 ptr[cert->subjectCNLen] = '\0';
wolfSSL 0:1239e9b70ca2 3965 cert->subjectCN = ptr;
wolfSSL 0:1239e9b70ca2 3966 cert->subjectCNStored = 1;
wolfSSL 0:1239e9b70ca2 3967 }
wolfSSL 0:1239e9b70ca2 3968
wolfSSL 0:1239e9b70ca2 3969 if (cert->keyOID == RSAk &&
wolfSSL 0:1239e9b70ca2 3970 cert->publicKey != NULL && cert->pubKeySize > 0) {
wolfSSL 0:1239e9b70ca2 3971 ptr = (char*) XMALLOC(cert->pubKeySize, cert->heap,
wolfSSL 0:1239e9b70ca2 3972 DYNAMIC_TYPE_PUBLIC_KEY);
wolfSSL 0:1239e9b70ca2 3973 if (ptr == NULL)
wolfSSL 0:1239e9b70ca2 3974 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 3975 XMEMCPY(ptr, cert->publicKey, cert->pubKeySize);
wolfSSL 0:1239e9b70ca2 3976 cert->publicKey = (byte *)ptr;
wolfSSL 0:1239e9b70ca2 3977 cert->pubKeyStored = 1;
wolfSSL 0:1239e9b70ca2 3978 }
wolfSSL 0:1239e9b70ca2 3979
wolfSSL 0:1239e9b70ca2 3980 return ret;
wolfSSL 0:1239e9b70ca2 3981 }
wolfSSL 0:1239e9b70ca2 3982
wolfSSL 0:1239e9b70ca2 3983
wolfSSL 0:1239e9b70ca2 3984 /* from SSL proper, for locking can't do find here anymore */
wolfSSL 0:1239e9b70ca2 3985 #ifdef __cplusplus
wolfSSL 0:1239e9b70ca2 3986 extern "C" {
wolfSSL 0:1239e9b70ca2 3987 #endif
wolfSSL 0:1239e9b70ca2 3988 CYASSL_LOCAL Signer* GetCA(void* signers, byte* hash);
wolfSSL 0:1239e9b70ca2 3989 #ifndef NO_SKID
wolfSSL 0:1239e9b70ca2 3990 CYASSL_LOCAL Signer* GetCAByName(void* signers, byte* hash);
wolfSSL 0:1239e9b70ca2 3991 #endif
wolfSSL 0:1239e9b70ca2 3992 #ifdef __cplusplus
wolfSSL 0:1239e9b70ca2 3993 }
wolfSSL 0:1239e9b70ca2 3994 #endif
wolfSSL 0:1239e9b70ca2 3995
wolfSSL 0:1239e9b70ca2 3996
wolfSSL 0:1239e9b70ca2 3997 int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm)
wolfSSL 0:1239e9b70ca2 3998 {
wolfSSL 0:1239e9b70ca2 3999 word32 confirmOID;
wolfSSL 0:1239e9b70ca2 4000 int ret;
wolfSSL 0:1239e9b70ca2 4001 int badDate = 0;
wolfSSL 0:1239e9b70ca2 4002 int criticalExt = 0;
wolfSSL 0:1239e9b70ca2 4003
wolfSSL 0:1239e9b70ca2 4004 if ((ret = DecodeToKey(cert, verify)) < 0) {
wolfSSL 0:1239e9b70ca2 4005 if (ret == ASN_BEFORE_DATE_E || ret == ASN_AFTER_DATE_E)
wolfSSL 0:1239e9b70ca2 4006 badDate = ret;
wolfSSL 0:1239e9b70ca2 4007 else
wolfSSL 0:1239e9b70ca2 4008 return ret;
wolfSSL 0:1239e9b70ca2 4009 }
wolfSSL 0:1239e9b70ca2 4010
wolfSSL 0:1239e9b70ca2 4011 CYASSL_MSG("Parsed Past Key");
wolfSSL 0:1239e9b70ca2 4012
wolfSSL 0:1239e9b70ca2 4013 if (cert->srcIdx < cert->sigIndex) {
wolfSSL 0:1239e9b70ca2 4014 #ifndef ALLOW_V1_EXTENSIONS
wolfSSL 0:1239e9b70ca2 4015 if (cert->version < 2) {
wolfSSL 0:1239e9b70ca2 4016 CYASSL_MSG(" v1 and v2 certs not allowed extensions");
wolfSSL 0:1239e9b70ca2 4017 return ASN_VERSION_E;
wolfSSL 0:1239e9b70ca2 4018 }
wolfSSL 0:1239e9b70ca2 4019 #endif
wolfSSL 0:1239e9b70ca2 4020 /* save extensions */
wolfSSL 0:1239e9b70ca2 4021 cert->extensions = &cert->source[cert->srcIdx];
wolfSSL 0:1239e9b70ca2 4022 cert->extensionsSz = cert->sigIndex - cert->srcIdx;
wolfSSL 0:1239e9b70ca2 4023 cert->extensionsIdx = cert->srcIdx; /* for potential later use */
wolfSSL 0:1239e9b70ca2 4024
wolfSSL 0:1239e9b70ca2 4025 if ((ret = DecodeCertExtensions(cert)) < 0) {
wolfSSL 0:1239e9b70ca2 4026 if (ret == ASN_CRIT_EXT_E)
wolfSSL 0:1239e9b70ca2 4027 criticalExt = ret;
wolfSSL 0:1239e9b70ca2 4028 else
wolfSSL 0:1239e9b70ca2 4029 return ret;
wolfSSL 0:1239e9b70ca2 4030 }
wolfSSL 0:1239e9b70ca2 4031
wolfSSL 0:1239e9b70ca2 4032 /* advance past extensions */
wolfSSL 0:1239e9b70ca2 4033 cert->srcIdx = cert->sigIndex;
wolfSSL 0:1239e9b70ca2 4034 }
wolfSSL 0:1239e9b70ca2 4035
wolfSSL 0:1239e9b70ca2 4036 if ((ret = GetAlgoId(cert->source, &cert->srcIdx, &confirmOID,
wolfSSL 0:1239e9b70ca2 4037 cert->maxIdx)) < 0)
wolfSSL 0:1239e9b70ca2 4038 return ret;
wolfSSL 0:1239e9b70ca2 4039
wolfSSL 0:1239e9b70ca2 4040 if ((ret = GetSignature(cert)) < 0)
wolfSSL 0:1239e9b70ca2 4041 return ret;
wolfSSL 0:1239e9b70ca2 4042
wolfSSL 0:1239e9b70ca2 4043 if (confirmOID != cert->signatureOID)
wolfSSL 0:1239e9b70ca2 4044 return ASN_SIG_OID_E;
wolfSSL 0:1239e9b70ca2 4045
wolfSSL 0:1239e9b70ca2 4046 #ifndef NO_SKID
wolfSSL 0:1239e9b70ca2 4047 if (cert->extSubjKeyIdSet == 0
wolfSSL 0:1239e9b70ca2 4048 && cert->publicKey != NULL && cert->pubKeySize > 0) {
wolfSSL 0:1239e9b70ca2 4049 Sha sha;
wolfSSL 0:1239e9b70ca2 4050 ret = InitSha(&sha);
wolfSSL 0:1239e9b70ca2 4051 if (ret != 0)
wolfSSL 0:1239e9b70ca2 4052 return ret;
wolfSSL 0:1239e9b70ca2 4053 ShaUpdate(&sha, cert->publicKey, cert->pubKeySize);
wolfSSL 0:1239e9b70ca2 4054 ShaFinal(&sha, cert->extSubjKeyId);
wolfSSL 0:1239e9b70ca2 4055 }
wolfSSL 0:1239e9b70ca2 4056 #endif
wolfSSL 0:1239e9b70ca2 4057
wolfSSL 0:1239e9b70ca2 4058 if (verify && type != CA_TYPE) {
wolfSSL 0:1239e9b70ca2 4059 Signer* ca = NULL;
wolfSSL 0:1239e9b70ca2 4060 #ifndef NO_SKID
wolfSSL 0:1239e9b70ca2 4061 if (cert->extAuthKeyIdSet)
wolfSSL 0:1239e9b70ca2 4062 ca = GetCA(cm, cert->extAuthKeyId);
wolfSSL 0:1239e9b70ca2 4063 if (ca == NULL)
wolfSSL 0:1239e9b70ca2 4064 ca = GetCAByName(cm, cert->issuerHash);
wolfSSL 0:1239e9b70ca2 4065 #else /* NO_SKID */
wolfSSL 0:1239e9b70ca2 4066 ca = GetCA(cm, cert->issuerHash);
wolfSSL 0:1239e9b70ca2 4067 #endif /* NO SKID */
wolfSSL 0:1239e9b70ca2 4068 CYASSL_MSG("About to verify certificate signature");
wolfSSL 0:1239e9b70ca2 4069
wolfSSL 0:1239e9b70ca2 4070 if (ca) {
wolfSSL 0:1239e9b70ca2 4071 #ifdef HAVE_OCSP
wolfSSL 0:1239e9b70ca2 4072 /* Need the ca's public key hash for OCSP */
wolfSSL 0:1239e9b70ca2 4073 {
wolfSSL 0:1239e9b70ca2 4074 Sha sha;
wolfSSL 0:1239e9b70ca2 4075 ret = InitSha(&sha);
wolfSSL 0:1239e9b70ca2 4076 if (ret != 0)
wolfSSL 0:1239e9b70ca2 4077 return ret;
wolfSSL 0:1239e9b70ca2 4078 ShaUpdate(&sha, ca->publicKey, ca->pubKeySize);
wolfSSL 0:1239e9b70ca2 4079 ShaFinal(&sha, cert->issuerKeyHash);
wolfSSL 0:1239e9b70ca2 4080 }
wolfSSL 0:1239e9b70ca2 4081 #endif /* HAVE_OCSP */
wolfSSL 0:1239e9b70ca2 4082 /* try to confirm/verify signature */
wolfSSL 0:1239e9b70ca2 4083 if (!ConfirmSignature(cert->source + cert->certBegin,
wolfSSL 0:1239e9b70ca2 4084 cert->sigIndex - cert->certBegin,
wolfSSL 0:1239e9b70ca2 4085 ca->publicKey, ca->pubKeySize, ca->keyOID,
wolfSSL 0:1239e9b70ca2 4086 cert->signature, cert->sigLength, cert->signatureOID,
wolfSSL 0:1239e9b70ca2 4087 cert->heap)) {
wolfSSL 0:1239e9b70ca2 4088 CYASSL_MSG("Confirm signature failed");
wolfSSL 0:1239e9b70ca2 4089 return ASN_SIG_CONFIRM_E;
wolfSSL 0:1239e9b70ca2 4090 }
wolfSSL 0:1239e9b70ca2 4091 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 4092 /* check that this cert's name is permitted by the signer's
wolfSSL 0:1239e9b70ca2 4093 * name constraints */
wolfSSL 0:1239e9b70ca2 4094 if (!ConfirmNameConstraints(ca, cert)) {
wolfSSL 0:1239e9b70ca2 4095 CYASSL_MSG("Confirm name constraint failed");
wolfSSL 0:1239e9b70ca2 4096 return ASN_NAME_INVALID_E;
wolfSSL 0:1239e9b70ca2 4097 }
wolfSSL 0:1239e9b70ca2 4098 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 4099 }
wolfSSL 0:1239e9b70ca2 4100 else {
wolfSSL 0:1239e9b70ca2 4101 /* no signer */
wolfSSL 0:1239e9b70ca2 4102 CYASSL_MSG("No CA signer to verify with");
wolfSSL 0:1239e9b70ca2 4103 return ASN_NO_SIGNER_E;
wolfSSL 0:1239e9b70ca2 4104 }
wolfSSL 0:1239e9b70ca2 4105 }
wolfSSL 0:1239e9b70ca2 4106
wolfSSL 0:1239e9b70ca2 4107 if (badDate != 0)
wolfSSL 0:1239e9b70ca2 4108 return badDate;
wolfSSL 0:1239e9b70ca2 4109
wolfSSL 0:1239e9b70ca2 4110 if (criticalExt != 0)
wolfSSL 0:1239e9b70ca2 4111 return criticalExt;
wolfSSL 0:1239e9b70ca2 4112
wolfSSL 0:1239e9b70ca2 4113 return 0;
wolfSSL 0:1239e9b70ca2 4114 }
wolfSSL 0:1239e9b70ca2 4115
wolfSSL 0:1239e9b70ca2 4116
wolfSSL 0:1239e9b70ca2 4117 /* Create and init an new signer */
wolfSSL 0:1239e9b70ca2 4118 Signer* MakeSigner(void* heap)
wolfSSL 0:1239e9b70ca2 4119 {
wolfSSL 0:1239e9b70ca2 4120 Signer* signer = (Signer*) XMALLOC(sizeof(Signer), heap,
wolfSSL 0:1239e9b70ca2 4121 DYNAMIC_TYPE_SIGNER);
wolfSSL 0:1239e9b70ca2 4122 if (signer) {
wolfSSL 0:1239e9b70ca2 4123 signer->pubKeySize = 0;
wolfSSL 0:1239e9b70ca2 4124 signer->keyOID = 0;
wolfSSL 0:1239e9b70ca2 4125 signer->publicKey = NULL;
wolfSSL 0:1239e9b70ca2 4126 signer->nameLen = 0;
wolfSSL 0:1239e9b70ca2 4127 signer->name = NULL;
wolfSSL 0:1239e9b70ca2 4128 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 4129 signer->permittedNames = NULL;
wolfSSL 0:1239e9b70ca2 4130 signer->excludedNames = NULL;
wolfSSL 0:1239e9b70ca2 4131 #endif /* IGNORE_NAME_CONSTRAINTS */
wolfSSL 0:1239e9b70ca2 4132 signer->next = NULL;
wolfSSL 0:1239e9b70ca2 4133 }
wolfSSL 0:1239e9b70ca2 4134 (void)heap;
wolfSSL 0:1239e9b70ca2 4135
wolfSSL 0:1239e9b70ca2 4136 return signer;
wolfSSL 0:1239e9b70ca2 4137 }
wolfSSL 0:1239e9b70ca2 4138
wolfSSL 0:1239e9b70ca2 4139
wolfSSL 0:1239e9b70ca2 4140 /* Free an individual signer */
wolfSSL 0:1239e9b70ca2 4141 void FreeSigner(Signer* signer, void* heap)
wolfSSL 0:1239e9b70ca2 4142 {
wolfSSL 0:1239e9b70ca2 4143 XFREE(signer->name, heap, DYNAMIC_TYPE_SUBJECT_CN);
wolfSSL 0:1239e9b70ca2 4144 XFREE(signer->publicKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
wolfSSL 0:1239e9b70ca2 4145 #ifndef IGNORE_NAME_CONSTRAINTS
wolfSSL 0:1239e9b70ca2 4146 if (signer->permittedNames)
wolfSSL 0:1239e9b70ca2 4147 FreeNameSubtrees(signer->permittedNames, heap);
wolfSSL 0:1239e9b70ca2 4148 if (signer->excludedNames)
wolfSSL 0:1239e9b70ca2 4149 FreeNameSubtrees(signer->excludedNames, heap);
wolfSSL 0:1239e9b70ca2 4150 #endif
wolfSSL 0:1239e9b70ca2 4151 XFREE(signer, heap, DYNAMIC_TYPE_SIGNER);
wolfSSL 0:1239e9b70ca2 4152
wolfSSL 0:1239e9b70ca2 4153 (void)heap;
wolfSSL 0:1239e9b70ca2 4154 }
wolfSSL 0:1239e9b70ca2 4155
wolfSSL 0:1239e9b70ca2 4156
wolfSSL 0:1239e9b70ca2 4157 /* Free the whole singer table with number of rows */
wolfSSL 0:1239e9b70ca2 4158 void FreeSignerTable(Signer** table, int rows, void* heap)
wolfSSL 0:1239e9b70ca2 4159 {
wolfSSL 0:1239e9b70ca2 4160 int i;
wolfSSL 0:1239e9b70ca2 4161
wolfSSL 0:1239e9b70ca2 4162 for (i = 0; i < rows; i++) {
wolfSSL 0:1239e9b70ca2 4163 Signer* signer = table[i];
wolfSSL 0:1239e9b70ca2 4164 while (signer) {
wolfSSL 0:1239e9b70ca2 4165 Signer* next = signer->next;
wolfSSL 0:1239e9b70ca2 4166 FreeSigner(signer, heap);
wolfSSL 0:1239e9b70ca2 4167 signer = next;
wolfSSL 0:1239e9b70ca2 4168 }
wolfSSL 0:1239e9b70ca2 4169 table[i] = NULL;
wolfSSL 0:1239e9b70ca2 4170 }
wolfSSL 0:1239e9b70ca2 4171 }
wolfSSL 0:1239e9b70ca2 4172
wolfSSL 0:1239e9b70ca2 4173
wolfSSL 0:1239e9b70ca2 4174 CYASSL_LOCAL int SetMyVersion(word32 version, byte* output, int header)
wolfSSL 0:1239e9b70ca2 4175 {
wolfSSL 0:1239e9b70ca2 4176 int i = 0;
wolfSSL 0:1239e9b70ca2 4177
wolfSSL 0:1239e9b70ca2 4178 if (header) {
wolfSSL 0:1239e9b70ca2 4179 output[i++] = ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED;
wolfSSL 0:1239e9b70ca2 4180 output[i++] = ASN_BIT_STRING;
wolfSSL 0:1239e9b70ca2 4181 }
wolfSSL 0:1239e9b70ca2 4182 output[i++] = ASN_INTEGER;
wolfSSL 0:1239e9b70ca2 4183 output[i++] = 0x01;
wolfSSL 0:1239e9b70ca2 4184 output[i++] = (byte)version;
wolfSSL 0:1239e9b70ca2 4185
wolfSSL 0:1239e9b70ca2 4186 return i;
wolfSSL 0:1239e9b70ca2 4187 }
wolfSSL 0:1239e9b70ca2 4188
wolfSSL 0:1239e9b70ca2 4189
wolfSSL 0:1239e9b70ca2 4190 CYASSL_LOCAL int SetSerialNumber(const byte* sn, word32 snSz, byte* output)
wolfSSL 0:1239e9b70ca2 4191 {
wolfSSL 0:1239e9b70ca2 4192 int result = 0;
wolfSSL 0:1239e9b70ca2 4193
wolfSSL 0:1239e9b70ca2 4194 CYASSL_ENTER("SetSerialNumber");
wolfSSL 0:1239e9b70ca2 4195
wolfSSL 0:1239e9b70ca2 4196 if (snSz <= EXTERNAL_SERIAL_SIZE) {
wolfSSL 0:1239e9b70ca2 4197 output[0] = ASN_INTEGER;
wolfSSL 0:1239e9b70ca2 4198 /* The serial number is always positive. When encoding the
wolfSSL 0:1239e9b70ca2 4199 * INTEGER, if the MSB is 1, add a padding zero to keep the
wolfSSL 0:1239e9b70ca2 4200 * number positive. */
wolfSSL 0:1239e9b70ca2 4201 if (sn[0] & 0x80) {
wolfSSL 0:1239e9b70ca2 4202 output[1] = (byte)snSz + 1;
wolfSSL 0:1239e9b70ca2 4203 output[2] = 0;
wolfSSL 0:1239e9b70ca2 4204 XMEMCPY(&output[3], sn, snSz);
wolfSSL 0:1239e9b70ca2 4205 result = snSz + 3;
wolfSSL 0:1239e9b70ca2 4206 }
wolfSSL 0:1239e9b70ca2 4207 else {
wolfSSL 0:1239e9b70ca2 4208 output[1] = (byte)snSz;
wolfSSL 0:1239e9b70ca2 4209 XMEMCPY(&output[2], sn, snSz);
wolfSSL 0:1239e9b70ca2 4210 result = snSz + 2;
wolfSSL 0:1239e9b70ca2 4211 }
wolfSSL 0:1239e9b70ca2 4212 }
wolfSSL 0:1239e9b70ca2 4213 return result;
wolfSSL 0:1239e9b70ca2 4214 }
wolfSSL 0:1239e9b70ca2 4215
wolfSSL 0:1239e9b70ca2 4216
wolfSSL 0:1239e9b70ca2 4217
wolfSSL 0:1239e9b70ca2 4218
wolfSSL 0:1239e9b70ca2 4219 #if defined(CYASSL_KEY_GEN) || defined(CYASSL_CERT_GEN)
wolfSSL 0:1239e9b70ca2 4220
wolfSSL 0:1239e9b70ca2 4221 /* convert der buffer to pem into output, can't do inplace, der and output
wolfSSL 0:1239e9b70ca2 4222 need to be different */
wolfSSL 0:1239e9b70ca2 4223 int DerToPem(const byte* der, word32 derSz, byte* output, word32 outSz,
wolfSSL 0:1239e9b70ca2 4224 int type)
wolfSSL 0:1239e9b70ca2 4225 {
wolfSSL 0:1239e9b70ca2 4226 char header[80];
wolfSSL 0:1239e9b70ca2 4227 char footer[80];
wolfSSL 0:1239e9b70ca2 4228
wolfSSL 0:1239e9b70ca2 4229 int headerLen;
wolfSSL 0:1239e9b70ca2 4230 int footerLen;
wolfSSL 0:1239e9b70ca2 4231 int i;
wolfSSL 0:1239e9b70ca2 4232 int err;
wolfSSL 0:1239e9b70ca2 4233 int outLen; /* return length or error */
wolfSSL 0:1239e9b70ca2 4234
wolfSSL 0:1239e9b70ca2 4235 if (der == output) /* no in place conversion */
wolfSSL 0:1239e9b70ca2 4236 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 4237
wolfSSL 0:1239e9b70ca2 4238 if (type == CERT_TYPE) {
wolfSSL 0:1239e9b70ca2 4239 XSTRNCPY(header, "-----BEGIN CERTIFICATE-----\n", sizeof(header));
wolfSSL 0:1239e9b70ca2 4240 XSTRNCPY(footer, "-----END CERTIFICATE-----\n", sizeof(footer));
wolfSSL 0:1239e9b70ca2 4241 }
wolfSSL 0:1239e9b70ca2 4242 else if (type == PRIVATEKEY_TYPE) {
wolfSSL 0:1239e9b70ca2 4243 XSTRNCPY(header, "-----BEGIN RSA PRIVATE KEY-----\n", sizeof(header));
wolfSSL 0:1239e9b70ca2 4244 XSTRNCPY(footer, "-----END RSA PRIVATE KEY-----\n", sizeof(footer));
wolfSSL 0:1239e9b70ca2 4245 }
wolfSSL 0:1239e9b70ca2 4246 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 4247 else if (type == ECC_PRIVATEKEY_TYPE) {
wolfSSL 0:1239e9b70ca2 4248 XSTRNCPY(header, "-----BEGIN EC PRIVATE KEY-----\n", sizeof(header));
wolfSSL 0:1239e9b70ca2 4249 XSTRNCPY(footer, "-----END EC PRIVATE KEY-----\n", sizeof(footer));
wolfSSL 0:1239e9b70ca2 4250 }
wolfSSL 0:1239e9b70ca2 4251 #endif
wolfSSL 0:1239e9b70ca2 4252 #ifdef CYASSL_CERT_REQ
wolfSSL 0:1239e9b70ca2 4253 else if (type == CERTREQ_TYPE)
wolfSSL 0:1239e9b70ca2 4254 {
wolfSSL 0:1239e9b70ca2 4255 XSTRNCPY(header,
wolfSSL 0:1239e9b70ca2 4256 "-----BEGIN CERTIFICATE REQUEST-----\n", sizeof(header));
wolfSSL 0:1239e9b70ca2 4257 XSTRNCPY(footer, "-----END CERTIFICATE REQUEST-----\n", sizeof(footer));
wolfSSL 0:1239e9b70ca2 4258 }
wolfSSL 0:1239e9b70ca2 4259 #endif
wolfSSL 0:1239e9b70ca2 4260 else
wolfSSL 0:1239e9b70ca2 4261 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 4262
wolfSSL 0:1239e9b70ca2 4263 headerLen = (int)XSTRLEN(header);
wolfSSL 0:1239e9b70ca2 4264 footerLen = (int)XSTRLEN(footer);
wolfSSL 0:1239e9b70ca2 4265
wolfSSL 0:1239e9b70ca2 4266 if (!der || !output)
wolfSSL 0:1239e9b70ca2 4267 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 4268
wolfSSL 0:1239e9b70ca2 4269 /* don't even try if outSz too short */
wolfSSL 0:1239e9b70ca2 4270 if (outSz < headerLen + footerLen + derSz)
wolfSSL 0:1239e9b70ca2 4271 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 4272
wolfSSL 0:1239e9b70ca2 4273 /* header */
wolfSSL 0:1239e9b70ca2 4274 XMEMCPY(output, header, headerLen);
wolfSSL 0:1239e9b70ca2 4275 i = headerLen;
wolfSSL 0:1239e9b70ca2 4276
wolfSSL 0:1239e9b70ca2 4277 /* body */
wolfSSL 0:1239e9b70ca2 4278 outLen = outSz - (headerLen + footerLen); /* input to Base64_Encode */
wolfSSL 0:1239e9b70ca2 4279 if ( (err = Base64_Encode(der, derSz, output + i, (word32*)&outLen)) < 0)
wolfSSL 0:1239e9b70ca2 4280 return err;
wolfSSL 0:1239e9b70ca2 4281 i += outLen;
wolfSSL 0:1239e9b70ca2 4282
wolfSSL 0:1239e9b70ca2 4283 /* footer */
wolfSSL 0:1239e9b70ca2 4284 if ( (i + footerLen) > (int)outSz)
wolfSSL 0:1239e9b70ca2 4285 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 4286 XMEMCPY(output + i, footer, footerLen);
wolfSSL 0:1239e9b70ca2 4287
wolfSSL 0:1239e9b70ca2 4288 return outLen + headerLen + footerLen;
wolfSSL 0:1239e9b70ca2 4289 }
wolfSSL 0:1239e9b70ca2 4290
wolfSSL 0:1239e9b70ca2 4291
wolfSSL 0:1239e9b70ca2 4292 #endif /* CYASSL_KEY_GEN || CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 4293
wolfSSL 0:1239e9b70ca2 4294
wolfSSL 0:1239e9b70ca2 4295 #if defined(CYASSL_KEY_GEN) && !defined(NO_RSA)
wolfSSL 0:1239e9b70ca2 4296
wolfSSL 0:1239e9b70ca2 4297
wolfSSL 0:1239e9b70ca2 4298 static mp_int* GetRsaInt(RsaKey* key, int idx)
wolfSSL 0:1239e9b70ca2 4299 {
wolfSSL 0:1239e9b70ca2 4300 if (idx == 0)
wolfSSL 0:1239e9b70ca2 4301 return &key->n;
wolfSSL 0:1239e9b70ca2 4302 if (idx == 1)
wolfSSL 0:1239e9b70ca2 4303 return &key->e;
wolfSSL 0:1239e9b70ca2 4304 if (idx == 2)
wolfSSL 0:1239e9b70ca2 4305 return &key->d;
wolfSSL 0:1239e9b70ca2 4306 if (idx == 3)
wolfSSL 0:1239e9b70ca2 4307 return &key->p;
wolfSSL 0:1239e9b70ca2 4308 if (idx == 4)
wolfSSL 0:1239e9b70ca2 4309 return &key->q;
wolfSSL 0:1239e9b70ca2 4310 if (idx == 5)
wolfSSL 0:1239e9b70ca2 4311 return &key->dP;
wolfSSL 0:1239e9b70ca2 4312 if (idx == 6)
wolfSSL 0:1239e9b70ca2 4313 return &key->dQ;
wolfSSL 0:1239e9b70ca2 4314 if (idx == 7)
wolfSSL 0:1239e9b70ca2 4315 return &key->u;
wolfSSL 0:1239e9b70ca2 4316
wolfSSL 0:1239e9b70ca2 4317 return NULL;
wolfSSL 0:1239e9b70ca2 4318 }
wolfSSL 0:1239e9b70ca2 4319
wolfSSL 0:1239e9b70ca2 4320
wolfSSL 0:1239e9b70ca2 4321 /* Release Tmp RSA resources */
wolfSSL 0:1239e9b70ca2 4322 static INLINE void FreeTmpRsas(byte** tmps, void* heap)
wolfSSL 0:1239e9b70ca2 4323 {
wolfSSL 0:1239e9b70ca2 4324 int i;
wolfSSL 0:1239e9b70ca2 4325
wolfSSL 0:1239e9b70ca2 4326 (void)heap;
wolfSSL 0:1239e9b70ca2 4327
wolfSSL 0:1239e9b70ca2 4328 for (i = 0; i < RSA_INTS; i++)
wolfSSL 0:1239e9b70ca2 4329 XFREE(tmps[i], heap, DYNAMIC_TYPE_RSA);
wolfSSL 0:1239e9b70ca2 4330 }
wolfSSL 0:1239e9b70ca2 4331
wolfSSL 0:1239e9b70ca2 4332
wolfSSL 0:1239e9b70ca2 4333 /* Convert RsaKey key to DER format, write to output (inLen), return bytes
wolfSSL 0:1239e9b70ca2 4334 written */
wolfSSL 0:1239e9b70ca2 4335 int RsaKeyToDer(RsaKey* key, byte* output, word32 inLen)
wolfSSL 0:1239e9b70ca2 4336 {
wolfSSL 0:1239e9b70ca2 4337 word32 seqSz, verSz, rawLen, intTotalLen = 0;
wolfSSL 0:1239e9b70ca2 4338 word32 sizes[RSA_INTS];
wolfSSL 0:1239e9b70ca2 4339 int i, j, outLen, ret = 0;
wolfSSL 0:1239e9b70ca2 4340
wolfSSL 0:1239e9b70ca2 4341 byte seq[MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 4342 byte ver[MAX_VERSION_SZ];
wolfSSL 0:1239e9b70ca2 4343 byte* tmps[RSA_INTS];
wolfSSL 0:1239e9b70ca2 4344
wolfSSL 0:1239e9b70ca2 4345 if (!key || !output)
wolfSSL 0:1239e9b70ca2 4346 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 4347
wolfSSL 0:1239e9b70ca2 4348 if (key->type != RSA_PRIVATE)
wolfSSL 0:1239e9b70ca2 4349 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 4350
wolfSSL 0:1239e9b70ca2 4351 for (i = 0; i < RSA_INTS; i++)
wolfSSL 0:1239e9b70ca2 4352 tmps[i] = NULL;
wolfSSL 0:1239e9b70ca2 4353
wolfSSL 0:1239e9b70ca2 4354 /* write all big ints from key to DER tmps */
wolfSSL 0:1239e9b70ca2 4355 for (i = 0; i < RSA_INTS; i++) {
wolfSSL 0:1239e9b70ca2 4356 mp_int* keyInt = GetRsaInt(key, i);
wolfSSL 0:1239e9b70ca2 4357 rawLen = mp_unsigned_bin_size(keyInt);
wolfSSL 0:1239e9b70ca2 4358 tmps[i] = (byte*)XMALLOC(rawLen + MAX_SEQ_SZ, key->heap,
wolfSSL 0:1239e9b70ca2 4359 DYNAMIC_TYPE_RSA);
wolfSSL 0:1239e9b70ca2 4360 if (tmps[i] == NULL) {
wolfSSL 0:1239e9b70ca2 4361 ret = MEMORY_E;
wolfSSL 0:1239e9b70ca2 4362 break;
wolfSSL 0:1239e9b70ca2 4363 }
wolfSSL 0:1239e9b70ca2 4364
wolfSSL 0:1239e9b70ca2 4365 tmps[i][0] = ASN_INTEGER;
wolfSSL 0:1239e9b70ca2 4366 sizes[i] = SetLength(rawLen, tmps[i] + 1) + 1; /* int tag */
wolfSSL 0:1239e9b70ca2 4367
wolfSSL 0:1239e9b70ca2 4368 if (sizes[i] <= MAX_SEQ_SZ) {
wolfSSL 0:1239e9b70ca2 4369 int err = mp_to_unsigned_bin(keyInt, tmps[i] + sizes[i]);
wolfSSL 0:1239e9b70ca2 4370 if (err == MP_OKAY) {
wolfSSL 0:1239e9b70ca2 4371 sizes[i] += rawLen;
wolfSSL 0:1239e9b70ca2 4372 intTotalLen += sizes[i];
wolfSSL 0:1239e9b70ca2 4373 }
wolfSSL 0:1239e9b70ca2 4374 else {
wolfSSL 0:1239e9b70ca2 4375 ret = err;
wolfSSL 0:1239e9b70ca2 4376 break;
wolfSSL 0:1239e9b70ca2 4377 }
wolfSSL 0:1239e9b70ca2 4378 }
wolfSSL 0:1239e9b70ca2 4379 else {
wolfSSL 0:1239e9b70ca2 4380 ret = ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 4381 break;
wolfSSL 0:1239e9b70ca2 4382 }
wolfSSL 0:1239e9b70ca2 4383 }
wolfSSL 0:1239e9b70ca2 4384
wolfSSL 0:1239e9b70ca2 4385 if (ret != 0) {
wolfSSL 0:1239e9b70ca2 4386 FreeTmpRsas(tmps, key->heap);
wolfSSL 0:1239e9b70ca2 4387 return ret;
wolfSSL 0:1239e9b70ca2 4388 }
wolfSSL 0:1239e9b70ca2 4389
wolfSSL 0:1239e9b70ca2 4390 /* make headers */
wolfSSL 0:1239e9b70ca2 4391 verSz = SetMyVersion(0, ver, FALSE);
wolfSSL 0:1239e9b70ca2 4392 seqSz = SetSequence(verSz + intTotalLen, seq);
wolfSSL 0:1239e9b70ca2 4393
wolfSSL 0:1239e9b70ca2 4394 outLen = seqSz + verSz + intTotalLen;
wolfSSL 0:1239e9b70ca2 4395 if (outLen > (int)inLen)
wolfSSL 0:1239e9b70ca2 4396 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 4397
wolfSSL 0:1239e9b70ca2 4398 /* write to output */
wolfSSL 0:1239e9b70ca2 4399 XMEMCPY(output, seq, seqSz);
wolfSSL 0:1239e9b70ca2 4400 j = seqSz;
wolfSSL 0:1239e9b70ca2 4401 XMEMCPY(output + j, ver, verSz);
wolfSSL 0:1239e9b70ca2 4402 j += verSz;
wolfSSL 0:1239e9b70ca2 4403
wolfSSL 0:1239e9b70ca2 4404 for (i = 0; i < RSA_INTS; i++) {
wolfSSL 0:1239e9b70ca2 4405 XMEMCPY(output + j, tmps[i], sizes[i]);
wolfSSL 0:1239e9b70ca2 4406 j += sizes[i];
wolfSSL 0:1239e9b70ca2 4407 }
wolfSSL 0:1239e9b70ca2 4408 FreeTmpRsas(tmps, key->heap);
wolfSSL 0:1239e9b70ca2 4409
wolfSSL 0:1239e9b70ca2 4410 return outLen;
wolfSSL 0:1239e9b70ca2 4411 }
wolfSSL 0:1239e9b70ca2 4412
wolfSSL 0:1239e9b70ca2 4413 #endif /* CYASSL_KEY_GEN && !NO_RSA */
wolfSSL 0:1239e9b70ca2 4414
wolfSSL 0:1239e9b70ca2 4415
wolfSSL 0:1239e9b70ca2 4416 #if defined(CYASSL_CERT_GEN) && !defined(NO_RSA)
wolfSSL 0:1239e9b70ca2 4417
wolfSSL 0:1239e9b70ca2 4418
wolfSSL 0:1239e9b70ca2 4419 #ifndef min
wolfSSL 0:1239e9b70ca2 4420
wolfSSL 0:1239e9b70ca2 4421 static INLINE word32 min(word32 a, word32 b)
wolfSSL 0:1239e9b70ca2 4422 {
wolfSSL 0:1239e9b70ca2 4423 return a > b ? b : a;
wolfSSL 0:1239e9b70ca2 4424 }
wolfSSL 0:1239e9b70ca2 4425
wolfSSL 0:1239e9b70ca2 4426 #endif /* min */
wolfSSL 0:1239e9b70ca2 4427
wolfSSL 0:1239e9b70ca2 4428
wolfSSL 0:1239e9b70ca2 4429 /* Initialize and Set Certficate defaults:
wolfSSL 0:1239e9b70ca2 4430 version = 3 (0x2)
wolfSSL 0:1239e9b70ca2 4431 serial = 0
wolfSSL 0:1239e9b70ca2 4432 sigType = SHA_WITH_RSA
wolfSSL 0:1239e9b70ca2 4433 issuer = blank
wolfSSL 0:1239e9b70ca2 4434 daysValid = 500
wolfSSL 0:1239e9b70ca2 4435 selfSigned = 1 (true) use subject as issuer
wolfSSL 0:1239e9b70ca2 4436 subject = blank
wolfSSL 0:1239e9b70ca2 4437 */
wolfSSL 0:1239e9b70ca2 4438 void InitCert(Cert* cert)
wolfSSL 0:1239e9b70ca2 4439 {
wolfSSL 0:1239e9b70ca2 4440 cert->version = 2; /* version 3 is hex 2 */
wolfSSL 0:1239e9b70ca2 4441 cert->sigType = CTC_SHAwRSA;
wolfSSL 0:1239e9b70ca2 4442 cert->daysValid = 500;
wolfSSL 0:1239e9b70ca2 4443 cert->selfSigned = 1;
wolfSSL 0:1239e9b70ca2 4444 cert->isCA = 0;
wolfSSL 0:1239e9b70ca2 4445 cert->bodySz = 0;
wolfSSL 0:1239e9b70ca2 4446 #ifdef CYASSL_ALT_NAMES
wolfSSL 0:1239e9b70ca2 4447 cert->altNamesSz = 0;
wolfSSL 0:1239e9b70ca2 4448 cert->beforeDateSz = 0;
wolfSSL 0:1239e9b70ca2 4449 cert->afterDateSz = 0;
wolfSSL 0:1239e9b70ca2 4450 #endif
wolfSSL 0:1239e9b70ca2 4451 cert->keyType = RSA_KEY;
wolfSSL 0:1239e9b70ca2 4452 XMEMSET(cert->serial, 0, CTC_SERIAL_SIZE);
wolfSSL 0:1239e9b70ca2 4453
wolfSSL 0:1239e9b70ca2 4454 cert->issuer.country[0] = '\0';
wolfSSL 0:1239e9b70ca2 4455 cert->issuer.state[0] = '\0';
wolfSSL 0:1239e9b70ca2 4456 cert->issuer.locality[0] = '\0';
wolfSSL 0:1239e9b70ca2 4457 cert->issuer.sur[0] = '\0';
wolfSSL 0:1239e9b70ca2 4458 cert->issuer.org[0] = '\0';
wolfSSL 0:1239e9b70ca2 4459 cert->issuer.unit[0] = '\0';
wolfSSL 0:1239e9b70ca2 4460 cert->issuer.commonName[0] = '\0';
wolfSSL 0:1239e9b70ca2 4461 cert->issuer.email[0] = '\0';
wolfSSL 0:1239e9b70ca2 4462
wolfSSL 0:1239e9b70ca2 4463 cert->subject.country[0] = '\0';
wolfSSL 0:1239e9b70ca2 4464 cert->subject.state[0] = '\0';
wolfSSL 0:1239e9b70ca2 4465 cert->subject.locality[0] = '\0';
wolfSSL 0:1239e9b70ca2 4466 cert->subject.sur[0] = '\0';
wolfSSL 0:1239e9b70ca2 4467 cert->subject.org[0] = '\0';
wolfSSL 0:1239e9b70ca2 4468 cert->subject.unit[0] = '\0';
wolfSSL 0:1239e9b70ca2 4469 cert->subject.commonName[0] = '\0';
wolfSSL 0:1239e9b70ca2 4470 cert->subject.email[0] = '\0';
wolfSSL 0:1239e9b70ca2 4471
wolfSSL 0:1239e9b70ca2 4472 #ifdef CYASSL_CERT_REQ
wolfSSL 0:1239e9b70ca2 4473 cert->challengePw[0] ='\0';
wolfSSL 0:1239e9b70ca2 4474 #endif
wolfSSL 0:1239e9b70ca2 4475 }
wolfSSL 0:1239e9b70ca2 4476
wolfSSL 0:1239e9b70ca2 4477
wolfSSL 0:1239e9b70ca2 4478 /* DER encoded x509 Certificate */
wolfSSL 0:1239e9b70ca2 4479 typedef struct DerCert {
wolfSSL 0:1239e9b70ca2 4480 byte size[MAX_LENGTH_SZ]; /* length encoded */
wolfSSL 0:1239e9b70ca2 4481 byte version[MAX_VERSION_SZ]; /* version encoded */
wolfSSL 0:1239e9b70ca2 4482 byte serial[CTC_SERIAL_SIZE + MAX_LENGTH_SZ]; /* serial number encoded */
wolfSSL 0:1239e9b70ca2 4483 byte sigAlgo[MAX_ALGO_SZ]; /* signature algo encoded */
wolfSSL 0:1239e9b70ca2 4484 byte issuer[ASN_NAME_MAX]; /* issuer encoded */
wolfSSL 0:1239e9b70ca2 4485 byte subject[ASN_NAME_MAX]; /* subject encoded */
wolfSSL 0:1239e9b70ca2 4486 byte validity[MAX_DATE_SIZE*2 + MAX_SEQ_SZ*2]; /* before and after dates */
wolfSSL 0:1239e9b70ca2 4487 byte publicKey[MAX_PUBLIC_KEY_SZ]; /* rsa / ntru public key encoded */
wolfSSL 0:1239e9b70ca2 4488 byte ca[MAX_CA_SZ]; /* basic constraint CA true size */
wolfSSL 0:1239e9b70ca2 4489 byte extensions[MAX_EXTENSIONS_SZ]; /* all extensions */
wolfSSL 0:1239e9b70ca2 4490 #ifdef CYASSL_CERT_REQ
wolfSSL 0:1239e9b70ca2 4491 byte attrib[MAX_ATTRIB_SZ]; /* Cert req attributes encoded */
wolfSSL 0:1239e9b70ca2 4492 #endif
wolfSSL 0:1239e9b70ca2 4493 int sizeSz; /* encoded size length */
wolfSSL 0:1239e9b70ca2 4494 int versionSz; /* encoded version length */
wolfSSL 0:1239e9b70ca2 4495 int serialSz; /* encoded serial length */
wolfSSL 0:1239e9b70ca2 4496 int sigAlgoSz; /* enocded sig alog length */
wolfSSL 0:1239e9b70ca2 4497 int issuerSz; /* encoded issuer length */
wolfSSL 0:1239e9b70ca2 4498 int subjectSz; /* encoded subject length */
wolfSSL 0:1239e9b70ca2 4499 int validitySz; /* encoded validity length */
wolfSSL 0:1239e9b70ca2 4500 int publicKeySz; /* encoded public key length */
wolfSSL 0:1239e9b70ca2 4501 int caSz; /* encoded CA extension length */
wolfSSL 0:1239e9b70ca2 4502 int extensionsSz; /* encoded extensions total length */
wolfSSL 0:1239e9b70ca2 4503 int total; /* total encoded lengths */
wolfSSL 0:1239e9b70ca2 4504 #ifdef CYASSL_CERT_REQ
wolfSSL 0:1239e9b70ca2 4505 int attribSz;
wolfSSL 0:1239e9b70ca2 4506 #endif
wolfSSL 0:1239e9b70ca2 4507 } DerCert;
wolfSSL 0:1239e9b70ca2 4508
wolfSSL 0:1239e9b70ca2 4509
wolfSSL 0:1239e9b70ca2 4510 #ifdef CYASSL_CERT_REQ
wolfSSL 0:1239e9b70ca2 4511
wolfSSL 0:1239e9b70ca2 4512 /* Write a set header to output */
wolfSSL 0:1239e9b70ca2 4513 static word32 SetUTF8String(word32 len, byte* output)
wolfSSL 0:1239e9b70ca2 4514 {
wolfSSL 0:1239e9b70ca2 4515 output[0] = ASN_UTF8STRING;
wolfSSL 0:1239e9b70ca2 4516 return SetLength(len, output + 1) + 1;
wolfSSL 0:1239e9b70ca2 4517 }
wolfSSL 0:1239e9b70ca2 4518
wolfSSL 0:1239e9b70ca2 4519 #endif /* CYASSL_CERT_REQ */
wolfSSL 0:1239e9b70ca2 4520
wolfSSL 0:1239e9b70ca2 4521
wolfSSL 0:1239e9b70ca2 4522 /* Write a serial number to output */
wolfSSL 0:1239e9b70ca2 4523 static int SetSerial(const byte* serial, byte* output)
wolfSSL 0:1239e9b70ca2 4524 {
wolfSSL 0:1239e9b70ca2 4525 int length = 0;
wolfSSL 0:1239e9b70ca2 4526
wolfSSL 0:1239e9b70ca2 4527 output[length++] = ASN_INTEGER;
wolfSSL 0:1239e9b70ca2 4528 length += SetLength(CTC_SERIAL_SIZE, &output[length]);
wolfSSL 0:1239e9b70ca2 4529 XMEMCPY(&output[length], serial, CTC_SERIAL_SIZE);
wolfSSL 0:1239e9b70ca2 4530
wolfSSL 0:1239e9b70ca2 4531 return length + CTC_SERIAL_SIZE;
wolfSSL 0:1239e9b70ca2 4532 }
wolfSSL 0:1239e9b70ca2 4533
wolfSSL 0:1239e9b70ca2 4534
wolfSSL 0:1239e9b70ca2 4535 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 4536
wolfSSL 0:1239e9b70ca2 4537 /* Write a public ECC key to output */
wolfSSL 0:1239e9b70ca2 4538 static int SetEccPublicKey(byte* output, ecc_key* key)
wolfSSL 0:1239e9b70ca2 4539 {
wolfSSL 0:1239e9b70ca2 4540 byte algo[MAX_ALGO_SZ];
wolfSSL 0:1239e9b70ca2 4541 byte curve[MAX_ALGO_SZ];
wolfSSL 0:1239e9b70ca2 4542 byte len[MAX_LENGTH_SZ + 1]; /* trailing 0 */
wolfSSL 0:1239e9b70ca2 4543 byte pub[ECC_BUFSIZE];
wolfSSL 0:1239e9b70ca2 4544 int algoSz;
wolfSSL 0:1239e9b70ca2 4545 int curveSz;
wolfSSL 0:1239e9b70ca2 4546 int lenSz;
wolfSSL 0:1239e9b70ca2 4547 int idx;
wolfSSL 0:1239e9b70ca2 4548 word32 pubSz = sizeof(pub);
wolfSSL 0:1239e9b70ca2 4549
wolfSSL 0:1239e9b70ca2 4550 int ret = ecc_export_x963(key, pub, &pubSz);
wolfSSL 0:1239e9b70ca2 4551 if (ret != 0) return ret;
wolfSSL 0:1239e9b70ca2 4552
wolfSSL 0:1239e9b70ca2 4553 /* headers */
wolfSSL 0:1239e9b70ca2 4554 curveSz = SetCurve(key, curve);
wolfSSL 0:1239e9b70ca2 4555 if (curveSz <= 0) return curveSz;
wolfSSL 0:1239e9b70ca2 4556
wolfSSL 0:1239e9b70ca2 4557 algoSz = SetAlgoID(ECDSAk, algo, keyType, curveSz);
wolfSSL 0:1239e9b70ca2 4558 lenSz = SetLength(pubSz + 1, len);
wolfSSL 0:1239e9b70ca2 4559 len[lenSz++] = 0; /* trailing 0 */
wolfSSL 0:1239e9b70ca2 4560
wolfSSL 0:1239e9b70ca2 4561 /* write */
wolfSSL 0:1239e9b70ca2 4562 idx = SetSequence(pubSz + curveSz + lenSz + 1 + algoSz, output);
wolfSSL 0:1239e9b70ca2 4563 /* 1 is for ASN_BIT_STRING */
wolfSSL 0:1239e9b70ca2 4564 /* algo */
wolfSSL 0:1239e9b70ca2 4565 XMEMCPY(output + idx, algo, algoSz);
wolfSSL 0:1239e9b70ca2 4566 idx += algoSz;
wolfSSL 0:1239e9b70ca2 4567 /* curve */
wolfSSL 0:1239e9b70ca2 4568 XMEMCPY(output + idx, curve, curveSz);
wolfSSL 0:1239e9b70ca2 4569 idx += curveSz;
wolfSSL 0:1239e9b70ca2 4570 /* bit string */
wolfSSL 0:1239e9b70ca2 4571 output[idx++] = ASN_BIT_STRING;
wolfSSL 0:1239e9b70ca2 4572 /* length */
wolfSSL 0:1239e9b70ca2 4573 XMEMCPY(output + idx, len, lenSz);
wolfSSL 0:1239e9b70ca2 4574 idx += lenSz;
wolfSSL 0:1239e9b70ca2 4575 /* pub */
wolfSSL 0:1239e9b70ca2 4576 XMEMCPY(output + idx, pub, pubSz);
wolfSSL 0:1239e9b70ca2 4577 idx += pubSz;
wolfSSL 0:1239e9b70ca2 4578
wolfSSL 0:1239e9b70ca2 4579 return idx;
wolfSSL 0:1239e9b70ca2 4580 }
wolfSSL 0:1239e9b70ca2 4581
wolfSSL 0:1239e9b70ca2 4582
wolfSSL 0:1239e9b70ca2 4583 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 4584
wolfSSL 0:1239e9b70ca2 4585
wolfSSL 0:1239e9b70ca2 4586 /* Write a public RSA key to output */
wolfSSL 0:1239e9b70ca2 4587 static int SetRsaPublicKey(byte* output, RsaKey* key)
wolfSSL 0:1239e9b70ca2 4588 {
wolfSSL 0:1239e9b70ca2 4589 byte n[MAX_RSA_INT_SZ];
wolfSSL 0:1239e9b70ca2 4590 byte e[MAX_RSA_E_SZ];
wolfSSL 0:1239e9b70ca2 4591 byte algo[MAX_ALGO_SZ];
wolfSSL 0:1239e9b70ca2 4592 byte seq[MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 4593 byte len[MAX_LENGTH_SZ + 1]; /* trailing 0 */
wolfSSL 0:1239e9b70ca2 4594 int nSz;
wolfSSL 0:1239e9b70ca2 4595 int eSz;
wolfSSL 0:1239e9b70ca2 4596 int algoSz;
wolfSSL 0:1239e9b70ca2 4597 int seqSz;
wolfSSL 0:1239e9b70ca2 4598 int lenSz;
wolfSSL 0:1239e9b70ca2 4599 int idx;
wolfSSL 0:1239e9b70ca2 4600 int rawLen;
wolfSSL 0:1239e9b70ca2 4601 int leadingBit;
wolfSSL 0:1239e9b70ca2 4602 int err;
wolfSSL 0:1239e9b70ca2 4603
wolfSSL 0:1239e9b70ca2 4604 /* n */
wolfSSL 0:1239e9b70ca2 4605 leadingBit = mp_leading_bit(&key->n);
wolfSSL 0:1239e9b70ca2 4606 rawLen = mp_unsigned_bin_size(&key->n) + leadingBit;
wolfSSL 0:1239e9b70ca2 4607 n[0] = ASN_INTEGER;
wolfSSL 0:1239e9b70ca2 4608 nSz = SetLength(rawLen, n + 1) + 1; /* int tag */
wolfSSL 0:1239e9b70ca2 4609
wolfSSL 0:1239e9b70ca2 4610 if ( (nSz + rawLen) < (int)sizeof(n)) {
wolfSSL 0:1239e9b70ca2 4611 if (leadingBit)
wolfSSL 0:1239e9b70ca2 4612 n[nSz] = 0;
wolfSSL 0:1239e9b70ca2 4613 err = mp_to_unsigned_bin(&key->n, n + nSz + leadingBit);
wolfSSL 0:1239e9b70ca2 4614 if (err == MP_OKAY)
wolfSSL 0:1239e9b70ca2 4615 nSz += rawLen;
wolfSSL 0:1239e9b70ca2 4616 else
wolfSSL 0:1239e9b70ca2 4617 return MP_TO_E;
wolfSSL 0:1239e9b70ca2 4618 }
wolfSSL 0:1239e9b70ca2 4619 else
wolfSSL 0:1239e9b70ca2 4620 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 4621
wolfSSL 0:1239e9b70ca2 4622 /* e */
wolfSSL 0:1239e9b70ca2 4623 leadingBit = mp_leading_bit(&key->e);
wolfSSL 0:1239e9b70ca2 4624 rawLen = mp_unsigned_bin_size(&key->e) + leadingBit;
wolfSSL 0:1239e9b70ca2 4625 e[0] = ASN_INTEGER;
wolfSSL 0:1239e9b70ca2 4626 eSz = SetLength(rawLen, e + 1) + 1; /* int tag */
wolfSSL 0:1239e9b70ca2 4627
wolfSSL 0:1239e9b70ca2 4628 if ( (eSz + rawLen) < (int)sizeof(e)) {
wolfSSL 0:1239e9b70ca2 4629 if (leadingBit)
wolfSSL 0:1239e9b70ca2 4630 e[eSz] = 0;
wolfSSL 0:1239e9b70ca2 4631 err = mp_to_unsigned_bin(&key->e, e + eSz + leadingBit);
wolfSSL 0:1239e9b70ca2 4632 if (err == MP_OKAY)
wolfSSL 0:1239e9b70ca2 4633 eSz += rawLen;
wolfSSL 0:1239e9b70ca2 4634 else
wolfSSL 0:1239e9b70ca2 4635 return MP_TO_E;
wolfSSL 0:1239e9b70ca2 4636 }
wolfSSL 0:1239e9b70ca2 4637 else
wolfSSL 0:1239e9b70ca2 4638 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 4639
wolfSSL 0:1239e9b70ca2 4640 /* headers */
wolfSSL 0:1239e9b70ca2 4641 algoSz = SetAlgoID(RSAk, algo, keyType, 0);
wolfSSL 0:1239e9b70ca2 4642 seqSz = SetSequence(nSz + eSz, seq);
wolfSSL 0:1239e9b70ca2 4643 lenSz = SetLength(seqSz + nSz + eSz + 1, len);
wolfSSL 0:1239e9b70ca2 4644 len[lenSz++] = 0; /* trailing 0 */
wolfSSL 0:1239e9b70ca2 4645
wolfSSL 0:1239e9b70ca2 4646 /* write */
wolfSSL 0:1239e9b70ca2 4647 idx = SetSequence(nSz + eSz + seqSz + lenSz + 1 + algoSz, output);
wolfSSL 0:1239e9b70ca2 4648 /* 1 is for ASN_BIT_STRING */
wolfSSL 0:1239e9b70ca2 4649 /* algo */
wolfSSL 0:1239e9b70ca2 4650 XMEMCPY(output + idx, algo, algoSz);
wolfSSL 0:1239e9b70ca2 4651 idx += algoSz;
wolfSSL 0:1239e9b70ca2 4652 /* bit string */
wolfSSL 0:1239e9b70ca2 4653 output[idx++] = ASN_BIT_STRING;
wolfSSL 0:1239e9b70ca2 4654 /* length */
wolfSSL 0:1239e9b70ca2 4655 XMEMCPY(output + idx, len, lenSz);
wolfSSL 0:1239e9b70ca2 4656 idx += lenSz;
wolfSSL 0:1239e9b70ca2 4657 /* seq */
wolfSSL 0:1239e9b70ca2 4658 XMEMCPY(output + idx, seq, seqSz);
wolfSSL 0:1239e9b70ca2 4659 idx += seqSz;
wolfSSL 0:1239e9b70ca2 4660 /* n */
wolfSSL 0:1239e9b70ca2 4661 XMEMCPY(output + idx, n, nSz);
wolfSSL 0:1239e9b70ca2 4662 idx += nSz;
wolfSSL 0:1239e9b70ca2 4663 /* e */
wolfSSL 0:1239e9b70ca2 4664 XMEMCPY(output + idx, e, eSz);
wolfSSL 0:1239e9b70ca2 4665 idx += eSz;
wolfSSL 0:1239e9b70ca2 4666
wolfSSL 0:1239e9b70ca2 4667 return idx;
wolfSSL 0:1239e9b70ca2 4668 }
wolfSSL 0:1239e9b70ca2 4669
wolfSSL 0:1239e9b70ca2 4670
wolfSSL 0:1239e9b70ca2 4671 static INLINE byte itob(int number)
wolfSSL 0:1239e9b70ca2 4672 {
wolfSSL 0:1239e9b70ca2 4673 return (byte)number + 0x30;
wolfSSL 0:1239e9b70ca2 4674 }
wolfSSL 0:1239e9b70ca2 4675
wolfSSL 0:1239e9b70ca2 4676
wolfSSL 0:1239e9b70ca2 4677 /* write time to output, format */
wolfSSL 0:1239e9b70ca2 4678 static void SetTime(struct tm* date, byte* output)
wolfSSL 0:1239e9b70ca2 4679 {
wolfSSL 0:1239e9b70ca2 4680 int i = 0;
wolfSSL 0:1239e9b70ca2 4681
wolfSSL 0:1239e9b70ca2 4682 output[i++] = itob((date->tm_year % 10000) / 1000);
wolfSSL 0:1239e9b70ca2 4683 output[i++] = itob((date->tm_year % 1000) / 100);
wolfSSL 0:1239e9b70ca2 4684 output[i++] = itob((date->tm_year % 100) / 10);
wolfSSL 0:1239e9b70ca2 4685 output[i++] = itob( date->tm_year % 10);
wolfSSL 0:1239e9b70ca2 4686
wolfSSL 0:1239e9b70ca2 4687 output[i++] = itob(date->tm_mon / 10);
wolfSSL 0:1239e9b70ca2 4688 output[i++] = itob(date->tm_mon % 10);
wolfSSL 0:1239e9b70ca2 4689
wolfSSL 0:1239e9b70ca2 4690 output[i++] = itob(date->tm_mday / 10);
wolfSSL 0:1239e9b70ca2 4691 output[i++] = itob(date->tm_mday % 10);
wolfSSL 0:1239e9b70ca2 4692
wolfSSL 0:1239e9b70ca2 4693 output[i++] = itob(date->tm_hour / 10);
wolfSSL 0:1239e9b70ca2 4694 output[i++] = itob(date->tm_hour % 10);
wolfSSL 0:1239e9b70ca2 4695
wolfSSL 0:1239e9b70ca2 4696 output[i++] = itob(date->tm_min / 10);
wolfSSL 0:1239e9b70ca2 4697 output[i++] = itob(date->tm_min % 10);
wolfSSL 0:1239e9b70ca2 4698
wolfSSL 0:1239e9b70ca2 4699 output[i++] = itob(date->tm_sec / 10);
wolfSSL 0:1239e9b70ca2 4700 output[i++] = itob(date->tm_sec % 10);
wolfSSL 0:1239e9b70ca2 4701
wolfSSL 0:1239e9b70ca2 4702 output[i] = 'Z'; /* Zulu profile */
wolfSSL 0:1239e9b70ca2 4703 }
wolfSSL 0:1239e9b70ca2 4704
wolfSSL 0:1239e9b70ca2 4705
wolfSSL 0:1239e9b70ca2 4706 #ifdef CYASSL_ALT_NAMES
wolfSSL 0:1239e9b70ca2 4707
wolfSSL 0:1239e9b70ca2 4708 /* Copy Dates from cert, return bytes written */
wolfSSL 0:1239e9b70ca2 4709 static int CopyValidity(byte* output, Cert* cert)
wolfSSL 0:1239e9b70ca2 4710 {
wolfSSL 0:1239e9b70ca2 4711 int seqSz;
wolfSSL 0:1239e9b70ca2 4712
wolfSSL 0:1239e9b70ca2 4713 CYASSL_ENTER("CopyValidity");
wolfSSL 0:1239e9b70ca2 4714
wolfSSL 0:1239e9b70ca2 4715 /* headers and output */
wolfSSL 0:1239e9b70ca2 4716 seqSz = SetSequence(cert->beforeDateSz + cert->afterDateSz, output);
wolfSSL 0:1239e9b70ca2 4717 XMEMCPY(output + seqSz, cert->beforeDate, cert->beforeDateSz);
wolfSSL 0:1239e9b70ca2 4718 XMEMCPY(output + seqSz + cert->beforeDateSz, cert->afterDate,
wolfSSL 0:1239e9b70ca2 4719 cert->afterDateSz);
wolfSSL 0:1239e9b70ca2 4720 return seqSz + cert->beforeDateSz + cert->afterDateSz;
wolfSSL 0:1239e9b70ca2 4721 }
wolfSSL 0:1239e9b70ca2 4722
wolfSSL 0:1239e9b70ca2 4723 #endif
wolfSSL 0:1239e9b70ca2 4724
wolfSSL 0:1239e9b70ca2 4725
wolfSSL 0:1239e9b70ca2 4726 /* Set Date validity from now until now + daysValid */
wolfSSL 0:1239e9b70ca2 4727 static int SetValidity(byte* output, int daysValid)
wolfSSL 0:1239e9b70ca2 4728 {
wolfSSL 0:1239e9b70ca2 4729 byte before[MAX_DATE_SIZE];
wolfSSL 0:1239e9b70ca2 4730 byte after[MAX_DATE_SIZE];
wolfSSL 0:1239e9b70ca2 4731
wolfSSL 0:1239e9b70ca2 4732 int beforeSz;
wolfSSL 0:1239e9b70ca2 4733 int afterSz;
wolfSSL 0:1239e9b70ca2 4734 int seqSz;
wolfSSL 0:1239e9b70ca2 4735
wolfSSL 0:1239e9b70ca2 4736 time_t ticks;
wolfSSL 0:1239e9b70ca2 4737 struct tm* now;
wolfSSL 0:1239e9b70ca2 4738 struct tm local;
wolfSSL 0:1239e9b70ca2 4739
wolfSSL 0:1239e9b70ca2 4740 ticks = XTIME(0);
wolfSSL 0:1239e9b70ca2 4741 now = XGMTIME(&ticks);
wolfSSL 0:1239e9b70ca2 4742
wolfSSL 0:1239e9b70ca2 4743 /* before now */
wolfSSL 0:1239e9b70ca2 4744 local = *now;
wolfSSL 0:1239e9b70ca2 4745 before[0] = ASN_GENERALIZED_TIME;
wolfSSL 0:1239e9b70ca2 4746 beforeSz = SetLength(ASN_GEN_TIME_SZ, before + 1) + 1; /* gen tag */
wolfSSL 0:1239e9b70ca2 4747
wolfSSL 0:1239e9b70ca2 4748 /* subtract 1 day for more compliance */
wolfSSL 0:1239e9b70ca2 4749 local.tm_mday -= 1;
wolfSSL 0:1239e9b70ca2 4750 mktime(&local);
wolfSSL 0:1239e9b70ca2 4751
wolfSSL 0:1239e9b70ca2 4752 /* adjust */
wolfSSL 0:1239e9b70ca2 4753 local.tm_year += 1900;
wolfSSL 0:1239e9b70ca2 4754 local.tm_mon += 1;
wolfSSL 0:1239e9b70ca2 4755
wolfSSL 0:1239e9b70ca2 4756 SetTime(&local, before + beforeSz);
wolfSSL 0:1239e9b70ca2 4757 beforeSz += ASN_GEN_TIME_SZ;
wolfSSL 0:1239e9b70ca2 4758
wolfSSL 0:1239e9b70ca2 4759 /* after now + daysValid */
wolfSSL 0:1239e9b70ca2 4760 local = *now;
wolfSSL 0:1239e9b70ca2 4761 after[0] = ASN_GENERALIZED_TIME;
wolfSSL 0:1239e9b70ca2 4762 afterSz = SetLength(ASN_GEN_TIME_SZ, after + 1) + 1; /* gen tag */
wolfSSL 0:1239e9b70ca2 4763
wolfSSL 0:1239e9b70ca2 4764 /* add daysValid */
wolfSSL 0:1239e9b70ca2 4765 local.tm_mday += daysValid;
wolfSSL 0:1239e9b70ca2 4766 mktime(&local);
wolfSSL 0:1239e9b70ca2 4767
wolfSSL 0:1239e9b70ca2 4768 /* adjust */
wolfSSL 0:1239e9b70ca2 4769 local.tm_year += 1900;
wolfSSL 0:1239e9b70ca2 4770 local.tm_mon += 1;
wolfSSL 0:1239e9b70ca2 4771
wolfSSL 0:1239e9b70ca2 4772 SetTime(&local, after + afterSz);
wolfSSL 0:1239e9b70ca2 4773 afterSz += ASN_GEN_TIME_SZ;
wolfSSL 0:1239e9b70ca2 4774
wolfSSL 0:1239e9b70ca2 4775 /* headers and output */
wolfSSL 0:1239e9b70ca2 4776 seqSz = SetSequence(beforeSz + afterSz, output);
wolfSSL 0:1239e9b70ca2 4777 XMEMCPY(output + seqSz, before, beforeSz);
wolfSSL 0:1239e9b70ca2 4778 XMEMCPY(output + seqSz + beforeSz, after, afterSz);
wolfSSL 0:1239e9b70ca2 4779
wolfSSL 0:1239e9b70ca2 4780 return seqSz + beforeSz + afterSz;
wolfSSL 0:1239e9b70ca2 4781 }
wolfSSL 0:1239e9b70ca2 4782
wolfSSL 0:1239e9b70ca2 4783
wolfSSL 0:1239e9b70ca2 4784 /* ASN Encoded Name field */
wolfSSL 0:1239e9b70ca2 4785 typedef struct EncodedName {
wolfSSL 0:1239e9b70ca2 4786 int nameLen; /* actual string value length */
wolfSSL 0:1239e9b70ca2 4787 int totalLen; /* total encoded length */
wolfSSL 0:1239e9b70ca2 4788 int type; /* type of name */
wolfSSL 0:1239e9b70ca2 4789 int used; /* are we actually using this one */
wolfSSL 0:1239e9b70ca2 4790 byte encoded[CTC_NAME_SIZE * 2]; /* encoding */
wolfSSL 0:1239e9b70ca2 4791 } EncodedName;
wolfSSL 0:1239e9b70ca2 4792
wolfSSL 0:1239e9b70ca2 4793
wolfSSL 0:1239e9b70ca2 4794 /* Get Which Name from index */
wolfSSL 0:1239e9b70ca2 4795 static const char* GetOneName(CertName* name, int idx)
wolfSSL 0:1239e9b70ca2 4796 {
wolfSSL 0:1239e9b70ca2 4797 switch (idx) {
wolfSSL 0:1239e9b70ca2 4798 case 0:
wolfSSL 0:1239e9b70ca2 4799 return name->country;
wolfSSL 0:1239e9b70ca2 4800
wolfSSL 0:1239e9b70ca2 4801 case 1:
wolfSSL 0:1239e9b70ca2 4802 return name->state;
wolfSSL 0:1239e9b70ca2 4803
wolfSSL 0:1239e9b70ca2 4804 case 2:
wolfSSL 0:1239e9b70ca2 4805 return name->locality;
wolfSSL 0:1239e9b70ca2 4806
wolfSSL 0:1239e9b70ca2 4807 case 3:
wolfSSL 0:1239e9b70ca2 4808 return name->sur;
wolfSSL 0:1239e9b70ca2 4809
wolfSSL 0:1239e9b70ca2 4810 case 4:
wolfSSL 0:1239e9b70ca2 4811 return name->org;
wolfSSL 0:1239e9b70ca2 4812
wolfSSL 0:1239e9b70ca2 4813 case 5:
wolfSSL 0:1239e9b70ca2 4814 return name->unit;
wolfSSL 0:1239e9b70ca2 4815
wolfSSL 0:1239e9b70ca2 4816 case 6:
wolfSSL 0:1239e9b70ca2 4817 return name->commonName;
wolfSSL 0:1239e9b70ca2 4818
wolfSSL 0:1239e9b70ca2 4819 case 7:
wolfSSL 0:1239e9b70ca2 4820 return name->email;
wolfSSL 0:1239e9b70ca2 4821
wolfSSL 0:1239e9b70ca2 4822 default:
wolfSSL 0:1239e9b70ca2 4823 return 0;
wolfSSL 0:1239e9b70ca2 4824 }
wolfSSL 0:1239e9b70ca2 4825 }
wolfSSL 0:1239e9b70ca2 4826
wolfSSL 0:1239e9b70ca2 4827
wolfSSL 0:1239e9b70ca2 4828 /* Get ASN Name from index */
wolfSSL 0:1239e9b70ca2 4829 static byte GetNameId(int idx)
wolfSSL 0:1239e9b70ca2 4830 {
wolfSSL 0:1239e9b70ca2 4831 switch (idx) {
wolfSSL 0:1239e9b70ca2 4832 case 0:
wolfSSL 0:1239e9b70ca2 4833 return ASN_COUNTRY_NAME;
wolfSSL 0:1239e9b70ca2 4834
wolfSSL 0:1239e9b70ca2 4835 case 1:
wolfSSL 0:1239e9b70ca2 4836 return ASN_STATE_NAME;
wolfSSL 0:1239e9b70ca2 4837
wolfSSL 0:1239e9b70ca2 4838 case 2:
wolfSSL 0:1239e9b70ca2 4839 return ASN_LOCALITY_NAME;
wolfSSL 0:1239e9b70ca2 4840
wolfSSL 0:1239e9b70ca2 4841 case 3:
wolfSSL 0:1239e9b70ca2 4842 return ASN_SUR_NAME;
wolfSSL 0:1239e9b70ca2 4843
wolfSSL 0:1239e9b70ca2 4844 case 4:
wolfSSL 0:1239e9b70ca2 4845 return ASN_ORG_NAME;
wolfSSL 0:1239e9b70ca2 4846
wolfSSL 0:1239e9b70ca2 4847 case 5:
wolfSSL 0:1239e9b70ca2 4848 return ASN_ORGUNIT_NAME;
wolfSSL 0:1239e9b70ca2 4849
wolfSSL 0:1239e9b70ca2 4850 case 6:
wolfSSL 0:1239e9b70ca2 4851 return ASN_COMMON_NAME;
wolfSSL 0:1239e9b70ca2 4852
wolfSSL 0:1239e9b70ca2 4853 case 7:
wolfSSL 0:1239e9b70ca2 4854 /* email uses different id type */
wolfSSL 0:1239e9b70ca2 4855 return 0;
wolfSSL 0:1239e9b70ca2 4856
wolfSSL 0:1239e9b70ca2 4857 default:
wolfSSL 0:1239e9b70ca2 4858 return 0;
wolfSSL 0:1239e9b70ca2 4859 }
wolfSSL 0:1239e9b70ca2 4860 }
wolfSSL 0:1239e9b70ca2 4861
wolfSSL 0:1239e9b70ca2 4862
wolfSSL 0:1239e9b70ca2 4863 /* encode all extensions, return total bytes written */
wolfSSL 0:1239e9b70ca2 4864 static int SetExtensions(byte* output, const byte* ext, int extSz, int header)
wolfSSL 0:1239e9b70ca2 4865 {
wolfSSL 0:1239e9b70ca2 4866 byte sequence[MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 4867 byte len[MAX_LENGTH_SZ];
wolfSSL 0:1239e9b70ca2 4868
wolfSSL 0:1239e9b70ca2 4869 int sz = 0;
wolfSSL 0:1239e9b70ca2 4870 int seqSz = SetSequence(extSz, sequence);
wolfSSL 0:1239e9b70ca2 4871
wolfSSL 0:1239e9b70ca2 4872 if (header) {
wolfSSL 0:1239e9b70ca2 4873 int lenSz = SetLength(seqSz + extSz, len);
wolfSSL 0:1239e9b70ca2 4874 output[0] = ASN_EXTENSIONS; /* extensions id */
wolfSSL 0:1239e9b70ca2 4875 sz++;
wolfSSL 0:1239e9b70ca2 4876 XMEMCPY(&output[sz], len, lenSz); /* length */
wolfSSL 0:1239e9b70ca2 4877 sz += lenSz;
wolfSSL 0:1239e9b70ca2 4878 }
wolfSSL 0:1239e9b70ca2 4879 XMEMCPY(&output[sz], sequence, seqSz); /* sequence */
wolfSSL 0:1239e9b70ca2 4880 sz += seqSz;
wolfSSL 0:1239e9b70ca2 4881 XMEMCPY(&output[sz], ext, extSz); /* extensions */
wolfSSL 0:1239e9b70ca2 4882 sz += extSz;
wolfSSL 0:1239e9b70ca2 4883
wolfSSL 0:1239e9b70ca2 4884 return sz;
wolfSSL 0:1239e9b70ca2 4885 }
wolfSSL 0:1239e9b70ca2 4886
wolfSSL 0:1239e9b70ca2 4887
wolfSSL 0:1239e9b70ca2 4888 /* encode CA basic constraint true, return total bytes written */
wolfSSL 0:1239e9b70ca2 4889 static int SetCa(byte* output)
wolfSSL 0:1239e9b70ca2 4890 {
wolfSSL 0:1239e9b70ca2 4891 static const byte ca[] = { 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04,
wolfSSL 0:1239e9b70ca2 4892 0x05, 0x30, 0x03, 0x01, 0x01, 0xff };
wolfSSL 0:1239e9b70ca2 4893
wolfSSL 0:1239e9b70ca2 4894 XMEMCPY(output, ca, sizeof(ca));
wolfSSL 0:1239e9b70ca2 4895
wolfSSL 0:1239e9b70ca2 4896 return (int)sizeof(ca);
wolfSSL 0:1239e9b70ca2 4897 }
wolfSSL 0:1239e9b70ca2 4898
wolfSSL 0:1239e9b70ca2 4899
wolfSSL 0:1239e9b70ca2 4900 /* encode CertName into output, return total bytes written */
wolfSSL 0:1239e9b70ca2 4901 static int SetName(byte* output, CertName* name)
wolfSSL 0:1239e9b70ca2 4902 {
wolfSSL 0:1239e9b70ca2 4903 int totalBytes = 0, i, idx;
wolfSSL 0:1239e9b70ca2 4904 EncodedName names[NAME_ENTRIES];
wolfSSL 0:1239e9b70ca2 4905
wolfSSL 0:1239e9b70ca2 4906 for (i = 0; i < NAME_ENTRIES; i++) {
wolfSSL 0:1239e9b70ca2 4907 const char* nameStr = GetOneName(name, i);
wolfSSL 0:1239e9b70ca2 4908 if (nameStr) {
wolfSSL 0:1239e9b70ca2 4909 /* bottom up */
wolfSSL 0:1239e9b70ca2 4910 byte firstLen[MAX_LENGTH_SZ];
wolfSSL 0:1239e9b70ca2 4911 byte secondLen[MAX_LENGTH_SZ];
wolfSSL 0:1239e9b70ca2 4912 byte sequence[MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 4913 byte set[MAX_SET_SZ];
wolfSSL 0:1239e9b70ca2 4914
wolfSSL 0:1239e9b70ca2 4915 int email = i == (NAME_ENTRIES - 1) ? 1 : 0;
wolfSSL 0:1239e9b70ca2 4916 int strLen = (int)XSTRLEN(nameStr);
wolfSSL 0:1239e9b70ca2 4917 int thisLen = strLen;
wolfSSL 0:1239e9b70ca2 4918 int firstSz, secondSz, seqSz, setSz;
wolfSSL 0:1239e9b70ca2 4919
wolfSSL 0:1239e9b70ca2 4920 if (strLen == 0) { /* no user data for this item */
wolfSSL 0:1239e9b70ca2 4921 names[i].used = 0;
wolfSSL 0:1239e9b70ca2 4922 continue;
wolfSSL 0:1239e9b70ca2 4923 }
wolfSSL 0:1239e9b70ca2 4924
wolfSSL 0:1239e9b70ca2 4925 secondSz = SetLength(strLen, secondLen);
wolfSSL 0:1239e9b70ca2 4926 thisLen += secondSz;
wolfSSL 0:1239e9b70ca2 4927 if (email) {
wolfSSL 0:1239e9b70ca2 4928 thisLen += EMAIL_JOINT_LEN;
wolfSSL 0:1239e9b70ca2 4929 thisLen ++; /* id type */
wolfSSL 0:1239e9b70ca2 4930 firstSz = SetLength(EMAIL_JOINT_LEN, firstLen);
wolfSSL 0:1239e9b70ca2 4931 }
wolfSSL 0:1239e9b70ca2 4932 else {
wolfSSL 0:1239e9b70ca2 4933 thisLen++; /* str type */
wolfSSL 0:1239e9b70ca2 4934 thisLen++; /* id type */
wolfSSL 0:1239e9b70ca2 4935 thisLen += JOINT_LEN;
wolfSSL 0:1239e9b70ca2 4936 firstSz = SetLength(JOINT_LEN + 1, firstLen);
wolfSSL 0:1239e9b70ca2 4937 }
wolfSSL 0:1239e9b70ca2 4938 thisLen += firstSz;
wolfSSL 0:1239e9b70ca2 4939 thisLen++; /* object id */
wolfSSL 0:1239e9b70ca2 4940
wolfSSL 0:1239e9b70ca2 4941 seqSz = SetSequence(thisLen, sequence);
wolfSSL 0:1239e9b70ca2 4942 thisLen += seqSz;
wolfSSL 0:1239e9b70ca2 4943 setSz = SetSet(thisLen, set);
wolfSSL 0:1239e9b70ca2 4944 thisLen += setSz;
wolfSSL 0:1239e9b70ca2 4945
wolfSSL 0:1239e9b70ca2 4946 if (thisLen > (int)sizeof(names[i].encoded))
wolfSSL 0:1239e9b70ca2 4947 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 4948
wolfSSL 0:1239e9b70ca2 4949 /* store it */
wolfSSL 0:1239e9b70ca2 4950 idx = 0;
wolfSSL 0:1239e9b70ca2 4951 /* set */
wolfSSL 0:1239e9b70ca2 4952 XMEMCPY(names[i].encoded, set, setSz);
wolfSSL 0:1239e9b70ca2 4953 idx += setSz;
wolfSSL 0:1239e9b70ca2 4954 /* seq */
wolfSSL 0:1239e9b70ca2 4955 XMEMCPY(names[i].encoded + idx, sequence, seqSz);
wolfSSL 0:1239e9b70ca2 4956 idx += seqSz;
wolfSSL 0:1239e9b70ca2 4957 /* asn object id */
wolfSSL 0:1239e9b70ca2 4958 names[i].encoded[idx++] = ASN_OBJECT_ID;
wolfSSL 0:1239e9b70ca2 4959 /* first length */
wolfSSL 0:1239e9b70ca2 4960 XMEMCPY(names[i].encoded + idx, firstLen, firstSz);
wolfSSL 0:1239e9b70ca2 4961 idx += firstSz;
wolfSSL 0:1239e9b70ca2 4962 if (email) {
wolfSSL 0:1239e9b70ca2 4963 const byte EMAIL_OID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
wolfSSL 0:1239e9b70ca2 4964 0x01, 0x09, 0x01, 0x16 };
wolfSSL 0:1239e9b70ca2 4965 /* email joint id */
wolfSSL 0:1239e9b70ca2 4966 XMEMCPY(names[i].encoded + idx, EMAIL_OID, sizeof(EMAIL_OID));
wolfSSL 0:1239e9b70ca2 4967 idx += (int)sizeof(EMAIL_OID);
wolfSSL 0:1239e9b70ca2 4968 }
wolfSSL 0:1239e9b70ca2 4969 else {
wolfSSL 0:1239e9b70ca2 4970 /* joint id */
wolfSSL 0:1239e9b70ca2 4971 byte bType = GetNameId(i);
wolfSSL 0:1239e9b70ca2 4972 names[i].encoded[idx++] = 0x55;
wolfSSL 0:1239e9b70ca2 4973 names[i].encoded[idx++] = 0x04;
wolfSSL 0:1239e9b70ca2 4974 /* id type */
wolfSSL 0:1239e9b70ca2 4975 names[i].encoded[idx++] = bType;
wolfSSL 0:1239e9b70ca2 4976 /* str type */
wolfSSL 0:1239e9b70ca2 4977 if (bType == ASN_COUNTRY_NAME)
wolfSSL 0:1239e9b70ca2 4978 names[i].encoded[idx++] = 0x13; /* printable */
wolfSSL 0:1239e9b70ca2 4979 else
wolfSSL 0:1239e9b70ca2 4980 names[i].encoded[idx++] = 0x0c; /* utf8 */
wolfSSL 0:1239e9b70ca2 4981 }
wolfSSL 0:1239e9b70ca2 4982 /* second length */
wolfSSL 0:1239e9b70ca2 4983 XMEMCPY(names[i].encoded + idx, secondLen, secondSz);
wolfSSL 0:1239e9b70ca2 4984 idx += secondSz;
wolfSSL 0:1239e9b70ca2 4985 /* str value */
wolfSSL 0:1239e9b70ca2 4986 XMEMCPY(names[i].encoded + idx, nameStr, strLen);
wolfSSL 0:1239e9b70ca2 4987 idx += strLen;
wolfSSL 0:1239e9b70ca2 4988
wolfSSL 0:1239e9b70ca2 4989 totalBytes += idx;
wolfSSL 0:1239e9b70ca2 4990 names[i].totalLen = idx;
wolfSSL 0:1239e9b70ca2 4991 names[i].used = 1;
wolfSSL 0:1239e9b70ca2 4992 }
wolfSSL 0:1239e9b70ca2 4993 else
wolfSSL 0:1239e9b70ca2 4994 names[i].used = 0;
wolfSSL 0:1239e9b70ca2 4995 }
wolfSSL 0:1239e9b70ca2 4996
wolfSSL 0:1239e9b70ca2 4997 /* header */
wolfSSL 0:1239e9b70ca2 4998 idx = SetSequence(totalBytes, output);
wolfSSL 0:1239e9b70ca2 4999 totalBytes += idx;
wolfSSL 0:1239e9b70ca2 5000 if (totalBytes > ASN_NAME_MAX)
wolfSSL 0:1239e9b70ca2 5001 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 5002
wolfSSL 0:1239e9b70ca2 5003 for (i = 0; i < NAME_ENTRIES; i++) {
wolfSSL 0:1239e9b70ca2 5004 if (names[i].used) {
wolfSSL 0:1239e9b70ca2 5005 XMEMCPY(output + idx, names[i].encoded, names[i].totalLen);
wolfSSL 0:1239e9b70ca2 5006 idx += names[i].totalLen;
wolfSSL 0:1239e9b70ca2 5007 }
wolfSSL 0:1239e9b70ca2 5008 }
wolfSSL 0:1239e9b70ca2 5009 return totalBytes;
wolfSSL 0:1239e9b70ca2 5010 }
wolfSSL 0:1239e9b70ca2 5011
wolfSSL 0:1239e9b70ca2 5012 /* encode info from cert into DER encoded format */
wolfSSL 0:1239e9b70ca2 5013 static int EncodeCert(Cert* cert, DerCert* der, RsaKey* rsaKey, ecc_key* eccKey,
wolfSSL 0:1239e9b70ca2 5014 RNG* rng, const byte* ntruKey, word16 ntruSz)
wolfSSL 0:1239e9b70ca2 5015 {
wolfSSL 0:1239e9b70ca2 5016 int ret;
wolfSSL 0:1239e9b70ca2 5017
wolfSSL 0:1239e9b70ca2 5018 (void)eccKey;
wolfSSL 0:1239e9b70ca2 5019 (void)ntruKey;
wolfSSL 0:1239e9b70ca2 5020 (void)ntruSz;
wolfSSL 0:1239e9b70ca2 5021
wolfSSL 0:1239e9b70ca2 5022 /* init */
wolfSSL 0:1239e9b70ca2 5023 XMEMSET(der, 0, sizeof(DerCert));
wolfSSL 0:1239e9b70ca2 5024
wolfSSL 0:1239e9b70ca2 5025 /* version */
wolfSSL 0:1239e9b70ca2 5026 der->versionSz = SetMyVersion(cert->version, der->version, TRUE);
wolfSSL 0:1239e9b70ca2 5027
wolfSSL 0:1239e9b70ca2 5028 /* serial number */
wolfSSL 0:1239e9b70ca2 5029 ret = RNG_GenerateBlock(rng, cert->serial, CTC_SERIAL_SIZE);
wolfSSL 0:1239e9b70ca2 5030 if (ret != 0)
wolfSSL 0:1239e9b70ca2 5031 return ret;
wolfSSL 0:1239e9b70ca2 5032
wolfSSL 0:1239e9b70ca2 5033 cert->serial[0] = 0x01; /* ensure positive */
wolfSSL 0:1239e9b70ca2 5034 der->serialSz = SetSerial(cert->serial, der->serial);
wolfSSL 0:1239e9b70ca2 5035
wolfSSL 0:1239e9b70ca2 5036 /* signature algo */
wolfSSL 0:1239e9b70ca2 5037 der->sigAlgoSz = SetAlgoID(cert->sigType, der->sigAlgo, sigType, 0);
wolfSSL 0:1239e9b70ca2 5038 if (der->sigAlgoSz == 0)
wolfSSL 0:1239e9b70ca2 5039 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 5040
wolfSSL 0:1239e9b70ca2 5041 /* public key */
wolfSSL 0:1239e9b70ca2 5042 if (cert->keyType == RSA_KEY) {
wolfSSL 0:1239e9b70ca2 5043 if (rsaKey == NULL)
wolfSSL 0:1239e9b70ca2 5044 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5045 der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey);
wolfSSL 0:1239e9b70ca2 5046 if (der->publicKeySz <= 0)
wolfSSL 0:1239e9b70ca2 5047 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5048 }
wolfSSL 0:1239e9b70ca2 5049
wolfSSL 0:1239e9b70ca2 5050 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 5051 if (cert->keyType == ECC_KEY) {
wolfSSL 0:1239e9b70ca2 5052 if (eccKey == NULL)
wolfSSL 0:1239e9b70ca2 5053 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5054 der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey);
wolfSSL 0:1239e9b70ca2 5055 if (der->publicKeySz <= 0)
wolfSSL 0:1239e9b70ca2 5056 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5057 }
wolfSSL 0:1239e9b70ca2 5058 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 5059
wolfSSL 0:1239e9b70ca2 5060 #ifdef HAVE_NTRU
wolfSSL 0:1239e9b70ca2 5061 if (cert->keyType == NTRU_KEY) {
wolfSSL 0:1239e9b70ca2 5062 word32 rc;
wolfSSL 0:1239e9b70ca2 5063 word16 encodedSz;
wolfSSL 0:1239e9b70ca2 5064
wolfSSL 0:1239e9b70ca2 5065 rc = crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo( ntruSz,
wolfSSL 0:1239e9b70ca2 5066 ntruKey, &encodedSz, NULL);
wolfSSL 0:1239e9b70ca2 5067 if (rc != NTRU_OK)
wolfSSL 0:1239e9b70ca2 5068 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5069 if (encodedSz > MAX_PUBLIC_KEY_SZ)
wolfSSL 0:1239e9b70ca2 5070 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5071
wolfSSL 0:1239e9b70ca2 5072 rc = crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo( ntruSz,
wolfSSL 0:1239e9b70ca2 5073 ntruKey, &encodedSz, der->publicKey);
wolfSSL 0:1239e9b70ca2 5074 if (rc != NTRU_OK)
wolfSSL 0:1239e9b70ca2 5075 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5076
wolfSSL 0:1239e9b70ca2 5077 der->publicKeySz = encodedSz;
wolfSSL 0:1239e9b70ca2 5078 }
wolfSSL 0:1239e9b70ca2 5079 #endif /* HAVE_NTRU */
wolfSSL 0:1239e9b70ca2 5080
wolfSSL 0:1239e9b70ca2 5081 der->validitySz = 0;
wolfSSL 0:1239e9b70ca2 5082 #ifdef CYASSL_ALT_NAMES
wolfSSL 0:1239e9b70ca2 5083 /* date validity copy ? */
wolfSSL 0:1239e9b70ca2 5084 if (cert->beforeDateSz && cert->afterDateSz) {
wolfSSL 0:1239e9b70ca2 5085 der->validitySz = CopyValidity(der->validity, cert);
wolfSSL 0:1239e9b70ca2 5086 if (der->validitySz == 0)
wolfSSL 0:1239e9b70ca2 5087 return DATE_E;
wolfSSL 0:1239e9b70ca2 5088 }
wolfSSL 0:1239e9b70ca2 5089 #endif
wolfSSL 0:1239e9b70ca2 5090
wolfSSL 0:1239e9b70ca2 5091 /* date validity */
wolfSSL 0:1239e9b70ca2 5092 if (der->validitySz == 0) {
wolfSSL 0:1239e9b70ca2 5093 der->validitySz = SetValidity(der->validity, cert->daysValid);
wolfSSL 0:1239e9b70ca2 5094 if (der->validitySz == 0)
wolfSSL 0:1239e9b70ca2 5095 return DATE_E;
wolfSSL 0:1239e9b70ca2 5096 }
wolfSSL 0:1239e9b70ca2 5097
wolfSSL 0:1239e9b70ca2 5098 /* subject name */
wolfSSL 0:1239e9b70ca2 5099 der->subjectSz = SetName(der->subject, &cert->subject);
wolfSSL 0:1239e9b70ca2 5100 if (der->subjectSz == 0)
wolfSSL 0:1239e9b70ca2 5101 return SUBJECT_E;
wolfSSL 0:1239e9b70ca2 5102
wolfSSL 0:1239e9b70ca2 5103 /* issuer name */
wolfSSL 0:1239e9b70ca2 5104 der->issuerSz = SetName(der->issuer, cert->selfSigned ?
wolfSSL 0:1239e9b70ca2 5105 &cert->subject : &cert->issuer);
wolfSSL 0:1239e9b70ca2 5106 if (der->issuerSz == 0)
wolfSSL 0:1239e9b70ca2 5107 return ISSUER_E;
wolfSSL 0:1239e9b70ca2 5108
wolfSSL 0:1239e9b70ca2 5109 /* CA */
wolfSSL 0:1239e9b70ca2 5110 if (cert->isCA) {
wolfSSL 0:1239e9b70ca2 5111 der->caSz = SetCa(der->ca);
wolfSSL 0:1239e9b70ca2 5112 if (der->caSz == 0)
wolfSSL 0:1239e9b70ca2 5113 return CA_TRUE_E;
wolfSSL 0:1239e9b70ca2 5114 }
wolfSSL 0:1239e9b70ca2 5115 else
wolfSSL 0:1239e9b70ca2 5116 der->caSz = 0;
wolfSSL 0:1239e9b70ca2 5117
wolfSSL 0:1239e9b70ca2 5118 /* extensions, just CA now */
wolfSSL 0:1239e9b70ca2 5119 if (cert->isCA) {
wolfSSL 0:1239e9b70ca2 5120 der->extensionsSz = SetExtensions(der->extensions,
wolfSSL 0:1239e9b70ca2 5121 der->ca, der->caSz, TRUE);
wolfSSL 0:1239e9b70ca2 5122 if (der->extensionsSz == 0)
wolfSSL 0:1239e9b70ca2 5123 return EXTENSIONS_E;
wolfSSL 0:1239e9b70ca2 5124 }
wolfSSL 0:1239e9b70ca2 5125 else
wolfSSL 0:1239e9b70ca2 5126 der->extensionsSz = 0;
wolfSSL 0:1239e9b70ca2 5127
wolfSSL 0:1239e9b70ca2 5128 #ifdef CYASSL_ALT_NAMES
wolfSSL 0:1239e9b70ca2 5129 if (der->extensionsSz == 0 && cert->altNamesSz) {
wolfSSL 0:1239e9b70ca2 5130 der->extensionsSz = SetExtensions(der->extensions, cert->altNames,
wolfSSL 0:1239e9b70ca2 5131 cert->altNamesSz, TRUE);
wolfSSL 0:1239e9b70ca2 5132 if (der->extensionsSz == 0)
wolfSSL 0:1239e9b70ca2 5133 return EXTENSIONS_E;
wolfSSL 0:1239e9b70ca2 5134 }
wolfSSL 0:1239e9b70ca2 5135 #endif
wolfSSL 0:1239e9b70ca2 5136
wolfSSL 0:1239e9b70ca2 5137 der->total = der->versionSz + der->serialSz + der->sigAlgoSz +
wolfSSL 0:1239e9b70ca2 5138 der->publicKeySz + der->validitySz + der->subjectSz + der->issuerSz +
wolfSSL 0:1239e9b70ca2 5139 der->extensionsSz;
wolfSSL 0:1239e9b70ca2 5140
wolfSSL 0:1239e9b70ca2 5141 return 0;
wolfSSL 0:1239e9b70ca2 5142 }
wolfSSL 0:1239e9b70ca2 5143
wolfSSL 0:1239e9b70ca2 5144
wolfSSL 0:1239e9b70ca2 5145 /* write DER encoded cert to buffer, size already checked */
wolfSSL 0:1239e9b70ca2 5146 static int WriteCertBody(DerCert* der, byte* buffer)
wolfSSL 0:1239e9b70ca2 5147 {
wolfSSL 0:1239e9b70ca2 5148 int idx;
wolfSSL 0:1239e9b70ca2 5149
wolfSSL 0:1239e9b70ca2 5150 /* signed part header */
wolfSSL 0:1239e9b70ca2 5151 idx = SetSequence(der->total, buffer);
wolfSSL 0:1239e9b70ca2 5152 /* version */
wolfSSL 0:1239e9b70ca2 5153 XMEMCPY(buffer + idx, der->version, der->versionSz);
wolfSSL 0:1239e9b70ca2 5154 idx += der->versionSz;
wolfSSL 0:1239e9b70ca2 5155 /* serial */
wolfSSL 0:1239e9b70ca2 5156 XMEMCPY(buffer + idx, der->serial, der->serialSz);
wolfSSL 0:1239e9b70ca2 5157 idx += der->serialSz;
wolfSSL 0:1239e9b70ca2 5158 /* sig algo */
wolfSSL 0:1239e9b70ca2 5159 XMEMCPY(buffer + idx, der->sigAlgo, der->sigAlgoSz);
wolfSSL 0:1239e9b70ca2 5160 idx += der->sigAlgoSz;
wolfSSL 0:1239e9b70ca2 5161 /* issuer */
wolfSSL 0:1239e9b70ca2 5162 XMEMCPY(buffer + idx, der->issuer, der->issuerSz);
wolfSSL 0:1239e9b70ca2 5163 idx += der->issuerSz;
wolfSSL 0:1239e9b70ca2 5164 /* validity */
wolfSSL 0:1239e9b70ca2 5165 XMEMCPY(buffer + idx, der->validity, der->validitySz);
wolfSSL 0:1239e9b70ca2 5166 idx += der->validitySz;
wolfSSL 0:1239e9b70ca2 5167 /* subject */
wolfSSL 0:1239e9b70ca2 5168 XMEMCPY(buffer + idx, der->subject, der->subjectSz);
wolfSSL 0:1239e9b70ca2 5169 idx += der->subjectSz;
wolfSSL 0:1239e9b70ca2 5170 /* public key */
wolfSSL 0:1239e9b70ca2 5171 XMEMCPY(buffer + idx, der->publicKey, der->publicKeySz);
wolfSSL 0:1239e9b70ca2 5172 idx += der->publicKeySz;
wolfSSL 0:1239e9b70ca2 5173 if (der->extensionsSz) {
wolfSSL 0:1239e9b70ca2 5174 /* extensions */
wolfSSL 0:1239e9b70ca2 5175 XMEMCPY(buffer + idx, der->extensions, min(der->extensionsSz,
wolfSSL 0:1239e9b70ca2 5176 sizeof(der->extensions)));
wolfSSL 0:1239e9b70ca2 5177 idx += der->extensionsSz;
wolfSSL 0:1239e9b70ca2 5178 }
wolfSSL 0:1239e9b70ca2 5179
wolfSSL 0:1239e9b70ca2 5180 return idx;
wolfSSL 0:1239e9b70ca2 5181 }
wolfSSL 0:1239e9b70ca2 5182
wolfSSL 0:1239e9b70ca2 5183
wolfSSL 0:1239e9b70ca2 5184 /* Make RSA signature from buffer (sz), write to sig (sigSz) */
wolfSSL 0:1239e9b70ca2 5185 static int MakeSignature(const byte* buffer, int sz, byte* sig, int sigSz,
wolfSSL 0:1239e9b70ca2 5186 RsaKey* rsaKey, ecc_key* eccKey, RNG* rng,
wolfSSL 0:1239e9b70ca2 5187 int sigAlgoType)
wolfSSL 0:1239e9b70ca2 5188 {
wolfSSL 0:1239e9b70ca2 5189 byte digest[SHA256_DIGEST_SIZE]; /* max size */
wolfSSL 0:1239e9b70ca2 5190 byte encSig[MAX_ENCODED_DIG_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 5191 int encSigSz, digestSz, typeH, ret = 0;
wolfSSL 0:1239e9b70ca2 5192
wolfSSL 0:1239e9b70ca2 5193 (void)eccKey;
wolfSSL 0:1239e9b70ca2 5194
wolfSSL 0:1239e9b70ca2 5195 if (sigAlgoType == CTC_MD5wRSA) {
wolfSSL 0:1239e9b70ca2 5196 Md5 md5;
wolfSSL 0:1239e9b70ca2 5197
wolfSSL 0:1239e9b70ca2 5198 InitMd5(&md5);
wolfSSL 0:1239e9b70ca2 5199 Md5Update(&md5, buffer, sz);
wolfSSL 0:1239e9b70ca2 5200 Md5Final(&md5, digest);
wolfSSL 0:1239e9b70ca2 5201
wolfSSL 0:1239e9b70ca2 5202 digestSz = MD5_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 5203 typeH = MD5h;
wolfSSL 0:1239e9b70ca2 5204 }
wolfSSL 0:1239e9b70ca2 5205 else if (sigAlgoType == CTC_SHAwRSA || sigAlgoType == CTC_SHAwECDSA) {
wolfSSL 0:1239e9b70ca2 5206 Sha sha;
wolfSSL 0:1239e9b70ca2 5207
wolfSSL 0:1239e9b70ca2 5208 ret = InitSha(&sha);
wolfSSL 0:1239e9b70ca2 5209 if (ret != 0)
wolfSSL 0:1239e9b70ca2 5210 return ret;
wolfSSL 0:1239e9b70ca2 5211
wolfSSL 0:1239e9b70ca2 5212 ShaUpdate(&sha, buffer, sz);
wolfSSL 0:1239e9b70ca2 5213 ShaFinal(&sha, digest);
wolfSSL 0:1239e9b70ca2 5214
wolfSSL 0:1239e9b70ca2 5215 digestSz = SHA_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 5216 typeH = SHAh;
wolfSSL 0:1239e9b70ca2 5217 }
wolfSSL 0:1239e9b70ca2 5218 else if (sigAlgoType == CTC_SHA256wRSA || sigAlgoType == CTC_SHA256wECDSA) {
wolfSSL 0:1239e9b70ca2 5219 Sha256 sha256;
wolfSSL 0:1239e9b70ca2 5220
wolfSSL 0:1239e9b70ca2 5221 ret = InitSha256(&sha256);
wolfSSL 0:1239e9b70ca2 5222 if (ret != 0)
wolfSSL 0:1239e9b70ca2 5223 return ret;
wolfSSL 0:1239e9b70ca2 5224
wolfSSL 0:1239e9b70ca2 5225 ret = Sha256Update(&sha256, buffer, sz);
wolfSSL 0:1239e9b70ca2 5226 if (ret != 0)
wolfSSL 0:1239e9b70ca2 5227 return ret;
wolfSSL 0:1239e9b70ca2 5228
wolfSSL 0:1239e9b70ca2 5229 ret = Sha256Final(&sha256, digest);
wolfSSL 0:1239e9b70ca2 5230 if (ret != 0)
wolfSSL 0:1239e9b70ca2 5231 return ret;
wolfSSL 0:1239e9b70ca2 5232
wolfSSL 0:1239e9b70ca2 5233 digestSz = SHA256_DIGEST_SIZE;
wolfSSL 0:1239e9b70ca2 5234 typeH = SHA256h;
wolfSSL 0:1239e9b70ca2 5235 }
wolfSSL 0:1239e9b70ca2 5236 else
wolfSSL 0:1239e9b70ca2 5237 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 5238
wolfSSL 0:1239e9b70ca2 5239 if (rsaKey) {
wolfSSL 0:1239e9b70ca2 5240 /* signature */
wolfSSL 0:1239e9b70ca2 5241 encSigSz = EncodeSignature(encSig, digest, digestSz, typeH);
wolfSSL 0:1239e9b70ca2 5242 return RsaSSL_Sign(encSig, encSigSz, sig, sigSz, rsaKey, rng);
wolfSSL 0:1239e9b70ca2 5243 }
wolfSSL 0:1239e9b70ca2 5244 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 5245 else if (eccKey) {
wolfSSL 0:1239e9b70ca2 5246 word32 outSz = sigSz;
wolfSSL 0:1239e9b70ca2 5247 ret = ecc_sign_hash(digest, digestSz, sig, &outSz, rng, eccKey);
wolfSSL 0:1239e9b70ca2 5248
wolfSSL 0:1239e9b70ca2 5249 if (ret != 0)
wolfSSL 0:1239e9b70ca2 5250 return ret;
wolfSSL 0:1239e9b70ca2 5251 return outSz;
wolfSSL 0:1239e9b70ca2 5252 }
wolfSSL 0:1239e9b70ca2 5253 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 5254
wolfSSL 0:1239e9b70ca2 5255 return ALGO_ID_E;
wolfSSL 0:1239e9b70ca2 5256 }
wolfSSL 0:1239e9b70ca2 5257
wolfSSL 0:1239e9b70ca2 5258
wolfSSL 0:1239e9b70ca2 5259 /* add signature to end of buffer, size of buffer assumed checked, return
wolfSSL 0:1239e9b70ca2 5260 new length */
wolfSSL 0:1239e9b70ca2 5261 static int AddSignature(byte* buffer, int bodySz, const byte* sig, int sigSz,
wolfSSL 0:1239e9b70ca2 5262 int sigAlgoType)
wolfSSL 0:1239e9b70ca2 5263 {
wolfSSL 0:1239e9b70ca2 5264 byte seq[MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 5265 int idx = bodySz, seqSz;
wolfSSL 0:1239e9b70ca2 5266
wolfSSL 0:1239e9b70ca2 5267 /* algo */
wolfSSL 0:1239e9b70ca2 5268 idx += SetAlgoID(sigAlgoType, buffer + idx, sigType, 0);
wolfSSL 0:1239e9b70ca2 5269 /* bit string */
wolfSSL 0:1239e9b70ca2 5270 buffer[idx++] = ASN_BIT_STRING;
wolfSSL 0:1239e9b70ca2 5271 /* length */
wolfSSL 0:1239e9b70ca2 5272 idx += SetLength(sigSz + 1, buffer + idx);
wolfSSL 0:1239e9b70ca2 5273 buffer[idx++] = 0; /* trailing 0 */
wolfSSL 0:1239e9b70ca2 5274 /* signature */
wolfSSL 0:1239e9b70ca2 5275 XMEMCPY(buffer + idx, sig, sigSz);
wolfSSL 0:1239e9b70ca2 5276 idx += sigSz;
wolfSSL 0:1239e9b70ca2 5277
wolfSSL 0:1239e9b70ca2 5278 /* make room for overall header */
wolfSSL 0:1239e9b70ca2 5279 seqSz = SetSequence(idx, seq);
wolfSSL 0:1239e9b70ca2 5280 XMEMMOVE(buffer + seqSz, buffer, idx);
wolfSSL 0:1239e9b70ca2 5281 XMEMCPY(buffer, seq, seqSz);
wolfSSL 0:1239e9b70ca2 5282
wolfSSL 0:1239e9b70ca2 5283 return idx + seqSz;
wolfSSL 0:1239e9b70ca2 5284 }
wolfSSL 0:1239e9b70ca2 5285
wolfSSL 0:1239e9b70ca2 5286
wolfSSL 0:1239e9b70ca2 5287 /* Make an x509 Certificate v3 any key type from cert input, write to buffer */
wolfSSL 0:1239e9b70ca2 5288 static int MakeAnyCert(Cert* cert, byte* derBuffer, word32 derSz,
wolfSSL 0:1239e9b70ca2 5289 RsaKey* rsaKey, ecc_key* eccKey, RNG* rng,
wolfSSL 0:1239e9b70ca2 5290 const byte* ntruKey, word16 ntruSz)
wolfSSL 0:1239e9b70ca2 5291 {
wolfSSL 0:1239e9b70ca2 5292 DerCert der;
wolfSSL 0:1239e9b70ca2 5293 int ret;
wolfSSL 0:1239e9b70ca2 5294
wolfSSL 0:1239e9b70ca2 5295 if (eccKey)
wolfSSL 0:1239e9b70ca2 5296 cert->keyType = ECC_KEY;
wolfSSL 0:1239e9b70ca2 5297 else
wolfSSL 0:1239e9b70ca2 5298 cert->keyType = rsaKey ? RSA_KEY : NTRU_KEY;
wolfSSL 0:1239e9b70ca2 5299 ret = EncodeCert(cert, &der, rsaKey, eccKey, rng, ntruKey, ntruSz);
wolfSSL 0:1239e9b70ca2 5300 if (ret != 0)
wolfSSL 0:1239e9b70ca2 5301 return ret;
wolfSSL 0:1239e9b70ca2 5302
wolfSSL 0:1239e9b70ca2 5303 if (der.total + MAX_SEQ_SZ * 2 > (int)derSz)
wolfSSL 0:1239e9b70ca2 5304 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 5305
wolfSSL 0:1239e9b70ca2 5306 return cert->bodySz = WriteCertBody(&der, derBuffer);
wolfSSL 0:1239e9b70ca2 5307 }
wolfSSL 0:1239e9b70ca2 5308
wolfSSL 0:1239e9b70ca2 5309
wolfSSL 0:1239e9b70ca2 5310 /* Make an x509 Certificate v3 RSA or ECC from cert input, write to buffer */
wolfSSL 0:1239e9b70ca2 5311 int MakeCert(Cert* cert, byte* derBuffer, word32 derSz, RsaKey* rsaKey,
wolfSSL 0:1239e9b70ca2 5312 ecc_key* eccKey, RNG* rng)
wolfSSL 0:1239e9b70ca2 5313 {
wolfSSL 0:1239e9b70ca2 5314 return MakeAnyCert(cert, derBuffer, derSz, rsaKey, eccKey, rng, NULL, 0);
wolfSSL 0:1239e9b70ca2 5315 }
wolfSSL 0:1239e9b70ca2 5316
wolfSSL 0:1239e9b70ca2 5317
wolfSSL 0:1239e9b70ca2 5318 #ifdef HAVE_NTRU
wolfSSL 0:1239e9b70ca2 5319
wolfSSL 0:1239e9b70ca2 5320 int MakeNtruCert(Cert* cert, byte* derBuffer, word32 derSz,
wolfSSL 0:1239e9b70ca2 5321 const byte* ntruKey, word16 keySz, RNG* rng)
wolfSSL 0:1239e9b70ca2 5322 {
wolfSSL 0:1239e9b70ca2 5323 return MakeAnyCert(cert, derBuffer, derSz, NULL, NULL, rng, ntruKey, keySz);
wolfSSL 0:1239e9b70ca2 5324 }
wolfSSL 0:1239e9b70ca2 5325
wolfSSL 0:1239e9b70ca2 5326 #endif /* HAVE_NTRU */
wolfSSL 0:1239e9b70ca2 5327
wolfSSL 0:1239e9b70ca2 5328
wolfSSL 0:1239e9b70ca2 5329 #ifdef CYASSL_CERT_REQ
wolfSSL 0:1239e9b70ca2 5330
wolfSSL 0:1239e9b70ca2 5331 static int SetReqAttrib(byte* output, char* pw, int extSz)
wolfSSL 0:1239e9b70ca2 5332 {
wolfSSL 0:1239e9b70ca2 5333 static const byte cpOid[] =
wolfSSL 0:1239e9b70ca2 5334 { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
wolfSSL 0:1239e9b70ca2 5335 0x09, 0x07 };
wolfSSL 0:1239e9b70ca2 5336 static const byte erOid[] =
wolfSSL 0:1239e9b70ca2 5337 { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
wolfSSL 0:1239e9b70ca2 5338 0x09, 0x0e };
wolfSSL 0:1239e9b70ca2 5339
wolfSSL 0:1239e9b70ca2 5340 int sz = 0; /* overall size */
wolfSSL 0:1239e9b70ca2 5341 int cpSz = 0; /* Challenge Password section size */
wolfSSL 0:1239e9b70ca2 5342 int cpSeqSz = 0;
wolfSSL 0:1239e9b70ca2 5343 int cpSetSz = 0;
wolfSSL 0:1239e9b70ca2 5344 int cpStrSz = 0;
wolfSSL 0:1239e9b70ca2 5345 int pwSz = 0;
wolfSSL 0:1239e9b70ca2 5346 int erSz = 0; /* Extension Request section size */
wolfSSL 0:1239e9b70ca2 5347 int erSeqSz = 0;
wolfSSL 0:1239e9b70ca2 5348 int erSetSz = 0;
wolfSSL 0:1239e9b70ca2 5349 byte cpSeq[MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 5350 byte cpSet[MAX_SET_SZ];
wolfSSL 0:1239e9b70ca2 5351 byte cpStr[MAX_PRSTR_SZ];
wolfSSL 0:1239e9b70ca2 5352 byte erSeq[MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 5353 byte erSet[MAX_SET_SZ];
wolfSSL 0:1239e9b70ca2 5354
wolfSSL 0:1239e9b70ca2 5355 output[0] = 0xa0;
wolfSSL 0:1239e9b70ca2 5356 sz++;
wolfSSL 0:1239e9b70ca2 5357
wolfSSL 0:1239e9b70ca2 5358 if (pw && pw[0]) {
wolfSSL 0:1239e9b70ca2 5359 pwSz = (int)XSTRLEN(pw);
wolfSSL 0:1239e9b70ca2 5360 cpStrSz = SetUTF8String(pwSz, cpStr);
wolfSSL 0:1239e9b70ca2 5361 cpSetSz = SetSet(cpStrSz + pwSz, cpSet);
wolfSSL 0:1239e9b70ca2 5362 cpSeqSz = SetSequence(sizeof(cpOid) + cpSetSz + cpStrSz + pwSz, cpSeq);
wolfSSL 0:1239e9b70ca2 5363 cpSz = cpSeqSz + sizeof(cpOid) + cpSetSz + cpStrSz + pwSz;
wolfSSL 0:1239e9b70ca2 5364 }
wolfSSL 0:1239e9b70ca2 5365
wolfSSL 0:1239e9b70ca2 5366 if (extSz) {
wolfSSL 0:1239e9b70ca2 5367 erSetSz = SetSet(extSz, erSet);
wolfSSL 0:1239e9b70ca2 5368 erSeqSz = SetSequence(erSetSz + sizeof(erOid) + extSz, erSeq);
wolfSSL 0:1239e9b70ca2 5369 erSz = extSz + erSetSz + erSeqSz + sizeof(erOid);
wolfSSL 0:1239e9b70ca2 5370 }
wolfSSL 0:1239e9b70ca2 5371
wolfSSL 0:1239e9b70ca2 5372 /* Put the pieces together. */
wolfSSL 0:1239e9b70ca2 5373 sz += SetLength(cpSz + erSz, &output[sz]);
wolfSSL 0:1239e9b70ca2 5374
wolfSSL 0:1239e9b70ca2 5375 if (cpSz) {
wolfSSL 0:1239e9b70ca2 5376 XMEMCPY(&output[sz], cpSeq, cpSeqSz);
wolfSSL 0:1239e9b70ca2 5377 sz += cpSeqSz;
wolfSSL 0:1239e9b70ca2 5378 XMEMCPY(&output[sz], cpOid, sizeof(cpOid));
wolfSSL 0:1239e9b70ca2 5379 sz += sizeof(cpOid);
wolfSSL 0:1239e9b70ca2 5380 XMEMCPY(&output[sz], cpSet, cpSetSz);
wolfSSL 0:1239e9b70ca2 5381 sz += cpSetSz;
wolfSSL 0:1239e9b70ca2 5382 XMEMCPY(&output[sz], cpStr, cpStrSz);
wolfSSL 0:1239e9b70ca2 5383 sz += cpStrSz;
wolfSSL 0:1239e9b70ca2 5384 XMEMCPY(&output[sz], pw, pwSz);
wolfSSL 0:1239e9b70ca2 5385 sz += pwSz;
wolfSSL 0:1239e9b70ca2 5386 }
wolfSSL 0:1239e9b70ca2 5387
wolfSSL 0:1239e9b70ca2 5388 if (erSz) {
wolfSSL 0:1239e9b70ca2 5389 XMEMCPY(&output[sz], erSeq, erSeqSz);
wolfSSL 0:1239e9b70ca2 5390 sz += erSeqSz;
wolfSSL 0:1239e9b70ca2 5391 XMEMCPY(&output[sz], erOid, sizeof(erOid));
wolfSSL 0:1239e9b70ca2 5392 sz += sizeof(erOid);
wolfSSL 0:1239e9b70ca2 5393 XMEMCPY(&output[sz], erSet, erSetSz);
wolfSSL 0:1239e9b70ca2 5394 sz += erSetSz;
wolfSSL 0:1239e9b70ca2 5395 /* The actual extension data will be tacked onto the output later. */
wolfSSL 0:1239e9b70ca2 5396 }
wolfSSL 0:1239e9b70ca2 5397
wolfSSL 0:1239e9b70ca2 5398 return sz;
wolfSSL 0:1239e9b70ca2 5399 }
wolfSSL 0:1239e9b70ca2 5400
wolfSSL 0:1239e9b70ca2 5401
wolfSSL 0:1239e9b70ca2 5402 /* encode info from cert into DER encoded format */
wolfSSL 0:1239e9b70ca2 5403 static int EncodeCertReq(Cert* cert, DerCert* der,
wolfSSL 0:1239e9b70ca2 5404 RsaKey* rsaKey, ecc_key* eccKey)
wolfSSL 0:1239e9b70ca2 5405 {
wolfSSL 0:1239e9b70ca2 5406 (void)eccKey;
wolfSSL 0:1239e9b70ca2 5407
wolfSSL 0:1239e9b70ca2 5408 /* init */
wolfSSL 0:1239e9b70ca2 5409 XMEMSET(der, 0, sizeof(DerCert));
wolfSSL 0:1239e9b70ca2 5410
wolfSSL 0:1239e9b70ca2 5411 /* version */
wolfSSL 0:1239e9b70ca2 5412 der->versionSz = SetMyVersion(cert->version, der->version, FALSE);
wolfSSL 0:1239e9b70ca2 5413
wolfSSL 0:1239e9b70ca2 5414 /* subject name */
wolfSSL 0:1239e9b70ca2 5415 der->subjectSz = SetName(der->subject, &cert->subject);
wolfSSL 0:1239e9b70ca2 5416 if (der->subjectSz == 0)
wolfSSL 0:1239e9b70ca2 5417 return SUBJECT_E;
wolfSSL 0:1239e9b70ca2 5418
wolfSSL 0:1239e9b70ca2 5419 /* public key */
wolfSSL 0:1239e9b70ca2 5420 if (cert->keyType == RSA_KEY) {
wolfSSL 0:1239e9b70ca2 5421 if (rsaKey == NULL)
wolfSSL 0:1239e9b70ca2 5422 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5423 der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey);
wolfSSL 0:1239e9b70ca2 5424 if (der->publicKeySz <= 0)
wolfSSL 0:1239e9b70ca2 5425 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5426 }
wolfSSL 0:1239e9b70ca2 5427
wolfSSL 0:1239e9b70ca2 5428 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 5429 if (cert->keyType == ECC_KEY) {
wolfSSL 0:1239e9b70ca2 5430 if (eccKey == NULL)
wolfSSL 0:1239e9b70ca2 5431 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5432 der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey);
wolfSSL 0:1239e9b70ca2 5433 if (der->publicKeySz <= 0)
wolfSSL 0:1239e9b70ca2 5434 return PUBLIC_KEY_E;
wolfSSL 0:1239e9b70ca2 5435 }
wolfSSL 0:1239e9b70ca2 5436 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 5437
wolfSSL 0:1239e9b70ca2 5438 /* CA */
wolfSSL 0:1239e9b70ca2 5439 if (cert->isCA) {
wolfSSL 0:1239e9b70ca2 5440 der->caSz = SetCa(der->ca);
wolfSSL 0:1239e9b70ca2 5441 if (der->caSz == 0)
wolfSSL 0:1239e9b70ca2 5442 return CA_TRUE_E;
wolfSSL 0:1239e9b70ca2 5443 }
wolfSSL 0:1239e9b70ca2 5444 else
wolfSSL 0:1239e9b70ca2 5445 der->caSz = 0;
wolfSSL 0:1239e9b70ca2 5446
wolfSSL 0:1239e9b70ca2 5447 /* extensions, just CA now */
wolfSSL 0:1239e9b70ca2 5448 if (cert->isCA) {
wolfSSL 0:1239e9b70ca2 5449 der->extensionsSz = SetExtensions(der->extensions,
wolfSSL 0:1239e9b70ca2 5450 der->ca, der->caSz, FALSE);
wolfSSL 0:1239e9b70ca2 5451 if (der->extensionsSz == 0)
wolfSSL 0:1239e9b70ca2 5452 return EXTENSIONS_E;
wolfSSL 0:1239e9b70ca2 5453 }
wolfSSL 0:1239e9b70ca2 5454 else
wolfSSL 0:1239e9b70ca2 5455 der->extensionsSz = 0;
wolfSSL 0:1239e9b70ca2 5456
wolfSSL 0:1239e9b70ca2 5457 der->attribSz = SetReqAttrib(der->attrib,
wolfSSL 0:1239e9b70ca2 5458 cert->challengePw, der->extensionsSz);
wolfSSL 0:1239e9b70ca2 5459 if (der->attribSz == 0)
wolfSSL 0:1239e9b70ca2 5460 return REQ_ATTRIBUTE_E;
wolfSSL 0:1239e9b70ca2 5461
wolfSSL 0:1239e9b70ca2 5462 der->total = der->versionSz + der->subjectSz + der->publicKeySz +
wolfSSL 0:1239e9b70ca2 5463 der->extensionsSz + der->attribSz;
wolfSSL 0:1239e9b70ca2 5464
wolfSSL 0:1239e9b70ca2 5465 return 0;
wolfSSL 0:1239e9b70ca2 5466 }
wolfSSL 0:1239e9b70ca2 5467
wolfSSL 0:1239e9b70ca2 5468
wolfSSL 0:1239e9b70ca2 5469 /* write DER encoded cert req to buffer, size already checked */
wolfSSL 0:1239e9b70ca2 5470 static int WriteCertReqBody(DerCert* der, byte* buffer)
wolfSSL 0:1239e9b70ca2 5471 {
wolfSSL 0:1239e9b70ca2 5472 int idx;
wolfSSL 0:1239e9b70ca2 5473
wolfSSL 0:1239e9b70ca2 5474 /* signed part header */
wolfSSL 0:1239e9b70ca2 5475 idx = SetSequence(der->total, buffer);
wolfSSL 0:1239e9b70ca2 5476 /* version */
wolfSSL 0:1239e9b70ca2 5477 XMEMCPY(buffer + idx, der->version, der->versionSz);
wolfSSL 0:1239e9b70ca2 5478 idx += der->versionSz;
wolfSSL 0:1239e9b70ca2 5479 /* subject */
wolfSSL 0:1239e9b70ca2 5480 XMEMCPY(buffer + idx, der->subject, der->subjectSz);
wolfSSL 0:1239e9b70ca2 5481 idx += der->subjectSz;
wolfSSL 0:1239e9b70ca2 5482 /* public key */
wolfSSL 0:1239e9b70ca2 5483 XMEMCPY(buffer + idx, der->publicKey, der->publicKeySz);
wolfSSL 0:1239e9b70ca2 5484 idx += der->publicKeySz;
wolfSSL 0:1239e9b70ca2 5485 /* attributes */
wolfSSL 0:1239e9b70ca2 5486 XMEMCPY(buffer + idx, der->attrib, der->attribSz);
wolfSSL 0:1239e9b70ca2 5487 idx += der->attribSz;
wolfSSL 0:1239e9b70ca2 5488 /* extensions */
wolfSSL 0:1239e9b70ca2 5489 if (der->extensionsSz) {
wolfSSL 0:1239e9b70ca2 5490 XMEMCPY(buffer + idx, der->extensions, min(der->extensionsSz,
wolfSSL 0:1239e9b70ca2 5491 sizeof(der->extensions)));
wolfSSL 0:1239e9b70ca2 5492 idx += der->extensionsSz;
wolfSSL 0:1239e9b70ca2 5493 }
wolfSSL 0:1239e9b70ca2 5494
wolfSSL 0:1239e9b70ca2 5495 return idx;
wolfSSL 0:1239e9b70ca2 5496 }
wolfSSL 0:1239e9b70ca2 5497
wolfSSL 0:1239e9b70ca2 5498
wolfSSL 0:1239e9b70ca2 5499 int MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz,
wolfSSL 0:1239e9b70ca2 5500 RsaKey* rsaKey, ecc_key* eccKey)
wolfSSL 0:1239e9b70ca2 5501 {
wolfSSL 0:1239e9b70ca2 5502 DerCert der;
wolfSSL 0:1239e9b70ca2 5503 int ret;
wolfSSL 0:1239e9b70ca2 5504
wolfSSL 0:1239e9b70ca2 5505 cert->keyType = (eccKey != NULL) ? ECC_KEY : RSA_KEY;
wolfSSL 0:1239e9b70ca2 5506 ret = EncodeCertReq(cert, &der, rsaKey, eccKey);
wolfSSL 0:1239e9b70ca2 5507 if (ret != 0)
wolfSSL 0:1239e9b70ca2 5508 return ret;
wolfSSL 0:1239e9b70ca2 5509
wolfSSL 0:1239e9b70ca2 5510 if (der.total + MAX_SEQ_SZ * 2 > (int)derSz)
wolfSSL 0:1239e9b70ca2 5511 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 5512
wolfSSL 0:1239e9b70ca2 5513 return cert->bodySz = WriteCertReqBody(&der, derBuffer);
wolfSSL 0:1239e9b70ca2 5514 }
wolfSSL 0:1239e9b70ca2 5515
wolfSSL 0:1239e9b70ca2 5516 #endif /* CYASSL_CERT_REQ */
wolfSSL 0:1239e9b70ca2 5517
wolfSSL 0:1239e9b70ca2 5518
wolfSSL 0:1239e9b70ca2 5519 int SignCert(int requestSz, int sType, byte* buffer, word32 buffSz,
wolfSSL 0:1239e9b70ca2 5520 RsaKey* rsaKey, ecc_key* eccKey, RNG* rng)
wolfSSL 0:1239e9b70ca2 5521 {
wolfSSL 0:1239e9b70ca2 5522 byte sig[MAX_ENCODED_SIG_SZ];
wolfSSL 0:1239e9b70ca2 5523 int sigSz;
wolfSSL 0:1239e9b70ca2 5524
wolfSSL 0:1239e9b70ca2 5525 if (requestSz < 0)
wolfSSL 0:1239e9b70ca2 5526 return requestSz;
wolfSSL 0:1239e9b70ca2 5527
wolfSSL 0:1239e9b70ca2 5528 sigSz = MakeSignature(buffer, requestSz, sig, sizeof(sig), rsaKey, eccKey,
wolfSSL 0:1239e9b70ca2 5529 rng, sType);
wolfSSL 0:1239e9b70ca2 5530 if (sigSz < 0)
wolfSSL 0:1239e9b70ca2 5531 return sigSz;
wolfSSL 0:1239e9b70ca2 5532
wolfSSL 0:1239e9b70ca2 5533 if (requestSz + MAX_SEQ_SZ * 2 + sigSz > (int)buffSz)
wolfSSL 0:1239e9b70ca2 5534 return BUFFER_E;
wolfSSL 0:1239e9b70ca2 5535
wolfSSL 0:1239e9b70ca2 5536 return AddSignature(buffer, requestSz, sig, sigSz, sType);
wolfSSL 0:1239e9b70ca2 5537 }
wolfSSL 0:1239e9b70ca2 5538
wolfSSL 0:1239e9b70ca2 5539
wolfSSL 0:1239e9b70ca2 5540 int MakeSelfCert(Cert* cert, byte* buffer, word32 buffSz, RsaKey* key, RNG* rng)
wolfSSL 0:1239e9b70ca2 5541 {
wolfSSL 0:1239e9b70ca2 5542 int ret = MakeCert(cert, buffer, buffSz, key, NULL, rng);
wolfSSL 0:1239e9b70ca2 5543
wolfSSL 0:1239e9b70ca2 5544 if (ret < 0)
wolfSSL 0:1239e9b70ca2 5545 return ret;
wolfSSL 0:1239e9b70ca2 5546
wolfSSL 0:1239e9b70ca2 5547 return SignCert(cert->bodySz, cert->sigType, buffer, buffSz, key, NULL,rng);
wolfSSL 0:1239e9b70ca2 5548 }
wolfSSL 0:1239e9b70ca2 5549
wolfSSL 0:1239e9b70ca2 5550
wolfSSL 0:1239e9b70ca2 5551 #ifdef CYASSL_ALT_NAMES
wolfSSL 0:1239e9b70ca2 5552
wolfSSL 0:1239e9b70ca2 5553 /* Set Alt Names from der cert, return 0 on success */
wolfSSL 0:1239e9b70ca2 5554 static int SetAltNamesFromCert(Cert* cert, const byte* der, int derSz)
wolfSSL 0:1239e9b70ca2 5555 {
wolfSSL 0:1239e9b70ca2 5556 DecodedCert decoded;
wolfSSL 0:1239e9b70ca2 5557 int ret;
wolfSSL 0:1239e9b70ca2 5558
wolfSSL 0:1239e9b70ca2 5559 if (derSz < 0)
wolfSSL 0:1239e9b70ca2 5560 return derSz;
wolfSSL 0:1239e9b70ca2 5561
wolfSSL 0:1239e9b70ca2 5562 InitDecodedCert(&decoded, (byte*)der, derSz, 0);
wolfSSL 0:1239e9b70ca2 5563 ret = ParseCertRelative(&decoded, CA_TYPE, NO_VERIFY, 0);
wolfSSL 0:1239e9b70ca2 5564
wolfSSL 0:1239e9b70ca2 5565 if (ret < 0) {
wolfSSL 0:1239e9b70ca2 5566 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5567 return ret;
wolfSSL 0:1239e9b70ca2 5568 }
wolfSSL 0:1239e9b70ca2 5569
wolfSSL 0:1239e9b70ca2 5570 if (decoded.extensions) {
wolfSSL 0:1239e9b70ca2 5571 byte b;
wolfSSL 0:1239e9b70ca2 5572 int length;
wolfSSL 0:1239e9b70ca2 5573 word32 maxExtensionsIdx;
wolfSSL 0:1239e9b70ca2 5574
wolfSSL 0:1239e9b70ca2 5575 decoded.srcIdx = decoded.extensionsIdx;
wolfSSL 0:1239e9b70ca2 5576 b = decoded.source[decoded.srcIdx++];
wolfSSL 0:1239e9b70ca2 5577 if (b != ASN_EXTENSIONS) {
wolfSSL 0:1239e9b70ca2 5578 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5579 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5580 }
wolfSSL 0:1239e9b70ca2 5581
wolfSSL 0:1239e9b70ca2 5582 if (GetLength(decoded.source, &decoded.srcIdx, &length,
wolfSSL 0:1239e9b70ca2 5583 decoded.maxIdx) < 0) {
wolfSSL 0:1239e9b70ca2 5584 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5585 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5586 }
wolfSSL 0:1239e9b70ca2 5587
wolfSSL 0:1239e9b70ca2 5588 if (GetSequence(decoded.source, &decoded.srcIdx, &length,
wolfSSL 0:1239e9b70ca2 5589 decoded.maxIdx) < 0) {
wolfSSL 0:1239e9b70ca2 5590 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5591 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5592 }
wolfSSL 0:1239e9b70ca2 5593
wolfSSL 0:1239e9b70ca2 5594 maxExtensionsIdx = decoded.srcIdx + length;
wolfSSL 0:1239e9b70ca2 5595
wolfSSL 0:1239e9b70ca2 5596 while (decoded.srcIdx < maxExtensionsIdx) {
wolfSSL 0:1239e9b70ca2 5597 word32 oid;
wolfSSL 0:1239e9b70ca2 5598 word32 startIdx = decoded.srcIdx;
wolfSSL 0:1239e9b70ca2 5599 word32 tmpIdx;
wolfSSL 0:1239e9b70ca2 5600
wolfSSL 0:1239e9b70ca2 5601 if (GetSequence(decoded.source, &decoded.srcIdx, &length,
wolfSSL 0:1239e9b70ca2 5602 decoded.maxIdx) < 0) {
wolfSSL 0:1239e9b70ca2 5603 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5604 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5605 }
wolfSSL 0:1239e9b70ca2 5606
wolfSSL 0:1239e9b70ca2 5607 tmpIdx = decoded.srcIdx;
wolfSSL 0:1239e9b70ca2 5608 decoded.srcIdx = startIdx;
wolfSSL 0:1239e9b70ca2 5609
wolfSSL 0:1239e9b70ca2 5610 if (GetAlgoId(decoded.source, &decoded.srcIdx, &oid,
wolfSSL 0:1239e9b70ca2 5611 decoded.maxIdx) < 0) {
wolfSSL 0:1239e9b70ca2 5612 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5613 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5614 }
wolfSSL 0:1239e9b70ca2 5615
wolfSSL 0:1239e9b70ca2 5616 if (oid == ALT_NAMES_OID) {
wolfSSL 0:1239e9b70ca2 5617 cert->altNamesSz = length + (tmpIdx - startIdx);
wolfSSL 0:1239e9b70ca2 5618
wolfSSL 0:1239e9b70ca2 5619 if (cert->altNamesSz < (int)sizeof(cert->altNames))
wolfSSL 0:1239e9b70ca2 5620 XMEMCPY(cert->altNames, &decoded.source[startIdx],
wolfSSL 0:1239e9b70ca2 5621 cert->altNamesSz);
wolfSSL 0:1239e9b70ca2 5622 else {
wolfSSL 0:1239e9b70ca2 5623 cert->altNamesSz = 0;
wolfSSL 0:1239e9b70ca2 5624 CYASSL_MSG("AltNames extensions too big");
wolfSSL 0:1239e9b70ca2 5625 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5626 return ALT_NAME_E;
wolfSSL 0:1239e9b70ca2 5627 }
wolfSSL 0:1239e9b70ca2 5628 }
wolfSSL 0:1239e9b70ca2 5629 decoded.srcIdx = tmpIdx + length;
wolfSSL 0:1239e9b70ca2 5630 }
wolfSSL 0:1239e9b70ca2 5631 }
wolfSSL 0:1239e9b70ca2 5632 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5633
wolfSSL 0:1239e9b70ca2 5634 return 0;
wolfSSL 0:1239e9b70ca2 5635 }
wolfSSL 0:1239e9b70ca2 5636
wolfSSL 0:1239e9b70ca2 5637
wolfSSL 0:1239e9b70ca2 5638 /* Set Dates from der cert, return 0 on success */
wolfSSL 0:1239e9b70ca2 5639 static int SetDatesFromCert(Cert* cert, const byte* der, int derSz)
wolfSSL 0:1239e9b70ca2 5640 {
wolfSSL 0:1239e9b70ca2 5641 DecodedCert decoded;
wolfSSL 0:1239e9b70ca2 5642 int ret;
wolfSSL 0:1239e9b70ca2 5643
wolfSSL 0:1239e9b70ca2 5644 CYASSL_ENTER("SetDatesFromCert");
wolfSSL 0:1239e9b70ca2 5645 if (derSz < 0)
wolfSSL 0:1239e9b70ca2 5646 return derSz;
wolfSSL 0:1239e9b70ca2 5647
wolfSSL 0:1239e9b70ca2 5648 InitDecodedCert(&decoded, (byte*)der, derSz, 0);
wolfSSL 0:1239e9b70ca2 5649 ret = ParseCertRelative(&decoded, CA_TYPE, NO_VERIFY, 0);
wolfSSL 0:1239e9b70ca2 5650
wolfSSL 0:1239e9b70ca2 5651 if (ret < 0) {
wolfSSL 0:1239e9b70ca2 5652 CYASSL_MSG("ParseCertRelative error");
wolfSSL 0:1239e9b70ca2 5653 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5654 return ret;
wolfSSL 0:1239e9b70ca2 5655 }
wolfSSL 0:1239e9b70ca2 5656
wolfSSL 0:1239e9b70ca2 5657 if (decoded.beforeDate == NULL || decoded.afterDate == NULL) {
wolfSSL 0:1239e9b70ca2 5658 CYASSL_MSG("Couldn't extract dates");
wolfSSL 0:1239e9b70ca2 5659 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5660 return -1;
wolfSSL 0:1239e9b70ca2 5661 }
wolfSSL 0:1239e9b70ca2 5662
wolfSSL 0:1239e9b70ca2 5663 if (decoded.beforeDateLen > MAX_DATE_SIZE || decoded.afterDateLen >
wolfSSL 0:1239e9b70ca2 5664 MAX_DATE_SIZE) {
wolfSSL 0:1239e9b70ca2 5665 CYASSL_MSG("Bad date size");
wolfSSL 0:1239e9b70ca2 5666 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5667 return -1;
wolfSSL 0:1239e9b70ca2 5668 }
wolfSSL 0:1239e9b70ca2 5669
wolfSSL 0:1239e9b70ca2 5670 XMEMCPY(cert->beforeDate, decoded.beforeDate, decoded.beforeDateLen);
wolfSSL 0:1239e9b70ca2 5671 XMEMCPY(cert->afterDate, decoded.afterDate, decoded.afterDateLen);
wolfSSL 0:1239e9b70ca2 5672
wolfSSL 0:1239e9b70ca2 5673 cert->beforeDateSz = decoded.beforeDateLen;
wolfSSL 0:1239e9b70ca2 5674 cert->afterDateSz = decoded.afterDateLen;
wolfSSL 0:1239e9b70ca2 5675
wolfSSL 0:1239e9b70ca2 5676 return 0;
wolfSSL 0:1239e9b70ca2 5677 }
wolfSSL 0:1239e9b70ca2 5678
wolfSSL 0:1239e9b70ca2 5679
wolfSSL 0:1239e9b70ca2 5680 #endif /* CYASSL_ALT_NAMES && !NO_RSA */
wolfSSL 0:1239e9b70ca2 5681
wolfSSL 0:1239e9b70ca2 5682
wolfSSL 0:1239e9b70ca2 5683 /* Set cn name from der buffer, return 0 on success */
wolfSSL 0:1239e9b70ca2 5684 static int SetNameFromCert(CertName* cn, const byte* der, int derSz)
wolfSSL 0:1239e9b70ca2 5685 {
wolfSSL 0:1239e9b70ca2 5686 DecodedCert decoded;
wolfSSL 0:1239e9b70ca2 5687 int ret;
wolfSSL 0:1239e9b70ca2 5688 int sz;
wolfSSL 0:1239e9b70ca2 5689
wolfSSL 0:1239e9b70ca2 5690 if (derSz < 0)
wolfSSL 0:1239e9b70ca2 5691 return derSz;
wolfSSL 0:1239e9b70ca2 5692
wolfSSL 0:1239e9b70ca2 5693 InitDecodedCert(&decoded, (byte*)der, derSz, 0);
wolfSSL 0:1239e9b70ca2 5694 ret = ParseCertRelative(&decoded, CA_TYPE, NO_VERIFY, 0);
wolfSSL 0:1239e9b70ca2 5695
wolfSSL 0:1239e9b70ca2 5696 if (ret < 0)
wolfSSL 0:1239e9b70ca2 5697 return ret;
wolfSSL 0:1239e9b70ca2 5698
wolfSSL 0:1239e9b70ca2 5699 if (decoded.subjectCN) {
wolfSSL 0:1239e9b70ca2 5700 sz = (decoded.subjectCNLen < CTC_NAME_SIZE) ? decoded.subjectCNLen :
wolfSSL 0:1239e9b70ca2 5701 CTC_NAME_SIZE - 1;
wolfSSL 0:1239e9b70ca2 5702 strncpy(cn->commonName, decoded.subjectCN, CTC_NAME_SIZE);
wolfSSL 0:1239e9b70ca2 5703 cn->commonName[sz] = 0;
wolfSSL 0:1239e9b70ca2 5704 }
wolfSSL 0:1239e9b70ca2 5705 if (decoded.subjectC) {
wolfSSL 0:1239e9b70ca2 5706 sz = (decoded.subjectCLen < CTC_NAME_SIZE) ? decoded.subjectCLen :
wolfSSL 0:1239e9b70ca2 5707 CTC_NAME_SIZE - 1;
wolfSSL 0:1239e9b70ca2 5708 strncpy(cn->country, decoded.subjectC, CTC_NAME_SIZE);
wolfSSL 0:1239e9b70ca2 5709 cn->country[sz] = 0;
wolfSSL 0:1239e9b70ca2 5710 }
wolfSSL 0:1239e9b70ca2 5711 if (decoded.subjectST) {
wolfSSL 0:1239e9b70ca2 5712 sz = (decoded.subjectSTLen < CTC_NAME_SIZE) ? decoded.subjectSTLen :
wolfSSL 0:1239e9b70ca2 5713 CTC_NAME_SIZE - 1;
wolfSSL 0:1239e9b70ca2 5714 strncpy(cn->state, decoded.subjectST, CTC_NAME_SIZE);
wolfSSL 0:1239e9b70ca2 5715 cn->state[sz] = 0;
wolfSSL 0:1239e9b70ca2 5716 }
wolfSSL 0:1239e9b70ca2 5717 if (decoded.subjectL) {
wolfSSL 0:1239e9b70ca2 5718 sz = (decoded.subjectLLen < CTC_NAME_SIZE) ? decoded.subjectLLen :
wolfSSL 0:1239e9b70ca2 5719 CTC_NAME_SIZE - 1;
wolfSSL 0:1239e9b70ca2 5720 strncpy(cn->locality, decoded.subjectL, CTC_NAME_SIZE);
wolfSSL 0:1239e9b70ca2 5721 cn->locality[sz] = 0;
wolfSSL 0:1239e9b70ca2 5722 }
wolfSSL 0:1239e9b70ca2 5723 if (decoded.subjectO) {
wolfSSL 0:1239e9b70ca2 5724 sz = (decoded.subjectOLen < CTC_NAME_SIZE) ? decoded.subjectOLen :
wolfSSL 0:1239e9b70ca2 5725 CTC_NAME_SIZE - 1;
wolfSSL 0:1239e9b70ca2 5726 strncpy(cn->org, decoded.subjectO, CTC_NAME_SIZE);
wolfSSL 0:1239e9b70ca2 5727 cn->org[sz] = 0;
wolfSSL 0:1239e9b70ca2 5728 }
wolfSSL 0:1239e9b70ca2 5729 if (decoded.subjectOU) {
wolfSSL 0:1239e9b70ca2 5730 sz = (decoded.subjectOULen < CTC_NAME_SIZE) ? decoded.subjectOULen :
wolfSSL 0:1239e9b70ca2 5731 CTC_NAME_SIZE - 1;
wolfSSL 0:1239e9b70ca2 5732 strncpy(cn->unit, decoded.subjectOU, CTC_NAME_SIZE);
wolfSSL 0:1239e9b70ca2 5733 cn->unit[sz] = 0;
wolfSSL 0:1239e9b70ca2 5734 }
wolfSSL 0:1239e9b70ca2 5735 if (decoded.subjectSN) {
wolfSSL 0:1239e9b70ca2 5736 sz = (decoded.subjectSNLen < CTC_NAME_SIZE) ? decoded.subjectSNLen :
wolfSSL 0:1239e9b70ca2 5737 CTC_NAME_SIZE - 1;
wolfSSL 0:1239e9b70ca2 5738 strncpy(cn->sur, decoded.subjectSN, CTC_NAME_SIZE);
wolfSSL 0:1239e9b70ca2 5739 cn->sur[sz] = 0;
wolfSSL 0:1239e9b70ca2 5740 }
wolfSSL 0:1239e9b70ca2 5741 if (decoded.subjectEmail) {
wolfSSL 0:1239e9b70ca2 5742 sz = (decoded.subjectEmailLen < CTC_NAME_SIZE) ?
wolfSSL 0:1239e9b70ca2 5743 decoded.subjectEmailLen : CTC_NAME_SIZE - 1;
wolfSSL 0:1239e9b70ca2 5744 strncpy(cn->email, decoded.subjectEmail, CTC_NAME_SIZE);
wolfSSL 0:1239e9b70ca2 5745 cn->email[sz] = 0;
wolfSSL 0:1239e9b70ca2 5746 }
wolfSSL 0:1239e9b70ca2 5747
wolfSSL 0:1239e9b70ca2 5748 FreeDecodedCert(&decoded);
wolfSSL 0:1239e9b70ca2 5749
wolfSSL 0:1239e9b70ca2 5750 return 0;
wolfSSL 0:1239e9b70ca2 5751 }
wolfSSL 0:1239e9b70ca2 5752
wolfSSL 0:1239e9b70ca2 5753
wolfSSL 0:1239e9b70ca2 5754 #ifndef NO_FILESYSTEM
wolfSSL 0:1239e9b70ca2 5755
wolfSSL 0:1239e9b70ca2 5756 /* forward from CyaSSL */
wolfSSL 0:1239e9b70ca2 5757 int CyaSSL_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz);
wolfSSL 0:1239e9b70ca2 5758
wolfSSL 0:1239e9b70ca2 5759 /* Set cert issuer from issuerFile in PEM */
wolfSSL 0:1239e9b70ca2 5760 int SetIssuer(Cert* cert, const char* issuerFile)
wolfSSL 0:1239e9b70ca2 5761 {
wolfSSL 0:1239e9b70ca2 5762 int ret;
wolfSSL 0:1239e9b70ca2 5763 int derSz;
wolfSSL 0:1239e9b70ca2 5764 byte* der = (byte*)XMALLOC(EIGHTK_BUF, NULL, DYNAMIC_TYPE_CERT);
wolfSSL 0:1239e9b70ca2 5765
wolfSSL 0:1239e9b70ca2 5766 if (der == NULL) {
wolfSSL 0:1239e9b70ca2 5767 CYASSL_MSG("SetIssuer OOF Problem");
wolfSSL 0:1239e9b70ca2 5768 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 5769 }
wolfSSL 0:1239e9b70ca2 5770 derSz = CyaSSL_PemCertToDer(issuerFile, der, EIGHTK_BUF);
wolfSSL 0:1239e9b70ca2 5771 cert->selfSigned = 0;
wolfSSL 0:1239e9b70ca2 5772 ret = SetNameFromCert(&cert->issuer, der, derSz);
wolfSSL 0:1239e9b70ca2 5773 XFREE(der, NULL, DYNAMIC_TYPE_CERT);
wolfSSL 0:1239e9b70ca2 5774
wolfSSL 0:1239e9b70ca2 5775 return ret;
wolfSSL 0:1239e9b70ca2 5776 }
wolfSSL 0:1239e9b70ca2 5777
wolfSSL 0:1239e9b70ca2 5778
wolfSSL 0:1239e9b70ca2 5779 /* Set cert subject from subjectFile in PEM */
wolfSSL 0:1239e9b70ca2 5780 int SetSubject(Cert* cert, const char* subjectFile)
wolfSSL 0:1239e9b70ca2 5781 {
wolfSSL 0:1239e9b70ca2 5782 int ret;
wolfSSL 0:1239e9b70ca2 5783 int derSz;
wolfSSL 0:1239e9b70ca2 5784 byte* der = (byte*)XMALLOC(EIGHTK_BUF, NULL, DYNAMIC_TYPE_CERT);
wolfSSL 0:1239e9b70ca2 5785
wolfSSL 0:1239e9b70ca2 5786 if (der == NULL) {
wolfSSL 0:1239e9b70ca2 5787 CYASSL_MSG("SetSubject OOF Problem");
wolfSSL 0:1239e9b70ca2 5788 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 5789 }
wolfSSL 0:1239e9b70ca2 5790 derSz = CyaSSL_PemCertToDer(subjectFile, der, EIGHTK_BUF);
wolfSSL 0:1239e9b70ca2 5791 ret = SetNameFromCert(&cert->subject, der, derSz);
wolfSSL 0:1239e9b70ca2 5792 XFREE(der, NULL, DYNAMIC_TYPE_CERT);
wolfSSL 0:1239e9b70ca2 5793
wolfSSL 0:1239e9b70ca2 5794 return ret;
wolfSSL 0:1239e9b70ca2 5795 }
wolfSSL 0:1239e9b70ca2 5796
wolfSSL 0:1239e9b70ca2 5797
wolfSSL 0:1239e9b70ca2 5798 #ifdef CYASSL_ALT_NAMES
wolfSSL 0:1239e9b70ca2 5799
wolfSSL 0:1239e9b70ca2 5800 /* Set atl names from file in PEM */
wolfSSL 0:1239e9b70ca2 5801 int SetAltNames(Cert* cert, const char* file)
wolfSSL 0:1239e9b70ca2 5802 {
wolfSSL 0:1239e9b70ca2 5803 int ret;
wolfSSL 0:1239e9b70ca2 5804 int derSz;
wolfSSL 0:1239e9b70ca2 5805 byte* der = (byte*)XMALLOC(EIGHTK_BUF, NULL, DYNAMIC_TYPE_CERT);
wolfSSL 0:1239e9b70ca2 5806
wolfSSL 0:1239e9b70ca2 5807 if (der == NULL) {
wolfSSL 0:1239e9b70ca2 5808 CYASSL_MSG("SetAltNames OOF Problem");
wolfSSL 0:1239e9b70ca2 5809 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 5810 }
wolfSSL 0:1239e9b70ca2 5811 derSz = CyaSSL_PemCertToDer(file, der, EIGHTK_BUF);
wolfSSL 0:1239e9b70ca2 5812 ret = SetAltNamesFromCert(cert, der, derSz);
wolfSSL 0:1239e9b70ca2 5813 XFREE(der, NULL, DYNAMIC_TYPE_CERT);
wolfSSL 0:1239e9b70ca2 5814
wolfSSL 0:1239e9b70ca2 5815 return ret;
wolfSSL 0:1239e9b70ca2 5816 }
wolfSSL 0:1239e9b70ca2 5817
wolfSSL 0:1239e9b70ca2 5818 #endif /* CYASSL_ALT_NAMES */
wolfSSL 0:1239e9b70ca2 5819
wolfSSL 0:1239e9b70ca2 5820 #endif /* NO_FILESYSTEM */
wolfSSL 0:1239e9b70ca2 5821
wolfSSL 0:1239e9b70ca2 5822 /* Set cert issuer from DER buffer */
wolfSSL 0:1239e9b70ca2 5823 int SetIssuerBuffer(Cert* cert, const byte* der, int derSz)
wolfSSL 0:1239e9b70ca2 5824 {
wolfSSL 0:1239e9b70ca2 5825 cert->selfSigned = 0;
wolfSSL 0:1239e9b70ca2 5826 return SetNameFromCert(&cert->issuer, der, derSz);
wolfSSL 0:1239e9b70ca2 5827 }
wolfSSL 0:1239e9b70ca2 5828
wolfSSL 0:1239e9b70ca2 5829
wolfSSL 0:1239e9b70ca2 5830 /* Set cert subject from DER buffer */
wolfSSL 0:1239e9b70ca2 5831 int SetSubjectBuffer(Cert* cert, const byte* der, int derSz)
wolfSSL 0:1239e9b70ca2 5832 {
wolfSSL 0:1239e9b70ca2 5833 return SetNameFromCert(&cert->subject, der, derSz);
wolfSSL 0:1239e9b70ca2 5834 }
wolfSSL 0:1239e9b70ca2 5835
wolfSSL 0:1239e9b70ca2 5836
wolfSSL 0:1239e9b70ca2 5837 #ifdef CYASSL_ALT_NAMES
wolfSSL 0:1239e9b70ca2 5838
wolfSSL 0:1239e9b70ca2 5839 /* Set cert alt names from DER buffer */
wolfSSL 0:1239e9b70ca2 5840 int SetAltNamesBuffer(Cert* cert, const byte* der, int derSz)
wolfSSL 0:1239e9b70ca2 5841 {
wolfSSL 0:1239e9b70ca2 5842 return SetAltNamesFromCert(cert, der, derSz);
wolfSSL 0:1239e9b70ca2 5843 }
wolfSSL 0:1239e9b70ca2 5844
wolfSSL 0:1239e9b70ca2 5845 /* Set cert dates from DER buffer */
wolfSSL 0:1239e9b70ca2 5846 int SetDatesBuffer(Cert* cert, const byte* der, int derSz)
wolfSSL 0:1239e9b70ca2 5847 {
wolfSSL 0:1239e9b70ca2 5848 return SetDatesFromCert(cert, der, derSz);
wolfSSL 0:1239e9b70ca2 5849 }
wolfSSL 0:1239e9b70ca2 5850
wolfSSL 0:1239e9b70ca2 5851 #endif /* CYASSL_ALT_NAMES */
wolfSSL 0:1239e9b70ca2 5852
wolfSSL 0:1239e9b70ca2 5853 #endif /* CYASSL_CERT_GEN */
wolfSSL 0:1239e9b70ca2 5854
wolfSSL 0:1239e9b70ca2 5855
wolfSSL 0:1239e9b70ca2 5856 #ifdef HAVE_ECC
wolfSSL 0:1239e9b70ca2 5857
wolfSSL 0:1239e9b70ca2 5858 /* Der Encode r & s ints into out, outLen is (in/out) size */
wolfSSL 0:1239e9b70ca2 5859 int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r, mp_int* s)
wolfSSL 0:1239e9b70ca2 5860 {
wolfSSL 0:1239e9b70ca2 5861 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 5862 word32 rSz; /* encoding size */
wolfSSL 0:1239e9b70ca2 5863 word32 sSz;
wolfSSL 0:1239e9b70ca2 5864 word32 headerSz = 4; /* 2*ASN_TAG + 2*LEN(ENUM) */
wolfSSL 0:1239e9b70ca2 5865
wolfSSL 0:1239e9b70ca2 5866 /* If the leading bit on the INTEGER is a 1, add a leading zero */
wolfSSL 0:1239e9b70ca2 5867 int rLeadingZero = mp_leading_bit(r);
wolfSSL 0:1239e9b70ca2 5868 int sLeadingZero = mp_leading_bit(s);
wolfSSL 0:1239e9b70ca2 5869 int rLen = mp_unsigned_bin_size(r); /* big int size */
wolfSSL 0:1239e9b70ca2 5870 int sLen = mp_unsigned_bin_size(s);
wolfSSL 0:1239e9b70ca2 5871 int err;
wolfSSL 0:1239e9b70ca2 5872
wolfSSL 0:1239e9b70ca2 5873 if (*outLen < (rLen + rLeadingZero + sLen + sLeadingZero +
wolfSSL 0:1239e9b70ca2 5874 headerSz + 2)) /* SEQ_TAG + LEN(ENUM) */
wolfSSL 0:1239e9b70ca2 5875 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 5876
wolfSSL 0:1239e9b70ca2 5877 idx = SetSequence(rLen+rLeadingZero+sLen+sLeadingZero+headerSz, out);
wolfSSL 0:1239e9b70ca2 5878
wolfSSL 0:1239e9b70ca2 5879 /* store r */
wolfSSL 0:1239e9b70ca2 5880 out[idx++] = ASN_INTEGER;
wolfSSL 0:1239e9b70ca2 5881 rSz = SetLength(rLen + rLeadingZero, &out[idx]);
wolfSSL 0:1239e9b70ca2 5882 idx += rSz;
wolfSSL 0:1239e9b70ca2 5883 if (rLeadingZero)
wolfSSL 0:1239e9b70ca2 5884 out[idx++] = 0;
wolfSSL 0:1239e9b70ca2 5885 err = mp_to_unsigned_bin(r, &out[idx]);
wolfSSL 0:1239e9b70ca2 5886 if (err != MP_OKAY) return err;
wolfSSL 0:1239e9b70ca2 5887 idx += rLen;
wolfSSL 0:1239e9b70ca2 5888
wolfSSL 0:1239e9b70ca2 5889 /* store s */
wolfSSL 0:1239e9b70ca2 5890 out[idx++] = ASN_INTEGER;
wolfSSL 0:1239e9b70ca2 5891 sSz = SetLength(sLen + sLeadingZero, &out[idx]);
wolfSSL 0:1239e9b70ca2 5892 idx += sSz;
wolfSSL 0:1239e9b70ca2 5893 if (sLeadingZero)
wolfSSL 0:1239e9b70ca2 5894 out[idx++] = 0;
wolfSSL 0:1239e9b70ca2 5895 err = mp_to_unsigned_bin(s, &out[idx]);
wolfSSL 0:1239e9b70ca2 5896 if (err != MP_OKAY) return err;
wolfSSL 0:1239e9b70ca2 5897 idx += sLen;
wolfSSL 0:1239e9b70ca2 5898
wolfSSL 0:1239e9b70ca2 5899 *outLen = idx;
wolfSSL 0:1239e9b70ca2 5900
wolfSSL 0:1239e9b70ca2 5901 return 0;
wolfSSL 0:1239e9b70ca2 5902 }
wolfSSL 0:1239e9b70ca2 5903
wolfSSL 0:1239e9b70ca2 5904
wolfSSL 0:1239e9b70ca2 5905 /* Der Decode ECC-DSA Signautre, r & s stored as big ints */
wolfSSL 0:1239e9b70ca2 5906 int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen, mp_int* r, mp_int* s)
wolfSSL 0:1239e9b70ca2 5907 {
wolfSSL 0:1239e9b70ca2 5908 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 5909 int len = 0;
wolfSSL 0:1239e9b70ca2 5910
wolfSSL 0:1239e9b70ca2 5911 if (GetSequence(sig, &idx, &len, sigLen) < 0)
wolfSSL 0:1239e9b70ca2 5912 return ASN_ECC_KEY_E;
wolfSSL 0:1239e9b70ca2 5913
wolfSSL 0:1239e9b70ca2 5914 if ((word32)len > (sigLen - idx))
wolfSSL 0:1239e9b70ca2 5915 return ASN_ECC_KEY_E;
wolfSSL 0:1239e9b70ca2 5916
wolfSSL 0:1239e9b70ca2 5917 if (GetInt(r, sig, &idx, sigLen) < 0)
wolfSSL 0:1239e9b70ca2 5918 return ASN_ECC_KEY_E;
wolfSSL 0:1239e9b70ca2 5919
wolfSSL 0:1239e9b70ca2 5920 if (GetInt(s, sig, &idx, sigLen) < 0)
wolfSSL 0:1239e9b70ca2 5921 return ASN_ECC_KEY_E;
wolfSSL 0:1239e9b70ca2 5922
wolfSSL 0:1239e9b70ca2 5923 return 0;
wolfSSL 0:1239e9b70ca2 5924 }
wolfSSL 0:1239e9b70ca2 5925
wolfSSL 0:1239e9b70ca2 5926
wolfSSL 0:1239e9b70ca2 5927 int EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
wolfSSL 0:1239e9b70ca2 5928 word32 inSz)
wolfSSL 0:1239e9b70ca2 5929 {
wolfSSL 0:1239e9b70ca2 5930 word32 oid = 0;
wolfSSL 0:1239e9b70ca2 5931 int version, length;
wolfSSL 0:1239e9b70ca2 5932 int privSz, pubSz;
wolfSSL 0:1239e9b70ca2 5933 byte b;
wolfSSL 0:1239e9b70ca2 5934 byte priv[ECC_MAXSIZE];
wolfSSL 0:1239e9b70ca2 5935 byte pub[ECC_MAXSIZE * 2 + 1]; /* public key has two parts plus header */
wolfSSL 0:1239e9b70ca2 5936
wolfSSL 0:1239e9b70ca2 5937 if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0)
wolfSSL 0:1239e9b70ca2 5938 return BAD_FUNC_ARG;
wolfSSL 0:1239e9b70ca2 5939
wolfSSL 0:1239e9b70ca2 5940 if (GetSequence(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 5941 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5942
wolfSSL 0:1239e9b70ca2 5943 if (GetMyVersion(input, inOutIdx, &version) < 0)
wolfSSL 0:1239e9b70ca2 5944 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5945
wolfSSL 0:1239e9b70ca2 5946 b = input[*inOutIdx];
wolfSSL 0:1239e9b70ca2 5947 *inOutIdx += 1;
wolfSSL 0:1239e9b70ca2 5948
wolfSSL 0:1239e9b70ca2 5949 /* priv type */
wolfSSL 0:1239e9b70ca2 5950 if (b != 4 && b != 6 && b != 7)
wolfSSL 0:1239e9b70ca2 5951 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5952
wolfSSL 0:1239e9b70ca2 5953 if (GetLength(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 5954 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5955
wolfSSL 0:1239e9b70ca2 5956 /* priv key */
wolfSSL 0:1239e9b70ca2 5957 privSz = length;
wolfSSL 0:1239e9b70ca2 5958 XMEMCPY(priv, &input[*inOutIdx], privSz);
wolfSSL 0:1239e9b70ca2 5959 *inOutIdx += length;
wolfSSL 0:1239e9b70ca2 5960
wolfSSL 0:1239e9b70ca2 5961 /* prefix 0, may have */
wolfSSL 0:1239e9b70ca2 5962 b = input[*inOutIdx];
wolfSSL 0:1239e9b70ca2 5963 if (b == ECC_PREFIX_0) {
wolfSSL 0:1239e9b70ca2 5964 *inOutIdx += 1;
wolfSSL 0:1239e9b70ca2 5965
wolfSSL 0:1239e9b70ca2 5966 if (GetLength(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 5967 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5968
wolfSSL 0:1239e9b70ca2 5969 /* object id */
wolfSSL 0:1239e9b70ca2 5970 b = input[*inOutIdx];
wolfSSL 0:1239e9b70ca2 5971 *inOutIdx += 1;
wolfSSL 0:1239e9b70ca2 5972
wolfSSL 0:1239e9b70ca2 5973 if (b != ASN_OBJECT_ID)
wolfSSL 0:1239e9b70ca2 5974 return ASN_OBJECT_ID_E;
wolfSSL 0:1239e9b70ca2 5975
wolfSSL 0:1239e9b70ca2 5976 if (GetLength(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 5977 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5978
wolfSSL 0:1239e9b70ca2 5979 while(length--) {
wolfSSL 0:1239e9b70ca2 5980 oid += input[*inOutIdx];
wolfSSL 0:1239e9b70ca2 5981 *inOutIdx += 1;
wolfSSL 0:1239e9b70ca2 5982 }
wolfSSL 0:1239e9b70ca2 5983 if (CheckCurve(oid) < 0)
wolfSSL 0:1239e9b70ca2 5984 return ECC_CURVE_OID_E;
wolfSSL 0:1239e9b70ca2 5985 }
wolfSSL 0:1239e9b70ca2 5986
wolfSSL 0:1239e9b70ca2 5987 /* prefix 1 */
wolfSSL 0:1239e9b70ca2 5988 b = input[*inOutIdx];
wolfSSL 0:1239e9b70ca2 5989 *inOutIdx += 1;
wolfSSL 0:1239e9b70ca2 5990 if (b != ECC_PREFIX_1)
wolfSSL 0:1239e9b70ca2 5991 return ASN_ECC_KEY_E;
wolfSSL 0:1239e9b70ca2 5992
wolfSSL 0:1239e9b70ca2 5993 if (GetLength(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 5994 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 5995
wolfSSL 0:1239e9b70ca2 5996 /* key header */
wolfSSL 0:1239e9b70ca2 5997 b = input[*inOutIdx];
wolfSSL 0:1239e9b70ca2 5998 *inOutIdx += 1;
wolfSSL 0:1239e9b70ca2 5999 if (b != ASN_BIT_STRING)
wolfSSL 0:1239e9b70ca2 6000 return ASN_BITSTR_E;
wolfSSL 0:1239e9b70ca2 6001
wolfSSL 0:1239e9b70ca2 6002 if (GetLength(input, inOutIdx, &length, inSz) < 0)
wolfSSL 0:1239e9b70ca2 6003 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6004 b = input[*inOutIdx];
wolfSSL 0:1239e9b70ca2 6005 *inOutIdx += 1;
wolfSSL 0:1239e9b70ca2 6006 if (b != 0x00)
wolfSSL 0:1239e9b70ca2 6007 return ASN_EXPECT_0_E;
wolfSSL 0:1239e9b70ca2 6008
wolfSSL 0:1239e9b70ca2 6009 pubSz = length - 1; /* null prefix */
wolfSSL 0:1239e9b70ca2 6010 XMEMCPY(pub, &input[*inOutIdx], pubSz);
wolfSSL 0:1239e9b70ca2 6011
wolfSSL 0:1239e9b70ca2 6012 *inOutIdx += length;
wolfSSL 0:1239e9b70ca2 6013
wolfSSL 0:1239e9b70ca2 6014 return ecc_import_private_key(priv, privSz, pub, pubSz, key);
wolfSSL 0:1239e9b70ca2 6015 }
wolfSSL 0:1239e9b70ca2 6016
wolfSSL 0:1239e9b70ca2 6017 #endif /* HAVE_ECC */
wolfSSL 0:1239e9b70ca2 6018
wolfSSL 0:1239e9b70ca2 6019
wolfSSL 0:1239e9b70ca2 6020 #if defined(HAVE_OCSP) || defined(HAVE_CRL)
wolfSSL 0:1239e9b70ca2 6021
wolfSSL 0:1239e9b70ca2 6022 /* Get raw Date only, no processing, 0 on success */
wolfSSL 0:1239e9b70ca2 6023 static int GetBasicDate(const byte* source, word32* idx, byte* date,
wolfSSL 0:1239e9b70ca2 6024 byte* format, int maxIdx)
wolfSSL 0:1239e9b70ca2 6025 {
wolfSSL 0:1239e9b70ca2 6026 int length;
wolfSSL 0:1239e9b70ca2 6027
wolfSSL 0:1239e9b70ca2 6028 CYASSL_ENTER("GetBasicDate");
wolfSSL 0:1239e9b70ca2 6029
wolfSSL 0:1239e9b70ca2 6030 *format = source[*idx];
wolfSSL 0:1239e9b70ca2 6031 *idx += 1;
wolfSSL 0:1239e9b70ca2 6032 if (*format != ASN_UTC_TIME && *format != ASN_GENERALIZED_TIME)
wolfSSL 0:1239e9b70ca2 6033 return ASN_TIME_E;
wolfSSL 0:1239e9b70ca2 6034
wolfSSL 0:1239e9b70ca2 6035 if (GetLength(source, idx, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 6036 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6037
wolfSSL 0:1239e9b70ca2 6038 if (length > MAX_DATE_SIZE || length < MIN_DATE_SIZE)
wolfSSL 0:1239e9b70ca2 6039 return ASN_DATE_SZ_E;
wolfSSL 0:1239e9b70ca2 6040
wolfSSL 0:1239e9b70ca2 6041 XMEMCPY(date, &source[*idx], length);
wolfSSL 0:1239e9b70ca2 6042 *idx += length;
wolfSSL 0:1239e9b70ca2 6043
wolfSSL 0:1239e9b70ca2 6044 return 0;
wolfSSL 0:1239e9b70ca2 6045 }
wolfSSL 0:1239e9b70ca2 6046
wolfSSL 0:1239e9b70ca2 6047 #endif
wolfSSL 0:1239e9b70ca2 6048
wolfSSL 0:1239e9b70ca2 6049
wolfSSL 0:1239e9b70ca2 6050 #ifdef HAVE_OCSP
wolfSSL 0:1239e9b70ca2 6051
wolfSSL 0:1239e9b70ca2 6052 static int GetEnumerated(const byte* input, word32* inOutIdx, int *value)
wolfSSL 0:1239e9b70ca2 6053 {
wolfSSL 0:1239e9b70ca2 6054 word32 idx = *inOutIdx;
wolfSSL 0:1239e9b70ca2 6055 word32 len;
wolfSSL 0:1239e9b70ca2 6056
wolfSSL 0:1239e9b70ca2 6057 CYASSL_ENTER("GetEnumerated");
wolfSSL 0:1239e9b70ca2 6058
wolfSSL 0:1239e9b70ca2 6059 *value = 0;
wolfSSL 0:1239e9b70ca2 6060
wolfSSL 0:1239e9b70ca2 6061 if (input[idx++] != ASN_ENUMERATED)
wolfSSL 0:1239e9b70ca2 6062 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6063
wolfSSL 0:1239e9b70ca2 6064 len = input[idx++];
wolfSSL 0:1239e9b70ca2 6065 if (len > 4)
wolfSSL 0:1239e9b70ca2 6066 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6067
wolfSSL 0:1239e9b70ca2 6068 while (len--) {
wolfSSL 0:1239e9b70ca2 6069 *value = *value << 8 | input[idx++];
wolfSSL 0:1239e9b70ca2 6070 }
wolfSSL 0:1239e9b70ca2 6071
wolfSSL 0:1239e9b70ca2 6072 *inOutIdx = idx;
wolfSSL 0:1239e9b70ca2 6073
wolfSSL 0:1239e9b70ca2 6074 return *value;
wolfSSL 0:1239e9b70ca2 6075 }
wolfSSL 0:1239e9b70ca2 6076
wolfSSL 0:1239e9b70ca2 6077
wolfSSL 0:1239e9b70ca2 6078 static int DecodeSingleResponse(byte* source,
wolfSSL 0:1239e9b70ca2 6079 word32* ioIndex, OcspResponse* resp, word32 size)
wolfSSL 0:1239e9b70ca2 6080 {
wolfSSL 0:1239e9b70ca2 6081 word32 idx = *ioIndex, prevIndex, oid;
wolfSSL 0:1239e9b70ca2 6082 int length, wrapperSz;
wolfSSL 0:1239e9b70ca2 6083 CertStatus* cs = resp->status;
wolfSSL 0:1239e9b70ca2 6084
wolfSSL 0:1239e9b70ca2 6085 CYASSL_ENTER("DecodeSingleResponse");
wolfSSL 0:1239e9b70ca2 6086
wolfSSL 0:1239e9b70ca2 6087 /* Outer wrapper of the SEQUENCE OF Single Responses. */
wolfSSL 0:1239e9b70ca2 6088 if (GetSequence(source, &idx, &wrapperSz, size) < 0)
wolfSSL 0:1239e9b70ca2 6089 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6090
wolfSSL 0:1239e9b70ca2 6091 prevIndex = idx;
wolfSSL 0:1239e9b70ca2 6092
wolfSSL 0:1239e9b70ca2 6093 /* When making a request, we only request one status on one certificate
wolfSSL 0:1239e9b70ca2 6094 * at a time. There should only be one SingleResponse */
wolfSSL 0:1239e9b70ca2 6095
wolfSSL 0:1239e9b70ca2 6096 /* Wrapper around the Single Response */
wolfSSL 0:1239e9b70ca2 6097 if (GetSequence(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6098 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6099
wolfSSL 0:1239e9b70ca2 6100 /* Wrapper around the CertID */
wolfSSL 0:1239e9b70ca2 6101 if (GetSequence(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6102 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6103 /* Skip the hash algorithm */
wolfSSL 0:1239e9b70ca2 6104 if (GetAlgoId(source, &idx, &oid, size) < 0)
wolfSSL 0:1239e9b70ca2 6105 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6106 /* Save reference to the hash of CN */
wolfSSL 0:1239e9b70ca2 6107 if (source[idx++] != ASN_OCTET_STRING)
wolfSSL 0:1239e9b70ca2 6108 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6109 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6110 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6111 resp->issuerHash = source + idx;
wolfSSL 0:1239e9b70ca2 6112 idx += length;
wolfSSL 0:1239e9b70ca2 6113 /* Save reference to the hash of the issuer public key */
wolfSSL 0:1239e9b70ca2 6114 if (source[idx++] != ASN_OCTET_STRING)
wolfSSL 0:1239e9b70ca2 6115 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6116 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6117 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6118 resp->issuerKeyHash = source + idx;
wolfSSL 0:1239e9b70ca2 6119 idx += length;
wolfSSL 0:1239e9b70ca2 6120
wolfSSL 0:1239e9b70ca2 6121 /* Read the serial number, it is handled as a string, not as a
wolfSSL 0:1239e9b70ca2 6122 * proper number. Just XMEMCPY the data over, rather than load it
wolfSSL 0:1239e9b70ca2 6123 * as an mp_int. */
wolfSSL 0:1239e9b70ca2 6124 if (source[idx++] != ASN_INTEGER)
wolfSSL 0:1239e9b70ca2 6125 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6126 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6127 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6128 if (length <= EXTERNAL_SERIAL_SIZE)
wolfSSL 0:1239e9b70ca2 6129 {
wolfSSL 0:1239e9b70ca2 6130 if (source[idx] == 0)
wolfSSL 0:1239e9b70ca2 6131 {
wolfSSL 0:1239e9b70ca2 6132 idx++;
wolfSSL 0:1239e9b70ca2 6133 length--;
wolfSSL 0:1239e9b70ca2 6134 }
wolfSSL 0:1239e9b70ca2 6135 XMEMCPY(cs->serial, source + idx, length);
wolfSSL 0:1239e9b70ca2 6136 cs->serialSz = length;
wolfSSL 0:1239e9b70ca2 6137 }
wolfSSL 0:1239e9b70ca2 6138 else
wolfSSL 0:1239e9b70ca2 6139 {
wolfSSL 0:1239e9b70ca2 6140 return ASN_GETINT_E;
wolfSSL 0:1239e9b70ca2 6141 }
wolfSSL 0:1239e9b70ca2 6142 idx += length;
wolfSSL 0:1239e9b70ca2 6143
wolfSSL 0:1239e9b70ca2 6144 /* CertStatus */
wolfSSL 0:1239e9b70ca2 6145 switch (source[idx++])
wolfSSL 0:1239e9b70ca2 6146 {
wolfSSL 0:1239e9b70ca2 6147 case (ASN_CONTEXT_SPECIFIC | CERT_GOOD):
wolfSSL 0:1239e9b70ca2 6148 cs->status = CERT_GOOD;
wolfSSL 0:1239e9b70ca2 6149 idx++;
wolfSSL 0:1239e9b70ca2 6150 break;
wolfSSL 0:1239e9b70ca2 6151 case (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | CERT_REVOKED):
wolfSSL 0:1239e9b70ca2 6152 cs->status = CERT_REVOKED;
wolfSSL 0:1239e9b70ca2 6153 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6154 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6155 idx += length;
wolfSSL 0:1239e9b70ca2 6156 break;
wolfSSL 0:1239e9b70ca2 6157 case (ASN_CONTEXT_SPECIFIC | CERT_UNKNOWN):
wolfSSL 0:1239e9b70ca2 6158 cs->status = CERT_UNKNOWN;
wolfSSL 0:1239e9b70ca2 6159 idx++;
wolfSSL 0:1239e9b70ca2 6160 break;
wolfSSL 0:1239e9b70ca2 6161 default:
wolfSSL 0:1239e9b70ca2 6162 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6163 }
wolfSSL 0:1239e9b70ca2 6164
wolfSSL 0:1239e9b70ca2 6165 if (GetBasicDate(source, &idx, cs->thisDate,
wolfSSL 0:1239e9b70ca2 6166 &cs->thisDateFormat, size) < 0)
wolfSSL 0:1239e9b70ca2 6167 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6168 if (!XVALIDATE_DATE(cs->thisDate, cs->thisDateFormat, BEFORE))
wolfSSL 0:1239e9b70ca2 6169 return ASN_BEFORE_DATE_E;
wolfSSL 0:1239e9b70ca2 6170
wolfSSL 0:1239e9b70ca2 6171 /* The following items are optional. Only check for them if there is more
wolfSSL 0:1239e9b70ca2 6172 * unprocessed data in the singleResponse wrapper. */
wolfSSL 0:1239e9b70ca2 6173
wolfSSL 0:1239e9b70ca2 6174 if (((int)(idx - prevIndex) < wrapperSz) &&
wolfSSL 0:1239e9b70ca2 6175 (source[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)))
wolfSSL 0:1239e9b70ca2 6176 {
wolfSSL 0:1239e9b70ca2 6177 idx++;
wolfSSL 0:1239e9b70ca2 6178 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6179 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6180 if (GetBasicDate(source, &idx, cs->nextDate,
wolfSSL 0:1239e9b70ca2 6181 &cs->nextDateFormat, size) < 0)
wolfSSL 0:1239e9b70ca2 6182 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6183 }
wolfSSL 0:1239e9b70ca2 6184 if (((int)(idx - prevIndex) < wrapperSz) &&
wolfSSL 0:1239e9b70ca2 6185 (source[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1)))
wolfSSL 0:1239e9b70ca2 6186 {
wolfSSL 0:1239e9b70ca2 6187 idx++;
wolfSSL 0:1239e9b70ca2 6188 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6189 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6190 idx += length;
wolfSSL 0:1239e9b70ca2 6191 }
wolfSSL 0:1239e9b70ca2 6192
wolfSSL 0:1239e9b70ca2 6193 *ioIndex = idx;
wolfSSL 0:1239e9b70ca2 6194
wolfSSL 0:1239e9b70ca2 6195 return 0;
wolfSSL 0:1239e9b70ca2 6196 }
wolfSSL 0:1239e9b70ca2 6197
wolfSSL 0:1239e9b70ca2 6198 static int DecodeOcspRespExtensions(byte* source,
wolfSSL 0:1239e9b70ca2 6199 word32* ioIndex, OcspResponse* resp, word32 sz)
wolfSSL 0:1239e9b70ca2 6200 {
wolfSSL 0:1239e9b70ca2 6201 word32 idx = *ioIndex;
wolfSSL 0:1239e9b70ca2 6202 int length;
wolfSSL 0:1239e9b70ca2 6203 int ext_bound; /* boundary index for the sequence of extensions */
wolfSSL 0:1239e9b70ca2 6204 word32 oid;
wolfSSL 0:1239e9b70ca2 6205
wolfSSL 0:1239e9b70ca2 6206 CYASSL_ENTER("DecodeOcspRespExtensions");
wolfSSL 0:1239e9b70ca2 6207
wolfSSL 0:1239e9b70ca2 6208 if (source[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1))
wolfSSL 0:1239e9b70ca2 6209 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6210
wolfSSL 0:1239e9b70ca2 6211 if (GetLength(source, &idx, &length, sz) < 0) return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6212
wolfSSL 0:1239e9b70ca2 6213 if (GetSequence(source, &idx, &length, sz) < 0) return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6214
wolfSSL 0:1239e9b70ca2 6215 ext_bound = idx + length;
wolfSSL 0:1239e9b70ca2 6216
wolfSSL 0:1239e9b70ca2 6217 while (idx < (word32)ext_bound) {
wolfSSL 0:1239e9b70ca2 6218 if (GetSequence(source, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 6219 CYASSL_MSG("\tfail: should be a SEQUENCE");
wolfSSL 0:1239e9b70ca2 6220 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6221 }
wolfSSL 0:1239e9b70ca2 6222
wolfSSL 0:1239e9b70ca2 6223 oid = 0;
wolfSSL 0:1239e9b70ca2 6224 if (GetObjectId(source, &idx, &oid, sz) < 0) {
wolfSSL 0:1239e9b70ca2 6225 CYASSL_MSG("\tfail: OBJECT ID");
wolfSSL 0:1239e9b70ca2 6226 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6227 }
wolfSSL 0:1239e9b70ca2 6228
wolfSSL 0:1239e9b70ca2 6229 /* check for critical flag */
wolfSSL 0:1239e9b70ca2 6230 if (source[idx] == ASN_BOOLEAN) {
wolfSSL 0:1239e9b70ca2 6231 CYASSL_MSG("\tfound optional critical flag, moving past");
wolfSSL 0:1239e9b70ca2 6232 idx += (ASN_BOOL_SIZE + 1);
wolfSSL 0:1239e9b70ca2 6233 }
wolfSSL 0:1239e9b70ca2 6234
wolfSSL 0:1239e9b70ca2 6235 /* process the extension based on the OID */
wolfSSL 0:1239e9b70ca2 6236 if (source[idx++] != ASN_OCTET_STRING) {
wolfSSL 0:1239e9b70ca2 6237 CYASSL_MSG("\tfail: should be an OCTET STRING");
wolfSSL 0:1239e9b70ca2 6238 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6239 }
wolfSSL 0:1239e9b70ca2 6240
wolfSSL 0:1239e9b70ca2 6241 if (GetLength(source, &idx, &length, sz) < 0) {
wolfSSL 0:1239e9b70ca2 6242 CYASSL_MSG("\tfail: extension data length");
wolfSSL 0:1239e9b70ca2 6243 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6244 }
wolfSSL 0:1239e9b70ca2 6245
wolfSSL 0:1239e9b70ca2 6246 if (oid == OCSP_NONCE_OID) {
wolfSSL 0:1239e9b70ca2 6247 resp->nonce = source + idx;
wolfSSL 0:1239e9b70ca2 6248 resp->nonceSz = length;
wolfSSL 0:1239e9b70ca2 6249 }
wolfSSL 0:1239e9b70ca2 6250
wolfSSL 0:1239e9b70ca2 6251 idx += length;
wolfSSL 0:1239e9b70ca2 6252 }
wolfSSL 0:1239e9b70ca2 6253
wolfSSL 0:1239e9b70ca2 6254 *ioIndex = idx;
wolfSSL 0:1239e9b70ca2 6255 return 0;
wolfSSL 0:1239e9b70ca2 6256 }
wolfSSL 0:1239e9b70ca2 6257
wolfSSL 0:1239e9b70ca2 6258
wolfSSL 0:1239e9b70ca2 6259 static int DecodeResponseData(byte* source,
wolfSSL 0:1239e9b70ca2 6260 word32* ioIndex, OcspResponse* resp, word32 size)
wolfSSL 0:1239e9b70ca2 6261 {
wolfSSL 0:1239e9b70ca2 6262 word32 idx = *ioIndex, prev_idx;
wolfSSL 0:1239e9b70ca2 6263 int length;
wolfSSL 0:1239e9b70ca2 6264 int version;
wolfSSL 0:1239e9b70ca2 6265 word32 responderId = 0;
wolfSSL 0:1239e9b70ca2 6266
wolfSSL 0:1239e9b70ca2 6267 CYASSL_ENTER("DecodeResponseData");
wolfSSL 0:1239e9b70ca2 6268
wolfSSL 0:1239e9b70ca2 6269 resp->response = source + idx;
wolfSSL 0:1239e9b70ca2 6270 prev_idx = idx;
wolfSSL 0:1239e9b70ca2 6271 if (GetSequence(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6272 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6273 resp->responseSz = length + idx - prev_idx;
wolfSSL 0:1239e9b70ca2 6274
wolfSSL 0:1239e9b70ca2 6275 /* Get version. It is an EXPLICIT[0] DEFAULT(0) value. If this
wolfSSL 0:1239e9b70ca2 6276 * item isn't an EXPLICIT[0], then set version to zero and move
wolfSSL 0:1239e9b70ca2 6277 * onto the next item.
wolfSSL 0:1239e9b70ca2 6278 */
wolfSSL 0:1239e9b70ca2 6279 if (source[idx] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED))
wolfSSL 0:1239e9b70ca2 6280 {
wolfSSL 0:1239e9b70ca2 6281 idx += 2; /* Eat the value and length */
wolfSSL 0:1239e9b70ca2 6282 if (GetMyVersion(source, &idx, &version) < 0)
wolfSSL 0:1239e9b70ca2 6283 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6284 } else
wolfSSL 0:1239e9b70ca2 6285 version = 0;
wolfSSL 0:1239e9b70ca2 6286
wolfSSL 0:1239e9b70ca2 6287 responderId = source[idx++];
wolfSSL 0:1239e9b70ca2 6288 if ((responderId == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1)) ||
wolfSSL 0:1239e9b70ca2 6289 (responderId == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 2)))
wolfSSL 0:1239e9b70ca2 6290 {
wolfSSL 0:1239e9b70ca2 6291 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6292 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6293 idx += length;
wolfSSL 0:1239e9b70ca2 6294 }
wolfSSL 0:1239e9b70ca2 6295 else
wolfSSL 0:1239e9b70ca2 6296 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6297
wolfSSL 0:1239e9b70ca2 6298 /* save pointer to the producedAt time */
wolfSSL 0:1239e9b70ca2 6299 if (GetBasicDate(source, &idx, resp->producedDate,
wolfSSL 0:1239e9b70ca2 6300 &resp->producedDateFormat, size) < 0)
wolfSSL 0:1239e9b70ca2 6301 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6302
wolfSSL 0:1239e9b70ca2 6303 if (DecodeSingleResponse(source, &idx, resp, size) < 0)
wolfSSL 0:1239e9b70ca2 6304 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6305
wolfSSL 0:1239e9b70ca2 6306 if (DecodeOcspRespExtensions(source, &idx, resp, size) < 0)
wolfSSL 0:1239e9b70ca2 6307 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6308
wolfSSL 0:1239e9b70ca2 6309 *ioIndex = idx;
wolfSSL 0:1239e9b70ca2 6310 return 0;
wolfSSL 0:1239e9b70ca2 6311 }
wolfSSL 0:1239e9b70ca2 6312
wolfSSL 0:1239e9b70ca2 6313
wolfSSL 0:1239e9b70ca2 6314 static int DecodeCerts(byte* source,
wolfSSL 0:1239e9b70ca2 6315 word32* ioIndex, OcspResponse* resp, word32 size)
wolfSSL 0:1239e9b70ca2 6316 {
wolfSSL 0:1239e9b70ca2 6317 word32 idx = *ioIndex;
wolfSSL 0:1239e9b70ca2 6318
wolfSSL 0:1239e9b70ca2 6319 CYASSL_ENTER("DecodeCerts");
wolfSSL 0:1239e9b70ca2 6320
wolfSSL 0:1239e9b70ca2 6321 if (source[idx++] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC))
wolfSSL 0:1239e9b70ca2 6322 {
wolfSSL 0:1239e9b70ca2 6323 int length;
wolfSSL 0:1239e9b70ca2 6324
wolfSSL 0:1239e9b70ca2 6325 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6326 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6327
wolfSSL 0:1239e9b70ca2 6328 if (GetSequence(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6329 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6330
wolfSSL 0:1239e9b70ca2 6331 resp->cert = source + idx;
wolfSSL 0:1239e9b70ca2 6332 resp->certSz = length;
wolfSSL 0:1239e9b70ca2 6333
wolfSSL 0:1239e9b70ca2 6334 idx += length;
wolfSSL 0:1239e9b70ca2 6335 }
wolfSSL 0:1239e9b70ca2 6336 *ioIndex = idx;
wolfSSL 0:1239e9b70ca2 6337 return 0;
wolfSSL 0:1239e9b70ca2 6338 }
wolfSSL 0:1239e9b70ca2 6339
wolfSSL 0:1239e9b70ca2 6340 static int DecodeBasicOcspResponse(byte* source,
wolfSSL 0:1239e9b70ca2 6341 word32* ioIndex, OcspResponse* resp, word32 size)
wolfSSL 0:1239e9b70ca2 6342 {
wolfSSL 0:1239e9b70ca2 6343 int length;
wolfSSL 0:1239e9b70ca2 6344 word32 idx = *ioIndex;
wolfSSL 0:1239e9b70ca2 6345 word32 end_index;
wolfSSL 0:1239e9b70ca2 6346
wolfSSL 0:1239e9b70ca2 6347 CYASSL_ENTER("DecodeBasicOcspResponse");
wolfSSL 0:1239e9b70ca2 6348
wolfSSL 0:1239e9b70ca2 6349 if (GetSequence(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6350 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6351
wolfSSL 0:1239e9b70ca2 6352 if (idx + length > size)
wolfSSL 0:1239e9b70ca2 6353 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 6354 end_index = idx + length;
wolfSSL 0:1239e9b70ca2 6355
wolfSSL 0:1239e9b70ca2 6356 if (DecodeResponseData(source, &idx, resp, size) < 0)
wolfSSL 0:1239e9b70ca2 6357 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6358
wolfSSL 0:1239e9b70ca2 6359 /* Get the signature algorithm */
wolfSSL 0:1239e9b70ca2 6360 if (GetAlgoId(source, &idx, &resp->sigOID, size) < 0)
wolfSSL 0:1239e9b70ca2 6361 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6362
wolfSSL 0:1239e9b70ca2 6363 /* Obtain pointer to the start of the signature, and save the size */
wolfSSL 0:1239e9b70ca2 6364 if (source[idx++] == ASN_BIT_STRING)
wolfSSL 0:1239e9b70ca2 6365 {
wolfSSL 0:1239e9b70ca2 6366 int sigLength = 0;
wolfSSL 0:1239e9b70ca2 6367 if (GetLength(source, &idx, &sigLength, size) < 0)
wolfSSL 0:1239e9b70ca2 6368 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6369 resp->sigSz = sigLength;
wolfSSL 0:1239e9b70ca2 6370 resp->sig = source + idx;
wolfSSL 0:1239e9b70ca2 6371 idx += sigLength;
wolfSSL 0:1239e9b70ca2 6372 }
wolfSSL 0:1239e9b70ca2 6373
wolfSSL 0:1239e9b70ca2 6374 /*
wolfSSL 0:1239e9b70ca2 6375 * Check the length of the BasicOcspResponse against the current index to
wolfSSL 0:1239e9b70ca2 6376 * see if there are certificates, they are optional.
wolfSSL 0:1239e9b70ca2 6377 */
wolfSSL 0:1239e9b70ca2 6378 if (idx < end_index)
wolfSSL 0:1239e9b70ca2 6379 {
wolfSSL 0:1239e9b70ca2 6380 DecodedCert cert;
wolfSSL 0:1239e9b70ca2 6381 int ret;
wolfSSL 0:1239e9b70ca2 6382
wolfSSL 0:1239e9b70ca2 6383 if (DecodeCerts(source, &idx, resp, size) < 0)
wolfSSL 0:1239e9b70ca2 6384 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6385
wolfSSL 0:1239e9b70ca2 6386 InitDecodedCert(&cert, resp->cert, resp->certSz, 0);
wolfSSL 0:1239e9b70ca2 6387 ret = ParseCertRelative(&cert, CA_TYPE, NO_VERIFY, 0);
wolfSSL 0:1239e9b70ca2 6388 if (ret < 0)
wolfSSL 0:1239e9b70ca2 6389 return ret;
wolfSSL 0:1239e9b70ca2 6390
wolfSSL 0:1239e9b70ca2 6391 ret = ConfirmSignature(resp->response, resp->responseSz,
wolfSSL 0:1239e9b70ca2 6392 cert.publicKey, cert.pubKeySize, cert.keyOID,
wolfSSL 0:1239e9b70ca2 6393 resp->sig, resp->sigSz, resp->sigOID, NULL);
wolfSSL 0:1239e9b70ca2 6394 FreeDecodedCert(&cert);
wolfSSL 0:1239e9b70ca2 6395
wolfSSL 0:1239e9b70ca2 6396 if (ret == 0)
wolfSSL 0:1239e9b70ca2 6397 {
wolfSSL 0:1239e9b70ca2 6398 CYASSL_MSG("\tOCSP Confirm signature failed");
wolfSSL 0:1239e9b70ca2 6399 return ASN_OCSP_CONFIRM_E;
wolfSSL 0:1239e9b70ca2 6400 }
wolfSSL 0:1239e9b70ca2 6401 }
wolfSSL 0:1239e9b70ca2 6402
wolfSSL 0:1239e9b70ca2 6403 *ioIndex = idx;
wolfSSL 0:1239e9b70ca2 6404 return 0;
wolfSSL 0:1239e9b70ca2 6405 }
wolfSSL 0:1239e9b70ca2 6406
wolfSSL 0:1239e9b70ca2 6407
wolfSSL 0:1239e9b70ca2 6408 void InitOcspResponse(OcspResponse* resp, CertStatus* status,
wolfSSL 0:1239e9b70ca2 6409 byte* source, word32 inSz)
wolfSSL 0:1239e9b70ca2 6410 {
wolfSSL 0:1239e9b70ca2 6411 CYASSL_ENTER("InitOcspResponse");
wolfSSL 0:1239e9b70ca2 6412
wolfSSL 0:1239e9b70ca2 6413 resp->responseStatus = -1;
wolfSSL 0:1239e9b70ca2 6414 resp->response = NULL;
wolfSSL 0:1239e9b70ca2 6415 resp->responseSz = 0;
wolfSSL 0:1239e9b70ca2 6416 resp->producedDateFormat = 0;
wolfSSL 0:1239e9b70ca2 6417 resp->issuerHash = NULL;
wolfSSL 0:1239e9b70ca2 6418 resp->issuerKeyHash = NULL;
wolfSSL 0:1239e9b70ca2 6419 resp->sig = NULL;
wolfSSL 0:1239e9b70ca2 6420 resp->sigSz = 0;
wolfSSL 0:1239e9b70ca2 6421 resp->sigOID = 0;
wolfSSL 0:1239e9b70ca2 6422 resp->status = status;
wolfSSL 0:1239e9b70ca2 6423 resp->nonce = NULL;
wolfSSL 0:1239e9b70ca2 6424 resp->nonceSz = 0;
wolfSSL 0:1239e9b70ca2 6425 resp->source = source;
wolfSSL 0:1239e9b70ca2 6426 resp->maxIdx = inSz;
wolfSSL 0:1239e9b70ca2 6427 }
wolfSSL 0:1239e9b70ca2 6428
wolfSSL 0:1239e9b70ca2 6429
wolfSSL 0:1239e9b70ca2 6430 int OcspResponseDecode(OcspResponse* resp)
wolfSSL 0:1239e9b70ca2 6431 {
wolfSSL 0:1239e9b70ca2 6432 int length = 0;
wolfSSL 0:1239e9b70ca2 6433 word32 idx = 0;
wolfSSL 0:1239e9b70ca2 6434 byte* source = resp->source;
wolfSSL 0:1239e9b70ca2 6435 word32 size = resp->maxIdx;
wolfSSL 0:1239e9b70ca2 6436 word32 oid;
wolfSSL 0:1239e9b70ca2 6437
wolfSSL 0:1239e9b70ca2 6438 CYASSL_ENTER("OcspResponseDecode");
wolfSSL 0:1239e9b70ca2 6439
wolfSSL 0:1239e9b70ca2 6440 /* peel the outer SEQUENCE wrapper */
wolfSSL 0:1239e9b70ca2 6441 if (GetSequence(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6442 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6443
wolfSSL 0:1239e9b70ca2 6444 /* First get the responseStatus, an ENUMERATED */
wolfSSL 0:1239e9b70ca2 6445 if (GetEnumerated(source, &idx, &resp->responseStatus) < 0)
wolfSSL 0:1239e9b70ca2 6446 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6447
wolfSSL 0:1239e9b70ca2 6448 if (resp->responseStatus != OCSP_SUCCESSFUL)
wolfSSL 0:1239e9b70ca2 6449 return 0;
wolfSSL 0:1239e9b70ca2 6450
wolfSSL 0:1239e9b70ca2 6451 /* Next is an EXPLICIT record called ResponseBytes, OPTIONAL */
wolfSSL 0:1239e9b70ca2 6452 if (idx >= size)
wolfSSL 0:1239e9b70ca2 6453 return ASN_INPUT_E;
wolfSSL 0:1239e9b70ca2 6454 if (source[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC))
wolfSSL 0:1239e9b70ca2 6455 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6456 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6457 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6458
wolfSSL 0:1239e9b70ca2 6459 /* Get the responseBytes SEQUENCE */
wolfSSL 0:1239e9b70ca2 6460 if (GetSequence(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6461 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6462
wolfSSL 0:1239e9b70ca2 6463 /* Check ObjectID for the resposeBytes */
wolfSSL 0:1239e9b70ca2 6464 if (GetObjectId(source, &idx, &oid, size) < 0)
wolfSSL 0:1239e9b70ca2 6465 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6466 if (oid != OCSP_BASIC_OID)
wolfSSL 0:1239e9b70ca2 6467 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6468 if (source[idx++] != ASN_OCTET_STRING)
wolfSSL 0:1239e9b70ca2 6469 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6470
wolfSSL 0:1239e9b70ca2 6471 if (GetLength(source, &idx, &length, size) < 0)
wolfSSL 0:1239e9b70ca2 6472 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6473
wolfSSL 0:1239e9b70ca2 6474 if (DecodeBasicOcspResponse(source, &idx, resp, size) < 0)
wolfSSL 0:1239e9b70ca2 6475 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6476
wolfSSL 0:1239e9b70ca2 6477 return 0;
wolfSSL 0:1239e9b70ca2 6478 }
wolfSSL 0:1239e9b70ca2 6479
wolfSSL 0:1239e9b70ca2 6480
wolfSSL 0:1239e9b70ca2 6481 static word32 SetOcspReqExtensions(word32 extSz, byte* output,
wolfSSL 0:1239e9b70ca2 6482 const byte* nonce, word32 nonceSz)
wolfSSL 0:1239e9b70ca2 6483 {
wolfSSL 0:1239e9b70ca2 6484 static const byte NonceObjId[] = { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
wolfSSL 0:1239e9b70ca2 6485 0x30, 0x01, 0x02 };
wolfSSL 0:1239e9b70ca2 6486 byte seqArray[5][MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 6487 word32 seqSz[5], totalSz;
wolfSSL 0:1239e9b70ca2 6488
wolfSSL 0:1239e9b70ca2 6489 CYASSL_ENTER("SetOcspReqExtensions");
wolfSSL 0:1239e9b70ca2 6490
wolfSSL 0:1239e9b70ca2 6491 if (nonce == NULL || nonceSz == 0) return 0;
wolfSSL 0:1239e9b70ca2 6492
wolfSSL 0:1239e9b70ca2 6493 seqArray[0][0] = ASN_OCTET_STRING;
wolfSSL 0:1239e9b70ca2 6494 seqSz[0] = 1 + SetLength(nonceSz, &seqArray[0][1]);
wolfSSL 0:1239e9b70ca2 6495
wolfSSL 0:1239e9b70ca2 6496 seqArray[1][0] = ASN_OBJECT_ID;
wolfSSL 0:1239e9b70ca2 6497 seqSz[1] = 1 + SetLength(sizeof(NonceObjId), &seqArray[1][1]);
wolfSSL 0:1239e9b70ca2 6498
wolfSSL 0:1239e9b70ca2 6499 totalSz = seqSz[0] + seqSz[1] + nonceSz + (word32)sizeof(NonceObjId);
wolfSSL 0:1239e9b70ca2 6500
wolfSSL 0:1239e9b70ca2 6501 seqSz[2] = SetSequence(totalSz, seqArray[2]);
wolfSSL 0:1239e9b70ca2 6502 totalSz += seqSz[2];
wolfSSL 0:1239e9b70ca2 6503
wolfSSL 0:1239e9b70ca2 6504 seqSz[3] = SetSequence(totalSz, seqArray[3]);
wolfSSL 0:1239e9b70ca2 6505 totalSz += seqSz[3];
wolfSSL 0:1239e9b70ca2 6506
wolfSSL 0:1239e9b70ca2 6507 seqArray[4][0] = (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 2);
wolfSSL 0:1239e9b70ca2 6508 seqSz[4] = 1 + SetLength(totalSz, &seqArray[4][1]);
wolfSSL 0:1239e9b70ca2 6509 totalSz += seqSz[4];
wolfSSL 0:1239e9b70ca2 6510
wolfSSL 0:1239e9b70ca2 6511 if (totalSz < extSz)
wolfSSL 0:1239e9b70ca2 6512 {
wolfSSL 0:1239e9b70ca2 6513 totalSz = 0;
wolfSSL 0:1239e9b70ca2 6514 XMEMCPY(output + totalSz, seqArray[4], seqSz[4]);
wolfSSL 0:1239e9b70ca2 6515 totalSz += seqSz[4];
wolfSSL 0:1239e9b70ca2 6516 XMEMCPY(output + totalSz, seqArray[3], seqSz[3]);
wolfSSL 0:1239e9b70ca2 6517 totalSz += seqSz[3];
wolfSSL 0:1239e9b70ca2 6518 XMEMCPY(output + totalSz, seqArray[2], seqSz[2]);
wolfSSL 0:1239e9b70ca2 6519 totalSz += seqSz[2];
wolfSSL 0:1239e9b70ca2 6520 XMEMCPY(output + totalSz, seqArray[1], seqSz[1]);
wolfSSL 0:1239e9b70ca2 6521 totalSz += seqSz[1];
wolfSSL 0:1239e9b70ca2 6522 XMEMCPY(output + totalSz, NonceObjId, sizeof(NonceObjId));
wolfSSL 0:1239e9b70ca2 6523 totalSz += (word32)sizeof(NonceObjId);
wolfSSL 0:1239e9b70ca2 6524 XMEMCPY(output + totalSz, seqArray[0], seqSz[0]);
wolfSSL 0:1239e9b70ca2 6525 totalSz += seqSz[0];
wolfSSL 0:1239e9b70ca2 6526 XMEMCPY(output + totalSz, nonce, nonceSz);
wolfSSL 0:1239e9b70ca2 6527 totalSz += nonceSz;
wolfSSL 0:1239e9b70ca2 6528 }
wolfSSL 0:1239e9b70ca2 6529
wolfSSL 0:1239e9b70ca2 6530 return totalSz;
wolfSSL 0:1239e9b70ca2 6531 }
wolfSSL 0:1239e9b70ca2 6532
wolfSSL 0:1239e9b70ca2 6533
wolfSSL 0:1239e9b70ca2 6534 int EncodeOcspRequest(OcspRequest* req)
wolfSSL 0:1239e9b70ca2 6535 {
wolfSSL 0:1239e9b70ca2 6536 byte seqArray[5][MAX_SEQ_SZ];
wolfSSL 0:1239e9b70ca2 6537 /* The ASN.1 of the OCSP Request is an onion of sequences */
wolfSSL 0:1239e9b70ca2 6538 byte algoArray[MAX_ALGO_SZ];
wolfSSL 0:1239e9b70ca2 6539 byte issuerArray[MAX_ENCODED_DIG_SZ];
wolfSSL 0:1239e9b70ca2 6540 byte issuerKeyArray[MAX_ENCODED_DIG_SZ];
wolfSSL 0:1239e9b70ca2 6541 byte snArray[MAX_SN_SZ];
wolfSSL 0:1239e9b70ca2 6542 byte extArray[MAX_OCSP_EXT_SZ];
wolfSSL 0:1239e9b70ca2 6543 byte* output = req->dest;
wolfSSL 0:1239e9b70ca2 6544 word32 seqSz[5], algoSz, issuerSz, issuerKeySz, snSz, extSz, totalSz;
wolfSSL 0:1239e9b70ca2 6545 int i;
wolfSSL 0:1239e9b70ca2 6546
wolfSSL 0:1239e9b70ca2 6547 CYASSL_ENTER("EncodeOcspRequest");
wolfSSL 0:1239e9b70ca2 6548
wolfSSL 0:1239e9b70ca2 6549 algoSz = SetAlgoID(SHAh, algoArray, hashType, 0);
wolfSSL 0:1239e9b70ca2 6550
wolfSSL 0:1239e9b70ca2 6551 req->issuerHash = req->cert->issuerHash;
wolfSSL 0:1239e9b70ca2 6552 issuerSz = SetDigest(req->cert->issuerHash, SHA_SIZE, issuerArray);
wolfSSL 0:1239e9b70ca2 6553
wolfSSL 0:1239e9b70ca2 6554 req->issuerKeyHash = req->cert->issuerKeyHash;
wolfSSL 0:1239e9b70ca2 6555 issuerKeySz = SetDigest(req->cert->issuerKeyHash, SHA_SIZE, issuerKeyArray);
wolfSSL 0:1239e9b70ca2 6556
wolfSSL 0:1239e9b70ca2 6557 req->serial = req->cert->serial;
wolfSSL 0:1239e9b70ca2 6558 req->serialSz = req->cert->serialSz;
wolfSSL 0:1239e9b70ca2 6559 snSz = SetSerialNumber(req->cert->serial, req->cert->serialSz, snArray);
wolfSSL 0:1239e9b70ca2 6560
wolfSSL 0:1239e9b70ca2 6561 extSz = 0;
wolfSSL 0:1239e9b70ca2 6562 if (req->useNonce) {
wolfSSL 0:1239e9b70ca2 6563 RNG rng;
wolfSSL 0:1239e9b70ca2 6564 if (InitRng(&rng) != 0) {
wolfSSL 0:1239e9b70ca2 6565 CYASSL_MSG("\tCannot initialize RNG. Skipping the OSCP Nonce.");
wolfSSL 0:1239e9b70ca2 6566 } else {
wolfSSL 0:1239e9b70ca2 6567 if (RNG_GenerateBlock(&rng, req->nonce, MAX_OCSP_NONCE_SZ) != 0)
wolfSSL 0:1239e9b70ca2 6568 CYASSL_MSG("\tCannot run RNG. Skipping the OSCP Nonce.");
wolfSSL 0:1239e9b70ca2 6569 else {
wolfSSL 0:1239e9b70ca2 6570 req->nonceSz = MAX_OCSP_NONCE_SZ;
wolfSSL 0:1239e9b70ca2 6571 extSz = SetOcspReqExtensions(MAX_OCSP_EXT_SZ, extArray,
wolfSSL 0:1239e9b70ca2 6572 req->nonce, req->nonceSz);
wolfSSL 0:1239e9b70ca2 6573 }
wolfSSL 0:1239e9b70ca2 6574 }
wolfSSL 0:1239e9b70ca2 6575 }
wolfSSL 0:1239e9b70ca2 6576
wolfSSL 0:1239e9b70ca2 6577 totalSz = algoSz + issuerSz + issuerKeySz + snSz;
wolfSSL 0:1239e9b70ca2 6578
wolfSSL 0:1239e9b70ca2 6579 for (i = 4; i >= 0; i--) {
wolfSSL 0:1239e9b70ca2 6580 seqSz[i] = SetSequence(totalSz, seqArray[i]);
wolfSSL 0:1239e9b70ca2 6581 totalSz += seqSz[i];
wolfSSL 0:1239e9b70ca2 6582 if (i == 2) totalSz += extSz;
wolfSSL 0:1239e9b70ca2 6583 }
wolfSSL 0:1239e9b70ca2 6584 totalSz = 0;
wolfSSL 0:1239e9b70ca2 6585 for (i = 0; i < 5; i++) {
wolfSSL 0:1239e9b70ca2 6586 XMEMCPY(output + totalSz, seqArray[i], seqSz[i]);
wolfSSL 0:1239e9b70ca2 6587 totalSz += seqSz[i];
wolfSSL 0:1239e9b70ca2 6588 }
wolfSSL 0:1239e9b70ca2 6589 XMEMCPY(output + totalSz, algoArray, algoSz);
wolfSSL 0:1239e9b70ca2 6590 totalSz += algoSz;
wolfSSL 0:1239e9b70ca2 6591 XMEMCPY(output + totalSz, issuerArray, issuerSz);
wolfSSL 0:1239e9b70ca2 6592 totalSz += issuerSz;
wolfSSL 0:1239e9b70ca2 6593 XMEMCPY(output + totalSz, issuerKeyArray, issuerKeySz);
wolfSSL 0:1239e9b70ca2 6594 totalSz += issuerKeySz;
wolfSSL 0:1239e9b70ca2 6595 XMEMCPY(output + totalSz, snArray, snSz);
wolfSSL 0:1239e9b70ca2 6596 totalSz += snSz;
wolfSSL 0:1239e9b70ca2 6597 if (extSz != 0) {
wolfSSL 0:1239e9b70ca2 6598 XMEMCPY(output + totalSz, extArray, extSz);
wolfSSL 0:1239e9b70ca2 6599 totalSz += extSz;
wolfSSL 0:1239e9b70ca2 6600 }
wolfSSL 0:1239e9b70ca2 6601
wolfSSL 0:1239e9b70ca2 6602 return totalSz;
wolfSSL 0:1239e9b70ca2 6603 }
wolfSSL 0:1239e9b70ca2 6604
wolfSSL 0:1239e9b70ca2 6605
wolfSSL 0:1239e9b70ca2 6606 void InitOcspRequest(OcspRequest* req, DecodedCert* cert, byte useNonce,
wolfSSL 0:1239e9b70ca2 6607 byte* dest, word32 destSz)
wolfSSL 0:1239e9b70ca2 6608 {
wolfSSL 0:1239e9b70ca2 6609 CYASSL_ENTER("InitOcspRequest");
wolfSSL 0:1239e9b70ca2 6610
wolfSSL 0:1239e9b70ca2 6611 req->cert = cert;
wolfSSL 0:1239e9b70ca2 6612 req->useNonce = useNonce;
wolfSSL 0:1239e9b70ca2 6613 req->nonceSz = 0;
wolfSSL 0:1239e9b70ca2 6614 req->issuerHash = NULL;
wolfSSL 0:1239e9b70ca2 6615 req->issuerKeyHash = NULL;
wolfSSL 0:1239e9b70ca2 6616 req->serial = NULL;
wolfSSL 0:1239e9b70ca2 6617 req->dest = dest;
wolfSSL 0:1239e9b70ca2 6618 req->destSz = destSz;
wolfSSL 0:1239e9b70ca2 6619 }
wolfSSL 0:1239e9b70ca2 6620
wolfSSL 0:1239e9b70ca2 6621
wolfSSL 0:1239e9b70ca2 6622 int CompareOcspReqResp(OcspRequest* req, OcspResponse* resp)
wolfSSL 0:1239e9b70ca2 6623 {
wolfSSL 0:1239e9b70ca2 6624 int cmp;
wolfSSL 0:1239e9b70ca2 6625
wolfSSL 0:1239e9b70ca2 6626 CYASSL_ENTER("CompareOcspReqResp");
wolfSSL 0:1239e9b70ca2 6627
wolfSSL 0:1239e9b70ca2 6628 if (req == NULL)
wolfSSL 0:1239e9b70ca2 6629 {
wolfSSL 0:1239e9b70ca2 6630 CYASSL_MSG("\tReq missing");
wolfSSL 0:1239e9b70ca2 6631 return -1;
wolfSSL 0:1239e9b70ca2 6632 }
wolfSSL 0:1239e9b70ca2 6633
wolfSSL 0:1239e9b70ca2 6634 if (resp == NULL)
wolfSSL 0:1239e9b70ca2 6635 {
wolfSSL 0:1239e9b70ca2 6636 CYASSL_MSG("\tResp missing");
wolfSSL 0:1239e9b70ca2 6637 return 1;
wolfSSL 0:1239e9b70ca2 6638 }
wolfSSL 0:1239e9b70ca2 6639
wolfSSL 0:1239e9b70ca2 6640 /* Nonces are not critical. The responder may not necessarily add
wolfSSL 0:1239e9b70ca2 6641 * the nonce to the response. */
wolfSSL 0:1239e9b70ca2 6642 if (req->useNonce && resp->nonceSz != 0) {
wolfSSL 0:1239e9b70ca2 6643 cmp = req->nonceSz - resp->nonceSz;
wolfSSL 0:1239e9b70ca2 6644 if (cmp != 0)
wolfSSL 0:1239e9b70ca2 6645 {
wolfSSL 0:1239e9b70ca2 6646 CYASSL_MSG("\tnonceSz mismatch");
wolfSSL 0:1239e9b70ca2 6647 return cmp;
wolfSSL 0:1239e9b70ca2 6648 }
wolfSSL 0:1239e9b70ca2 6649
wolfSSL 0:1239e9b70ca2 6650 cmp = XMEMCMP(req->nonce, resp->nonce, req->nonceSz);
wolfSSL 0:1239e9b70ca2 6651 if (cmp != 0)
wolfSSL 0:1239e9b70ca2 6652 {
wolfSSL 0:1239e9b70ca2 6653 CYASSL_MSG("\tnonce mismatch");
wolfSSL 0:1239e9b70ca2 6654 return cmp;
wolfSSL 0:1239e9b70ca2 6655 }
wolfSSL 0:1239e9b70ca2 6656 }
wolfSSL 0:1239e9b70ca2 6657
wolfSSL 0:1239e9b70ca2 6658 cmp = XMEMCMP(req->issuerHash, resp->issuerHash, SHA_DIGEST_SIZE);
wolfSSL 0:1239e9b70ca2 6659 if (cmp != 0)
wolfSSL 0:1239e9b70ca2 6660 {
wolfSSL 0:1239e9b70ca2 6661 CYASSL_MSG("\tissuerHash mismatch");
wolfSSL 0:1239e9b70ca2 6662 return cmp;
wolfSSL 0:1239e9b70ca2 6663 }
wolfSSL 0:1239e9b70ca2 6664
wolfSSL 0:1239e9b70ca2 6665 cmp = XMEMCMP(req->issuerKeyHash, resp->issuerKeyHash, SHA_DIGEST_SIZE);
wolfSSL 0:1239e9b70ca2 6666 if (cmp != 0)
wolfSSL 0:1239e9b70ca2 6667 {
wolfSSL 0:1239e9b70ca2 6668 CYASSL_MSG("\tissuerKeyHash mismatch");
wolfSSL 0:1239e9b70ca2 6669 return cmp;
wolfSSL 0:1239e9b70ca2 6670 }
wolfSSL 0:1239e9b70ca2 6671
wolfSSL 0:1239e9b70ca2 6672 cmp = req->serialSz - resp->status->serialSz;
wolfSSL 0:1239e9b70ca2 6673 if (cmp != 0)
wolfSSL 0:1239e9b70ca2 6674 {
wolfSSL 0:1239e9b70ca2 6675 CYASSL_MSG("\tserialSz mismatch");
wolfSSL 0:1239e9b70ca2 6676 return cmp;
wolfSSL 0:1239e9b70ca2 6677 }
wolfSSL 0:1239e9b70ca2 6678
wolfSSL 0:1239e9b70ca2 6679 cmp = XMEMCMP(req->serial, resp->status->serial, req->serialSz);
wolfSSL 0:1239e9b70ca2 6680 if (cmp != 0)
wolfSSL 0:1239e9b70ca2 6681 {
wolfSSL 0:1239e9b70ca2 6682 CYASSL_MSG("\tserial mismatch");
wolfSSL 0:1239e9b70ca2 6683 return cmp;
wolfSSL 0:1239e9b70ca2 6684 }
wolfSSL 0:1239e9b70ca2 6685
wolfSSL 0:1239e9b70ca2 6686 return 0;
wolfSSL 0:1239e9b70ca2 6687 }
wolfSSL 0:1239e9b70ca2 6688
wolfSSL 0:1239e9b70ca2 6689 #endif
wolfSSL 0:1239e9b70ca2 6690
wolfSSL 0:1239e9b70ca2 6691
wolfSSL 0:1239e9b70ca2 6692 /* store SHA1 hash of NAME */
wolfSSL 0:1239e9b70ca2 6693 CYASSL_LOCAL int GetNameHash(const byte* source, word32* idx, byte* hash,
wolfSSL 0:1239e9b70ca2 6694 int maxIdx)
wolfSSL 0:1239e9b70ca2 6695 {
wolfSSL 0:1239e9b70ca2 6696 Sha sha;
wolfSSL 0:1239e9b70ca2 6697 int length; /* length of all distinguished names */
wolfSSL 0:1239e9b70ca2 6698 int ret = 0;
wolfSSL 0:1239e9b70ca2 6699 word32 dummy;
wolfSSL 0:1239e9b70ca2 6700
wolfSSL 0:1239e9b70ca2 6701 CYASSL_ENTER("GetNameHash");
wolfSSL 0:1239e9b70ca2 6702
wolfSSL 0:1239e9b70ca2 6703 if (source[*idx] == ASN_OBJECT_ID) {
wolfSSL 0:1239e9b70ca2 6704 CYASSL_MSG("Trying optional prefix...");
wolfSSL 0:1239e9b70ca2 6705
wolfSSL 0:1239e9b70ca2 6706 if (GetLength(source, idx, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 6707 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6708
wolfSSL 0:1239e9b70ca2 6709 *idx += length;
wolfSSL 0:1239e9b70ca2 6710 CYASSL_MSG("Got optional prefix");
wolfSSL 0:1239e9b70ca2 6711 }
wolfSSL 0:1239e9b70ca2 6712
wolfSSL 0:1239e9b70ca2 6713 /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be
wolfSSL 0:1239e9b70ca2 6714 * calculated over the entire DER encoding of the Name field, including
wolfSSL 0:1239e9b70ca2 6715 * the tag and length. */
wolfSSL 0:1239e9b70ca2 6716 dummy = *idx;
wolfSSL 0:1239e9b70ca2 6717 if (GetSequence(source, idx, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 6718 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6719
wolfSSL 0:1239e9b70ca2 6720 ret = InitSha(&sha);
wolfSSL 0:1239e9b70ca2 6721 if (ret != 0)
wolfSSL 0:1239e9b70ca2 6722 return ret;
wolfSSL 0:1239e9b70ca2 6723 ShaUpdate(&sha, source + dummy, length + *idx - dummy);
wolfSSL 0:1239e9b70ca2 6724 ShaFinal(&sha, hash);
wolfSSL 0:1239e9b70ca2 6725
wolfSSL 0:1239e9b70ca2 6726 *idx += length;
wolfSSL 0:1239e9b70ca2 6727
wolfSSL 0:1239e9b70ca2 6728 return 0;
wolfSSL 0:1239e9b70ca2 6729 }
wolfSSL 0:1239e9b70ca2 6730
wolfSSL 0:1239e9b70ca2 6731
wolfSSL 0:1239e9b70ca2 6732 #ifdef HAVE_CRL
wolfSSL 0:1239e9b70ca2 6733
wolfSSL 0:1239e9b70ca2 6734 /* initialize decoded CRL */
wolfSSL 0:1239e9b70ca2 6735 void InitDecodedCRL(DecodedCRL* dcrl)
wolfSSL 0:1239e9b70ca2 6736 {
wolfSSL 0:1239e9b70ca2 6737 CYASSL_MSG("InitDecodedCRL");
wolfSSL 0:1239e9b70ca2 6738
wolfSSL 0:1239e9b70ca2 6739 dcrl->certBegin = 0;
wolfSSL 0:1239e9b70ca2 6740 dcrl->sigIndex = 0;
wolfSSL 0:1239e9b70ca2 6741 dcrl->sigLength = 0;
wolfSSL 0:1239e9b70ca2 6742 dcrl->signatureOID = 0;
wolfSSL 0:1239e9b70ca2 6743 dcrl->certs = NULL;
wolfSSL 0:1239e9b70ca2 6744 dcrl->totalCerts = 0;
wolfSSL 0:1239e9b70ca2 6745 }
wolfSSL 0:1239e9b70ca2 6746
wolfSSL 0:1239e9b70ca2 6747
wolfSSL 0:1239e9b70ca2 6748 /* free decoded CRL resources */
wolfSSL 0:1239e9b70ca2 6749 void FreeDecodedCRL(DecodedCRL* dcrl)
wolfSSL 0:1239e9b70ca2 6750 {
wolfSSL 0:1239e9b70ca2 6751 RevokedCert* tmp = dcrl->certs;
wolfSSL 0:1239e9b70ca2 6752
wolfSSL 0:1239e9b70ca2 6753 CYASSL_MSG("FreeDecodedCRL");
wolfSSL 0:1239e9b70ca2 6754
wolfSSL 0:1239e9b70ca2 6755 while(tmp) {
wolfSSL 0:1239e9b70ca2 6756 RevokedCert* next = tmp->next;
wolfSSL 0:1239e9b70ca2 6757 XFREE(tmp, NULL, DYNAMIC_TYPE_REVOKED);
wolfSSL 0:1239e9b70ca2 6758 tmp = next;
wolfSSL 0:1239e9b70ca2 6759 }
wolfSSL 0:1239e9b70ca2 6760 }
wolfSSL 0:1239e9b70ca2 6761
wolfSSL 0:1239e9b70ca2 6762
wolfSSL 0:1239e9b70ca2 6763 /* Get Revoked Cert list, 0 on success */
wolfSSL 0:1239e9b70ca2 6764 static int GetRevoked(const byte* buff, word32* idx, DecodedCRL* dcrl,
wolfSSL 0:1239e9b70ca2 6765 int maxIdx)
wolfSSL 0:1239e9b70ca2 6766 {
wolfSSL 0:1239e9b70ca2 6767 int len;
wolfSSL 0:1239e9b70ca2 6768 word32 end;
wolfSSL 0:1239e9b70ca2 6769 byte b;
wolfSSL 0:1239e9b70ca2 6770 RevokedCert* rc;
wolfSSL 0:1239e9b70ca2 6771
wolfSSL 0:1239e9b70ca2 6772 CYASSL_ENTER("GetRevoked");
wolfSSL 0:1239e9b70ca2 6773
wolfSSL 0:1239e9b70ca2 6774 if (GetSequence(buff, idx, &len, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 6775 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6776
wolfSSL 0:1239e9b70ca2 6777 end = *idx + len;
wolfSSL 0:1239e9b70ca2 6778
wolfSSL 0:1239e9b70ca2 6779 /* get serial number */
wolfSSL 0:1239e9b70ca2 6780 b = buff[*idx];
wolfSSL 0:1239e9b70ca2 6781 *idx += 1;
wolfSSL 0:1239e9b70ca2 6782
wolfSSL 0:1239e9b70ca2 6783 if (b != ASN_INTEGER) {
wolfSSL 0:1239e9b70ca2 6784 CYASSL_MSG("Expecting Integer");
wolfSSL 0:1239e9b70ca2 6785 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6786 }
wolfSSL 0:1239e9b70ca2 6787
wolfSSL 0:1239e9b70ca2 6788 if (GetLength(buff, idx, &len, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 6789 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6790
wolfSSL 0:1239e9b70ca2 6791 if (len > EXTERNAL_SERIAL_SIZE) {
wolfSSL 0:1239e9b70ca2 6792 CYASSL_MSG("Serial Size too big");
wolfSSL 0:1239e9b70ca2 6793 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6794 }
wolfSSL 0:1239e9b70ca2 6795
wolfSSL 0:1239e9b70ca2 6796 rc = (RevokedCert*)XMALLOC(sizeof(RevokedCert), NULL, DYNAMIC_TYPE_CRL);
wolfSSL 0:1239e9b70ca2 6797 if (rc == NULL) {
wolfSSL 0:1239e9b70ca2 6798 CYASSL_MSG("Alloc Revoked Cert failed");
wolfSSL 0:1239e9b70ca2 6799 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 6800 }
wolfSSL 0:1239e9b70ca2 6801
wolfSSL 0:1239e9b70ca2 6802 XMEMCPY(rc->serialNumber, &buff[*idx], len);
wolfSSL 0:1239e9b70ca2 6803 rc->serialSz = len;
wolfSSL 0:1239e9b70ca2 6804
wolfSSL 0:1239e9b70ca2 6805 /* add to list */
wolfSSL 0:1239e9b70ca2 6806 rc->next = dcrl->certs;
wolfSSL 0:1239e9b70ca2 6807 dcrl->certs = rc;
wolfSSL 0:1239e9b70ca2 6808 dcrl->totalCerts++;
wolfSSL 0:1239e9b70ca2 6809
wolfSSL 0:1239e9b70ca2 6810 *idx += len;
wolfSSL 0:1239e9b70ca2 6811
wolfSSL 0:1239e9b70ca2 6812 /* get date */
wolfSSL 0:1239e9b70ca2 6813 b = buff[*idx];
wolfSSL 0:1239e9b70ca2 6814 *idx += 1;
wolfSSL 0:1239e9b70ca2 6815
wolfSSL 0:1239e9b70ca2 6816 if (b != ASN_UTC_TIME && b != ASN_GENERALIZED_TIME) {
wolfSSL 0:1239e9b70ca2 6817 CYASSL_MSG("Expecting Date");
wolfSSL 0:1239e9b70ca2 6818 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6819 }
wolfSSL 0:1239e9b70ca2 6820
wolfSSL 0:1239e9b70ca2 6821 if (GetLength(buff, idx, &len, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 6822 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6823
wolfSSL 0:1239e9b70ca2 6824 /* skip for now */
wolfSSL 0:1239e9b70ca2 6825 *idx += len;
wolfSSL 0:1239e9b70ca2 6826
wolfSSL 0:1239e9b70ca2 6827 if (*idx != end) /* skip extensions */
wolfSSL 0:1239e9b70ca2 6828 *idx = end;
wolfSSL 0:1239e9b70ca2 6829
wolfSSL 0:1239e9b70ca2 6830 return 0;
wolfSSL 0:1239e9b70ca2 6831 }
wolfSSL 0:1239e9b70ca2 6832
wolfSSL 0:1239e9b70ca2 6833
wolfSSL 0:1239e9b70ca2 6834 /* Get CRL Signature, 0 on success */
wolfSSL 0:1239e9b70ca2 6835 static int GetCRL_Signature(const byte* source, word32* idx, DecodedCRL* dcrl,
wolfSSL 0:1239e9b70ca2 6836 int maxIdx)
wolfSSL 0:1239e9b70ca2 6837 {
wolfSSL 0:1239e9b70ca2 6838 int length;
wolfSSL 0:1239e9b70ca2 6839 byte b;
wolfSSL 0:1239e9b70ca2 6840
wolfSSL 0:1239e9b70ca2 6841 CYASSL_ENTER("GetCRL_Signature");
wolfSSL 0:1239e9b70ca2 6842
wolfSSL 0:1239e9b70ca2 6843 b = source[*idx];
wolfSSL 0:1239e9b70ca2 6844 *idx += 1;
wolfSSL 0:1239e9b70ca2 6845 if (b != ASN_BIT_STRING)
wolfSSL 0:1239e9b70ca2 6846 return ASN_BITSTR_E;
wolfSSL 0:1239e9b70ca2 6847
wolfSSL 0:1239e9b70ca2 6848 if (GetLength(source, idx, &length, maxIdx) < 0)
wolfSSL 0:1239e9b70ca2 6849 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6850
wolfSSL 0:1239e9b70ca2 6851 dcrl->sigLength = length;
wolfSSL 0:1239e9b70ca2 6852
wolfSSL 0:1239e9b70ca2 6853 b = source[*idx];
wolfSSL 0:1239e9b70ca2 6854 *idx += 1;
wolfSSL 0:1239e9b70ca2 6855 if (b != 0x00)
wolfSSL 0:1239e9b70ca2 6856 return ASN_EXPECT_0_E;
wolfSSL 0:1239e9b70ca2 6857
wolfSSL 0:1239e9b70ca2 6858 dcrl->sigLength--;
wolfSSL 0:1239e9b70ca2 6859 dcrl->signature = (byte*)&source[*idx];
wolfSSL 0:1239e9b70ca2 6860
wolfSSL 0:1239e9b70ca2 6861 *idx += dcrl->sigLength;
wolfSSL 0:1239e9b70ca2 6862
wolfSSL 0:1239e9b70ca2 6863 return 0;
wolfSSL 0:1239e9b70ca2 6864 }
wolfSSL 0:1239e9b70ca2 6865
wolfSSL 0:1239e9b70ca2 6866
wolfSSL 0:1239e9b70ca2 6867 /* prase crl buffer into decoded state, 0 on success */
wolfSSL 0:1239e9b70ca2 6868 int ParseCRL(DecodedCRL* dcrl, const byte* buff, word32 sz, void* cm)
wolfSSL 0:1239e9b70ca2 6869 {
wolfSSL 0:1239e9b70ca2 6870 int version, len;
wolfSSL 0:1239e9b70ca2 6871 word32 oid, idx = 0;
wolfSSL 0:1239e9b70ca2 6872 Signer* ca = NULL;
wolfSSL 0:1239e9b70ca2 6873
wolfSSL 0:1239e9b70ca2 6874 CYASSL_MSG("ParseCRL");
wolfSSL 0:1239e9b70ca2 6875
wolfSSL 0:1239e9b70ca2 6876 /* raw crl hash */
wolfSSL 0:1239e9b70ca2 6877 /* hash here if needed for optimized comparisons
wolfSSL 0:1239e9b70ca2 6878 * Sha sha;
wolfSSL 0:1239e9b70ca2 6879 * InitSha(&sha);
wolfSSL 0:1239e9b70ca2 6880 * ShaUpdate(&sha, buff, sz);
wolfSSL 0:1239e9b70ca2 6881 * ShaFinal(&sha, dcrl->crlHash); */
wolfSSL 0:1239e9b70ca2 6882
wolfSSL 0:1239e9b70ca2 6883 if (GetSequence(buff, &idx, &len, sz) < 0)
wolfSSL 0:1239e9b70ca2 6884 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6885
wolfSSL 0:1239e9b70ca2 6886 dcrl->certBegin = idx;
wolfSSL 0:1239e9b70ca2 6887
wolfSSL 0:1239e9b70ca2 6888 if (GetSequence(buff, &idx, &len, sz) < 0)
wolfSSL 0:1239e9b70ca2 6889 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6890 dcrl->sigIndex = len + idx;
wolfSSL 0:1239e9b70ca2 6891
wolfSSL 0:1239e9b70ca2 6892 /* may have version */
wolfSSL 0:1239e9b70ca2 6893 if (buff[idx] == ASN_INTEGER) {
wolfSSL 0:1239e9b70ca2 6894 if (GetMyVersion(buff, &idx, &version) < 0)
wolfSSL 0:1239e9b70ca2 6895 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6896 }
wolfSSL 0:1239e9b70ca2 6897
wolfSSL 0:1239e9b70ca2 6898 if (GetAlgoId(buff, &idx, &oid, sz) < 0)
wolfSSL 0:1239e9b70ca2 6899 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6900
wolfSSL 0:1239e9b70ca2 6901 if (GetNameHash(buff, &idx, dcrl->issuerHash, sz) < 0)
wolfSSL 0:1239e9b70ca2 6902 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6903
wolfSSL 0:1239e9b70ca2 6904 if (GetBasicDate(buff, &idx, dcrl->lastDate, &dcrl->lastDateFormat, sz) < 0)
wolfSSL 0:1239e9b70ca2 6905 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6906
wolfSSL 0:1239e9b70ca2 6907 if (GetBasicDate(buff, &idx, dcrl->nextDate, &dcrl->nextDateFormat, sz) < 0)
wolfSSL 0:1239e9b70ca2 6908 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6909
wolfSSL 0:1239e9b70ca2 6910 if (!XVALIDATE_DATE(dcrl->nextDate, dcrl->nextDateFormat, AFTER)) {
wolfSSL 0:1239e9b70ca2 6911 CYASSL_MSG("CRL after date is no longer valid");
wolfSSL 0:1239e9b70ca2 6912 return ASN_AFTER_DATE_E;
wolfSSL 0:1239e9b70ca2 6913 }
wolfSSL 0:1239e9b70ca2 6914
wolfSSL 0:1239e9b70ca2 6915 if (idx != dcrl->sigIndex && buff[idx] != CRL_EXTENSIONS) {
wolfSSL 0:1239e9b70ca2 6916 if (GetSequence(buff, &idx, &len, sz) < 0)
wolfSSL 0:1239e9b70ca2 6917 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6918
wolfSSL 0:1239e9b70ca2 6919 len += idx;
wolfSSL 0:1239e9b70ca2 6920
wolfSSL 0:1239e9b70ca2 6921 while (idx < (word32)len) {
wolfSSL 0:1239e9b70ca2 6922 if (GetRevoked(buff, &idx, dcrl, sz) < 0)
wolfSSL 0:1239e9b70ca2 6923 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6924 }
wolfSSL 0:1239e9b70ca2 6925 }
wolfSSL 0:1239e9b70ca2 6926
wolfSSL 0:1239e9b70ca2 6927 if (idx != dcrl->sigIndex)
wolfSSL 0:1239e9b70ca2 6928 idx = dcrl->sigIndex; /* skip extensions */
wolfSSL 0:1239e9b70ca2 6929
wolfSSL 0:1239e9b70ca2 6930 if (GetAlgoId(buff, &idx, &dcrl->signatureOID, sz) < 0)
wolfSSL 0:1239e9b70ca2 6931 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6932
wolfSSL 0:1239e9b70ca2 6933 if (GetCRL_Signature(buff, &idx, dcrl, sz) < 0)
wolfSSL 0:1239e9b70ca2 6934 return ASN_PARSE_E;
wolfSSL 0:1239e9b70ca2 6935
wolfSSL 0:1239e9b70ca2 6936 /* openssl doesn't add skid by default for CRLs cause firefox chokes
wolfSSL 0:1239e9b70ca2 6937 we're not assuming it's available yet */
wolfSSL 0:1239e9b70ca2 6938 #if !defined(NO_SKID) && defined(CRL_SKID_READY)
wolfSSL 0:1239e9b70ca2 6939 if (dcrl->extAuthKeyIdSet)
wolfSSL 0:1239e9b70ca2 6940 ca = GetCA(cm, dcrl->extAuthKeyId);
wolfSSL 0:1239e9b70ca2 6941 if (ca == NULL)
wolfSSL 0:1239e9b70ca2 6942 ca = GetCAByName(cm, dcrl->issuerHash);
wolfSSL 0:1239e9b70ca2 6943 #else /* NO_SKID */
wolfSSL 0:1239e9b70ca2 6944 ca = GetCA(cm, dcrl->issuerHash);
wolfSSL 0:1239e9b70ca2 6945 #endif /* NO_SKID */
wolfSSL 0:1239e9b70ca2 6946 CYASSL_MSG("About to verify CRL signature");
wolfSSL 0:1239e9b70ca2 6947
wolfSSL 0:1239e9b70ca2 6948 if (ca) {
wolfSSL 0:1239e9b70ca2 6949 CYASSL_MSG("Found CRL issuer CA");
wolfSSL 0:1239e9b70ca2 6950 /* try to confirm/verify signature */
wolfSSL 0:1239e9b70ca2 6951 #ifndef IGNORE_KEY_EXTENSIONS
wolfSSL 0:1239e9b70ca2 6952 if ((ca->keyUsage & KEYUSE_CRL_SIGN) == 0) {
wolfSSL 0:1239e9b70ca2 6953 CYASSL_MSG("CA cannot sign CRLs");
wolfSSL 0:1239e9b70ca2 6954 return ASN_CRL_NO_SIGNER_E;
wolfSSL 0:1239e9b70ca2 6955 }
wolfSSL 0:1239e9b70ca2 6956 #endif /* IGNORE_KEY_EXTENSIONS */
wolfSSL 0:1239e9b70ca2 6957 if (!ConfirmSignature(buff + dcrl->certBegin,
wolfSSL 0:1239e9b70ca2 6958 dcrl->sigIndex - dcrl->certBegin,
wolfSSL 0:1239e9b70ca2 6959 ca->publicKey, ca->pubKeySize, ca->keyOID,
wolfSSL 0:1239e9b70ca2 6960 dcrl->signature, dcrl->sigLength, dcrl->signatureOID, NULL)) {
wolfSSL 0:1239e9b70ca2 6961 CYASSL_MSG("CRL Confirm signature failed");
wolfSSL 0:1239e9b70ca2 6962 return ASN_CRL_CONFIRM_E;
wolfSSL 0:1239e9b70ca2 6963 }
wolfSSL 0:1239e9b70ca2 6964 }
wolfSSL 0:1239e9b70ca2 6965 else {
wolfSSL 0:1239e9b70ca2 6966 CYASSL_MSG("Did NOT find CRL issuer CA");
wolfSSL 0:1239e9b70ca2 6967 return ASN_CRL_NO_SIGNER_E;
wolfSSL 0:1239e9b70ca2 6968 }
wolfSSL 0:1239e9b70ca2 6969
wolfSSL 0:1239e9b70ca2 6970 return 0;
wolfSSL 0:1239e9b70ca2 6971 }
wolfSSL 0:1239e9b70ca2 6972
wolfSSL 0:1239e9b70ca2 6973 #endif /* HAVE_CRL */
wolfSSL 0:1239e9b70ca2 6974 #endif
wolfSSL 0:1239e9b70ca2 6975
wolfSSL 0:1239e9b70ca2 6976 #ifdef CYASSL_SEP
wolfSSL 0:1239e9b70ca2 6977
wolfSSL 0:1239e9b70ca2 6978
wolfSSL 0:1239e9b70ca2 6979
wolfSSL 0:1239e9b70ca2 6980 #endif /* CYASSL_SEP */
wolfSSL 0:1239e9b70ca2 6981
wolfSSL 0:1239e9b70ca2 6982