SSL/TLS Library
CyaSSL is SSL/TLS library for embedded systems.
ctaocrypt/src/asn.c@0:9d17e4342598, 2014-04-20 (annotated)
- Committer:
- wolfSSL
- Date:
- Sun Apr 20 12:40:57 2014 +0000
- Revision:
- 0:9d17e4342598
CyaSSL SSL/TLS Library 2.9.4;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 0:9d17e4342598 | 1 | /* asn.c |
wolfSSL | 0:9d17e4342598 | 2 | * |
wolfSSL | 0:9d17e4342598 | 3 | * Copyright (C) 2006-2013 wolfSSL Inc. |
wolfSSL | 0:9d17e4342598 | 4 | * |
wolfSSL | 0:9d17e4342598 | 5 | * This file is part of CyaSSL. |
wolfSSL | 0:9d17e4342598 | 6 | * |
wolfSSL | 0:9d17e4342598 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
wolfSSL | 0:9d17e4342598 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 0:9d17e4342598 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 0:9d17e4342598 | 10 | * (at your option) any later version. |
wolfSSL | 0:9d17e4342598 | 11 | * |
wolfSSL | 0:9d17e4342598 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
wolfSSL | 0:9d17e4342598 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 0:9d17e4342598 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 0:9d17e4342598 | 15 | * GNU General Public License for more details. |
wolfSSL | 0:9d17e4342598 | 16 | * |
wolfSSL | 0:9d17e4342598 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 0:9d17e4342598 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 0:9d17e4342598 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
wolfSSL | 0:9d17e4342598 | 20 | */ |
wolfSSL | 0:9d17e4342598 | 21 | |
wolfSSL | 0:9d17e4342598 | 22 | #ifdef HAVE_CONFIG_H |
wolfSSL | 0:9d17e4342598 | 23 | #include <config.h> |
wolfSSL | 0:9d17e4342598 | 24 | #endif |
wolfSSL | 0:9d17e4342598 | 25 | |
wolfSSL | 0:9d17e4342598 | 26 | #include <cyassl/ctaocrypt/settings.h> |
wolfSSL | 0:9d17e4342598 | 27 | |
wolfSSL | 0:9d17e4342598 | 28 | #ifndef NO_ASN |
wolfSSL | 0:9d17e4342598 | 29 | |
wolfSSL | 0:9d17e4342598 | 30 | #ifdef HAVE_RTP_SYS |
wolfSSL | 0:9d17e4342598 | 31 | #include "os.h" /* dc_rtc_api needs */ |
wolfSSL | 0:9d17e4342598 | 32 | #include "dc_rtc_api.h" /* to get current time */ |
wolfSSL | 0:9d17e4342598 | 33 | #endif |
wolfSSL | 0:9d17e4342598 | 34 | |
wolfSSL | 0:9d17e4342598 | 35 | #include <cyassl/ctaocrypt/integer.h> |
wolfSSL | 0:9d17e4342598 | 36 | #include <cyassl/ctaocrypt/asn.h> |
wolfSSL | 0:9d17e4342598 | 37 | #include <cyassl/ctaocrypt/coding.h> |
wolfSSL | 0:9d17e4342598 | 38 | #include <cyassl/ctaocrypt/sha.h> |
wolfSSL | 0:9d17e4342598 | 39 | #include <cyassl/ctaocrypt/md5.h> |
wolfSSL | 0:9d17e4342598 | 40 | #include <cyassl/ctaocrypt/md2.h> |
wolfSSL | 0:9d17e4342598 | 41 | #include <cyassl/ctaocrypt/error-crypt.h> |
wolfSSL | 0:9d17e4342598 | 42 | #include <cyassl/ctaocrypt/pwdbased.h> |
wolfSSL | 0:9d17e4342598 | 43 | #include <cyassl/ctaocrypt/des3.h> |
wolfSSL | 0:9d17e4342598 | 44 | #include <cyassl/ctaocrypt/sha256.h> |
wolfSSL | 0:9d17e4342598 | 45 | #include <cyassl/ctaocrypt/sha512.h> |
wolfSSL | 0:9d17e4342598 | 46 | #include <cyassl/ctaocrypt/logging.h> |
wolfSSL | 0:9d17e4342598 | 47 | |
wolfSSL | 0:9d17e4342598 | 48 | #include <cyassl/ctaocrypt/random.h> |
wolfSSL | 0:9d17e4342598 | 49 | |
wolfSSL | 0:9d17e4342598 | 50 | |
wolfSSL | 0:9d17e4342598 | 51 | #ifndef NO_RC4 |
wolfSSL | 0:9d17e4342598 | 52 | #include <cyassl/ctaocrypt/arc4.h> |
wolfSSL | 0:9d17e4342598 | 53 | #endif |
wolfSSL | 0:9d17e4342598 | 54 | |
wolfSSL | 0:9d17e4342598 | 55 | #ifdef HAVE_NTRU |
wolfSSL | 0:9d17e4342598 | 56 | #include "crypto_ntru.h" |
wolfSSL | 0:9d17e4342598 | 57 | #endif |
wolfSSL | 0:9d17e4342598 | 58 | |
wolfSSL | 0:9d17e4342598 | 59 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 60 | #include <cyassl/ctaocrypt/ecc.h> |
wolfSSL | 0:9d17e4342598 | 61 | #endif |
wolfSSL | 0:9d17e4342598 | 62 | |
wolfSSL | 0:9d17e4342598 | 63 | #ifdef CYASSL_DEBUG_ENCODING |
wolfSSL | 0:9d17e4342598 | 64 | #ifdef FREESCALE_MQX |
wolfSSL | 0:9d17e4342598 | 65 | #include <fio.h> |
wolfSSL | 0:9d17e4342598 | 66 | #else |
wolfSSL | 0:9d17e4342598 | 67 | #include <stdio.h> |
wolfSSL | 0:9d17e4342598 | 68 | #endif |
wolfSSL | 0:9d17e4342598 | 69 | #endif |
wolfSSL | 0:9d17e4342598 | 70 | |
wolfSSL | 0:9d17e4342598 | 71 | #ifdef _MSC_VER |
wolfSSL | 0:9d17e4342598 | 72 | /* 4996 warning to use MS extensions e.g., strcpy_s instead of XSTRNCPY */ |
wolfSSL | 0:9d17e4342598 | 73 | #pragma warning(disable: 4996) |
wolfSSL | 0:9d17e4342598 | 74 | #endif |
wolfSSL | 0:9d17e4342598 | 75 | |
wolfSSL | 0:9d17e4342598 | 76 | |
wolfSSL | 0:9d17e4342598 | 77 | #ifndef TRUE |
wolfSSL | 0:9d17e4342598 | 78 | #define TRUE 1 |
wolfSSL | 0:9d17e4342598 | 79 | #endif |
wolfSSL | 0:9d17e4342598 | 80 | #ifndef FALSE |
wolfSSL | 0:9d17e4342598 | 81 | #define FALSE 0 |
wolfSSL | 0:9d17e4342598 | 82 | #endif |
wolfSSL | 0:9d17e4342598 | 83 | |
wolfSSL | 0:9d17e4342598 | 84 | |
wolfSSL | 0:9d17e4342598 | 85 | #ifdef HAVE_RTP_SYS |
wolfSSL | 0:9d17e4342598 | 86 | /* uses parital <time.h> structures */ |
wolfSSL | 0:9d17e4342598 | 87 | #define XTIME(tl) (0) |
wolfSSL | 0:9d17e4342598 | 88 | #define XGMTIME(c) my_gmtime((c)) |
wolfSSL | 0:9d17e4342598 | 89 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) |
wolfSSL | 0:9d17e4342598 | 90 | #elif defined(MICRIUM) |
wolfSSL | 0:9d17e4342598 | 91 | #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) |
wolfSSL | 0:9d17e4342598 | 92 | #define XVALIDATE_DATE(d,f,t) NetSecure_ValidateDateHandler((d),(f),(t)) |
wolfSSL | 0:9d17e4342598 | 93 | #else |
wolfSSL | 0:9d17e4342598 | 94 | #define XVALIDATE_DATE(d, f, t) (0) |
wolfSSL | 0:9d17e4342598 | 95 | #endif |
wolfSSL | 0:9d17e4342598 | 96 | #define NO_TIME_H |
wolfSSL | 0:9d17e4342598 | 97 | /* since Micrium not defining XTIME or XGMTIME, CERT_GEN not available */ |
wolfSSL | 0:9d17e4342598 | 98 | #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP) |
wolfSSL | 0:9d17e4342598 | 99 | #include <time.h> |
wolfSSL | 0:9d17e4342598 | 100 | #define XTIME(t1) pic32_time((t1)) |
wolfSSL | 0:9d17e4342598 | 101 | #define XGMTIME(c) gmtime((c)) |
wolfSSL | 0:9d17e4342598 | 102 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) |
wolfSSL | 0:9d17e4342598 | 103 | #elif defined(FREESCALE_MQX) |
wolfSSL | 0:9d17e4342598 | 104 | #include <time.h> |
wolfSSL | 0:9d17e4342598 | 105 | #define XTIME(t1) mqx_time((t1)) |
wolfSSL | 0:9d17e4342598 | 106 | #define XGMTIME(c) gmtime((c)) |
wolfSSL | 0:9d17e4342598 | 107 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) |
wolfSSL | 0:9d17e4342598 | 108 | #elif defined(CYASSL_MDK_ARM) |
wolfSSL | 0:9d17e4342598 | 109 | #if defined(CYASSL_MDK5) |
wolfSSL | 0:9d17e4342598 | 110 | #include "cmsis_os.h" |
wolfSSL | 0:9d17e4342598 | 111 | #else |
wolfSSL | 0:9d17e4342598 | 112 | #include <rtl.h> |
wolfSSL | 0:9d17e4342598 | 113 | #endif |
wolfSSL | 0:9d17e4342598 | 114 | #undef RNG |
wolfSSL | 0:9d17e4342598 | 115 | #include "cyassl_MDK_ARM.h" |
wolfSSL | 0:9d17e4342598 | 116 | #undef RNG |
wolfSSL | 0:9d17e4342598 | 117 | #define RNG CyaSSL_RNG /*for avoiding name conflict in "stm32f2xx.h" */ |
wolfSSL | 0:9d17e4342598 | 118 | #define XTIME(tl) (0) |
wolfSSL | 0:9d17e4342598 | 119 | #define XGMTIME(c) Cyassl_MDK_gmtime((c)) |
wolfSSL | 0:9d17e4342598 | 120 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) |
wolfSSL | 0:9d17e4342598 | 121 | #elif defined(USER_TIME) |
wolfSSL | 0:9d17e4342598 | 122 | /* user time, and gmtime compatible functions, there is a gmtime |
wolfSSL | 0:9d17e4342598 | 123 | implementation here that WINCE uses, so really just need some ticks |
wolfSSL | 0:9d17e4342598 | 124 | since the EPOCH |
wolfSSL | 0:9d17e4342598 | 125 | */ |
wolfSSL | 0:9d17e4342598 | 126 | |
wolfSSL | 0:9d17e4342598 | 127 | struct tm { |
wolfSSL | 0:9d17e4342598 | 128 | int tm_sec; /* seconds after the minute [0-60] */ |
wolfSSL | 0:9d17e4342598 | 129 | int tm_min; /* minutes after the hour [0-59] */ |
wolfSSL | 0:9d17e4342598 | 130 | int tm_hour; /* hours since midnight [0-23] */ |
wolfSSL | 0:9d17e4342598 | 131 | int tm_mday; /* day of the month [1-31] */ |
wolfSSL | 0:9d17e4342598 | 132 | int tm_mon; /* months since January [0-11] */ |
wolfSSL | 0:9d17e4342598 | 133 | int tm_year; /* years since 1900 */ |
wolfSSL | 0:9d17e4342598 | 134 | int tm_wday; /* days since Sunday [0-6] */ |
wolfSSL | 0:9d17e4342598 | 135 | int tm_yday; /* days since January 1 [0-365] */ |
wolfSSL | 0:9d17e4342598 | 136 | int tm_isdst; /* Daylight Savings Time flag */ |
wolfSSL | 0:9d17e4342598 | 137 | long tm_gmtoff; /* offset from CUT in seconds */ |
wolfSSL | 0:9d17e4342598 | 138 | char *tm_zone; /* timezone abbreviation */ |
wolfSSL | 0:9d17e4342598 | 139 | }; |
wolfSSL | 0:9d17e4342598 | 140 | typedef long time_t; |
wolfSSL | 0:9d17e4342598 | 141 | |
wolfSSL | 0:9d17e4342598 | 142 | /* forward declaration */ |
wolfSSL | 0:9d17e4342598 | 143 | struct tm* gmtime(const time_t* timer); |
wolfSSL | 0:9d17e4342598 | 144 | extern time_t XTIME(time_t * timer); |
wolfSSL | 0:9d17e4342598 | 145 | |
wolfSSL | 0:9d17e4342598 | 146 | #define XGMTIME(c) gmtime((c)) |
wolfSSL | 0:9d17e4342598 | 147 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) |
wolfSSL | 0:9d17e4342598 | 148 | |
wolfSSL | 0:9d17e4342598 | 149 | #ifdef STACK_TRAP |
wolfSSL | 0:9d17e4342598 | 150 | /* for stack trap tracking, don't call os gmtime on OS X/linux, |
wolfSSL | 0:9d17e4342598 | 151 | uses a lot of stack spce */ |
wolfSSL | 0:9d17e4342598 | 152 | extern time_t time(time_t * timer); |
wolfSSL | 0:9d17e4342598 | 153 | #define XTIME(tl) time((tl)) |
wolfSSL | 0:9d17e4342598 | 154 | #endif /* STACK_TRAP */ |
wolfSSL | 0:9d17e4342598 | 155 | |
wolfSSL | 0:9d17e4342598 | 156 | #else |
wolfSSL | 0:9d17e4342598 | 157 | /* default */ |
wolfSSL | 0:9d17e4342598 | 158 | /* uses complete <time.h> facility */ |
wolfSSL | 0:9d17e4342598 | 159 | #include <time.h> |
wolfSSL | 0:9d17e4342598 | 160 | #define XTIME(tl) time((tl)) |
wolfSSL | 0:9d17e4342598 | 161 | #define XGMTIME(c) gmtime((c)) |
wolfSSL | 0:9d17e4342598 | 162 | #define XVALIDATE_DATE(d, f, t) ValidateDate((d), (f), (t)) |
wolfSSL | 0:9d17e4342598 | 163 | #endif |
wolfSSL | 0:9d17e4342598 | 164 | |
wolfSSL | 0:9d17e4342598 | 165 | |
wolfSSL | 0:9d17e4342598 | 166 | #ifdef _WIN32_WCE |
wolfSSL | 0:9d17e4342598 | 167 | /* no time() or gmtime() even though in time.h header?? */ |
wolfSSL | 0:9d17e4342598 | 168 | |
wolfSSL | 0:9d17e4342598 | 169 | #include <windows.h> |
wolfSSL | 0:9d17e4342598 | 170 | |
wolfSSL | 0:9d17e4342598 | 171 | |
wolfSSL | 0:9d17e4342598 | 172 | time_t time(time_t* timer) |
wolfSSL | 0:9d17e4342598 | 173 | { |
wolfSSL | 0:9d17e4342598 | 174 | SYSTEMTIME sysTime; |
wolfSSL | 0:9d17e4342598 | 175 | FILETIME fTime; |
wolfSSL | 0:9d17e4342598 | 176 | ULARGE_INTEGER intTime; |
wolfSSL | 0:9d17e4342598 | 177 | time_t localTime; |
wolfSSL | 0:9d17e4342598 | 178 | |
wolfSSL | 0:9d17e4342598 | 179 | if (timer == NULL) |
wolfSSL | 0:9d17e4342598 | 180 | timer = &localTime; |
wolfSSL | 0:9d17e4342598 | 181 | |
wolfSSL | 0:9d17e4342598 | 182 | GetSystemTime(&sysTime); |
wolfSSL | 0:9d17e4342598 | 183 | SystemTimeToFileTime(&sysTime, &fTime); |
wolfSSL | 0:9d17e4342598 | 184 | |
wolfSSL | 0:9d17e4342598 | 185 | XMEMCPY(&intTime, &fTime, sizeof(FILETIME)); |
wolfSSL | 0:9d17e4342598 | 186 | /* subtract EPOCH */ |
wolfSSL | 0:9d17e4342598 | 187 | intTime.QuadPart -= 0x19db1ded53e8000; |
wolfSSL | 0:9d17e4342598 | 188 | /* to secs */ |
wolfSSL | 0:9d17e4342598 | 189 | intTime.QuadPart /= 10000000; |
wolfSSL | 0:9d17e4342598 | 190 | *timer = (time_t)intTime.QuadPart; |
wolfSSL | 0:9d17e4342598 | 191 | |
wolfSSL | 0:9d17e4342598 | 192 | return *timer; |
wolfSSL | 0:9d17e4342598 | 193 | } |
wolfSSL | 0:9d17e4342598 | 194 | |
wolfSSL | 0:9d17e4342598 | 195 | #endif /* _WIN32_WCE */ |
wolfSSL | 0:9d17e4342598 | 196 | #if defined( _WIN32_WCE ) || defined( USER_TIME ) |
wolfSSL | 0:9d17e4342598 | 197 | |
wolfSSL | 0:9d17e4342598 | 198 | struct tm* gmtime(const time_t* timer) |
wolfSSL | 0:9d17e4342598 | 199 | { |
wolfSSL | 0:9d17e4342598 | 200 | #define YEAR0 1900 |
wolfSSL | 0:9d17e4342598 | 201 | #define EPOCH_YEAR 1970 |
wolfSSL | 0:9d17e4342598 | 202 | #define SECS_DAY (24L * 60L * 60L) |
wolfSSL | 0:9d17e4342598 | 203 | #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400))) |
wolfSSL | 0:9d17e4342598 | 204 | #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365) |
wolfSSL | 0:9d17e4342598 | 205 | |
wolfSSL | 0:9d17e4342598 | 206 | static const int _ytab[2][12] = |
wolfSSL | 0:9d17e4342598 | 207 | { |
wolfSSL | 0:9d17e4342598 | 208 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, |
wolfSSL | 0:9d17e4342598 | 209 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
wolfSSL | 0:9d17e4342598 | 210 | }; |
wolfSSL | 0:9d17e4342598 | 211 | |
wolfSSL | 0:9d17e4342598 | 212 | static struct tm st_time; |
wolfSSL | 0:9d17e4342598 | 213 | struct tm* ret = &st_time; |
wolfSSL | 0:9d17e4342598 | 214 | time_t secs = *timer; |
wolfSSL | 0:9d17e4342598 | 215 | unsigned long dayclock, dayno; |
wolfSSL | 0:9d17e4342598 | 216 | int year = EPOCH_YEAR; |
wolfSSL | 0:9d17e4342598 | 217 | |
wolfSSL | 0:9d17e4342598 | 218 | dayclock = (unsigned long)secs % SECS_DAY; |
wolfSSL | 0:9d17e4342598 | 219 | dayno = (unsigned long)secs / SECS_DAY; |
wolfSSL | 0:9d17e4342598 | 220 | |
wolfSSL | 0:9d17e4342598 | 221 | ret->tm_sec = (int) dayclock % 60; |
wolfSSL | 0:9d17e4342598 | 222 | ret->tm_min = (int)(dayclock % 3600) / 60; |
wolfSSL | 0:9d17e4342598 | 223 | ret->tm_hour = (int) dayclock / 3600; |
wolfSSL | 0:9d17e4342598 | 224 | ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */ |
wolfSSL | 0:9d17e4342598 | 225 | |
wolfSSL | 0:9d17e4342598 | 226 | while(dayno >= (unsigned long)YEARSIZE(year)) { |
wolfSSL | 0:9d17e4342598 | 227 | dayno -= YEARSIZE(year); |
wolfSSL | 0:9d17e4342598 | 228 | year++; |
wolfSSL | 0:9d17e4342598 | 229 | } |
wolfSSL | 0:9d17e4342598 | 230 | |
wolfSSL | 0:9d17e4342598 | 231 | ret->tm_year = year - YEAR0; |
wolfSSL | 0:9d17e4342598 | 232 | ret->tm_yday = (int)dayno; |
wolfSSL | 0:9d17e4342598 | 233 | ret->tm_mon = 0; |
wolfSSL | 0:9d17e4342598 | 234 | |
wolfSSL | 0:9d17e4342598 | 235 | while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) { |
wolfSSL | 0:9d17e4342598 | 236 | dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon]; |
wolfSSL | 0:9d17e4342598 | 237 | ret->tm_mon++; |
wolfSSL | 0:9d17e4342598 | 238 | } |
wolfSSL | 0:9d17e4342598 | 239 | |
wolfSSL | 0:9d17e4342598 | 240 | ret->tm_mday = (int)++dayno; |
wolfSSL | 0:9d17e4342598 | 241 | ret->tm_isdst = 0; |
wolfSSL | 0:9d17e4342598 | 242 | |
wolfSSL | 0:9d17e4342598 | 243 | return ret; |
wolfSSL | 0:9d17e4342598 | 244 | } |
wolfSSL | 0:9d17e4342598 | 245 | |
wolfSSL | 0:9d17e4342598 | 246 | #endif /* _WIN32_WCE || USER_TIME */ |
wolfSSL | 0:9d17e4342598 | 247 | |
wolfSSL | 0:9d17e4342598 | 248 | |
wolfSSL | 0:9d17e4342598 | 249 | #ifdef HAVE_RTP_SYS |
wolfSSL | 0:9d17e4342598 | 250 | |
wolfSSL | 0:9d17e4342598 | 251 | #define YEAR0 1900 |
wolfSSL | 0:9d17e4342598 | 252 | |
wolfSSL | 0:9d17e4342598 | 253 | struct tm* my_gmtime(const time_t* timer) /* has a gmtime() but hangs */ |
wolfSSL | 0:9d17e4342598 | 254 | { |
wolfSSL | 0:9d17e4342598 | 255 | static struct tm st_time; |
wolfSSL | 0:9d17e4342598 | 256 | struct tm* ret = &st_time; |
wolfSSL | 0:9d17e4342598 | 257 | |
wolfSSL | 0:9d17e4342598 | 258 | DC_RTC_CALENDAR cal; |
wolfSSL | 0:9d17e4342598 | 259 | dc_rtc_time_get(&cal, TRUE); |
wolfSSL | 0:9d17e4342598 | 260 | |
wolfSSL | 0:9d17e4342598 | 261 | ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */ |
wolfSSL | 0:9d17e4342598 | 262 | ret->tm_mon = cal.month - 1; /* gm starts at 0 */ |
wolfSSL | 0:9d17e4342598 | 263 | ret->tm_mday = cal.day; |
wolfSSL | 0:9d17e4342598 | 264 | ret->tm_hour = cal.hour; |
wolfSSL | 0:9d17e4342598 | 265 | ret->tm_min = cal.minute; |
wolfSSL | 0:9d17e4342598 | 266 | ret->tm_sec = cal.second; |
wolfSSL | 0:9d17e4342598 | 267 | |
wolfSSL | 0:9d17e4342598 | 268 | return ret; |
wolfSSL | 0:9d17e4342598 | 269 | } |
wolfSSL | 0:9d17e4342598 | 270 | |
wolfSSL | 0:9d17e4342598 | 271 | #endif /* HAVE_RTP_SYS */ |
wolfSSL | 0:9d17e4342598 | 272 | |
wolfSSL | 0:9d17e4342598 | 273 | |
wolfSSL | 0:9d17e4342598 | 274 | #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP) |
wolfSSL | 0:9d17e4342598 | 275 | |
wolfSSL | 0:9d17e4342598 | 276 | /* |
wolfSSL | 0:9d17e4342598 | 277 | * time() is just a stub in Microchip libraries. We need our own |
wolfSSL | 0:9d17e4342598 | 278 | * implementation. Use SNTP client to get seconds since epoch. |
wolfSSL | 0:9d17e4342598 | 279 | */ |
wolfSSL | 0:9d17e4342598 | 280 | time_t pic32_time(time_t* timer) |
wolfSSL | 0:9d17e4342598 | 281 | { |
wolfSSL | 0:9d17e4342598 | 282 | #ifdef MICROCHIP_TCPIP_V5 |
wolfSSL | 0:9d17e4342598 | 283 | DWORD sec = 0; |
wolfSSL | 0:9d17e4342598 | 284 | #else |
wolfSSL | 0:9d17e4342598 | 285 | uint32_t sec = 0; |
wolfSSL | 0:9d17e4342598 | 286 | #endif |
wolfSSL | 0:9d17e4342598 | 287 | time_t localTime; |
wolfSSL | 0:9d17e4342598 | 288 | |
wolfSSL | 0:9d17e4342598 | 289 | if (timer == NULL) |
wolfSSL | 0:9d17e4342598 | 290 | timer = &localTime; |
wolfSSL | 0:9d17e4342598 | 291 | |
wolfSSL | 0:9d17e4342598 | 292 | #ifdef MICROCHIP_MPLAB_HARMONY |
wolfSSL | 0:9d17e4342598 | 293 | sec = TCPIP_SNTP_UTCSecondsGet(); |
wolfSSL | 0:9d17e4342598 | 294 | #else |
wolfSSL | 0:9d17e4342598 | 295 | sec = SNTPGetUTCSeconds(); |
wolfSSL | 0:9d17e4342598 | 296 | #endif |
wolfSSL | 0:9d17e4342598 | 297 | *timer = (time_t) sec; |
wolfSSL | 0:9d17e4342598 | 298 | |
wolfSSL | 0:9d17e4342598 | 299 | return *timer; |
wolfSSL | 0:9d17e4342598 | 300 | } |
wolfSSL | 0:9d17e4342598 | 301 | |
wolfSSL | 0:9d17e4342598 | 302 | #endif /* MICROCHIP_TCPIP */ |
wolfSSL | 0:9d17e4342598 | 303 | |
wolfSSL | 0:9d17e4342598 | 304 | |
wolfSSL | 0:9d17e4342598 | 305 | #ifdef FREESCALE_MQX |
wolfSSL | 0:9d17e4342598 | 306 | |
wolfSSL | 0:9d17e4342598 | 307 | time_t mqx_time(time_t* timer) |
wolfSSL | 0:9d17e4342598 | 308 | { |
wolfSSL | 0:9d17e4342598 | 309 | time_t localTime; |
wolfSSL | 0:9d17e4342598 | 310 | TIME_STRUCT time_s; |
wolfSSL | 0:9d17e4342598 | 311 | |
wolfSSL | 0:9d17e4342598 | 312 | if (timer == NULL) |
wolfSSL | 0:9d17e4342598 | 313 | timer = &localTime; |
wolfSSL | 0:9d17e4342598 | 314 | |
wolfSSL | 0:9d17e4342598 | 315 | _time_get(&time_s); |
wolfSSL | 0:9d17e4342598 | 316 | *timer = (time_t) time_s.SECONDS; |
wolfSSL | 0:9d17e4342598 | 317 | |
wolfSSL | 0:9d17e4342598 | 318 | return *timer; |
wolfSSL | 0:9d17e4342598 | 319 | } |
wolfSSL | 0:9d17e4342598 | 320 | |
wolfSSL | 0:9d17e4342598 | 321 | #endif /* FREESCALE_MQX */ |
wolfSSL | 0:9d17e4342598 | 322 | |
wolfSSL | 0:9d17e4342598 | 323 | |
wolfSSL | 0:9d17e4342598 | 324 | static INLINE word32 btoi(byte b) |
wolfSSL | 0:9d17e4342598 | 325 | { |
wolfSSL | 0:9d17e4342598 | 326 | return b - 0x30; |
wolfSSL | 0:9d17e4342598 | 327 | } |
wolfSSL | 0:9d17e4342598 | 328 | |
wolfSSL | 0:9d17e4342598 | 329 | |
wolfSSL | 0:9d17e4342598 | 330 | /* two byte date/time, add to value */ |
wolfSSL | 0:9d17e4342598 | 331 | static INLINE void GetTime(int* value, const byte* date, int* idx) |
wolfSSL | 0:9d17e4342598 | 332 | { |
wolfSSL | 0:9d17e4342598 | 333 | int i = *idx; |
wolfSSL | 0:9d17e4342598 | 334 | |
wolfSSL | 0:9d17e4342598 | 335 | *value += btoi(date[i++]) * 10; |
wolfSSL | 0:9d17e4342598 | 336 | *value += btoi(date[i++]); |
wolfSSL | 0:9d17e4342598 | 337 | |
wolfSSL | 0:9d17e4342598 | 338 | *idx = i; |
wolfSSL | 0:9d17e4342598 | 339 | } |
wolfSSL | 0:9d17e4342598 | 340 | |
wolfSSL | 0:9d17e4342598 | 341 | |
wolfSSL | 0:9d17e4342598 | 342 | #if defined(MICRIUM) |
wolfSSL | 0:9d17e4342598 | 343 | |
wolfSSL | 0:9d17e4342598 | 344 | CPU_INT32S NetSecure_ValidateDateHandler(CPU_INT08U *date, CPU_INT08U format, |
wolfSSL | 0:9d17e4342598 | 345 | CPU_INT08U dateType) |
wolfSSL | 0:9d17e4342598 | 346 | { |
wolfSSL | 0:9d17e4342598 | 347 | CPU_BOOLEAN rtn_code; |
wolfSSL | 0:9d17e4342598 | 348 | CPU_INT32S i; |
wolfSSL | 0:9d17e4342598 | 349 | CPU_INT32S val; |
wolfSSL | 0:9d17e4342598 | 350 | CPU_INT16U year; |
wolfSSL | 0:9d17e4342598 | 351 | CPU_INT08U month; |
wolfSSL | 0:9d17e4342598 | 352 | CPU_INT16U day; |
wolfSSL | 0:9d17e4342598 | 353 | CPU_INT08U hour; |
wolfSSL | 0:9d17e4342598 | 354 | CPU_INT08U min; |
wolfSSL | 0:9d17e4342598 | 355 | CPU_INT08U sec; |
wolfSSL | 0:9d17e4342598 | 356 | |
wolfSSL | 0:9d17e4342598 | 357 | i = 0; |
wolfSSL | 0:9d17e4342598 | 358 | year = 0u; |
wolfSSL | 0:9d17e4342598 | 359 | |
wolfSSL | 0:9d17e4342598 | 360 | if (format == ASN_UTC_TIME) { |
wolfSSL | 0:9d17e4342598 | 361 | if (btoi(date[0]) >= 5) |
wolfSSL | 0:9d17e4342598 | 362 | year = 1900; |
wolfSSL | 0:9d17e4342598 | 363 | else |
wolfSSL | 0:9d17e4342598 | 364 | year = 2000; |
wolfSSL | 0:9d17e4342598 | 365 | } |
wolfSSL | 0:9d17e4342598 | 366 | else { /* format == GENERALIZED_TIME */ |
wolfSSL | 0:9d17e4342598 | 367 | year += btoi(date[i++]) * 1000; |
wolfSSL | 0:9d17e4342598 | 368 | year += btoi(date[i++]) * 100; |
wolfSSL | 0:9d17e4342598 | 369 | } |
wolfSSL | 0:9d17e4342598 | 370 | |
wolfSSL | 0:9d17e4342598 | 371 | val = year; |
wolfSSL | 0:9d17e4342598 | 372 | GetTime(&val, date, &i); |
wolfSSL | 0:9d17e4342598 | 373 | year = (CPU_INT16U)val; |
wolfSSL | 0:9d17e4342598 | 374 | |
wolfSSL | 0:9d17e4342598 | 375 | val = 0; |
wolfSSL | 0:9d17e4342598 | 376 | GetTime(&val, date, &i); |
wolfSSL | 0:9d17e4342598 | 377 | month = (CPU_INT08U)val; |
wolfSSL | 0:9d17e4342598 | 378 | |
wolfSSL | 0:9d17e4342598 | 379 | val = 0; |
wolfSSL | 0:9d17e4342598 | 380 | GetTime(&val, date, &i); |
wolfSSL | 0:9d17e4342598 | 381 | day = (CPU_INT16U)val; |
wolfSSL | 0:9d17e4342598 | 382 | |
wolfSSL | 0:9d17e4342598 | 383 | val = 0; |
wolfSSL | 0:9d17e4342598 | 384 | GetTime(&val, date, &i); |
wolfSSL | 0:9d17e4342598 | 385 | hour = (CPU_INT08U)val; |
wolfSSL | 0:9d17e4342598 | 386 | |
wolfSSL | 0:9d17e4342598 | 387 | val = 0; |
wolfSSL | 0:9d17e4342598 | 388 | GetTime(&val, date, &i); |
wolfSSL | 0:9d17e4342598 | 389 | min = (CPU_INT08U)val; |
wolfSSL | 0:9d17e4342598 | 390 | |
wolfSSL | 0:9d17e4342598 | 391 | val = 0; |
wolfSSL | 0:9d17e4342598 | 392 | GetTime(&val, date, &i); |
wolfSSL | 0:9d17e4342598 | 393 | sec = (CPU_INT08U)val; |
wolfSSL | 0:9d17e4342598 | 394 | |
wolfSSL | 0:9d17e4342598 | 395 | return NetSecure_ValidateDate(year, month, day, hour, min, sec, dateType); |
wolfSSL | 0:9d17e4342598 | 396 | } |
wolfSSL | 0:9d17e4342598 | 397 | |
wolfSSL | 0:9d17e4342598 | 398 | #endif /* MICRIUM */ |
wolfSSL | 0:9d17e4342598 | 399 | |
wolfSSL | 0:9d17e4342598 | 400 | |
wolfSSL | 0:9d17e4342598 | 401 | CYASSL_LOCAL int GetLength(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 0:9d17e4342598 | 402 | word32 maxIdx) |
wolfSSL | 0:9d17e4342598 | 403 | { |
wolfSSL | 0:9d17e4342598 | 404 | int length = 0; |
wolfSSL | 0:9d17e4342598 | 405 | word32 i = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 406 | byte b; |
wolfSSL | 0:9d17e4342598 | 407 | |
wolfSSL | 0:9d17e4342598 | 408 | if ( (i+1) > maxIdx) { /* for first read */ |
wolfSSL | 0:9d17e4342598 | 409 | CYASSL_MSG("GetLength bad index on input"); |
wolfSSL | 0:9d17e4342598 | 410 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 411 | } |
wolfSSL | 0:9d17e4342598 | 412 | |
wolfSSL | 0:9d17e4342598 | 413 | b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 414 | if (b >= ASN_LONG_LENGTH) { |
wolfSSL | 0:9d17e4342598 | 415 | word32 bytes = b & 0x7F; |
wolfSSL | 0:9d17e4342598 | 416 | |
wolfSSL | 0:9d17e4342598 | 417 | if ( (i+bytes) > maxIdx) { /* for reading bytes */ |
wolfSSL | 0:9d17e4342598 | 418 | CYASSL_MSG("GetLength bad long length"); |
wolfSSL | 0:9d17e4342598 | 419 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 420 | } |
wolfSSL | 0:9d17e4342598 | 421 | |
wolfSSL | 0:9d17e4342598 | 422 | while (bytes--) { |
wolfSSL | 0:9d17e4342598 | 423 | b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 424 | length = (length << 8) | b; |
wolfSSL | 0:9d17e4342598 | 425 | } |
wolfSSL | 0:9d17e4342598 | 426 | } |
wolfSSL | 0:9d17e4342598 | 427 | else |
wolfSSL | 0:9d17e4342598 | 428 | length = b; |
wolfSSL | 0:9d17e4342598 | 429 | |
wolfSSL | 0:9d17e4342598 | 430 | if ( (i+length) > maxIdx) { /* for user of length */ |
wolfSSL | 0:9d17e4342598 | 431 | CYASSL_MSG("GetLength value exceeds buffer length"); |
wolfSSL | 0:9d17e4342598 | 432 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 433 | } |
wolfSSL | 0:9d17e4342598 | 434 | |
wolfSSL | 0:9d17e4342598 | 435 | *inOutIdx = i; |
wolfSSL | 0:9d17e4342598 | 436 | *len = length; |
wolfSSL | 0:9d17e4342598 | 437 | |
wolfSSL | 0:9d17e4342598 | 438 | return length; |
wolfSSL | 0:9d17e4342598 | 439 | } |
wolfSSL | 0:9d17e4342598 | 440 | |
wolfSSL | 0:9d17e4342598 | 441 | |
wolfSSL | 0:9d17e4342598 | 442 | CYASSL_LOCAL int GetSequence(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 0:9d17e4342598 | 443 | word32 maxIdx) |
wolfSSL | 0:9d17e4342598 | 444 | { |
wolfSSL | 0:9d17e4342598 | 445 | int length = -1; |
wolfSSL | 0:9d17e4342598 | 446 | word32 idx = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 447 | |
wolfSSL | 0:9d17e4342598 | 448 | if (input[idx++] != (ASN_SEQUENCE | ASN_CONSTRUCTED) || |
wolfSSL | 0:9d17e4342598 | 449 | GetLength(input, &idx, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 450 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 451 | |
wolfSSL | 0:9d17e4342598 | 452 | *len = length; |
wolfSSL | 0:9d17e4342598 | 453 | *inOutIdx = idx; |
wolfSSL | 0:9d17e4342598 | 454 | |
wolfSSL | 0:9d17e4342598 | 455 | return length; |
wolfSSL | 0:9d17e4342598 | 456 | } |
wolfSSL | 0:9d17e4342598 | 457 | |
wolfSSL | 0:9d17e4342598 | 458 | |
wolfSSL | 0:9d17e4342598 | 459 | CYASSL_LOCAL int GetSet(const byte* input, word32* inOutIdx, int* len, |
wolfSSL | 0:9d17e4342598 | 460 | word32 maxIdx) |
wolfSSL | 0:9d17e4342598 | 461 | { |
wolfSSL | 0:9d17e4342598 | 462 | int length = -1; |
wolfSSL | 0:9d17e4342598 | 463 | word32 idx = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 464 | |
wolfSSL | 0:9d17e4342598 | 465 | if (input[idx++] != (ASN_SET | ASN_CONSTRUCTED) || |
wolfSSL | 0:9d17e4342598 | 466 | GetLength(input, &idx, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 467 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 468 | |
wolfSSL | 0:9d17e4342598 | 469 | *len = length; |
wolfSSL | 0:9d17e4342598 | 470 | *inOutIdx = idx; |
wolfSSL | 0:9d17e4342598 | 471 | |
wolfSSL | 0:9d17e4342598 | 472 | return length; |
wolfSSL | 0:9d17e4342598 | 473 | } |
wolfSSL | 0:9d17e4342598 | 474 | |
wolfSSL | 0:9d17e4342598 | 475 | |
wolfSSL | 0:9d17e4342598 | 476 | /* winodws header clash for WinCE using GetVersion */ |
wolfSSL | 0:9d17e4342598 | 477 | CYASSL_LOCAL int GetMyVersion(const byte* input, word32* inOutIdx, int* version) |
wolfSSL | 0:9d17e4342598 | 478 | { |
wolfSSL | 0:9d17e4342598 | 479 | word32 idx = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 480 | |
wolfSSL | 0:9d17e4342598 | 481 | CYASSL_ENTER("GetMyVersion"); |
wolfSSL | 0:9d17e4342598 | 482 | |
wolfSSL | 0:9d17e4342598 | 483 | if (input[idx++] != ASN_INTEGER) |
wolfSSL | 0:9d17e4342598 | 484 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 485 | |
wolfSSL | 0:9d17e4342598 | 486 | if (input[idx++] != 0x01) |
wolfSSL | 0:9d17e4342598 | 487 | return ASN_VERSION_E; |
wolfSSL | 0:9d17e4342598 | 488 | |
wolfSSL | 0:9d17e4342598 | 489 | *version = input[idx++]; |
wolfSSL | 0:9d17e4342598 | 490 | *inOutIdx = idx; |
wolfSSL | 0:9d17e4342598 | 491 | |
wolfSSL | 0:9d17e4342598 | 492 | return *version; |
wolfSSL | 0:9d17e4342598 | 493 | } |
wolfSSL | 0:9d17e4342598 | 494 | |
wolfSSL | 0:9d17e4342598 | 495 | |
wolfSSL | 0:9d17e4342598 | 496 | #ifndef NO_PWDBASED |
wolfSSL | 0:9d17e4342598 | 497 | /* Get small count integer, 32 bits or less */ |
wolfSSL | 0:9d17e4342598 | 498 | static int GetShortInt(const byte* input, word32* inOutIdx, int* number) |
wolfSSL | 0:9d17e4342598 | 499 | { |
wolfSSL | 0:9d17e4342598 | 500 | word32 idx = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 501 | word32 len; |
wolfSSL | 0:9d17e4342598 | 502 | |
wolfSSL | 0:9d17e4342598 | 503 | *number = 0; |
wolfSSL | 0:9d17e4342598 | 504 | |
wolfSSL | 0:9d17e4342598 | 505 | if (input[idx++] != ASN_INTEGER) |
wolfSSL | 0:9d17e4342598 | 506 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 507 | |
wolfSSL | 0:9d17e4342598 | 508 | len = input[idx++]; |
wolfSSL | 0:9d17e4342598 | 509 | if (len > 4) |
wolfSSL | 0:9d17e4342598 | 510 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 511 | |
wolfSSL | 0:9d17e4342598 | 512 | while (len--) { |
wolfSSL | 0:9d17e4342598 | 513 | *number = *number << 8 | input[idx++]; |
wolfSSL | 0:9d17e4342598 | 514 | } |
wolfSSL | 0:9d17e4342598 | 515 | |
wolfSSL | 0:9d17e4342598 | 516 | *inOutIdx = idx; |
wolfSSL | 0:9d17e4342598 | 517 | |
wolfSSL | 0:9d17e4342598 | 518 | return *number; |
wolfSSL | 0:9d17e4342598 | 519 | } |
wolfSSL | 0:9d17e4342598 | 520 | #endif /* !NO_PWDBASED */ |
wolfSSL | 0:9d17e4342598 | 521 | |
wolfSSL | 0:9d17e4342598 | 522 | |
wolfSSL | 0:9d17e4342598 | 523 | /* May not have one, not an error */ |
wolfSSL | 0:9d17e4342598 | 524 | static int GetExplicitVersion(const byte* input, word32* inOutIdx, int* version) |
wolfSSL | 0:9d17e4342598 | 525 | { |
wolfSSL | 0:9d17e4342598 | 526 | word32 idx = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 527 | |
wolfSSL | 0:9d17e4342598 | 528 | CYASSL_ENTER("GetExplicitVersion"); |
wolfSSL | 0:9d17e4342598 | 529 | if (input[idx++] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) { |
wolfSSL | 0:9d17e4342598 | 530 | *inOutIdx = ++idx; /* eat header */ |
wolfSSL | 0:9d17e4342598 | 531 | return GetMyVersion(input, inOutIdx, version); |
wolfSSL | 0:9d17e4342598 | 532 | } |
wolfSSL | 0:9d17e4342598 | 533 | |
wolfSSL | 0:9d17e4342598 | 534 | /* go back as is */ |
wolfSSL | 0:9d17e4342598 | 535 | *version = 0; |
wolfSSL | 0:9d17e4342598 | 536 | |
wolfSSL | 0:9d17e4342598 | 537 | return 0; |
wolfSSL | 0:9d17e4342598 | 538 | } |
wolfSSL | 0:9d17e4342598 | 539 | |
wolfSSL | 0:9d17e4342598 | 540 | |
wolfSSL | 0:9d17e4342598 | 541 | CYASSL_LOCAL int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx, |
wolfSSL | 0:9d17e4342598 | 542 | word32 maxIdx) |
wolfSSL | 0:9d17e4342598 | 543 | { |
wolfSSL | 0:9d17e4342598 | 544 | word32 i = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 545 | byte b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 546 | int length; |
wolfSSL | 0:9d17e4342598 | 547 | |
wolfSSL | 0:9d17e4342598 | 548 | if (b != ASN_INTEGER) |
wolfSSL | 0:9d17e4342598 | 549 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 550 | |
wolfSSL | 0:9d17e4342598 | 551 | if (GetLength(input, &i, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 552 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 553 | |
wolfSSL | 0:9d17e4342598 | 554 | if ( (b = input[i++]) == 0x00) |
wolfSSL | 0:9d17e4342598 | 555 | length--; |
wolfSSL | 0:9d17e4342598 | 556 | else |
wolfSSL | 0:9d17e4342598 | 557 | i--; |
wolfSSL | 0:9d17e4342598 | 558 | |
wolfSSL | 0:9d17e4342598 | 559 | if (mp_init(mpi) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 560 | return MP_INIT_E; |
wolfSSL | 0:9d17e4342598 | 561 | |
wolfSSL | 0:9d17e4342598 | 562 | if (mp_read_unsigned_bin(mpi, (byte*)input + i, length) != 0) { |
wolfSSL | 0:9d17e4342598 | 563 | mp_clear(mpi); |
wolfSSL | 0:9d17e4342598 | 564 | return ASN_GETINT_E; |
wolfSSL | 0:9d17e4342598 | 565 | } |
wolfSSL | 0:9d17e4342598 | 566 | |
wolfSSL | 0:9d17e4342598 | 567 | *inOutIdx = i + length; |
wolfSSL | 0:9d17e4342598 | 568 | return 0; |
wolfSSL | 0:9d17e4342598 | 569 | } |
wolfSSL | 0:9d17e4342598 | 570 | |
wolfSSL | 0:9d17e4342598 | 571 | |
wolfSSL | 0:9d17e4342598 | 572 | static int GetObjectId(const byte* input, word32* inOutIdx, word32* oid, |
wolfSSL | 0:9d17e4342598 | 573 | word32 maxIdx) |
wolfSSL | 0:9d17e4342598 | 574 | { |
wolfSSL | 0:9d17e4342598 | 575 | int length; |
wolfSSL | 0:9d17e4342598 | 576 | word32 i = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 577 | byte b; |
wolfSSL | 0:9d17e4342598 | 578 | *oid = 0; |
wolfSSL | 0:9d17e4342598 | 579 | |
wolfSSL | 0:9d17e4342598 | 580 | b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 581 | if (b != ASN_OBJECT_ID) |
wolfSSL | 0:9d17e4342598 | 582 | return ASN_OBJECT_ID_E; |
wolfSSL | 0:9d17e4342598 | 583 | |
wolfSSL | 0:9d17e4342598 | 584 | if (GetLength(input, &i, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 585 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 586 | |
wolfSSL | 0:9d17e4342598 | 587 | while(length--) |
wolfSSL | 0:9d17e4342598 | 588 | *oid += input[i++]; |
wolfSSL | 0:9d17e4342598 | 589 | /* just sum it up for now */ |
wolfSSL | 0:9d17e4342598 | 590 | |
wolfSSL | 0:9d17e4342598 | 591 | *inOutIdx = i; |
wolfSSL | 0:9d17e4342598 | 592 | |
wolfSSL | 0:9d17e4342598 | 593 | return 0; |
wolfSSL | 0:9d17e4342598 | 594 | } |
wolfSSL | 0:9d17e4342598 | 595 | |
wolfSSL | 0:9d17e4342598 | 596 | |
wolfSSL | 0:9d17e4342598 | 597 | CYASSL_LOCAL int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid, |
wolfSSL | 0:9d17e4342598 | 598 | word32 maxIdx) |
wolfSSL | 0:9d17e4342598 | 599 | { |
wolfSSL | 0:9d17e4342598 | 600 | int length; |
wolfSSL | 0:9d17e4342598 | 601 | word32 i = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 602 | byte b; |
wolfSSL | 0:9d17e4342598 | 603 | *oid = 0; |
wolfSSL | 0:9d17e4342598 | 604 | |
wolfSSL | 0:9d17e4342598 | 605 | CYASSL_ENTER("GetAlgoId"); |
wolfSSL | 0:9d17e4342598 | 606 | |
wolfSSL | 0:9d17e4342598 | 607 | if (GetSequence(input, &i, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 608 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 609 | |
wolfSSL | 0:9d17e4342598 | 610 | b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 611 | if (b != ASN_OBJECT_ID) |
wolfSSL | 0:9d17e4342598 | 612 | return ASN_OBJECT_ID_E; |
wolfSSL | 0:9d17e4342598 | 613 | |
wolfSSL | 0:9d17e4342598 | 614 | if (GetLength(input, &i, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 615 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 616 | |
wolfSSL | 0:9d17e4342598 | 617 | while(length--) { |
wolfSSL | 0:9d17e4342598 | 618 | /* odd HC08 compiler behavior here when input[i++] */ |
wolfSSL | 0:9d17e4342598 | 619 | *oid += input[i]; |
wolfSSL | 0:9d17e4342598 | 620 | i++; |
wolfSSL | 0:9d17e4342598 | 621 | } |
wolfSSL | 0:9d17e4342598 | 622 | /* just sum it up for now */ |
wolfSSL | 0:9d17e4342598 | 623 | |
wolfSSL | 0:9d17e4342598 | 624 | /* could have NULL tag and 0 terminator, but may not */ |
wolfSSL | 0:9d17e4342598 | 625 | b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 626 | |
wolfSSL | 0:9d17e4342598 | 627 | if (b == ASN_TAG_NULL) { |
wolfSSL | 0:9d17e4342598 | 628 | b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 629 | if (b != 0) |
wolfSSL | 0:9d17e4342598 | 630 | return ASN_EXPECT_0_E; |
wolfSSL | 0:9d17e4342598 | 631 | } |
wolfSSL | 0:9d17e4342598 | 632 | else |
wolfSSL | 0:9d17e4342598 | 633 | /* go back, didn't have it */ |
wolfSSL | 0:9d17e4342598 | 634 | i--; |
wolfSSL | 0:9d17e4342598 | 635 | |
wolfSSL | 0:9d17e4342598 | 636 | *inOutIdx = i; |
wolfSSL | 0:9d17e4342598 | 637 | |
wolfSSL | 0:9d17e4342598 | 638 | return 0; |
wolfSSL | 0:9d17e4342598 | 639 | } |
wolfSSL | 0:9d17e4342598 | 640 | |
wolfSSL | 0:9d17e4342598 | 641 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 642 | |
wolfSSL | 0:9d17e4342598 | 643 | |
wolfSSL | 0:9d17e4342598 | 644 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 645 | |
wolfSSL | 0:9d17e4342598 | 646 | static int GetCaviumInt(byte** buff, word16* buffSz, const byte* input, |
wolfSSL | 0:9d17e4342598 | 647 | word32* inOutIdx, word32 maxIdx, void* heap) |
wolfSSL | 0:9d17e4342598 | 648 | { |
wolfSSL | 0:9d17e4342598 | 649 | word32 i = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 650 | byte b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 651 | int length; |
wolfSSL | 0:9d17e4342598 | 652 | |
wolfSSL | 0:9d17e4342598 | 653 | if (b != ASN_INTEGER) |
wolfSSL | 0:9d17e4342598 | 654 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 655 | |
wolfSSL | 0:9d17e4342598 | 656 | if (GetLength(input, &i, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 657 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 658 | |
wolfSSL | 0:9d17e4342598 | 659 | if ( (b = input[i++]) == 0x00) |
wolfSSL | 0:9d17e4342598 | 660 | length--; |
wolfSSL | 0:9d17e4342598 | 661 | else |
wolfSSL | 0:9d17e4342598 | 662 | i--; |
wolfSSL | 0:9d17e4342598 | 663 | |
wolfSSL | 0:9d17e4342598 | 664 | *buffSz = (word16)length; |
wolfSSL | 0:9d17e4342598 | 665 | *buff = XMALLOC(*buffSz, heap, DYNAMIC_TYPE_CAVIUM_RSA); |
wolfSSL | 0:9d17e4342598 | 666 | if (*buff == NULL) |
wolfSSL | 0:9d17e4342598 | 667 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 668 | |
wolfSSL | 0:9d17e4342598 | 669 | XMEMCPY(*buff, input + i, *buffSz); |
wolfSSL | 0:9d17e4342598 | 670 | |
wolfSSL | 0:9d17e4342598 | 671 | *inOutIdx = i + length; |
wolfSSL | 0:9d17e4342598 | 672 | return 0; |
wolfSSL | 0:9d17e4342598 | 673 | } |
wolfSSL | 0:9d17e4342598 | 674 | |
wolfSSL | 0:9d17e4342598 | 675 | static int CaviumRsaPrivateKeyDecode(const byte* input, word32* inOutIdx, |
wolfSSL | 0:9d17e4342598 | 676 | RsaKey* key, word32 inSz) |
wolfSSL | 0:9d17e4342598 | 677 | { |
wolfSSL | 0:9d17e4342598 | 678 | int version, length; |
wolfSSL | 0:9d17e4342598 | 679 | void* h = key->heap; |
wolfSSL | 0:9d17e4342598 | 680 | |
wolfSSL | 0:9d17e4342598 | 681 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 682 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 683 | |
wolfSSL | 0:9d17e4342598 | 684 | if (GetMyVersion(input, inOutIdx, &version) < 0) |
wolfSSL | 0:9d17e4342598 | 685 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 686 | |
wolfSSL | 0:9d17e4342598 | 687 | key->type = RSA_PRIVATE; |
wolfSSL | 0:9d17e4342598 | 688 | |
wolfSSL | 0:9d17e4342598 | 689 | if (GetCaviumInt(&key->c_n, &key->c_nSz, input, inOutIdx, inSz, h) < 0 || |
wolfSSL | 0:9d17e4342598 | 690 | GetCaviumInt(&key->c_e, &key->c_eSz, input, inOutIdx, inSz, h) < 0 || |
wolfSSL | 0:9d17e4342598 | 691 | GetCaviumInt(&key->c_d, &key->c_dSz, input, inOutIdx, inSz, h) < 0 || |
wolfSSL | 0:9d17e4342598 | 692 | GetCaviumInt(&key->c_p, &key->c_pSz, input, inOutIdx, inSz, h) < 0 || |
wolfSSL | 0:9d17e4342598 | 693 | GetCaviumInt(&key->c_q, &key->c_qSz, input, inOutIdx, inSz, h) < 0 || |
wolfSSL | 0:9d17e4342598 | 694 | GetCaviumInt(&key->c_dP, &key->c_dP_Sz, input, inOutIdx, inSz, h) < 0 || |
wolfSSL | 0:9d17e4342598 | 695 | GetCaviumInt(&key->c_dQ, &key->c_dQ_Sz, input, inOutIdx, inSz, h) < 0 || |
wolfSSL | 0:9d17e4342598 | 696 | GetCaviumInt(&key->c_u, &key->c_uSz, input, inOutIdx, inSz, h) < 0 ) |
wolfSSL | 0:9d17e4342598 | 697 | return ASN_RSA_KEY_E; |
wolfSSL | 0:9d17e4342598 | 698 | |
wolfSSL | 0:9d17e4342598 | 699 | return 0; |
wolfSSL | 0:9d17e4342598 | 700 | } |
wolfSSL | 0:9d17e4342598 | 701 | |
wolfSSL | 0:9d17e4342598 | 702 | |
wolfSSL | 0:9d17e4342598 | 703 | #endif /* HAVE_CAVIUM */ |
wolfSSL | 0:9d17e4342598 | 704 | |
wolfSSL | 0:9d17e4342598 | 705 | int RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key, |
wolfSSL | 0:9d17e4342598 | 706 | word32 inSz) |
wolfSSL | 0:9d17e4342598 | 707 | { |
wolfSSL | 0:9d17e4342598 | 708 | int version, length; |
wolfSSL | 0:9d17e4342598 | 709 | |
wolfSSL | 0:9d17e4342598 | 710 | #ifdef HAVE_CAVIUM |
wolfSSL | 0:9d17e4342598 | 711 | if (key->magic == CYASSL_RSA_CAVIUM_MAGIC) |
wolfSSL | 0:9d17e4342598 | 712 | return CaviumRsaPrivateKeyDecode(input, inOutIdx, key, inSz); |
wolfSSL | 0:9d17e4342598 | 713 | #endif |
wolfSSL | 0:9d17e4342598 | 714 | |
wolfSSL | 0:9d17e4342598 | 715 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 716 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 717 | |
wolfSSL | 0:9d17e4342598 | 718 | if (GetMyVersion(input, inOutIdx, &version) < 0) |
wolfSSL | 0:9d17e4342598 | 719 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 720 | |
wolfSSL | 0:9d17e4342598 | 721 | key->type = RSA_PRIVATE; |
wolfSSL | 0:9d17e4342598 | 722 | |
wolfSSL | 0:9d17e4342598 | 723 | if (GetInt(&key->n, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 724 | GetInt(&key->e, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 725 | GetInt(&key->d, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 726 | GetInt(&key->p, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 727 | GetInt(&key->q, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 728 | GetInt(&key->dP, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 729 | GetInt(&key->dQ, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 730 | GetInt(&key->u, input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E; |
wolfSSL | 0:9d17e4342598 | 731 | |
wolfSSL | 0:9d17e4342598 | 732 | return 0; |
wolfSSL | 0:9d17e4342598 | 733 | } |
wolfSSL | 0:9d17e4342598 | 734 | |
wolfSSL | 0:9d17e4342598 | 735 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 736 | |
wolfSSL | 0:9d17e4342598 | 737 | /* Remove PKCS8 header, move beginning of traditional to beginning of input */ |
wolfSSL | 0:9d17e4342598 | 738 | int ToTraditional(byte* input, word32 sz) |
wolfSSL | 0:9d17e4342598 | 739 | { |
wolfSSL | 0:9d17e4342598 | 740 | word32 inOutIdx = 0, oid; |
wolfSSL | 0:9d17e4342598 | 741 | int version, length; |
wolfSSL | 0:9d17e4342598 | 742 | |
wolfSSL | 0:9d17e4342598 | 743 | if (GetSequence(input, &inOutIdx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 744 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 745 | |
wolfSSL | 0:9d17e4342598 | 746 | if (GetMyVersion(input, &inOutIdx, &version) < 0) |
wolfSSL | 0:9d17e4342598 | 747 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 748 | |
wolfSSL | 0:9d17e4342598 | 749 | if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 750 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 751 | |
wolfSSL | 0:9d17e4342598 | 752 | if (input[inOutIdx] == ASN_OBJECT_ID) { |
wolfSSL | 0:9d17e4342598 | 753 | /* pkcs8 ecc uses slightly different format */ |
wolfSSL | 0:9d17e4342598 | 754 | inOutIdx++; /* past id */ |
wolfSSL | 0:9d17e4342598 | 755 | if (GetLength(input, &inOutIdx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 756 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 757 | inOutIdx += length; /* over sub id, key input will verify */ |
wolfSSL | 0:9d17e4342598 | 758 | } |
wolfSSL | 0:9d17e4342598 | 759 | |
wolfSSL | 0:9d17e4342598 | 760 | if (input[inOutIdx++] != ASN_OCTET_STRING) |
wolfSSL | 0:9d17e4342598 | 761 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 762 | |
wolfSSL | 0:9d17e4342598 | 763 | if (GetLength(input, &inOutIdx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 764 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 765 | |
wolfSSL | 0:9d17e4342598 | 766 | XMEMMOVE(input, input + inOutIdx, length); |
wolfSSL | 0:9d17e4342598 | 767 | |
wolfSSL | 0:9d17e4342598 | 768 | return length; |
wolfSSL | 0:9d17e4342598 | 769 | } |
wolfSSL | 0:9d17e4342598 | 770 | |
wolfSSL | 0:9d17e4342598 | 771 | |
wolfSSL | 0:9d17e4342598 | 772 | #ifndef NO_PWDBASED |
wolfSSL | 0:9d17e4342598 | 773 | |
wolfSSL | 0:9d17e4342598 | 774 | /* Check To see if PKCS version algo is supported, set id if it is return 0 |
wolfSSL | 0:9d17e4342598 | 775 | < 0 on error */ |
wolfSSL | 0:9d17e4342598 | 776 | static int CheckAlgo(int first, int second, int* id, int* version) |
wolfSSL | 0:9d17e4342598 | 777 | { |
wolfSSL | 0:9d17e4342598 | 778 | *id = ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 779 | *version = PKCS5; /* default */ |
wolfSSL | 0:9d17e4342598 | 780 | |
wolfSSL | 0:9d17e4342598 | 781 | if (first == 1) { |
wolfSSL | 0:9d17e4342598 | 782 | switch (second) { |
wolfSSL | 0:9d17e4342598 | 783 | case 1: |
wolfSSL | 0:9d17e4342598 | 784 | *id = PBE_SHA1_RC4_128; |
wolfSSL | 0:9d17e4342598 | 785 | *version = PKCS12; |
wolfSSL | 0:9d17e4342598 | 786 | return 0; |
wolfSSL | 0:9d17e4342598 | 787 | case 3: |
wolfSSL | 0:9d17e4342598 | 788 | *id = PBE_SHA1_DES3; |
wolfSSL | 0:9d17e4342598 | 789 | *version = PKCS12; |
wolfSSL | 0:9d17e4342598 | 790 | return 0; |
wolfSSL | 0:9d17e4342598 | 791 | default: |
wolfSSL | 0:9d17e4342598 | 792 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 793 | } |
wolfSSL | 0:9d17e4342598 | 794 | } |
wolfSSL | 0:9d17e4342598 | 795 | |
wolfSSL | 0:9d17e4342598 | 796 | if (first != PKCS5) |
wolfSSL | 0:9d17e4342598 | 797 | return ASN_INPUT_E; /* VERSION ERROR */ |
wolfSSL | 0:9d17e4342598 | 798 | |
wolfSSL | 0:9d17e4342598 | 799 | if (second == PBES2) { |
wolfSSL | 0:9d17e4342598 | 800 | *version = PKCS5v2; |
wolfSSL | 0:9d17e4342598 | 801 | return 0; |
wolfSSL | 0:9d17e4342598 | 802 | } |
wolfSSL | 0:9d17e4342598 | 803 | |
wolfSSL | 0:9d17e4342598 | 804 | switch (second) { |
wolfSSL | 0:9d17e4342598 | 805 | case 3: /* see RFC 2898 for ids */ |
wolfSSL | 0:9d17e4342598 | 806 | *id = PBE_MD5_DES; |
wolfSSL | 0:9d17e4342598 | 807 | return 0; |
wolfSSL | 0:9d17e4342598 | 808 | case 10: |
wolfSSL | 0:9d17e4342598 | 809 | *id = PBE_SHA1_DES; |
wolfSSL | 0:9d17e4342598 | 810 | return 0; |
wolfSSL | 0:9d17e4342598 | 811 | default: |
wolfSSL | 0:9d17e4342598 | 812 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 813 | |
wolfSSL | 0:9d17e4342598 | 814 | } |
wolfSSL | 0:9d17e4342598 | 815 | } |
wolfSSL | 0:9d17e4342598 | 816 | |
wolfSSL | 0:9d17e4342598 | 817 | |
wolfSSL | 0:9d17e4342598 | 818 | /* Check To see if PKCS v2 algo is supported, set id if it is return 0 |
wolfSSL | 0:9d17e4342598 | 819 | < 0 on error */ |
wolfSSL | 0:9d17e4342598 | 820 | static int CheckAlgoV2(int oid, int* id) |
wolfSSL | 0:9d17e4342598 | 821 | { |
wolfSSL | 0:9d17e4342598 | 822 | switch (oid) { |
wolfSSL | 0:9d17e4342598 | 823 | case 69: |
wolfSSL | 0:9d17e4342598 | 824 | *id = PBE_SHA1_DES; |
wolfSSL | 0:9d17e4342598 | 825 | return 0; |
wolfSSL | 0:9d17e4342598 | 826 | case 652: |
wolfSSL | 0:9d17e4342598 | 827 | *id = PBE_SHA1_DES3; |
wolfSSL | 0:9d17e4342598 | 828 | return 0; |
wolfSSL | 0:9d17e4342598 | 829 | default: |
wolfSSL | 0:9d17e4342598 | 830 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 831 | |
wolfSSL | 0:9d17e4342598 | 832 | } |
wolfSSL | 0:9d17e4342598 | 833 | } |
wolfSSL | 0:9d17e4342598 | 834 | |
wolfSSL | 0:9d17e4342598 | 835 | |
wolfSSL | 0:9d17e4342598 | 836 | /* Decrypt intput in place from parameters based on id */ |
wolfSSL | 0:9d17e4342598 | 837 | static int DecryptKey(const char* password, int passwordSz, byte* salt, |
wolfSSL | 0:9d17e4342598 | 838 | int saltSz, int iterations, int id, byte* input, |
wolfSSL | 0:9d17e4342598 | 839 | int length, int version, byte* cbcIv) |
wolfSSL | 0:9d17e4342598 | 840 | { |
wolfSSL | 0:9d17e4342598 | 841 | byte key[MAX_KEY_SIZE]; |
wolfSSL | 0:9d17e4342598 | 842 | int typeH; |
wolfSSL | 0:9d17e4342598 | 843 | int derivedLen; |
wolfSSL | 0:9d17e4342598 | 844 | int decryptionType; |
wolfSSL | 0:9d17e4342598 | 845 | int ret = 0; |
wolfSSL | 0:9d17e4342598 | 846 | |
wolfSSL | 0:9d17e4342598 | 847 | switch (id) { |
wolfSSL | 0:9d17e4342598 | 848 | case PBE_MD5_DES: |
wolfSSL | 0:9d17e4342598 | 849 | typeH = MD5; |
wolfSSL | 0:9d17e4342598 | 850 | derivedLen = 16; /* may need iv for v1.5 */ |
wolfSSL | 0:9d17e4342598 | 851 | decryptionType = DES_TYPE; |
wolfSSL | 0:9d17e4342598 | 852 | break; |
wolfSSL | 0:9d17e4342598 | 853 | |
wolfSSL | 0:9d17e4342598 | 854 | case PBE_SHA1_DES: |
wolfSSL | 0:9d17e4342598 | 855 | typeH = SHA; |
wolfSSL | 0:9d17e4342598 | 856 | derivedLen = 16; /* may need iv for v1.5 */ |
wolfSSL | 0:9d17e4342598 | 857 | decryptionType = DES_TYPE; |
wolfSSL | 0:9d17e4342598 | 858 | break; |
wolfSSL | 0:9d17e4342598 | 859 | |
wolfSSL | 0:9d17e4342598 | 860 | case PBE_SHA1_DES3: |
wolfSSL | 0:9d17e4342598 | 861 | typeH = SHA; |
wolfSSL | 0:9d17e4342598 | 862 | derivedLen = 32; /* may need iv for v1.5 */ |
wolfSSL | 0:9d17e4342598 | 863 | decryptionType = DES3_TYPE; |
wolfSSL | 0:9d17e4342598 | 864 | break; |
wolfSSL | 0:9d17e4342598 | 865 | |
wolfSSL | 0:9d17e4342598 | 866 | case PBE_SHA1_RC4_128: |
wolfSSL | 0:9d17e4342598 | 867 | typeH = SHA; |
wolfSSL | 0:9d17e4342598 | 868 | derivedLen = 16; |
wolfSSL | 0:9d17e4342598 | 869 | decryptionType = RC4_TYPE; |
wolfSSL | 0:9d17e4342598 | 870 | break; |
wolfSSL | 0:9d17e4342598 | 871 | |
wolfSSL | 0:9d17e4342598 | 872 | default: |
wolfSSL | 0:9d17e4342598 | 873 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 874 | } |
wolfSSL | 0:9d17e4342598 | 875 | |
wolfSSL | 0:9d17e4342598 | 876 | if (version == PKCS5v2) |
wolfSSL | 0:9d17e4342598 | 877 | ret = PBKDF2(key, (byte*)password, passwordSz, salt, saltSz, iterations, |
wolfSSL | 0:9d17e4342598 | 878 | derivedLen, typeH); |
wolfSSL | 0:9d17e4342598 | 879 | else if (version == PKCS5) |
wolfSSL | 0:9d17e4342598 | 880 | ret = PBKDF1(key, (byte*)password, passwordSz, salt, saltSz, iterations, |
wolfSSL | 0:9d17e4342598 | 881 | derivedLen, typeH); |
wolfSSL | 0:9d17e4342598 | 882 | else if (version == PKCS12) { |
wolfSSL | 0:9d17e4342598 | 883 | int i, idx = 0; |
wolfSSL | 0:9d17e4342598 | 884 | byte unicodePasswd[MAX_UNICODE_SZ]; |
wolfSSL | 0:9d17e4342598 | 885 | |
wolfSSL | 0:9d17e4342598 | 886 | if ( (passwordSz * 2 + 2) > (int)sizeof(unicodePasswd)) |
wolfSSL | 0:9d17e4342598 | 887 | return UNICODE_SIZE_E; |
wolfSSL | 0:9d17e4342598 | 888 | |
wolfSSL | 0:9d17e4342598 | 889 | for (i = 0; i < passwordSz; i++) { |
wolfSSL | 0:9d17e4342598 | 890 | unicodePasswd[idx++] = 0x00; |
wolfSSL | 0:9d17e4342598 | 891 | unicodePasswd[idx++] = (byte)password[i]; |
wolfSSL | 0:9d17e4342598 | 892 | } |
wolfSSL | 0:9d17e4342598 | 893 | /* add trailing NULL */ |
wolfSSL | 0:9d17e4342598 | 894 | unicodePasswd[idx++] = 0x00; |
wolfSSL | 0:9d17e4342598 | 895 | unicodePasswd[idx++] = 0x00; |
wolfSSL | 0:9d17e4342598 | 896 | |
wolfSSL | 0:9d17e4342598 | 897 | ret = PKCS12_PBKDF(key, unicodePasswd, idx, salt, saltSz, |
wolfSSL | 0:9d17e4342598 | 898 | iterations, derivedLen, typeH, 1); |
wolfSSL | 0:9d17e4342598 | 899 | if (decryptionType != RC4_TYPE) |
wolfSSL | 0:9d17e4342598 | 900 | ret += PKCS12_PBKDF(cbcIv, unicodePasswd, idx, salt, saltSz, |
wolfSSL | 0:9d17e4342598 | 901 | iterations, 8, typeH, 2); |
wolfSSL | 0:9d17e4342598 | 902 | } |
wolfSSL | 0:9d17e4342598 | 903 | else |
wolfSSL | 0:9d17e4342598 | 904 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 905 | |
wolfSSL | 0:9d17e4342598 | 906 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 907 | return ret; |
wolfSSL | 0:9d17e4342598 | 908 | |
wolfSSL | 0:9d17e4342598 | 909 | switch (decryptionType) { |
wolfSSL | 0:9d17e4342598 | 910 | #ifndef NO_DES3 |
wolfSSL | 0:9d17e4342598 | 911 | case DES_TYPE: |
wolfSSL | 0:9d17e4342598 | 912 | { |
wolfSSL | 0:9d17e4342598 | 913 | Des dec; |
wolfSSL | 0:9d17e4342598 | 914 | byte* desIv = key + 8; |
wolfSSL | 0:9d17e4342598 | 915 | |
wolfSSL | 0:9d17e4342598 | 916 | if (version == PKCS5v2 || version == PKCS12) |
wolfSSL | 0:9d17e4342598 | 917 | desIv = cbcIv; |
wolfSSL | 0:9d17e4342598 | 918 | |
wolfSSL | 0:9d17e4342598 | 919 | ret = Des_SetKey(&dec, key, desIv, DES_DECRYPTION); |
wolfSSL | 0:9d17e4342598 | 920 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 921 | return ret; |
wolfSSL | 0:9d17e4342598 | 922 | |
wolfSSL | 0:9d17e4342598 | 923 | Des_CbcDecrypt(&dec, input, input, length); |
wolfSSL | 0:9d17e4342598 | 924 | break; |
wolfSSL | 0:9d17e4342598 | 925 | } |
wolfSSL | 0:9d17e4342598 | 926 | |
wolfSSL | 0:9d17e4342598 | 927 | case DES3_TYPE: |
wolfSSL | 0:9d17e4342598 | 928 | { |
wolfSSL | 0:9d17e4342598 | 929 | Des3 dec; |
wolfSSL | 0:9d17e4342598 | 930 | byte* desIv = key + 24; |
wolfSSL | 0:9d17e4342598 | 931 | |
wolfSSL | 0:9d17e4342598 | 932 | if (version == PKCS5v2 || version == PKCS12) |
wolfSSL | 0:9d17e4342598 | 933 | desIv = cbcIv; |
wolfSSL | 0:9d17e4342598 | 934 | ret = Des3_SetKey(&dec, key, desIv, DES_DECRYPTION); |
wolfSSL | 0:9d17e4342598 | 935 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 936 | return ret; |
wolfSSL | 0:9d17e4342598 | 937 | ret = Des3_CbcDecrypt(&dec, input, input, length); |
wolfSSL | 0:9d17e4342598 | 938 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 939 | return ret; |
wolfSSL | 0:9d17e4342598 | 940 | break; |
wolfSSL | 0:9d17e4342598 | 941 | } |
wolfSSL | 0:9d17e4342598 | 942 | #endif |
wolfSSL | 0:9d17e4342598 | 943 | #ifndef NO_RC4 |
wolfSSL | 0:9d17e4342598 | 944 | case RC4_TYPE: |
wolfSSL | 0:9d17e4342598 | 945 | { |
wolfSSL | 0:9d17e4342598 | 946 | Arc4 dec; |
wolfSSL | 0:9d17e4342598 | 947 | |
wolfSSL | 0:9d17e4342598 | 948 | Arc4SetKey(&dec, key, derivedLen); |
wolfSSL | 0:9d17e4342598 | 949 | Arc4Process(&dec, input, input, length); |
wolfSSL | 0:9d17e4342598 | 950 | break; |
wolfSSL | 0:9d17e4342598 | 951 | } |
wolfSSL | 0:9d17e4342598 | 952 | #endif |
wolfSSL | 0:9d17e4342598 | 953 | |
wolfSSL | 0:9d17e4342598 | 954 | default: |
wolfSSL | 0:9d17e4342598 | 955 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 956 | } |
wolfSSL | 0:9d17e4342598 | 957 | |
wolfSSL | 0:9d17e4342598 | 958 | return 0; |
wolfSSL | 0:9d17e4342598 | 959 | } |
wolfSSL | 0:9d17e4342598 | 960 | |
wolfSSL | 0:9d17e4342598 | 961 | |
wolfSSL | 0:9d17e4342598 | 962 | /* Remove Encrypted PKCS8 header, move beginning of traditional to beginning |
wolfSSL | 0:9d17e4342598 | 963 | of input */ |
wolfSSL | 0:9d17e4342598 | 964 | int ToTraditionalEnc(byte* input, word32 sz,const char* password,int passwordSz) |
wolfSSL | 0:9d17e4342598 | 965 | { |
wolfSSL | 0:9d17e4342598 | 966 | word32 inOutIdx = 0, oid; |
wolfSSL | 0:9d17e4342598 | 967 | int first, second, length, version, saltSz, id; |
wolfSSL | 0:9d17e4342598 | 968 | int iterations = 0; |
wolfSSL | 0:9d17e4342598 | 969 | byte salt[MAX_SALT_SIZE]; |
wolfSSL | 0:9d17e4342598 | 970 | byte cbcIv[MAX_IV_SIZE]; |
wolfSSL | 0:9d17e4342598 | 971 | |
wolfSSL | 0:9d17e4342598 | 972 | if (GetSequence(input, &inOutIdx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 973 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 974 | |
wolfSSL | 0:9d17e4342598 | 975 | if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 976 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 977 | |
wolfSSL | 0:9d17e4342598 | 978 | first = input[inOutIdx - 2]; /* PKCS version alwyas 2nd to last byte */ |
wolfSSL | 0:9d17e4342598 | 979 | second = input[inOutIdx - 1]; /* version.algo, algo id last byte */ |
wolfSSL | 0:9d17e4342598 | 980 | |
wolfSSL | 0:9d17e4342598 | 981 | if (CheckAlgo(first, second, &id, &version) < 0) |
wolfSSL | 0:9d17e4342598 | 982 | return ASN_INPUT_E; /* Algo ID error */ |
wolfSSL | 0:9d17e4342598 | 983 | |
wolfSSL | 0:9d17e4342598 | 984 | if (version == PKCS5v2) { |
wolfSSL | 0:9d17e4342598 | 985 | |
wolfSSL | 0:9d17e4342598 | 986 | if (GetSequence(input, &inOutIdx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 987 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 988 | |
wolfSSL | 0:9d17e4342598 | 989 | if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 990 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 991 | |
wolfSSL | 0:9d17e4342598 | 992 | if (oid != PBKDF2_OID) |
wolfSSL | 0:9d17e4342598 | 993 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 994 | } |
wolfSSL | 0:9d17e4342598 | 995 | |
wolfSSL | 0:9d17e4342598 | 996 | if (GetSequence(input, &inOutIdx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 997 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 998 | |
wolfSSL | 0:9d17e4342598 | 999 | if (input[inOutIdx++] != ASN_OCTET_STRING) |
wolfSSL | 0:9d17e4342598 | 1000 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1001 | |
wolfSSL | 0:9d17e4342598 | 1002 | if (GetLength(input, &inOutIdx, &saltSz, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 1003 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1004 | |
wolfSSL | 0:9d17e4342598 | 1005 | if (saltSz > MAX_SALT_SIZE) |
wolfSSL | 0:9d17e4342598 | 1006 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1007 | |
wolfSSL | 0:9d17e4342598 | 1008 | XMEMCPY(salt, &input[inOutIdx], saltSz); |
wolfSSL | 0:9d17e4342598 | 1009 | inOutIdx += saltSz; |
wolfSSL | 0:9d17e4342598 | 1010 | |
wolfSSL | 0:9d17e4342598 | 1011 | if (GetShortInt(input, &inOutIdx, &iterations) < 0) |
wolfSSL | 0:9d17e4342598 | 1012 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1013 | |
wolfSSL | 0:9d17e4342598 | 1014 | if (version == PKCS5v2) { |
wolfSSL | 0:9d17e4342598 | 1015 | /* get encryption algo */ |
wolfSSL | 0:9d17e4342598 | 1016 | if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 1017 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1018 | |
wolfSSL | 0:9d17e4342598 | 1019 | if (CheckAlgoV2(oid, &id) < 0) |
wolfSSL | 0:9d17e4342598 | 1020 | return ASN_PARSE_E; /* PKCS v2 algo id error */ |
wolfSSL | 0:9d17e4342598 | 1021 | |
wolfSSL | 0:9d17e4342598 | 1022 | if (input[inOutIdx++] != ASN_OCTET_STRING) |
wolfSSL | 0:9d17e4342598 | 1023 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1024 | |
wolfSSL | 0:9d17e4342598 | 1025 | if (GetLength(input, &inOutIdx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 1026 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1027 | |
wolfSSL | 0:9d17e4342598 | 1028 | XMEMCPY(cbcIv, &input[inOutIdx], length); |
wolfSSL | 0:9d17e4342598 | 1029 | inOutIdx += length; |
wolfSSL | 0:9d17e4342598 | 1030 | } |
wolfSSL | 0:9d17e4342598 | 1031 | |
wolfSSL | 0:9d17e4342598 | 1032 | if (input[inOutIdx++] != ASN_OCTET_STRING) |
wolfSSL | 0:9d17e4342598 | 1033 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1034 | |
wolfSSL | 0:9d17e4342598 | 1035 | if (GetLength(input, &inOutIdx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 1036 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1037 | |
wolfSSL | 0:9d17e4342598 | 1038 | if (DecryptKey(password, passwordSz, salt, saltSz, iterations, id, |
wolfSSL | 0:9d17e4342598 | 1039 | input + inOutIdx, length, version, cbcIv) < 0) |
wolfSSL | 0:9d17e4342598 | 1040 | return ASN_INPUT_E; /* decrypt failure */ |
wolfSSL | 0:9d17e4342598 | 1041 | |
wolfSSL | 0:9d17e4342598 | 1042 | XMEMMOVE(input, input + inOutIdx, length); |
wolfSSL | 0:9d17e4342598 | 1043 | return ToTraditional(input, length); |
wolfSSL | 0:9d17e4342598 | 1044 | } |
wolfSSL | 0:9d17e4342598 | 1045 | |
wolfSSL | 0:9d17e4342598 | 1046 | #endif /* NO_PWDBASED */ |
wolfSSL | 0:9d17e4342598 | 1047 | |
wolfSSL | 0:9d17e4342598 | 1048 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 1049 | |
wolfSSL | 0:9d17e4342598 | 1050 | int RsaPublicKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key, |
wolfSSL | 0:9d17e4342598 | 1051 | word32 inSz) |
wolfSSL | 0:9d17e4342598 | 1052 | { |
wolfSSL | 0:9d17e4342598 | 1053 | int length; |
wolfSSL | 0:9d17e4342598 | 1054 | |
wolfSSL | 0:9d17e4342598 | 1055 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1056 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1057 | |
wolfSSL | 0:9d17e4342598 | 1058 | key->type = RSA_PUBLIC; |
wolfSSL | 0:9d17e4342598 | 1059 | |
wolfSSL | 0:9d17e4342598 | 1060 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1061 | { |
wolfSSL | 0:9d17e4342598 | 1062 | byte b = input[*inOutIdx]; |
wolfSSL | 0:9d17e4342598 | 1063 | if (b != ASN_INTEGER) { |
wolfSSL | 0:9d17e4342598 | 1064 | /* not from decoded cert, will have algo id, skip past */ |
wolfSSL | 0:9d17e4342598 | 1065 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1066 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1067 | |
wolfSSL | 0:9d17e4342598 | 1068 | b = input[(*inOutIdx)++]; |
wolfSSL | 0:9d17e4342598 | 1069 | if (b != ASN_OBJECT_ID) |
wolfSSL | 0:9d17e4342598 | 1070 | return ASN_OBJECT_ID_E; |
wolfSSL | 0:9d17e4342598 | 1071 | |
wolfSSL | 0:9d17e4342598 | 1072 | if (GetLength(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1073 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1074 | |
wolfSSL | 0:9d17e4342598 | 1075 | *inOutIdx += length; /* skip past */ |
wolfSSL | 0:9d17e4342598 | 1076 | |
wolfSSL | 0:9d17e4342598 | 1077 | /* could have NULL tag and 0 terminator, but may not */ |
wolfSSL | 0:9d17e4342598 | 1078 | b = input[(*inOutIdx)++]; |
wolfSSL | 0:9d17e4342598 | 1079 | |
wolfSSL | 0:9d17e4342598 | 1080 | if (b == ASN_TAG_NULL) { |
wolfSSL | 0:9d17e4342598 | 1081 | b = input[(*inOutIdx)++]; |
wolfSSL | 0:9d17e4342598 | 1082 | if (b != 0) |
wolfSSL | 0:9d17e4342598 | 1083 | return ASN_EXPECT_0_E; |
wolfSSL | 0:9d17e4342598 | 1084 | } |
wolfSSL | 0:9d17e4342598 | 1085 | else |
wolfSSL | 0:9d17e4342598 | 1086 | /* go back, didn't have it */ |
wolfSSL | 0:9d17e4342598 | 1087 | (*inOutIdx)--; |
wolfSSL | 0:9d17e4342598 | 1088 | |
wolfSSL | 0:9d17e4342598 | 1089 | /* should have bit tag length and seq next */ |
wolfSSL | 0:9d17e4342598 | 1090 | b = input[(*inOutIdx)++]; |
wolfSSL | 0:9d17e4342598 | 1091 | if (b != ASN_BIT_STRING) |
wolfSSL | 0:9d17e4342598 | 1092 | return ASN_BITSTR_E; |
wolfSSL | 0:9d17e4342598 | 1093 | |
wolfSSL | 0:9d17e4342598 | 1094 | if (GetLength(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1095 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1096 | |
wolfSSL | 0:9d17e4342598 | 1097 | /* could have 0 */ |
wolfSSL | 0:9d17e4342598 | 1098 | b = input[(*inOutIdx)++]; |
wolfSSL | 0:9d17e4342598 | 1099 | if (b != 0) |
wolfSSL | 0:9d17e4342598 | 1100 | (*inOutIdx)--; |
wolfSSL | 0:9d17e4342598 | 1101 | |
wolfSSL | 0:9d17e4342598 | 1102 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1103 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1104 | } /* end if */ |
wolfSSL | 0:9d17e4342598 | 1105 | } /* openssl var block */ |
wolfSSL | 0:9d17e4342598 | 1106 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1107 | |
wolfSSL | 0:9d17e4342598 | 1108 | if (GetInt(&key->n, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1109 | GetInt(&key->e, input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1110 | |
wolfSSL | 0:9d17e4342598 | 1111 | return 0; |
wolfSSL | 0:9d17e4342598 | 1112 | } |
wolfSSL | 0:9d17e4342598 | 1113 | |
wolfSSL | 0:9d17e4342598 | 1114 | #endif |
wolfSSL | 0:9d17e4342598 | 1115 | |
wolfSSL | 0:9d17e4342598 | 1116 | #ifndef NO_DH |
wolfSSL | 0:9d17e4342598 | 1117 | |
wolfSSL | 0:9d17e4342598 | 1118 | int DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz) |
wolfSSL | 0:9d17e4342598 | 1119 | { |
wolfSSL | 0:9d17e4342598 | 1120 | int length; |
wolfSSL | 0:9d17e4342598 | 1121 | |
wolfSSL | 0:9d17e4342598 | 1122 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1123 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1124 | |
wolfSSL | 0:9d17e4342598 | 1125 | if (GetInt(&key->p, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1126 | GetInt(&key->g, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1127 | |
wolfSSL | 0:9d17e4342598 | 1128 | return 0; |
wolfSSL | 0:9d17e4342598 | 1129 | } |
wolfSSL | 0:9d17e4342598 | 1130 | |
wolfSSL | 0:9d17e4342598 | 1131 | int DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g, word32 gSz) |
wolfSSL | 0:9d17e4342598 | 1132 | { |
wolfSSL | 0:9d17e4342598 | 1133 | if (key == NULL || p == NULL || g == NULL || pSz == 0 || gSz == 0) |
wolfSSL | 0:9d17e4342598 | 1134 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 1135 | |
wolfSSL | 0:9d17e4342598 | 1136 | /* may have leading 0 */ |
wolfSSL | 0:9d17e4342598 | 1137 | if (p[0] == 0) { |
wolfSSL | 0:9d17e4342598 | 1138 | pSz--; p++; |
wolfSSL | 0:9d17e4342598 | 1139 | } |
wolfSSL | 0:9d17e4342598 | 1140 | |
wolfSSL | 0:9d17e4342598 | 1141 | if (g[0] == 0) { |
wolfSSL | 0:9d17e4342598 | 1142 | gSz--; g++; |
wolfSSL | 0:9d17e4342598 | 1143 | } |
wolfSSL | 0:9d17e4342598 | 1144 | |
wolfSSL | 0:9d17e4342598 | 1145 | if (mp_init(&key->p) != MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 1146 | return MP_INIT_E; |
wolfSSL | 0:9d17e4342598 | 1147 | if (mp_read_unsigned_bin(&key->p, p, pSz) != 0) { |
wolfSSL | 0:9d17e4342598 | 1148 | mp_clear(&key->p); |
wolfSSL | 0:9d17e4342598 | 1149 | return ASN_DH_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1150 | } |
wolfSSL | 0:9d17e4342598 | 1151 | |
wolfSSL | 0:9d17e4342598 | 1152 | if (mp_init(&key->g) != MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1153 | mp_clear(&key->p); |
wolfSSL | 0:9d17e4342598 | 1154 | return MP_INIT_E; |
wolfSSL | 0:9d17e4342598 | 1155 | } |
wolfSSL | 0:9d17e4342598 | 1156 | if (mp_read_unsigned_bin(&key->g, g, gSz) != 0) { |
wolfSSL | 0:9d17e4342598 | 1157 | mp_clear(&key->g); |
wolfSSL | 0:9d17e4342598 | 1158 | mp_clear(&key->p); |
wolfSSL | 0:9d17e4342598 | 1159 | return ASN_DH_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1160 | } |
wolfSSL | 0:9d17e4342598 | 1161 | |
wolfSSL | 0:9d17e4342598 | 1162 | return 0; |
wolfSSL | 0:9d17e4342598 | 1163 | } |
wolfSSL | 0:9d17e4342598 | 1164 | |
wolfSSL | 0:9d17e4342598 | 1165 | |
wolfSSL | 0:9d17e4342598 | 1166 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1167 | |
wolfSSL | 0:9d17e4342598 | 1168 | int DhParamsLoad(const byte* input, word32 inSz, byte* p, word32* pInOutSz, |
wolfSSL | 0:9d17e4342598 | 1169 | byte* g, word32* gInOutSz) |
wolfSSL | 0:9d17e4342598 | 1170 | { |
wolfSSL | 0:9d17e4342598 | 1171 | word32 i = 0; |
wolfSSL | 0:9d17e4342598 | 1172 | byte b; |
wolfSSL | 0:9d17e4342598 | 1173 | int length; |
wolfSSL | 0:9d17e4342598 | 1174 | |
wolfSSL | 0:9d17e4342598 | 1175 | if (GetSequence(input, &i, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1176 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1177 | |
wolfSSL | 0:9d17e4342598 | 1178 | b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 1179 | if (b != ASN_INTEGER) |
wolfSSL | 0:9d17e4342598 | 1180 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1181 | |
wolfSSL | 0:9d17e4342598 | 1182 | if (GetLength(input, &i, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1183 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1184 | |
wolfSSL | 0:9d17e4342598 | 1185 | if ( (b = input[i++]) == 0x00) |
wolfSSL | 0:9d17e4342598 | 1186 | length--; |
wolfSSL | 0:9d17e4342598 | 1187 | else |
wolfSSL | 0:9d17e4342598 | 1188 | i--; |
wolfSSL | 0:9d17e4342598 | 1189 | |
wolfSSL | 0:9d17e4342598 | 1190 | if (length <= (int)*pInOutSz) { |
wolfSSL | 0:9d17e4342598 | 1191 | XMEMCPY(p, &input[i], length); |
wolfSSL | 0:9d17e4342598 | 1192 | *pInOutSz = length; |
wolfSSL | 0:9d17e4342598 | 1193 | } |
wolfSSL | 0:9d17e4342598 | 1194 | else |
wolfSSL | 0:9d17e4342598 | 1195 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 1196 | |
wolfSSL | 0:9d17e4342598 | 1197 | i += length; |
wolfSSL | 0:9d17e4342598 | 1198 | |
wolfSSL | 0:9d17e4342598 | 1199 | b = input[i++]; |
wolfSSL | 0:9d17e4342598 | 1200 | if (b != ASN_INTEGER) |
wolfSSL | 0:9d17e4342598 | 1201 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1202 | |
wolfSSL | 0:9d17e4342598 | 1203 | if (GetLength(input, &i, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1204 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1205 | |
wolfSSL | 0:9d17e4342598 | 1206 | if (length <= (int)*gInOutSz) { |
wolfSSL | 0:9d17e4342598 | 1207 | XMEMCPY(g, &input[i], length); |
wolfSSL | 0:9d17e4342598 | 1208 | *gInOutSz = length; |
wolfSSL | 0:9d17e4342598 | 1209 | } |
wolfSSL | 0:9d17e4342598 | 1210 | else |
wolfSSL | 0:9d17e4342598 | 1211 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 1212 | |
wolfSSL | 0:9d17e4342598 | 1213 | return 0; |
wolfSSL | 0:9d17e4342598 | 1214 | } |
wolfSSL | 0:9d17e4342598 | 1215 | |
wolfSSL | 0:9d17e4342598 | 1216 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1217 | #endif /* NO_DH */ |
wolfSSL | 0:9d17e4342598 | 1218 | |
wolfSSL | 0:9d17e4342598 | 1219 | |
wolfSSL | 0:9d17e4342598 | 1220 | #ifndef NO_DSA |
wolfSSL | 0:9d17e4342598 | 1221 | |
wolfSSL | 0:9d17e4342598 | 1222 | int DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, |
wolfSSL | 0:9d17e4342598 | 1223 | word32 inSz) |
wolfSSL | 0:9d17e4342598 | 1224 | { |
wolfSSL | 0:9d17e4342598 | 1225 | int length; |
wolfSSL | 0:9d17e4342598 | 1226 | |
wolfSSL | 0:9d17e4342598 | 1227 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1228 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1229 | |
wolfSSL | 0:9d17e4342598 | 1230 | if (GetInt(&key->p, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1231 | GetInt(&key->q, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1232 | GetInt(&key->g, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1233 | GetInt(&key->y, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1234 | |
wolfSSL | 0:9d17e4342598 | 1235 | key->type = DSA_PUBLIC; |
wolfSSL | 0:9d17e4342598 | 1236 | return 0; |
wolfSSL | 0:9d17e4342598 | 1237 | } |
wolfSSL | 0:9d17e4342598 | 1238 | |
wolfSSL | 0:9d17e4342598 | 1239 | |
wolfSSL | 0:9d17e4342598 | 1240 | int DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, |
wolfSSL | 0:9d17e4342598 | 1241 | word32 inSz) |
wolfSSL | 0:9d17e4342598 | 1242 | { |
wolfSSL | 0:9d17e4342598 | 1243 | int length, version; |
wolfSSL | 0:9d17e4342598 | 1244 | |
wolfSSL | 0:9d17e4342598 | 1245 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 1246 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1247 | |
wolfSSL | 0:9d17e4342598 | 1248 | if (GetMyVersion(input, inOutIdx, &version) < 0) |
wolfSSL | 0:9d17e4342598 | 1249 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1250 | |
wolfSSL | 0:9d17e4342598 | 1251 | if (GetInt(&key->p, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1252 | GetInt(&key->q, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1253 | GetInt(&key->g, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1254 | GetInt(&key->y, input, inOutIdx, inSz) < 0 || |
wolfSSL | 0:9d17e4342598 | 1255 | GetInt(&key->x, input, inOutIdx, inSz) < 0 ) return ASN_DH_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1256 | |
wolfSSL | 0:9d17e4342598 | 1257 | key->type = DSA_PRIVATE; |
wolfSSL | 0:9d17e4342598 | 1258 | return 0; |
wolfSSL | 0:9d17e4342598 | 1259 | } |
wolfSSL | 0:9d17e4342598 | 1260 | |
wolfSSL | 0:9d17e4342598 | 1261 | #endif /* NO_DSA */ |
wolfSSL | 0:9d17e4342598 | 1262 | |
wolfSSL | 0:9d17e4342598 | 1263 | |
wolfSSL | 0:9d17e4342598 | 1264 | void InitDecodedCert(DecodedCert* cert, byte* source, word32 inSz, void* heap) |
wolfSSL | 0:9d17e4342598 | 1265 | { |
wolfSSL | 0:9d17e4342598 | 1266 | cert->publicKey = 0; |
wolfSSL | 0:9d17e4342598 | 1267 | cert->pubKeySize = 0; |
wolfSSL | 0:9d17e4342598 | 1268 | cert->pubKeyStored = 0; |
wolfSSL | 0:9d17e4342598 | 1269 | cert->version = 0; |
wolfSSL | 0:9d17e4342598 | 1270 | cert->signature = 0; |
wolfSSL | 0:9d17e4342598 | 1271 | cert->subjectCN = 0; |
wolfSSL | 0:9d17e4342598 | 1272 | cert->subjectCNLen = 0; |
wolfSSL | 0:9d17e4342598 | 1273 | cert->subjectCNStored = 0; |
wolfSSL | 0:9d17e4342598 | 1274 | cert->altNames = NULL; |
wolfSSL | 0:9d17e4342598 | 1275 | cert->issuer[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 1276 | cert->subject[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 1277 | cert->source = source; /* don't own */ |
wolfSSL | 0:9d17e4342598 | 1278 | cert->srcIdx = 0; |
wolfSSL | 0:9d17e4342598 | 1279 | cert->maxIdx = inSz; /* can't go over this index */ |
wolfSSL | 0:9d17e4342598 | 1280 | cert->heap = heap; |
wolfSSL | 0:9d17e4342598 | 1281 | XMEMSET(cert->serial, 0, EXTERNAL_SERIAL_SIZE); |
wolfSSL | 0:9d17e4342598 | 1282 | cert->serialSz = 0; |
wolfSSL | 0:9d17e4342598 | 1283 | cert->extensions = 0; |
wolfSSL | 0:9d17e4342598 | 1284 | cert->extensionsSz = 0; |
wolfSSL | 0:9d17e4342598 | 1285 | cert->extensionsIdx = 0; |
wolfSSL | 0:9d17e4342598 | 1286 | cert->extAuthInfo = NULL; |
wolfSSL | 0:9d17e4342598 | 1287 | cert->extAuthInfoSz = 0; |
wolfSSL | 0:9d17e4342598 | 1288 | cert->extCrlInfo = NULL; |
wolfSSL | 0:9d17e4342598 | 1289 | cert->extCrlInfoSz = 0; |
wolfSSL | 0:9d17e4342598 | 1290 | XMEMSET(cert->extSubjKeyId, 0, SHA_SIZE); |
wolfSSL | 0:9d17e4342598 | 1291 | cert->extSubjKeyIdSet = 0; |
wolfSSL | 0:9d17e4342598 | 1292 | XMEMSET(cert->extAuthKeyId, 0, SHA_SIZE); |
wolfSSL | 0:9d17e4342598 | 1293 | cert->extAuthKeyIdSet = 0; |
wolfSSL | 0:9d17e4342598 | 1294 | cert->extKeyUsageSet = 0; |
wolfSSL | 0:9d17e4342598 | 1295 | cert->extKeyUsage = 0; |
wolfSSL | 0:9d17e4342598 | 1296 | cert->extExtKeyUsageSet = 0; |
wolfSSL | 0:9d17e4342598 | 1297 | cert->extExtKeyUsage = 0; |
wolfSSL | 0:9d17e4342598 | 1298 | cert->isCA = 0; |
wolfSSL | 0:9d17e4342598 | 1299 | #ifdef HAVE_PKCS7 |
wolfSSL | 0:9d17e4342598 | 1300 | cert->issuerRaw = NULL; |
wolfSSL | 0:9d17e4342598 | 1301 | cert->issuerRawLen = 0; |
wolfSSL | 0:9d17e4342598 | 1302 | #endif |
wolfSSL | 0:9d17e4342598 | 1303 | #ifdef CYASSL_CERT_GEN |
wolfSSL | 0:9d17e4342598 | 1304 | cert->subjectSN = 0; |
wolfSSL | 0:9d17e4342598 | 1305 | cert->subjectSNLen = 0; |
wolfSSL | 0:9d17e4342598 | 1306 | cert->subjectC = 0; |
wolfSSL | 0:9d17e4342598 | 1307 | cert->subjectCLen = 0; |
wolfSSL | 0:9d17e4342598 | 1308 | cert->subjectL = 0; |
wolfSSL | 0:9d17e4342598 | 1309 | cert->subjectLLen = 0; |
wolfSSL | 0:9d17e4342598 | 1310 | cert->subjectST = 0; |
wolfSSL | 0:9d17e4342598 | 1311 | cert->subjectSTLen = 0; |
wolfSSL | 0:9d17e4342598 | 1312 | cert->subjectO = 0; |
wolfSSL | 0:9d17e4342598 | 1313 | cert->subjectOLen = 0; |
wolfSSL | 0:9d17e4342598 | 1314 | cert->subjectOU = 0; |
wolfSSL | 0:9d17e4342598 | 1315 | cert->subjectOULen = 0; |
wolfSSL | 0:9d17e4342598 | 1316 | cert->subjectEmail = 0; |
wolfSSL | 0:9d17e4342598 | 1317 | cert->subjectEmailLen = 0; |
wolfSSL | 0:9d17e4342598 | 1318 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 1319 | cert->beforeDate = NULL; |
wolfSSL | 0:9d17e4342598 | 1320 | cert->beforeDateLen = 0; |
wolfSSL | 0:9d17e4342598 | 1321 | cert->afterDate = NULL; |
wolfSSL | 0:9d17e4342598 | 1322 | cert->afterDateLen = 0; |
wolfSSL | 0:9d17e4342598 | 1323 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1324 | XMEMSET(&cert->issuerName, 0, sizeof(DecodedName)); |
wolfSSL | 0:9d17e4342598 | 1325 | XMEMSET(&cert->subjectName, 0, sizeof(DecodedName)); |
wolfSSL | 0:9d17e4342598 | 1326 | cert->extBasicConstSet = 0; |
wolfSSL | 0:9d17e4342598 | 1327 | cert->extBasicConstCrit = 0; |
wolfSSL | 0:9d17e4342598 | 1328 | cert->extBasicConstPlSet = 0; |
wolfSSL | 0:9d17e4342598 | 1329 | cert->pathLength = 0; |
wolfSSL | 0:9d17e4342598 | 1330 | cert->extSubjAltNameSet = 0; |
wolfSSL | 0:9d17e4342598 | 1331 | cert->extSubjAltNameCrit = 0; |
wolfSSL | 0:9d17e4342598 | 1332 | cert->extAuthKeyIdCrit = 0; |
wolfSSL | 0:9d17e4342598 | 1333 | cert->extSubjKeyIdCrit = 0; |
wolfSSL | 0:9d17e4342598 | 1334 | cert->extKeyUsageCrit = 0; |
wolfSSL | 0:9d17e4342598 | 1335 | cert->extExtKeyUsageCrit = 0; |
wolfSSL | 0:9d17e4342598 | 1336 | cert->extExtKeyUsageSrc = NULL; |
wolfSSL | 0:9d17e4342598 | 1337 | cert->extExtKeyUsageSz = 0; |
wolfSSL | 0:9d17e4342598 | 1338 | cert->extExtKeyUsageCount = 0; |
wolfSSL | 0:9d17e4342598 | 1339 | cert->extAuthKeyIdSrc = NULL; |
wolfSSL | 0:9d17e4342598 | 1340 | cert->extAuthKeyIdSz = 0; |
wolfSSL | 0:9d17e4342598 | 1341 | cert->extSubjKeyIdSrc = NULL; |
wolfSSL | 0:9d17e4342598 | 1342 | cert->extSubjKeyIdSz = 0; |
wolfSSL | 0:9d17e4342598 | 1343 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1344 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 1345 | cert->pkCurveOID = 0; |
wolfSSL | 0:9d17e4342598 | 1346 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 1347 | #ifdef CYASSL_SEP |
wolfSSL | 0:9d17e4342598 | 1348 | cert->deviceTypeSz = 0; |
wolfSSL | 0:9d17e4342598 | 1349 | cert->deviceType = NULL; |
wolfSSL | 0:9d17e4342598 | 1350 | cert->hwTypeSz = 0; |
wolfSSL | 0:9d17e4342598 | 1351 | cert->hwType = NULL; |
wolfSSL | 0:9d17e4342598 | 1352 | cert->hwSerialNumSz = 0; |
wolfSSL | 0:9d17e4342598 | 1353 | cert->hwSerialNum = NULL; |
wolfSSL | 0:9d17e4342598 | 1354 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1355 | cert->extCertPolicySet = 0; |
wolfSSL | 0:9d17e4342598 | 1356 | cert->extCertPolicyCrit = 0; |
wolfSSL | 0:9d17e4342598 | 1357 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1358 | #endif /* CYASSL_SEP */ |
wolfSSL | 0:9d17e4342598 | 1359 | } |
wolfSSL | 0:9d17e4342598 | 1360 | |
wolfSSL | 0:9d17e4342598 | 1361 | |
wolfSSL | 0:9d17e4342598 | 1362 | void FreeAltNames(DNS_entry* altNames, void* heap) |
wolfSSL | 0:9d17e4342598 | 1363 | { |
wolfSSL | 0:9d17e4342598 | 1364 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 1365 | |
wolfSSL | 0:9d17e4342598 | 1366 | while (altNames) { |
wolfSSL | 0:9d17e4342598 | 1367 | DNS_entry* tmp = altNames->next; |
wolfSSL | 0:9d17e4342598 | 1368 | |
wolfSSL | 0:9d17e4342598 | 1369 | XFREE(altNames->name, heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 0:9d17e4342598 | 1370 | XFREE(altNames, heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 0:9d17e4342598 | 1371 | altNames = tmp; |
wolfSSL | 0:9d17e4342598 | 1372 | } |
wolfSSL | 0:9d17e4342598 | 1373 | } |
wolfSSL | 0:9d17e4342598 | 1374 | |
wolfSSL | 0:9d17e4342598 | 1375 | |
wolfSSL | 0:9d17e4342598 | 1376 | void FreeDecodedCert(DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 1377 | { |
wolfSSL | 0:9d17e4342598 | 1378 | if (cert->subjectCNStored == 1) |
wolfSSL | 0:9d17e4342598 | 1379 | XFREE(cert->subjectCN, cert->heap, DYNAMIC_TYPE_SUBJECT_CN); |
wolfSSL | 0:9d17e4342598 | 1380 | if (cert->pubKeyStored == 1) |
wolfSSL | 0:9d17e4342598 | 1381 | XFREE(cert->publicKey, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 0:9d17e4342598 | 1382 | if (cert->altNames) |
wolfSSL | 0:9d17e4342598 | 1383 | FreeAltNames(cert->altNames, cert->heap); |
wolfSSL | 0:9d17e4342598 | 1384 | #ifdef CYASSL_SEP |
wolfSSL | 0:9d17e4342598 | 1385 | XFREE(cert->deviceType, cert->heap, 0); |
wolfSSL | 0:9d17e4342598 | 1386 | XFREE(cert->hwType, cert->heap, 0); |
wolfSSL | 0:9d17e4342598 | 1387 | XFREE(cert->hwSerialNum, cert->heap, 0); |
wolfSSL | 0:9d17e4342598 | 1388 | #endif /* CYASSL_SEP */ |
wolfSSL | 0:9d17e4342598 | 1389 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1390 | if (cert->issuerName.fullName != NULL) |
wolfSSL | 0:9d17e4342598 | 1391 | XFREE(cert->issuerName.fullName, NULL, DYNAMIC_TYPE_X509); |
wolfSSL | 0:9d17e4342598 | 1392 | if (cert->subjectName.fullName != NULL) |
wolfSSL | 0:9d17e4342598 | 1393 | XFREE(cert->subjectName.fullName, NULL, DYNAMIC_TYPE_X509); |
wolfSSL | 0:9d17e4342598 | 1394 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1395 | } |
wolfSSL | 0:9d17e4342598 | 1396 | |
wolfSSL | 0:9d17e4342598 | 1397 | |
wolfSSL | 0:9d17e4342598 | 1398 | static int GetCertHeader(DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 1399 | { |
wolfSSL | 0:9d17e4342598 | 1400 | int ret = 0, len; |
wolfSSL | 0:9d17e4342598 | 1401 | byte serialTmp[EXTERNAL_SERIAL_SIZE]; |
wolfSSL | 0:9d17e4342598 | 1402 | mp_int mpi; |
wolfSSL | 0:9d17e4342598 | 1403 | |
wolfSSL | 0:9d17e4342598 | 1404 | if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1405 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1406 | |
wolfSSL | 0:9d17e4342598 | 1407 | cert->certBegin = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1408 | |
wolfSSL | 0:9d17e4342598 | 1409 | if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1410 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1411 | cert->sigIndex = len + cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1412 | |
wolfSSL | 0:9d17e4342598 | 1413 | if (GetExplicitVersion(cert->source, &cert->srcIdx, &cert->version) < 0) |
wolfSSL | 0:9d17e4342598 | 1414 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1415 | |
wolfSSL | 0:9d17e4342598 | 1416 | if (GetInt(&mpi, cert->source, &cert->srcIdx, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1417 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1418 | |
wolfSSL | 0:9d17e4342598 | 1419 | len = mp_unsigned_bin_size(&mpi); |
wolfSSL | 0:9d17e4342598 | 1420 | if (len < (int)sizeof(serialTmp)) { |
wolfSSL | 0:9d17e4342598 | 1421 | if ( (ret = mp_to_unsigned_bin(&mpi, serialTmp)) == MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 1422 | if (len > EXTERNAL_SERIAL_SIZE) |
wolfSSL | 0:9d17e4342598 | 1423 | len = EXTERNAL_SERIAL_SIZE; |
wolfSSL | 0:9d17e4342598 | 1424 | XMEMCPY(cert->serial, serialTmp, len); |
wolfSSL | 0:9d17e4342598 | 1425 | cert->serialSz = len; |
wolfSSL | 0:9d17e4342598 | 1426 | } |
wolfSSL | 0:9d17e4342598 | 1427 | } |
wolfSSL | 0:9d17e4342598 | 1428 | mp_clear(&mpi); |
wolfSSL | 0:9d17e4342598 | 1429 | return ret; |
wolfSSL | 0:9d17e4342598 | 1430 | } |
wolfSSL | 0:9d17e4342598 | 1431 | |
wolfSSL | 0:9d17e4342598 | 1432 | #if !defined(NO_RSA) |
wolfSSL | 0:9d17e4342598 | 1433 | /* Store Rsa Key, may save later, Dsa could use in future */ |
wolfSSL | 0:9d17e4342598 | 1434 | static int StoreRsaKey(DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 1435 | { |
wolfSSL | 0:9d17e4342598 | 1436 | int length; |
wolfSSL | 0:9d17e4342598 | 1437 | word32 recvd = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1438 | |
wolfSSL | 0:9d17e4342598 | 1439 | if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1440 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1441 | |
wolfSSL | 0:9d17e4342598 | 1442 | recvd = cert->srcIdx - recvd; |
wolfSSL | 0:9d17e4342598 | 1443 | length += recvd; |
wolfSSL | 0:9d17e4342598 | 1444 | |
wolfSSL | 0:9d17e4342598 | 1445 | while (recvd--) |
wolfSSL | 0:9d17e4342598 | 1446 | cert->srcIdx--; |
wolfSSL | 0:9d17e4342598 | 1447 | |
wolfSSL | 0:9d17e4342598 | 1448 | cert->pubKeySize = length; |
wolfSSL | 0:9d17e4342598 | 1449 | cert->publicKey = cert->source + cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1450 | cert->srcIdx += length; |
wolfSSL | 0:9d17e4342598 | 1451 | |
wolfSSL | 0:9d17e4342598 | 1452 | return 0; |
wolfSSL | 0:9d17e4342598 | 1453 | } |
wolfSSL | 0:9d17e4342598 | 1454 | #endif |
wolfSSL | 0:9d17e4342598 | 1455 | |
wolfSSL | 0:9d17e4342598 | 1456 | |
wolfSSL | 0:9d17e4342598 | 1457 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 1458 | |
wolfSSL | 0:9d17e4342598 | 1459 | /* return 0 on sucess if the ECC curve oid sum is supported */ |
wolfSSL | 0:9d17e4342598 | 1460 | static int CheckCurve(word32 oid) |
wolfSSL | 0:9d17e4342598 | 1461 | { |
wolfSSL | 0:9d17e4342598 | 1462 | if (oid != ECC_256R1 && oid != ECC_384R1 && oid != ECC_521R1 && oid != |
wolfSSL | 0:9d17e4342598 | 1463 | ECC_160R1 && oid != ECC_192R1 && oid != ECC_224R1) |
wolfSSL | 0:9d17e4342598 | 1464 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 1465 | |
wolfSSL | 0:9d17e4342598 | 1466 | return 0; |
wolfSSL | 0:9d17e4342598 | 1467 | } |
wolfSSL | 0:9d17e4342598 | 1468 | |
wolfSSL | 0:9d17e4342598 | 1469 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 1470 | |
wolfSSL | 0:9d17e4342598 | 1471 | |
wolfSSL | 0:9d17e4342598 | 1472 | static int GetKey(DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 1473 | { |
wolfSSL | 0:9d17e4342598 | 1474 | int length; |
wolfSSL | 0:9d17e4342598 | 1475 | #ifdef HAVE_NTRU |
wolfSSL | 0:9d17e4342598 | 1476 | int tmpIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1477 | #endif |
wolfSSL | 0:9d17e4342598 | 1478 | |
wolfSSL | 0:9d17e4342598 | 1479 | if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1480 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1481 | |
wolfSSL | 0:9d17e4342598 | 1482 | if (GetAlgoId(cert->source, &cert->srcIdx, &cert->keyOID, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1483 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1484 | |
wolfSSL | 0:9d17e4342598 | 1485 | switch (cert->keyOID) { |
wolfSSL | 0:9d17e4342598 | 1486 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 1487 | case RSAk: |
wolfSSL | 0:9d17e4342598 | 1488 | { |
wolfSSL | 0:9d17e4342598 | 1489 | byte b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 1490 | if (b != ASN_BIT_STRING) |
wolfSSL | 0:9d17e4342598 | 1491 | return ASN_BITSTR_E; |
wolfSSL | 0:9d17e4342598 | 1492 | |
wolfSSL | 0:9d17e4342598 | 1493 | if (GetLength(cert->source,&cert->srcIdx,&length,cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1494 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1495 | b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 1496 | if (b != 0x00) |
wolfSSL | 0:9d17e4342598 | 1497 | return ASN_EXPECT_0_E; |
wolfSSL | 0:9d17e4342598 | 1498 | |
wolfSSL | 0:9d17e4342598 | 1499 | return StoreRsaKey(cert); |
wolfSSL | 0:9d17e4342598 | 1500 | } |
wolfSSL | 0:9d17e4342598 | 1501 | |
wolfSSL | 0:9d17e4342598 | 1502 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 1503 | #ifdef HAVE_NTRU |
wolfSSL | 0:9d17e4342598 | 1504 | case NTRUk: |
wolfSSL | 0:9d17e4342598 | 1505 | { |
wolfSSL | 0:9d17e4342598 | 1506 | const byte* key = &cert->source[tmpIdx]; |
wolfSSL | 0:9d17e4342598 | 1507 | byte* next = (byte*)key; |
wolfSSL | 0:9d17e4342598 | 1508 | word16 keyLen; |
wolfSSL | 0:9d17e4342598 | 1509 | byte keyBlob[MAX_NTRU_KEY_SZ]; |
wolfSSL | 0:9d17e4342598 | 1510 | |
wolfSSL | 0:9d17e4342598 | 1511 | word32 rc = crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key, |
wolfSSL | 0:9d17e4342598 | 1512 | &keyLen, NULL, &next); |
wolfSSL | 0:9d17e4342598 | 1513 | |
wolfSSL | 0:9d17e4342598 | 1514 | if (rc != NTRU_OK) |
wolfSSL | 0:9d17e4342598 | 1515 | return ASN_NTRU_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1516 | if (keyLen > sizeof(keyBlob)) |
wolfSSL | 0:9d17e4342598 | 1517 | return ASN_NTRU_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1518 | |
wolfSSL | 0:9d17e4342598 | 1519 | rc = crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key,&keyLen, |
wolfSSL | 0:9d17e4342598 | 1520 | keyBlob, &next); |
wolfSSL | 0:9d17e4342598 | 1521 | if (rc != NTRU_OK) |
wolfSSL | 0:9d17e4342598 | 1522 | return ASN_NTRU_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1523 | |
wolfSSL | 0:9d17e4342598 | 1524 | if ( (next - key) < 0) |
wolfSSL | 0:9d17e4342598 | 1525 | return ASN_NTRU_KEY_E; |
wolfSSL | 0:9d17e4342598 | 1526 | |
wolfSSL | 0:9d17e4342598 | 1527 | cert->srcIdx = tmpIdx + (int)(next - key); |
wolfSSL | 0:9d17e4342598 | 1528 | |
wolfSSL | 0:9d17e4342598 | 1529 | cert->publicKey = (byte*) XMALLOC(keyLen, cert->heap, |
wolfSSL | 0:9d17e4342598 | 1530 | DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 0:9d17e4342598 | 1531 | if (cert->publicKey == NULL) |
wolfSSL | 0:9d17e4342598 | 1532 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 1533 | XMEMCPY(cert->publicKey, keyBlob, keyLen); |
wolfSSL | 0:9d17e4342598 | 1534 | cert->pubKeyStored = 1; |
wolfSSL | 0:9d17e4342598 | 1535 | cert->pubKeySize = keyLen; |
wolfSSL | 0:9d17e4342598 | 1536 | |
wolfSSL | 0:9d17e4342598 | 1537 | return 0; |
wolfSSL | 0:9d17e4342598 | 1538 | } |
wolfSSL | 0:9d17e4342598 | 1539 | #endif /* HAVE_NTRU */ |
wolfSSL | 0:9d17e4342598 | 1540 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 1541 | case ECDSAk: |
wolfSSL | 0:9d17e4342598 | 1542 | { |
wolfSSL | 0:9d17e4342598 | 1543 | int oidSz = 0; |
wolfSSL | 0:9d17e4342598 | 1544 | byte b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 1545 | |
wolfSSL | 0:9d17e4342598 | 1546 | if (b != ASN_OBJECT_ID) |
wolfSSL | 0:9d17e4342598 | 1547 | return ASN_OBJECT_ID_E; |
wolfSSL | 0:9d17e4342598 | 1548 | |
wolfSSL | 0:9d17e4342598 | 1549 | if (GetLength(cert->source,&cert->srcIdx,&oidSz,cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1550 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1551 | |
wolfSSL | 0:9d17e4342598 | 1552 | while(oidSz--) |
wolfSSL | 0:9d17e4342598 | 1553 | cert->pkCurveOID += cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 1554 | |
wolfSSL | 0:9d17e4342598 | 1555 | if (CheckCurve(cert->pkCurveOID) < 0) |
wolfSSL | 0:9d17e4342598 | 1556 | return ECC_CURVE_OID_E; |
wolfSSL | 0:9d17e4342598 | 1557 | |
wolfSSL | 0:9d17e4342598 | 1558 | /* key header */ |
wolfSSL | 0:9d17e4342598 | 1559 | b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 1560 | if (b != ASN_BIT_STRING) |
wolfSSL | 0:9d17e4342598 | 1561 | return ASN_BITSTR_E; |
wolfSSL | 0:9d17e4342598 | 1562 | |
wolfSSL | 0:9d17e4342598 | 1563 | if (GetLength(cert->source,&cert->srcIdx,&length,cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1564 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1565 | b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 1566 | if (b != 0x00) |
wolfSSL | 0:9d17e4342598 | 1567 | return ASN_EXPECT_0_E; |
wolfSSL | 0:9d17e4342598 | 1568 | |
wolfSSL | 0:9d17e4342598 | 1569 | /* actual key, use length - 1 since ate preceding 0 */ |
wolfSSL | 0:9d17e4342598 | 1570 | length -= 1; |
wolfSSL | 0:9d17e4342598 | 1571 | |
wolfSSL | 0:9d17e4342598 | 1572 | cert->publicKey = (byte*) XMALLOC(length, cert->heap, |
wolfSSL | 0:9d17e4342598 | 1573 | DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 0:9d17e4342598 | 1574 | if (cert->publicKey == NULL) |
wolfSSL | 0:9d17e4342598 | 1575 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 1576 | XMEMCPY(cert->publicKey, &cert->source[cert->srcIdx], length); |
wolfSSL | 0:9d17e4342598 | 1577 | cert->pubKeyStored = 1; |
wolfSSL | 0:9d17e4342598 | 1578 | cert->pubKeySize = length; |
wolfSSL | 0:9d17e4342598 | 1579 | |
wolfSSL | 0:9d17e4342598 | 1580 | cert->srcIdx += length; |
wolfSSL | 0:9d17e4342598 | 1581 | |
wolfSSL | 0:9d17e4342598 | 1582 | return 0; |
wolfSSL | 0:9d17e4342598 | 1583 | } |
wolfSSL | 0:9d17e4342598 | 1584 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 1585 | default: |
wolfSSL | 0:9d17e4342598 | 1586 | return ASN_UNKNOWN_OID_E; |
wolfSSL | 0:9d17e4342598 | 1587 | } |
wolfSSL | 0:9d17e4342598 | 1588 | } |
wolfSSL | 0:9d17e4342598 | 1589 | |
wolfSSL | 0:9d17e4342598 | 1590 | |
wolfSSL | 0:9d17e4342598 | 1591 | /* process NAME, either issuer or subject */ |
wolfSSL | 0:9d17e4342598 | 1592 | static int GetName(DecodedCert* cert, int nameType) |
wolfSSL | 0:9d17e4342598 | 1593 | { |
wolfSSL | 0:9d17e4342598 | 1594 | Sha sha; /* MUST have SHA-1 hash for cert names */ |
wolfSSL | 0:9d17e4342598 | 1595 | int length; /* length of all distinguished names */ |
wolfSSL | 0:9d17e4342598 | 1596 | int dummy; |
wolfSSL | 0:9d17e4342598 | 1597 | int ret; |
wolfSSL | 0:9d17e4342598 | 1598 | char* full = (nameType == ISSUER) ? cert->issuer : cert->subject; |
wolfSSL | 0:9d17e4342598 | 1599 | word32 idx; |
wolfSSL | 0:9d17e4342598 | 1600 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1601 | DecodedName* dName = |
wolfSSL | 0:9d17e4342598 | 1602 | (nameType == ISSUER) ? &cert->issuerName : &cert->subjectName; |
wolfSSL | 0:9d17e4342598 | 1603 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1604 | |
wolfSSL | 0:9d17e4342598 | 1605 | CYASSL_MSG("Getting Cert Name"); |
wolfSSL | 0:9d17e4342598 | 1606 | |
wolfSSL | 0:9d17e4342598 | 1607 | if (cert->source[cert->srcIdx] == ASN_OBJECT_ID) { |
wolfSSL | 0:9d17e4342598 | 1608 | CYASSL_MSG("Trying optional prefix..."); |
wolfSSL | 0:9d17e4342598 | 1609 | |
wolfSSL | 0:9d17e4342598 | 1610 | if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1611 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1612 | |
wolfSSL | 0:9d17e4342598 | 1613 | cert->srcIdx += length; |
wolfSSL | 0:9d17e4342598 | 1614 | CYASSL_MSG("Got optional prefix"); |
wolfSSL | 0:9d17e4342598 | 1615 | } |
wolfSSL | 0:9d17e4342598 | 1616 | |
wolfSSL | 0:9d17e4342598 | 1617 | /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be |
wolfSSL | 0:9d17e4342598 | 1618 | * calculated over the entire DER encoding of the Name field, including |
wolfSSL | 0:9d17e4342598 | 1619 | * the tag and length. */ |
wolfSSL | 0:9d17e4342598 | 1620 | idx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1621 | if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1622 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1623 | |
wolfSSL | 0:9d17e4342598 | 1624 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 1625 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 1626 | return ret; |
wolfSSL | 0:9d17e4342598 | 1627 | ShaUpdate(&sha, &cert->source[idx], length + cert->srcIdx - idx); |
wolfSSL | 0:9d17e4342598 | 1628 | if (nameType == ISSUER) |
wolfSSL | 0:9d17e4342598 | 1629 | ShaFinal(&sha, cert->issuerHash); |
wolfSSL | 0:9d17e4342598 | 1630 | else |
wolfSSL | 0:9d17e4342598 | 1631 | ShaFinal(&sha, cert->subjectHash); |
wolfSSL | 0:9d17e4342598 | 1632 | |
wolfSSL | 0:9d17e4342598 | 1633 | length += cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1634 | idx = 0; |
wolfSSL | 0:9d17e4342598 | 1635 | |
wolfSSL | 0:9d17e4342598 | 1636 | #ifdef HAVE_PKCS7 |
wolfSSL | 0:9d17e4342598 | 1637 | /* store pointer to raw issuer */ |
wolfSSL | 0:9d17e4342598 | 1638 | if (nameType == ISSUER) { |
wolfSSL | 0:9d17e4342598 | 1639 | cert->issuerRaw = &cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1640 | cert->issuerRawLen = length - cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1641 | } |
wolfSSL | 0:9d17e4342598 | 1642 | #endif |
wolfSSL | 0:9d17e4342598 | 1643 | |
wolfSSL | 0:9d17e4342598 | 1644 | while (cert->srcIdx < (word32)length) { |
wolfSSL | 0:9d17e4342598 | 1645 | byte b; |
wolfSSL | 0:9d17e4342598 | 1646 | byte joint[2]; |
wolfSSL | 0:9d17e4342598 | 1647 | byte tooBig = FALSE; |
wolfSSL | 0:9d17e4342598 | 1648 | int oidSz; |
wolfSSL | 0:9d17e4342598 | 1649 | |
wolfSSL | 0:9d17e4342598 | 1650 | if (GetSet(cert->source, &cert->srcIdx, &dummy, cert->maxIdx) < 0) { |
wolfSSL | 0:9d17e4342598 | 1651 | CYASSL_MSG("Cert name lacks set header, trying sequence"); |
wolfSSL | 0:9d17e4342598 | 1652 | } |
wolfSSL | 0:9d17e4342598 | 1653 | |
wolfSSL | 0:9d17e4342598 | 1654 | if (GetSequence(cert->source, &cert->srcIdx, &dummy, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1655 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1656 | |
wolfSSL | 0:9d17e4342598 | 1657 | b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 1658 | if (b != ASN_OBJECT_ID) |
wolfSSL | 0:9d17e4342598 | 1659 | return ASN_OBJECT_ID_E; |
wolfSSL | 0:9d17e4342598 | 1660 | |
wolfSSL | 0:9d17e4342598 | 1661 | if (GetLength(cert->source, &cert->srcIdx, &oidSz, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1662 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1663 | |
wolfSSL | 0:9d17e4342598 | 1664 | XMEMCPY(joint, &cert->source[cert->srcIdx], sizeof(joint)); |
wolfSSL | 0:9d17e4342598 | 1665 | |
wolfSSL | 0:9d17e4342598 | 1666 | /* v1 name types */ |
wolfSSL | 0:9d17e4342598 | 1667 | if (joint[0] == 0x55 && joint[1] == 0x04) { |
wolfSSL | 0:9d17e4342598 | 1668 | byte id; |
wolfSSL | 0:9d17e4342598 | 1669 | byte copy = FALSE; |
wolfSSL | 0:9d17e4342598 | 1670 | int strLen; |
wolfSSL | 0:9d17e4342598 | 1671 | |
wolfSSL | 0:9d17e4342598 | 1672 | cert->srcIdx += 2; |
wolfSSL | 0:9d17e4342598 | 1673 | id = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 1674 | b = cert->source[cert->srcIdx++]; /* strType */ |
wolfSSL | 0:9d17e4342598 | 1675 | (void)b; /* may want to validate? */ |
wolfSSL | 0:9d17e4342598 | 1676 | |
wolfSSL | 0:9d17e4342598 | 1677 | if (GetLength(cert->source, &cert->srcIdx, &strLen, |
wolfSSL | 0:9d17e4342598 | 1678 | cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1679 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1680 | |
wolfSSL | 0:9d17e4342598 | 1681 | if ( (strLen + 14) > (int)(ASN_NAME_MAX - idx)) { |
wolfSSL | 0:9d17e4342598 | 1682 | /* include biggest pre fix header too 4 = "/serialNumber=" */ |
wolfSSL | 0:9d17e4342598 | 1683 | CYASSL_MSG("ASN Name too big, skipping"); |
wolfSSL | 0:9d17e4342598 | 1684 | tooBig = TRUE; |
wolfSSL | 0:9d17e4342598 | 1685 | } |
wolfSSL | 0:9d17e4342598 | 1686 | |
wolfSSL | 0:9d17e4342598 | 1687 | if (id == ASN_COMMON_NAME) { |
wolfSSL | 0:9d17e4342598 | 1688 | if (nameType == SUBJECT) { |
wolfSSL | 0:9d17e4342598 | 1689 | cert->subjectCN = (char *)&cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1690 | cert->subjectCNLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1691 | } |
wolfSSL | 0:9d17e4342598 | 1692 | |
wolfSSL | 0:9d17e4342598 | 1693 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1694 | XMEMCPY(&full[idx], "/CN=", 4); |
wolfSSL | 0:9d17e4342598 | 1695 | idx += 4; |
wolfSSL | 0:9d17e4342598 | 1696 | copy = TRUE; |
wolfSSL | 0:9d17e4342598 | 1697 | } |
wolfSSL | 0:9d17e4342598 | 1698 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1699 | dName->cnIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1700 | dName->cnLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1701 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1702 | } |
wolfSSL | 0:9d17e4342598 | 1703 | else if (id == ASN_SUR_NAME) { |
wolfSSL | 0:9d17e4342598 | 1704 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1705 | XMEMCPY(&full[idx], "/SN=", 4); |
wolfSSL | 0:9d17e4342598 | 1706 | idx += 4; |
wolfSSL | 0:9d17e4342598 | 1707 | copy = TRUE; |
wolfSSL | 0:9d17e4342598 | 1708 | } |
wolfSSL | 0:9d17e4342598 | 1709 | #ifdef CYASSL_CERT_GEN |
wolfSSL | 0:9d17e4342598 | 1710 | if (nameType == SUBJECT) { |
wolfSSL | 0:9d17e4342598 | 1711 | cert->subjectSN = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1712 | cert->subjectSNLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1713 | } |
wolfSSL | 0:9d17e4342598 | 1714 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 1715 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1716 | dName->snIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1717 | dName->snLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1718 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1719 | } |
wolfSSL | 0:9d17e4342598 | 1720 | else if (id == ASN_COUNTRY_NAME) { |
wolfSSL | 0:9d17e4342598 | 1721 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1722 | XMEMCPY(&full[idx], "/C=", 3); |
wolfSSL | 0:9d17e4342598 | 1723 | idx += 3; |
wolfSSL | 0:9d17e4342598 | 1724 | copy = TRUE; |
wolfSSL | 0:9d17e4342598 | 1725 | } |
wolfSSL | 0:9d17e4342598 | 1726 | #ifdef CYASSL_CERT_GEN |
wolfSSL | 0:9d17e4342598 | 1727 | if (nameType == SUBJECT) { |
wolfSSL | 0:9d17e4342598 | 1728 | cert->subjectC = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1729 | cert->subjectCLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1730 | } |
wolfSSL | 0:9d17e4342598 | 1731 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 1732 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1733 | dName->cIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1734 | dName->cLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1735 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1736 | } |
wolfSSL | 0:9d17e4342598 | 1737 | else if (id == ASN_LOCALITY_NAME) { |
wolfSSL | 0:9d17e4342598 | 1738 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1739 | XMEMCPY(&full[idx], "/L=", 3); |
wolfSSL | 0:9d17e4342598 | 1740 | idx += 3; |
wolfSSL | 0:9d17e4342598 | 1741 | copy = TRUE; |
wolfSSL | 0:9d17e4342598 | 1742 | } |
wolfSSL | 0:9d17e4342598 | 1743 | #ifdef CYASSL_CERT_GEN |
wolfSSL | 0:9d17e4342598 | 1744 | if (nameType == SUBJECT) { |
wolfSSL | 0:9d17e4342598 | 1745 | cert->subjectL = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1746 | cert->subjectLLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1747 | } |
wolfSSL | 0:9d17e4342598 | 1748 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 1749 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1750 | dName->lIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1751 | dName->lLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1752 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1753 | } |
wolfSSL | 0:9d17e4342598 | 1754 | else if (id == ASN_STATE_NAME) { |
wolfSSL | 0:9d17e4342598 | 1755 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1756 | XMEMCPY(&full[idx], "/ST=", 4); |
wolfSSL | 0:9d17e4342598 | 1757 | idx += 4; |
wolfSSL | 0:9d17e4342598 | 1758 | copy = TRUE; |
wolfSSL | 0:9d17e4342598 | 1759 | } |
wolfSSL | 0:9d17e4342598 | 1760 | #ifdef CYASSL_CERT_GEN |
wolfSSL | 0:9d17e4342598 | 1761 | if (nameType == SUBJECT) { |
wolfSSL | 0:9d17e4342598 | 1762 | cert->subjectST = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1763 | cert->subjectSTLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1764 | } |
wolfSSL | 0:9d17e4342598 | 1765 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 1766 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1767 | dName->stIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1768 | dName->stLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1769 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1770 | } |
wolfSSL | 0:9d17e4342598 | 1771 | else if (id == ASN_ORG_NAME) { |
wolfSSL | 0:9d17e4342598 | 1772 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1773 | XMEMCPY(&full[idx], "/O=", 3); |
wolfSSL | 0:9d17e4342598 | 1774 | idx += 3; |
wolfSSL | 0:9d17e4342598 | 1775 | copy = TRUE; |
wolfSSL | 0:9d17e4342598 | 1776 | } |
wolfSSL | 0:9d17e4342598 | 1777 | #ifdef CYASSL_CERT_GEN |
wolfSSL | 0:9d17e4342598 | 1778 | if (nameType == SUBJECT) { |
wolfSSL | 0:9d17e4342598 | 1779 | cert->subjectO = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1780 | cert->subjectOLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1781 | } |
wolfSSL | 0:9d17e4342598 | 1782 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 1783 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1784 | dName->oIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1785 | dName->oLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1786 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1787 | } |
wolfSSL | 0:9d17e4342598 | 1788 | else if (id == ASN_ORGUNIT_NAME) { |
wolfSSL | 0:9d17e4342598 | 1789 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1790 | XMEMCPY(&full[idx], "/OU=", 4); |
wolfSSL | 0:9d17e4342598 | 1791 | idx += 4; |
wolfSSL | 0:9d17e4342598 | 1792 | copy = TRUE; |
wolfSSL | 0:9d17e4342598 | 1793 | } |
wolfSSL | 0:9d17e4342598 | 1794 | #ifdef CYASSL_CERT_GEN |
wolfSSL | 0:9d17e4342598 | 1795 | if (nameType == SUBJECT) { |
wolfSSL | 0:9d17e4342598 | 1796 | cert->subjectOU = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1797 | cert->subjectOULen = strLen; |
wolfSSL | 0:9d17e4342598 | 1798 | } |
wolfSSL | 0:9d17e4342598 | 1799 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 1800 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1801 | dName->ouIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1802 | dName->ouLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1803 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1804 | } |
wolfSSL | 0:9d17e4342598 | 1805 | else if (id == ASN_SERIAL_NUMBER) { |
wolfSSL | 0:9d17e4342598 | 1806 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1807 | XMEMCPY(&full[idx], "/serialNumber=", 14); |
wolfSSL | 0:9d17e4342598 | 1808 | idx += 14; |
wolfSSL | 0:9d17e4342598 | 1809 | copy = TRUE; |
wolfSSL | 0:9d17e4342598 | 1810 | } |
wolfSSL | 0:9d17e4342598 | 1811 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1812 | dName->snIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1813 | dName->snLen = strLen; |
wolfSSL | 0:9d17e4342598 | 1814 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1815 | } |
wolfSSL | 0:9d17e4342598 | 1816 | |
wolfSSL | 0:9d17e4342598 | 1817 | if (copy && !tooBig) { |
wolfSSL | 0:9d17e4342598 | 1818 | XMEMCPY(&full[idx], &cert->source[cert->srcIdx], strLen); |
wolfSSL | 0:9d17e4342598 | 1819 | idx += strLen; |
wolfSSL | 0:9d17e4342598 | 1820 | } |
wolfSSL | 0:9d17e4342598 | 1821 | |
wolfSSL | 0:9d17e4342598 | 1822 | cert->srcIdx += strLen; |
wolfSSL | 0:9d17e4342598 | 1823 | } |
wolfSSL | 0:9d17e4342598 | 1824 | else { |
wolfSSL | 0:9d17e4342598 | 1825 | /* skip */ |
wolfSSL | 0:9d17e4342598 | 1826 | byte email = FALSE; |
wolfSSL | 0:9d17e4342598 | 1827 | byte uid = FALSE; |
wolfSSL | 0:9d17e4342598 | 1828 | int adv; |
wolfSSL | 0:9d17e4342598 | 1829 | |
wolfSSL | 0:9d17e4342598 | 1830 | if (joint[0] == 0x2a && joint[1] == 0x86) /* email id hdr */ |
wolfSSL | 0:9d17e4342598 | 1831 | email = TRUE; |
wolfSSL | 0:9d17e4342598 | 1832 | |
wolfSSL | 0:9d17e4342598 | 1833 | if (joint[0] == 0x9 && joint[1] == 0x92) /* uid id hdr */ |
wolfSSL | 0:9d17e4342598 | 1834 | uid = TRUE; |
wolfSSL | 0:9d17e4342598 | 1835 | |
wolfSSL | 0:9d17e4342598 | 1836 | cert->srcIdx += oidSz + 1; |
wolfSSL | 0:9d17e4342598 | 1837 | |
wolfSSL | 0:9d17e4342598 | 1838 | if (GetLength(cert->source, &cert->srcIdx, &adv, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 1839 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 1840 | |
wolfSSL | 0:9d17e4342598 | 1841 | if (adv > (int)(ASN_NAME_MAX - idx)) { |
wolfSSL | 0:9d17e4342598 | 1842 | CYASSL_MSG("ASN name too big, skipping"); |
wolfSSL | 0:9d17e4342598 | 1843 | tooBig = TRUE; |
wolfSSL | 0:9d17e4342598 | 1844 | } |
wolfSSL | 0:9d17e4342598 | 1845 | |
wolfSSL | 0:9d17e4342598 | 1846 | if (email) { |
wolfSSL | 0:9d17e4342598 | 1847 | if ( (14 + adv) > (int)(ASN_NAME_MAX - idx)) { |
wolfSSL | 0:9d17e4342598 | 1848 | CYASSL_MSG("ASN name too big, skipping"); |
wolfSSL | 0:9d17e4342598 | 1849 | tooBig = TRUE; |
wolfSSL | 0:9d17e4342598 | 1850 | } |
wolfSSL | 0:9d17e4342598 | 1851 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1852 | XMEMCPY(&full[idx], "/emailAddress=", 14); |
wolfSSL | 0:9d17e4342598 | 1853 | idx += 14; |
wolfSSL | 0:9d17e4342598 | 1854 | } |
wolfSSL | 0:9d17e4342598 | 1855 | |
wolfSSL | 0:9d17e4342598 | 1856 | #ifdef CYASSL_CERT_GEN |
wolfSSL | 0:9d17e4342598 | 1857 | if (nameType == SUBJECT) { |
wolfSSL | 0:9d17e4342598 | 1858 | cert->subjectEmail = (char*)&cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 1859 | cert->subjectEmailLen = adv; |
wolfSSL | 0:9d17e4342598 | 1860 | } |
wolfSSL | 0:9d17e4342598 | 1861 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 1862 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1863 | dName->emailIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1864 | dName->emailLen = adv; |
wolfSSL | 0:9d17e4342598 | 1865 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1866 | |
wolfSSL | 0:9d17e4342598 | 1867 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1868 | XMEMCPY(&full[idx], &cert->source[cert->srcIdx], adv); |
wolfSSL | 0:9d17e4342598 | 1869 | idx += adv; |
wolfSSL | 0:9d17e4342598 | 1870 | } |
wolfSSL | 0:9d17e4342598 | 1871 | } |
wolfSSL | 0:9d17e4342598 | 1872 | |
wolfSSL | 0:9d17e4342598 | 1873 | if (uid) { |
wolfSSL | 0:9d17e4342598 | 1874 | if ( (5 + adv) > (int)(ASN_NAME_MAX - idx)) { |
wolfSSL | 0:9d17e4342598 | 1875 | CYASSL_MSG("ASN name too big, skipping"); |
wolfSSL | 0:9d17e4342598 | 1876 | tooBig = TRUE; |
wolfSSL | 0:9d17e4342598 | 1877 | } |
wolfSSL | 0:9d17e4342598 | 1878 | if (!tooBig) { |
wolfSSL | 0:9d17e4342598 | 1879 | XMEMCPY(&full[idx], "/UID=", 5); |
wolfSSL | 0:9d17e4342598 | 1880 | idx += 5; |
wolfSSL | 0:9d17e4342598 | 1881 | |
wolfSSL | 0:9d17e4342598 | 1882 | XMEMCPY(&full[idx], &cert->source[cert->srcIdx], adv); |
wolfSSL | 0:9d17e4342598 | 1883 | idx += adv; |
wolfSSL | 0:9d17e4342598 | 1884 | } |
wolfSSL | 0:9d17e4342598 | 1885 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1886 | dName->uidIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 1887 | dName->uidLen = adv; |
wolfSSL | 0:9d17e4342598 | 1888 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 1889 | } |
wolfSSL | 0:9d17e4342598 | 1890 | |
wolfSSL | 0:9d17e4342598 | 1891 | cert->srcIdx += adv; |
wolfSSL | 0:9d17e4342598 | 1892 | } |
wolfSSL | 0:9d17e4342598 | 1893 | } |
wolfSSL | 0:9d17e4342598 | 1894 | full[idx++] = 0; |
wolfSSL | 0:9d17e4342598 | 1895 | |
wolfSSL | 0:9d17e4342598 | 1896 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 1897 | { |
wolfSSL | 0:9d17e4342598 | 1898 | int totalLen = 0; |
wolfSSL | 0:9d17e4342598 | 1899 | |
wolfSSL | 0:9d17e4342598 | 1900 | if (dName->cnLen != 0) |
wolfSSL | 0:9d17e4342598 | 1901 | totalLen += dName->cnLen + 4; |
wolfSSL | 0:9d17e4342598 | 1902 | if (dName->snLen != 0) |
wolfSSL | 0:9d17e4342598 | 1903 | totalLen += dName->snLen + 4; |
wolfSSL | 0:9d17e4342598 | 1904 | if (dName->cLen != 0) |
wolfSSL | 0:9d17e4342598 | 1905 | totalLen += dName->cLen + 3; |
wolfSSL | 0:9d17e4342598 | 1906 | if (dName->lLen != 0) |
wolfSSL | 0:9d17e4342598 | 1907 | totalLen += dName->lLen + 3; |
wolfSSL | 0:9d17e4342598 | 1908 | if (dName->stLen != 0) |
wolfSSL | 0:9d17e4342598 | 1909 | totalLen += dName->stLen + 4; |
wolfSSL | 0:9d17e4342598 | 1910 | if (dName->oLen != 0) |
wolfSSL | 0:9d17e4342598 | 1911 | totalLen += dName->oLen + 3; |
wolfSSL | 0:9d17e4342598 | 1912 | if (dName->ouLen != 0) |
wolfSSL | 0:9d17e4342598 | 1913 | totalLen += dName->ouLen + 4; |
wolfSSL | 0:9d17e4342598 | 1914 | if (dName->emailLen != 0) |
wolfSSL | 0:9d17e4342598 | 1915 | totalLen += dName->emailLen + 14; |
wolfSSL | 0:9d17e4342598 | 1916 | if (dName->uidLen != 0) |
wolfSSL | 0:9d17e4342598 | 1917 | totalLen += dName->uidLen + 5; |
wolfSSL | 0:9d17e4342598 | 1918 | if (dName->serialLen != 0) |
wolfSSL | 0:9d17e4342598 | 1919 | totalLen += dName->serialLen + 14; |
wolfSSL | 0:9d17e4342598 | 1920 | |
wolfSSL | 0:9d17e4342598 | 1921 | dName->fullName = (char*)XMALLOC(totalLen + 1, NULL, DYNAMIC_TYPE_X509); |
wolfSSL | 0:9d17e4342598 | 1922 | if (dName->fullName != NULL) { |
wolfSSL | 0:9d17e4342598 | 1923 | idx = 0; |
wolfSSL | 0:9d17e4342598 | 1924 | |
wolfSSL | 0:9d17e4342598 | 1925 | if (dName->cnLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1926 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1927 | XMEMCPY(&dName->fullName[idx], "/CN=", 4); |
wolfSSL | 0:9d17e4342598 | 1928 | idx += 4; |
wolfSSL | 0:9d17e4342598 | 1929 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 1930 | &cert->source[dName->cnIdx], dName->cnLen); |
wolfSSL | 0:9d17e4342598 | 1931 | dName->cnIdx = idx; |
wolfSSL | 0:9d17e4342598 | 1932 | idx += dName->cnLen; |
wolfSSL | 0:9d17e4342598 | 1933 | } |
wolfSSL | 0:9d17e4342598 | 1934 | if (dName->snLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1935 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1936 | XMEMCPY(&dName->fullName[idx], "/SN=", 4); |
wolfSSL | 0:9d17e4342598 | 1937 | idx += 4; |
wolfSSL | 0:9d17e4342598 | 1938 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 1939 | &cert->source[dName->snIdx], dName->snLen); |
wolfSSL | 0:9d17e4342598 | 1940 | dName->snIdx = idx; |
wolfSSL | 0:9d17e4342598 | 1941 | idx += dName->snLen; |
wolfSSL | 0:9d17e4342598 | 1942 | } |
wolfSSL | 0:9d17e4342598 | 1943 | if (dName->cLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1944 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1945 | XMEMCPY(&dName->fullName[idx], "/C=", 3); |
wolfSSL | 0:9d17e4342598 | 1946 | idx += 3; |
wolfSSL | 0:9d17e4342598 | 1947 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 1948 | &cert->source[dName->cIdx], dName->cLen); |
wolfSSL | 0:9d17e4342598 | 1949 | dName->cIdx = idx; |
wolfSSL | 0:9d17e4342598 | 1950 | idx += dName->cLen; |
wolfSSL | 0:9d17e4342598 | 1951 | } |
wolfSSL | 0:9d17e4342598 | 1952 | if (dName->lLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1953 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1954 | XMEMCPY(&dName->fullName[idx], "/L=", 3); |
wolfSSL | 0:9d17e4342598 | 1955 | idx += 3; |
wolfSSL | 0:9d17e4342598 | 1956 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 1957 | &cert->source[dName->lIdx], dName->lLen); |
wolfSSL | 0:9d17e4342598 | 1958 | dName->lIdx = idx; |
wolfSSL | 0:9d17e4342598 | 1959 | idx += dName->lLen; |
wolfSSL | 0:9d17e4342598 | 1960 | } |
wolfSSL | 0:9d17e4342598 | 1961 | if (dName->stLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1962 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1963 | XMEMCPY(&dName->fullName[idx], "/ST=", 4); |
wolfSSL | 0:9d17e4342598 | 1964 | idx += 4; |
wolfSSL | 0:9d17e4342598 | 1965 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 1966 | &cert->source[dName->stIdx], dName->stLen); |
wolfSSL | 0:9d17e4342598 | 1967 | dName->stIdx = idx; |
wolfSSL | 0:9d17e4342598 | 1968 | idx += dName->stLen; |
wolfSSL | 0:9d17e4342598 | 1969 | } |
wolfSSL | 0:9d17e4342598 | 1970 | if (dName->oLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1971 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1972 | XMEMCPY(&dName->fullName[idx], "/O=", 3); |
wolfSSL | 0:9d17e4342598 | 1973 | idx += 3; |
wolfSSL | 0:9d17e4342598 | 1974 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 1975 | &cert->source[dName->oIdx], dName->oLen); |
wolfSSL | 0:9d17e4342598 | 1976 | dName->oIdx = idx; |
wolfSSL | 0:9d17e4342598 | 1977 | idx += dName->oLen; |
wolfSSL | 0:9d17e4342598 | 1978 | } |
wolfSSL | 0:9d17e4342598 | 1979 | if (dName->ouLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1980 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1981 | XMEMCPY(&dName->fullName[idx], "/OU=", 4); |
wolfSSL | 0:9d17e4342598 | 1982 | idx += 4; |
wolfSSL | 0:9d17e4342598 | 1983 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 1984 | &cert->source[dName->ouIdx], dName->ouLen); |
wolfSSL | 0:9d17e4342598 | 1985 | dName->ouIdx = idx; |
wolfSSL | 0:9d17e4342598 | 1986 | idx += dName->ouLen; |
wolfSSL | 0:9d17e4342598 | 1987 | } |
wolfSSL | 0:9d17e4342598 | 1988 | if (dName->emailLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1989 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1990 | XMEMCPY(&dName->fullName[idx], "/emailAddress=", 14); |
wolfSSL | 0:9d17e4342598 | 1991 | idx += 14; |
wolfSSL | 0:9d17e4342598 | 1992 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 1993 | &cert->source[dName->emailIdx], dName->emailLen); |
wolfSSL | 0:9d17e4342598 | 1994 | dName->emailIdx = idx; |
wolfSSL | 0:9d17e4342598 | 1995 | idx += dName->emailLen; |
wolfSSL | 0:9d17e4342598 | 1996 | } |
wolfSSL | 0:9d17e4342598 | 1997 | if (dName->uidLen != 0) { |
wolfSSL | 0:9d17e4342598 | 1998 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 1999 | XMEMCPY(&dName->fullName[idx], "/UID=", 5); |
wolfSSL | 0:9d17e4342598 | 2000 | idx += 5; |
wolfSSL | 0:9d17e4342598 | 2001 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 2002 | &cert->source[dName->uidIdx], dName->uidLen); |
wolfSSL | 0:9d17e4342598 | 2003 | dName->uidIdx = idx; |
wolfSSL | 0:9d17e4342598 | 2004 | idx += dName->uidLen; |
wolfSSL | 0:9d17e4342598 | 2005 | } |
wolfSSL | 0:9d17e4342598 | 2006 | if (dName->serialLen != 0) { |
wolfSSL | 0:9d17e4342598 | 2007 | dName->entryCount++; |
wolfSSL | 0:9d17e4342598 | 2008 | XMEMCPY(&dName->fullName[idx], "/serialNumber=", 14); |
wolfSSL | 0:9d17e4342598 | 2009 | idx += 14; |
wolfSSL | 0:9d17e4342598 | 2010 | XMEMCPY(&dName->fullName[idx], |
wolfSSL | 0:9d17e4342598 | 2011 | &cert->source[dName->serialIdx], dName->serialLen); |
wolfSSL | 0:9d17e4342598 | 2012 | dName->serialIdx = idx; |
wolfSSL | 0:9d17e4342598 | 2013 | idx += dName->serialLen; |
wolfSSL | 0:9d17e4342598 | 2014 | } |
wolfSSL | 0:9d17e4342598 | 2015 | dName->fullName[idx] = '\0'; |
wolfSSL | 0:9d17e4342598 | 2016 | dName->fullNameLen = totalLen; |
wolfSSL | 0:9d17e4342598 | 2017 | } |
wolfSSL | 0:9d17e4342598 | 2018 | } |
wolfSSL | 0:9d17e4342598 | 2019 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 2020 | |
wolfSSL | 0:9d17e4342598 | 2021 | return 0; |
wolfSSL | 0:9d17e4342598 | 2022 | } |
wolfSSL | 0:9d17e4342598 | 2023 | |
wolfSSL | 0:9d17e4342598 | 2024 | |
wolfSSL | 0:9d17e4342598 | 2025 | #ifndef NO_TIME_H |
wolfSSL | 0:9d17e4342598 | 2026 | |
wolfSSL | 0:9d17e4342598 | 2027 | /* to the second */ |
wolfSSL | 0:9d17e4342598 | 2028 | static int DateGreaterThan(const struct tm* a, const struct tm* b) |
wolfSSL | 0:9d17e4342598 | 2029 | { |
wolfSSL | 0:9d17e4342598 | 2030 | if (a->tm_year > b->tm_year) |
wolfSSL | 0:9d17e4342598 | 2031 | return 1; |
wolfSSL | 0:9d17e4342598 | 2032 | |
wolfSSL | 0:9d17e4342598 | 2033 | if (a->tm_year == b->tm_year && a->tm_mon > b->tm_mon) |
wolfSSL | 0:9d17e4342598 | 2034 | return 1; |
wolfSSL | 0:9d17e4342598 | 2035 | |
wolfSSL | 0:9d17e4342598 | 2036 | if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && |
wolfSSL | 0:9d17e4342598 | 2037 | a->tm_mday > b->tm_mday) |
wolfSSL | 0:9d17e4342598 | 2038 | return 1; |
wolfSSL | 0:9d17e4342598 | 2039 | |
wolfSSL | 0:9d17e4342598 | 2040 | if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && |
wolfSSL | 0:9d17e4342598 | 2041 | a->tm_mday == b->tm_mday && a->tm_hour > b->tm_hour) |
wolfSSL | 0:9d17e4342598 | 2042 | return 1; |
wolfSSL | 0:9d17e4342598 | 2043 | |
wolfSSL | 0:9d17e4342598 | 2044 | if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && |
wolfSSL | 0:9d17e4342598 | 2045 | a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour && |
wolfSSL | 0:9d17e4342598 | 2046 | a->tm_min > b->tm_min) |
wolfSSL | 0:9d17e4342598 | 2047 | return 1; |
wolfSSL | 0:9d17e4342598 | 2048 | |
wolfSSL | 0:9d17e4342598 | 2049 | if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon && |
wolfSSL | 0:9d17e4342598 | 2050 | a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour && |
wolfSSL | 0:9d17e4342598 | 2051 | a->tm_min == b->tm_min && a->tm_sec > b->tm_sec) |
wolfSSL | 0:9d17e4342598 | 2052 | return 1; |
wolfSSL | 0:9d17e4342598 | 2053 | |
wolfSSL | 0:9d17e4342598 | 2054 | return 0; /* false */ |
wolfSSL | 0:9d17e4342598 | 2055 | } |
wolfSSL | 0:9d17e4342598 | 2056 | |
wolfSSL | 0:9d17e4342598 | 2057 | |
wolfSSL | 0:9d17e4342598 | 2058 | static INLINE int DateLessThan(const struct tm* a, const struct tm* b) |
wolfSSL | 0:9d17e4342598 | 2059 | { |
wolfSSL | 0:9d17e4342598 | 2060 | return !DateGreaterThan(a,b); |
wolfSSL | 0:9d17e4342598 | 2061 | } |
wolfSSL | 0:9d17e4342598 | 2062 | |
wolfSSL | 0:9d17e4342598 | 2063 | |
wolfSSL | 0:9d17e4342598 | 2064 | /* like atoi but only use first byte */ |
wolfSSL | 0:9d17e4342598 | 2065 | /* Make sure before and after dates are valid */ |
wolfSSL | 0:9d17e4342598 | 2066 | int ValidateDate(const byte* date, byte format, int dateType) |
wolfSSL | 0:9d17e4342598 | 2067 | { |
wolfSSL | 0:9d17e4342598 | 2068 | time_t ltime; |
wolfSSL | 0:9d17e4342598 | 2069 | struct tm certTime; |
wolfSSL | 0:9d17e4342598 | 2070 | struct tm* localTime; |
wolfSSL | 0:9d17e4342598 | 2071 | int i = 0; |
wolfSSL | 0:9d17e4342598 | 2072 | |
wolfSSL | 0:9d17e4342598 | 2073 | ltime = XTIME(0); |
wolfSSL | 0:9d17e4342598 | 2074 | XMEMSET(&certTime, 0, sizeof(certTime)); |
wolfSSL | 0:9d17e4342598 | 2075 | |
wolfSSL | 0:9d17e4342598 | 2076 | if (format == ASN_UTC_TIME) { |
wolfSSL | 0:9d17e4342598 | 2077 | if (btoi(date[0]) >= 5) |
wolfSSL | 0:9d17e4342598 | 2078 | certTime.tm_year = 1900; |
wolfSSL | 0:9d17e4342598 | 2079 | else |
wolfSSL | 0:9d17e4342598 | 2080 | certTime.tm_year = 2000; |
wolfSSL | 0:9d17e4342598 | 2081 | } |
wolfSSL | 0:9d17e4342598 | 2082 | else { /* format == GENERALIZED_TIME */ |
wolfSSL | 0:9d17e4342598 | 2083 | certTime.tm_year += btoi(date[i++]) * 1000; |
wolfSSL | 0:9d17e4342598 | 2084 | certTime.tm_year += btoi(date[i++]) * 100; |
wolfSSL | 0:9d17e4342598 | 2085 | } |
wolfSSL | 0:9d17e4342598 | 2086 | |
wolfSSL | 0:9d17e4342598 | 2087 | GetTime(&certTime.tm_year, date, &i); certTime.tm_year -= 1900; /* adjust */ |
wolfSSL | 0:9d17e4342598 | 2088 | GetTime(&certTime.tm_mon, date, &i); certTime.tm_mon -= 1; /* adjust */ |
wolfSSL | 0:9d17e4342598 | 2089 | GetTime(&certTime.tm_mday, date, &i); |
wolfSSL | 0:9d17e4342598 | 2090 | GetTime(&certTime.tm_hour, date, &i); |
wolfSSL | 0:9d17e4342598 | 2091 | GetTime(&certTime.tm_min, date, &i); |
wolfSSL | 0:9d17e4342598 | 2092 | GetTime(&certTime.tm_sec, date, &i); |
wolfSSL | 0:9d17e4342598 | 2093 | |
wolfSSL | 0:9d17e4342598 | 2094 | if (date[i] != 'Z') { /* only Zulu supported for this profile */ |
wolfSSL | 0:9d17e4342598 | 2095 | CYASSL_MSG("Only Zulu time supported for this profile"); |
wolfSSL | 0:9d17e4342598 | 2096 | return 0; |
wolfSSL | 0:9d17e4342598 | 2097 | } |
wolfSSL | 0:9d17e4342598 | 2098 | |
wolfSSL | 0:9d17e4342598 | 2099 | localTime = XGMTIME(<ime); |
wolfSSL | 0:9d17e4342598 | 2100 | |
wolfSSL | 0:9d17e4342598 | 2101 | if (dateType == BEFORE) { |
wolfSSL | 0:9d17e4342598 | 2102 | if (DateLessThan(localTime, &certTime)) |
wolfSSL | 0:9d17e4342598 | 2103 | return 0; |
wolfSSL | 0:9d17e4342598 | 2104 | } |
wolfSSL | 0:9d17e4342598 | 2105 | else |
wolfSSL | 0:9d17e4342598 | 2106 | if (DateGreaterThan(localTime, &certTime)) |
wolfSSL | 0:9d17e4342598 | 2107 | return 0; |
wolfSSL | 0:9d17e4342598 | 2108 | |
wolfSSL | 0:9d17e4342598 | 2109 | return 1; |
wolfSSL | 0:9d17e4342598 | 2110 | } |
wolfSSL | 0:9d17e4342598 | 2111 | |
wolfSSL | 0:9d17e4342598 | 2112 | #endif /* NO_TIME_H */ |
wolfSSL | 0:9d17e4342598 | 2113 | |
wolfSSL | 0:9d17e4342598 | 2114 | |
wolfSSL | 0:9d17e4342598 | 2115 | static int GetDate(DecodedCert* cert, int dateType) |
wolfSSL | 0:9d17e4342598 | 2116 | { |
wolfSSL | 0:9d17e4342598 | 2117 | int length; |
wolfSSL | 0:9d17e4342598 | 2118 | byte date[MAX_DATE_SIZE]; |
wolfSSL | 0:9d17e4342598 | 2119 | byte b; |
wolfSSL | 0:9d17e4342598 | 2120 | word32 startIdx = 0; |
wolfSSL | 0:9d17e4342598 | 2121 | |
wolfSSL | 0:9d17e4342598 | 2122 | if (dateType == BEFORE) |
wolfSSL | 0:9d17e4342598 | 2123 | cert->beforeDate = &cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 2124 | else |
wolfSSL | 0:9d17e4342598 | 2125 | cert->afterDate = &cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 2126 | startIdx = cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 2127 | |
wolfSSL | 0:9d17e4342598 | 2128 | b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 2129 | if (b != ASN_UTC_TIME && b != ASN_GENERALIZED_TIME) |
wolfSSL | 0:9d17e4342598 | 2130 | return ASN_TIME_E; |
wolfSSL | 0:9d17e4342598 | 2131 | |
wolfSSL | 0:9d17e4342598 | 2132 | if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 2133 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2134 | |
wolfSSL | 0:9d17e4342598 | 2135 | if (length > MAX_DATE_SIZE || length < MIN_DATE_SIZE) |
wolfSSL | 0:9d17e4342598 | 2136 | return ASN_DATE_SZ_E; |
wolfSSL | 0:9d17e4342598 | 2137 | |
wolfSSL | 0:9d17e4342598 | 2138 | XMEMCPY(date, &cert->source[cert->srcIdx], length); |
wolfSSL | 0:9d17e4342598 | 2139 | cert->srcIdx += length; |
wolfSSL | 0:9d17e4342598 | 2140 | |
wolfSSL | 0:9d17e4342598 | 2141 | if (dateType == BEFORE) |
wolfSSL | 0:9d17e4342598 | 2142 | cert->beforeDateLen = cert->srcIdx - startIdx; |
wolfSSL | 0:9d17e4342598 | 2143 | else |
wolfSSL | 0:9d17e4342598 | 2144 | cert->afterDateLen = cert->srcIdx - startIdx; |
wolfSSL | 0:9d17e4342598 | 2145 | |
wolfSSL | 0:9d17e4342598 | 2146 | if (!XVALIDATE_DATE(date, b, dateType)) { |
wolfSSL | 0:9d17e4342598 | 2147 | if (dateType == BEFORE) |
wolfSSL | 0:9d17e4342598 | 2148 | return ASN_BEFORE_DATE_E; |
wolfSSL | 0:9d17e4342598 | 2149 | else |
wolfSSL | 0:9d17e4342598 | 2150 | return ASN_AFTER_DATE_E; |
wolfSSL | 0:9d17e4342598 | 2151 | } |
wolfSSL | 0:9d17e4342598 | 2152 | |
wolfSSL | 0:9d17e4342598 | 2153 | return 0; |
wolfSSL | 0:9d17e4342598 | 2154 | } |
wolfSSL | 0:9d17e4342598 | 2155 | |
wolfSSL | 0:9d17e4342598 | 2156 | |
wolfSSL | 0:9d17e4342598 | 2157 | static int GetValidity(DecodedCert* cert, int verify) |
wolfSSL | 0:9d17e4342598 | 2158 | { |
wolfSSL | 0:9d17e4342598 | 2159 | int length; |
wolfSSL | 0:9d17e4342598 | 2160 | int badDate = 0; |
wolfSSL | 0:9d17e4342598 | 2161 | |
wolfSSL | 0:9d17e4342598 | 2162 | if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 2163 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2164 | |
wolfSSL | 0:9d17e4342598 | 2165 | if (GetDate(cert, BEFORE) < 0 && verify) |
wolfSSL | 0:9d17e4342598 | 2166 | badDate = ASN_BEFORE_DATE_E; /* continue parsing */ |
wolfSSL | 0:9d17e4342598 | 2167 | |
wolfSSL | 0:9d17e4342598 | 2168 | if (GetDate(cert, AFTER) < 0 && verify) |
wolfSSL | 0:9d17e4342598 | 2169 | return ASN_AFTER_DATE_E; |
wolfSSL | 0:9d17e4342598 | 2170 | |
wolfSSL | 0:9d17e4342598 | 2171 | if (badDate != 0) |
wolfSSL | 0:9d17e4342598 | 2172 | return badDate; |
wolfSSL | 0:9d17e4342598 | 2173 | |
wolfSSL | 0:9d17e4342598 | 2174 | return 0; |
wolfSSL | 0:9d17e4342598 | 2175 | } |
wolfSSL | 0:9d17e4342598 | 2176 | |
wolfSSL | 0:9d17e4342598 | 2177 | |
wolfSSL | 0:9d17e4342598 | 2178 | int DecodeToKey(DecodedCert* cert, int verify) |
wolfSSL | 0:9d17e4342598 | 2179 | { |
wolfSSL | 0:9d17e4342598 | 2180 | int badDate = 0; |
wolfSSL | 0:9d17e4342598 | 2181 | int ret; |
wolfSSL | 0:9d17e4342598 | 2182 | |
wolfSSL | 0:9d17e4342598 | 2183 | if ( (ret = GetCertHeader(cert)) < 0) |
wolfSSL | 0:9d17e4342598 | 2184 | return ret; |
wolfSSL | 0:9d17e4342598 | 2185 | |
wolfSSL | 0:9d17e4342598 | 2186 | CYASSL_MSG("Got Cert Header"); |
wolfSSL | 0:9d17e4342598 | 2187 | |
wolfSSL | 0:9d17e4342598 | 2188 | if ( (ret = GetAlgoId(cert->source, &cert->srcIdx, &cert->signatureOID, |
wolfSSL | 0:9d17e4342598 | 2189 | cert->maxIdx)) < 0) |
wolfSSL | 0:9d17e4342598 | 2190 | return ret; |
wolfSSL | 0:9d17e4342598 | 2191 | |
wolfSSL | 0:9d17e4342598 | 2192 | CYASSL_MSG("Got Algo ID"); |
wolfSSL | 0:9d17e4342598 | 2193 | |
wolfSSL | 0:9d17e4342598 | 2194 | if ( (ret = GetName(cert, ISSUER)) < 0) |
wolfSSL | 0:9d17e4342598 | 2195 | return ret; |
wolfSSL | 0:9d17e4342598 | 2196 | |
wolfSSL | 0:9d17e4342598 | 2197 | if ( (ret = GetValidity(cert, verify)) < 0) |
wolfSSL | 0:9d17e4342598 | 2198 | badDate = ret; |
wolfSSL | 0:9d17e4342598 | 2199 | |
wolfSSL | 0:9d17e4342598 | 2200 | if ( (ret = GetName(cert, SUBJECT)) < 0) |
wolfSSL | 0:9d17e4342598 | 2201 | return ret; |
wolfSSL | 0:9d17e4342598 | 2202 | |
wolfSSL | 0:9d17e4342598 | 2203 | CYASSL_MSG("Got Subject Name"); |
wolfSSL | 0:9d17e4342598 | 2204 | |
wolfSSL | 0:9d17e4342598 | 2205 | if ( (ret = GetKey(cert)) < 0) |
wolfSSL | 0:9d17e4342598 | 2206 | return ret; |
wolfSSL | 0:9d17e4342598 | 2207 | |
wolfSSL | 0:9d17e4342598 | 2208 | CYASSL_MSG("Got Key"); |
wolfSSL | 0:9d17e4342598 | 2209 | |
wolfSSL | 0:9d17e4342598 | 2210 | if (badDate != 0) |
wolfSSL | 0:9d17e4342598 | 2211 | return badDate; |
wolfSSL | 0:9d17e4342598 | 2212 | |
wolfSSL | 0:9d17e4342598 | 2213 | return ret; |
wolfSSL | 0:9d17e4342598 | 2214 | } |
wolfSSL | 0:9d17e4342598 | 2215 | |
wolfSSL | 0:9d17e4342598 | 2216 | |
wolfSSL | 0:9d17e4342598 | 2217 | static int GetSignature(DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 2218 | { |
wolfSSL | 0:9d17e4342598 | 2219 | int length; |
wolfSSL | 0:9d17e4342598 | 2220 | byte b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 2221 | |
wolfSSL | 0:9d17e4342598 | 2222 | if (b != ASN_BIT_STRING) |
wolfSSL | 0:9d17e4342598 | 2223 | return ASN_BITSTR_E; |
wolfSSL | 0:9d17e4342598 | 2224 | |
wolfSSL | 0:9d17e4342598 | 2225 | if (GetLength(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 2226 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2227 | |
wolfSSL | 0:9d17e4342598 | 2228 | cert->sigLength = length; |
wolfSSL | 0:9d17e4342598 | 2229 | |
wolfSSL | 0:9d17e4342598 | 2230 | b = cert->source[cert->srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 2231 | if (b != 0x00) |
wolfSSL | 0:9d17e4342598 | 2232 | return ASN_EXPECT_0_E; |
wolfSSL | 0:9d17e4342598 | 2233 | |
wolfSSL | 0:9d17e4342598 | 2234 | cert->sigLength--; |
wolfSSL | 0:9d17e4342598 | 2235 | cert->signature = &cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 2236 | cert->srcIdx += cert->sigLength; |
wolfSSL | 0:9d17e4342598 | 2237 | |
wolfSSL | 0:9d17e4342598 | 2238 | return 0; |
wolfSSL | 0:9d17e4342598 | 2239 | } |
wolfSSL | 0:9d17e4342598 | 2240 | |
wolfSSL | 0:9d17e4342598 | 2241 | |
wolfSSL | 0:9d17e4342598 | 2242 | static word32 SetDigest(const byte* digest, word32 digSz, byte* output) |
wolfSSL | 0:9d17e4342598 | 2243 | { |
wolfSSL | 0:9d17e4342598 | 2244 | output[0] = ASN_OCTET_STRING; |
wolfSSL | 0:9d17e4342598 | 2245 | output[1] = (byte)digSz; |
wolfSSL | 0:9d17e4342598 | 2246 | XMEMCPY(&output[2], digest, digSz); |
wolfSSL | 0:9d17e4342598 | 2247 | |
wolfSSL | 0:9d17e4342598 | 2248 | return digSz + 2; |
wolfSSL | 0:9d17e4342598 | 2249 | } |
wolfSSL | 0:9d17e4342598 | 2250 | |
wolfSSL | 0:9d17e4342598 | 2251 | |
wolfSSL | 0:9d17e4342598 | 2252 | static word32 BytePrecision(word32 value) |
wolfSSL | 0:9d17e4342598 | 2253 | { |
wolfSSL | 0:9d17e4342598 | 2254 | word32 i; |
wolfSSL | 0:9d17e4342598 | 2255 | for (i = sizeof(value); i; --i) |
wolfSSL | 0:9d17e4342598 | 2256 | if (value >> ((i - 1) * CYASSL_BIT_SIZE)) |
wolfSSL | 0:9d17e4342598 | 2257 | break; |
wolfSSL | 0:9d17e4342598 | 2258 | |
wolfSSL | 0:9d17e4342598 | 2259 | return i; |
wolfSSL | 0:9d17e4342598 | 2260 | } |
wolfSSL | 0:9d17e4342598 | 2261 | |
wolfSSL | 0:9d17e4342598 | 2262 | |
wolfSSL | 0:9d17e4342598 | 2263 | CYASSL_LOCAL word32 SetLength(word32 length, byte* output) |
wolfSSL | 0:9d17e4342598 | 2264 | { |
wolfSSL | 0:9d17e4342598 | 2265 | word32 i = 0, j; |
wolfSSL | 0:9d17e4342598 | 2266 | |
wolfSSL | 0:9d17e4342598 | 2267 | if (length < ASN_LONG_LENGTH) |
wolfSSL | 0:9d17e4342598 | 2268 | output[i++] = (byte)length; |
wolfSSL | 0:9d17e4342598 | 2269 | else { |
wolfSSL | 0:9d17e4342598 | 2270 | output[i++] = (byte)(BytePrecision(length) | ASN_LONG_LENGTH); |
wolfSSL | 0:9d17e4342598 | 2271 | |
wolfSSL | 0:9d17e4342598 | 2272 | for (j = BytePrecision(length); j; --j) { |
wolfSSL | 0:9d17e4342598 | 2273 | output[i] = (byte)(length >> ((j - 1) * CYASSL_BIT_SIZE)); |
wolfSSL | 0:9d17e4342598 | 2274 | i++; |
wolfSSL | 0:9d17e4342598 | 2275 | } |
wolfSSL | 0:9d17e4342598 | 2276 | } |
wolfSSL | 0:9d17e4342598 | 2277 | |
wolfSSL | 0:9d17e4342598 | 2278 | return i; |
wolfSSL | 0:9d17e4342598 | 2279 | } |
wolfSSL | 0:9d17e4342598 | 2280 | |
wolfSSL | 0:9d17e4342598 | 2281 | |
wolfSSL | 0:9d17e4342598 | 2282 | CYASSL_LOCAL word32 SetSequence(word32 len, byte* output) |
wolfSSL | 0:9d17e4342598 | 2283 | { |
wolfSSL | 0:9d17e4342598 | 2284 | output[0] = ASN_SEQUENCE | ASN_CONSTRUCTED; |
wolfSSL | 0:9d17e4342598 | 2285 | return SetLength(len, output + 1) + 1; |
wolfSSL | 0:9d17e4342598 | 2286 | } |
wolfSSL | 0:9d17e4342598 | 2287 | |
wolfSSL | 0:9d17e4342598 | 2288 | CYASSL_LOCAL word32 SetOctetString(word32 len, byte* output) |
wolfSSL | 0:9d17e4342598 | 2289 | { |
wolfSSL | 0:9d17e4342598 | 2290 | output[0] = ASN_OCTET_STRING; |
wolfSSL | 0:9d17e4342598 | 2291 | return SetLength(len, output + 1) + 1; |
wolfSSL | 0:9d17e4342598 | 2292 | } |
wolfSSL | 0:9d17e4342598 | 2293 | |
wolfSSL | 0:9d17e4342598 | 2294 | /* Write a set header to output */ |
wolfSSL | 0:9d17e4342598 | 2295 | CYASSL_LOCAL word32 SetSet(word32 len, byte* output) |
wolfSSL | 0:9d17e4342598 | 2296 | { |
wolfSSL | 0:9d17e4342598 | 2297 | output[0] = ASN_SET | ASN_CONSTRUCTED; |
wolfSSL | 0:9d17e4342598 | 2298 | return SetLength(len, output + 1) + 1; |
wolfSSL | 0:9d17e4342598 | 2299 | } |
wolfSSL | 0:9d17e4342598 | 2300 | |
wolfSSL | 0:9d17e4342598 | 2301 | CYASSL_LOCAL word32 SetImplicit(byte tag, byte number, word32 len, byte* output) |
wolfSSL | 0:9d17e4342598 | 2302 | { |
wolfSSL | 0:9d17e4342598 | 2303 | |
wolfSSL | 0:9d17e4342598 | 2304 | output[0] = ((tag == ASN_SEQUENCE || tag == ASN_SET) ? ASN_CONSTRUCTED : 0) |
wolfSSL | 0:9d17e4342598 | 2305 | | ASN_CONTEXT_SPECIFIC | number; |
wolfSSL | 0:9d17e4342598 | 2306 | return SetLength(len, output + 1) + 1; |
wolfSSL | 0:9d17e4342598 | 2307 | } |
wolfSSL | 0:9d17e4342598 | 2308 | |
wolfSSL | 0:9d17e4342598 | 2309 | CYASSL_LOCAL word32 SetExplicit(byte number, word32 len, byte* output) |
wolfSSL | 0:9d17e4342598 | 2310 | { |
wolfSSL | 0:9d17e4342598 | 2311 | output[0] = ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | number; |
wolfSSL | 0:9d17e4342598 | 2312 | return SetLength(len, output + 1) + 1; |
wolfSSL | 0:9d17e4342598 | 2313 | } |
wolfSSL | 0:9d17e4342598 | 2314 | |
wolfSSL | 0:9d17e4342598 | 2315 | |
wolfSSL | 0:9d17e4342598 | 2316 | #if defined(HAVE_ECC) && defined(CYASSL_CERT_GEN) |
wolfSSL | 0:9d17e4342598 | 2317 | |
wolfSSL | 0:9d17e4342598 | 2318 | static word32 SetCurve(ecc_key* key, byte* output) |
wolfSSL | 0:9d17e4342598 | 2319 | { |
wolfSSL | 0:9d17e4342598 | 2320 | |
wolfSSL | 0:9d17e4342598 | 2321 | /* curve types */ |
wolfSSL | 0:9d17e4342598 | 2322 | static const byte ECC_192v1_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE, 0x3d, |
wolfSSL | 0:9d17e4342598 | 2323 | 0x03, 0x01, 0x01}; |
wolfSSL | 0:9d17e4342598 | 2324 | static const byte ECC_256v1_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE, 0x3d, |
wolfSSL | 0:9d17e4342598 | 2325 | 0x03, 0x01, 0x07}; |
wolfSSL | 0:9d17e4342598 | 2326 | static const byte ECC_160r1_AlgoID[] = { 0x2b, 0x81, 0x04, 0x00, |
wolfSSL | 0:9d17e4342598 | 2327 | 0x02}; |
wolfSSL | 0:9d17e4342598 | 2328 | static const byte ECC_224r1_AlgoID[] = { 0x2b, 0x81, 0x04, 0x00, |
wolfSSL | 0:9d17e4342598 | 2329 | 0x21}; |
wolfSSL | 0:9d17e4342598 | 2330 | static const byte ECC_384r1_AlgoID[] = { 0x2b, 0x81, 0x04, 0x00, |
wolfSSL | 0:9d17e4342598 | 2331 | 0x22}; |
wolfSSL | 0:9d17e4342598 | 2332 | static const byte ECC_521r1_AlgoID[] = { 0x2b, 0x81, 0x04, 0x00, |
wolfSSL | 0:9d17e4342598 | 2333 | 0x23}; |
wolfSSL | 0:9d17e4342598 | 2334 | |
wolfSSL | 0:9d17e4342598 | 2335 | int oidSz = 0; |
wolfSSL | 0:9d17e4342598 | 2336 | int idx = 0; |
wolfSSL | 0:9d17e4342598 | 2337 | int lenSz = 0; |
wolfSSL | 0:9d17e4342598 | 2338 | const byte* oid = 0; |
wolfSSL | 0:9d17e4342598 | 2339 | |
wolfSSL | 0:9d17e4342598 | 2340 | output[0] = ASN_OBJECT_ID; |
wolfSSL | 0:9d17e4342598 | 2341 | idx++; |
wolfSSL | 0:9d17e4342598 | 2342 | |
wolfSSL | 0:9d17e4342598 | 2343 | switch (key->dp->size) { |
wolfSSL | 0:9d17e4342598 | 2344 | case 20: |
wolfSSL | 0:9d17e4342598 | 2345 | oidSz = sizeof(ECC_160r1_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2346 | oid = ECC_160r1_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2347 | break; |
wolfSSL | 0:9d17e4342598 | 2348 | |
wolfSSL | 0:9d17e4342598 | 2349 | case 24: |
wolfSSL | 0:9d17e4342598 | 2350 | oidSz = sizeof(ECC_192v1_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2351 | oid = ECC_192v1_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2352 | break; |
wolfSSL | 0:9d17e4342598 | 2353 | |
wolfSSL | 0:9d17e4342598 | 2354 | case 28: |
wolfSSL | 0:9d17e4342598 | 2355 | oidSz = sizeof(ECC_224r1_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2356 | oid = ECC_224r1_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2357 | break; |
wolfSSL | 0:9d17e4342598 | 2358 | |
wolfSSL | 0:9d17e4342598 | 2359 | case 32: |
wolfSSL | 0:9d17e4342598 | 2360 | oidSz = sizeof(ECC_256v1_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2361 | oid = ECC_256v1_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2362 | break; |
wolfSSL | 0:9d17e4342598 | 2363 | |
wolfSSL | 0:9d17e4342598 | 2364 | case 48: |
wolfSSL | 0:9d17e4342598 | 2365 | oidSz = sizeof(ECC_384r1_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2366 | oid = ECC_384r1_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2367 | break; |
wolfSSL | 0:9d17e4342598 | 2368 | |
wolfSSL | 0:9d17e4342598 | 2369 | case 66: |
wolfSSL | 0:9d17e4342598 | 2370 | oidSz = sizeof(ECC_521r1_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2371 | oid = ECC_521r1_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2372 | break; |
wolfSSL | 0:9d17e4342598 | 2373 | |
wolfSSL | 0:9d17e4342598 | 2374 | default: |
wolfSSL | 0:9d17e4342598 | 2375 | return ASN_UNKNOWN_OID_E; |
wolfSSL | 0:9d17e4342598 | 2376 | } |
wolfSSL | 0:9d17e4342598 | 2377 | lenSz = SetLength(oidSz, output+idx); |
wolfSSL | 0:9d17e4342598 | 2378 | idx += lenSz; |
wolfSSL | 0:9d17e4342598 | 2379 | |
wolfSSL | 0:9d17e4342598 | 2380 | XMEMCPY(output+idx, oid, oidSz); |
wolfSSL | 0:9d17e4342598 | 2381 | idx += oidSz; |
wolfSSL | 0:9d17e4342598 | 2382 | |
wolfSSL | 0:9d17e4342598 | 2383 | return idx; |
wolfSSL | 0:9d17e4342598 | 2384 | } |
wolfSSL | 0:9d17e4342598 | 2385 | |
wolfSSL | 0:9d17e4342598 | 2386 | #endif /* HAVE_ECC && CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 2387 | |
wolfSSL | 0:9d17e4342598 | 2388 | |
wolfSSL | 0:9d17e4342598 | 2389 | CYASSL_LOCAL word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz) |
wolfSSL | 0:9d17e4342598 | 2390 | { |
wolfSSL | 0:9d17e4342598 | 2391 | /* adding TAG_NULL and 0 to end */ |
wolfSSL | 0:9d17e4342598 | 2392 | |
wolfSSL | 0:9d17e4342598 | 2393 | /* hashTypes */ |
wolfSSL | 0:9d17e4342598 | 2394 | static const byte shaAlgoID[] = { 0x2b, 0x0e, 0x03, 0x02, 0x1a, |
wolfSSL | 0:9d17e4342598 | 2395 | 0x05, 0x00 }; |
wolfSSL | 0:9d17e4342598 | 2396 | static const byte sha256AlgoID[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
wolfSSL | 0:9d17e4342598 | 2397 | 0x04, 0x02, 0x01, 0x05, 0x00 }; |
wolfSSL | 0:9d17e4342598 | 2398 | static const byte sha384AlgoID[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
wolfSSL | 0:9d17e4342598 | 2399 | 0x04, 0x02, 0x02, 0x05, 0x00 }; |
wolfSSL | 0:9d17e4342598 | 2400 | static const byte sha512AlgoID[] = { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, |
wolfSSL | 0:9d17e4342598 | 2401 | 0x04, 0x02, 0x03, 0x05, 0x00 }; |
wolfSSL | 0:9d17e4342598 | 2402 | static const byte md5AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
wolfSSL | 0:9d17e4342598 | 2403 | 0x02, 0x05, 0x05, 0x00 }; |
wolfSSL | 0:9d17e4342598 | 2404 | static const byte md2AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
wolfSSL | 0:9d17e4342598 | 2405 | 0x02, 0x02, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2406 | |
wolfSSL | 0:9d17e4342598 | 2407 | /* blkTypes, no NULL tags because IV is there instead */ |
wolfSSL | 0:9d17e4342598 | 2408 | static const byte desCbcAlgoID[] = { 0x2B, 0x0E, 0x03, 0x02, 0x07 }; |
wolfSSL | 0:9d17e4342598 | 2409 | static const byte des3CbcAlgoID[] = { 0x2A, 0x86, 0x48, 0x86, 0xF7, |
wolfSSL | 0:9d17e4342598 | 2410 | 0x0D, 0x03, 0x07 }; |
wolfSSL | 0:9d17e4342598 | 2411 | |
wolfSSL | 0:9d17e4342598 | 2412 | /* RSA sigTypes */ |
wolfSSL | 0:9d17e4342598 | 2413 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 2414 | static const byte md5wRSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, |
wolfSSL | 0:9d17e4342598 | 2415 | 0x0d, 0x01, 0x01, 0x04, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2416 | static const byte shawRSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, |
wolfSSL | 0:9d17e4342598 | 2417 | 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2418 | static const byte sha256wRSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, |
wolfSSL | 0:9d17e4342598 | 2419 | 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2420 | static const byte sha384wRSA_AlgoID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
wolfSSL | 0:9d17e4342598 | 2421 | 0x0d, 0x01, 0x01, 0x0c, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2422 | static const byte sha512wRSA_AlgoID[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, |
wolfSSL | 0:9d17e4342598 | 2423 | 0x0d, 0x01, 0x01, 0x0d, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2424 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 2425 | |
wolfSSL | 0:9d17e4342598 | 2426 | /* ECDSA sigTypes */ |
wolfSSL | 0:9d17e4342598 | 2427 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 2428 | static const byte shawECDSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE, 0x3d, |
wolfSSL | 0:9d17e4342598 | 2429 | 0x04, 0x01, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2430 | static const byte sha256wECDSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE,0x3d, |
wolfSSL | 0:9d17e4342598 | 2431 | 0x04, 0x03, 0x02, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2432 | static const byte sha384wECDSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE,0x3d, |
wolfSSL | 0:9d17e4342598 | 2433 | 0x04, 0x03, 0x03, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2434 | static const byte sha512wECDSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE,0x3d, |
wolfSSL | 0:9d17e4342598 | 2435 | 0x04, 0x03, 0x04, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2436 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 2437 | |
wolfSSL | 0:9d17e4342598 | 2438 | /* RSA keyType */ |
wolfSSL | 0:9d17e4342598 | 2439 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 2440 | static const byte RSA_AlgoID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
wolfSSL | 0:9d17e4342598 | 2441 | 0x01, 0x01, 0x01, 0x05, 0x00}; |
wolfSSL | 0:9d17e4342598 | 2442 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 2443 | |
wolfSSL | 0:9d17e4342598 | 2444 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 2445 | /* ECC keyType */ |
wolfSSL | 0:9d17e4342598 | 2446 | /* no tags, so set tagSz smaller later */ |
wolfSSL | 0:9d17e4342598 | 2447 | static const byte ECC_AlgoID[] = { 0x2a, 0x86, 0x48, 0xCE, 0x3d, |
wolfSSL | 0:9d17e4342598 | 2448 | 0x02, 0x01}; |
wolfSSL | 0:9d17e4342598 | 2449 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 2450 | |
wolfSSL | 0:9d17e4342598 | 2451 | int algoSz = 0; |
wolfSSL | 0:9d17e4342598 | 2452 | int tagSz = 2; /* tag null and terminator */ |
wolfSSL | 0:9d17e4342598 | 2453 | word32 idSz, seqSz; |
wolfSSL | 0:9d17e4342598 | 2454 | const byte* algoName = 0; |
wolfSSL | 0:9d17e4342598 | 2455 | byte ID_Length[MAX_LENGTH_SZ]; |
wolfSSL | 0:9d17e4342598 | 2456 | byte seqArray[MAX_SEQ_SZ + 1]; /* add object_id to end */ |
wolfSSL | 0:9d17e4342598 | 2457 | |
wolfSSL | 0:9d17e4342598 | 2458 | if (type == hashType) { |
wolfSSL | 0:9d17e4342598 | 2459 | switch (algoOID) { |
wolfSSL | 0:9d17e4342598 | 2460 | case SHAh: |
wolfSSL | 0:9d17e4342598 | 2461 | algoSz = sizeof(shaAlgoID); |
wolfSSL | 0:9d17e4342598 | 2462 | algoName = shaAlgoID; |
wolfSSL | 0:9d17e4342598 | 2463 | break; |
wolfSSL | 0:9d17e4342598 | 2464 | |
wolfSSL | 0:9d17e4342598 | 2465 | case SHA256h: |
wolfSSL | 0:9d17e4342598 | 2466 | algoSz = sizeof(sha256AlgoID); |
wolfSSL | 0:9d17e4342598 | 2467 | algoName = sha256AlgoID; |
wolfSSL | 0:9d17e4342598 | 2468 | break; |
wolfSSL | 0:9d17e4342598 | 2469 | |
wolfSSL | 0:9d17e4342598 | 2470 | case SHA384h: |
wolfSSL | 0:9d17e4342598 | 2471 | algoSz = sizeof(sha384AlgoID); |
wolfSSL | 0:9d17e4342598 | 2472 | algoName = sha384AlgoID; |
wolfSSL | 0:9d17e4342598 | 2473 | break; |
wolfSSL | 0:9d17e4342598 | 2474 | |
wolfSSL | 0:9d17e4342598 | 2475 | case SHA512h: |
wolfSSL | 0:9d17e4342598 | 2476 | algoSz = sizeof(sha512AlgoID); |
wolfSSL | 0:9d17e4342598 | 2477 | algoName = sha512AlgoID; |
wolfSSL | 0:9d17e4342598 | 2478 | break; |
wolfSSL | 0:9d17e4342598 | 2479 | |
wolfSSL | 0:9d17e4342598 | 2480 | case MD2h: |
wolfSSL | 0:9d17e4342598 | 2481 | algoSz = sizeof(md2AlgoID); |
wolfSSL | 0:9d17e4342598 | 2482 | algoName = md2AlgoID; |
wolfSSL | 0:9d17e4342598 | 2483 | break; |
wolfSSL | 0:9d17e4342598 | 2484 | |
wolfSSL | 0:9d17e4342598 | 2485 | case MD5h: |
wolfSSL | 0:9d17e4342598 | 2486 | algoSz = sizeof(md5AlgoID); |
wolfSSL | 0:9d17e4342598 | 2487 | algoName = md5AlgoID; |
wolfSSL | 0:9d17e4342598 | 2488 | break; |
wolfSSL | 0:9d17e4342598 | 2489 | |
wolfSSL | 0:9d17e4342598 | 2490 | default: |
wolfSSL | 0:9d17e4342598 | 2491 | CYASSL_MSG("Unknown Hash Algo"); |
wolfSSL | 0:9d17e4342598 | 2492 | return 0; /* UNKOWN_HASH_E; */ |
wolfSSL | 0:9d17e4342598 | 2493 | } |
wolfSSL | 0:9d17e4342598 | 2494 | } |
wolfSSL | 0:9d17e4342598 | 2495 | else if (type == blkType) { |
wolfSSL | 0:9d17e4342598 | 2496 | switch (algoOID) { |
wolfSSL | 0:9d17e4342598 | 2497 | case DESb: |
wolfSSL | 0:9d17e4342598 | 2498 | algoSz = sizeof(desCbcAlgoID); |
wolfSSL | 0:9d17e4342598 | 2499 | algoName = desCbcAlgoID; |
wolfSSL | 0:9d17e4342598 | 2500 | tagSz = 0; |
wolfSSL | 0:9d17e4342598 | 2501 | break; |
wolfSSL | 0:9d17e4342598 | 2502 | case DES3b: |
wolfSSL | 0:9d17e4342598 | 2503 | algoSz = sizeof(des3CbcAlgoID); |
wolfSSL | 0:9d17e4342598 | 2504 | algoName = des3CbcAlgoID; |
wolfSSL | 0:9d17e4342598 | 2505 | tagSz = 0; |
wolfSSL | 0:9d17e4342598 | 2506 | break; |
wolfSSL | 0:9d17e4342598 | 2507 | default: |
wolfSSL | 0:9d17e4342598 | 2508 | CYASSL_MSG("Unknown Block Algo"); |
wolfSSL | 0:9d17e4342598 | 2509 | return 0; |
wolfSSL | 0:9d17e4342598 | 2510 | } |
wolfSSL | 0:9d17e4342598 | 2511 | } |
wolfSSL | 0:9d17e4342598 | 2512 | else if (type == sigType) { /* sigType */ |
wolfSSL | 0:9d17e4342598 | 2513 | switch (algoOID) { |
wolfSSL | 0:9d17e4342598 | 2514 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 2515 | case CTC_MD5wRSA: |
wolfSSL | 0:9d17e4342598 | 2516 | algoSz = sizeof(md5wRSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2517 | algoName = md5wRSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2518 | break; |
wolfSSL | 0:9d17e4342598 | 2519 | |
wolfSSL | 0:9d17e4342598 | 2520 | case CTC_SHAwRSA: |
wolfSSL | 0:9d17e4342598 | 2521 | algoSz = sizeof(shawRSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2522 | algoName = shawRSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2523 | break; |
wolfSSL | 0:9d17e4342598 | 2524 | |
wolfSSL | 0:9d17e4342598 | 2525 | case CTC_SHA256wRSA: |
wolfSSL | 0:9d17e4342598 | 2526 | algoSz = sizeof(sha256wRSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2527 | algoName = sha256wRSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2528 | break; |
wolfSSL | 0:9d17e4342598 | 2529 | |
wolfSSL | 0:9d17e4342598 | 2530 | case CTC_SHA384wRSA: |
wolfSSL | 0:9d17e4342598 | 2531 | algoSz = sizeof(sha384wRSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2532 | algoName = sha384wRSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2533 | break; |
wolfSSL | 0:9d17e4342598 | 2534 | |
wolfSSL | 0:9d17e4342598 | 2535 | case CTC_SHA512wRSA: |
wolfSSL | 0:9d17e4342598 | 2536 | algoSz = sizeof(sha512wRSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2537 | algoName = sha512wRSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2538 | break; |
wolfSSL | 0:9d17e4342598 | 2539 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 2540 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 2541 | case CTC_SHAwECDSA: |
wolfSSL | 0:9d17e4342598 | 2542 | algoSz = sizeof(shawECDSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2543 | algoName = shawECDSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2544 | break; |
wolfSSL | 0:9d17e4342598 | 2545 | |
wolfSSL | 0:9d17e4342598 | 2546 | case CTC_SHA256wECDSA: |
wolfSSL | 0:9d17e4342598 | 2547 | algoSz = sizeof(sha256wECDSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2548 | algoName = sha256wECDSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2549 | break; |
wolfSSL | 0:9d17e4342598 | 2550 | |
wolfSSL | 0:9d17e4342598 | 2551 | case CTC_SHA384wECDSA: |
wolfSSL | 0:9d17e4342598 | 2552 | algoSz = sizeof(sha384wECDSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2553 | algoName = sha384wECDSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2554 | break; |
wolfSSL | 0:9d17e4342598 | 2555 | |
wolfSSL | 0:9d17e4342598 | 2556 | case CTC_SHA512wECDSA: |
wolfSSL | 0:9d17e4342598 | 2557 | algoSz = sizeof(sha512wECDSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2558 | algoName = sha512wECDSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2559 | break; |
wolfSSL | 0:9d17e4342598 | 2560 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 2561 | default: |
wolfSSL | 0:9d17e4342598 | 2562 | CYASSL_MSG("Unknown Signature Algo"); |
wolfSSL | 0:9d17e4342598 | 2563 | return 0; |
wolfSSL | 0:9d17e4342598 | 2564 | } |
wolfSSL | 0:9d17e4342598 | 2565 | } |
wolfSSL | 0:9d17e4342598 | 2566 | else if (type == keyType) { /* keyType */ |
wolfSSL | 0:9d17e4342598 | 2567 | switch (algoOID) { |
wolfSSL | 0:9d17e4342598 | 2568 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 2569 | case RSAk: |
wolfSSL | 0:9d17e4342598 | 2570 | algoSz = sizeof(RSA_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2571 | algoName = RSA_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2572 | break; |
wolfSSL | 0:9d17e4342598 | 2573 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 2574 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 2575 | case ECDSAk: |
wolfSSL | 0:9d17e4342598 | 2576 | algoSz = sizeof(ECC_AlgoID); |
wolfSSL | 0:9d17e4342598 | 2577 | algoName = ECC_AlgoID; |
wolfSSL | 0:9d17e4342598 | 2578 | tagSz = 0; |
wolfSSL | 0:9d17e4342598 | 2579 | break; |
wolfSSL | 0:9d17e4342598 | 2580 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 2581 | default: |
wolfSSL | 0:9d17e4342598 | 2582 | CYASSL_MSG("Unknown Key Algo"); |
wolfSSL | 0:9d17e4342598 | 2583 | return 0; |
wolfSSL | 0:9d17e4342598 | 2584 | } |
wolfSSL | 0:9d17e4342598 | 2585 | } |
wolfSSL | 0:9d17e4342598 | 2586 | else { |
wolfSSL | 0:9d17e4342598 | 2587 | CYASSL_MSG("Unknown Algo type"); |
wolfSSL | 0:9d17e4342598 | 2588 | return 0; |
wolfSSL | 0:9d17e4342598 | 2589 | } |
wolfSSL | 0:9d17e4342598 | 2590 | |
wolfSSL | 0:9d17e4342598 | 2591 | idSz = SetLength(algoSz - tagSz, ID_Length); /* don't include tags */ |
wolfSSL | 0:9d17e4342598 | 2592 | seqSz = SetSequence(idSz + algoSz + 1 + curveSz, seqArray); |
wolfSSL | 0:9d17e4342598 | 2593 | /* +1 for object id, curveID of curveSz follows for ecc */ |
wolfSSL | 0:9d17e4342598 | 2594 | seqArray[seqSz++] = ASN_OBJECT_ID; |
wolfSSL | 0:9d17e4342598 | 2595 | |
wolfSSL | 0:9d17e4342598 | 2596 | XMEMCPY(output, seqArray, seqSz); |
wolfSSL | 0:9d17e4342598 | 2597 | XMEMCPY(output + seqSz, ID_Length, idSz); |
wolfSSL | 0:9d17e4342598 | 2598 | XMEMCPY(output + seqSz + idSz, algoName, algoSz); |
wolfSSL | 0:9d17e4342598 | 2599 | |
wolfSSL | 0:9d17e4342598 | 2600 | return seqSz + idSz + algoSz; |
wolfSSL | 0:9d17e4342598 | 2601 | |
wolfSSL | 0:9d17e4342598 | 2602 | } |
wolfSSL | 0:9d17e4342598 | 2603 | |
wolfSSL | 0:9d17e4342598 | 2604 | |
wolfSSL | 0:9d17e4342598 | 2605 | word32 EncodeSignature(byte* out, const byte* digest, word32 digSz, int hashOID) |
wolfSSL | 0:9d17e4342598 | 2606 | { |
wolfSSL | 0:9d17e4342598 | 2607 | byte digArray[MAX_ENCODED_DIG_SZ]; |
wolfSSL | 0:9d17e4342598 | 2608 | byte algoArray[MAX_ALGO_SZ]; |
wolfSSL | 0:9d17e4342598 | 2609 | byte seqArray[MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 2610 | word32 encDigSz, algoSz, seqSz; |
wolfSSL | 0:9d17e4342598 | 2611 | |
wolfSSL | 0:9d17e4342598 | 2612 | encDigSz = SetDigest(digest, digSz, digArray); |
wolfSSL | 0:9d17e4342598 | 2613 | algoSz = SetAlgoID(hashOID, algoArray, hashType, 0); |
wolfSSL | 0:9d17e4342598 | 2614 | seqSz = SetSequence(encDigSz + algoSz, seqArray); |
wolfSSL | 0:9d17e4342598 | 2615 | |
wolfSSL | 0:9d17e4342598 | 2616 | XMEMCPY(out, seqArray, seqSz); |
wolfSSL | 0:9d17e4342598 | 2617 | XMEMCPY(out + seqSz, algoArray, algoSz); |
wolfSSL | 0:9d17e4342598 | 2618 | XMEMCPY(out + seqSz + algoSz, digArray, encDigSz); |
wolfSSL | 0:9d17e4342598 | 2619 | |
wolfSSL | 0:9d17e4342598 | 2620 | return encDigSz + algoSz + seqSz; |
wolfSSL | 0:9d17e4342598 | 2621 | } |
wolfSSL | 0:9d17e4342598 | 2622 | |
wolfSSL | 0:9d17e4342598 | 2623 | |
wolfSSL | 0:9d17e4342598 | 2624 | /* return true (1) for Confirmation */ |
wolfSSL | 0:9d17e4342598 | 2625 | static int ConfirmSignature(const byte* buf, word32 bufSz, |
wolfSSL | 0:9d17e4342598 | 2626 | const byte* key, word32 keySz, word32 keyOID, |
wolfSSL | 0:9d17e4342598 | 2627 | const byte* sig, word32 sigSz, word32 sigOID, |
wolfSSL | 0:9d17e4342598 | 2628 | void* heap) |
wolfSSL | 0:9d17e4342598 | 2629 | { |
wolfSSL | 0:9d17e4342598 | 2630 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 2631 | byte digest[SHA512_DIGEST_SIZE]; /* max size */ |
wolfSSL | 0:9d17e4342598 | 2632 | #elif !defined(NO_SHA256) |
wolfSSL | 0:9d17e4342598 | 2633 | byte digest[SHA256_DIGEST_SIZE]; /* max size */ |
wolfSSL | 0:9d17e4342598 | 2634 | #else |
wolfSSL | 0:9d17e4342598 | 2635 | byte digest[SHA_DIGEST_SIZE]; /* max size */ |
wolfSSL | 0:9d17e4342598 | 2636 | #endif |
wolfSSL | 0:9d17e4342598 | 2637 | int typeH, digestSz, ret = 0; |
wolfSSL | 0:9d17e4342598 | 2638 | |
wolfSSL | 0:9d17e4342598 | 2639 | (void)key; |
wolfSSL | 0:9d17e4342598 | 2640 | (void)keySz; |
wolfSSL | 0:9d17e4342598 | 2641 | (void)sig; |
wolfSSL | 0:9d17e4342598 | 2642 | (void)sigSz; |
wolfSSL | 0:9d17e4342598 | 2643 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 2644 | (void)ret; |
wolfSSL | 0:9d17e4342598 | 2645 | |
wolfSSL | 0:9d17e4342598 | 2646 | switch (sigOID) { |
wolfSSL | 0:9d17e4342598 | 2647 | #ifndef NO_MD5 |
wolfSSL | 0:9d17e4342598 | 2648 | case CTC_MD5wRSA: |
wolfSSL | 0:9d17e4342598 | 2649 | { |
wolfSSL | 0:9d17e4342598 | 2650 | Md5 md5; |
wolfSSL | 0:9d17e4342598 | 2651 | InitMd5(&md5); |
wolfSSL | 0:9d17e4342598 | 2652 | Md5Update(&md5, buf, bufSz); |
wolfSSL | 0:9d17e4342598 | 2653 | Md5Final(&md5, digest); |
wolfSSL | 0:9d17e4342598 | 2654 | typeH = MD5h; |
wolfSSL | 0:9d17e4342598 | 2655 | digestSz = MD5_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 2656 | } |
wolfSSL | 0:9d17e4342598 | 2657 | break; |
wolfSSL | 0:9d17e4342598 | 2658 | #endif |
wolfSSL | 0:9d17e4342598 | 2659 | #if defined(CYASSL_MD2) |
wolfSSL | 0:9d17e4342598 | 2660 | case CTC_MD2wRSA: |
wolfSSL | 0:9d17e4342598 | 2661 | { |
wolfSSL | 0:9d17e4342598 | 2662 | Md2 md2; |
wolfSSL | 0:9d17e4342598 | 2663 | InitMd2(&md2); |
wolfSSL | 0:9d17e4342598 | 2664 | Md2Update(&md2, buf, bufSz); |
wolfSSL | 0:9d17e4342598 | 2665 | Md2Final(&md2, digest); |
wolfSSL | 0:9d17e4342598 | 2666 | typeH = MD2h; |
wolfSSL | 0:9d17e4342598 | 2667 | digestSz = MD2_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 2668 | } |
wolfSSL | 0:9d17e4342598 | 2669 | break; |
wolfSSL | 0:9d17e4342598 | 2670 | #endif |
wolfSSL | 0:9d17e4342598 | 2671 | #ifndef NO_SHA |
wolfSSL | 0:9d17e4342598 | 2672 | case CTC_SHAwRSA: |
wolfSSL | 0:9d17e4342598 | 2673 | case CTC_SHAwDSA: |
wolfSSL | 0:9d17e4342598 | 2674 | case CTC_SHAwECDSA: |
wolfSSL | 0:9d17e4342598 | 2675 | { |
wolfSSL | 0:9d17e4342598 | 2676 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 2677 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 2678 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 2679 | CYASSL_MSG("InitSha failed"); |
wolfSSL | 0:9d17e4342598 | 2680 | return 0; /* not confirmed */ |
wolfSSL | 0:9d17e4342598 | 2681 | } |
wolfSSL | 0:9d17e4342598 | 2682 | ShaUpdate(&sha, buf, bufSz); |
wolfSSL | 0:9d17e4342598 | 2683 | ShaFinal(&sha, digest); |
wolfSSL | 0:9d17e4342598 | 2684 | typeH = SHAh; |
wolfSSL | 0:9d17e4342598 | 2685 | digestSz = SHA_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 2686 | } |
wolfSSL | 0:9d17e4342598 | 2687 | break; |
wolfSSL | 0:9d17e4342598 | 2688 | #endif |
wolfSSL | 0:9d17e4342598 | 2689 | #ifndef NO_SHA256 |
wolfSSL | 0:9d17e4342598 | 2690 | case CTC_SHA256wRSA: |
wolfSSL | 0:9d17e4342598 | 2691 | case CTC_SHA256wECDSA: |
wolfSSL | 0:9d17e4342598 | 2692 | { |
wolfSSL | 0:9d17e4342598 | 2693 | Sha256 sha256; |
wolfSSL | 0:9d17e4342598 | 2694 | ret = InitSha256(&sha256); |
wolfSSL | 0:9d17e4342598 | 2695 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 2696 | CYASSL_MSG("InitSha256 failed"); |
wolfSSL | 0:9d17e4342598 | 2697 | return 0; /* not confirmed */ |
wolfSSL | 0:9d17e4342598 | 2698 | } |
wolfSSL | 0:9d17e4342598 | 2699 | Sha256Update(&sha256, buf, bufSz); |
wolfSSL | 0:9d17e4342598 | 2700 | Sha256Final(&sha256, digest); |
wolfSSL | 0:9d17e4342598 | 2701 | typeH = SHA256h; |
wolfSSL | 0:9d17e4342598 | 2702 | digestSz = SHA256_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 2703 | } |
wolfSSL | 0:9d17e4342598 | 2704 | break; |
wolfSSL | 0:9d17e4342598 | 2705 | #endif |
wolfSSL | 0:9d17e4342598 | 2706 | #ifdef CYASSL_SHA512 |
wolfSSL | 0:9d17e4342598 | 2707 | case CTC_SHA512wRSA: |
wolfSSL | 0:9d17e4342598 | 2708 | case CTC_SHA512wECDSA: |
wolfSSL | 0:9d17e4342598 | 2709 | { |
wolfSSL | 0:9d17e4342598 | 2710 | Sha512 sha512; |
wolfSSL | 0:9d17e4342598 | 2711 | ret = InitSha512(&sha512); |
wolfSSL | 0:9d17e4342598 | 2712 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 2713 | CYASSL_MSG("InitSha512 failed"); |
wolfSSL | 0:9d17e4342598 | 2714 | return 0; /* not confirmed */ |
wolfSSL | 0:9d17e4342598 | 2715 | } |
wolfSSL | 0:9d17e4342598 | 2716 | Sha512Update(&sha512, buf, bufSz); |
wolfSSL | 0:9d17e4342598 | 2717 | Sha512Final(&sha512, digest); |
wolfSSL | 0:9d17e4342598 | 2718 | typeH = SHA512h; |
wolfSSL | 0:9d17e4342598 | 2719 | digestSz = SHA512_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 2720 | } |
wolfSSL | 0:9d17e4342598 | 2721 | break; |
wolfSSL | 0:9d17e4342598 | 2722 | #endif |
wolfSSL | 0:9d17e4342598 | 2723 | #ifdef CYASSL_SHA384 |
wolfSSL | 0:9d17e4342598 | 2724 | case CTC_SHA384wRSA: |
wolfSSL | 0:9d17e4342598 | 2725 | case CTC_SHA384wECDSA: |
wolfSSL | 0:9d17e4342598 | 2726 | { |
wolfSSL | 0:9d17e4342598 | 2727 | Sha384 sha384; |
wolfSSL | 0:9d17e4342598 | 2728 | ret = InitSha384(&sha384); |
wolfSSL | 0:9d17e4342598 | 2729 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 2730 | CYASSL_MSG("InitSha384 failed"); |
wolfSSL | 0:9d17e4342598 | 2731 | return 0; /* not confirmed */ |
wolfSSL | 0:9d17e4342598 | 2732 | } |
wolfSSL | 0:9d17e4342598 | 2733 | Sha384Update(&sha384, buf, bufSz); |
wolfSSL | 0:9d17e4342598 | 2734 | Sha384Final(&sha384, digest); |
wolfSSL | 0:9d17e4342598 | 2735 | typeH = SHA384h; |
wolfSSL | 0:9d17e4342598 | 2736 | digestSz = SHA384_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 2737 | } |
wolfSSL | 0:9d17e4342598 | 2738 | break; |
wolfSSL | 0:9d17e4342598 | 2739 | #endif |
wolfSSL | 0:9d17e4342598 | 2740 | default: |
wolfSSL | 0:9d17e4342598 | 2741 | CYASSL_MSG("Verify Signautre has unsupported type"); |
wolfSSL | 0:9d17e4342598 | 2742 | return 0; |
wolfSSL | 0:9d17e4342598 | 2743 | } |
wolfSSL | 0:9d17e4342598 | 2744 | (void)typeH; /* some builds won't read */ |
wolfSSL | 0:9d17e4342598 | 2745 | |
wolfSSL | 0:9d17e4342598 | 2746 | switch (keyOID) { |
wolfSSL | 0:9d17e4342598 | 2747 | #ifndef NO_RSA |
wolfSSL | 0:9d17e4342598 | 2748 | case RSAk: |
wolfSSL | 0:9d17e4342598 | 2749 | { |
wolfSSL | 0:9d17e4342598 | 2750 | RsaKey pubKey; |
wolfSSL | 0:9d17e4342598 | 2751 | byte encodedSig[MAX_ENCODED_SIG_SZ]; |
wolfSSL | 0:9d17e4342598 | 2752 | byte plain[MAX_ENCODED_SIG_SZ]; |
wolfSSL | 0:9d17e4342598 | 2753 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 2754 | int encodedSigSz, verifySz; |
wolfSSL | 0:9d17e4342598 | 2755 | byte* out; |
wolfSSL | 0:9d17e4342598 | 2756 | |
wolfSSL | 0:9d17e4342598 | 2757 | if (sigSz > MAX_ENCODED_SIG_SZ) { |
wolfSSL | 0:9d17e4342598 | 2758 | CYASSL_MSG("Verify Signautre is too big"); |
wolfSSL | 0:9d17e4342598 | 2759 | return 0; |
wolfSSL | 0:9d17e4342598 | 2760 | } |
wolfSSL | 0:9d17e4342598 | 2761 | |
wolfSSL | 0:9d17e4342598 | 2762 | ret = InitRsaKey(&pubKey, heap); |
wolfSSL | 0:9d17e4342598 | 2763 | if (ret != 0) return ret; |
wolfSSL | 0:9d17e4342598 | 2764 | if (RsaPublicKeyDecode(key, &idx, &pubKey, keySz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2765 | CYASSL_MSG("ASN Key decode error RSA"); |
wolfSSL | 0:9d17e4342598 | 2766 | ret = 0; |
wolfSSL | 0:9d17e4342598 | 2767 | } |
wolfSSL | 0:9d17e4342598 | 2768 | else { |
wolfSSL | 0:9d17e4342598 | 2769 | XMEMCPY(plain, sig, sigSz); |
wolfSSL | 0:9d17e4342598 | 2770 | if ( (verifySz = RsaSSL_VerifyInline(plain, sigSz, &out, |
wolfSSL | 0:9d17e4342598 | 2771 | &pubKey)) < 0) { |
wolfSSL | 0:9d17e4342598 | 2772 | CYASSL_MSG("Rsa SSL verify error"); |
wolfSSL | 0:9d17e4342598 | 2773 | ret = 0; |
wolfSSL | 0:9d17e4342598 | 2774 | } |
wolfSSL | 0:9d17e4342598 | 2775 | else { |
wolfSSL | 0:9d17e4342598 | 2776 | /* make sure we're right justified */ |
wolfSSL | 0:9d17e4342598 | 2777 | encodedSigSz = |
wolfSSL | 0:9d17e4342598 | 2778 | EncodeSignature(encodedSig, digest, digestSz, typeH); |
wolfSSL | 0:9d17e4342598 | 2779 | if (encodedSigSz != verifySz || |
wolfSSL | 0:9d17e4342598 | 2780 | XMEMCMP(out, encodedSig, encodedSigSz) != 0) { |
wolfSSL | 0:9d17e4342598 | 2781 | CYASSL_MSG("Rsa SSL verify match encode error"); |
wolfSSL | 0:9d17e4342598 | 2782 | ret = 0; |
wolfSSL | 0:9d17e4342598 | 2783 | } |
wolfSSL | 0:9d17e4342598 | 2784 | else |
wolfSSL | 0:9d17e4342598 | 2785 | ret = 1; /* match */ |
wolfSSL | 0:9d17e4342598 | 2786 | |
wolfSSL | 0:9d17e4342598 | 2787 | #ifdef CYASSL_DEBUG_ENCODING |
wolfSSL | 0:9d17e4342598 | 2788 | { |
wolfSSL | 0:9d17e4342598 | 2789 | int x; |
wolfSSL | 0:9d17e4342598 | 2790 | printf("cyassl encodedSig:\n"); |
wolfSSL | 0:9d17e4342598 | 2791 | for (x = 0; x < encodedSigSz; x++) { |
wolfSSL | 0:9d17e4342598 | 2792 | printf("%02x ", encodedSig[x]); |
wolfSSL | 0:9d17e4342598 | 2793 | if ( (x % 16) == 15) |
wolfSSL | 0:9d17e4342598 | 2794 | printf("\n"); |
wolfSSL | 0:9d17e4342598 | 2795 | } |
wolfSSL | 0:9d17e4342598 | 2796 | printf("\n"); |
wolfSSL | 0:9d17e4342598 | 2797 | printf("actual digest:\n"); |
wolfSSL | 0:9d17e4342598 | 2798 | for (x = 0; x < verifySz; x++) { |
wolfSSL | 0:9d17e4342598 | 2799 | printf("%02x ", out[x]); |
wolfSSL | 0:9d17e4342598 | 2800 | if ( (x % 16) == 15) |
wolfSSL | 0:9d17e4342598 | 2801 | printf("\n"); |
wolfSSL | 0:9d17e4342598 | 2802 | } |
wolfSSL | 0:9d17e4342598 | 2803 | printf("\n"); |
wolfSSL | 0:9d17e4342598 | 2804 | } |
wolfSSL | 0:9d17e4342598 | 2805 | #endif /* CYASSL_DEBUG_ENCODING */ |
wolfSSL | 0:9d17e4342598 | 2806 | } |
wolfSSL | 0:9d17e4342598 | 2807 | } |
wolfSSL | 0:9d17e4342598 | 2808 | FreeRsaKey(&pubKey); |
wolfSSL | 0:9d17e4342598 | 2809 | return ret; |
wolfSSL | 0:9d17e4342598 | 2810 | } |
wolfSSL | 0:9d17e4342598 | 2811 | |
wolfSSL | 0:9d17e4342598 | 2812 | #endif /* NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 2813 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 2814 | case ECDSAk: |
wolfSSL | 0:9d17e4342598 | 2815 | { |
wolfSSL | 0:9d17e4342598 | 2816 | ecc_key pubKey; |
wolfSSL | 0:9d17e4342598 | 2817 | int verify = 0; |
wolfSSL | 0:9d17e4342598 | 2818 | |
wolfSSL | 0:9d17e4342598 | 2819 | if (ecc_import_x963(key, keySz, &pubKey) < 0) { |
wolfSSL | 0:9d17e4342598 | 2820 | CYASSL_MSG("ASN Key import error ECC"); |
wolfSSL | 0:9d17e4342598 | 2821 | return 0; |
wolfSSL | 0:9d17e4342598 | 2822 | } |
wolfSSL | 0:9d17e4342598 | 2823 | |
wolfSSL | 0:9d17e4342598 | 2824 | ret = ecc_verify_hash(sig,sigSz,digest,digestSz,&verify,&pubKey); |
wolfSSL | 0:9d17e4342598 | 2825 | ecc_free(&pubKey); |
wolfSSL | 0:9d17e4342598 | 2826 | if (ret == 0 && verify == 1) |
wolfSSL | 0:9d17e4342598 | 2827 | return 1; /* match */ |
wolfSSL | 0:9d17e4342598 | 2828 | |
wolfSSL | 0:9d17e4342598 | 2829 | CYASSL_MSG("ECC Verify didn't match"); |
wolfSSL | 0:9d17e4342598 | 2830 | return 0; |
wolfSSL | 0:9d17e4342598 | 2831 | } |
wolfSSL | 0:9d17e4342598 | 2832 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 2833 | default: |
wolfSSL | 0:9d17e4342598 | 2834 | CYASSL_MSG("Verify Key type unknown"); |
wolfSSL | 0:9d17e4342598 | 2835 | return 0; |
wolfSSL | 0:9d17e4342598 | 2836 | } |
wolfSSL | 0:9d17e4342598 | 2837 | } |
wolfSSL | 0:9d17e4342598 | 2838 | |
wolfSSL | 0:9d17e4342598 | 2839 | |
wolfSSL | 0:9d17e4342598 | 2840 | static int DecodeAltNames(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 2841 | { |
wolfSSL | 0:9d17e4342598 | 2842 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 2843 | int length = 0; |
wolfSSL | 0:9d17e4342598 | 2844 | |
wolfSSL | 0:9d17e4342598 | 2845 | CYASSL_ENTER("DecodeAltNames"); |
wolfSSL | 0:9d17e4342598 | 2846 | |
wolfSSL | 0:9d17e4342598 | 2847 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2848 | CYASSL_MSG("\tBad Sequence"); |
wolfSSL | 0:9d17e4342598 | 2849 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2850 | } |
wolfSSL | 0:9d17e4342598 | 2851 | |
wolfSSL | 0:9d17e4342598 | 2852 | while (length > 0) { |
wolfSSL | 0:9d17e4342598 | 2853 | byte b = input[idx++]; |
wolfSSL | 0:9d17e4342598 | 2854 | |
wolfSSL | 0:9d17e4342598 | 2855 | length--; |
wolfSSL | 0:9d17e4342598 | 2856 | |
wolfSSL | 0:9d17e4342598 | 2857 | /* Save DNS Type names in the altNames list. */ |
wolfSSL | 0:9d17e4342598 | 2858 | /* Save Other Type names in the cert's OidMap */ |
wolfSSL | 0:9d17e4342598 | 2859 | if (b == (ASN_CONTEXT_SPECIFIC | ASN_DNS_TYPE)) { |
wolfSSL | 0:9d17e4342598 | 2860 | DNS_entry* dnsEntry; |
wolfSSL | 0:9d17e4342598 | 2861 | int strLen; |
wolfSSL | 0:9d17e4342598 | 2862 | word32 lenStartIdx = idx; |
wolfSSL | 0:9d17e4342598 | 2863 | |
wolfSSL | 0:9d17e4342598 | 2864 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2865 | CYASSL_MSG("\tfail: str length"); |
wolfSSL | 0:9d17e4342598 | 2866 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2867 | } |
wolfSSL | 0:9d17e4342598 | 2868 | length -= (idx - lenStartIdx); |
wolfSSL | 0:9d17e4342598 | 2869 | |
wolfSSL | 0:9d17e4342598 | 2870 | dnsEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap, |
wolfSSL | 0:9d17e4342598 | 2871 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 0:9d17e4342598 | 2872 | if (dnsEntry == NULL) { |
wolfSSL | 0:9d17e4342598 | 2873 | CYASSL_MSG("\tOut of Memory"); |
wolfSSL | 0:9d17e4342598 | 2874 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2875 | } |
wolfSSL | 0:9d17e4342598 | 2876 | |
wolfSSL | 0:9d17e4342598 | 2877 | dnsEntry->name = (char*)XMALLOC(strLen + 1, cert->heap, |
wolfSSL | 0:9d17e4342598 | 2878 | DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 0:9d17e4342598 | 2879 | if (dnsEntry->name == NULL) { |
wolfSSL | 0:9d17e4342598 | 2880 | CYASSL_MSG("\tOut of Memory"); |
wolfSSL | 0:9d17e4342598 | 2881 | XFREE(dnsEntry, cert->heap, DYNAMIC_TYPE_ALTNAME); |
wolfSSL | 0:9d17e4342598 | 2882 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2883 | } |
wolfSSL | 0:9d17e4342598 | 2884 | |
wolfSSL | 0:9d17e4342598 | 2885 | XMEMCPY(dnsEntry->name, &input[idx], strLen); |
wolfSSL | 0:9d17e4342598 | 2886 | dnsEntry->name[strLen] = '\0'; |
wolfSSL | 0:9d17e4342598 | 2887 | |
wolfSSL | 0:9d17e4342598 | 2888 | dnsEntry->next = cert->altNames; |
wolfSSL | 0:9d17e4342598 | 2889 | cert->altNames = dnsEntry; |
wolfSSL | 0:9d17e4342598 | 2890 | |
wolfSSL | 0:9d17e4342598 | 2891 | length -= strLen; |
wolfSSL | 0:9d17e4342598 | 2892 | idx += strLen; |
wolfSSL | 0:9d17e4342598 | 2893 | } |
wolfSSL | 0:9d17e4342598 | 2894 | #ifdef CYASSL_SEP |
wolfSSL | 0:9d17e4342598 | 2895 | else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_OTHER_TYPE)) |
wolfSSL | 0:9d17e4342598 | 2896 | { |
wolfSSL | 0:9d17e4342598 | 2897 | int strLen; |
wolfSSL | 0:9d17e4342598 | 2898 | word32 lenStartIdx = idx; |
wolfSSL | 0:9d17e4342598 | 2899 | word32 oid = 0; |
wolfSSL | 0:9d17e4342598 | 2900 | |
wolfSSL | 0:9d17e4342598 | 2901 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2902 | CYASSL_MSG("\tfail: other name length"); |
wolfSSL | 0:9d17e4342598 | 2903 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2904 | } |
wolfSSL | 0:9d17e4342598 | 2905 | /* Consume the rest of this sequence. */ |
wolfSSL | 0:9d17e4342598 | 2906 | length -= (strLen + idx - lenStartIdx); |
wolfSSL | 0:9d17e4342598 | 2907 | |
wolfSSL | 0:9d17e4342598 | 2908 | if (GetObjectId(input, &idx, &oid, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2909 | CYASSL_MSG("\tbad OID"); |
wolfSSL | 0:9d17e4342598 | 2910 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2911 | } |
wolfSSL | 0:9d17e4342598 | 2912 | |
wolfSSL | 0:9d17e4342598 | 2913 | if (oid != HW_NAME_OID) { |
wolfSSL | 0:9d17e4342598 | 2914 | CYASSL_MSG("\tincorrect OID"); |
wolfSSL | 0:9d17e4342598 | 2915 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2916 | } |
wolfSSL | 0:9d17e4342598 | 2917 | |
wolfSSL | 0:9d17e4342598 | 2918 | if (input[idx++] != (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) { |
wolfSSL | 0:9d17e4342598 | 2919 | CYASSL_MSG("\twrong type"); |
wolfSSL | 0:9d17e4342598 | 2920 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2921 | } |
wolfSSL | 0:9d17e4342598 | 2922 | |
wolfSSL | 0:9d17e4342598 | 2923 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2924 | CYASSL_MSG("\tfail: str len"); |
wolfSSL | 0:9d17e4342598 | 2925 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2926 | } |
wolfSSL | 0:9d17e4342598 | 2927 | |
wolfSSL | 0:9d17e4342598 | 2928 | if (GetSequence(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2929 | CYASSL_MSG("\tBad Sequence"); |
wolfSSL | 0:9d17e4342598 | 2930 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2931 | } |
wolfSSL | 0:9d17e4342598 | 2932 | |
wolfSSL | 0:9d17e4342598 | 2933 | if (input[idx++] != ASN_OBJECT_ID) { |
wolfSSL | 0:9d17e4342598 | 2934 | CYASSL_MSG("\texpected OID"); |
wolfSSL | 0:9d17e4342598 | 2935 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2936 | } |
wolfSSL | 0:9d17e4342598 | 2937 | |
wolfSSL | 0:9d17e4342598 | 2938 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2939 | CYASSL_MSG("\tfailed: str len"); |
wolfSSL | 0:9d17e4342598 | 2940 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2941 | } |
wolfSSL | 0:9d17e4342598 | 2942 | |
wolfSSL | 0:9d17e4342598 | 2943 | cert->hwType = (byte*)XMALLOC(strLen, cert->heap, 0); |
wolfSSL | 0:9d17e4342598 | 2944 | if (cert->hwType == NULL) { |
wolfSSL | 0:9d17e4342598 | 2945 | CYASSL_MSG("\tOut of Memory"); |
wolfSSL | 0:9d17e4342598 | 2946 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 2947 | } |
wolfSSL | 0:9d17e4342598 | 2948 | |
wolfSSL | 0:9d17e4342598 | 2949 | XMEMCPY(cert->hwType, &input[idx], strLen); |
wolfSSL | 0:9d17e4342598 | 2950 | cert->hwTypeSz = strLen; |
wolfSSL | 0:9d17e4342598 | 2951 | idx += strLen; |
wolfSSL | 0:9d17e4342598 | 2952 | |
wolfSSL | 0:9d17e4342598 | 2953 | if (input[idx++] != ASN_OCTET_STRING) { |
wolfSSL | 0:9d17e4342598 | 2954 | CYASSL_MSG("\texpected Octet String"); |
wolfSSL | 0:9d17e4342598 | 2955 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2956 | } |
wolfSSL | 0:9d17e4342598 | 2957 | |
wolfSSL | 0:9d17e4342598 | 2958 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2959 | CYASSL_MSG("\tfailed: str len"); |
wolfSSL | 0:9d17e4342598 | 2960 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2961 | } |
wolfSSL | 0:9d17e4342598 | 2962 | |
wolfSSL | 0:9d17e4342598 | 2963 | cert->hwSerialNum = (byte*)XMALLOC(strLen + 1, cert->heap, 0); |
wolfSSL | 0:9d17e4342598 | 2964 | if (cert->hwSerialNum == NULL) { |
wolfSSL | 0:9d17e4342598 | 2965 | CYASSL_MSG("\tOut of Memory"); |
wolfSSL | 0:9d17e4342598 | 2966 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 2967 | } |
wolfSSL | 0:9d17e4342598 | 2968 | |
wolfSSL | 0:9d17e4342598 | 2969 | XMEMCPY(cert->hwSerialNum, &input[idx], strLen); |
wolfSSL | 0:9d17e4342598 | 2970 | cert->hwSerialNum[strLen] = '\0'; |
wolfSSL | 0:9d17e4342598 | 2971 | cert->hwSerialNumSz = strLen; |
wolfSSL | 0:9d17e4342598 | 2972 | idx += strLen; |
wolfSSL | 0:9d17e4342598 | 2973 | } |
wolfSSL | 0:9d17e4342598 | 2974 | #endif /* CYASSL_SEP */ |
wolfSSL | 0:9d17e4342598 | 2975 | else { |
wolfSSL | 0:9d17e4342598 | 2976 | int strLen; |
wolfSSL | 0:9d17e4342598 | 2977 | word32 lenStartIdx = idx; |
wolfSSL | 0:9d17e4342598 | 2978 | |
wolfSSL | 0:9d17e4342598 | 2979 | CYASSL_MSG("\tUnsupported name type, skipping"); |
wolfSSL | 0:9d17e4342598 | 2980 | |
wolfSSL | 0:9d17e4342598 | 2981 | if (GetLength(input, &idx, &strLen, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 2982 | CYASSL_MSG("\tfail: unsupported name length"); |
wolfSSL | 0:9d17e4342598 | 2983 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 2984 | } |
wolfSSL | 0:9d17e4342598 | 2985 | length -= (strLen + idx - lenStartIdx); |
wolfSSL | 0:9d17e4342598 | 2986 | idx += strLen; |
wolfSSL | 0:9d17e4342598 | 2987 | } |
wolfSSL | 0:9d17e4342598 | 2988 | } |
wolfSSL | 0:9d17e4342598 | 2989 | return 0; |
wolfSSL | 0:9d17e4342598 | 2990 | } |
wolfSSL | 0:9d17e4342598 | 2991 | |
wolfSSL | 0:9d17e4342598 | 2992 | |
wolfSSL | 0:9d17e4342598 | 2993 | static int DecodeBasicCaConstraint(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 2994 | { |
wolfSSL | 0:9d17e4342598 | 2995 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 2996 | int length = 0; |
wolfSSL | 0:9d17e4342598 | 2997 | |
wolfSSL | 0:9d17e4342598 | 2998 | CYASSL_ENTER("DecodeBasicCaConstraint"); |
wolfSSL | 0:9d17e4342598 | 2999 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3000 | CYASSL_MSG("\tfail: bad SEQUENCE"); |
wolfSSL | 0:9d17e4342598 | 3001 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3002 | } |
wolfSSL | 0:9d17e4342598 | 3003 | |
wolfSSL | 0:9d17e4342598 | 3004 | if (length == 0) |
wolfSSL | 0:9d17e4342598 | 3005 | return 0; |
wolfSSL | 0:9d17e4342598 | 3006 | |
wolfSSL | 0:9d17e4342598 | 3007 | /* If the basic ca constraint is false, this extension may be named, but |
wolfSSL | 0:9d17e4342598 | 3008 | * left empty. So, if the length is 0, just return. */ |
wolfSSL | 0:9d17e4342598 | 3009 | |
wolfSSL | 0:9d17e4342598 | 3010 | if (input[idx++] != ASN_BOOLEAN) |
wolfSSL | 0:9d17e4342598 | 3011 | { |
wolfSSL | 0:9d17e4342598 | 3012 | CYASSL_MSG("\tfail: constraint not BOOLEAN"); |
wolfSSL | 0:9d17e4342598 | 3013 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3014 | } |
wolfSSL | 0:9d17e4342598 | 3015 | |
wolfSSL | 0:9d17e4342598 | 3016 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3017 | { |
wolfSSL | 0:9d17e4342598 | 3018 | CYASSL_MSG("\tfail: length"); |
wolfSSL | 0:9d17e4342598 | 3019 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3020 | } |
wolfSSL | 0:9d17e4342598 | 3021 | |
wolfSSL | 0:9d17e4342598 | 3022 | if (input[idx++]) |
wolfSSL | 0:9d17e4342598 | 3023 | cert->isCA = 1; |
wolfSSL | 0:9d17e4342598 | 3024 | |
wolfSSL | 0:9d17e4342598 | 3025 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3026 | /* If there isn't any more data, return. */ |
wolfSSL | 0:9d17e4342598 | 3027 | if (idx >= (word32)sz) |
wolfSSL | 0:9d17e4342598 | 3028 | return 0; |
wolfSSL | 0:9d17e4342598 | 3029 | |
wolfSSL | 0:9d17e4342598 | 3030 | /* Anything left should be the optional pathlength */ |
wolfSSL | 0:9d17e4342598 | 3031 | if (input[idx++] != ASN_INTEGER) { |
wolfSSL | 0:9d17e4342598 | 3032 | CYASSL_MSG("\tfail: pathlen not INTEGER"); |
wolfSSL | 0:9d17e4342598 | 3033 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3034 | } |
wolfSSL | 0:9d17e4342598 | 3035 | |
wolfSSL | 0:9d17e4342598 | 3036 | if (input[idx++] != 1) { |
wolfSSL | 0:9d17e4342598 | 3037 | CYASSL_MSG("\tfail: pathlen too long"); |
wolfSSL | 0:9d17e4342598 | 3038 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3039 | } |
wolfSSL | 0:9d17e4342598 | 3040 | |
wolfSSL | 0:9d17e4342598 | 3041 | cert->pathLength = input[idx]; |
wolfSSL | 0:9d17e4342598 | 3042 | cert->extBasicConstPlSet = 1; |
wolfSSL | 0:9d17e4342598 | 3043 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 3044 | |
wolfSSL | 0:9d17e4342598 | 3045 | return 0; |
wolfSSL | 0:9d17e4342598 | 3046 | } |
wolfSSL | 0:9d17e4342598 | 3047 | |
wolfSSL | 0:9d17e4342598 | 3048 | |
wolfSSL | 0:9d17e4342598 | 3049 | #define CRLDP_FULL_NAME 0 |
wolfSSL | 0:9d17e4342598 | 3050 | /* From RFC3280 SS4.2.1.14, Distribution Point Name*/ |
wolfSSL | 0:9d17e4342598 | 3051 | #define GENERALNAME_URI 6 |
wolfSSL | 0:9d17e4342598 | 3052 | /* From RFC3280 SS4.2.1.7, GeneralName */ |
wolfSSL | 0:9d17e4342598 | 3053 | |
wolfSSL | 0:9d17e4342598 | 3054 | static int DecodeCrlDist(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 3055 | { |
wolfSSL | 0:9d17e4342598 | 3056 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 3057 | int length = 0; |
wolfSSL | 0:9d17e4342598 | 3058 | |
wolfSSL | 0:9d17e4342598 | 3059 | CYASSL_ENTER("DecodeCrlDist"); |
wolfSSL | 0:9d17e4342598 | 3060 | |
wolfSSL | 0:9d17e4342598 | 3061 | /* Unwrap the list of Distribution Points*/ |
wolfSSL | 0:9d17e4342598 | 3062 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3063 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3064 | |
wolfSSL | 0:9d17e4342598 | 3065 | /* Unwrap a single Distribution Point */ |
wolfSSL | 0:9d17e4342598 | 3066 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3067 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3068 | |
wolfSSL | 0:9d17e4342598 | 3069 | /* The Distribution Point has three explicit optional members |
wolfSSL | 0:9d17e4342598 | 3070 | * First check for a DistributionPointName |
wolfSSL | 0:9d17e4342598 | 3071 | */ |
wolfSSL | 0:9d17e4342598 | 3072 | if (input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0)) |
wolfSSL | 0:9d17e4342598 | 3073 | { |
wolfSSL | 0:9d17e4342598 | 3074 | idx++; |
wolfSSL | 0:9d17e4342598 | 3075 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3076 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3077 | |
wolfSSL | 0:9d17e4342598 | 3078 | if (input[idx] == |
wolfSSL | 0:9d17e4342598 | 3079 | (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | CRLDP_FULL_NAME)) |
wolfSSL | 0:9d17e4342598 | 3080 | { |
wolfSSL | 0:9d17e4342598 | 3081 | idx++; |
wolfSSL | 0:9d17e4342598 | 3082 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3083 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3084 | |
wolfSSL | 0:9d17e4342598 | 3085 | if (input[idx] == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI)) |
wolfSSL | 0:9d17e4342598 | 3086 | { |
wolfSSL | 0:9d17e4342598 | 3087 | idx++; |
wolfSSL | 0:9d17e4342598 | 3088 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3089 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3090 | |
wolfSSL | 0:9d17e4342598 | 3091 | cert->extCrlInfoSz = length; |
wolfSSL | 0:9d17e4342598 | 3092 | cert->extCrlInfo = input + idx; |
wolfSSL | 0:9d17e4342598 | 3093 | idx += length; |
wolfSSL | 0:9d17e4342598 | 3094 | } |
wolfSSL | 0:9d17e4342598 | 3095 | else |
wolfSSL | 0:9d17e4342598 | 3096 | /* This isn't a URI, skip it. */ |
wolfSSL | 0:9d17e4342598 | 3097 | idx += length; |
wolfSSL | 0:9d17e4342598 | 3098 | } |
wolfSSL | 0:9d17e4342598 | 3099 | else |
wolfSSL | 0:9d17e4342598 | 3100 | /* This isn't a FULLNAME, skip it. */ |
wolfSSL | 0:9d17e4342598 | 3101 | idx += length; |
wolfSSL | 0:9d17e4342598 | 3102 | } |
wolfSSL | 0:9d17e4342598 | 3103 | |
wolfSSL | 0:9d17e4342598 | 3104 | /* Check for reasonFlags */ |
wolfSSL | 0:9d17e4342598 | 3105 | if (idx < (word32)sz && |
wolfSSL | 0:9d17e4342598 | 3106 | input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1)) |
wolfSSL | 0:9d17e4342598 | 3107 | { |
wolfSSL | 0:9d17e4342598 | 3108 | idx++; |
wolfSSL | 0:9d17e4342598 | 3109 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3110 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3111 | idx += length; |
wolfSSL | 0:9d17e4342598 | 3112 | } |
wolfSSL | 0:9d17e4342598 | 3113 | |
wolfSSL | 0:9d17e4342598 | 3114 | /* Check for cRLIssuer */ |
wolfSSL | 0:9d17e4342598 | 3115 | if (idx < (word32)sz && |
wolfSSL | 0:9d17e4342598 | 3116 | input[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 2)) |
wolfSSL | 0:9d17e4342598 | 3117 | { |
wolfSSL | 0:9d17e4342598 | 3118 | idx++; |
wolfSSL | 0:9d17e4342598 | 3119 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3120 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3121 | idx += length; |
wolfSSL | 0:9d17e4342598 | 3122 | } |
wolfSSL | 0:9d17e4342598 | 3123 | |
wolfSSL | 0:9d17e4342598 | 3124 | if (idx < (word32)sz) |
wolfSSL | 0:9d17e4342598 | 3125 | { |
wolfSSL | 0:9d17e4342598 | 3126 | CYASSL_MSG("\tThere are more CRL Distribution Point records, " |
wolfSSL | 0:9d17e4342598 | 3127 | "but we only use the first one."); |
wolfSSL | 0:9d17e4342598 | 3128 | } |
wolfSSL | 0:9d17e4342598 | 3129 | |
wolfSSL | 0:9d17e4342598 | 3130 | return 0; |
wolfSSL | 0:9d17e4342598 | 3131 | } |
wolfSSL | 0:9d17e4342598 | 3132 | |
wolfSSL | 0:9d17e4342598 | 3133 | |
wolfSSL | 0:9d17e4342598 | 3134 | static int DecodeAuthInfo(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 3135 | /* |
wolfSSL | 0:9d17e4342598 | 3136 | * Read the first of the Authority Information Access records. If there are |
wolfSSL | 0:9d17e4342598 | 3137 | * any issues, return without saving the record. |
wolfSSL | 0:9d17e4342598 | 3138 | */ |
wolfSSL | 0:9d17e4342598 | 3139 | { |
wolfSSL | 0:9d17e4342598 | 3140 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 3141 | int length = 0; |
wolfSSL | 0:9d17e4342598 | 3142 | byte b; |
wolfSSL | 0:9d17e4342598 | 3143 | word32 oid; |
wolfSSL | 0:9d17e4342598 | 3144 | |
wolfSSL | 0:9d17e4342598 | 3145 | CYASSL_ENTER("DecodeAuthInfo"); |
wolfSSL | 0:9d17e4342598 | 3146 | |
wolfSSL | 0:9d17e4342598 | 3147 | /* Unwrap the list of AIAs */ |
wolfSSL | 0:9d17e4342598 | 3148 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3149 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3150 | |
wolfSSL | 0:9d17e4342598 | 3151 | while (idx < (word32)sz) { |
wolfSSL | 0:9d17e4342598 | 3152 | /* Unwrap a single AIA */ |
wolfSSL | 0:9d17e4342598 | 3153 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3154 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3155 | |
wolfSSL | 0:9d17e4342598 | 3156 | oid = 0; |
wolfSSL | 0:9d17e4342598 | 3157 | if (GetObjectId(input, &idx, &oid, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3158 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3159 | |
wolfSSL | 0:9d17e4342598 | 3160 | /* Only supporting URIs right now. */ |
wolfSSL | 0:9d17e4342598 | 3161 | b = input[idx++]; |
wolfSSL | 0:9d17e4342598 | 3162 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3163 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3164 | |
wolfSSL | 0:9d17e4342598 | 3165 | if (b == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI) && |
wolfSSL | 0:9d17e4342598 | 3166 | oid == AIA_OCSP_OID) |
wolfSSL | 0:9d17e4342598 | 3167 | { |
wolfSSL | 0:9d17e4342598 | 3168 | cert->extAuthInfoSz = length; |
wolfSSL | 0:9d17e4342598 | 3169 | cert->extAuthInfo = input + idx; |
wolfSSL | 0:9d17e4342598 | 3170 | break; |
wolfSSL | 0:9d17e4342598 | 3171 | } |
wolfSSL | 0:9d17e4342598 | 3172 | idx += length; |
wolfSSL | 0:9d17e4342598 | 3173 | } |
wolfSSL | 0:9d17e4342598 | 3174 | |
wolfSSL | 0:9d17e4342598 | 3175 | return 0; |
wolfSSL | 0:9d17e4342598 | 3176 | } |
wolfSSL | 0:9d17e4342598 | 3177 | |
wolfSSL | 0:9d17e4342598 | 3178 | |
wolfSSL | 0:9d17e4342598 | 3179 | static int DecodeAuthKeyId(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 3180 | { |
wolfSSL | 0:9d17e4342598 | 3181 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 3182 | int length = 0, ret = 0; |
wolfSSL | 0:9d17e4342598 | 3183 | |
wolfSSL | 0:9d17e4342598 | 3184 | CYASSL_ENTER("DecodeAuthKeyId"); |
wolfSSL | 0:9d17e4342598 | 3185 | |
wolfSSL | 0:9d17e4342598 | 3186 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3187 | CYASSL_MSG("\tfail: should be a SEQUENCE\n"); |
wolfSSL | 0:9d17e4342598 | 3188 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3189 | } |
wolfSSL | 0:9d17e4342598 | 3190 | |
wolfSSL | 0:9d17e4342598 | 3191 | if (input[idx++] != (ASN_CONTEXT_SPECIFIC | 0)) { |
wolfSSL | 0:9d17e4342598 | 3192 | CYASSL_MSG("\tfail: wanted OPTIONAL item 0, not available\n"); |
wolfSSL | 0:9d17e4342598 | 3193 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3194 | } |
wolfSSL | 0:9d17e4342598 | 3195 | |
wolfSSL | 0:9d17e4342598 | 3196 | if (GetLength(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3197 | CYASSL_MSG("\tfail: extension data length"); |
wolfSSL | 0:9d17e4342598 | 3198 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3199 | } |
wolfSSL | 0:9d17e4342598 | 3200 | |
wolfSSL | 0:9d17e4342598 | 3201 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3202 | cert->extAuthKeyIdSrc = &input[idx]; |
wolfSSL | 0:9d17e4342598 | 3203 | cert->extAuthKeyIdSz = length; |
wolfSSL | 0:9d17e4342598 | 3204 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 3205 | |
wolfSSL | 0:9d17e4342598 | 3206 | if (length == SHA_SIZE) { |
wolfSSL | 0:9d17e4342598 | 3207 | XMEMCPY(cert->extAuthKeyId, input + idx, length); |
wolfSSL | 0:9d17e4342598 | 3208 | } |
wolfSSL | 0:9d17e4342598 | 3209 | else { |
wolfSSL | 0:9d17e4342598 | 3210 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 3211 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 3212 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 3213 | return ret; |
wolfSSL | 0:9d17e4342598 | 3214 | ShaUpdate(&sha, input + idx, length); |
wolfSSL | 0:9d17e4342598 | 3215 | ShaFinal(&sha, cert->extAuthKeyId); |
wolfSSL | 0:9d17e4342598 | 3216 | } |
wolfSSL | 0:9d17e4342598 | 3217 | |
wolfSSL | 0:9d17e4342598 | 3218 | return 0; |
wolfSSL | 0:9d17e4342598 | 3219 | } |
wolfSSL | 0:9d17e4342598 | 3220 | |
wolfSSL | 0:9d17e4342598 | 3221 | |
wolfSSL | 0:9d17e4342598 | 3222 | static int DecodeSubjKeyId(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 3223 | { |
wolfSSL | 0:9d17e4342598 | 3224 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 3225 | int length = 0, ret = 0; |
wolfSSL | 0:9d17e4342598 | 3226 | |
wolfSSL | 0:9d17e4342598 | 3227 | CYASSL_ENTER("DecodeSubjKeyId"); |
wolfSSL | 0:9d17e4342598 | 3228 | |
wolfSSL | 0:9d17e4342598 | 3229 | if (input[idx++] != ASN_OCTET_STRING) { |
wolfSSL | 0:9d17e4342598 | 3230 | CYASSL_MSG("\tfail: should be an OCTET STRING"); |
wolfSSL | 0:9d17e4342598 | 3231 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3232 | } |
wolfSSL | 0:9d17e4342598 | 3233 | |
wolfSSL | 0:9d17e4342598 | 3234 | if (GetLength(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3235 | CYASSL_MSG("\tfail: extension data length"); |
wolfSSL | 0:9d17e4342598 | 3236 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3237 | } |
wolfSSL | 0:9d17e4342598 | 3238 | |
wolfSSL | 0:9d17e4342598 | 3239 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3240 | cert->extSubjKeyIdSrc = &input[idx]; |
wolfSSL | 0:9d17e4342598 | 3241 | cert->extSubjKeyIdSz = length; |
wolfSSL | 0:9d17e4342598 | 3242 | #endif /* OPENSSL_EXTRA */ |
wolfSSL | 0:9d17e4342598 | 3243 | |
wolfSSL | 0:9d17e4342598 | 3244 | if (length == SIGNER_DIGEST_SIZE) { |
wolfSSL | 0:9d17e4342598 | 3245 | XMEMCPY(cert->extSubjKeyId, input + idx, length); |
wolfSSL | 0:9d17e4342598 | 3246 | } |
wolfSSL | 0:9d17e4342598 | 3247 | else { |
wolfSSL | 0:9d17e4342598 | 3248 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 3249 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 3250 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 3251 | return ret; |
wolfSSL | 0:9d17e4342598 | 3252 | ShaUpdate(&sha, input + idx, length); |
wolfSSL | 0:9d17e4342598 | 3253 | ShaFinal(&sha, cert->extSubjKeyId); |
wolfSSL | 0:9d17e4342598 | 3254 | } |
wolfSSL | 0:9d17e4342598 | 3255 | |
wolfSSL | 0:9d17e4342598 | 3256 | return ret; |
wolfSSL | 0:9d17e4342598 | 3257 | } |
wolfSSL | 0:9d17e4342598 | 3258 | |
wolfSSL | 0:9d17e4342598 | 3259 | |
wolfSSL | 0:9d17e4342598 | 3260 | static int DecodeKeyUsage(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 3261 | { |
wolfSSL | 0:9d17e4342598 | 3262 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 3263 | int length; |
wolfSSL | 0:9d17e4342598 | 3264 | byte unusedBits; |
wolfSSL | 0:9d17e4342598 | 3265 | CYASSL_ENTER("DecodeKeyUsage"); |
wolfSSL | 0:9d17e4342598 | 3266 | |
wolfSSL | 0:9d17e4342598 | 3267 | if (input[idx++] != ASN_BIT_STRING) { |
wolfSSL | 0:9d17e4342598 | 3268 | CYASSL_MSG("\tfail: key usage expected bit string"); |
wolfSSL | 0:9d17e4342598 | 3269 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3270 | } |
wolfSSL | 0:9d17e4342598 | 3271 | |
wolfSSL | 0:9d17e4342598 | 3272 | if (GetLength(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3273 | CYASSL_MSG("\tfail: key usage bad length"); |
wolfSSL | 0:9d17e4342598 | 3274 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3275 | } |
wolfSSL | 0:9d17e4342598 | 3276 | |
wolfSSL | 0:9d17e4342598 | 3277 | unusedBits = input[idx++]; |
wolfSSL | 0:9d17e4342598 | 3278 | length--; |
wolfSSL | 0:9d17e4342598 | 3279 | |
wolfSSL | 0:9d17e4342598 | 3280 | if (length == 2) { |
wolfSSL | 0:9d17e4342598 | 3281 | cert->extKeyUsage = (word16)((input[idx] << 8) | input[idx+1]); |
wolfSSL | 0:9d17e4342598 | 3282 | cert->extKeyUsage >>= unusedBits; |
wolfSSL | 0:9d17e4342598 | 3283 | } |
wolfSSL | 0:9d17e4342598 | 3284 | else if (length == 1) |
wolfSSL | 0:9d17e4342598 | 3285 | cert->extKeyUsage = (word16)(input[idx] << 1); |
wolfSSL | 0:9d17e4342598 | 3286 | |
wolfSSL | 0:9d17e4342598 | 3287 | return 0; |
wolfSSL | 0:9d17e4342598 | 3288 | } |
wolfSSL | 0:9d17e4342598 | 3289 | |
wolfSSL | 0:9d17e4342598 | 3290 | |
wolfSSL | 0:9d17e4342598 | 3291 | static int DecodeExtKeyUsage(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 3292 | { |
wolfSSL | 0:9d17e4342598 | 3293 | word32 idx = 0, oid; |
wolfSSL | 0:9d17e4342598 | 3294 | int length; |
wolfSSL | 0:9d17e4342598 | 3295 | |
wolfSSL | 0:9d17e4342598 | 3296 | CYASSL_ENTER("DecodeExtKeyUsage"); |
wolfSSL | 0:9d17e4342598 | 3297 | |
wolfSSL | 0:9d17e4342598 | 3298 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3299 | CYASSL_MSG("\tfail: should be a SEQUENCE\n"); |
wolfSSL | 0:9d17e4342598 | 3300 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3301 | } |
wolfSSL | 0:9d17e4342598 | 3302 | |
wolfSSL | 0:9d17e4342598 | 3303 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3304 | cert->extExtKeyUsageSrc = input + idx; |
wolfSSL | 0:9d17e4342598 | 3305 | cert->extExtKeyUsageSz = length; |
wolfSSL | 0:9d17e4342598 | 3306 | #endif |
wolfSSL | 0:9d17e4342598 | 3307 | |
wolfSSL | 0:9d17e4342598 | 3308 | while (idx < (word32)sz) { |
wolfSSL | 0:9d17e4342598 | 3309 | if (GetObjectId(input, &idx, &oid, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3310 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3311 | |
wolfSSL | 0:9d17e4342598 | 3312 | switch (oid) { |
wolfSSL | 0:9d17e4342598 | 3313 | case EKU_ANY_OID: |
wolfSSL | 0:9d17e4342598 | 3314 | cert->extExtKeyUsage |= EXTKEYUSE_ANY; |
wolfSSL | 0:9d17e4342598 | 3315 | break; |
wolfSSL | 0:9d17e4342598 | 3316 | case EKU_SERVER_AUTH_OID: |
wolfSSL | 0:9d17e4342598 | 3317 | cert->extExtKeyUsage |= EXTKEYUSE_SERVER_AUTH; |
wolfSSL | 0:9d17e4342598 | 3318 | break; |
wolfSSL | 0:9d17e4342598 | 3319 | case EKU_CLIENT_AUTH_OID: |
wolfSSL | 0:9d17e4342598 | 3320 | cert->extExtKeyUsage |= EXTKEYUSE_CLIENT_AUTH; |
wolfSSL | 0:9d17e4342598 | 3321 | break; |
wolfSSL | 0:9d17e4342598 | 3322 | case EKU_OCSP_SIGN_OID: |
wolfSSL | 0:9d17e4342598 | 3323 | cert->extExtKeyUsage |= EXTKEYUSE_OCSP_SIGN; |
wolfSSL | 0:9d17e4342598 | 3324 | break; |
wolfSSL | 0:9d17e4342598 | 3325 | } |
wolfSSL | 0:9d17e4342598 | 3326 | |
wolfSSL | 0:9d17e4342598 | 3327 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3328 | cert->extExtKeyUsageCount++; |
wolfSSL | 0:9d17e4342598 | 3329 | #endif |
wolfSSL | 0:9d17e4342598 | 3330 | } |
wolfSSL | 0:9d17e4342598 | 3331 | |
wolfSSL | 0:9d17e4342598 | 3332 | return 0; |
wolfSSL | 0:9d17e4342598 | 3333 | } |
wolfSSL | 0:9d17e4342598 | 3334 | |
wolfSSL | 0:9d17e4342598 | 3335 | |
wolfSSL | 0:9d17e4342598 | 3336 | #ifdef CYASSL_SEP |
wolfSSL | 0:9d17e4342598 | 3337 | static int DecodeCertPolicy(byte* input, int sz, DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 3338 | { |
wolfSSL | 0:9d17e4342598 | 3339 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 3340 | int length = 0; |
wolfSSL | 0:9d17e4342598 | 3341 | |
wolfSSL | 0:9d17e4342598 | 3342 | CYASSL_ENTER("DecodeCertPolicy"); |
wolfSSL | 0:9d17e4342598 | 3343 | |
wolfSSL | 0:9d17e4342598 | 3344 | /* Unwrap certificatePolicies */ |
wolfSSL | 0:9d17e4342598 | 3345 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3346 | CYASSL_MSG("\tdeviceType isn't OID"); |
wolfSSL | 0:9d17e4342598 | 3347 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3348 | } |
wolfSSL | 0:9d17e4342598 | 3349 | |
wolfSSL | 0:9d17e4342598 | 3350 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3351 | CYASSL_MSG("\tdeviceType isn't OID"); |
wolfSSL | 0:9d17e4342598 | 3352 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3353 | } |
wolfSSL | 0:9d17e4342598 | 3354 | |
wolfSSL | 0:9d17e4342598 | 3355 | if (input[idx++] != ASN_OBJECT_ID) { |
wolfSSL | 0:9d17e4342598 | 3356 | CYASSL_MSG("\tdeviceType isn't OID"); |
wolfSSL | 0:9d17e4342598 | 3357 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3358 | } |
wolfSSL | 0:9d17e4342598 | 3359 | |
wolfSSL | 0:9d17e4342598 | 3360 | if (GetLength(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3361 | CYASSL_MSG("\tCouldn't read length of deviceType"); |
wolfSSL | 0:9d17e4342598 | 3362 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3363 | } |
wolfSSL | 0:9d17e4342598 | 3364 | |
wolfSSL | 0:9d17e4342598 | 3365 | if (length > 0) { |
wolfSSL | 0:9d17e4342598 | 3366 | cert->deviceType = (byte*)XMALLOC(length, cert->heap, 0); |
wolfSSL | 0:9d17e4342598 | 3367 | if (cert->deviceType == NULL) { |
wolfSSL | 0:9d17e4342598 | 3368 | CYASSL_MSG("\tCouldn't alloc memory for deviceType"); |
wolfSSL | 0:9d17e4342598 | 3369 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 3370 | } |
wolfSSL | 0:9d17e4342598 | 3371 | cert->deviceTypeSz = length; |
wolfSSL | 0:9d17e4342598 | 3372 | XMEMCPY(cert->deviceType, input + idx, length); |
wolfSSL | 0:9d17e4342598 | 3373 | } |
wolfSSL | 0:9d17e4342598 | 3374 | |
wolfSSL | 0:9d17e4342598 | 3375 | CYASSL_LEAVE("DecodeCertPolicy", 0); |
wolfSSL | 0:9d17e4342598 | 3376 | return 0; |
wolfSSL | 0:9d17e4342598 | 3377 | } |
wolfSSL | 0:9d17e4342598 | 3378 | #endif /* CYASSL_SEP */ |
wolfSSL | 0:9d17e4342598 | 3379 | |
wolfSSL | 0:9d17e4342598 | 3380 | |
wolfSSL | 0:9d17e4342598 | 3381 | static int DecodeCertExtensions(DecodedCert* cert) |
wolfSSL | 0:9d17e4342598 | 3382 | /* |
wolfSSL | 0:9d17e4342598 | 3383 | * Processing the Certificate Extensions. This does not modify the current |
wolfSSL | 0:9d17e4342598 | 3384 | * index. It is works starting with the recorded extensions pointer. |
wolfSSL | 0:9d17e4342598 | 3385 | */ |
wolfSSL | 0:9d17e4342598 | 3386 | { |
wolfSSL | 0:9d17e4342598 | 3387 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 3388 | int sz = cert->extensionsSz; |
wolfSSL | 0:9d17e4342598 | 3389 | byte* input = cert->extensions; |
wolfSSL | 0:9d17e4342598 | 3390 | int length; |
wolfSSL | 0:9d17e4342598 | 3391 | word32 oid; |
wolfSSL | 0:9d17e4342598 | 3392 | byte critical = 0; |
wolfSSL | 0:9d17e4342598 | 3393 | byte criticalFail = 0; |
wolfSSL | 0:9d17e4342598 | 3394 | |
wolfSSL | 0:9d17e4342598 | 3395 | CYASSL_ENTER("DecodeCertExtensions"); |
wolfSSL | 0:9d17e4342598 | 3396 | |
wolfSSL | 0:9d17e4342598 | 3397 | if (input == NULL || sz == 0) |
wolfSSL | 0:9d17e4342598 | 3398 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3399 | |
wolfSSL | 0:9d17e4342598 | 3400 | if (input[idx++] != ASN_EXTENSIONS) |
wolfSSL | 0:9d17e4342598 | 3401 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3402 | |
wolfSSL | 0:9d17e4342598 | 3403 | if (GetLength(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3404 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3405 | |
wolfSSL | 0:9d17e4342598 | 3406 | if (GetSequence(input, &idx, &length, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 3407 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3408 | |
wolfSSL | 0:9d17e4342598 | 3409 | while (idx < (word32)sz) { |
wolfSSL | 0:9d17e4342598 | 3410 | if (GetSequence(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3411 | CYASSL_MSG("\tfail: should be a SEQUENCE"); |
wolfSSL | 0:9d17e4342598 | 3412 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3413 | } |
wolfSSL | 0:9d17e4342598 | 3414 | |
wolfSSL | 0:9d17e4342598 | 3415 | oid = 0; |
wolfSSL | 0:9d17e4342598 | 3416 | if (GetObjectId(input, &idx, &oid, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3417 | CYASSL_MSG("\tfail: OBJECT ID"); |
wolfSSL | 0:9d17e4342598 | 3418 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3419 | } |
wolfSSL | 0:9d17e4342598 | 3420 | |
wolfSSL | 0:9d17e4342598 | 3421 | /* check for critical flag */ |
wolfSSL | 0:9d17e4342598 | 3422 | critical = 0; |
wolfSSL | 0:9d17e4342598 | 3423 | if (input[idx] == ASN_BOOLEAN) { |
wolfSSL | 0:9d17e4342598 | 3424 | int boolLength = 0; |
wolfSSL | 0:9d17e4342598 | 3425 | idx++; |
wolfSSL | 0:9d17e4342598 | 3426 | if (GetLength(input, &idx, &boolLength, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3427 | CYASSL_MSG("\tfail: critical boolean length"); |
wolfSSL | 0:9d17e4342598 | 3428 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3429 | } |
wolfSSL | 0:9d17e4342598 | 3430 | if (input[idx++]) |
wolfSSL | 0:9d17e4342598 | 3431 | critical = 1; |
wolfSSL | 0:9d17e4342598 | 3432 | } |
wolfSSL | 0:9d17e4342598 | 3433 | |
wolfSSL | 0:9d17e4342598 | 3434 | /* process the extension based on the OID */ |
wolfSSL | 0:9d17e4342598 | 3435 | if (input[idx++] != ASN_OCTET_STRING) { |
wolfSSL | 0:9d17e4342598 | 3436 | CYASSL_MSG("\tfail: should be an OCTET STRING"); |
wolfSSL | 0:9d17e4342598 | 3437 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3438 | } |
wolfSSL | 0:9d17e4342598 | 3439 | |
wolfSSL | 0:9d17e4342598 | 3440 | if (GetLength(input, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 3441 | CYASSL_MSG("\tfail: extension data length"); |
wolfSSL | 0:9d17e4342598 | 3442 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3443 | } |
wolfSSL | 0:9d17e4342598 | 3444 | |
wolfSSL | 0:9d17e4342598 | 3445 | switch (oid) { |
wolfSSL | 0:9d17e4342598 | 3446 | case BASIC_CA_OID: |
wolfSSL | 0:9d17e4342598 | 3447 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3448 | cert->extBasicConstSet = 1; |
wolfSSL | 0:9d17e4342598 | 3449 | cert->extBasicConstCrit = critical; |
wolfSSL | 0:9d17e4342598 | 3450 | #endif |
wolfSSL | 0:9d17e4342598 | 3451 | if (DecodeBasicCaConstraint(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3452 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3453 | break; |
wolfSSL | 0:9d17e4342598 | 3454 | |
wolfSSL | 0:9d17e4342598 | 3455 | case CRL_DIST_OID: |
wolfSSL | 0:9d17e4342598 | 3456 | if (DecodeCrlDist(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3457 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3458 | break; |
wolfSSL | 0:9d17e4342598 | 3459 | |
wolfSSL | 0:9d17e4342598 | 3460 | case AUTH_INFO_OID: |
wolfSSL | 0:9d17e4342598 | 3461 | if (DecodeAuthInfo(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3462 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3463 | break; |
wolfSSL | 0:9d17e4342598 | 3464 | |
wolfSSL | 0:9d17e4342598 | 3465 | case ALT_NAMES_OID: |
wolfSSL | 0:9d17e4342598 | 3466 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3467 | cert->extSubjAltNameSet = 1; |
wolfSSL | 0:9d17e4342598 | 3468 | cert->extSubjAltNameCrit = critical; |
wolfSSL | 0:9d17e4342598 | 3469 | #endif |
wolfSSL | 0:9d17e4342598 | 3470 | if (DecodeAltNames(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3471 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3472 | break; |
wolfSSL | 0:9d17e4342598 | 3473 | |
wolfSSL | 0:9d17e4342598 | 3474 | case AUTH_KEY_OID: |
wolfSSL | 0:9d17e4342598 | 3475 | cert->extAuthKeyIdSet = 1; |
wolfSSL | 0:9d17e4342598 | 3476 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3477 | cert->extAuthKeyIdCrit = critical; |
wolfSSL | 0:9d17e4342598 | 3478 | #endif |
wolfSSL | 0:9d17e4342598 | 3479 | if (DecodeAuthKeyId(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3480 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3481 | break; |
wolfSSL | 0:9d17e4342598 | 3482 | |
wolfSSL | 0:9d17e4342598 | 3483 | case SUBJ_KEY_OID: |
wolfSSL | 0:9d17e4342598 | 3484 | cert->extSubjKeyIdSet = 1; |
wolfSSL | 0:9d17e4342598 | 3485 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3486 | cert->extSubjKeyIdCrit = critical; |
wolfSSL | 0:9d17e4342598 | 3487 | #endif |
wolfSSL | 0:9d17e4342598 | 3488 | if (DecodeSubjKeyId(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3489 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3490 | break; |
wolfSSL | 0:9d17e4342598 | 3491 | |
wolfSSL | 0:9d17e4342598 | 3492 | case CERT_POLICY_OID: |
wolfSSL | 0:9d17e4342598 | 3493 | CYASSL_MSG("Certificate Policy extension not supported yet."); |
wolfSSL | 0:9d17e4342598 | 3494 | #ifdef CYASSL_SEP |
wolfSSL | 0:9d17e4342598 | 3495 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3496 | cert->extCertPolicySet = 1; |
wolfSSL | 0:9d17e4342598 | 3497 | cert->extCertPolicyCrit = critical; |
wolfSSL | 0:9d17e4342598 | 3498 | #endif |
wolfSSL | 0:9d17e4342598 | 3499 | if (DecodeCertPolicy(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3500 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3501 | #endif |
wolfSSL | 0:9d17e4342598 | 3502 | break; |
wolfSSL | 0:9d17e4342598 | 3503 | |
wolfSSL | 0:9d17e4342598 | 3504 | case KEY_USAGE_OID: |
wolfSSL | 0:9d17e4342598 | 3505 | cert->extKeyUsageSet = 1; |
wolfSSL | 0:9d17e4342598 | 3506 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3507 | cert->extKeyUsageCrit = critical; |
wolfSSL | 0:9d17e4342598 | 3508 | #endif |
wolfSSL | 0:9d17e4342598 | 3509 | if (DecodeKeyUsage(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3510 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3511 | break; |
wolfSSL | 0:9d17e4342598 | 3512 | |
wolfSSL | 0:9d17e4342598 | 3513 | case EXT_KEY_USAGE_OID: |
wolfSSL | 0:9d17e4342598 | 3514 | cert->extExtKeyUsageSet = 1; |
wolfSSL | 0:9d17e4342598 | 3515 | #ifdef OPENSSL_EXTRA |
wolfSSL | 0:9d17e4342598 | 3516 | cert->extExtKeyUsageCrit = critical; |
wolfSSL | 0:9d17e4342598 | 3517 | #endif |
wolfSSL | 0:9d17e4342598 | 3518 | if (DecodeExtKeyUsage(&input[idx], length, cert) < 0) |
wolfSSL | 0:9d17e4342598 | 3519 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 3520 | break; |
wolfSSL | 0:9d17e4342598 | 3521 | |
wolfSSL | 0:9d17e4342598 | 3522 | case INHIBIT_ANY_OID: |
wolfSSL | 0:9d17e4342598 | 3523 | CYASSL_MSG("Inhibit anyPolicy extension not supported yet."); |
wolfSSL | 0:9d17e4342598 | 3524 | break; |
wolfSSL | 0:9d17e4342598 | 3525 | |
wolfSSL | 0:9d17e4342598 | 3526 | default: |
wolfSSL | 0:9d17e4342598 | 3527 | /* While it is a failure to not support critical extensions, |
wolfSSL | 0:9d17e4342598 | 3528 | * still parse the certificate ignoring the unsupported |
wolfSSL | 0:9d17e4342598 | 3529 | * extention to allow caller to accept it with the verify |
wolfSSL | 0:9d17e4342598 | 3530 | * callback. */ |
wolfSSL | 0:9d17e4342598 | 3531 | if (critical) |
wolfSSL | 0:9d17e4342598 | 3532 | criticalFail = 1; |
wolfSSL | 0:9d17e4342598 | 3533 | break; |
wolfSSL | 0:9d17e4342598 | 3534 | } |
wolfSSL | 0:9d17e4342598 | 3535 | idx += length; |
wolfSSL | 0:9d17e4342598 | 3536 | } |
wolfSSL | 0:9d17e4342598 | 3537 | |
wolfSSL | 0:9d17e4342598 | 3538 | return criticalFail ? ASN_CRIT_EXT_E : 0; |
wolfSSL | 0:9d17e4342598 | 3539 | } |
wolfSSL | 0:9d17e4342598 | 3540 | |
wolfSSL | 0:9d17e4342598 | 3541 | |
wolfSSL | 0:9d17e4342598 | 3542 | int ParseCert(DecodedCert* cert, int type, int verify, void* cm) |
wolfSSL | 0:9d17e4342598 | 3543 | { |
wolfSSL | 0:9d17e4342598 | 3544 | int ret; |
wolfSSL | 0:9d17e4342598 | 3545 | char* ptr; |
wolfSSL | 0:9d17e4342598 | 3546 | |
wolfSSL | 0:9d17e4342598 | 3547 | ret = ParseCertRelative(cert, type, verify, cm); |
wolfSSL | 0:9d17e4342598 | 3548 | if (ret < 0) |
wolfSSL | 0:9d17e4342598 | 3549 | return ret; |
wolfSSL | 0:9d17e4342598 | 3550 | |
wolfSSL | 0:9d17e4342598 | 3551 | if (cert->subjectCNLen > 0) { |
wolfSSL | 0:9d17e4342598 | 3552 | ptr = (char*) XMALLOC(cert->subjectCNLen + 1, cert->heap, |
wolfSSL | 0:9d17e4342598 | 3553 | DYNAMIC_TYPE_SUBJECT_CN); |
wolfSSL | 0:9d17e4342598 | 3554 | if (ptr == NULL) |
wolfSSL | 0:9d17e4342598 | 3555 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 3556 | XMEMCPY(ptr, cert->subjectCN, cert->subjectCNLen); |
wolfSSL | 0:9d17e4342598 | 3557 | ptr[cert->subjectCNLen] = '\0'; |
wolfSSL | 0:9d17e4342598 | 3558 | cert->subjectCN = ptr; |
wolfSSL | 0:9d17e4342598 | 3559 | cert->subjectCNStored = 1; |
wolfSSL | 0:9d17e4342598 | 3560 | } |
wolfSSL | 0:9d17e4342598 | 3561 | |
wolfSSL | 0:9d17e4342598 | 3562 | if (cert->keyOID == RSAk && |
wolfSSL | 0:9d17e4342598 | 3563 | cert->publicKey != NULL && cert->pubKeySize > 0) { |
wolfSSL | 0:9d17e4342598 | 3564 | ptr = (char*) XMALLOC(cert->pubKeySize, cert->heap, |
wolfSSL | 0:9d17e4342598 | 3565 | DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 0:9d17e4342598 | 3566 | if (ptr == NULL) |
wolfSSL | 0:9d17e4342598 | 3567 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 3568 | XMEMCPY(ptr, cert->publicKey, cert->pubKeySize); |
wolfSSL | 0:9d17e4342598 | 3569 | cert->publicKey = (byte *)ptr; |
wolfSSL | 0:9d17e4342598 | 3570 | cert->pubKeyStored = 1; |
wolfSSL | 0:9d17e4342598 | 3571 | } |
wolfSSL | 0:9d17e4342598 | 3572 | |
wolfSSL | 0:9d17e4342598 | 3573 | return ret; |
wolfSSL | 0:9d17e4342598 | 3574 | } |
wolfSSL | 0:9d17e4342598 | 3575 | |
wolfSSL | 0:9d17e4342598 | 3576 | |
wolfSSL | 0:9d17e4342598 | 3577 | /* from SSL proper, for locking can't do find here anymore */ |
wolfSSL | 0:9d17e4342598 | 3578 | #ifdef __cplusplus |
wolfSSL | 0:9d17e4342598 | 3579 | extern "C" { |
wolfSSL | 0:9d17e4342598 | 3580 | #endif |
wolfSSL | 0:9d17e4342598 | 3581 | CYASSL_LOCAL Signer* GetCA(void* signers, byte* hash); |
wolfSSL | 0:9d17e4342598 | 3582 | #ifndef NO_SKID |
wolfSSL | 0:9d17e4342598 | 3583 | CYASSL_LOCAL Signer* GetCAByName(void* signers, byte* hash); |
wolfSSL | 0:9d17e4342598 | 3584 | #endif |
wolfSSL | 0:9d17e4342598 | 3585 | #ifdef __cplusplus |
wolfSSL | 0:9d17e4342598 | 3586 | } |
wolfSSL | 0:9d17e4342598 | 3587 | #endif |
wolfSSL | 0:9d17e4342598 | 3588 | |
wolfSSL | 0:9d17e4342598 | 3589 | |
wolfSSL | 0:9d17e4342598 | 3590 | int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm) |
wolfSSL | 0:9d17e4342598 | 3591 | { |
wolfSSL | 0:9d17e4342598 | 3592 | word32 confirmOID; |
wolfSSL | 0:9d17e4342598 | 3593 | int ret; |
wolfSSL | 0:9d17e4342598 | 3594 | int badDate = 0; |
wolfSSL | 0:9d17e4342598 | 3595 | int criticalExt = 0; |
wolfSSL | 0:9d17e4342598 | 3596 | |
wolfSSL | 0:9d17e4342598 | 3597 | if ((ret = DecodeToKey(cert, verify)) < 0) { |
wolfSSL | 0:9d17e4342598 | 3598 | if (ret == ASN_BEFORE_DATE_E || ret == ASN_AFTER_DATE_E) |
wolfSSL | 0:9d17e4342598 | 3599 | badDate = ret; |
wolfSSL | 0:9d17e4342598 | 3600 | else |
wolfSSL | 0:9d17e4342598 | 3601 | return ret; |
wolfSSL | 0:9d17e4342598 | 3602 | } |
wolfSSL | 0:9d17e4342598 | 3603 | |
wolfSSL | 0:9d17e4342598 | 3604 | CYASSL_MSG("Parsed Past Key"); |
wolfSSL | 0:9d17e4342598 | 3605 | |
wolfSSL | 0:9d17e4342598 | 3606 | if (cert->srcIdx != cert->sigIndex) { |
wolfSSL | 0:9d17e4342598 | 3607 | if (cert->srcIdx < cert->sigIndex) { |
wolfSSL | 0:9d17e4342598 | 3608 | /* save extensions */ |
wolfSSL | 0:9d17e4342598 | 3609 | cert->extensions = &cert->source[cert->srcIdx]; |
wolfSSL | 0:9d17e4342598 | 3610 | cert->extensionsSz = cert->sigIndex - cert->srcIdx; |
wolfSSL | 0:9d17e4342598 | 3611 | cert->extensionsIdx = cert->srcIdx; /* for potential later use */ |
wolfSSL | 0:9d17e4342598 | 3612 | } |
wolfSSL | 0:9d17e4342598 | 3613 | if ((ret = DecodeCertExtensions(cert)) < 0) { |
wolfSSL | 0:9d17e4342598 | 3614 | if (ret == ASN_CRIT_EXT_E) |
wolfSSL | 0:9d17e4342598 | 3615 | criticalExt = ret; |
wolfSSL | 0:9d17e4342598 | 3616 | else |
wolfSSL | 0:9d17e4342598 | 3617 | return ret; |
wolfSSL | 0:9d17e4342598 | 3618 | } |
wolfSSL | 0:9d17e4342598 | 3619 | |
wolfSSL | 0:9d17e4342598 | 3620 | /* advance past extensions */ |
wolfSSL | 0:9d17e4342598 | 3621 | cert->srcIdx = cert->sigIndex; |
wolfSSL | 0:9d17e4342598 | 3622 | } |
wolfSSL | 0:9d17e4342598 | 3623 | |
wolfSSL | 0:9d17e4342598 | 3624 | if ((ret = GetAlgoId(cert->source, &cert->srcIdx, &confirmOID, |
wolfSSL | 0:9d17e4342598 | 3625 | cert->maxIdx)) < 0) |
wolfSSL | 0:9d17e4342598 | 3626 | return ret; |
wolfSSL | 0:9d17e4342598 | 3627 | |
wolfSSL | 0:9d17e4342598 | 3628 | if ((ret = GetSignature(cert)) < 0) |
wolfSSL | 0:9d17e4342598 | 3629 | return ret; |
wolfSSL | 0:9d17e4342598 | 3630 | |
wolfSSL | 0:9d17e4342598 | 3631 | if (confirmOID != cert->signatureOID) |
wolfSSL | 0:9d17e4342598 | 3632 | return ASN_SIG_OID_E; |
wolfSSL | 0:9d17e4342598 | 3633 | |
wolfSSL | 0:9d17e4342598 | 3634 | #ifndef NO_SKID |
wolfSSL | 0:9d17e4342598 | 3635 | if (cert->extSubjKeyIdSet == 0 |
wolfSSL | 0:9d17e4342598 | 3636 | && cert->publicKey != NULL && cert->pubKeySize > 0) { |
wolfSSL | 0:9d17e4342598 | 3637 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 3638 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 3639 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 3640 | return ret; |
wolfSSL | 0:9d17e4342598 | 3641 | ShaUpdate(&sha, cert->publicKey, cert->pubKeySize); |
wolfSSL | 0:9d17e4342598 | 3642 | ShaFinal(&sha, cert->extSubjKeyId); |
wolfSSL | 0:9d17e4342598 | 3643 | } |
wolfSSL | 0:9d17e4342598 | 3644 | #endif |
wolfSSL | 0:9d17e4342598 | 3645 | |
wolfSSL | 0:9d17e4342598 | 3646 | if (verify && type != CA_TYPE) { |
wolfSSL | 0:9d17e4342598 | 3647 | Signer* ca = NULL; |
wolfSSL | 0:9d17e4342598 | 3648 | #ifndef NO_SKID |
wolfSSL | 0:9d17e4342598 | 3649 | if (cert->extAuthKeyIdSet) |
wolfSSL | 0:9d17e4342598 | 3650 | ca = GetCA(cm, cert->extAuthKeyId); |
wolfSSL | 0:9d17e4342598 | 3651 | if (ca == NULL) |
wolfSSL | 0:9d17e4342598 | 3652 | ca = GetCAByName(cm, cert->issuerHash); |
wolfSSL | 0:9d17e4342598 | 3653 | #else /* NO_SKID */ |
wolfSSL | 0:9d17e4342598 | 3654 | ca = GetCA(cm, cert->issuerHash); |
wolfSSL | 0:9d17e4342598 | 3655 | #endif /* NO SKID */ |
wolfSSL | 0:9d17e4342598 | 3656 | CYASSL_MSG("About to verify certificate signature"); |
wolfSSL | 0:9d17e4342598 | 3657 | |
wolfSSL | 0:9d17e4342598 | 3658 | if (ca) { |
wolfSSL | 0:9d17e4342598 | 3659 | #ifdef HAVE_OCSP |
wolfSSL | 0:9d17e4342598 | 3660 | /* Need the ca's public key hash for OCSP */ |
wolfSSL | 0:9d17e4342598 | 3661 | { |
wolfSSL | 0:9d17e4342598 | 3662 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 3663 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 3664 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 3665 | return ret; |
wolfSSL | 0:9d17e4342598 | 3666 | ShaUpdate(&sha, ca->publicKey, ca->pubKeySize); |
wolfSSL | 0:9d17e4342598 | 3667 | ShaFinal(&sha, cert->issuerKeyHash); |
wolfSSL | 0:9d17e4342598 | 3668 | } |
wolfSSL | 0:9d17e4342598 | 3669 | #endif /* HAVE_OCSP */ |
wolfSSL | 0:9d17e4342598 | 3670 | /* try to confirm/verify signature */ |
wolfSSL | 0:9d17e4342598 | 3671 | if (!ConfirmSignature(cert->source + cert->certBegin, |
wolfSSL | 0:9d17e4342598 | 3672 | cert->sigIndex - cert->certBegin, |
wolfSSL | 0:9d17e4342598 | 3673 | ca->publicKey, ca->pubKeySize, ca->keyOID, |
wolfSSL | 0:9d17e4342598 | 3674 | cert->signature, cert->sigLength, cert->signatureOID, |
wolfSSL | 0:9d17e4342598 | 3675 | cert->heap)) { |
wolfSSL | 0:9d17e4342598 | 3676 | CYASSL_MSG("Confirm signature failed"); |
wolfSSL | 0:9d17e4342598 | 3677 | return ASN_SIG_CONFIRM_E; |
wolfSSL | 0:9d17e4342598 | 3678 | } |
wolfSSL | 0:9d17e4342598 | 3679 | } |
wolfSSL | 0:9d17e4342598 | 3680 | else { |
wolfSSL | 0:9d17e4342598 | 3681 | /* no signer */ |
wolfSSL | 0:9d17e4342598 | 3682 | CYASSL_MSG("No CA signer to verify with"); |
wolfSSL | 0:9d17e4342598 | 3683 | return ASN_NO_SIGNER_E; |
wolfSSL | 0:9d17e4342598 | 3684 | } |
wolfSSL | 0:9d17e4342598 | 3685 | } |
wolfSSL | 0:9d17e4342598 | 3686 | |
wolfSSL | 0:9d17e4342598 | 3687 | if (badDate != 0) |
wolfSSL | 0:9d17e4342598 | 3688 | return badDate; |
wolfSSL | 0:9d17e4342598 | 3689 | |
wolfSSL | 0:9d17e4342598 | 3690 | if (criticalExt != 0) |
wolfSSL | 0:9d17e4342598 | 3691 | return criticalExt; |
wolfSSL | 0:9d17e4342598 | 3692 | |
wolfSSL | 0:9d17e4342598 | 3693 | return 0; |
wolfSSL | 0:9d17e4342598 | 3694 | } |
wolfSSL | 0:9d17e4342598 | 3695 | |
wolfSSL | 0:9d17e4342598 | 3696 | |
wolfSSL | 0:9d17e4342598 | 3697 | /* Create and init an new signer */ |
wolfSSL | 0:9d17e4342598 | 3698 | Signer* MakeSigner(void* heap) |
wolfSSL | 0:9d17e4342598 | 3699 | { |
wolfSSL | 0:9d17e4342598 | 3700 | Signer* signer = (Signer*) XMALLOC(sizeof(Signer), heap, |
wolfSSL | 0:9d17e4342598 | 3701 | DYNAMIC_TYPE_SIGNER); |
wolfSSL | 0:9d17e4342598 | 3702 | if (signer) { |
wolfSSL | 0:9d17e4342598 | 3703 | signer->pubKeySize = 0; |
wolfSSL | 0:9d17e4342598 | 3704 | signer->keyOID = 0; |
wolfSSL | 0:9d17e4342598 | 3705 | signer->publicKey = NULL; |
wolfSSL | 0:9d17e4342598 | 3706 | signer->nameLen = 0; |
wolfSSL | 0:9d17e4342598 | 3707 | signer->name = NULL; |
wolfSSL | 0:9d17e4342598 | 3708 | signer->next = NULL; |
wolfSSL | 0:9d17e4342598 | 3709 | } |
wolfSSL | 0:9d17e4342598 | 3710 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 3711 | |
wolfSSL | 0:9d17e4342598 | 3712 | return signer; |
wolfSSL | 0:9d17e4342598 | 3713 | } |
wolfSSL | 0:9d17e4342598 | 3714 | |
wolfSSL | 0:9d17e4342598 | 3715 | |
wolfSSL | 0:9d17e4342598 | 3716 | /* Free an individual signer */ |
wolfSSL | 0:9d17e4342598 | 3717 | void FreeSigner(Signer* signer, void* heap) |
wolfSSL | 0:9d17e4342598 | 3718 | { |
wolfSSL | 0:9d17e4342598 | 3719 | XFREE(signer->name, heap, DYNAMIC_TYPE_SUBJECT_CN); |
wolfSSL | 0:9d17e4342598 | 3720 | XFREE(signer->publicKey, heap, DYNAMIC_TYPE_PUBLIC_KEY); |
wolfSSL | 0:9d17e4342598 | 3721 | XFREE(signer, heap, DYNAMIC_TYPE_SIGNER); |
wolfSSL | 0:9d17e4342598 | 3722 | |
wolfSSL | 0:9d17e4342598 | 3723 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 3724 | } |
wolfSSL | 0:9d17e4342598 | 3725 | |
wolfSSL | 0:9d17e4342598 | 3726 | |
wolfSSL | 0:9d17e4342598 | 3727 | /* Free the whole singer table with number of rows */ |
wolfSSL | 0:9d17e4342598 | 3728 | void FreeSignerTable(Signer** table, int rows, void* heap) |
wolfSSL | 0:9d17e4342598 | 3729 | { |
wolfSSL | 0:9d17e4342598 | 3730 | int i; |
wolfSSL | 0:9d17e4342598 | 3731 | |
wolfSSL | 0:9d17e4342598 | 3732 | for (i = 0; i < rows; i++) { |
wolfSSL | 0:9d17e4342598 | 3733 | Signer* signer = table[i]; |
wolfSSL | 0:9d17e4342598 | 3734 | while (signer) { |
wolfSSL | 0:9d17e4342598 | 3735 | Signer* next = signer->next; |
wolfSSL | 0:9d17e4342598 | 3736 | FreeSigner(signer, heap); |
wolfSSL | 0:9d17e4342598 | 3737 | signer = next; |
wolfSSL | 0:9d17e4342598 | 3738 | } |
wolfSSL | 0:9d17e4342598 | 3739 | table[i] = NULL; |
wolfSSL | 0:9d17e4342598 | 3740 | } |
wolfSSL | 0:9d17e4342598 | 3741 | } |
wolfSSL | 0:9d17e4342598 | 3742 | |
wolfSSL | 0:9d17e4342598 | 3743 | |
wolfSSL | 0:9d17e4342598 | 3744 | CYASSL_LOCAL int SetMyVersion(word32 version, byte* output, int header) |
wolfSSL | 0:9d17e4342598 | 3745 | { |
wolfSSL | 0:9d17e4342598 | 3746 | int i = 0; |
wolfSSL | 0:9d17e4342598 | 3747 | |
wolfSSL | 0:9d17e4342598 | 3748 | if (header) { |
wolfSSL | 0:9d17e4342598 | 3749 | output[i++] = ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED; |
wolfSSL | 0:9d17e4342598 | 3750 | output[i++] = ASN_BIT_STRING; |
wolfSSL | 0:9d17e4342598 | 3751 | } |
wolfSSL | 0:9d17e4342598 | 3752 | output[i++] = ASN_INTEGER; |
wolfSSL | 0:9d17e4342598 | 3753 | output[i++] = 0x01; |
wolfSSL | 0:9d17e4342598 | 3754 | output[i++] = (byte)version; |
wolfSSL | 0:9d17e4342598 | 3755 | |
wolfSSL | 0:9d17e4342598 | 3756 | return i; |
wolfSSL | 0:9d17e4342598 | 3757 | } |
wolfSSL | 0:9d17e4342598 | 3758 | |
wolfSSL | 0:9d17e4342598 | 3759 | |
wolfSSL | 0:9d17e4342598 | 3760 | CYASSL_LOCAL int SetSerialNumber(const byte* sn, word32 snSz, byte* output) |
wolfSSL | 0:9d17e4342598 | 3761 | { |
wolfSSL | 0:9d17e4342598 | 3762 | int result = 0; |
wolfSSL | 0:9d17e4342598 | 3763 | |
wolfSSL | 0:9d17e4342598 | 3764 | CYASSL_ENTER("SetSerialNumber"); |
wolfSSL | 0:9d17e4342598 | 3765 | |
wolfSSL | 0:9d17e4342598 | 3766 | if (snSz <= EXTERNAL_SERIAL_SIZE) { |
wolfSSL | 0:9d17e4342598 | 3767 | output[0] = ASN_INTEGER; |
wolfSSL | 0:9d17e4342598 | 3768 | /* The serial number is always positive. When encoding the |
wolfSSL | 0:9d17e4342598 | 3769 | * INTEGER, if the MSB is 1, add a padding zero to keep the |
wolfSSL | 0:9d17e4342598 | 3770 | * number positive. */ |
wolfSSL | 0:9d17e4342598 | 3771 | if (sn[0] & 0x80) { |
wolfSSL | 0:9d17e4342598 | 3772 | output[1] = (byte)snSz + 1; |
wolfSSL | 0:9d17e4342598 | 3773 | output[2] = 0; |
wolfSSL | 0:9d17e4342598 | 3774 | XMEMCPY(&output[3], sn, snSz); |
wolfSSL | 0:9d17e4342598 | 3775 | result = snSz + 3; |
wolfSSL | 0:9d17e4342598 | 3776 | } |
wolfSSL | 0:9d17e4342598 | 3777 | else { |
wolfSSL | 0:9d17e4342598 | 3778 | output[1] = (byte)snSz; |
wolfSSL | 0:9d17e4342598 | 3779 | XMEMCPY(&output[2], sn, snSz); |
wolfSSL | 0:9d17e4342598 | 3780 | result = snSz + 2; |
wolfSSL | 0:9d17e4342598 | 3781 | } |
wolfSSL | 0:9d17e4342598 | 3782 | } |
wolfSSL | 0:9d17e4342598 | 3783 | return result; |
wolfSSL | 0:9d17e4342598 | 3784 | } |
wolfSSL | 0:9d17e4342598 | 3785 | |
wolfSSL | 0:9d17e4342598 | 3786 | |
wolfSSL | 0:9d17e4342598 | 3787 | |
wolfSSL | 0:9d17e4342598 | 3788 | |
wolfSSL | 0:9d17e4342598 | 3789 | #if defined(CYASSL_KEY_GEN) || defined(CYASSL_CERT_GEN) |
wolfSSL | 0:9d17e4342598 | 3790 | |
wolfSSL | 0:9d17e4342598 | 3791 | /* convert der buffer to pem into output, can't do inplace, der and output |
wolfSSL | 0:9d17e4342598 | 3792 | need to be different */ |
wolfSSL | 0:9d17e4342598 | 3793 | int DerToPem(const byte* der, word32 derSz, byte* output, word32 outSz, |
wolfSSL | 0:9d17e4342598 | 3794 | int type) |
wolfSSL | 0:9d17e4342598 | 3795 | { |
wolfSSL | 0:9d17e4342598 | 3796 | char header[80]; |
wolfSSL | 0:9d17e4342598 | 3797 | char footer[80]; |
wolfSSL | 0:9d17e4342598 | 3798 | |
wolfSSL | 0:9d17e4342598 | 3799 | int headerLen; |
wolfSSL | 0:9d17e4342598 | 3800 | int footerLen; |
wolfSSL | 0:9d17e4342598 | 3801 | int i; |
wolfSSL | 0:9d17e4342598 | 3802 | int err; |
wolfSSL | 0:9d17e4342598 | 3803 | int outLen; /* return length or error */ |
wolfSSL | 0:9d17e4342598 | 3804 | |
wolfSSL | 0:9d17e4342598 | 3805 | if (der == output) /* no in place conversion */ |
wolfSSL | 0:9d17e4342598 | 3806 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3807 | |
wolfSSL | 0:9d17e4342598 | 3808 | if (type == CERT_TYPE) { |
wolfSSL | 0:9d17e4342598 | 3809 | XSTRNCPY(header, "-----BEGIN CERTIFICATE-----\n", sizeof(header)); |
wolfSSL | 0:9d17e4342598 | 3810 | XSTRNCPY(footer, "-----END CERTIFICATE-----\n", sizeof(footer)); |
wolfSSL | 0:9d17e4342598 | 3811 | } |
wolfSSL | 0:9d17e4342598 | 3812 | else if (type == PRIVATEKEY_TYPE) { |
wolfSSL | 0:9d17e4342598 | 3813 | XSTRNCPY(header, "-----BEGIN RSA PRIVATE KEY-----\n", sizeof(header)); |
wolfSSL | 0:9d17e4342598 | 3814 | XSTRNCPY(footer, "-----END RSA PRIVATE KEY-----\n", sizeof(footer)); |
wolfSSL | 0:9d17e4342598 | 3815 | } |
wolfSSL | 0:9d17e4342598 | 3816 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 3817 | else if (type == ECC_PRIVATEKEY_TYPE) { |
wolfSSL | 0:9d17e4342598 | 3818 | XSTRNCPY(header, "-----BEGIN EC PRIVATE KEY-----\n", sizeof(header)); |
wolfSSL | 0:9d17e4342598 | 3819 | XSTRNCPY(footer, "-----END EC PRIVATE KEY-----\n", sizeof(footer)); |
wolfSSL | 0:9d17e4342598 | 3820 | } |
wolfSSL | 0:9d17e4342598 | 3821 | #endif |
wolfSSL | 0:9d17e4342598 | 3822 | #ifdef CYASSL_CERT_REQ |
wolfSSL | 0:9d17e4342598 | 3823 | else if (type == CERTREQ_TYPE) |
wolfSSL | 0:9d17e4342598 | 3824 | { |
wolfSSL | 0:9d17e4342598 | 3825 | XSTRNCPY(header, |
wolfSSL | 0:9d17e4342598 | 3826 | "-----BEGIN CERTIFICATE REQUEST-----\n", sizeof(header)); |
wolfSSL | 0:9d17e4342598 | 3827 | XSTRNCPY(footer, "-----END CERTIFICATE REQUEST-----\n", sizeof(footer)); |
wolfSSL | 0:9d17e4342598 | 3828 | } |
wolfSSL | 0:9d17e4342598 | 3829 | #endif |
wolfSSL | 0:9d17e4342598 | 3830 | else |
wolfSSL | 0:9d17e4342598 | 3831 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3832 | |
wolfSSL | 0:9d17e4342598 | 3833 | headerLen = (int)XSTRLEN(header); |
wolfSSL | 0:9d17e4342598 | 3834 | footerLen = (int)XSTRLEN(footer); |
wolfSSL | 0:9d17e4342598 | 3835 | |
wolfSSL | 0:9d17e4342598 | 3836 | if (!der || !output) |
wolfSSL | 0:9d17e4342598 | 3837 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3838 | |
wolfSSL | 0:9d17e4342598 | 3839 | /* don't even try if outSz too short */ |
wolfSSL | 0:9d17e4342598 | 3840 | if (outSz < headerLen + footerLen + derSz) |
wolfSSL | 0:9d17e4342598 | 3841 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3842 | |
wolfSSL | 0:9d17e4342598 | 3843 | /* header */ |
wolfSSL | 0:9d17e4342598 | 3844 | XMEMCPY(output, header, headerLen); |
wolfSSL | 0:9d17e4342598 | 3845 | i = headerLen; |
wolfSSL | 0:9d17e4342598 | 3846 | |
wolfSSL | 0:9d17e4342598 | 3847 | /* body */ |
wolfSSL | 0:9d17e4342598 | 3848 | outLen = outSz - (headerLen + footerLen); /* input to Base64_Encode */ |
wolfSSL | 0:9d17e4342598 | 3849 | if ( (err = Base64_Encode(der, derSz, output + i, (word32*)&outLen)) < 0) |
wolfSSL | 0:9d17e4342598 | 3850 | return err; |
wolfSSL | 0:9d17e4342598 | 3851 | i += outLen; |
wolfSSL | 0:9d17e4342598 | 3852 | |
wolfSSL | 0:9d17e4342598 | 3853 | /* footer */ |
wolfSSL | 0:9d17e4342598 | 3854 | if ( (i + footerLen) > (int)outSz) |
wolfSSL | 0:9d17e4342598 | 3855 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3856 | XMEMCPY(output + i, footer, footerLen); |
wolfSSL | 0:9d17e4342598 | 3857 | |
wolfSSL | 0:9d17e4342598 | 3858 | return outLen + headerLen + footerLen; |
wolfSSL | 0:9d17e4342598 | 3859 | } |
wolfSSL | 0:9d17e4342598 | 3860 | |
wolfSSL | 0:9d17e4342598 | 3861 | |
wolfSSL | 0:9d17e4342598 | 3862 | #endif /* CYASSL_KEY_GEN || CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 3863 | |
wolfSSL | 0:9d17e4342598 | 3864 | |
wolfSSL | 0:9d17e4342598 | 3865 | #if defined(CYASSL_KEY_GEN) && !defined(NO_RSA) |
wolfSSL | 0:9d17e4342598 | 3866 | |
wolfSSL | 0:9d17e4342598 | 3867 | |
wolfSSL | 0:9d17e4342598 | 3868 | static mp_int* GetRsaInt(RsaKey* key, int idx) |
wolfSSL | 0:9d17e4342598 | 3869 | { |
wolfSSL | 0:9d17e4342598 | 3870 | if (idx == 0) |
wolfSSL | 0:9d17e4342598 | 3871 | return &key->n; |
wolfSSL | 0:9d17e4342598 | 3872 | if (idx == 1) |
wolfSSL | 0:9d17e4342598 | 3873 | return &key->e; |
wolfSSL | 0:9d17e4342598 | 3874 | if (idx == 2) |
wolfSSL | 0:9d17e4342598 | 3875 | return &key->d; |
wolfSSL | 0:9d17e4342598 | 3876 | if (idx == 3) |
wolfSSL | 0:9d17e4342598 | 3877 | return &key->p; |
wolfSSL | 0:9d17e4342598 | 3878 | if (idx == 4) |
wolfSSL | 0:9d17e4342598 | 3879 | return &key->q; |
wolfSSL | 0:9d17e4342598 | 3880 | if (idx == 5) |
wolfSSL | 0:9d17e4342598 | 3881 | return &key->dP; |
wolfSSL | 0:9d17e4342598 | 3882 | if (idx == 6) |
wolfSSL | 0:9d17e4342598 | 3883 | return &key->dQ; |
wolfSSL | 0:9d17e4342598 | 3884 | if (idx == 7) |
wolfSSL | 0:9d17e4342598 | 3885 | return &key->u; |
wolfSSL | 0:9d17e4342598 | 3886 | |
wolfSSL | 0:9d17e4342598 | 3887 | return NULL; |
wolfSSL | 0:9d17e4342598 | 3888 | } |
wolfSSL | 0:9d17e4342598 | 3889 | |
wolfSSL | 0:9d17e4342598 | 3890 | |
wolfSSL | 0:9d17e4342598 | 3891 | /* Release Tmp RSA resources */ |
wolfSSL | 0:9d17e4342598 | 3892 | static INLINE void FreeTmpRsas(byte** tmps, void* heap) |
wolfSSL | 0:9d17e4342598 | 3893 | { |
wolfSSL | 0:9d17e4342598 | 3894 | int i; |
wolfSSL | 0:9d17e4342598 | 3895 | |
wolfSSL | 0:9d17e4342598 | 3896 | (void)heap; |
wolfSSL | 0:9d17e4342598 | 3897 | |
wolfSSL | 0:9d17e4342598 | 3898 | for (i = 0; i < RSA_INTS; i++) |
wolfSSL | 0:9d17e4342598 | 3899 | XFREE(tmps[i], heap, DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 3900 | } |
wolfSSL | 0:9d17e4342598 | 3901 | |
wolfSSL | 0:9d17e4342598 | 3902 | |
wolfSSL | 0:9d17e4342598 | 3903 | /* Convert RsaKey key to DER format, write to output (inLen), return bytes |
wolfSSL | 0:9d17e4342598 | 3904 | written */ |
wolfSSL | 0:9d17e4342598 | 3905 | int RsaKeyToDer(RsaKey* key, byte* output, word32 inLen) |
wolfSSL | 0:9d17e4342598 | 3906 | { |
wolfSSL | 0:9d17e4342598 | 3907 | word32 seqSz, verSz, rawLen, intTotalLen = 0; |
wolfSSL | 0:9d17e4342598 | 3908 | word32 sizes[RSA_INTS]; |
wolfSSL | 0:9d17e4342598 | 3909 | int i, j, outLen, ret = 0; |
wolfSSL | 0:9d17e4342598 | 3910 | |
wolfSSL | 0:9d17e4342598 | 3911 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 3912 | byte ver[MAX_VERSION_SZ]; |
wolfSSL | 0:9d17e4342598 | 3913 | byte* tmps[RSA_INTS]; |
wolfSSL | 0:9d17e4342598 | 3914 | |
wolfSSL | 0:9d17e4342598 | 3915 | if (!key || !output) |
wolfSSL | 0:9d17e4342598 | 3916 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3917 | |
wolfSSL | 0:9d17e4342598 | 3918 | if (key->type != RSA_PRIVATE) |
wolfSSL | 0:9d17e4342598 | 3919 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3920 | |
wolfSSL | 0:9d17e4342598 | 3921 | for (i = 0; i < RSA_INTS; i++) |
wolfSSL | 0:9d17e4342598 | 3922 | tmps[i] = NULL; |
wolfSSL | 0:9d17e4342598 | 3923 | |
wolfSSL | 0:9d17e4342598 | 3924 | /* write all big ints from key to DER tmps */ |
wolfSSL | 0:9d17e4342598 | 3925 | for (i = 0; i < RSA_INTS; i++) { |
wolfSSL | 0:9d17e4342598 | 3926 | mp_int* keyInt = GetRsaInt(key, i); |
wolfSSL | 0:9d17e4342598 | 3927 | rawLen = mp_unsigned_bin_size(keyInt); |
wolfSSL | 0:9d17e4342598 | 3928 | tmps[i] = (byte*)XMALLOC(rawLen + MAX_SEQ_SZ, key->heap, |
wolfSSL | 0:9d17e4342598 | 3929 | DYNAMIC_TYPE_RSA); |
wolfSSL | 0:9d17e4342598 | 3930 | if (tmps[i] == NULL) { |
wolfSSL | 0:9d17e4342598 | 3931 | ret = MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 3932 | break; |
wolfSSL | 0:9d17e4342598 | 3933 | } |
wolfSSL | 0:9d17e4342598 | 3934 | |
wolfSSL | 0:9d17e4342598 | 3935 | tmps[i][0] = ASN_INTEGER; |
wolfSSL | 0:9d17e4342598 | 3936 | sizes[i] = SetLength(rawLen, tmps[i] + 1) + 1; /* int tag */ |
wolfSSL | 0:9d17e4342598 | 3937 | |
wolfSSL | 0:9d17e4342598 | 3938 | if (sizes[i] <= MAX_SEQ_SZ) { |
wolfSSL | 0:9d17e4342598 | 3939 | int err = mp_to_unsigned_bin(keyInt, tmps[i] + sizes[i]); |
wolfSSL | 0:9d17e4342598 | 3940 | if (err == MP_OKAY) { |
wolfSSL | 0:9d17e4342598 | 3941 | sizes[i] += rawLen; |
wolfSSL | 0:9d17e4342598 | 3942 | intTotalLen += sizes[i]; |
wolfSSL | 0:9d17e4342598 | 3943 | } |
wolfSSL | 0:9d17e4342598 | 3944 | else { |
wolfSSL | 0:9d17e4342598 | 3945 | ret = err; |
wolfSSL | 0:9d17e4342598 | 3946 | break; |
wolfSSL | 0:9d17e4342598 | 3947 | } |
wolfSSL | 0:9d17e4342598 | 3948 | } |
wolfSSL | 0:9d17e4342598 | 3949 | else { |
wolfSSL | 0:9d17e4342598 | 3950 | ret = ASN_INPUT_E; |
wolfSSL | 0:9d17e4342598 | 3951 | break; |
wolfSSL | 0:9d17e4342598 | 3952 | } |
wolfSSL | 0:9d17e4342598 | 3953 | } |
wolfSSL | 0:9d17e4342598 | 3954 | |
wolfSSL | 0:9d17e4342598 | 3955 | if (ret != 0) { |
wolfSSL | 0:9d17e4342598 | 3956 | FreeTmpRsas(tmps, key->heap); |
wolfSSL | 0:9d17e4342598 | 3957 | return ret; |
wolfSSL | 0:9d17e4342598 | 3958 | } |
wolfSSL | 0:9d17e4342598 | 3959 | |
wolfSSL | 0:9d17e4342598 | 3960 | /* make headers */ |
wolfSSL | 0:9d17e4342598 | 3961 | verSz = SetMyVersion(0, ver, FALSE); |
wolfSSL | 0:9d17e4342598 | 3962 | seqSz = SetSequence(verSz + intTotalLen, seq); |
wolfSSL | 0:9d17e4342598 | 3963 | |
wolfSSL | 0:9d17e4342598 | 3964 | outLen = seqSz + verSz + intTotalLen; |
wolfSSL | 0:9d17e4342598 | 3965 | if (outLen > (int)inLen) |
wolfSSL | 0:9d17e4342598 | 3966 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 3967 | |
wolfSSL | 0:9d17e4342598 | 3968 | /* write to output */ |
wolfSSL | 0:9d17e4342598 | 3969 | XMEMCPY(output, seq, seqSz); |
wolfSSL | 0:9d17e4342598 | 3970 | j = seqSz; |
wolfSSL | 0:9d17e4342598 | 3971 | XMEMCPY(output + j, ver, verSz); |
wolfSSL | 0:9d17e4342598 | 3972 | j += verSz; |
wolfSSL | 0:9d17e4342598 | 3973 | |
wolfSSL | 0:9d17e4342598 | 3974 | for (i = 0; i < RSA_INTS; i++) { |
wolfSSL | 0:9d17e4342598 | 3975 | XMEMCPY(output + j, tmps[i], sizes[i]); |
wolfSSL | 0:9d17e4342598 | 3976 | j += sizes[i]; |
wolfSSL | 0:9d17e4342598 | 3977 | } |
wolfSSL | 0:9d17e4342598 | 3978 | FreeTmpRsas(tmps, key->heap); |
wolfSSL | 0:9d17e4342598 | 3979 | |
wolfSSL | 0:9d17e4342598 | 3980 | return outLen; |
wolfSSL | 0:9d17e4342598 | 3981 | } |
wolfSSL | 0:9d17e4342598 | 3982 | |
wolfSSL | 0:9d17e4342598 | 3983 | #endif /* CYASSL_KEY_GEN && !NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 3984 | |
wolfSSL | 0:9d17e4342598 | 3985 | |
wolfSSL | 0:9d17e4342598 | 3986 | #if defined(CYASSL_CERT_GEN) && !defined(NO_RSA) |
wolfSSL | 0:9d17e4342598 | 3987 | |
wolfSSL | 0:9d17e4342598 | 3988 | |
wolfSSL | 0:9d17e4342598 | 3989 | #ifndef min |
wolfSSL | 0:9d17e4342598 | 3990 | |
wolfSSL | 0:9d17e4342598 | 3991 | static INLINE word32 min(word32 a, word32 b) |
wolfSSL | 0:9d17e4342598 | 3992 | { |
wolfSSL | 0:9d17e4342598 | 3993 | return a > b ? b : a; |
wolfSSL | 0:9d17e4342598 | 3994 | } |
wolfSSL | 0:9d17e4342598 | 3995 | |
wolfSSL | 0:9d17e4342598 | 3996 | #endif /* min */ |
wolfSSL | 0:9d17e4342598 | 3997 | |
wolfSSL | 0:9d17e4342598 | 3998 | |
wolfSSL | 0:9d17e4342598 | 3999 | /* Initialize and Set Certficate defaults: |
wolfSSL | 0:9d17e4342598 | 4000 | version = 3 (0x2) |
wolfSSL | 0:9d17e4342598 | 4001 | serial = 0 |
wolfSSL | 0:9d17e4342598 | 4002 | sigType = SHA_WITH_RSA |
wolfSSL | 0:9d17e4342598 | 4003 | issuer = blank |
wolfSSL | 0:9d17e4342598 | 4004 | daysValid = 500 |
wolfSSL | 0:9d17e4342598 | 4005 | selfSigned = 1 (true) use subject as issuer |
wolfSSL | 0:9d17e4342598 | 4006 | subject = blank |
wolfSSL | 0:9d17e4342598 | 4007 | */ |
wolfSSL | 0:9d17e4342598 | 4008 | void InitCert(Cert* cert) |
wolfSSL | 0:9d17e4342598 | 4009 | { |
wolfSSL | 0:9d17e4342598 | 4010 | cert->version = 2; /* version 3 is hex 2 */ |
wolfSSL | 0:9d17e4342598 | 4011 | cert->sigType = CTC_SHAwRSA; |
wolfSSL | 0:9d17e4342598 | 4012 | cert->daysValid = 500; |
wolfSSL | 0:9d17e4342598 | 4013 | cert->selfSigned = 1; |
wolfSSL | 0:9d17e4342598 | 4014 | cert->isCA = 0; |
wolfSSL | 0:9d17e4342598 | 4015 | cert->bodySz = 0; |
wolfSSL | 0:9d17e4342598 | 4016 | #ifdef CYASSL_ALT_NAMES |
wolfSSL | 0:9d17e4342598 | 4017 | cert->altNamesSz = 0; |
wolfSSL | 0:9d17e4342598 | 4018 | cert->beforeDateSz = 0; |
wolfSSL | 0:9d17e4342598 | 4019 | cert->afterDateSz = 0; |
wolfSSL | 0:9d17e4342598 | 4020 | #endif |
wolfSSL | 0:9d17e4342598 | 4021 | cert->keyType = RSA_KEY; |
wolfSSL | 0:9d17e4342598 | 4022 | XMEMSET(cert->serial, 0, CTC_SERIAL_SIZE); |
wolfSSL | 0:9d17e4342598 | 4023 | |
wolfSSL | 0:9d17e4342598 | 4024 | cert->issuer.country[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4025 | cert->issuer.state[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4026 | cert->issuer.locality[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4027 | cert->issuer.sur[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4028 | cert->issuer.org[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4029 | cert->issuer.unit[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4030 | cert->issuer.commonName[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4031 | cert->issuer.email[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4032 | |
wolfSSL | 0:9d17e4342598 | 4033 | cert->subject.country[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4034 | cert->subject.state[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4035 | cert->subject.locality[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4036 | cert->subject.sur[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4037 | cert->subject.org[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4038 | cert->subject.unit[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4039 | cert->subject.commonName[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4040 | cert->subject.email[0] = '\0'; |
wolfSSL | 0:9d17e4342598 | 4041 | |
wolfSSL | 0:9d17e4342598 | 4042 | #ifdef CYASSL_CERT_REQ |
wolfSSL | 0:9d17e4342598 | 4043 | cert->challengePw[0] ='\0'; |
wolfSSL | 0:9d17e4342598 | 4044 | #endif |
wolfSSL | 0:9d17e4342598 | 4045 | } |
wolfSSL | 0:9d17e4342598 | 4046 | |
wolfSSL | 0:9d17e4342598 | 4047 | |
wolfSSL | 0:9d17e4342598 | 4048 | /* DER encoded x509 Certificate */ |
wolfSSL | 0:9d17e4342598 | 4049 | typedef struct DerCert { |
wolfSSL | 0:9d17e4342598 | 4050 | byte size[MAX_LENGTH_SZ]; /* length encoded */ |
wolfSSL | 0:9d17e4342598 | 4051 | byte version[MAX_VERSION_SZ]; /* version encoded */ |
wolfSSL | 0:9d17e4342598 | 4052 | byte serial[CTC_SERIAL_SIZE + MAX_LENGTH_SZ]; /* serial number encoded */ |
wolfSSL | 0:9d17e4342598 | 4053 | byte sigAlgo[MAX_ALGO_SZ]; /* signature algo encoded */ |
wolfSSL | 0:9d17e4342598 | 4054 | byte issuer[ASN_NAME_MAX]; /* issuer encoded */ |
wolfSSL | 0:9d17e4342598 | 4055 | byte subject[ASN_NAME_MAX]; /* subject encoded */ |
wolfSSL | 0:9d17e4342598 | 4056 | byte validity[MAX_DATE_SIZE*2 + MAX_SEQ_SZ*2]; /* before and after dates */ |
wolfSSL | 0:9d17e4342598 | 4057 | byte publicKey[MAX_PUBLIC_KEY_SZ]; /* rsa / ntru public key encoded */ |
wolfSSL | 0:9d17e4342598 | 4058 | byte ca[MAX_CA_SZ]; /* basic constraint CA true size */ |
wolfSSL | 0:9d17e4342598 | 4059 | byte extensions[MAX_EXTENSIONS_SZ]; /* all extensions */ |
wolfSSL | 0:9d17e4342598 | 4060 | #ifdef CYASSL_CERT_REQ |
wolfSSL | 0:9d17e4342598 | 4061 | byte attrib[MAX_ATTRIB_SZ]; /* Cert req attributes encoded */ |
wolfSSL | 0:9d17e4342598 | 4062 | #endif |
wolfSSL | 0:9d17e4342598 | 4063 | int sizeSz; /* encoded size length */ |
wolfSSL | 0:9d17e4342598 | 4064 | int versionSz; /* encoded version length */ |
wolfSSL | 0:9d17e4342598 | 4065 | int serialSz; /* encoded serial length */ |
wolfSSL | 0:9d17e4342598 | 4066 | int sigAlgoSz; /* enocded sig alog length */ |
wolfSSL | 0:9d17e4342598 | 4067 | int issuerSz; /* encoded issuer length */ |
wolfSSL | 0:9d17e4342598 | 4068 | int subjectSz; /* encoded subject length */ |
wolfSSL | 0:9d17e4342598 | 4069 | int validitySz; /* encoded validity length */ |
wolfSSL | 0:9d17e4342598 | 4070 | int publicKeySz; /* encoded public key length */ |
wolfSSL | 0:9d17e4342598 | 4071 | int caSz; /* encoded CA extension length */ |
wolfSSL | 0:9d17e4342598 | 4072 | int extensionsSz; /* encoded extensions total length */ |
wolfSSL | 0:9d17e4342598 | 4073 | int total; /* total encoded lengths */ |
wolfSSL | 0:9d17e4342598 | 4074 | #ifdef CYASSL_CERT_REQ |
wolfSSL | 0:9d17e4342598 | 4075 | int attribSz; |
wolfSSL | 0:9d17e4342598 | 4076 | #endif |
wolfSSL | 0:9d17e4342598 | 4077 | } DerCert; |
wolfSSL | 0:9d17e4342598 | 4078 | |
wolfSSL | 0:9d17e4342598 | 4079 | |
wolfSSL | 0:9d17e4342598 | 4080 | #ifdef CYASSL_CERT_REQ |
wolfSSL | 0:9d17e4342598 | 4081 | |
wolfSSL | 0:9d17e4342598 | 4082 | /* Write a set header to output */ |
wolfSSL | 0:9d17e4342598 | 4083 | static word32 SetUTF8String(word32 len, byte* output) |
wolfSSL | 0:9d17e4342598 | 4084 | { |
wolfSSL | 0:9d17e4342598 | 4085 | output[0] = ASN_UTF8STRING; |
wolfSSL | 0:9d17e4342598 | 4086 | return SetLength(len, output + 1) + 1; |
wolfSSL | 0:9d17e4342598 | 4087 | } |
wolfSSL | 0:9d17e4342598 | 4088 | |
wolfSSL | 0:9d17e4342598 | 4089 | #endif /* CYASSL_CERT_REQ */ |
wolfSSL | 0:9d17e4342598 | 4090 | |
wolfSSL | 0:9d17e4342598 | 4091 | |
wolfSSL | 0:9d17e4342598 | 4092 | /* Write a serial number to output */ |
wolfSSL | 0:9d17e4342598 | 4093 | static int SetSerial(const byte* serial, byte* output) |
wolfSSL | 0:9d17e4342598 | 4094 | { |
wolfSSL | 0:9d17e4342598 | 4095 | int length = 0; |
wolfSSL | 0:9d17e4342598 | 4096 | |
wolfSSL | 0:9d17e4342598 | 4097 | output[length++] = ASN_INTEGER; |
wolfSSL | 0:9d17e4342598 | 4098 | length += SetLength(CTC_SERIAL_SIZE, &output[length]); |
wolfSSL | 0:9d17e4342598 | 4099 | XMEMCPY(&output[length], serial, CTC_SERIAL_SIZE); |
wolfSSL | 0:9d17e4342598 | 4100 | |
wolfSSL | 0:9d17e4342598 | 4101 | return length + CTC_SERIAL_SIZE; |
wolfSSL | 0:9d17e4342598 | 4102 | } |
wolfSSL | 0:9d17e4342598 | 4103 | |
wolfSSL | 0:9d17e4342598 | 4104 | |
wolfSSL | 0:9d17e4342598 | 4105 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 4106 | |
wolfSSL | 0:9d17e4342598 | 4107 | /* Write a public ECC key to output */ |
wolfSSL | 0:9d17e4342598 | 4108 | static int SetEccPublicKey(byte* output, ecc_key* key) |
wolfSSL | 0:9d17e4342598 | 4109 | { |
wolfSSL | 0:9d17e4342598 | 4110 | byte algo[MAX_ALGO_SZ]; |
wolfSSL | 0:9d17e4342598 | 4111 | byte curve[MAX_ALGO_SZ]; |
wolfSSL | 0:9d17e4342598 | 4112 | byte len[MAX_LENGTH_SZ + 1]; /* trailing 0 */ |
wolfSSL | 0:9d17e4342598 | 4113 | byte pub[ECC_BUFSIZE]; |
wolfSSL | 0:9d17e4342598 | 4114 | int algoSz; |
wolfSSL | 0:9d17e4342598 | 4115 | int curveSz; |
wolfSSL | 0:9d17e4342598 | 4116 | int lenSz; |
wolfSSL | 0:9d17e4342598 | 4117 | int idx; |
wolfSSL | 0:9d17e4342598 | 4118 | word32 pubSz = sizeof(pub); |
wolfSSL | 0:9d17e4342598 | 4119 | |
wolfSSL | 0:9d17e4342598 | 4120 | int ret = ecc_export_x963(key, pub, &pubSz); |
wolfSSL | 0:9d17e4342598 | 4121 | if (ret != 0) return ret; |
wolfSSL | 0:9d17e4342598 | 4122 | |
wolfSSL | 0:9d17e4342598 | 4123 | /* headers */ |
wolfSSL | 0:9d17e4342598 | 4124 | curveSz = SetCurve(key, curve); |
wolfSSL | 0:9d17e4342598 | 4125 | if (curveSz <= 0) return curveSz; |
wolfSSL | 0:9d17e4342598 | 4126 | |
wolfSSL | 0:9d17e4342598 | 4127 | algoSz = SetAlgoID(ECDSAk, algo, keyType, curveSz); |
wolfSSL | 0:9d17e4342598 | 4128 | lenSz = SetLength(pubSz + 1, len); |
wolfSSL | 0:9d17e4342598 | 4129 | len[lenSz++] = 0; /* trailing 0 */ |
wolfSSL | 0:9d17e4342598 | 4130 | |
wolfSSL | 0:9d17e4342598 | 4131 | /* write */ |
wolfSSL | 0:9d17e4342598 | 4132 | idx = SetSequence(pubSz + curveSz + lenSz + 1 + algoSz, output); |
wolfSSL | 0:9d17e4342598 | 4133 | /* 1 is for ASN_BIT_STRING */ |
wolfSSL | 0:9d17e4342598 | 4134 | /* algo */ |
wolfSSL | 0:9d17e4342598 | 4135 | XMEMCPY(output + idx, algo, algoSz); |
wolfSSL | 0:9d17e4342598 | 4136 | idx += algoSz; |
wolfSSL | 0:9d17e4342598 | 4137 | /* curve */ |
wolfSSL | 0:9d17e4342598 | 4138 | XMEMCPY(output + idx, curve, curveSz); |
wolfSSL | 0:9d17e4342598 | 4139 | idx += curveSz; |
wolfSSL | 0:9d17e4342598 | 4140 | /* bit string */ |
wolfSSL | 0:9d17e4342598 | 4141 | output[idx++] = ASN_BIT_STRING; |
wolfSSL | 0:9d17e4342598 | 4142 | /* length */ |
wolfSSL | 0:9d17e4342598 | 4143 | XMEMCPY(output + idx, len, lenSz); |
wolfSSL | 0:9d17e4342598 | 4144 | idx += lenSz; |
wolfSSL | 0:9d17e4342598 | 4145 | /* pub */ |
wolfSSL | 0:9d17e4342598 | 4146 | XMEMCPY(output + idx, pub, pubSz); |
wolfSSL | 0:9d17e4342598 | 4147 | idx += pubSz; |
wolfSSL | 0:9d17e4342598 | 4148 | |
wolfSSL | 0:9d17e4342598 | 4149 | return idx; |
wolfSSL | 0:9d17e4342598 | 4150 | } |
wolfSSL | 0:9d17e4342598 | 4151 | |
wolfSSL | 0:9d17e4342598 | 4152 | |
wolfSSL | 0:9d17e4342598 | 4153 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 4154 | |
wolfSSL | 0:9d17e4342598 | 4155 | |
wolfSSL | 0:9d17e4342598 | 4156 | /* Write a public RSA key to output */ |
wolfSSL | 0:9d17e4342598 | 4157 | static int SetRsaPublicKey(byte* output, RsaKey* key) |
wolfSSL | 0:9d17e4342598 | 4158 | { |
wolfSSL | 0:9d17e4342598 | 4159 | byte n[MAX_RSA_INT_SZ]; |
wolfSSL | 0:9d17e4342598 | 4160 | byte e[MAX_RSA_E_SZ]; |
wolfSSL | 0:9d17e4342598 | 4161 | byte algo[MAX_ALGO_SZ]; |
wolfSSL | 0:9d17e4342598 | 4162 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 4163 | byte len[MAX_LENGTH_SZ + 1]; /* trailing 0 */ |
wolfSSL | 0:9d17e4342598 | 4164 | int nSz; |
wolfSSL | 0:9d17e4342598 | 4165 | int eSz; |
wolfSSL | 0:9d17e4342598 | 4166 | int algoSz; |
wolfSSL | 0:9d17e4342598 | 4167 | int seqSz; |
wolfSSL | 0:9d17e4342598 | 4168 | int lenSz; |
wolfSSL | 0:9d17e4342598 | 4169 | int idx; |
wolfSSL | 0:9d17e4342598 | 4170 | int rawLen; |
wolfSSL | 0:9d17e4342598 | 4171 | int leadingBit; |
wolfSSL | 0:9d17e4342598 | 4172 | int err; |
wolfSSL | 0:9d17e4342598 | 4173 | |
wolfSSL | 0:9d17e4342598 | 4174 | /* n */ |
wolfSSL | 0:9d17e4342598 | 4175 | leadingBit = mp_leading_bit(&key->n); |
wolfSSL | 0:9d17e4342598 | 4176 | rawLen = mp_unsigned_bin_size(&key->n) + leadingBit; |
wolfSSL | 0:9d17e4342598 | 4177 | n[0] = ASN_INTEGER; |
wolfSSL | 0:9d17e4342598 | 4178 | nSz = SetLength(rawLen, n + 1) + 1; /* int tag */ |
wolfSSL | 0:9d17e4342598 | 4179 | |
wolfSSL | 0:9d17e4342598 | 4180 | if ( (nSz + rawLen) < (int)sizeof(n)) { |
wolfSSL | 0:9d17e4342598 | 4181 | if (leadingBit) |
wolfSSL | 0:9d17e4342598 | 4182 | n[nSz] = 0; |
wolfSSL | 0:9d17e4342598 | 4183 | err = mp_to_unsigned_bin(&key->n, n + nSz + leadingBit); |
wolfSSL | 0:9d17e4342598 | 4184 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 4185 | nSz += rawLen; |
wolfSSL | 0:9d17e4342598 | 4186 | else |
wolfSSL | 0:9d17e4342598 | 4187 | return MP_TO_E; |
wolfSSL | 0:9d17e4342598 | 4188 | } |
wolfSSL | 0:9d17e4342598 | 4189 | else |
wolfSSL | 0:9d17e4342598 | 4190 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 4191 | |
wolfSSL | 0:9d17e4342598 | 4192 | /* e */ |
wolfSSL | 0:9d17e4342598 | 4193 | leadingBit = mp_leading_bit(&key->e); |
wolfSSL | 0:9d17e4342598 | 4194 | rawLen = mp_unsigned_bin_size(&key->e) + leadingBit; |
wolfSSL | 0:9d17e4342598 | 4195 | e[0] = ASN_INTEGER; |
wolfSSL | 0:9d17e4342598 | 4196 | eSz = SetLength(rawLen, e + 1) + 1; /* int tag */ |
wolfSSL | 0:9d17e4342598 | 4197 | |
wolfSSL | 0:9d17e4342598 | 4198 | if ( (eSz + rawLen) < (int)sizeof(e)) { |
wolfSSL | 0:9d17e4342598 | 4199 | if (leadingBit) |
wolfSSL | 0:9d17e4342598 | 4200 | e[eSz] = 0; |
wolfSSL | 0:9d17e4342598 | 4201 | err = mp_to_unsigned_bin(&key->e, e + eSz + leadingBit); |
wolfSSL | 0:9d17e4342598 | 4202 | if (err == MP_OKAY) |
wolfSSL | 0:9d17e4342598 | 4203 | eSz += rawLen; |
wolfSSL | 0:9d17e4342598 | 4204 | else |
wolfSSL | 0:9d17e4342598 | 4205 | return MP_TO_E; |
wolfSSL | 0:9d17e4342598 | 4206 | } |
wolfSSL | 0:9d17e4342598 | 4207 | else |
wolfSSL | 0:9d17e4342598 | 4208 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 4209 | |
wolfSSL | 0:9d17e4342598 | 4210 | /* headers */ |
wolfSSL | 0:9d17e4342598 | 4211 | algoSz = SetAlgoID(RSAk, algo, keyType, 0); |
wolfSSL | 0:9d17e4342598 | 4212 | seqSz = SetSequence(nSz + eSz, seq); |
wolfSSL | 0:9d17e4342598 | 4213 | lenSz = SetLength(seqSz + nSz + eSz + 1, len); |
wolfSSL | 0:9d17e4342598 | 4214 | len[lenSz++] = 0; /* trailing 0 */ |
wolfSSL | 0:9d17e4342598 | 4215 | |
wolfSSL | 0:9d17e4342598 | 4216 | /* write */ |
wolfSSL | 0:9d17e4342598 | 4217 | idx = SetSequence(nSz + eSz + seqSz + lenSz + 1 + algoSz, output); |
wolfSSL | 0:9d17e4342598 | 4218 | /* 1 is for ASN_BIT_STRING */ |
wolfSSL | 0:9d17e4342598 | 4219 | /* algo */ |
wolfSSL | 0:9d17e4342598 | 4220 | XMEMCPY(output + idx, algo, algoSz); |
wolfSSL | 0:9d17e4342598 | 4221 | idx += algoSz; |
wolfSSL | 0:9d17e4342598 | 4222 | /* bit string */ |
wolfSSL | 0:9d17e4342598 | 4223 | output[idx++] = ASN_BIT_STRING; |
wolfSSL | 0:9d17e4342598 | 4224 | /* length */ |
wolfSSL | 0:9d17e4342598 | 4225 | XMEMCPY(output + idx, len, lenSz); |
wolfSSL | 0:9d17e4342598 | 4226 | idx += lenSz; |
wolfSSL | 0:9d17e4342598 | 4227 | /* seq */ |
wolfSSL | 0:9d17e4342598 | 4228 | XMEMCPY(output + idx, seq, seqSz); |
wolfSSL | 0:9d17e4342598 | 4229 | idx += seqSz; |
wolfSSL | 0:9d17e4342598 | 4230 | /* n */ |
wolfSSL | 0:9d17e4342598 | 4231 | XMEMCPY(output + idx, n, nSz); |
wolfSSL | 0:9d17e4342598 | 4232 | idx += nSz; |
wolfSSL | 0:9d17e4342598 | 4233 | /* e */ |
wolfSSL | 0:9d17e4342598 | 4234 | XMEMCPY(output + idx, e, eSz); |
wolfSSL | 0:9d17e4342598 | 4235 | idx += eSz; |
wolfSSL | 0:9d17e4342598 | 4236 | |
wolfSSL | 0:9d17e4342598 | 4237 | return idx; |
wolfSSL | 0:9d17e4342598 | 4238 | } |
wolfSSL | 0:9d17e4342598 | 4239 | |
wolfSSL | 0:9d17e4342598 | 4240 | |
wolfSSL | 0:9d17e4342598 | 4241 | static INLINE byte itob(int number) |
wolfSSL | 0:9d17e4342598 | 4242 | { |
wolfSSL | 0:9d17e4342598 | 4243 | return (byte)number + 0x30; |
wolfSSL | 0:9d17e4342598 | 4244 | } |
wolfSSL | 0:9d17e4342598 | 4245 | |
wolfSSL | 0:9d17e4342598 | 4246 | |
wolfSSL | 0:9d17e4342598 | 4247 | /* write time to output, format */ |
wolfSSL | 0:9d17e4342598 | 4248 | static void SetTime(struct tm* date, byte* output) |
wolfSSL | 0:9d17e4342598 | 4249 | { |
wolfSSL | 0:9d17e4342598 | 4250 | int i = 0; |
wolfSSL | 0:9d17e4342598 | 4251 | |
wolfSSL | 0:9d17e4342598 | 4252 | output[i++] = itob((date->tm_year % 10000) / 1000); |
wolfSSL | 0:9d17e4342598 | 4253 | output[i++] = itob((date->tm_year % 1000) / 100); |
wolfSSL | 0:9d17e4342598 | 4254 | output[i++] = itob((date->tm_year % 100) / 10); |
wolfSSL | 0:9d17e4342598 | 4255 | output[i++] = itob( date->tm_year % 10); |
wolfSSL | 0:9d17e4342598 | 4256 | |
wolfSSL | 0:9d17e4342598 | 4257 | output[i++] = itob(date->tm_mon / 10); |
wolfSSL | 0:9d17e4342598 | 4258 | output[i++] = itob(date->tm_mon % 10); |
wolfSSL | 0:9d17e4342598 | 4259 | |
wolfSSL | 0:9d17e4342598 | 4260 | output[i++] = itob(date->tm_mday / 10); |
wolfSSL | 0:9d17e4342598 | 4261 | output[i++] = itob(date->tm_mday % 10); |
wolfSSL | 0:9d17e4342598 | 4262 | |
wolfSSL | 0:9d17e4342598 | 4263 | output[i++] = itob(date->tm_hour / 10); |
wolfSSL | 0:9d17e4342598 | 4264 | output[i++] = itob(date->tm_hour % 10); |
wolfSSL | 0:9d17e4342598 | 4265 | |
wolfSSL | 0:9d17e4342598 | 4266 | output[i++] = itob(date->tm_min / 10); |
wolfSSL | 0:9d17e4342598 | 4267 | output[i++] = itob(date->tm_min % 10); |
wolfSSL | 0:9d17e4342598 | 4268 | |
wolfSSL | 0:9d17e4342598 | 4269 | output[i++] = itob(date->tm_sec / 10); |
wolfSSL | 0:9d17e4342598 | 4270 | output[i++] = itob(date->tm_sec % 10); |
wolfSSL | 0:9d17e4342598 | 4271 | |
wolfSSL | 0:9d17e4342598 | 4272 | output[i] = 'Z'; /* Zulu profile */ |
wolfSSL | 0:9d17e4342598 | 4273 | } |
wolfSSL | 0:9d17e4342598 | 4274 | |
wolfSSL | 0:9d17e4342598 | 4275 | |
wolfSSL | 0:9d17e4342598 | 4276 | #ifdef CYASSL_ALT_NAMES |
wolfSSL | 0:9d17e4342598 | 4277 | |
wolfSSL | 0:9d17e4342598 | 4278 | /* Copy Dates from cert, return bytes written */ |
wolfSSL | 0:9d17e4342598 | 4279 | static int CopyValidity(byte* output, Cert* cert) |
wolfSSL | 0:9d17e4342598 | 4280 | { |
wolfSSL | 0:9d17e4342598 | 4281 | int seqSz; |
wolfSSL | 0:9d17e4342598 | 4282 | |
wolfSSL | 0:9d17e4342598 | 4283 | CYASSL_ENTER("CopyValidity"); |
wolfSSL | 0:9d17e4342598 | 4284 | |
wolfSSL | 0:9d17e4342598 | 4285 | /* headers and output */ |
wolfSSL | 0:9d17e4342598 | 4286 | seqSz = SetSequence(cert->beforeDateSz + cert->afterDateSz, output); |
wolfSSL | 0:9d17e4342598 | 4287 | XMEMCPY(output + seqSz, cert->beforeDate, cert->beforeDateSz); |
wolfSSL | 0:9d17e4342598 | 4288 | XMEMCPY(output + seqSz + cert->beforeDateSz, cert->afterDate, |
wolfSSL | 0:9d17e4342598 | 4289 | cert->afterDateSz); |
wolfSSL | 0:9d17e4342598 | 4290 | return seqSz + cert->beforeDateSz + cert->afterDateSz; |
wolfSSL | 0:9d17e4342598 | 4291 | } |
wolfSSL | 0:9d17e4342598 | 4292 | |
wolfSSL | 0:9d17e4342598 | 4293 | #endif |
wolfSSL | 0:9d17e4342598 | 4294 | |
wolfSSL | 0:9d17e4342598 | 4295 | |
wolfSSL | 0:9d17e4342598 | 4296 | /* Set Date validity from now until now + daysValid */ |
wolfSSL | 0:9d17e4342598 | 4297 | static int SetValidity(byte* output, int daysValid) |
wolfSSL | 0:9d17e4342598 | 4298 | { |
wolfSSL | 0:9d17e4342598 | 4299 | byte before[MAX_DATE_SIZE]; |
wolfSSL | 0:9d17e4342598 | 4300 | byte after[MAX_DATE_SIZE]; |
wolfSSL | 0:9d17e4342598 | 4301 | |
wolfSSL | 0:9d17e4342598 | 4302 | int beforeSz; |
wolfSSL | 0:9d17e4342598 | 4303 | int afterSz; |
wolfSSL | 0:9d17e4342598 | 4304 | int seqSz; |
wolfSSL | 0:9d17e4342598 | 4305 | |
wolfSSL | 0:9d17e4342598 | 4306 | time_t ticks; |
wolfSSL | 0:9d17e4342598 | 4307 | struct tm* now; |
wolfSSL | 0:9d17e4342598 | 4308 | struct tm local; |
wolfSSL | 0:9d17e4342598 | 4309 | |
wolfSSL | 0:9d17e4342598 | 4310 | ticks = XTIME(0); |
wolfSSL | 0:9d17e4342598 | 4311 | now = XGMTIME(&ticks); |
wolfSSL | 0:9d17e4342598 | 4312 | |
wolfSSL | 0:9d17e4342598 | 4313 | /* before now */ |
wolfSSL | 0:9d17e4342598 | 4314 | local = *now; |
wolfSSL | 0:9d17e4342598 | 4315 | before[0] = ASN_GENERALIZED_TIME; |
wolfSSL | 0:9d17e4342598 | 4316 | beforeSz = SetLength(ASN_GEN_TIME_SZ, before + 1) + 1; /* gen tag */ |
wolfSSL | 0:9d17e4342598 | 4317 | |
wolfSSL | 0:9d17e4342598 | 4318 | /* subtract 1 day for more compliance */ |
wolfSSL | 0:9d17e4342598 | 4319 | local.tm_mday -= 1; |
wolfSSL | 0:9d17e4342598 | 4320 | mktime(&local); |
wolfSSL | 0:9d17e4342598 | 4321 | |
wolfSSL | 0:9d17e4342598 | 4322 | /* adjust */ |
wolfSSL | 0:9d17e4342598 | 4323 | local.tm_year += 1900; |
wolfSSL | 0:9d17e4342598 | 4324 | local.tm_mon += 1; |
wolfSSL | 0:9d17e4342598 | 4325 | |
wolfSSL | 0:9d17e4342598 | 4326 | SetTime(&local, before + beforeSz); |
wolfSSL | 0:9d17e4342598 | 4327 | beforeSz += ASN_GEN_TIME_SZ; |
wolfSSL | 0:9d17e4342598 | 4328 | |
wolfSSL | 0:9d17e4342598 | 4329 | /* after now + daysValid */ |
wolfSSL | 0:9d17e4342598 | 4330 | local = *now; |
wolfSSL | 0:9d17e4342598 | 4331 | after[0] = ASN_GENERALIZED_TIME; |
wolfSSL | 0:9d17e4342598 | 4332 | afterSz = SetLength(ASN_GEN_TIME_SZ, after + 1) + 1; /* gen tag */ |
wolfSSL | 0:9d17e4342598 | 4333 | |
wolfSSL | 0:9d17e4342598 | 4334 | /* add daysValid */ |
wolfSSL | 0:9d17e4342598 | 4335 | local.tm_mday += daysValid; |
wolfSSL | 0:9d17e4342598 | 4336 | mktime(&local); |
wolfSSL | 0:9d17e4342598 | 4337 | |
wolfSSL | 0:9d17e4342598 | 4338 | /* adjust */ |
wolfSSL | 0:9d17e4342598 | 4339 | local.tm_year += 1900; |
wolfSSL | 0:9d17e4342598 | 4340 | local.tm_mon += 1; |
wolfSSL | 0:9d17e4342598 | 4341 | |
wolfSSL | 0:9d17e4342598 | 4342 | SetTime(&local, after + afterSz); |
wolfSSL | 0:9d17e4342598 | 4343 | afterSz += ASN_GEN_TIME_SZ; |
wolfSSL | 0:9d17e4342598 | 4344 | |
wolfSSL | 0:9d17e4342598 | 4345 | /* headers and output */ |
wolfSSL | 0:9d17e4342598 | 4346 | seqSz = SetSequence(beforeSz + afterSz, output); |
wolfSSL | 0:9d17e4342598 | 4347 | XMEMCPY(output + seqSz, before, beforeSz); |
wolfSSL | 0:9d17e4342598 | 4348 | XMEMCPY(output + seqSz + beforeSz, after, afterSz); |
wolfSSL | 0:9d17e4342598 | 4349 | |
wolfSSL | 0:9d17e4342598 | 4350 | return seqSz + beforeSz + afterSz; |
wolfSSL | 0:9d17e4342598 | 4351 | } |
wolfSSL | 0:9d17e4342598 | 4352 | |
wolfSSL | 0:9d17e4342598 | 4353 | |
wolfSSL | 0:9d17e4342598 | 4354 | /* ASN Encoded Name field */ |
wolfSSL | 0:9d17e4342598 | 4355 | typedef struct EncodedName { |
wolfSSL | 0:9d17e4342598 | 4356 | int nameLen; /* actual string value length */ |
wolfSSL | 0:9d17e4342598 | 4357 | int totalLen; /* total encoded length */ |
wolfSSL | 0:9d17e4342598 | 4358 | int type; /* type of name */ |
wolfSSL | 0:9d17e4342598 | 4359 | int used; /* are we actually using this one */ |
wolfSSL | 0:9d17e4342598 | 4360 | byte encoded[CTC_NAME_SIZE * 2]; /* encoding */ |
wolfSSL | 0:9d17e4342598 | 4361 | } EncodedName; |
wolfSSL | 0:9d17e4342598 | 4362 | |
wolfSSL | 0:9d17e4342598 | 4363 | |
wolfSSL | 0:9d17e4342598 | 4364 | /* Get Which Name from index */ |
wolfSSL | 0:9d17e4342598 | 4365 | static const char* GetOneName(CertName* name, int idx) |
wolfSSL | 0:9d17e4342598 | 4366 | { |
wolfSSL | 0:9d17e4342598 | 4367 | switch (idx) { |
wolfSSL | 0:9d17e4342598 | 4368 | case 0: |
wolfSSL | 0:9d17e4342598 | 4369 | return name->country; |
wolfSSL | 0:9d17e4342598 | 4370 | |
wolfSSL | 0:9d17e4342598 | 4371 | case 1: |
wolfSSL | 0:9d17e4342598 | 4372 | return name->state; |
wolfSSL | 0:9d17e4342598 | 4373 | |
wolfSSL | 0:9d17e4342598 | 4374 | case 2: |
wolfSSL | 0:9d17e4342598 | 4375 | return name->locality; |
wolfSSL | 0:9d17e4342598 | 4376 | |
wolfSSL | 0:9d17e4342598 | 4377 | case 3: |
wolfSSL | 0:9d17e4342598 | 4378 | return name->sur; |
wolfSSL | 0:9d17e4342598 | 4379 | |
wolfSSL | 0:9d17e4342598 | 4380 | case 4: |
wolfSSL | 0:9d17e4342598 | 4381 | return name->org; |
wolfSSL | 0:9d17e4342598 | 4382 | |
wolfSSL | 0:9d17e4342598 | 4383 | case 5: |
wolfSSL | 0:9d17e4342598 | 4384 | return name->unit; |
wolfSSL | 0:9d17e4342598 | 4385 | |
wolfSSL | 0:9d17e4342598 | 4386 | case 6: |
wolfSSL | 0:9d17e4342598 | 4387 | return name->commonName; |
wolfSSL | 0:9d17e4342598 | 4388 | |
wolfSSL | 0:9d17e4342598 | 4389 | case 7: |
wolfSSL | 0:9d17e4342598 | 4390 | return name->email; |
wolfSSL | 0:9d17e4342598 | 4391 | |
wolfSSL | 0:9d17e4342598 | 4392 | default: |
wolfSSL | 0:9d17e4342598 | 4393 | return 0; |
wolfSSL | 0:9d17e4342598 | 4394 | } |
wolfSSL | 0:9d17e4342598 | 4395 | } |
wolfSSL | 0:9d17e4342598 | 4396 | |
wolfSSL | 0:9d17e4342598 | 4397 | |
wolfSSL | 0:9d17e4342598 | 4398 | /* Get ASN Name from index */ |
wolfSSL | 0:9d17e4342598 | 4399 | static byte GetNameId(int idx) |
wolfSSL | 0:9d17e4342598 | 4400 | { |
wolfSSL | 0:9d17e4342598 | 4401 | switch (idx) { |
wolfSSL | 0:9d17e4342598 | 4402 | case 0: |
wolfSSL | 0:9d17e4342598 | 4403 | return ASN_COUNTRY_NAME; |
wolfSSL | 0:9d17e4342598 | 4404 | |
wolfSSL | 0:9d17e4342598 | 4405 | case 1: |
wolfSSL | 0:9d17e4342598 | 4406 | return ASN_STATE_NAME; |
wolfSSL | 0:9d17e4342598 | 4407 | |
wolfSSL | 0:9d17e4342598 | 4408 | case 2: |
wolfSSL | 0:9d17e4342598 | 4409 | return ASN_LOCALITY_NAME; |
wolfSSL | 0:9d17e4342598 | 4410 | |
wolfSSL | 0:9d17e4342598 | 4411 | case 3: |
wolfSSL | 0:9d17e4342598 | 4412 | return ASN_SUR_NAME; |
wolfSSL | 0:9d17e4342598 | 4413 | |
wolfSSL | 0:9d17e4342598 | 4414 | case 4: |
wolfSSL | 0:9d17e4342598 | 4415 | return ASN_ORG_NAME; |
wolfSSL | 0:9d17e4342598 | 4416 | |
wolfSSL | 0:9d17e4342598 | 4417 | case 5: |
wolfSSL | 0:9d17e4342598 | 4418 | return ASN_ORGUNIT_NAME; |
wolfSSL | 0:9d17e4342598 | 4419 | |
wolfSSL | 0:9d17e4342598 | 4420 | case 6: |
wolfSSL | 0:9d17e4342598 | 4421 | return ASN_COMMON_NAME; |
wolfSSL | 0:9d17e4342598 | 4422 | |
wolfSSL | 0:9d17e4342598 | 4423 | case 7: |
wolfSSL | 0:9d17e4342598 | 4424 | /* email uses different id type */ |
wolfSSL | 0:9d17e4342598 | 4425 | return 0; |
wolfSSL | 0:9d17e4342598 | 4426 | |
wolfSSL | 0:9d17e4342598 | 4427 | default: |
wolfSSL | 0:9d17e4342598 | 4428 | return 0; |
wolfSSL | 0:9d17e4342598 | 4429 | } |
wolfSSL | 0:9d17e4342598 | 4430 | } |
wolfSSL | 0:9d17e4342598 | 4431 | |
wolfSSL | 0:9d17e4342598 | 4432 | |
wolfSSL | 0:9d17e4342598 | 4433 | /* encode all extensions, return total bytes written */ |
wolfSSL | 0:9d17e4342598 | 4434 | static int SetExtensions(byte* output, const byte* ext, int extSz, int header) |
wolfSSL | 0:9d17e4342598 | 4435 | { |
wolfSSL | 0:9d17e4342598 | 4436 | byte sequence[MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 4437 | byte len[MAX_LENGTH_SZ]; |
wolfSSL | 0:9d17e4342598 | 4438 | |
wolfSSL | 0:9d17e4342598 | 4439 | int sz = 0; |
wolfSSL | 0:9d17e4342598 | 4440 | int seqSz = SetSequence(extSz, sequence); |
wolfSSL | 0:9d17e4342598 | 4441 | |
wolfSSL | 0:9d17e4342598 | 4442 | if (header) { |
wolfSSL | 0:9d17e4342598 | 4443 | int lenSz = SetLength(seqSz + extSz, len); |
wolfSSL | 0:9d17e4342598 | 4444 | output[0] = ASN_EXTENSIONS; /* extensions id */ |
wolfSSL | 0:9d17e4342598 | 4445 | sz++; |
wolfSSL | 0:9d17e4342598 | 4446 | XMEMCPY(&output[sz], len, lenSz); /* length */ |
wolfSSL | 0:9d17e4342598 | 4447 | sz += lenSz; |
wolfSSL | 0:9d17e4342598 | 4448 | } |
wolfSSL | 0:9d17e4342598 | 4449 | XMEMCPY(&output[sz], sequence, seqSz); /* sequence */ |
wolfSSL | 0:9d17e4342598 | 4450 | sz += seqSz; |
wolfSSL | 0:9d17e4342598 | 4451 | XMEMCPY(&output[sz], ext, extSz); /* extensions */ |
wolfSSL | 0:9d17e4342598 | 4452 | sz += extSz; |
wolfSSL | 0:9d17e4342598 | 4453 | |
wolfSSL | 0:9d17e4342598 | 4454 | return sz; |
wolfSSL | 0:9d17e4342598 | 4455 | } |
wolfSSL | 0:9d17e4342598 | 4456 | |
wolfSSL | 0:9d17e4342598 | 4457 | |
wolfSSL | 0:9d17e4342598 | 4458 | /* encode CA basic constraint true, return total bytes written */ |
wolfSSL | 0:9d17e4342598 | 4459 | static int SetCa(byte* output) |
wolfSSL | 0:9d17e4342598 | 4460 | { |
wolfSSL | 0:9d17e4342598 | 4461 | static const byte ca[] = { 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, |
wolfSSL | 0:9d17e4342598 | 4462 | 0x05, 0x30, 0x03, 0x01, 0x01, 0xff }; |
wolfSSL | 0:9d17e4342598 | 4463 | |
wolfSSL | 0:9d17e4342598 | 4464 | XMEMCPY(output, ca, sizeof(ca)); |
wolfSSL | 0:9d17e4342598 | 4465 | |
wolfSSL | 0:9d17e4342598 | 4466 | return (int)sizeof(ca); |
wolfSSL | 0:9d17e4342598 | 4467 | } |
wolfSSL | 0:9d17e4342598 | 4468 | |
wolfSSL | 0:9d17e4342598 | 4469 | |
wolfSSL | 0:9d17e4342598 | 4470 | /* encode CertName into output, return total bytes written */ |
wolfSSL | 0:9d17e4342598 | 4471 | static int SetName(byte* output, CertName* name) |
wolfSSL | 0:9d17e4342598 | 4472 | { |
wolfSSL | 0:9d17e4342598 | 4473 | int totalBytes = 0, i, idx; |
wolfSSL | 0:9d17e4342598 | 4474 | EncodedName names[NAME_ENTRIES]; |
wolfSSL | 0:9d17e4342598 | 4475 | |
wolfSSL | 0:9d17e4342598 | 4476 | for (i = 0; i < NAME_ENTRIES; i++) { |
wolfSSL | 0:9d17e4342598 | 4477 | const char* nameStr = GetOneName(name, i); |
wolfSSL | 0:9d17e4342598 | 4478 | if (nameStr) { |
wolfSSL | 0:9d17e4342598 | 4479 | /* bottom up */ |
wolfSSL | 0:9d17e4342598 | 4480 | byte firstLen[MAX_LENGTH_SZ]; |
wolfSSL | 0:9d17e4342598 | 4481 | byte secondLen[MAX_LENGTH_SZ]; |
wolfSSL | 0:9d17e4342598 | 4482 | byte sequence[MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 4483 | byte set[MAX_SET_SZ]; |
wolfSSL | 0:9d17e4342598 | 4484 | |
wolfSSL | 0:9d17e4342598 | 4485 | int email = i == (NAME_ENTRIES - 1) ? 1 : 0; |
wolfSSL | 0:9d17e4342598 | 4486 | int strLen = (int)XSTRLEN(nameStr); |
wolfSSL | 0:9d17e4342598 | 4487 | int thisLen = strLen; |
wolfSSL | 0:9d17e4342598 | 4488 | int firstSz, secondSz, seqSz, setSz; |
wolfSSL | 0:9d17e4342598 | 4489 | |
wolfSSL | 0:9d17e4342598 | 4490 | if (strLen == 0) { /* no user data for this item */ |
wolfSSL | 0:9d17e4342598 | 4491 | names[i].used = 0; |
wolfSSL | 0:9d17e4342598 | 4492 | continue; |
wolfSSL | 0:9d17e4342598 | 4493 | } |
wolfSSL | 0:9d17e4342598 | 4494 | |
wolfSSL | 0:9d17e4342598 | 4495 | secondSz = SetLength(strLen, secondLen); |
wolfSSL | 0:9d17e4342598 | 4496 | thisLen += secondSz; |
wolfSSL | 0:9d17e4342598 | 4497 | if (email) { |
wolfSSL | 0:9d17e4342598 | 4498 | thisLen += EMAIL_JOINT_LEN; |
wolfSSL | 0:9d17e4342598 | 4499 | thisLen ++; /* id type */ |
wolfSSL | 0:9d17e4342598 | 4500 | firstSz = SetLength(EMAIL_JOINT_LEN, firstLen); |
wolfSSL | 0:9d17e4342598 | 4501 | } |
wolfSSL | 0:9d17e4342598 | 4502 | else { |
wolfSSL | 0:9d17e4342598 | 4503 | thisLen++; /* str type */ |
wolfSSL | 0:9d17e4342598 | 4504 | thisLen++; /* id type */ |
wolfSSL | 0:9d17e4342598 | 4505 | thisLen += JOINT_LEN; |
wolfSSL | 0:9d17e4342598 | 4506 | firstSz = SetLength(JOINT_LEN + 1, firstLen); |
wolfSSL | 0:9d17e4342598 | 4507 | } |
wolfSSL | 0:9d17e4342598 | 4508 | thisLen += firstSz; |
wolfSSL | 0:9d17e4342598 | 4509 | thisLen++; /* object id */ |
wolfSSL | 0:9d17e4342598 | 4510 | |
wolfSSL | 0:9d17e4342598 | 4511 | seqSz = SetSequence(thisLen, sequence); |
wolfSSL | 0:9d17e4342598 | 4512 | thisLen += seqSz; |
wolfSSL | 0:9d17e4342598 | 4513 | setSz = SetSet(thisLen, set); |
wolfSSL | 0:9d17e4342598 | 4514 | thisLen += setSz; |
wolfSSL | 0:9d17e4342598 | 4515 | |
wolfSSL | 0:9d17e4342598 | 4516 | if (thisLen > (int)sizeof(names[i].encoded)) |
wolfSSL | 0:9d17e4342598 | 4517 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 4518 | |
wolfSSL | 0:9d17e4342598 | 4519 | /* store it */ |
wolfSSL | 0:9d17e4342598 | 4520 | idx = 0; |
wolfSSL | 0:9d17e4342598 | 4521 | /* set */ |
wolfSSL | 0:9d17e4342598 | 4522 | XMEMCPY(names[i].encoded, set, setSz); |
wolfSSL | 0:9d17e4342598 | 4523 | idx += setSz; |
wolfSSL | 0:9d17e4342598 | 4524 | /* seq */ |
wolfSSL | 0:9d17e4342598 | 4525 | XMEMCPY(names[i].encoded + idx, sequence, seqSz); |
wolfSSL | 0:9d17e4342598 | 4526 | idx += seqSz; |
wolfSSL | 0:9d17e4342598 | 4527 | /* asn object id */ |
wolfSSL | 0:9d17e4342598 | 4528 | names[i].encoded[idx++] = ASN_OBJECT_ID; |
wolfSSL | 0:9d17e4342598 | 4529 | /* first length */ |
wolfSSL | 0:9d17e4342598 | 4530 | XMEMCPY(names[i].encoded + idx, firstLen, firstSz); |
wolfSSL | 0:9d17e4342598 | 4531 | idx += firstSz; |
wolfSSL | 0:9d17e4342598 | 4532 | if (email) { |
wolfSSL | 0:9d17e4342598 | 4533 | const byte EMAIL_OID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, |
wolfSSL | 0:9d17e4342598 | 4534 | 0x01, 0x09, 0x01, 0x16 }; |
wolfSSL | 0:9d17e4342598 | 4535 | /* email joint id */ |
wolfSSL | 0:9d17e4342598 | 4536 | XMEMCPY(names[i].encoded + idx, EMAIL_OID, sizeof(EMAIL_OID)); |
wolfSSL | 0:9d17e4342598 | 4537 | idx += (int)sizeof(EMAIL_OID); |
wolfSSL | 0:9d17e4342598 | 4538 | } |
wolfSSL | 0:9d17e4342598 | 4539 | else { |
wolfSSL | 0:9d17e4342598 | 4540 | /* joint id */ |
wolfSSL | 0:9d17e4342598 | 4541 | byte bType = GetNameId(i); |
wolfSSL | 0:9d17e4342598 | 4542 | names[i].encoded[idx++] = 0x55; |
wolfSSL | 0:9d17e4342598 | 4543 | names[i].encoded[idx++] = 0x04; |
wolfSSL | 0:9d17e4342598 | 4544 | /* id type */ |
wolfSSL | 0:9d17e4342598 | 4545 | names[i].encoded[idx++] = bType; |
wolfSSL | 0:9d17e4342598 | 4546 | /* str type */ |
wolfSSL | 0:9d17e4342598 | 4547 | if (bType == ASN_COUNTRY_NAME) |
wolfSSL | 0:9d17e4342598 | 4548 | names[i].encoded[idx++] = 0x13; /* printable */ |
wolfSSL | 0:9d17e4342598 | 4549 | else |
wolfSSL | 0:9d17e4342598 | 4550 | names[i].encoded[idx++] = 0x0c; /* utf8 */ |
wolfSSL | 0:9d17e4342598 | 4551 | } |
wolfSSL | 0:9d17e4342598 | 4552 | /* second length */ |
wolfSSL | 0:9d17e4342598 | 4553 | XMEMCPY(names[i].encoded + idx, secondLen, secondSz); |
wolfSSL | 0:9d17e4342598 | 4554 | idx += secondSz; |
wolfSSL | 0:9d17e4342598 | 4555 | /* str value */ |
wolfSSL | 0:9d17e4342598 | 4556 | XMEMCPY(names[i].encoded + idx, nameStr, strLen); |
wolfSSL | 0:9d17e4342598 | 4557 | idx += strLen; |
wolfSSL | 0:9d17e4342598 | 4558 | |
wolfSSL | 0:9d17e4342598 | 4559 | totalBytes += idx; |
wolfSSL | 0:9d17e4342598 | 4560 | names[i].totalLen = idx; |
wolfSSL | 0:9d17e4342598 | 4561 | names[i].used = 1; |
wolfSSL | 0:9d17e4342598 | 4562 | } |
wolfSSL | 0:9d17e4342598 | 4563 | else |
wolfSSL | 0:9d17e4342598 | 4564 | names[i].used = 0; |
wolfSSL | 0:9d17e4342598 | 4565 | } |
wolfSSL | 0:9d17e4342598 | 4566 | |
wolfSSL | 0:9d17e4342598 | 4567 | /* header */ |
wolfSSL | 0:9d17e4342598 | 4568 | idx = SetSequence(totalBytes, output); |
wolfSSL | 0:9d17e4342598 | 4569 | totalBytes += idx; |
wolfSSL | 0:9d17e4342598 | 4570 | if (totalBytes > ASN_NAME_MAX) |
wolfSSL | 0:9d17e4342598 | 4571 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 4572 | |
wolfSSL | 0:9d17e4342598 | 4573 | for (i = 0; i < NAME_ENTRIES; i++) { |
wolfSSL | 0:9d17e4342598 | 4574 | if (names[i].used) { |
wolfSSL | 0:9d17e4342598 | 4575 | XMEMCPY(output + idx, names[i].encoded, names[i].totalLen); |
wolfSSL | 0:9d17e4342598 | 4576 | idx += names[i].totalLen; |
wolfSSL | 0:9d17e4342598 | 4577 | } |
wolfSSL | 0:9d17e4342598 | 4578 | } |
wolfSSL | 0:9d17e4342598 | 4579 | return totalBytes; |
wolfSSL | 0:9d17e4342598 | 4580 | } |
wolfSSL | 0:9d17e4342598 | 4581 | |
wolfSSL | 0:9d17e4342598 | 4582 | /* encode info from cert into DER encoded format */ |
wolfSSL | 0:9d17e4342598 | 4583 | static int EncodeCert(Cert* cert, DerCert* der, RsaKey* rsaKey, ecc_key* eccKey, |
wolfSSL | 0:9d17e4342598 | 4584 | RNG* rng, const byte* ntruKey, word16 ntruSz) |
wolfSSL | 0:9d17e4342598 | 4585 | { |
wolfSSL | 0:9d17e4342598 | 4586 | (void)eccKey; |
wolfSSL | 0:9d17e4342598 | 4587 | (void)ntruKey; |
wolfSSL | 0:9d17e4342598 | 4588 | (void)ntruSz; |
wolfSSL | 0:9d17e4342598 | 4589 | |
wolfSSL | 0:9d17e4342598 | 4590 | /* init */ |
wolfSSL | 0:9d17e4342598 | 4591 | XMEMSET(der, 0, sizeof(DerCert)); |
wolfSSL | 0:9d17e4342598 | 4592 | |
wolfSSL | 0:9d17e4342598 | 4593 | /* version */ |
wolfSSL | 0:9d17e4342598 | 4594 | der->versionSz = SetMyVersion(cert->version, der->version, TRUE); |
wolfSSL | 0:9d17e4342598 | 4595 | |
wolfSSL | 0:9d17e4342598 | 4596 | /* serial number */ |
wolfSSL | 0:9d17e4342598 | 4597 | RNG_GenerateBlock(rng, cert->serial, CTC_SERIAL_SIZE); |
wolfSSL | 0:9d17e4342598 | 4598 | cert->serial[0] = 0x01; /* ensure positive */ |
wolfSSL | 0:9d17e4342598 | 4599 | der->serialSz = SetSerial(cert->serial, der->serial); |
wolfSSL | 0:9d17e4342598 | 4600 | |
wolfSSL | 0:9d17e4342598 | 4601 | /* signature algo */ |
wolfSSL | 0:9d17e4342598 | 4602 | der->sigAlgoSz = SetAlgoID(cert->sigType, der->sigAlgo, sigType, 0); |
wolfSSL | 0:9d17e4342598 | 4603 | if (der->sigAlgoSz == 0) |
wolfSSL | 0:9d17e4342598 | 4604 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 4605 | |
wolfSSL | 0:9d17e4342598 | 4606 | /* public key */ |
wolfSSL | 0:9d17e4342598 | 4607 | if (cert->keyType == RSA_KEY) { |
wolfSSL | 0:9d17e4342598 | 4608 | if (rsaKey == NULL) |
wolfSSL | 0:9d17e4342598 | 4609 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4610 | der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey); |
wolfSSL | 0:9d17e4342598 | 4611 | if (der->publicKeySz <= 0) |
wolfSSL | 0:9d17e4342598 | 4612 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4613 | } |
wolfSSL | 0:9d17e4342598 | 4614 | |
wolfSSL | 0:9d17e4342598 | 4615 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 4616 | if (cert->keyType == ECC_KEY) { |
wolfSSL | 0:9d17e4342598 | 4617 | if (eccKey == NULL) |
wolfSSL | 0:9d17e4342598 | 4618 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4619 | der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey); |
wolfSSL | 0:9d17e4342598 | 4620 | if (der->publicKeySz <= 0) |
wolfSSL | 0:9d17e4342598 | 4621 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4622 | } |
wolfSSL | 0:9d17e4342598 | 4623 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 4624 | |
wolfSSL | 0:9d17e4342598 | 4625 | #ifdef HAVE_NTRU |
wolfSSL | 0:9d17e4342598 | 4626 | if (cert->keyType == NTRU_KEY) { |
wolfSSL | 0:9d17e4342598 | 4627 | word32 rc; |
wolfSSL | 0:9d17e4342598 | 4628 | word16 encodedSz; |
wolfSSL | 0:9d17e4342598 | 4629 | |
wolfSSL | 0:9d17e4342598 | 4630 | rc = crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo( ntruSz, |
wolfSSL | 0:9d17e4342598 | 4631 | ntruKey, &encodedSz, NULL); |
wolfSSL | 0:9d17e4342598 | 4632 | if (rc != NTRU_OK) |
wolfSSL | 0:9d17e4342598 | 4633 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4634 | if (encodedSz > MAX_PUBLIC_KEY_SZ) |
wolfSSL | 0:9d17e4342598 | 4635 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4636 | |
wolfSSL | 0:9d17e4342598 | 4637 | rc = crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo( ntruSz, |
wolfSSL | 0:9d17e4342598 | 4638 | ntruKey, &encodedSz, der->publicKey); |
wolfSSL | 0:9d17e4342598 | 4639 | if (rc != NTRU_OK) |
wolfSSL | 0:9d17e4342598 | 4640 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4641 | |
wolfSSL | 0:9d17e4342598 | 4642 | der->publicKeySz = encodedSz; |
wolfSSL | 0:9d17e4342598 | 4643 | } |
wolfSSL | 0:9d17e4342598 | 4644 | #endif /* HAVE_NTRU */ |
wolfSSL | 0:9d17e4342598 | 4645 | |
wolfSSL | 0:9d17e4342598 | 4646 | der->validitySz = 0; |
wolfSSL | 0:9d17e4342598 | 4647 | #ifdef CYASSL_ALT_NAMES |
wolfSSL | 0:9d17e4342598 | 4648 | /* date validity copy ? */ |
wolfSSL | 0:9d17e4342598 | 4649 | if (cert->beforeDateSz && cert->afterDateSz) { |
wolfSSL | 0:9d17e4342598 | 4650 | der->validitySz = CopyValidity(der->validity, cert); |
wolfSSL | 0:9d17e4342598 | 4651 | if (der->validitySz == 0) |
wolfSSL | 0:9d17e4342598 | 4652 | return DATE_E; |
wolfSSL | 0:9d17e4342598 | 4653 | } |
wolfSSL | 0:9d17e4342598 | 4654 | #endif |
wolfSSL | 0:9d17e4342598 | 4655 | |
wolfSSL | 0:9d17e4342598 | 4656 | /* date validity */ |
wolfSSL | 0:9d17e4342598 | 4657 | if (der->validitySz == 0) { |
wolfSSL | 0:9d17e4342598 | 4658 | der->validitySz = SetValidity(der->validity, cert->daysValid); |
wolfSSL | 0:9d17e4342598 | 4659 | if (der->validitySz == 0) |
wolfSSL | 0:9d17e4342598 | 4660 | return DATE_E; |
wolfSSL | 0:9d17e4342598 | 4661 | } |
wolfSSL | 0:9d17e4342598 | 4662 | |
wolfSSL | 0:9d17e4342598 | 4663 | /* subject name */ |
wolfSSL | 0:9d17e4342598 | 4664 | der->subjectSz = SetName(der->subject, &cert->subject); |
wolfSSL | 0:9d17e4342598 | 4665 | if (der->subjectSz == 0) |
wolfSSL | 0:9d17e4342598 | 4666 | return SUBJECT_E; |
wolfSSL | 0:9d17e4342598 | 4667 | |
wolfSSL | 0:9d17e4342598 | 4668 | /* issuer name */ |
wolfSSL | 0:9d17e4342598 | 4669 | der->issuerSz = SetName(der->issuer, cert->selfSigned ? |
wolfSSL | 0:9d17e4342598 | 4670 | &cert->subject : &cert->issuer); |
wolfSSL | 0:9d17e4342598 | 4671 | if (der->issuerSz == 0) |
wolfSSL | 0:9d17e4342598 | 4672 | return ISSUER_E; |
wolfSSL | 0:9d17e4342598 | 4673 | |
wolfSSL | 0:9d17e4342598 | 4674 | /* CA */ |
wolfSSL | 0:9d17e4342598 | 4675 | if (cert->isCA) { |
wolfSSL | 0:9d17e4342598 | 4676 | der->caSz = SetCa(der->ca); |
wolfSSL | 0:9d17e4342598 | 4677 | if (der->caSz == 0) |
wolfSSL | 0:9d17e4342598 | 4678 | return CA_TRUE_E; |
wolfSSL | 0:9d17e4342598 | 4679 | } |
wolfSSL | 0:9d17e4342598 | 4680 | else |
wolfSSL | 0:9d17e4342598 | 4681 | der->caSz = 0; |
wolfSSL | 0:9d17e4342598 | 4682 | |
wolfSSL | 0:9d17e4342598 | 4683 | /* extensions, just CA now */ |
wolfSSL | 0:9d17e4342598 | 4684 | if (cert->isCA) { |
wolfSSL | 0:9d17e4342598 | 4685 | der->extensionsSz = SetExtensions(der->extensions, |
wolfSSL | 0:9d17e4342598 | 4686 | der->ca, der->caSz, TRUE); |
wolfSSL | 0:9d17e4342598 | 4687 | if (der->extensionsSz == 0) |
wolfSSL | 0:9d17e4342598 | 4688 | return EXTENSIONS_E; |
wolfSSL | 0:9d17e4342598 | 4689 | } |
wolfSSL | 0:9d17e4342598 | 4690 | else |
wolfSSL | 0:9d17e4342598 | 4691 | der->extensionsSz = 0; |
wolfSSL | 0:9d17e4342598 | 4692 | |
wolfSSL | 0:9d17e4342598 | 4693 | #ifdef CYASSL_ALT_NAMES |
wolfSSL | 0:9d17e4342598 | 4694 | if (der->extensionsSz == 0 && cert->altNamesSz) { |
wolfSSL | 0:9d17e4342598 | 4695 | der->extensionsSz = SetExtensions(der->extensions, cert->altNames, |
wolfSSL | 0:9d17e4342598 | 4696 | cert->altNamesSz, TRUE); |
wolfSSL | 0:9d17e4342598 | 4697 | if (der->extensionsSz == 0) |
wolfSSL | 0:9d17e4342598 | 4698 | return EXTENSIONS_E; |
wolfSSL | 0:9d17e4342598 | 4699 | } |
wolfSSL | 0:9d17e4342598 | 4700 | #endif |
wolfSSL | 0:9d17e4342598 | 4701 | |
wolfSSL | 0:9d17e4342598 | 4702 | der->total = der->versionSz + der->serialSz + der->sigAlgoSz + |
wolfSSL | 0:9d17e4342598 | 4703 | der->publicKeySz + der->validitySz + der->subjectSz + der->issuerSz + |
wolfSSL | 0:9d17e4342598 | 4704 | der->extensionsSz; |
wolfSSL | 0:9d17e4342598 | 4705 | |
wolfSSL | 0:9d17e4342598 | 4706 | return 0; |
wolfSSL | 0:9d17e4342598 | 4707 | } |
wolfSSL | 0:9d17e4342598 | 4708 | |
wolfSSL | 0:9d17e4342598 | 4709 | |
wolfSSL | 0:9d17e4342598 | 4710 | /* write DER encoded cert to buffer, size already checked */ |
wolfSSL | 0:9d17e4342598 | 4711 | static int WriteCertBody(DerCert* der, byte* buffer) |
wolfSSL | 0:9d17e4342598 | 4712 | { |
wolfSSL | 0:9d17e4342598 | 4713 | int idx; |
wolfSSL | 0:9d17e4342598 | 4714 | |
wolfSSL | 0:9d17e4342598 | 4715 | /* signed part header */ |
wolfSSL | 0:9d17e4342598 | 4716 | idx = SetSequence(der->total, buffer); |
wolfSSL | 0:9d17e4342598 | 4717 | /* version */ |
wolfSSL | 0:9d17e4342598 | 4718 | XMEMCPY(buffer + idx, der->version, der->versionSz); |
wolfSSL | 0:9d17e4342598 | 4719 | idx += der->versionSz; |
wolfSSL | 0:9d17e4342598 | 4720 | /* serial */ |
wolfSSL | 0:9d17e4342598 | 4721 | XMEMCPY(buffer + idx, der->serial, der->serialSz); |
wolfSSL | 0:9d17e4342598 | 4722 | idx += der->serialSz; |
wolfSSL | 0:9d17e4342598 | 4723 | /* sig algo */ |
wolfSSL | 0:9d17e4342598 | 4724 | XMEMCPY(buffer + idx, der->sigAlgo, der->sigAlgoSz); |
wolfSSL | 0:9d17e4342598 | 4725 | idx += der->sigAlgoSz; |
wolfSSL | 0:9d17e4342598 | 4726 | /* issuer */ |
wolfSSL | 0:9d17e4342598 | 4727 | XMEMCPY(buffer + idx, der->issuer, der->issuerSz); |
wolfSSL | 0:9d17e4342598 | 4728 | idx += der->issuerSz; |
wolfSSL | 0:9d17e4342598 | 4729 | /* validity */ |
wolfSSL | 0:9d17e4342598 | 4730 | XMEMCPY(buffer + idx, der->validity, der->validitySz); |
wolfSSL | 0:9d17e4342598 | 4731 | idx += der->validitySz; |
wolfSSL | 0:9d17e4342598 | 4732 | /* subject */ |
wolfSSL | 0:9d17e4342598 | 4733 | XMEMCPY(buffer + idx, der->subject, der->subjectSz); |
wolfSSL | 0:9d17e4342598 | 4734 | idx += der->subjectSz; |
wolfSSL | 0:9d17e4342598 | 4735 | /* public key */ |
wolfSSL | 0:9d17e4342598 | 4736 | XMEMCPY(buffer + idx, der->publicKey, der->publicKeySz); |
wolfSSL | 0:9d17e4342598 | 4737 | idx += der->publicKeySz; |
wolfSSL | 0:9d17e4342598 | 4738 | if (der->extensionsSz) { |
wolfSSL | 0:9d17e4342598 | 4739 | /* extensions */ |
wolfSSL | 0:9d17e4342598 | 4740 | XMEMCPY(buffer + idx, der->extensions, min(der->extensionsSz, |
wolfSSL | 0:9d17e4342598 | 4741 | sizeof(der->extensions))); |
wolfSSL | 0:9d17e4342598 | 4742 | idx += der->extensionsSz; |
wolfSSL | 0:9d17e4342598 | 4743 | } |
wolfSSL | 0:9d17e4342598 | 4744 | |
wolfSSL | 0:9d17e4342598 | 4745 | return idx; |
wolfSSL | 0:9d17e4342598 | 4746 | } |
wolfSSL | 0:9d17e4342598 | 4747 | |
wolfSSL | 0:9d17e4342598 | 4748 | |
wolfSSL | 0:9d17e4342598 | 4749 | /* Make RSA signature from buffer (sz), write to sig (sigSz) */ |
wolfSSL | 0:9d17e4342598 | 4750 | static int MakeSignature(const byte* buffer, int sz, byte* sig, int sigSz, |
wolfSSL | 0:9d17e4342598 | 4751 | RsaKey* rsaKey, ecc_key* eccKey, RNG* rng, |
wolfSSL | 0:9d17e4342598 | 4752 | int sigAlgoType) |
wolfSSL | 0:9d17e4342598 | 4753 | { |
wolfSSL | 0:9d17e4342598 | 4754 | byte digest[SHA256_DIGEST_SIZE]; /* max size */ |
wolfSSL | 0:9d17e4342598 | 4755 | byte encSig[MAX_ENCODED_DIG_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 4756 | int encSigSz, digestSz, typeH, ret = 0; |
wolfSSL | 0:9d17e4342598 | 4757 | |
wolfSSL | 0:9d17e4342598 | 4758 | (void)eccKey; |
wolfSSL | 0:9d17e4342598 | 4759 | |
wolfSSL | 0:9d17e4342598 | 4760 | if (sigAlgoType == CTC_MD5wRSA) { |
wolfSSL | 0:9d17e4342598 | 4761 | Md5 md5; |
wolfSSL | 0:9d17e4342598 | 4762 | InitMd5(&md5); |
wolfSSL | 0:9d17e4342598 | 4763 | Md5Update(&md5, buffer, sz); |
wolfSSL | 0:9d17e4342598 | 4764 | Md5Final(&md5, digest); |
wolfSSL | 0:9d17e4342598 | 4765 | digestSz = MD5_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 4766 | typeH = MD5h; |
wolfSSL | 0:9d17e4342598 | 4767 | } |
wolfSSL | 0:9d17e4342598 | 4768 | else if (sigAlgoType == CTC_SHAwRSA || sigAlgoType == CTC_SHAwECDSA) { |
wolfSSL | 0:9d17e4342598 | 4769 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 4770 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 4771 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 4772 | return ret; |
wolfSSL | 0:9d17e4342598 | 4773 | ShaUpdate(&sha, buffer, sz); |
wolfSSL | 0:9d17e4342598 | 4774 | ShaFinal(&sha, digest); |
wolfSSL | 0:9d17e4342598 | 4775 | digestSz = SHA_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 4776 | typeH = SHAh; |
wolfSSL | 0:9d17e4342598 | 4777 | } |
wolfSSL | 0:9d17e4342598 | 4778 | else if (sigAlgoType == CTC_SHA256wRSA || sigAlgoType == CTC_SHA256wECDSA) { |
wolfSSL | 0:9d17e4342598 | 4779 | Sha256 sha256; |
wolfSSL | 0:9d17e4342598 | 4780 | ret = InitSha256(&sha256); |
wolfSSL | 0:9d17e4342598 | 4781 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 4782 | return ret; |
wolfSSL | 0:9d17e4342598 | 4783 | Sha256Update(&sha256, buffer, sz); |
wolfSSL | 0:9d17e4342598 | 4784 | Sha256Final(&sha256, digest); |
wolfSSL | 0:9d17e4342598 | 4785 | digestSz = SHA256_DIGEST_SIZE; |
wolfSSL | 0:9d17e4342598 | 4786 | typeH = SHA256h; |
wolfSSL | 0:9d17e4342598 | 4787 | } |
wolfSSL | 0:9d17e4342598 | 4788 | else |
wolfSSL | 0:9d17e4342598 | 4789 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 4790 | |
wolfSSL | 0:9d17e4342598 | 4791 | if (rsaKey) { |
wolfSSL | 0:9d17e4342598 | 4792 | /* signature */ |
wolfSSL | 0:9d17e4342598 | 4793 | encSigSz = EncodeSignature(encSig, digest, digestSz, typeH); |
wolfSSL | 0:9d17e4342598 | 4794 | return RsaSSL_Sign(encSig, encSigSz, sig, sigSz, rsaKey, rng); |
wolfSSL | 0:9d17e4342598 | 4795 | } |
wolfSSL | 0:9d17e4342598 | 4796 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 4797 | else if (eccKey) { |
wolfSSL | 0:9d17e4342598 | 4798 | word32 outSz = sigSz; |
wolfSSL | 0:9d17e4342598 | 4799 | ret = ecc_sign_hash(digest, digestSz, sig, &outSz, rng, eccKey); |
wolfSSL | 0:9d17e4342598 | 4800 | |
wolfSSL | 0:9d17e4342598 | 4801 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 4802 | return ret; |
wolfSSL | 0:9d17e4342598 | 4803 | return outSz; |
wolfSSL | 0:9d17e4342598 | 4804 | } |
wolfSSL | 0:9d17e4342598 | 4805 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 4806 | |
wolfSSL | 0:9d17e4342598 | 4807 | return ALGO_ID_E; |
wolfSSL | 0:9d17e4342598 | 4808 | } |
wolfSSL | 0:9d17e4342598 | 4809 | |
wolfSSL | 0:9d17e4342598 | 4810 | |
wolfSSL | 0:9d17e4342598 | 4811 | /* add signature to end of buffer, size of buffer assumed checked, return |
wolfSSL | 0:9d17e4342598 | 4812 | new length */ |
wolfSSL | 0:9d17e4342598 | 4813 | static int AddSignature(byte* buffer, int bodySz, const byte* sig, int sigSz, |
wolfSSL | 0:9d17e4342598 | 4814 | int sigAlgoType) |
wolfSSL | 0:9d17e4342598 | 4815 | { |
wolfSSL | 0:9d17e4342598 | 4816 | byte seq[MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 4817 | int idx = bodySz, seqSz; |
wolfSSL | 0:9d17e4342598 | 4818 | |
wolfSSL | 0:9d17e4342598 | 4819 | /* algo */ |
wolfSSL | 0:9d17e4342598 | 4820 | idx += SetAlgoID(sigAlgoType, buffer + idx, sigType, 0); |
wolfSSL | 0:9d17e4342598 | 4821 | /* bit string */ |
wolfSSL | 0:9d17e4342598 | 4822 | buffer[idx++] = ASN_BIT_STRING; |
wolfSSL | 0:9d17e4342598 | 4823 | /* length */ |
wolfSSL | 0:9d17e4342598 | 4824 | idx += SetLength(sigSz + 1, buffer + idx); |
wolfSSL | 0:9d17e4342598 | 4825 | buffer[idx++] = 0; /* trailing 0 */ |
wolfSSL | 0:9d17e4342598 | 4826 | /* signature */ |
wolfSSL | 0:9d17e4342598 | 4827 | XMEMCPY(buffer + idx, sig, sigSz); |
wolfSSL | 0:9d17e4342598 | 4828 | idx += sigSz; |
wolfSSL | 0:9d17e4342598 | 4829 | |
wolfSSL | 0:9d17e4342598 | 4830 | /* make room for overall header */ |
wolfSSL | 0:9d17e4342598 | 4831 | seqSz = SetSequence(idx, seq); |
wolfSSL | 0:9d17e4342598 | 4832 | XMEMMOVE(buffer + seqSz, buffer, idx); |
wolfSSL | 0:9d17e4342598 | 4833 | XMEMCPY(buffer, seq, seqSz); |
wolfSSL | 0:9d17e4342598 | 4834 | |
wolfSSL | 0:9d17e4342598 | 4835 | return idx + seqSz; |
wolfSSL | 0:9d17e4342598 | 4836 | } |
wolfSSL | 0:9d17e4342598 | 4837 | |
wolfSSL | 0:9d17e4342598 | 4838 | |
wolfSSL | 0:9d17e4342598 | 4839 | /* Make an x509 Certificate v3 any key type from cert input, write to buffer */ |
wolfSSL | 0:9d17e4342598 | 4840 | static int MakeAnyCert(Cert* cert, byte* derBuffer, word32 derSz, |
wolfSSL | 0:9d17e4342598 | 4841 | RsaKey* rsaKey, ecc_key* eccKey, RNG* rng, |
wolfSSL | 0:9d17e4342598 | 4842 | const byte* ntruKey, word16 ntruSz) |
wolfSSL | 0:9d17e4342598 | 4843 | { |
wolfSSL | 0:9d17e4342598 | 4844 | DerCert der; |
wolfSSL | 0:9d17e4342598 | 4845 | int ret; |
wolfSSL | 0:9d17e4342598 | 4846 | |
wolfSSL | 0:9d17e4342598 | 4847 | if (eccKey) |
wolfSSL | 0:9d17e4342598 | 4848 | cert->keyType = ECC_KEY; |
wolfSSL | 0:9d17e4342598 | 4849 | else |
wolfSSL | 0:9d17e4342598 | 4850 | cert->keyType = rsaKey ? RSA_KEY : NTRU_KEY; |
wolfSSL | 0:9d17e4342598 | 4851 | ret = EncodeCert(cert, &der, rsaKey, eccKey, rng, ntruKey, ntruSz); |
wolfSSL | 0:9d17e4342598 | 4852 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 4853 | return ret; |
wolfSSL | 0:9d17e4342598 | 4854 | |
wolfSSL | 0:9d17e4342598 | 4855 | if (der.total + MAX_SEQ_SZ * 2 > (int)derSz) |
wolfSSL | 0:9d17e4342598 | 4856 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 4857 | |
wolfSSL | 0:9d17e4342598 | 4858 | return cert->bodySz = WriteCertBody(&der, derBuffer); |
wolfSSL | 0:9d17e4342598 | 4859 | } |
wolfSSL | 0:9d17e4342598 | 4860 | |
wolfSSL | 0:9d17e4342598 | 4861 | |
wolfSSL | 0:9d17e4342598 | 4862 | /* Make an x509 Certificate v3 RSA or ECC from cert input, write to buffer */ |
wolfSSL | 0:9d17e4342598 | 4863 | int MakeCert(Cert* cert, byte* derBuffer, word32 derSz, RsaKey* rsaKey, |
wolfSSL | 0:9d17e4342598 | 4864 | ecc_key* eccKey, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 4865 | { |
wolfSSL | 0:9d17e4342598 | 4866 | return MakeAnyCert(cert, derBuffer, derSz, rsaKey, eccKey, rng, NULL, 0); |
wolfSSL | 0:9d17e4342598 | 4867 | } |
wolfSSL | 0:9d17e4342598 | 4868 | |
wolfSSL | 0:9d17e4342598 | 4869 | |
wolfSSL | 0:9d17e4342598 | 4870 | #ifdef HAVE_NTRU |
wolfSSL | 0:9d17e4342598 | 4871 | |
wolfSSL | 0:9d17e4342598 | 4872 | int MakeNtruCert(Cert* cert, byte* derBuffer, word32 derSz, |
wolfSSL | 0:9d17e4342598 | 4873 | const byte* ntruKey, word16 keySz, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 4874 | { |
wolfSSL | 0:9d17e4342598 | 4875 | return MakeAnyCert(cert, derBuffer, derSz, NULL, NULL, rng, ntruKey, keySz); |
wolfSSL | 0:9d17e4342598 | 4876 | } |
wolfSSL | 0:9d17e4342598 | 4877 | |
wolfSSL | 0:9d17e4342598 | 4878 | #endif /* HAVE_NTRU */ |
wolfSSL | 0:9d17e4342598 | 4879 | |
wolfSSL | 0:9d17e4342598 | 4880 | |
wolfSSL | 0:9d17e4342598 | 4881 | #ifdef CYASSL_CERT_REQ |
wolfSSL | 0:9d17e4342598 | 4882 | |
wolfSSL | 0:9d17e4342598 | 4883 | static int SetReqAttrib(byte* output, char* pw, int extSz) |
wolfSSL | 0:9d17e4342598 | 4884 | { |
wolfSSL | 0:9d17e4342598 | 4885 | static const byte cpOid[] = |
wolfSSL | 0:9d17e4342598 | 4886 | { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, |
wolfSSL | 0:9d17e4342598 | 4887 | 0x09, 0x07 }; |
wolfSSL | 0:9d17e4342598 | 4888 | static const byte erOid[] = |
wolfSSL | 0:9d17e4342598 | 4889 | { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, |
wolfSSL | 0:9d17e4342598 | 4890 | 0x09, 0x0e }; |
wolfSSL | 0:9d17e4342598 | 4891 | |
wolfSSL | 0:9d17e4342598 | 4892 | int sz = 0; /* overall size */ |
wolfSSL | 0:9d17e4342598 | 4893 | int cpSz = 0; /* Challenge Password section size */ |
wolfSSL | 0:9d17e4342598 | 4894 | int cpSeqSz = 0; |
wolfSSL | 0:9d17e4342598 | 4895 | int cpSetSz = 0; |
wolfSSL | 0:9d17e4342598 | 4896 | int cpStrSz = 0; |
wolfSSL | 0:9d17e4342598 | 4897 | int pwSz = 0; |
wolfSSL | 0:9d17e4342598 | 4898 | int erSz = 0; /* Extension Request section size */ |
wolfSSL | 0:9d17e4342598 | 4899 | int erSeqSz = 0; |
wolfSSL | 0:9d17e4342598 | 4900 | int erSetSz = 0; |
wolfSSL | 0:9d17e4342598 | 4901 | byte cpSeq[MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 4902 | byte cpSet[MAX_SET_SZ]; |
wolfSSL | 0:9d17e4342598 | 4903 | byte cpStr[MAX_PRSTR_SZ]; |
wolfSSL | 0:9d17e4342598 | 4904 | byte erSeq[MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 4905 | byte erSet[MAX_SET_SZ]; |
wolfSSL | 0:9d17e4342598 | 4906 | |
wolfSSL | 0:9d17e4342598 | 4907 | output[0] = 0xa0; |
wolfSSL | 0:9d17e4342598 | 4908 | sz++; |
wolfSSL | 0:9d17e4342598 | 4909 | |
wolfSSL | 0:9d17e4342598 | 4910 | if (pw && pw[0]) { |
wolfSSL | 0:9d17e4342598 | 4911 | pwSz = (int)XSTRLEN(pw); |
wolfSSL | 0:9d17e4342598 | 4912 | cpStrSz = SetUTF8String(pwSz, cpStr); |
wolfSSL | 0:9d17e4342598 | 4913 | cpSetSz = SetSet(cpStrSz + pwSz, cpSet); |
wolfSSL | 0:9d17e4342598 | 4914 | cpSeqSz = SetSequence(sizeof(cpOid) + cpSetSz + cpStrSz + pwSz, cpSeq); |
wolfSSL | 0:9d17e4342598 | 4915 | cpSz = cpSeqSz + sizeof(cpOid) + cpSetSz + cpStrSz + pwSz; |
wolfSSL | 0:9d17e4342598 | 4916 | } |
wolfSSL | 0:9d17e4342598 | 4917 | |
wolfSSL | 0:9d17e4342598 | 4918 | if (extSz) { |
wolfSSL | 0:9d17e4342598 | 4919 | erSetSz = SetSet(extSz, erSet); |
wolfSSL | 0:9d17e4342598 | 4920 | erSeqSz = SetSequence(erSetSz + sizeof(erOid) + extSz, erSeq); |
wolfSSL | 0:9d17e4342598 | 4921 | erSz = extSz + erSetSz + erSeqSz + sizeof(erOid); |
wolfSSL | 0:9d17e4342598 | 4922 | } |
wolfSSL | 0:9d17e4342598 | 4923 | |
wolfSSL | 0:9d17e4342598 | 4924 | /* Put the pieces together. */ |
wolfSSL | 0:9d17e4342598 | 4925 | sz += SetLength(cpSz + erSz, &output[sz]); |
wolfSSL | 0:9d17e4342598 | 4926 | |
wolfSSL | 0:9d17e4342598 | 4927 | if (cpSz) { |
wolfSSL | 0:9d17e4342598 | 4928 | XMEMCPY(&output[sz], cpSeq, cpSeqSz); |
wolfSSL | 0:9d17e4342598 | 4929 | sz += cpSeqSz; |
wolfSSL | 0:9d17e4342598 | 4930 | XMEMCPY(&output[sz], cpOid, sizeof(cpOid)); |
wolfSSL | 0:9d17e4342598 | 4931 | sz += sizeof(cpOid); |
wolfSSL | 0:9d17e4342598 | 4932 | XMEMCPY(&output[sz], cpSet, cpSetSz); |
wolfSSL | 0:9d17e4342598 | 4933 | sz += cpSetSz; |
wolfSSL | 0:9d17e4342598 | 4934 | XMEMCPY(&output[sz], cpStr, cpStrSz); |
wolfSSL | 0:9d17e4342598 | 4935 | sz += cpStrSz; |
wolfSSL | 0:9d17e4342598 | 4936 | XMEMCPY(&output[sz], pw, pwSz); |
wolfSSL | 0:9d17e4342598 | 4937 | sz += pwSz; |
wolfSSL | 0:9d17e4342598 | 4938 | } |
wolfSSL | 0:9d17e4342598 | 4939 | |
wolfSSL | 0:9d17e4342598 | 4940 | if (erSz) { |
wolfSSL | 0:9d17e4342598 | 4941 | XMEMCPY(&output[sz], erSeq, erSeqSz); |
wolfSSL | 0:9d17e4342598 | 4942 | sz += erSeqSz; |
wolfSSL | 0:9d17e4342598 | 4943 | XMEMCPY(&output[sz], erOid, sizeof(erOid)); |
wolfSSL | 0:9d17e4342598 | 4944 | sz += sizeof(erOid); |
wolfSSL | 0:9d17e4342598 | 4945 | XMEMCPY(&output[sz], erSet, erSetSz); |
wolfSSL | 0:9d17e4342598 | 4946 | sz += erSetSz; |
wolfSSL | 0:9d17e4342598 | 4947 | /* The actual extension data will be tacked onto the output later. */ |
wolfSSL | 0:9d17e4342598 | 4948 | } |
wolfSSL | 0:9d17e4342598 | 4949 | |
wolfSSL | 0:9d17e4342598 | 4950 | return sz; |
wolfSSL | 0:9d17e4342598 | 4951 | } |
wolfSSL | 0:9d17e4342598 | 4952 | |
wolfSSL | 0:9d17e4342598 | 4953 | |
wolfSSL | 0:9d17e4342598 | 4954 | /* encode info from cert into DER encoded format */ |
wolfSSL | 0:9d17e4342598 | 4955 | static int EncodeCertReq(Cert* cert, DerCert* der, |
wolfSSL | 0:9d17e4342598 | 4956 | RsaKey* rsaKey, ecc_key* eccKey) |
wolfSSL | 0:9d17e4342598 | 4957 | { |
wolfSSL | 0:9d17e4342598 | 4958 | (void)eccKey; |
wolfSSL | 0:9d17e4342598 | 4959 | |
wolfSSL | 0:9d17e4342598 | 4960 | /* init */ |
wolfSSL | 0:9d17e4342598 | 4961 | XMEMSET(der, 0, sizeof(DerCert)); |
wolfSSL | 0:9d17e4342598 | 4962 | |
wolfSSL | 0:9d17e4342598 | 4963 | /* version */ |
wolfSSL | 0:9d17e4342598 | 4964 | der->versionSz = SetMyVersion(cert->version, der->version, FALSE); |
wolfSSL | 0:9d17e4342598 | 4965 | |
wolfSSL | 0:9d17e4342598 | 4966 | /* subject name */ |
wolfSSL | 0:9d17e4342598 | 4967 | der->subjectSz = SetName(der->subject, &cert->subject); |
wolfSSL | 0:9d17e4342598 | 4968 | if (der->subjectSz == 0) |
wolfSSL | 0:9d17e4342598 | 4969 | return SUBJECT_E; |
wolfSSL | 0:9d17e4342598 | 4970 | |
wolfSSL | 0:9d17e4342598 | 4971 | /* public key */ |
wolfSSL | 0:9d17e4342598 | 4972 | if (cert->keyType == RSA_KEY) { |
wolfSSL | 0:9d17e4342598 | 4973 | if (rsaKey == NULL) |
wolfSSL | 0:9d17e4342598 | 4974 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4975 | der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey); |
wolfSSL | 0:9d17e4342598 | 4976 | if (der->publicKeySz <= 0) |
wolfSSL | 0:9d17e4342598 | 4977 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4978 | } |
wolfSSL | 0:9d17e4342598 | 4979 | |
wolfSSL | 0:9d17e4342598 | 4980 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 4981 | if (cert->keyType == ECC_KEY) { |
wolfSSL | 0:9d17e4342598 | 4982 | if (eccKey == NULL) |
wolfSSL | 0:9d17e4342598 | 4983 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4984 | der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey); |
wolfSSL | 0:9d17e4342598 | 4985 | if (der->publicKeySz <= 0) |
wolfSSL | 0:9d17e4342598 | 4986 | return PUBLIC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 4987 | } |
wolfSSL | 0:9d17e4342598 | 4988 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 4989 | |
wolfSSL | 0:9d17e4342598 | 4990 | /* CA */ |
wolfSSL | 0:9d17e4342598 | 4991 | if (cert->isCA) { |
wolfSSL | 0:9d17e4342598 | 4992 | der->caSz = SetCa(der->ca); |
wolfSSL | 0:9d17e4342598 | 4993 | if (der->caSz == 0) |
wolfSSL | 0:9d17e4342598 | 4994 | return CA_TRUE_E; |
wolfSSL | 0:9d17e4342598 | 4995 | } |
wolfSSL | 0:9d17e4342598 | 4996 | else |
wolfSSL | 0:9d17e4342598 | 4997 | der->caSz = 0; |
wolfSSL | 0:9d17e4342598 | 4998 | |
wolfSSL | 0:9d17e4342598 | 4999 | /* extensions, just CA now */ |
wolfSSL | 0:9d17e4342598 | 5000 | if (cert->isCA) { |
wolfSSL | 0:9d17e4342598 | 5001 | der->extensionsSz = SetExtensions(der->extensions, |
wolfSSL | 0:9d17e4342598 | 5002 | der->ca, der->caSz, FALSE); |
wolfSSL | 0:9d17e4342598 | 5003 | if (der->extensionsSz == 0) |
wolfSSL | 0:9d17e4342598 | 5004 | return EXTENSIONS_E; |
wolfSSL | 0:9d17e4342598 | 5005 | } |
wolfSSL | 0:9d17e4342598 | 5006 | else |
wolfSSL | 0:9d17e4342598 | 5007 | der->extensionsSz = 0; |
wolfSSL | 0:9d17e4342598 | 5008 | |
wolfSSL | 0:9d17e4342598 | 5009 | der->attribSz = SetReqAttrib(der->attrib, |
wolfSSL | 0:9d17e4342598 | 5010 | cert->challengePw, der->extensionsSz); |
wolfSSL | 0:9d17e4342598 | 5011 | if (der->attribSz == 0) |
wolfSSL | 0:9d17e4342598 | 5012 | return REQ_ATTRIBUTE_E; |
wolfSSL | 0:9d17e4342598 | 5013 | |
wolfSSL | 0:9d17e4342598 | 5014 | der->total = der->versionSz + der->subjectSz + der->publicKeySz + |
wolfSSL | 0:9d17e4342598 | 5015 | der->extensionsSz + der->attribSz; |
wolfSSL | 0:9d17e4342598 | 5016 | |
wolfSSL | 0:9d17e4342598 | 5017 | return 0; |
wolfSSL | 0:9d17e4342598 | 5018 | } |
wolfSSL | 0:9d17e4342598 | 5019 | |
wolfSSL | 0:9d17e4342598 | 5020 | |
wolfSSL | 0:9d17e4342598 | 5021 | /* write DER encoded cert req to buffer, size already checked */ |
wolfSSL | 0:9d17e4342598 | 5022 | static int WriteCertReqBody(DerCert* der, byte* buffer) |
wolfSSL | 0:9d17e4342598 | 5023 | { |
wolfSSL | 0:9d17e4342598 | 5024 | int idx; |
wolfSSL | 0:9d17e4342598 | 5025 | |
wolfSSL | 0:9d17e4342598 | 5026 | /* signed part header */ |
wolfSSL | 0:9d17e4342598 | 5027 | idx = SetSequence(der->total, buffer); |
wolfSSL | 0:9d17e4342598 | 5028 | /* version */ |
wolfSSL | 0:9d17e4342598 | 5029 | XMEMCPY(buffer + idx, der->version, der->versionSz); |
wolfSSL | 0:9d17e4342598 | 5030 | idx += der->versionSz; |
wolfSSL | 0:9d17e4342598 | 5031 | /* subject */ |
wolfSSL | 0:9d17e4342598 | 5032 | XMEMCPY(buffer + idx, der->subject, der->subjectSz); |
wolfSSL | 0:9d17e4342598 | 5033 | idx += der->subjectSz; |
wolfSSL | 0:9d17e4342598 | 5034 | /* public key */ |
wolfSSL | 0:9d17e4342598 | 5035 | XMEMCPY(buffer + idx, der->publicKey, der->publicKeySz); |
wolfSSL | 0:9d17e4342598 | 5036 | idx += der->publicKeySz; |
wolfSSL | 0:9d17e4342598 | 5037 | /* attributes */ |
wolfSSL | 0:9d17e4342598 | 5038 | XMEMCPY(buffer + idx, der->attrib, der->attribSz); |
wolfSSL | 0:9d17e4342598 | 5039 | idx += der->attribSz; |
wolfSSL | 0:9d17e4342598 | 5040 | /* extensions */ |
wolfSSL | 0:9d17e4342598 | 5041 | if (der->extensionsSz) { |
wolfSSL | 0:9d17e4342598 | 5042 | XMEMCPY(buffer + idx, der->extensions, min(der->extensionsSz, |
wolfSSL | 0:9d17e4342598 | 5043 | sizeof(der->extensions))); |
wolfSSL | 0:9d17e4342598 | 5044 | idx += der->extensionsSz; |
wolfSSL | 0:9d17e4342598 | 5045 | } |
wolfSSL | 0:9d17e4342598 | 5046 | |
wolfSSL | 0:9d17e4342598 | 5047 | return idx; |
wolfSSL | 0:9d17e4342598 | 5048 | } |
wolfSSL | 0:9d17e4342598 | 5049 | |
wolfSSL | 0:9d17e4342598 | 5050 | |
wolfSSL | 0:9d17e4342598 | 5051 | int MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz, |
wolfSSL | 0:9d17e4342598 | 5052 | RsaKey* rsaKey, ecc_key* eccKey) |
wolfSSL | 0:9d17e4342598 | 5053 | { |
wolfSSL | 0:9d17e4342598 | 5054 | DerCert der; |
wolfSSL | 0:9d17e4342598 | 5055 | int ret; |
wolfSSL | 0:9d17e4342598 | 5056 | |
wolfSSL | 0:9d17e4342598 | 5057 | cert->keyType = (eccKey != NULL) ? ECC_KEY : RSA_KEY; |
wolfSSL | 0:9d17e4342598 | 5058 | ret = EncodeCertReq(cert, &der, rsaKey, eccKey); |
wolfSSL | 0:9d17e4342598 | 5059 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 5060 | return ret; |
wolfSSL | 0:9d17e4342598 | 5061 | |
wolfSSL | 0:9d17e4342598 | 5062 | if (der.total + MAX_SEQ_SZ * 2 > (int)derSz) |
wolfSSL | 0:9d17e4342598 | 5063 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 5064 | |
wolfSSL | 0:9d17e4342598 | 5065 | return cert->bodySz = WriteCertReqBody(&der, derBuffer); |
wolfSSL | 0:9d17e4342598 | 5066 | } |
wolfSSL | 0:9d17e4342598 | 5067 | |
wolfSSL | 0:9d17e4342598 | 5068 | #endif /* CYASSL_CERT_REQ */ |
wolfSSL | 0:9d17e4342598 | 5069 | |
wolfSSL | 0:9d17e4342598 | 5070 | |
wolfSSL | 0:9d17e4342598 | 5071 | int SignCert(int requestSz, int sType, byte* buffer, word32 buffSz, |
wolfSSL | 0:9d17e4342598 | 5072 | RsaKey* rsaKey, ecc_key* eccKey, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 5073 | { |
wolfSSL | 0:9d17e4342598 | 5074 | byte sig[MAX_ENCODED_SIG_SZ]; |
wolfSSL | 0:9d17e4342598 | 5075 | int sigSz; |
wolfSSL | 0:9d17e4342598 | 5076 | |
wolfSSL | 0:9d17e4342598 | 5077 | if (requestSz < 0) |
wolfSSL | 0:9d17e4342598 | 5078 | return requestSz; |
wolfSSL | 0:9d17e4342598 | 5079 | |
wolfSSL | 0:9d17e4342598 | 5080 | sigSz = MakeSignature(buffer, requestSz, sig, sizeof(sig), rsaKey, eccKey, |
wolfSSL | 0:9d17e4342598 | 5081 | rng, sType); |
wolfSSL | 0:9d17e4342598 | 5082 | if (sigSz < 0) |
wolfSSL | 0:9d17e4342598 | 5083 | return sigSz; |
wolfSSL | 0:9d17e4342598 | 5084 | |
wolfSSL | 0:9d17e4342598 | 5085 | if (requestSz + MAX_SEQ_SZ * 2 + sigSz > (int)buffSz) |
wolfSSL | 0:9d17e4342598 | 5086 | return BUFFER_E; |
wolfSSL | 0:9d17e4342598 | 5087 | |
wolfSSL | 0:9d17e4342598 | 5088 | return AddSignature(buffer, requestSz, sig, sigSz, sType); |
wolfSSL | 0:9d17e4342598 | 5089 | } |
wolfSSL | 0:9d17e4342598 | 5090 | |
wolfSSL | 0:9d17e4342598 | 5091 | |
wolfSSL | 0:9d17e4342598 | 5092 | int MakeSelfCert(Cert* cert, byte* buffer, word32 buffSz, RsaKey* key, RNG* rng) |
wolfSSL | 0:9d17e4342598 | 5093 | { |
wolfSSL | 0:9d17e4342598 | 5094 | int ret = MakeCert(cert, buffer, buffSz, key, NULL, rng); |
wolfSSL | 0:9d17e4342598 | 5095 | |
wolfSSL | 0:9d17e4342598 | 5096 | if (ret < 0) |
wolfSSL | 0:9d17e4342598 | 5097 | return ret; |
wolfSSL | 0:9d17e4342598 | 5098 | |
wolfSSL | 0:9d17e4342598 | 5099 | return SignCert(cert->bodySz, cert->sigType, buffer, buffSz, key, NULL,rng); |
wolfSSL | 0:9d17e4342598 | 5100 | } |
wolfSSL | 0:9d17e4342598 | 5101 | |
wolfSSL | 0:9d17e4342598 | 5102 | |
wolfSSL | 0:9d17e4342598 | 5103 | #ifdef CYASSL_ALT_NAMES |
wolfSSL | 0:9d17e4342598 | 5104 | |
wolfSSL | 0:9d17e4342598 | 5105 | /* Set Alt Names from der cert, return 0 on success */ |
wolfSSL | 0:9d17e4342598 | 5106 | static int SetAltNamesFromCert(Cert* cert, const byte* der, int derSz) |
wolfSSL | 0:9d17e4342598 | 5107 | { |
wolfSSL | 0:9d17e4342598 | 5108 | DecodedCert decoded; |
wolfSSL | 0:9d17e4342598 | 5109 | int ret; |
wolfSSL | 0:9d17e4342598 | 5110 | |
wolfSSL | 0:9d17e4342598 | 5111 | if (derSz < 0) |
wolfSSL | 0:9d17e4342598 | 5112 | return derSz; |
wolfSSL | 0:9d17e4342598 | 5113 | |
wolfSSL | 0:9d17e4342598 | 5114 | InitDecodedCert(&decoded, (byte*)der, derSz, 0); |
wolfSSL | 0:9d17e4342598 | 5115 | ret = ParseCertRelative(&decoded, CA_TYPE, NO_VERIFY, 0); |
wolfSSL | 0:9d17e4342598 | 5116 | |
wolfSSL | 0:9d17e4342598 | 5117 | if (ret < 0) { |
wolfSSL | 0:9d17e4342598 | 5118 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5119 | return ret; |
wolfSSL | 0:9d17e4342598 | 5120 | } |
wolfSSL | 0:9d17e4342598 | 5121 | |
wolfSSL | 0:9d17e4342598 | 5122 | if (decoded.extensions) { |
wolfSSL | 0:9d17e4342598 | 5123 | byte b; |
wolfSSL | 0:9d17e4342598 | 5124 | int length; |
wolfSSL | 0:9d17e4342598 | 5125 | word32 maxExtensionsIdx; |
wolfSSL | 0:9d17e4342598 | 5126 | |
wolfSSL | 0:9d17e4342598 | 5127 | decoded.srcIdx = decoded.extensionsIdx; |
wolfSSL | 0:9d17e4342598 | 5128 | b = decoded.source[decoded.srcIdx++]; |
wolfSSL | 0:9d17e4342598 | 5129 | if (b != ASN_EXTENSIONS) { |
wolfSSL | 0:9d17e4342598 | 5130 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5131 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5132 | } |
wolfSSL | 0:9d17e4342598 | 5133 | |
wolfSSL | 0:9d17e4342598 | 5134 | if (GetLength(decoded.source, &decoded.srcIdx, &length, |
wolfSSL | 0:9d17e4342598 | 5135 | decoded.maxIdx) < 0) { |
wolfSSL | 0:9d17e4342598 | 5136 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5137 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5138 | } |
wolfSSL | 0:9d17e4342598 | 5139 | |
wolfSSL | 0:9d17e4342598 | 5140 | if (GetSequence(decoded.source, &decoded.srcIdx, &length, |
wolfSSL | 0:9d17e4342598 | 5141 | decoded.maxIdx) < 0) { |
wolfSSL | 0:9d17e4342598 | 5142 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5143 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5144 | } |
wolfSSL | 0:9d17e4342598 | 5145 | |
wolfSSL | 0:9d17e4342598 | 5146 | maxExtensionsIdx = decoded.srcIdx + length; |
wolfSSL | 0:9d17e4342598 | 5147 | |
wolfSSL | 0:9d17e4342598 | 5148 | while (decoded.srcIdx < maxExtensionsIdx) { |
wolfSSL | 0:9d17e4342598 | 5149 | word32 oid; |
wolfSSL | 0:9d17e4342598 | 5150 | word32 startIdx = decoded.srcIdx; |
wolfSSL | 0:9d17e4342598 | 5151 | word32 tmpIdx; |
wolfSSL | 0:9d17e4342598 | 5152 | |
wolfSSL | 0:9d17e4342598 | 5153 | if (GetSequence(decoded.source, &decoded.srcIdx, &length, |
wolfSSL | 0:9d17e4342598 | 5154 | decoded.maxIdx) < 0) { |
wolfSSL | 0:9d17e4342598 | 5155 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5156 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5157 | } |
wolfSSL | 0:9d17e4342598 | 5158 | |
wolfSSL | 0:9d17e4342598 | 5159 | tmpIdx = decoded.srcIdx; |
wolfSSL | 0:9d17e4342598 | 5160 | decoded.srcIdx = startIdx; |
wolfSSL | 0:9d17e4342598 | 5161 | |
wolfSSL | 0:9d17e4342598 | 5162 | if (GetAlgoId(decoded.source, &decoded.srcIdx, &oid, |
wolfSSL | 0:9d17e4342598 | 5163 | decoded.maxIdx) < 0) { |
wolfSSL | 0:9d17e4342598 | 5164 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5165 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5166 | } |
wolfSSL | 0:9d17e4342598 | 5167 | |
wolfSSL | 0:9d17e4342598 | 5168 | if (oid == ALT_NAMES_OID) { |
wolfSSL | 0:9d17e4342598 | 5169 | cert->altNamesSz = length + (tmpIdx - startIdx); |
wolfSSL | 0:9d17e4342598 | 5170 | |
wolfSSL | 0:9d17e4342598 | 5171 | if (cert->altNamesSz < (int)sizeof(cert->altNames)) |
wolfSSL | 0:9d17e4342598 | 5172 | XMEMCPY(cert->altNames, &decoded.source[startIdx], |
wolfSSL | 0:9d17e4342598 | 5173 | cert->altNamesSz); |
wolfSSL | 0:9d17e4342598 | 5174 | else { |
wolfSSL | 0:9d17e4342598 | 5175 | cert->altNamesSz = 0; |
wolfSSL | 0:9d17e4342598 | 5176 | CYASSL_MSG("AltNames extensions too big"); |
wolfSSL | 0:9d17e4342598 | 5177 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5178 | return ALT_NAME_E; |
wolfSSL | 0:9d17e4342598 | 5179 | } |
wolfSSL | 0:9d17e4342598 | 5180 | } |
wolfSSL | 0:9d17e4342598 | 5181 | decoded.srcIdx = tmpIdx + length; |
wolfSSL | 0:9d17e4342598 | 5182 | } |
wolfSSL | 0:9d17e4342598 | 5183 | } |
wolfSSL | 0:9d17e4342598 | 5184 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5185 | |
wolfSSL | 0:9d17e4342598 | 5186 | return 0; |
wolfSSL | 0:9d17e4342598 | 5187 | } |
wolfSSL | 0:9d17e4342598 | 5188 | |
wolfSSL | 0:9d17e4342598 | 5189 | |
wolfSSL | 0:9d17e4342598 | 5190 | /* Set Dates from der cert, return 0 on success */ |
wolfSSL | 0:9d17e4342598 | 5191 | static int SetDatesFromCert(Cert* cert, const byte* der, int derSz) |
wolfSSL | 0:9d17e4342598 | 5192 | { |
wolfSSL | 0:9d17e4342598 | 5193 | DecodedCert decoded; |
wolfSSL | 0:9d17e4342598 | 5194 | int ret; |
wolfSSL | 0:9d17e4342598 | 5195 | |
wolfSSL | 0:9d17e4342598 | 5196 | CYASSL_ENTER("SetDatesFromCert"); |
wolfSSL | 0:9d17e4342598 | 5197 | if (derSz < 0) |
wolfSSL | 0:9d17e4342598 | 5198 | return derSz; |
wolfSSL | 0:9d17e4342598 | 5199 | |
wolfSSL | 0:9d17e4342598 | 5200 | InitDecodedCert(&decoded, (byte*)der, derSz, 0); |
wolfSSL | 0:9d17e4342598 | 5201 | ret = ParseCertRelative(&decoded, CA_TYPE, NO_VERIFY, 0); |
wolfSSL | 0:9d17e4342598 | 5202 | |
wolfSSL | 0:9d17e4342598 | 5203 | if (ret < 0) { |
wolfSSL | 0:9d17e4342598 | 5204 | CYASSL_MSG("ParseCertRelative error"); |
wolfSSL | 0:9d17e4342598 | 5205 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5206 | return ret; |
wolfSSL | 0:9d17e4342598 | 5207 | } |
wolfSSL | 0:9d17e4342598 | 5208 | |
wolfSSL | 0:9d17e4342598 | 5209 | if (decoded.beforeDate == NULL || decoded.afterDate == NULL) { |
wolfSSL | 0:9d17e4342598 | 5210 | CYASSL_MSG("Couldn't extract dates"); |
wolfSSL | 0:9d17e4342598 | 5211 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5212 | return -1; |
wolfSSL | 0:9d17e4342598 | 5213 | } |
wolfSSL | 0:9d17e4342598 | 5214 | |
wolfSSL | 0:9d17e4342598 | 5215 | if (decoded.beforeDateLen > MAX_DATE_SIZE || decoded.afterDateLen > |
wolfSSL | 0:9d17e4342598 | 5216 | MAX_DATE_SIZE) { |
wolfSSL | 0:9d17e4342598 | 5217 | CYASSL_MSG("Bad date size"); |
wolfSSL | 0:9d17e4342598 | 5218 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5219 | return -1; |
wolfSSL | 0:9d17e4342598 | 5220 | } |
wolfSSL | 0:9d17e4342598 | 5221 | |
wolfSSL | 0:9d17e4342598 | 5222 | XMEMCPY(cert->beforeDate, decoded.beforeDate, decoded.beforeDateLen); |
wolfSSL | 0:9d17e4342598 | 5223 | XMEMCPY(cert->afterDate, decoded.afterDate, decoded.afterDateLen); |
wolfSSL | 0:9d17e4342598 | 5224 | |
wolfSSL | 0:9d17e4342598 | 5225 | cert->beforeDateSz = decoded.beforeDateLen; |
wolfSSL | 0:9d17e4342598 | 5226 | cert->afterDateSz = decoded.afterDateLen; |
wolfSSL | 0:9d17e4342598 | 5227 | |
wolfSSL | 0:9d17e4342598 | 5228 | return 0; |
wolfSSL | 0:9d17e4342598 | 5229 | } |
wolfSSL | 0:9d17e4342598 | 5230 | |
wolfSSL | 0:9d17e4342598 | 5231 | |
wolfSSL | 0:9d17e4342598 | 5232 | #endif /* CYASSL_ALT_NAMES && !NO_RSA */ |
wolfSSL | 0:9d17e4342598 | 5233 | |
wolfSSL | 0:9d17e4342598 | 5234 | |
wolfSSL | 0:9d17e4342598 | 5235 | /* Set cn name from der buffer, return 0 on success */ |
wolfSSL | 0:9d17e4342598 | 5236 | static int SetNameFromCert(CertName* cn, const byte* der, int derSz) |
wolfSSL | 0:9d17e4342598 | 5237 | { |
wolfSSL | 0:9d17e4342598 | 5238 | DecodedCert decoded; |
wolfSSL | 0:9d17e4342598 | 5239 | int ret; |
wolfSSL | 0:9d17e4342598 | 5240 | int sz; |
wolfSSL | 0:9d17e4342598 | 5241 | |
wolfSSL | 0:9d17e4342598 | 5242 | if (derSz < 0) |
wolfSSL | 0:9d17e4342598 | 5243 | return derSz; |
wolfSSL | 0:9d17e4342598 | 5244 | |
wolfSSL | 0:9d17e4342598 | 5245 | InitDecodedCert(&decoded, (byte*)der, derSz, 0); |
wolfSSL | 0:9d17e4342598 | 5246 | ret = ParseCertRelative(&decoded, CA_TYPE, NO_VERIFY, 0); |
wolfSSL | 0:9d17e4342598 | 5247 | |
wolfSSL | 0:9d17e4342598 | 5248 | if (ret < 0) |
wolfSSL | 0:9d17e4342598 | 5249 | return ret; |
wolfSSL | 0:9d17e4342598 | 5250 | |
wolfSSL | 0:9d17e4342598 | 5251 | if (decoded.subjectCN) { |
wolfSSL | 0:9d17e4342598 | 5252 | sz = (decoded.subjectCNLen < CTC_NAME_SIZE) ? decoded.subjectCNLen : |
wolfSSL | 0:9d17e4342598 | 5253 | CTC_NAME_SIZE - 1; |
wolfSSL | 0:9d17e4342598 | 5254 | strncpy(cn->commonName, decoded.subjectCN, CTC_NAME_SIZE); |
wolfSSL | 0:9d17e4342598 | 5255 | cn->commonName[sz] = 0; |
wolfSSL | 0:9d17e4342598 | 5256 | } |
wolfSSL | 0:9d17e4342598 | 5257 | if (decoded.subjectC) { |
wolfSSL | 0:9d17e4342598 | 5258 | sz = (decoded.subjectCLen < CTC_NAME_SIZE) ? decoded.subjectCLen : |
wolfSSL | 0:9d17e4342598 | 5259 | CTC_NAME_SIZE - 1; |
wolfSSL | 0:9d17e4342598 | 5260 | strncpy(cn->country, decoded.subjectC, CTC_NAME_SIZE); |
wolfSSL | 0:9d17e4342598 | 5261 | cn->country[sz] = 0; |
wolfSSL | 0:9d17e4342598 | 5262 | } |
wolfSSL | 0:9d17e4342598 | 5263 | if (decoded.subjectST) { |
wolfSSL | 0:9d17e4342598 | 5264 | sz = (decoded.subjectSTLen < CTC_NAME_SIZE) ? decoded.subjectSTLen : |
wolfSSL | 0:9d17e4342598 | 5265 | CTC_NAME_SIZE - 1; |
wolfSSL | 0:9d17e4342598 | 5266 | strncpy(cn->state, decoded.subjectST, CTC_NAME_SIZE); |
wolfSSL | 0:9d17e4342598 | 5267 | cn->state[sz] = 0; |
wolfSSL | 0:9d17e4342598 | 5268 | } |
wolfSSL | 0:9d17e4342598 | 5269 | if (decoded.subjectL) { |
wolfSSL | 0:9d17e4342598 | 5270 | sz = (decoded.subjectLLen < CTC_NAME_SIZE) ? decoded.subjectLLen : |
wolfSSL | 0:9d17e4342598 | 5271 | CTC_NAME_SIZE - 1; |
wolfSSL | 0:9d17e4342598 | 5272 | strncpy(cn->locality, decoded.subjectL, CTC_NAME_SIZE); |
wolfSSL | 0:9d17e4342598 | 5273 | cn->locality[sz] = 0; |
wolfSSL | 0:9d17e4342598 | 5274 | } |
wolfSSL | 0:9d17e4342598 | 5275 | if (decoded.subjectO) { |
wolfSSL | 0:9d17e4342598 | 5276 | sz = (decoded.subjectOLen < CTC_NAME_SIZE) ? decoded.subjectOLen : |
wolfSSL | 0:9d17e4342598 | 5277 | CTC_NAME_SIZE - 1; |
wolfSSL | 0:9d17e4342598 | 5278 | strncpy(cn->org, decoded.subjectO, CTC_NAME_SIZE); |
wolfSSL | 0:9d17e4342598 | 5279 | cn->org[sz] = 0; |
wolfSSL | 0:9d17e4342598 | 5280 | } |
wolfSSL | 0:9d17e4342598 | 5281 | if (decoded.subjectOU) { |
wolfSSL | 0:9d17e4342598 | 5282 | sz = (decoded.subjectOULen < CTC_NAME_SIZE) ? decoded.subjectOULen : |
wolfSSL | 0:9d17e4342598 | 5283 | CTC_NAME_SIZE - 1; |
wolfSSL | 0:9d17e4342598 | 5284 | strncpy(cn->unit, decoded.subjectOU, CTC_NAME_SIZE); |
wolfSSL | 0:9d17e4342598 | 5285 | cn->unit[sz] = 0; |
wolfSSL | 0:9d17e4342598 | 5286 | } |
wolfSSL | 0:9d17e4342598 | 5287 | if (decoded.subjectSN) { |
wolfSSL | 0:9d17e4342598 | 5288 | sz = (decoded.subjectSNLen < CTC_NAME_SIZE) ? decoded.subjectSNLen : |
wolfSSL | 0:9d17e4342598 | 5289 | CTC_NAME_SIZE - 1; |
wolfSSL | 0:9d17e4342598 | 5290 | strncpy(cn->sur, decoded.subjectSN, CTC_NAME_SIZE); |
wolfSSL | 0:9d17e4342598 | 5291 | cn->sur[sz] = 0; |
wolfSSL | 0:9d17e4342598 | 5292 | } |
wolfSSL | 0:9d17e4342598 | 5293 | if (decoded.subjectEmail) { |
wolfSSL | 0:9d17e4342598 | 5294 | sz = (decoded.subjectEmailLen < CTC_NAME_SIZE) ? |
wolfSSL | 0:9d17e4342598 | 5295 | decoded.subjectEmailLen : CTC_NAME_SIZE - 1; |
wolfSSL | 0:9d17e4342598 | 5296 | strncpy(cn->email, decoded.subjectEmail, CTC_NAME_SIZE); |
wolfSSL | 0:9d17e4342598 | 5297 | cn->email[sz] = 0; |
wolfSSL | 0:9d17e4342598 | 5298 | } |
wolfSSL | 0:9d17e4342598 | 5299 | |
wolfSSL | 0:9d17e4342598 | 5300 | FreeDecodedCert(&decoded); |
wolfSSL | 0:9d17e4342598 | 5301 | |
wolfSSL | 0:9d17e4342598 | 5302 | return 0; |
wolfSSL | 0:9d17e4342598 | 5303 | } |
wolfSSL | 0:9d17e4342598 | 5304 | |
wolfSSL | 0:9d17e4342598 | 5305 | |
wolfSSL | 0:9d17e4342598 | 5306 | #ifndef NO_FILESYSTEM |
wolfSSL | 0:9d17e4342598 | 5307 | |
wolfSSL | 0:9d17e4342598 | 5308 | /* forward from CyaSSL */ |
wolfSSL | 0:9d17e4342598 | 5309 | int CyaSSL_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz); |
wolfSSL | 0:9d17e4342598 | 5310 | |
wolfSSL | 0:9d17e4342598 | 5311 | /* Set cert issuer from issuerFile in PEM */ |
wolfSSL | 0:9d17e4342598 | 5312 | int SetIssuer(Cert* cert, const char* issuerFile) |
wolfSSL | 0:9d17e4342598 | 5313 | { |
wolfSSL | 0:9d17e4342598 | 5314 | int ret; |
wolfSSL | 0:9d17e4342598 | 5315 | int derSz; |
wolfSSL | 0:9d17e4342598 | 5316 | byte* der = (byte*)XMALLOC(EIGHTK_BUF, NULL, DYNAMIC_TYPE_CERT); |
wolfSSL | 0:9d17e4342598 | 5317 | |
wolfSSL | 0:9d17e4342598 | 5318 | if (der == NULL) { |
wolfSSL | 0:9d17e4342598 | 5319 | CYASSL_MSG("SetIssuer OOF Problem"); |
wolfSSL | 0:9d17e4342598 | 5320 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 5321 | } |
wolfSSL | 0:9d17e4342598 | 5322 | derSz = CyaSSL_PemCertToDer(issuerFile, der, EIGHTK_BUF); |
wolfSSL | 0:9d17e4342598 | 5323 | cert->selfSigned = 0; |
wolfSSL | 0:9d17e4342598 | 5324 | ret = SetNameFromCert(&cert->issuer, der, derSz); |
wolfSSL | 0:9d17e4342598 | 5325 | XFREE(der, NULL, DYNAMIC_TYPE_CERT); |
wolfSSL | 0:9d17e4342598 | 5326 | |
wolfSSL | 0:9d17e4342598 | 5327 | return ret; |
wolfSSL | 0:9d17e4342598 | 5328 | } |
wolfSSL | 0:9d17e4342598 | 5329 | |
wolfSSL | 0:9d17e4342598 | 5330 | |
wolfSSL | 0:9d17e4342598 | 5331 | /* Set cert subject from subjectFile in PEM */ |
wolfSSL | 0:9d17e4342598 | 5332 | int SetSubject(Cert* cert, const char* subjectFile) |
wolfSSL | 0:9d17e4342598 | 5333 | { |
wolfSSL | 0:9d17e4342598 | 5334 | int ret; |
wolfSSL | 0:9d17e4342598 | 5335 | int derSz; |
wolfSSL | 0:9d17e4342598 | 5336 | byte* der = (byte*)XMALLOC(EIGHTK_BUF, NULL, DYNAMIC_TYPE_CERT); |
wolfSSL | 0:9d17e4342598 | 5337 | |
wolfSSL | 0:9d17e4342598 | 5338 | if (der == NULL) { |
wolfSSL | 0:9d17e4342598 | 5339 | CYASSL_MSG("SetSubject OOF Problem"); |
wolfSSL | 0:9d17e4342598 | 5340 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 5341 | } |
wolfSSL | 0:9d17e4342598 | 5342 | derSz = CyaSSL_PemCertToDer(subjectFile, der, EIGHTK_BUF); |
wolfSSL | 0:9d17e4342598 | 5343 | ret = SetNameFromCert(&cert->subject, der, derSz); |
wolfSSL | 0:9d17e4342598 | 5344 | XFREE(der, NULL, DYNAMIC_TYPE_CERT); |
wolfSSL | 0:9d17e4342598 | 5345 | |
wolfSSL | 0:9d17e4342598 | 5346 | return ret; |
wolfSSL | 0:9d17e4342598 | 5347 | } |
wolfSSL | 0:9d17e4342598 | 5348 | |
wolfSSL | 0:9d17e4342598 | 5349 | |
wolfSSL | 0:9d17e4342598 | 5350 | #ifdef CYASSL_ALT_NAMES |
wolfSSL | 0:9d17e4342598 | 5351 | |
wolfSSL | 0:9d17e4342598 | 5352 | /* Set atl names from file in PEM */ |
wolfSSL | 0:9d17e4342598 | 5353 | int SetAltNames(Cert* cert, const char* file) |
wolfSSL | 0:9d17e4342598 | 5354 | { |
wolfSSL | 0:9d17e4342598 | 5355 | int ret; |
wolfSSL | 0:9d17e4342598 | 5356 | int derSz; |
wolfSSL | 0:9d17e4342598 | 5357 | byte* der = (byte*)XMALLOC(EIGHTK_BUF, NULL, DYNAMIC_TYPE_CERT); |
wolfSSL | 0:9d17e4342598 | 5358 | |
wolfSSL | 0:9d17e4342598 | 5359 | if (der == NULL) { |
wolfSSL | 0:9d17e4342598 | 5360 | CYASSL_MSG("SetAltNames OOF Problem"); |
wolfSSL | 0:9d17e4342598 | 5361 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 5362 | } |
wolfSSL | 0:9d17e4342598 | 5363 | derSz = CyaSSL_PemCertToDer(file, der, EIGHTK_BUF); |
wolfSSL | 0:9d17e4342598 | 5364 | ret = SetAltNamesFromCert(cert, der, derSz); |
wolfSSL | 0:9d17e4342598 | 5365 | XFREE(der, NULL, DYNAMIC_TYPE_CERT); |
wolfSSL | 0:9d17e4342598 | 5366 | |
wolfSSL | 0:9d17e4342598 | 5367 | return ret; |
wolfSSL | 0:9d17e4342598 | 5368 | } |
wolfSSL | 0:9d17e4342598 | 5369 | |
wolfSSL | 0:9d17e4342598 | 5370 | #endif /* CYASSL_ALT_NAMES */ |
wolfSSL | 0:9d17e4342598 | 5371 | |
wolfSSL | 0:9d17e4342598 | 5372 | #endif /* NO_FILESYSTEM */ |
wolfSSL | 0:9d17e4342598 | 5373 | |
wolfSSL | 0:9d17e4342598 | 5374 | /* Set cert issuer from DER buffer */ |
wolfSSL | 0:9d17e4342598 | 5375 | int SetIssuerBuffer(Cert* cert, const byte* der, int derSz) |
wolfSSL | 0:9d17e4342598 | 5376 | { |
wolfSSL | 0:9d17e4342598 | 5377 | cert->selfSigned = 0; |
wolfSSL | 0:9d17e4342598 | 5378 | return SetNameFromCert(&cert->issuer, der, derSz); |
wolfSSL | 0:9d17e4342598 | 5379 | } |
wolfSSL | 0:9d17e4342598 | 5380 | |
wolfSSL | 0:9d17e4342598 | 5381 | |
wolfSSL | 0:9d17e4342598 | 5382 | /* Set cert subject from DER buffer */ |
wolfSSL | 0:9d17e4342598 | 5383 | int SetSubjectBuffer(Cert* cert, const byte* der, int derSz) |
wolfSSL | 0:9d17e4342598 | 5384 | { |
wolfSSL | 0:9d17e4342598 | 5385 | return SetNameFromCert(&cert->subject, der, derSz); |
wolfSSL | 0:9d17e4342598 | 5386 | } |
wolfSSL | 0:9d17e4342598 | 5387 | |
wolfSSL | 0:9d17e4342598 | 5388 | |
wolfSSL | 0:9d17e4342598 | 5389 | #ifdef CYASSL_ALT_NAMES |
wolfSSL | 0:9d17e4342598 | 5390 | |
wolfSSL | 0:9d17e4342598 | 5391 | /* Set cert alt names from DER buffer */ |
wolfSSL | 0:9d17e4342598 | 5392 | int SetAltNamesBuffer(Cert* cert, const byte* der, int derSz) |
wolfSSL | 0:9d17e4342598 | 5393 | { |
wolfSSL | 0:9d17e4342598 | 5394 | return SetAltNamesFromCert(cert, der, derSz); |
wolfSSL | 0:9d17e4342598 | 5395 | } |
wolfSSL | 0:9d17e4342598 | 5396 | |
wolfSSL | 0:9d17e4342598 | 5397 | /* Set cert dates from DER buffer */ |
wolfSSL | 0:9d17e4342598 | 5398 | int SetDatesBuffer(Cert* cert, const byte* der, int derSz) |
wolfSSL | 0:9d17e4342598 | 5399 | { |
wolfSSL | 0:9d17e4342598 | 5400 | return SetDatesFromCert(cert, der, derSz); |
wolfSSL | 0:9d17e4342598 | 5401 | } |
wolfSSL | 0:9d17e4342598 | 5402 | |
wolfSSL | 0:9d17e4342598 | 5403 | #endif /* CYASSL_ALT_NAMES */ |
wolfSSL | 0:9d17e4342598 | 5404 | |
wolfSSL | 0:9d17e4342598 | 5405 | #endif /* CYASSL_CERT_GEN */ |
wolfSSL | 0:9d17e4342598 | 5406 | |
wolfSSL | 0:9d17e4342598 | 5407 | |
wolfSSL | 0:9d17e4342598 | 5408 | #ifdef HAVE_ECC |
wolfSSL | 0:9d17e4342598 | 5409 | |
wolfSSL | 0:9d17e4342598 | 5410 | /* Der Encode r & s ints into out, outLen is (in/out) size */ |
wolfSSL | 0:9d17e4342598 | 5411 | int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r, mp_int* s) |
wolfSSL | 0:9d17e4342598 | 5412 | { |
wolfSSL | 0:9d17e4342598 | 5413 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 5414 | word32 rSz; /* encoding size */ |
wolfSSL | 0:9d17e4342598 | 5415 | word32 sSz; |
wolfSSL | 0:9d17e4342598 | 5416 | word32 headerSz = 4; /* 2*ASN_TAG + 2*LEN(ENUM) */ |
wolfSSL | 0:9d17e4342598 | 5417 | |
wolfSSL | 0:9d17e4342598 | 5418 | /* If the leading bit on the INTEGER is a 1, add a leading zero */ |
wolfSSL | 0:9d17e4342598 | 5419 | int rLeadingZero = mp_leading_bit(r); |
wolfSSL | 0:9d17e4342598 | 5420 | int sLeadingZero = mp_leading_bit(s); |
wolfSSL | 0:9d17e4342598 | 5421 | int rLen = mp_unsigned_bin_size(r); /* big int size */ |
wolfSSL | 0:9d17e4342598 | 5422 | int sLen = mp_unsigned_bin_size(s); |
wolfSSL | 0:9d17e4342598 | 5423 | int err; |
wolfSSL | 0:9d17e4342598 | 5424 | |
wolfSSL | 0:9d17e4342598 | 5425 | if (*outLen < (rLen + rLeadingZero + sLen + sLeadingZero + |
wolfSSL | 0:9d17e4342598 | 5426 | headerSz + 2)) /* SEQ_TAG + LEN(ENUM) */ |
wolfSSL | 0:9d17e4342598 | 5427 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 5428 | |
wolfSSL | 0:9d17e4342598 | 5429 | idx = SetSequence(rLen+rLeadingZero+sLen+sLeadingZero+headerSz, out); |
wolfSSL | 0:9d17e4342598 | 5430 | |
wolfSSL | 0:9d17e4342598 | 5431 | /* store r */ |
wolfSSL | 0:9d17e4342598 | 5432 | out[idx++] = ASN_INTEGER; |
wolfSSL | 0:9d17e4342598 | 5433 | rSz = SetLength(rLen + rLeadingZero, &out[idx]); |
wolfSSL | 0:9d17e4342598 | 5434 | idx += rSz; |
wolfSSL | 0:9d17e4342598 | 5435 | if (rLeadingZero) |
wolfSSL | 0:9d17e4342598 | 5436 | out[idx++] = 0; |
wolfSSL | 0:9d17e4342598 | 5437 | err = mp_to_unsigned_bin(r, &out[idx]); |
wolfSSL | 0:9d17e4342598 | 5438 | if (err != MP_OKAY) return err; |
wolfSSL | 0:9d17e4342598 | 5439 | idx += rLen; |
wolfSSL | 0:9d17e4342598 | 5440 | |
wolfSSL | 0:9d17e4342598 | 5441 | /* store s */ |
wolfSSL | 0:9d17e4342598 | 5442 | out[idx++] = ASN_INTEGER; |
wolfSSL | 0:9d17e4342598 | 5443 | sSz = SetLength(sLen + sLeadingZero, &out[idx]); |
wolfSSL | 0:9d17e4342598 | 5444 | idx += sSz; |
wolfSSL | 0:9d17e4342598 | 5445 | if (sLeadingZero) |
wolfSSL | 0:9d17e4342598 | 5446 | out[idx++] = 0; |
wolfSSL | 0:9d17e4342598 | 5447 | err = mp_to_unsigned_bin(s, &out[idx]); |
wolfSSL | 0:9d17e4342598 | 5448 | if (err != MP_OKAY) return err; |
wolfSSL | 0:9d17e4342598 | 5449 | idx += sLen; |
wolfSSL | 0:9d17e4342598 | 5450 | |
wolfSSL | 0:9d17e4342598 | 5451 | *outLen = idx; |
wolfSSL | 0:9d17e4342598 | 5452 | |
wolfSSL | 0:9d17e4342598 | 5453 | return 0; |
wolfSSL | 0:9d17e4342598 | 5454 | } |
wolfSSL | 0:9d17e4342598 | 5455 | |
wolfSSL | 0:9d17e4342598 | 5456 | |
wolfSSL | 0:9d17e4342598 | 5457 | /* Der Decode ECC-DSA Signautre, r & s stored as big ints */ |
wolfSSL | 0:9d17e4342598 | 5458 | int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen, mp_int* r, mp_int* s) |
wolfSSL | 0:9d17e4342598 | 5459 | { |
wolfSSL | 0:9d17e4342598 | 5460 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 5461 | int len = 0; |
wolfSSL | 0:9d17e4342598 | 5462 | |
wolfSSL | 0:9d17e4342598 | 5463 | if (GetSequence(sig, &idx, &len, sigLen) < 0) |
wolfSSL | 0:9d17e4342598 | 5464 | return ASN_ECC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 5465 | |
wolfSSL | 0:9d17e4342598 | 5466 | if ((word32)len > (sigLen - idx)) |
wolfSSL | 0:9d17e4342598 | 5467 | return ASN_ECC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 5468 | |
wolfSSL | 0:9d17e4342598 | 5469 | if (GetInt(r, sig, &idx, sigLen) < 0) |
wolfSSL | 0:9d17e4342598 | 5470 | return ASN_ECC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 5471 | |
wolfSSL | 0:9d17e4342598 | 5472 | if (GetInt(s, sig, &idx, sigLen) < 0) |
wolfSSL | 0:9d17e4342598 | 5473 | return ASN_ECC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 5474 | |
wolfSSL | 0:9d17e4342598 | 5475 | return 0; |
wolfSSL | 0:9d17e4342598 | 5476 | } |
wolfSSL | 0:9d17e4342598 | 5477 | |
wolfSSL | 0:9d17e4342598 | 5478 | |
wolfSSL | 0:9d17e4342598 | 5479 | int EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key, |
wolfSSL | 0:9d17e4342598 | 5480 | word32 inSz) |
wolfSSL | 0:9d17e4342598 | 5481 | { |
wolfSSL | 0:9d17e4342598 | 5482 | word32 oid = 0; |
wolfSSL | 0:9d17e4342598 | 5483 | int version, length; |
wolfSSL | 0:9d17e4342598 | 5484 | int privSz, pubSz; |
wolfSSL | 0:9d17e4342598 | 5485 | byte b; |
wolfSSL | 0:9d17e4342598 | 5486 | byte priv[ECC_MAXSIZE]; |
wolfSSL | 0:9d17e4342598 | 5487 | byte pub[ECC_MAXSIZE * 2 + 1]; /* public key has two parts plus header */ |
wolfSSL | 0:9d17e4342598 | 5488 | |
wolfSSL | 0:9d17e4342598 | 5489 | if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0) |
wolfSSL | 0:9d17e4342598 | 5490 | return BAD_FUNC_ARG; |
wolfSSL | 0:9d17e4342598 | 5491 | |
wolfSSL | 0:9d17e4342598 | 5492 | if (GetSequence(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 5493 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5494 | |
wolfSSL | 0:9d17e4342598 | 5495 | if (GetMyVersion(input, inOutIdx, &version) < 0) |
wolfSSL | 0:9d17e4342598 | 5496 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5497 | |
wolfSSL | 0:9d17e4342598 | 5498 | b = input[*inOutIdx]; |
wolfSSL | 0:9d17e4342598 | 5499 | *inOutIdx += 1; |
wolfSSL | 0:9d17e4342598 | 5500 | |
wolfSSL | 0:9d17e4342598 | 5501 | /* priv type */ |
wolfSSL | 0:9d17e4342598 | 5502 | if (b != 4 && b != 6 && b != 7) |
wolfSSL | 0:9d17e4342598 | 5503 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5504 | |
wolfSSL | 0:9d17e4342598 | 5505 | if (GetLength(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 5506 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5507 | |
wolfSSL | 0:9d17e4342598 | 5508 | /* priv key */ |
wolfSSL | 0:9d17e4342598 | 5509 | privSz = length; |
wolfSSL | 0:9d17e4342598 | 5510 | XMEMCPY(priv, &input[*inOutIdx], privSz); |
wolfSSL | 0:9d17e4342598 | 5511 | *inOutIdx += length; |
wolfSSL | 0:9d17e4342598 | 5512 | |
wolfSSL | 0:9d17e4342598 | 5513 | /* prefix 0, may have */ |
wolfSSL | 0:9d17e4342598 | 5514 | b = input[*inOutIdx]; |
wolfSSL | 0:9d17e4342598 | 5515 | if (b == ECC_PREFIX_0) { |
wolfSSL | 0:9d17e4342598 | 5516 | *inOutIdx += 1; |
wolfSSL | 0:9d17e4342598 | 5517 | |
wolfSSL | 0:9d17e4342598 | 5518 | if (GetLength(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 5519 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5520 | |
wolfSSL | 0:9d17e4342598 | 5521 | /* object id */ |
wolfSSL | 0:9d17e4342598 | 5522 | b = input[*inOutIdx]; |
wolfSSL | 0:9d17e4342598 | 5523 | *inOutIdx += 1; |
wolfSSL | 0:9d17e4342598 | 5524 | |
wolfSSL | 0:9d17e4342598 | 5525 | if (b != ASN_OBJECT_ID) |
wolfSSL | 0:9d17e4342598 | 5526 | return ASN_OBJECT_ID_E; |
wolfSSL | 0:9d17e4342598 | 5527 | |
wolfSSL | 0:9d17e4342598 | 5528 | if (GetLength(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 5529 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5530 | |
wolfSSL | 0:9d17e4342598 | 5531 | while(length--) { |
wolfSSL | 0:9d17e4342598 | 5532 | oid += input[*inOutIdx]; |
wolfSSL | 0:9d17e4342598 | 5533 | *inOutIdx += 1; |
wolfSSL | 0:9d17e4342598 | 5534 | } |
wolfSSL | 0:9d17e4342598 | 5535 | if (CheckCurve(oid) < 0) |
wolfSSL | 0:9d17e4342598 | 5536 | return ECC_CURVE_OID_E; |
wolfSSL | 0:9d17e4342598 | 5537 | } |
wolfSSL | 0:9d17e4342598 | 5538 | |
wolfSSL | 0:9d17e4342598 | 5539 | /* prefix 1 */ |
wolfSSL | 0:9d17e4342598 | 5540 | b = input[*inOutIdx]; |
wolfSSL | 0:9d17e4342598 | 5541 | *inOutIdx += 1; |
wolfSSL | 0:9d17e4342598 | 5542 | if (b != ECC_PREFIX_1) |
wolfSSL | 0:9d17e4342598 | 5543 | return ASN_ECC_KEY_E; |
wolfSSL | 0:9d17e4342598 | 5544 | |
wolfSSL | 0:9d17e4342598 | 5545 | if (GetLength(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 5546 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5547 | |
wolfSSL | 0:9d17e4342598 | 5548 | /* key header */ |
wolfSSL | 0:9d17e4342598 | 5549 | b = input[*inOutIdx]; |
wolfSSL | 0:9d17e4342598 | 5550 | *inOutIdx += 1; |
wolfSSL | 0:9d17e4342598 | 5551 | if (b != ASN_BIT_STRING) |
wolfSSL | 0:9d17e4342598 | 5552 | return ASN_BITSTR_E; |
wolfSSL | 0:9d17e4342598 | 5553 | |
wolfSSL | 0:9d17e4342598 | 5554 | if (GetLength(input, inOutIdx, &length, inSz) < 0) |
wolfSSL | 0:9d17e4342598 | 5555 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5556 | b = input[*inOutIdx]; |
wolfSSL | 0:9d17e4342598 | 5557 | *inOutIdx += 1; |
wolfSSL | 0:9d17e4342598 | 5558 | if (b != 0x00) |
wolfSSL | 0:9d17e4342598 | 5559 | return ASN_EXPECT_0_E; |
wolfSSL | 0:9d17e4342598 | 5560 | |
wolfSSL | 0:9d17e4342598 | 5561 | pubSz = length - 1; /* null prefix */ |
wolfSSL | 0:9d17e4342598 | 5562 | XMEMCPY(pub, &input[*inOutIdx], pubSz); |
wolfSSL | 0:9d17e4342598 | 5563 | |
wolfSSL | 0:9d17e4342598 | 5564 | *inOutIdx += length; |
wolfSSL | 0:9d17e4342598 | 5565 | |
wolfSSL | 0:9d17e4342598 | 5566 | return ecc_import_private_key(priv, privSz, pub, pubSz, key); |
wolfSSL | 0:9d17e4342598 | 5567 | } |
wolfSSL | 0:9d17e4342598 | 5568 | |
wolfSSL | 0:9d17e4342598 | 5569 | #endif /* HAVE_ECC */ |
wolfSSL | 0:9d17e4342598 | 5570 | |
wolfSSL | 0:9d17e4342598 | 5571 | |
wolfSSL | 0:9d17e4342598 | 5572 | #if defined(HAVE_OCSP) || defined(HAVE_CRL) |
wolfSSL | 0:9d17e4342598 | 5573 | |
wolfSSL | 0:9d17e4342598 | 5574 | /* Get raw Date only, no processing, 0 on success */ |
wolfSSL | 0:9d17e4342598 | 5575 | static int GetBasicDate(const byte* source, word32* idx, byte* date, |
wolfSSL | 0:9d17e4342598 | 5576 | byte* format, int maxIdx) |
wolfSSL | 0:9d17e4342598 | 5577 | { |
wolfSSL | 0:9d17e4342598 | 5578 | int length; |
wolfSSL | 0:9d17e4342598 | 5579 | |
wolfSSL | 0:9d17e4342598 | 5580 | CYASSL_ENTER("GetBasicDate"); |
wolfSSL | 0:9d17e4342598 | 5581 | |
wolfSSL | 0:9d17e4342598 | 5582 | *format = source[*idx]; |
wolfSSL | 0:9d17e4342598 | 5583 | *idx += 1; |
wolfSSL | 0:9d17e4342598 | 5584 | if (*format != ASN_UTC_TIME && *format != ASN_GENERALIZED_TIME) |
wolfSSL | 0:9d17e4342598 | 5585 | return ASN_TIME_E; |
wolfSSL | 0:9d17e4342598 | 5586 | |
wolfSSL | 0:9d17e4342598 | 5587 | if (GetLength(source, idx, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 5588 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5589 | |
wolfSSL | 0:9d17e4342598 | 5590 | if (length > MAX_DATE_SIZE || length < MIN_DATE_SIZE) |
wolfSSL | 0:9d17e4342598 | 5591 | return ASN_DATE_SZ_E; |
wolfSSL | 0:9d17e4342598 | 5592 | |
wolfSSL | 0:9d17e4342598 | 5593 | XMEMCPY(date, &source[*idx], length); |
wolfSSL | 0:9d17e4342598 | 5594 | *idx += length; |
wolfSSL | 0:9d17e4342598 | 5595 | |
wolfSSL | 0:9d17e4342598 | 5596 | return 0; |
wolfSSL | 0:9d17e4342598 | 5597 | } |
wolfSSL | 0:9d17e4342598 | 5598 | |
wolfSSL | 0:9d17e4342598 | 5599 | #endif |
wolfSSL | 0:9d17e4342598 | 5600 | |
wolfSSL | 0:9d17e4342598 | 5601 | |
wolfSSL | 0:9d17e4342598 | 5602 | #ifdef HAVE_OCSP |
wolfSSL | 0:9d17e4342598 | 5603 | |
wolfSSL | 0:9d17e4342598 | 5604 | static int GetEnumerated(const byte* input, word32* inOutIdx, int *value) |
wolfSSL | 0:9d17e4342598 | 5605 | { |
wolfSSL | 0:9d17e4342598 | 5606 | word32 idx = *inOutIdx; |
wolfSSL | 0:9d17e4342598 | 5607 | word32 len; |
wolfSSL | 0:9d17e4342598 | 5608 | |
wolfSSL | 0:9d17e4342598 | 5609 | CYASSL_ENTER("GetEnumerated"); |
wolfSSL | 0:9d17e4342598 | 5610 | |
wolfSSL | 0:9d17e4342598 | 5611 | *value = 0; |
wolfSSL | 0:9d17e4342598 | 5612 | |
wolfSSL | 0:9d17e4342598 | 5613 | if (input[idx++] != ASN_ENUMERATED) |
wolfSSL | 0:9d17e4342598 | 5614 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5615 | |
wolfSSL | 0:9d17e4342598 | 5616 | len = input[idx++]; |
wolfSSL | 0:9d17e4342598 | 5617 | if (len > 4) |
wolfSSL | 0:9d17e4342598 | 5618 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5619 | |
wolfSSL | 0:9d17e4342598 | 5620 | while (len--) { |
wolfSSL | 0:9d17e4342598 | 5621 | *value = *value << 8 | input[idx++]; |
wolfSSL | 0:9d17e4342598 | 5622 | } |
wolfSSL | 0:9d17e4342598 | 5623 | |
wolfSSL | 0:9d17e4342598 | 5624 | *inOutIdx = idx; |
wolfSSL | 0:9d17e4342598 | 5625 | |
wolfSSL | 0:9d17e4342598 | 5626 | return *value; |
wolfSSL | 0:9d17e4342598 | 5627 | } |
wolfSSL | 0:9d17e4342598 | 5628 | |
wolfSSL | 0:9d17e4342598 | 5629 | |
wolfSSL | 0:9d17e4342598 | 5630 | static int DecodeSingleResponse(byte* source, |
wolfSSL | 0:9d17e4342598 | 5631 | word32* ioIndex, OcspResponse* resp, word32 size) |
wolfSSL | 0:9d17e4342598 | 5632 | { |
wolfSSL | 0:9d17e4342598 | 5633 | word32 idx = *ioIndex, prevIndex, oid; |
wolfSSL | 0:9d17e4342598 | 5634 | int length, wrapperSz; |
wolfSSL | 0:9d17e4342598 | 5635 | CertStatus* cs = resp->status; |
wolfSSL | 0:9d17e4342598 | 5636 | |
wolfSSL | 0:9d17e4342598 | 5637 | CYASSL_ENTER("DecodeSingleResponse"); |
wolfSSL | 0:9d17e4342598 | 5638 | |
wolfSSL | 0:9d17e4342598 | 5639 | /* Outer wrapper of the SEQUENCE OF Single Responses. */ |
wolfSSL | 0:9d17e4342598 | 5640 | if (GetSequence(source, &idx, &wrapperSz, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5641 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5642 | |
wolfSSL | 0:9d17e4342598 | 5643 | prevIndex = idx; |
wolfSSL | 0:9d17e4342598 | 5644 | |
wolfSSL | 0:9d17e4342598 | 5645 | /* When making a request, we only request one status on one certificate |
wolfSSL | 0:9d17e4342598 | 5646 | * at a time. There should only be one SingleResponse */ |
wolfSSL | 0:9d17e4342598 | 5647 | |
wolfSSL | 0:9d17e4342598 | 5648 | /* Wrapper around the Single Response */ |
wolfSSL | 0:9d17e4342598 | 5649 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5650 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5651 | |
wolfSSL | 0:9d17e4342598 | 5652 | /* Wrapper around the CertID */ |
wolfSSL | 0:9d17e4342598 | 5653 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5654 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5655 | /* Skip the hash algorithm */ |
wolfSSL | 0:9d17e4342598 | 5656 | if (GetAlgoId(source, &idx, &oid, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5657 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5658 | /* Save reference to the hash of CN */ |
wolfSSL | 0:9d17e4342598 | 5659 | if (source[idx++] != ASN_OCTET_STRING) |
wolfSSL | 0:9d17e4342598 | 5660 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5661 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5662 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5663 | resp->issuerHash = source + idx; |
wolfSSL | 0:9d17e4342598 | 5664 | idx += length; |
wolfSSL | 0:9d17e4342598 | 5665 | /* Save reference to the hash of the issuer public key */ |
wolfSSL | 0:9d17e4342598 | 5666 | if (source[idx++] != ASN_OCTET_STRING) |
wolfSSL | 0:9d17e4342598 | 5667 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5668 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5669 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5670 | resp->issuerKeyHash = source + idx; |
wolfSSL | 0:9d17e4342598 | 5671 | idx += length; |
wolfSSL | 0:9d17e4342598 | 5672 | |
wolfSSL | 0:9d17e4342598 | 5673 | /* Read the serial number, it is handled as a string, not as a |
wolfSSL | 0:9d17e4342598 | 5674 | * proper number. Just XMEMCPY the data over, rather than load it |
wolfSSL | 0:9d17e4342598 | 5675 | * as an mp_int. */ |
wolfSSL | 0:9d17e4342598 | 5676 | if (source[idx++] != ASN_INTEGER) |
wolfSSL | 0:9d17e4342598 | 5677 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5678 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5679 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5680 | if (length <= EXTERNAL_SERIAL_SIZE) |
wolfSSL | 0:9d17e4342598 | 5681 | { |
wolfSSL | 0:9d17e4342598 | 5682 | if (source[idx] == 0) |
wolfSSL | 0:9d17e4342598 | 5683 | { |
wolfSSL | 0:9d17e4342598 | 5684 | idx++; |
wolfSSL | 0:9d17e4342598 | 5685 | length--; |
wolfSSL | 0:9d17e4342598 | 5686 | } |
wolfSSL | 0:9d17e4342598 | 5687 | XMEMCPY(cs->serial, source + idx, length); |
wolfSSL | 0:9d17e4342598 | 5688 | cs->serialSz = length; |
wolfSSL | 0:9d17e4342598 | 5689 | } |
wolfSSL | 0:9d17e4342598 | 5690 | else |
wolfSSL | 0:9d17e4342598 | 5691 | { |
wolfSSL | 0:9d17e4342598 | 5692 | return ASN_GETINT_E; |
wolfSSL | 0:9d17e4342598 | 5693 | } |
wolfSSL | 0:9d17e4342598 | 5694 | idx += length; |
wolfSSL | 0:9d17e4342598 | 5695 | |
wolfSSL | 0:9d17e4342598 | 5696 | /* CertStatus */ |
wolfSSL | 0:9d17e4342598 | 5697 | switch (source[idx++]) |
wolfSSL | 0:9d17e4342598 | 5698 | { |
wolfSSL | 0:9d17e4342598 | 5699 | case (ASN_CONTEXT_SPECIFIC | CERT_GOOD): |
wolfSSL | 0:9d17e4342598 | 5700 | cs->status = CERT_GOOD; |
wolfSSL | 0:9d17e4342598 | 5701 | idx++; |
wolfSSL | 0:9d17e4342598 | 5702 | break; |
wolfSSL | 0:9d17e4342598 | 5703 | case (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | CERT_REVOKED): |
wolfSSL | 0:9d17e4342598 | 5704 | cs->status = CERT_REVOKED; |
wolfSSL | 0:9d17e4342598 | 5705 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5706 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5707 | idx += length; |
wolfSSL | 0:9d17e4342598 | 5708 | break; |
wolfSSL | 0:9d17e4342598 | 5709 | case (ASN_CONTEXT_SPECIFIC | CERT_UNKNOWN): |
wolfSSL | 0:9d17e4342598 | 5710 | cs->status = CERT_UNKNOWN; |
wolfSSL | 0:9d17e4342598 | 5711 | idx++; |
wolfSSL | 0:9d17e4342598 | 5712 | break; |
wolfSSL | 0:9d17e4342598 | 5713 | default: |
wolfSSL | 0:9d17e4342598 | 5714 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5715 | } |
wolfSSL | 0:9d17e4342598 | 5716 | |
wolfSSL | 0:9d17e4342598 | 5717 | if (GetBasicDate(source, &idx, cs->thisDate, |
wolfSSL | 0:9d17e4342598 | 5718 | &cs->thisDateFormat, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5719 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5720 | if (!XVALIDATE_DATE(cs->thisDate, cs->thisDateFormat, BEFORE)) |
wolfSSL | 0:9d17e4342598 | 5721 | return ASN_BEFORE_DATE_E; |
wolfSSL | 0:9d17e4342598 | 5722 | |
wolfSSL | 0:9d17e4342598 | 5723 | /* The following items are optional. Only check for them if there is more |
wolfSSL | 0:9d17e4342598 | 5724 | * unprocessed data in the singleResponse wrapper. */ |
wolfSSL | 0:9d17e4342598 | 5725 | |
wolfSSL | 0:9d17e4342598 | 5726 | if (((int)(idx - prevIndex) < wrapperSz) && |
wolfSSL | 0:9d17e4342598 | 5727 | (source[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0))) |
wolfSSL | 0:9d17e4342598 | 5728 | { |
wolfSSL | 0:9d17e4342598 | 5729 | idx++; |
wolfSSL | 0:9d17e4342598 | 5730 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5731 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5732 | if (GetBasicDate(source, &idx, cs->nextDate, |
wolfSSL | 0:9d17e4342598 | 5733 | &cs->nextDateFormat, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5734 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5735 | } |
wolfSSL | 0:9d17e4342598 | 5736 | if (((int)(idx - prevIndex) < wrapperSz) && |
wolfSSL | 0:9d17e4342598 | 5737 | (source[idx] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1))) |
wolfSSL | 0:9d17e4342598 | 5738 | { |
wolfSSL | 0:9d17e4342598 | 5739 | idx++; |
wolfSSL | 0:9d17e4342598 | 5740 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5741 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5742 | idx += length; |
wolfSSL | 0:9d17e4342598 | 5743 | } |
wolfSSL | 0:9d17e4342598 | 5744 | |
wolfSSL | 0:9d17e4342598 | 5745 | *ioIndex = idx; |
wolfSSL | 0:9d17e4342598 | 5746 | |
wolfSSL | 0:9d17e4342598 | 5747 | return 0; |
wolfSSL | 0:9d17e4342598 | 5748 | } |
wolfSSL | 0:9d17e4342598 | 5749 | |
wolfSSL | 0:9d17e4342598 | 5750 | static int DecodeOcspRespExtensions(byte* source, |
wolfSSL | 0:9d17e4342598 | 5751 | word32* ioIndex, OcspResponse* resp, word32 sz) |
wolfSSL | 0:9d17e4342598 | 5752 | { |
wolfSSL | 0:9d17e4342598 | 5753 | word32 idx = *ioIndex; |
wolfSSL | 0:9d17e4342598 | 5754 | int length; |
wolfSSL | 0:9d17e4342598 | 5755 | int ext_bound; /* boundary index for the sequence of extensions */ |
wolfSSL | 0:9d17e4342598 | 5756 | word32 oid; |
wolfSSL | 0:9d17e4342598 | 5757 | |
wolfSSL | 0:9d17e4342598 | 5758 | CYASSL_ENTER("DecodeOcspRespExtensions"); |
wolfSSL | 0:9d17e4342598 | 5759 | |
wolfSSL | 0:9d17e4342598 | 5760 | if (source[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1)) |
wolfSSL | 0:9d17e4342598 | 5761 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5762 | |
wolfSSL | 0:9d17e4342598 | 5763 | if (GetLength(source, &idx, &length, sz) < 0) return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5764 | |
wolfSSL | 0:9d17e4342598 | 5765 | if (GetSequence(source, &idx, &length, sz) < 0) return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5766 | |
wolfSSL | 0:9d17e4342598 | 5767 | ext_bound = idx + length; |
wolfSSL | 0:9d17e4342598 | 5768 | |
wolfSSL | 0:9d17e4342598 | 5769 | while (idx < (word32)ext_bound) { |
wolfSSL | 0:9d17e4342598 | 5770 | if (GetSequence(source, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 5771 | CYASSL_MSG("\tfail: should be a SEQUENCE"); |
wolfSSL | 0:9d17e4342598 | 5772 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5773 | } |
wolfSSL | 0:9d17e4342598 | 5774 | |
wolfSSL | 0:9d17e4342598 | 5775 | oid = 0; |
wolfSSL | 0:9d17e4342598 | 5776 | if (GetObjectId(source, &idx, &oid, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 5777 | CYASSL_MSG("\tfail: OBJECT ID"); |
wolfSSL | 0:9d17e4342598 | 5778 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5779 | } |
wolfSSL | 0:9d17e4342598 | 5780 | |
wolfSSL | 0:9d17e4342598 | 5781 | /* check for critical flag */ |
wolfSSL | 0:9d17e4342598 | 5782 | if (source[idx] == ASN_BOOLEAN) { |
wolfSSL | 0:9d17e4342598 | 5783 | CYASSL_MSG("\tfound optional critical flag, moving past"); |
wolfSSL | 0:9d17e4342598 | 5784 | idx += (ASN_BOOL_SIZE + 1); |
wolfSSL | 0:9d17e4342598 | 5785 | } |
wolfSSL | 0:9d17e4342598 | 5786 | |
wolfSSL | 0:9d17e4342598 | 5787 | /* process the extension based on the OID */ |
wolfSSL | 0:9d17e4342598 | 5788 | if (source[idx++] != ASN_OCTET_STRING) { |
wolfSSL | 0:9d17e4342598 | 5789 | CYASSL_MSG("\tfail: should be an OCTET STRING"); |
wolfSSL | 0:9d17e4342598 | 5790 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5791 | } |
wolfSSL | 0:9d17e4342598 | 5792 | |
wolfSSL | 0:9d17e4342598 | 5793 | if (GetLength(source, &idx, &length, sz) < 0) { |
wolfSSL | 0:9d17e4342598 | 5794 | CYASSL_MSG("\tfail: extension data length"); |
wolfSSL | 0:9d17e4342598 | 5795 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5796 | } |
wolfSSL | 0:9d17e4342598 | 5797 | |
wolfSSL | 0:9d17e4342598 | 5798 | if (oid == OCSP_NONCE_OID) { |
wolfSSL | 0:9d17e4342598 | 5799 | resp->nonce = source + idx; |
wolfSSL | 0:9d17e4342598 | 5800 | resp->nonceSz = length; |
wolfSSL | 0:9d17e4342598 | 5801 | } |
wolfSSL | 0:9d17e4342598 | 5802 | |
wolfSSL | 0:9d17e4342598 | 5803 | idx += length; |
wolfSSL | 0:9d17e4342598 | 5804 | } |
wolfSSL | 0:9d17e4342598 | 5805 | |
wolfSSL | 0:9d17e4342598 | 5806 | *ioIndex = idx; |
wolfSSL | 0:9d17e4342598 | 5807 | return 0; |
wolfSSL | 0:9d17e4342598 | 5808 | } |
wolfSSL | 0:9d17e4342598 | 5809 | |
wolfSSL | 0:9d17e4342598 | 5810 | |
wolfSSL | 0:9d17e4342598 | 5811 | static int DecodeResponseData(byte* source, |
wolfSSL | 0:9d17e4342598 | 5812 | word32* ioIndex, OcspResponse* resp, word32 size) |
wolfSSL | 0:9d17e4342598 | 5813 | { |
wolfSSL | 0:9d17e4342598 | 5814 | word32 idx = *ioIndex, prev_idx; |
wolfSSL | 0:9d17e4342598 | 5815 | int length; |
wolfSSL | 0:9d17e4342598 | 5816 | int version; |
wolfSSL | 0:9d17e4342598 | 5817 | word32 responderId = 0; |
wolfSSL | 0:9d17e4342598 | 5818 | |
wolfSSL | 0:9d17e4342598 | 5819 | CYASSL_ENTER("DecodeResponseData"); |
wolfSSL | 0:9d17e4342598 | 5820 | |
wolfSSL | 0:9d17e4342598 | 5821 | resp->response = source + idx; |
wolfSSL | 0:9d17e4342598 | 5822 | prev_idx = idx; |
wolfSSL | 0:9d17e4342598 | 5823 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5824 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5825 | resp->responseSz = length + idx - prev_idx; |
wolfSSL | 0:9d17e4342598 | 5826 | |
wolfSSL | 0:9d17e4342598 | 5827 | /* Get version. It is an EXPLICIT[0] DEFAULT(0) value. If this |
wolfSSL | 0:9d17e4342598 | 5828 | * item isn't an EXPLICIT[0], then set version to zero and move |
wolfSSL | 0:9d17e4342598 | 5829 | * onto the next item. |
wolfSSL | 0:9d17e4342598 | 5830 | */ |
wolfSSL | 0:9d17e4342598 | 5831 | if (source[idx] == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) |
wolfSSL | 0:9d17e4342598 | 5832 | { |
wolfSSL | 0:9d17e4342598 | 5833 | idx += 2; /* Eat the value and length */ |
wolfSSL | 0:9d17e4342598 | 5834 | if (GetMyVersion(source, &idx, &version) < 0) |
wolfSSL | 0:9d17e4342598 | 5835 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5836 | } else |
wolfSSL | 0:9d17e4342598 | 5837 | version = 0; |
wolfSSL | 0:9d17e4342598 | 5838 | |
wolfSSL | 0:9d17e4342598 | 5839 | responderId = source[idx++]; |
wolfSSL | 0:9d17e4342598 | 5840 | if ((responderId == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1)) || |
wolfSSL | 0:9d17e4342598 | 5841 | (responderId == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 2))) |
wolfSSL | 0:9d17e4342598 | 5842 | { |
wolfSSL | 0:9d17e4342598 | 5843 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5844 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5845 | idx += length; |
wolfSSL | 0:9d17e4342598 | 5846 | } |
wolfSSL | 0:9d17e4342598 | 5847 | else |
wolfSSL | 0:9d17e4342598 | 5848 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5849 | |
wolfSSL | 0:9d17e4342598 | 5850 | /* save pointer to the producedAt time */ |
wolfSSL | 0:9d17e4342598 | 5851 | if (GetBasicDate(source, &idx, resp->producedDate, |
wolfSSL | 0:9d17e4342598 | 5852 | &resp->producedDateFormat, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5853 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5854 | |
wolfSSL | 0:9d17e4342598 | 5855 | if (DecodeSingleResponse(source, &idx, resp, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5856 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5857 | |
wolfSSL | 0:9d17e4342598 | 5858 | if (DecodeOcspRespExtensions(source, &idx, resp, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5859 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5860 | |
wolfSSL | 0:9d17e4342598 | 5861 | *ioIndex = idx; |
wolfSSL | 0:9d17e4342598 | 5862 | return 0; |
wolfSSL | 0:9d17e4342598 | 5863 | } |
wolfSSL | 0:9d17e4342598 | 5864 | |
wolfSSL | 0:9d17e4342598 | 5865 | |
wolfSSL | 0:9d17e4342598 | 5866 | static int DecodeCerts(byte* source, |
wolfSSL | 0:9d17e4342598 | 5867 | word32* ioIndex, OcspResponse* resp, word32 size) |
wolfSSL | 0:9d17e4342598 | 5868 | { |
wolfSSL | 0:9d17e4342598 | 5869 | word32 idx = *ioIndex; |
wolfSSL | 0:9d17e4342598 | 5870 | |
wolfSSL | 0:9d17e4342598 | 5871 | CYASSL_ENTER("DecodeCerts"); |
wolfSSL | 0:9d17e4342598 | 5872 | |
wolfSSL | 0:9d17e4342598 | 5873 | if (source[idx++] == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) |
wolfSSL | 0:9d17e4342598 | 5874 | { |
wolfSSL | 0:9d17e4342598 | 5875 | int length; |
wolfSSL | 0:9d17e4342598 | 5876 | |
wolfSSL | 0:9d17e4342598 | 5877 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5878 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5879 | |
wolfSSL | 0:9d17e4342598 | 5880 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5881 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5882 | |
wolfSSL | 0:9d17e4342598 | 5883 | resp->cert = source + idx; |
wolfSSL | 0:9d17e4342598 | 5884 | resp->certSz = length; |
wolfSSL | 0:9d17e4342598 | 5885 | |
wolfSSL | 0:9d17e4342598 | 5886 | idx += length; |
wolfSSL | 0:9d17e4342598 | 5887 | } |
wolfSSL | 0:9d17e4342598 | 5888 | *ioIndex = idx; |
wolfSSL | 0:9d17e4342598 | 5889 | return 0; |
wolfSSL | 0:9d17e4342598 | 5890 | } |
wolfSSL | 0:9d17e4342598 | 5891 | |
wolfSSL | 0:9d17e4342598 | 5892 | static int DecodeBasicOcspResponse(byte* source, |
wolfSSL | 0:9d17e4342598 | 5893 | word32* ioIndex, OcspResponse* resp, word32 size) |
wolfSSL | 0:9d17e4342598 | 5894 | { |
wolfSSL | 0:9d17e4342598 | 5895 | int length; |
wolfSSL | 0:9d17e4342598 | 5896 | word32 idx = *ioIndex; |
wolfSSL | 0:9d17e4342598 | 5897 | word32 end_index; |
wolfSSL | 0:9d17e4342598 | 5898 | |
wolfSSL | 0:9d17e4342598 | 5899 | CYASSL_ENTER("DecodeBasicOcspResponse"); |
wolfSSL | 0:9d17e4342598 | 5900 | |
wolfSSL | 0:9d17e4342598 | 5901 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5902 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5903 | |
wolfSSL | 0:9d17e4342598 | 5904 | if (idx + length > size) |
wolfSSL | 0:9d17e4342598 | 5905 | return ASN_INPUT_E; |
wolfSSL | 0:9d17e4342598 | 5906 | end_index = idx + length; |
wolfSSL | 0:9d17e4342598 | 5907 | |
wolfSSL | 0:9d17e4342598 | 5908 | if (DecodeResponseData(source, &idx, resp, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5909 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5910 | |
wolfSSL | 0:9d17e4342598 | 5911 | /* Get the signature algorithm */ |
wolfSSL | 0:9d17e4342598 | 5912 | if (GetAlgoId(source, &idx, &resp->sigOID, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5913 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5914 | |
wolfSSL | 0:9d17e4342598 | 5915 | /* Obtain pointer to the start of the signature, and save the size */ |
wolfSSL | 0:9d17e4342598 | 5916 | if (source[idx++] == ASN_BIT_STRING) |
wolfSSL | 0:9d17e4342598 | 5917 | { |
wolfSSL | 0:9d17e4342598 | 5918 | int sigLength = 0; |
wolfSSL | 0:9d17e4342598 | 5919 | if (GetLength(source, &idx, &sigLength, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5920 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5921 | resp->sigSz = sigLength; |
wolfSSL | 0:9d17e4342598 | 5922 | resp->sig = source + idx; |
wolfSSL | 0:9d17e4342598 | 5923 | idx += sigLength; |
wolfSSL | 0:9d17e4342598 | 5924 | } |
wolfSSL | 0:9d17e4342598 | 5925 | |
wolfSSL | 0:9d17e4342598 | 5926 | /* |
wolfSSL | 0:9d17e4342598 | 5927 | * Check the length of the BasicOcspResponse against the current index to |
wolfSSL | 0:9d17e4342598 | 5928 | * see if there are certificates, they are optional. |
wolfSSL | 0:9d17e4342598 | 5929 | */ |
wolfSSL | 0:9d17e4342598 | 5930 | if (idx < end_index) |
wolfSSL | 0:9d17e4342598 | 5931 | { |
wolfSSL | 0:9d17e4342598 | 5932 | DecodedCert cert; |
wolfSSL | 0:9d17e4342598 | 5933 | int ret; |
wolfSSL | 0:9d17e4342598 | 5934 | |
wolfSSL | 0:9d17e4342598 | 5935 | if (DecodeCerts(source, &idx, resp, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5936 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5937 | |
wolfSSL | 0:9d17e4342598 | 5938 | InitDecodedCert(&cert, resp->cert, resp->certSz, 0); |
wolfSSL | 0:9d17e4342598 | 5939 | ret = ParseCertRelative(&cert, CA_TYPE, NO_VERIFY, 0); |
wolfSSL | 0:9d17e4342598 | 5940 | if (ret < 0) |
wolfSSL | 0:9d17e4342598 | 5941 | return ret; |
wolfSSL | 0:9d17e4342598 | 5942 | |
wolfSSL | 0:9d17e4342598 | 5943 | ret = ConfirmSignature(resp->response, resp->responseSz, |
wolfSSL | 0:9d17e4342598 | 5944 | cert.publicKey, cert.pubKeySize, cert.keyOID, |
wolfSSL | 0:9d17e4342598 | 5945 | resp->sig, resp->sigSz, resp->sigOID, NULL); |
wolfSSL | 0:9d17e4342598 | 5946 | FreeDecodedCert(&cert); |
wolfSSL | 0:9d17e4342598 | 5947 | |
wolfSSL | 0:9d17e4342598 | 5948 | if (ret == 0) |
wolfSSL | 0:9d17e4342598 | 5949 | { |
wolfSSL | 0:9d17e4342598 | 5950 | CYASSL_MSG("\tOCSP Confirm signature failed"); |
wolfSSL | 0:9d17e4342598 | 5951 | return ASN_OCSP_CONFIRM_E; |
wolfSSL | 0:9d17e4342598 | 5952 | } |
wolfSSL | 0:9d17e4342598 | 5953 | } |
wolfSSL | 0:9d17e4342598 | 5954 | |
wolfSSL | 0:9d17e4342598 | 5955 | *ioIndex = idx; |
wolfSSL | 0:9d17e4342598 | 5956 | return 0; |
wolfSSL | 0:9d17e4342598 | 5957 | } |
wolfSSL | 0:9d17e4342598 | 5958 | |
wolfSSL | 0:9d17e4342598 | 5959 | |
wolfSSL | 0:9d17e4342598 | 5960 | void InitOcspResponse(OcspResponse* resp, CertStatus* status, |
wolfSSL | 0:9d17e4342598 | 5961 | byte* source, word32 inSz) |
wolfSSL | 0:9d17e4342598 | 5962 | { |
wolfSSL | 0:9d17e4342598 | 5963 | CYASSL_ENTER("InitOcspResponse"); |
wolfSSL | 0:9d17e4342598 | 5964 | |
wolfSSL | 0:9d17e4342598 | 5965 | resp->responseStatus = -1; |
wolfSSL | 0:9d17e4342598 | 5966 | resp->response = NULL; |
wolfSSL | 0:9d17e4342598 | 5967 | resp->responseSz = 0; |
wolfSSL | 0:9d17e4342598 | 5968 | resp->producedDateFormat = 0; |
wolfSSL | 0:9d17e4342598 | 5969 | resp->issuerHash = NULL; |
wolfSSL | 0:9d17e4342598 | 5970 | resp->issuerKeyHash = NULL; |
wolfSSL | 0:9d17e4342598 | 5971 | resp->sig = NULL; |
wolfSSL | 0:9d17e4342598 | 5972 | resp->sigSz = 0; |
wolfSSL | 0:9d17e4342598 | 5973 | resp->sigOID = 0; |
wolfSSL | 0:9d17e4342598 | 5974 | resp->status = status; |
wolfSSL | 0:9d17e4342598 | 5975 | resp->nonce = NULL; |
wolfSSL | 0:9d17e4342598 | 5976 | resp->nonceSz = 0; |
wolfSSL | 0:9d17e4342598 | 5977 | resp->source = source; |
wolfSSL | 0:9d17e4342598 | 5978 | resp->maxIdx = inSz; |
wolfSSL | 0:9d17e4342598 | 5979 | } |
wolfSSL | 0:9d17e4342598 | 5980 | |
wolfSSL | 0:9d17e4342598 | 5981 | |
wolfSSL | 0:9d17e4342598 | 5982 | int OcspResponseDecode(OcspResponse* resp) |
wolfSSL | 0:9d17e4342598 | 5983 | { |
wolfSSL | 0:9d17e4342598 | 5984 | int length = 0; |
wolfSSL | 0:9d17e4342598 | 5985 | word32 idx = 0; |
wolfSSL | 0:9d17e4342598 | 5986 | byte* source = resp->source; |
wolfSSL | 0:9d17e4342598 | 5987 | word32 size = resp->maxIdx; |
wolfSSL | 0:9d17e4342598 | 5988 | word32 oid; |
wolfSSL | 0:9d17e4342598 | 5989 | |
wolfSSL | 0:9d17e4342598 | 5990 | CYASSL_ENTER("OcspResponseDecode"); |
wolfSSL | 0:9d17e4342598 | 5991 | |
wolfSSL | 0:9d17e4342598 | 5992 | /* peel the outer SEQUENCE wrapper */ |
wolfSSL | 0:9d17e4342598 | 5993 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 5994 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5995 | |
wolfSSL | 0:9d17e4342598 | 5996 | /* First get the responseStatus, an ENUMERATED */ |
wolfSSL | 0:9d17e4342598 | 5997 | if (GetEnumerated(source, &idx, &resp->responseStatus) < 0) |
wolfSSL | 0:9d17e4342598 | 5998 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 5999 | |
wolfSSL | 0:9d17e4342598 | 6000 | if (resp->responseStatus != OCSP_SUCCESSFUL) |
wolfSSL | 0:9d17e4342598 | 6001 | return 0; |
wolfSSL | 0:9d17e4342598 | 6002 | |
wolfSSL | 0:9d17e4342598 | 6003 | /* Next is an EXPLICIT record called ResponseBytes, OPTIONAL */ |
wolfSSL | 0:9d17e4342598 | 6004 | if (idx >= size) |
wolfSSL | 0:9d17e4342598 | 6005 | return ASN_INPUT_E; |
wolfSSL | 0:9d17e4342598 | 6006 | if (source[idx++] != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC)) |
wolfSSL | 0:9d17e4342598 | 6007 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6008 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 6009 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6010 | |
wolfSSL | 0:9d17e4342598 | 6011 | /* Get the responseBytes SEQUENCE */ |
wolfSSL | 0:9d17e4342598 | 6012 | if (GetSequence(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 6013 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6014 | |
wolfSSL | 0:9d17e4342598 | 6015 | /* Check ObjectID for the resposeBytes */ |
wolfSSL | 0:9d17e4342598 | 6016 | if (GetObjectId(source, &idx, &oid, size) < 0) |
wolfSSL | 0:9d17e4342598 | 6017 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6018 | if (oid != OCSP_BASIC_OID) |
wolfSSL | 0:9d17e4342598 | 6019 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6020 | if (source[idx++] != ASN_OCTET_STRING) |
wolfSSL | 0:9d17e4342598 | 6021 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6022 | |
wolfSSL | 0:9d17e4342598 | 6023 | if (GetLength(source, &idx, &length, size) < 0) |
wolfSSL | 0:9d17e4342598 | 6024 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6025 | |
wolfSSL | 0:9d17e4342598 | 6026 | if (DecodeBasicOcspResponse(source, &idx, resp, size) < 0) |
wolfSSL | 0:9d17e4342598 | 6027 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6028 | |
wolfSSL | 0:9d17e4342598 | 6029 | return 0; |
wolfSSL | 0:9d17e4342598 | 6030 | } |
wolfSSL | 0:9d17e4342598 | 6031 | |
wolfSSL | 0:9d17e4342598 | 6032 | |
wolfSSL | 0:9d17e4342598 | 6033 | static word32 SetOcspReqExtensions(word32 extSz, byte* output, |
wolfSSL | 0:9d17e4342598 | 6034 | const byte* nonce, word32 nonceSz) |
wolfSSL | 0:9d17e4342598 | 6035 | { |
wolfSSL | 0:9d17e4342598 | 6036 | static const byte NonceObjId[] = { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, |
wolfSSL | 0:9d17e4342598 | 6037 | 0x30, 0x01, 0x02 }; |
wolfSSL | 0:9d17e4342598 | 6038 | byte seqArray[5][MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 6039 | word32 seqSz[5], totalSz; |
wolfSSL | 0:9d17e4342598 | 6040 | |
wolfSSL | 0:9d17e4342598 | 6041 | CYASSL_ENTER("SetOcspReqExtensions"); |
wolfSSL | 0:9d17e4342598 | 6042 | |
wolfSSL | 0:9d17e4342598 | 6043 | if (nonce == NULL || nonceSz == 0) return 0; |
wolfSSL | 0:9d17e4342598 | 6044 | |
wolfSSL | 0:9d17e4342598 | 6045 | seqArray[0][0] = ASN_OCTET_STRING; |
wolfSSL | 0:9d17e4342598 | 6046 | seqSz[0] = 1 + SetLength(nonceSz, &seqArray[0][1]); |
wolfSSL | 0:9d17e4342598 | 6047 | |
wolfSSL | 0:9d17e4342598 | 6048 | seqArray[1][0] = ASN_OBJECT_ID; |
wolfSSL | 0:9d17e4342598 | 6049 | seqSz[1] = 1 + SetLength(sizeof(NonceObjId), &seqArray[1][1]); |
wolfSSL | 0:9d17e4342598 | 6050 | |
wolfSSL | 0:9d17e4342598 | 6051 | totalSz = seqSz[0] + seqSz[1] + nonceSz + (word32)sizeof(NonceObjId); |
wolfSSL | 0:9d17e4342598 | 6052 | |
wolfSSL | 0:9d17e4342598 | 6053 | seqSz[2] = SetSequence(totalSz, seqArray[2]); |
wolfSSL | 0:9d17e4342598 | 6054 | totalSz += seqSz[2]; |
wolfSSL | 0:9d17e4342598 | 6055 | |
wolfSSL | 0:9d17e4342598 | 6056 | seqSz[3] = SetSequence(totalSz, seqArray[3]); |
wolfSSL | 0:9d17e4342598 | 6057 | totalSz += seqSz[3]; |
wolfSSL | 0:9d17e4342598 | 6058 | |
wolfSSL | 0:9d17e4342598 | 6059 | seqArray[4][0] = (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 2); |
wolfSSL | 0:9d17e4342598 | 6060 | seqSz[4] = 1 + SetLength(totalSz, &seqArray[4][1]); |
wolfSSL | 0:9d17e4342598 | 6061 | totalSz += seqSz[4]; |
wolfSSL | 0:9d17e4342598 | 6062 | |
wolfSSL | 0:9d17e4342598 | 6063 | if (totalSz < extSz) |
wolfSSL | 0:9d17e4342598 | 6064 | { |
wolfSSL | 0:9d17e4342598 | 6065 | totalSz = 0; |
wolfSSL | 0:9d17e4342598 | 6066 | XMEMCPY(output + totalSz, seqArray[4], seqSz[4]); |
wolfSSL | 0:9d17e4342598 | 6067 | totalSz += seqSz[4]; |
wolfSSL | 0:9d17e4342598 | 6068 | XMEMCPY(output + totalSz, seqArray[3], seqSz[3]); |
wolfSSL | 0:9d17e4342598 | 6069 | totalSz += seqSz[3]; |
wolfSSL | 0:9d17e4342598 | 6070 | XMEMCPY(output + totalSz, seqArray[2], seqSz[2]); |
wolfSSL | 0:9d17e4342598 | 6071 | totalSz += seqSz[2]; |
wolfSSL | 0:9d17e4342598 | 6072 | XMEMCPY(output + totalSz, seqArray[1], seqSz[1]); |
wolfSSL | 0:9d17e4342598 | 6073 | totalSz += seqSz[1]; |
wolfSSL | 0:9d17e4342598 | 6074 | XMEMCPY(output + totalSz, NonceObjId, sizeof(NonceObjId)); |
wolfSSL | 0:9d17e4342598 | 6075 | totalSz += (word32)sizeof(NonceObjId); |
wolfSSL | 0:9d17e4342598 | 6076 | XMEMCPY(output + totalSz, seqArray[0], seqSz[0]); |
wolfSSL | 0:9d17e4342598 | 6077 | totalSz += seqSz[0]; |
wolfSSL | 0:9d17e4342598 | 6078 | XMEMCPY(output + totalSz, nonce, nonceSz); |
wolfSSL | 0:9d17e4342598 | 6079 | totalSz += nonceSz; |
wolfSSL | 0:9d17e4342598 | 6080 | } |
wolfSSL | 0:9d17e4342598 | 6081 | |
wolfSSL | 0:9d17e4342598 | 6082 | return totalSz; |
wolfSSL | 0:9d17e4342598 | 6083 | } |
wolfSSL | 0:9d17e4342598 | 6084 | |
wolfSSL | 0:9d17e4342598 | 6085 | |
wolfSSL | 0:9d17e4342598 | 6086 | int EncodeOcspRequest(OcspRequest* req) |
wolfSSL | 0:9d17e4342598 | 6087 | { |
wolfSSL | 0:9d17e4342598 | 6088 | byte seqArray[5][MAX_SEQ_SZ]; |
wolfSSL | 0:9d17e4342598 | 6089 | /* The ASN.1 of the OCSP Request is an onion of sequences */ |
wolfSSL | 0:9d17e4342598 | 6090 | byte algoArray[MAX_ALGO_SZ]; |
wolfSSL | 0:9d17e4342598 | 6091 | byte issuerArray[MAX_ENCODED_DIG_SZ]; |
wolfSSL | 0:9d17e4342598 | 6092 | byte issuerKeyArray[MAX_ENCODED_DIG_SZ]; |
wolfSSL | 0:9d17e4342598 | 6093 | byte snArray[MAX_SN_SZ]; |
wolfSSL | 0:9d17e4342598 | 6094 | byte extArray[MAX_OCSP_EXT_SZ]; |
wolfSSL | 0:9d17e4342598 | 6095 | byte* output = req->dest; |
wolfSSL | 0:9d17e4342598 | 6096 | word32 seqSz[5], algoSz, issuerSz, issuerKeySz, snSz, extSz, totalSz; |
wolfSSL | 0:9d17e4342598 | 6097 | int i; |
wolfSSL | 0:9d17e4342598 | 6098 | |
wolfSSL | 0:9d17e4342598 | 6099 | CYASSL_ENTER("EncodeOcspRequest"); |
wolfSSL | 0:9d17e4342598 | 6100 | |
wolfSSL | 0:9d17e4342598 | 6101 | algoSz = SetAlgoID(SHAh, algoArray, hashType, 0); |
wolfSSL | 0:9d17e4342598 | 6102 | |
wolfSSL | 0:9d17e4342598 | 6103 | req->issuerHash = req->cert->issuerHash; |
wolfSSL | 0:9d17e4342598 | 6104 | issuerSz = SetDigest(req->cert->issuerHash, SHA_SIZE, issuerArray); |
wolfSSL | 0:9d17e4342598 | 6105 | |
wolfSSL | 0:9d17e4342598 | 6106 | req->issuerKeyHash = req->cert->issuerKeyHash; |
wolfSSL | 0:9d17e4342598 | 6107 | issuerKeySz = SetDigest(req->cert->issuerKeyHash, SHA_SIZE, issuerKeyArray); |
wolfSSL | 0:9d17e4342598 | 6108 | |
wolfSSL | 0:9d17e4342598 | 6109 | req->serial = req->cert->serial; |
wolfSSL | 0:9d17e4342598 | 6110 | req->serialSz = req->cert->serialSz; |
wolfSSL | 0:9d17e4342598 | 6111 | snSz = SetSerialNumber(req->cert->serial, req->cert->serialSz, snArray); |
wolfSSL | 0:9d17e4342598 | 6112 | |
wolfSSL | 0:9d17e4342598 | 6113 | extSz = 0; |
wolfSSL | 0:9d17e4342598 | 6114 | if (req->useNonce) { |
wolfSSL | 0:9d17e4342598 | 6115 | RNG rng; |
wolfSSL | 0:9d17e4342598 | 6116 | if (InitRng(&rng) != 0) { |
wolfSSL | 0:9d17e4342598 | 6117 | CYASSL_MSG("\tCannot initialize RNG. Skipping the OSCP Nonce."); |
wolfSSL | 0:9d17e4342598 | 6118 | } else { |
wolfSSL | 0:9d17e4342598 | 6119 | req->nonceSz = MAX_OCSP_NONCE_SZ; |
wolfSSL | 0:9d17e4342598 | 6120 | RNG_GenerateBlock(&rng, req->nonce, req->nonceSz); |
wolfSSL | 0:9d17e4342598 | 6121 | extSz = SetOcspReqExtensions(MAX_OCSP_EXT_SZ, extArray, |
wolfSSL | 0:9d17e4342598 | 6122 | req->nonce, req->nonceSz); |
wolfSSL | 0:9d17e4342598 | 6123 | } |
wolfSSL | 0:9d17e4342598 | 6124 | } |
wolfSSL | 0:9d17e4342598 | 6125 | |
wolfSSL | 0:9d17e4342598 | 6126 | totalSz = algoSz + issuerSz + issuerKeySz + snSz; |
wolfSSL | 0:9d17e4342598 | 6127 | |
wolfSSL | 0:9d17e4342598 | 6128 | for (i = 4; i >= 0; i--) { |
wolfSSL | 0:9d17e4342598 | 6129 | seqSz[i] = SetSequence(totalSz, seqArray[i]); |
wolfSSL | 0:9d17e4342598 | 6130 | totalSz += seqSz[i]; |
wolfSSL | 0:9d17e4342598 | 6131 | if (i == 2) totalSz += extSz; |
wolfSSL | 0:9d17e4342598 | 6132 | } |
wolfSSL | 0:9d17e4342598 | 6133 | totalSz = 0; |
wolfSSL | 0:9d17e4342598 | 6134 | for (i = 0; i < 5; i++) { |
wolfSSL | 0:9d17e4342598 | 6135 | XMEMCPY(output + totalSz, seqArray[i], seqSz[i]); |
wolfSSL | 0:9d17e4342598 | 6136 | totalSz += seqSz[i]; |
wolfSSL | 0:9d17e4342598 | 6137 | } |
wolfSSL | 0:9d17e4342598 | 6138 | XMEMCPY(output + totalSz, algoArray, algoSz); |
wolfSSL | 0:9d17e4342598 | 6139 | totalSz += algoSz; |
wolfSSL | 0:9d17e4342598 | 6140 | XMEMCPY(output + totalSz, issuerArray, issuerSz); |
wolfSSL | 0:9d17e4342598 | 6141 | totalSz += issuerSz; |
wolfSSL | 0:9d17e4342598 | 6142 | XMEMCPY(output + totalSz, issuerKeyArray, issuerKeySz); |
wolfSSL | 0:9d17e4342598 | 6143 | totalSz += issuerKeySz; |
wolfSSL | 0:9d17e4342598 | 6144 | XMEMCPY(output + totalSz, snArray, snSz); |
wolfSSL | 0:9d17e4342598 | 6145 | totalSz += snSz; |
wolfSSL | 0:9d17e4342598 | 6146 | if (extSz != 0) { |
wolfSSL | 0:9d17e4342598 | 6147 | XMEMCPY(output + totalSz, extArray, extSz); |
wolfSSL | 0:9d17e4342598 | 6148 | totalSz += extSz; |
wolfSSL | 0:9d17e4342598 | 6149 | } |
wolfSSL | 0:9d17e4342598 | 6150 | |
wolfSSL | 0:9d17e4342598 | 6151 | return totalSz; |
wolfSSL | 0:9d17e4342598 | 6152 | } |
wolfSSL | 0:9d17e4342598 | 6153 | |
wolfSSL | 0:9d17e4342598 | 6154 | |
wolfSSL | 0:9d17e4342598 | 6155 | void InitOcspRequest(OcspRequest* req, DecodedCert* cert, byte useNonce, |
wolfSSL | 0:9d17e4342598 | 6156 | byte* dest, word32 destSz) |
wolfSSL | 0:9d17e4342598 | 6157 | { |
wolfSSL | 0:9d17e4342598 | 6158 | CYASSL_ENTER("InitOcspRequest"); |
wolfSSL | 0:9d17e4342598 | 6159 | |
wolfSSL | 0:9d17e4342598 | 6160 | req->cert = cert; |
wolfSSL | 0:9d17e4342598 | 6161 | req->useNonce = useNonce; |
wolfSSL | 0:9d17e4342598 | 6162 | req->nonceSz = 0; |
wolfSSL | 0:9d17e4342598 | 6163 | req->issuerHash = NULL; |
wolfSSL | 0:9d17e4342598 | 6164 | req->issuerKeyHash = NULL; |
wolfSSL | 0:9d17e4342598 | 6165 | req->serial = NULL; |
wolfSSL | 0:9d17e4342598 | 6166 | req->dest = dest; |
wolfSSL | 0:9d17e4342598 | 6167 | req->destSz = destSz; |
wolfSSL | 0:9d17e4342598 | 6168 | } |
wolfSSL | 0:9d17e4342598 | 6169 | |
wolfSSL | 0:9d17e4342598 | 6170 | |
wolfSSL | 0:9d17e4342598 | 6171 | int CompareOcspReqResp(OcspRequest* req, OcspResponse* resp) |
wolfSSL | 0:9d17e4342598 | 6172 | { |
wolfSSL | 0:9d17e4342598 | 6173 | int cmp; |
wolfSSL | 0:9d17e4342598 | 6174 | |
wolfSSL | 0:9d17e4342598 | 6175 | CYASSL_ENTER("CompareOcspReqResp"); |
wolfSSL | 0:9d17e4342598 | 6176 | |
wolfSSL | 0:9d17e4342598 | 6177 | if (req == NULL) |
wolfSSL | 0:9d17e4342598 | 6178 | { |
wolfSSL | 0:9d17e4342598 | 6179 | CYASSL_MSG("\tReq missing"); |
wolfSSL | 0:9d17e4342598 | 6180 | return -1; |
wolfSSL | 0:9d17e4342598 | 6181 | } |
wolfSSL | 0:9d17e4342598 | 6182 | |
wolfSSL | 0:9d17e4342598 | 6183 | if (resp == NULL) |
wolfSSL | 0:9d17e4342598 | 6184 | { |
wolfSSL | 0:9d17e4342598 | 6185 | CYASSL_MSG("\tResp missing"); |
wolfSSL | 0:9d17e4342598 | 6186 | return 1; |
wolfSSL | 0:9d17e4342598 | 6187 | } |
wolfSSL | 0:9d17e4342598 | 6188 | |
wolfSSL | 0:9d17e4342598 | 6189 | /* Nonces are not critical. The responder may not necessarily add |
wolfSSL | 0:9d17e4342598 | 6190 | * the nonce to the response. */ |
wolfSSL | 0:9d17e4342598 | 6191 | if (req->useNonce && resp->nonceSz != 0) { |
wolfSSL | 0:9d17e4342598 | 6192 | cmp = req->nonceSz - resp->nonceSz; |
wolfSSL | 0:9d17e4342598 | 6193 | if (cmp != 0) |
wolfSSL | 0:9d17e4342598 | 6194 | { |
wolfSSL | 0:9d17e4342598 | 6195 | CYASSL_MSG("\tnonceSz mismatch"); |
wolfSSL | 0:9d17e4342598 | 6196 | return cmp; |
wolfSSL | 0:9d17e4342598 | 6197 | } |
wolfSSL | 0:9d17e4342598 | 6198 | |
wolfSSL | 0:9d17e4342598 | 6199 | cmp = XMEMCMP(req->nonce, resp->nonce, req->nonceSz); |
wolfSSL | 0:9d17e4342598 | 6200 | if (cmp != 0) |
wolfSSL | 0:9d17e4342598 | 6201 | { |
wolfSSL | 0:9d17e4342598 | 6202 | CYASSL_MSG("\tnonce mismatch"); |
wolfSSL | 0:9d17e4342598 | 6203 | return cmp; |
wolfSSL | 0:9d17e4342598 | 6204 | } |
wolfSSL | 0:9d17e4342598 | 6205 | } |
wolfSSL | 0:9d17e4342598 | 6206 | |
wolfSSL | 0:9d17e4342598 | 6207 | cmp = XMEMCMP(req->issuerHash, resp->issuerHash, SHA_DIGEST_SIZE); |
wolfSSL | 0:9d17e4342598 | 6208 | if (cmp != 0) |
wolfSSL | 0:9d17e4342598 | 6209 | { |
wolfSSL | 0:9d17e4342598 | 6210 | CYASSL_MSG("\tissuerHash mismatch"); |
wolfSSL | 0:9d17e4342598 | 6211 | return cmp; |
wolfSSL | 0:9d17e4342598 | 6212 | } |
wolfSSL | 0:9d17e4342598 | 6213 | |
wolfSSL | 0:9d17e4342598 | 6214 | cmp = XMEMCMP(req->issuerKeyHash, resp->issuerKeyHash, SHA_DIGEST_SIZE); |
wolfSSL | 0:9d17e4342598 | 6215 | if (cmp != 0) |
wolfSSL | 0:9d17e4342598 | 6216 | { |
wolfSSL | 0:9d17e4342598 | 6217 | CYASSL_MSG("\tissuerKeyHash mismatch"); |
wolfSSL | 0:9d17e4342598 | 6218 | return cmp; |
wolfSSL | 0:9d17e4342598 | 6219 | } |
wolfSSL | 0:9d17e4342598 | 6220 | |
wolfSSL | 0:9d17e4342598 | 6221 | cmp = req->serialSz - resp->status->serialSz; |
wolfSSL | 0:9d17e4342598 | 6222 | if (cmp != 0) |
wolfSSL | 0:9d17e4342598 | 6223 | { |
wolfSSL | 0:9d17e4342598 | 6224 | CYASSL_MSG("\tserialSz mismatch"); |
wolfSSL | 0:9d17e4342598 | 6225 | return cmp; |
wolfSSL | 0:9d17e4342598 | 6226 | } |
wolfSSL | 0:9d17e4342598 | 6227 | |
wolfSSL | 0:9d17e4342598 | 6228 | cmp = XMEMCMP(req->serial, resp->status->serial, req->serialSz); |
wolfSSL | 0:9d17e4342598 | 6229 | if (cmp != 0) |
wolfSSL | 0:9d17e4342598 | 6230 | { |
wolfSSL | 0:9d17e4342598 | 6231 | CYASSL_MSG("\tserial mismatch"); |
wolfSSL | 0:9d17e4342598 | 6232 | return cmp; |
wolfSSL | 0:9d17e4342598 | 6233 | } |
wolfSSL | 0:9d17e4342598 | 6234 | |
wolfSSL | 0:9d17e4342598 | 6235 | return 0; |
wolfSSL | 0:9d17e4342598 | 6236 | } |
wolfSSL | 0:9d17e4342598 | 6237 | |
wolfSSL | 0:9d17e4342598 | 6238 | #endif |
wolfSSL | 0:9d17e4342598 | 6239 | |
wolfSSL | 0:9d17e4342598 | 6240 | |
wolfSSL | 0:9d17e4342598 | 6241 | /* store SHA1 hash of NAME */ |
wolfSSL | 0:9d17e4342598 | 6242 | CYASSL_LOCAL int GetNameHash(const byte* source, word32* idx, byte* hash, |
wolfSSL | 0:9d17e4342598 | 6243 | int maxIdx) |
wolfSSL | 0:9d17e4342598 | 6244 | { |
wolfSSL | 0:9d17e4342598 | 6245 | Sha sha; |
wolfSSL | 0:9d17e4342598 | 6246 | int length; /* length of all distinguished names */ |
wolfSSL | 0:9d17e4342598 | 6247 | int ret = 0; |
wolfSSL | 0:9d17e4342598 | 6248 | word32 dummy; |
wolfSSL | 0:9d17e4342598 | 6249 | |
wolfSSL | 0:9d17e4342598 | 6250 | CYASSL_ENTER("GetNameHash"); |
wolfSSL | 0:9d17e4342598 | 6251 | |
wolfSSL | 0:9d17e4342598 | 6252 | if (source[*idx] == ASN_OBJECT_ID) { |
wolfSSL | 0:9d17e4342598 | 6253 | CYASSL_MSG("Trying optional prefix..."); |
wolfSSL | 0:9d17e4342598 | 6254 | |
wolfSSL | 0:9d17e4342598 | 6255 | if (GetLength(source, idx, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 6256 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6257 | |
wolfSSL | 0:9d17e4342598 | 6258 | *idx += length; |
wolfSSL | 0:9d17e4342598 | 6259 | CYASSL_MSG("Got optional prefix"); |
wolfSSL | 0:9d17e4342598 | 6260 | } |
wolfSSL | 0:9d17e4342598 | 6261 | |
wolfSSL | 0:9d17e4342598 | 6262 | /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be |
wolfSSL | 0:9d17e4342598 | 6263 | * calculated over the entire DER encoding of the Name field, including |
wolfSSL | 0:9d17e4342598 | 6264 | * the tag and length. */ |
wolfSSL | 0:9d17e4342598 | 6265 | dummy = *idx; |
wolfSSL | 0:9d17e4342598 | 6266 | if (GetSequence(source, idx, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 6267 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6268 | |
wolfSSL | 0:9d17e4342598 | 6269 | ret = InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 6270 | if (ret != 0) |
wolfSSL | 0:9d17e4342598 | 6271 | return ret; |
wolfSSL | 0:9d17e4342598 | 6272 | ShaUpdate(&sha, source + dummy, length + *idx - dummy); |
wolfSSL | 0:9d17e4342598 | 6273 | ShaFinal(&sha, hash); |
wolfSSL | 0:9d17e4342598 | 6274 | |
wolfSSL | 0:9d17e4342598 | 6275 | *idx += length; |
wolfSSL | 0:9d17e4342598 | 6276 | |
wolfSSL | 0:9d17e4342598 | 6277 | return 0; |
wolfSSL | 0:9d17e4342598 | 6278 | } |
wolfSSL | 0:9d17e4342598 | 6279 | |
wolfSSL | 0:9d17e4342598 | 6280 | |
wolfSSL | 0:9d17e4342598 | 6281 | #ifdef HAVE_CRL |
wolfSSL | 0:9d17e4342598 | 6282 | |
wolfSSL | 0:9d17e4342598 | 6283 | /* initialize decoded CRL */ |
wolfSSL | 0:9d17e4342598 | 6284 | void InitDecodedCRL(DecodedCRL* dcrl) |
wolfSSL | 0:9d17e4342598 | 6285 | { |
wolfSSL | 0:9d17e4342598 | 6286 | CYASSL_MSG("InitDecodedCRL"); |
wolfSSL | 0:9d17e4342598 | 6287 | |
wolfSSL | 0:9d17e4342598 | 6288 | dcrl->certBegin = 0; |
wolfSSL | 0:9d17e4342598 | 6289 | dcrl->sigIndex = 0; |
wolfSSL | 0:9d17e4342598 | 6290 | dcrl->sigLength = 0; |
wolfSSL | 0:9d17e4342598 | 6291 | dcrl->signatureOID = 0; |
wolfSSL | 0:9d17e4342598 | 6292 | dcrl->certs = NULL; |
wolfSSL | 0:9d17e4342598 | 6293 | dcrl->totalCerts = 0; |
wolfSSL | 0:9d17e4342598 | 6294 | } |
wolfSSL | 0:9d17e4342598 | 6295 | |
wolfSSL | 0:9d17e4342598 | 6296 | |
wolfSSL | 0:9d17e4342598 | 6297 | /* free decoded CRL resources */ |
wolfSSL | 0:9d17e4342598 | 6298 | void FreeDecodedCRL(DecodedCRL* dcrl) |
wolfSSL | 0:9d17e4342598 | 6299 | { |
wolfSSL | 0:9d17e4342598 | 6300 | RevokedCert* tmp = dcrl->certs; |
wolfSSL | 0:9d17e4342598 | 6301 | |
wolfSSL | 0:9d17e4342598 | 6302 | CYASSL_MSG("FreeDecodedCRL"); |
wolfSSL | 0:9d17e4342598 | 6303 | |
wolfSSL | 0:9d17e4342598 | 6304 | while(tmp) { |
wolfSSL | 0:9d17e4342598 | 6305 | RevokedCert* next = tmp->next; |
wolfSSL | 0:9d17e4342598 | 6306 | XFREE(tmp, NULL, DYNAMIC_TYPE_REVOKED); |
wolfSSL | 0:9d17e4342598 | 6307 | tmp = next; |
wolfSSL | 0:9d17e4342598 | 6308 | } |
wolfSSL | 0:9d17e4342598 | 6309 | } |
wolfSSL | 0:9d17e4342598 | 6310 | |
wolfSSL | 0:9d17e4342598 | 6311 | |
wolfSSL | 0:9d17e4342598 | 6312 | /* Get Revoked Cert list, 0 on success */ |
wolfSSL | 0:9d17e4342598 | 6313 | static int GetRevoked(const byte* buff, word32* idx, DecodedCRL* dcrl, |
wolfSSL | 0:9d17e4342598 | 6314 | int maxIdx) |
wolfSSL | 0:9d17e4342598 | 6315 | { |
wolfSSL | 0:9d17e4342598 | 6316 | int len; |
wolfSSL | 0:9d17e4342598 | 6317 | word32 end; |
wolfSSL | 0:9d17e4342598 | 6318 | byte b; |
wolfSSL | 0:9d17e4342598 | 6319 | RevokedCert* rc; |
wolfSSL | 0:9d17e4342598 | 6320 | |
wolfSSL | 0:9d17e4342598 | 6321 | CYASSL_ENTER("GetRevoked"); |
wolfSSL | 0:9d17e4342598 | 6322 | |
wolfSSL | 0:9d17e4342598 | 6323 | if (GetSequence(buff, idx, &len, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 6324 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6325 | |
wolfSSL | 0:9d17e4342598 | 6326 | end = *idx + len; |
wolfSSL | 0:9d17e4342598 | 6327 | |
wolfSSL | 0:9d17e4342598 | 6328 | /* get serial number */ |
wolfSSL | 0:9d17e4342598 | 6329 | b = buff[*idx]; |
wolfSSL | 0:9d17e4342598 | 6330 | *idx += 1; |
wolfSSL | 0:9d17e4342598 | 6331 | |
wolfSSL | 0:9d17e4342598 | 6332 | if (b != ASN_INTEGER) { |
wolfSSL | 0:9d17e4342598 | 6333 | CYASSL_MSG("Expecting Integer"); |
wolfSSL | 0:9d17e4342598 | 6334 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6335 | } |
wolfSSL | 0:9d17e4342598 | 6336 | |
wolfSSL | 0:9d17e4342598 | 6337 | if (GetLength(buff, idx, &len, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 6338 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6339 | |
wolfSSL | 0:9d17e4342598 | 6340 | if (len > EXTERNAL_SERIAL_SIZE) { |
wolfSSL | 0:9d17e4342598 | 6341 | CYASSL_MSG("Serial Size too big"); |
wolfSSL | 0:9d17e4342598 | 6342 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6343 | } |
wolfSSL | 0:9d17e4342598 | 6344 | |
wolfSSL | 0:9d17e4342598 | 6345 | rc = (RevokedCert*)XMALLOC(sizeof(RevokedCert), NULL, DYNAMIC_TYPE_CRL); |
wolfSSL | 0:9d17e4342598 | 6346 | if (rc == NULL) { |
wolfSSL | 0:9d17e4342598 | 6347 | CYASSL_MSG("Alloc Revoked Cert failed"); |
wolfSSL | 0:9d17e4342598 | 6348 | return MEMORY_E; |
wolfSSL | 0:9d17e4342598 | 6349 | } |
wolfSSL | 0:9d17e4342598 | 6350 | |
wolfSSL | 0:9d17e4342598 | 6351 | XMEMCPY(rc->serialNumber, &buff[*idx], len); |
wolfSSL | 0:9d17e4342598 | 6352 | rc->serialSz = len; |
wolfSSL | 0:9d17e4342598 | 6353 | |
wolfSSL | 0:9d17e4342598 | 6354 | /* add to list */ |
wolfSSL | 0:9d17e4342598 | 6355 | rc->next = dcrl->certs; |
wolfSSL | 0:9d17e4342598 | 6356 | dcrl->certs = rc; |
wolfSSL | 0:9d17e4342598 | 6357 | dcrl->totalCerts++; |
wolfSSL | 0:9d17e4342598 | 6358 | |
wolfSSL | 0:9d17e4342598 | 6359 | *idx += len; |
wolfSSL | 0:9d17e4342598 | 6360 | |
wolfSSL | 0:9d17e4342598 | 6361 | /* get date */ |
wolfSSL | 0:9d17e4342598 | 6362 | b = buff[*idx]; |
wolfSSL | 0:9d17e4342598 | 6363 | *idx += 1; |
wolfSSL | 0:9d17e4342598 | 6364 | |
wolfSSL | 0:9d17e4342598 | 6365 | if (b != ASN_UTC_TIME && b != ASN_GENERALIZED_TIME) { |
wolfSSL | 0:9d17e4342598 | 6366 | CYASSL_MSG("Expecting Date"); |
wolfSSL | 0:9d17e4342598 | 6367 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6368 | } |
wolfSSL | 0:9d17e4342598 | 6369 | |
wolfSSL | 0:9d17e4342598 | 6370 | if (GetLength(buff, idx, &len, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 6371 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6372 | |
wolfSSL | 0:9d17e4342598 | 6373 | /* skip for now */ |
wolfSSL | 0:9d17e4342598 | 6374 | *idx += len; |
wolfSSL | 0:9d17e4342598 | 6375 | |
wolfSSL | 0:9d17e4342598 | 6376 | if (*idx != end) /* skip extensions */ |
wolfSSL | 0:9d17e4342598 | 6377 | *idx = end; |
wolfSSL | 0:9d17e4342598 | 6378 | |
wolfSSL | 0:9d17e4342598 | 6379 | return 0; |
wolfSSL | 0:9d17e4342598 | 6380 | } |
wolfSSL | 0:9d17e4342598 | 6381 | |
wolfSSL | 0:9d17e4342598 | 6382 | |
wolfSSL | 0:9d17e4342598 | 6383 | /* Get CRL Signature, 0 on success */ |
wolfSSL | 0:9d17e4342598 | 6384 | static int GetCRL_Signature(const byte* source, word32* idx, DecodedCRL* dcrl, |
wolfSSL | 0:9d17e4342598 | 6385 | int maxIdx) |
wolfSSL | 0:9d17e4342598 | 6386 | { |
wolfSSL | 0:9d17e4342598 | 6387 | int length; |
wolfSSL | 0:9d17e4342598 | 6388 | byte b; |
wolfSSL | 0:9d17e4342598 | 6389 | |
wolfSSL | 0:9d17e4342598 | 6390 | CYASSL_ENTER("GetCRL_Signature"); |
wolfSSL | 0:9d17e4342598 | 6391 | |
wolfSSL | 0:9d17e4342598 | 6392 | b = source[*idx]; |
wolfSSL | 0:9d17e4342598 | 6393 | *idx += 1; |
wolfSSL | 0:9d17e4342598 | 6394 | if (b != ASN_BIT_STRING) |
wolfSSL | 0:9d17e4342598 | 6395 | return ASN_BITSTR_E; |
wolfSSL | 0:9d17e4342598 | 6396 | |
wolfSSL | 0:9d17e4342598 | 6397 | if (GetLength(source, idx, &length, maxIdx) < 0) |
wolfSSL | 0:9d17e4342598 | 6398 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6399 | |
wolfSSL | 0:9d17e4342598 | 6400 | dcrl->sigLength = length; |
wolfSSL | 0:9d17e4342598 | 6401 | |
wolfSSL | 0:9d17e4342598 | 6402 | b = source[*idx]; |
wolfSSL | 0:9d17e4342598 | 6403 | *idx += 1; |
wolfSSL | 0:9d17e4342598 | 6404 | if (b != 0x00) |
wolfSSL | 0:9d17e4342598 | 6405 | return ASN_EXPECT_0_E; |
wolfSSL | 0:9d17e4342598 | 6406 | |
wolfSSL | 0:9d17e4342598 | 6407 | dcrl->sigLength--; |
wolfSSL | 0:9d17e4342598 | 6408 | dcrl->signature = (byte*)&source[*idx]; |
wolfSSL | 0:9d17e4342598 | 6409 | |
wolfSSL | 0:9d17e4342598 | 6410 | *idx += dcrl->sigLength; |
wolfSSL | 0:9d17e4342598 | 6411 | |
wolfSSL | 0:9d17e4342598 | 6412 | return 0; |
wolfSSL | 0:9d17e4342598 | 6413 | } |
wolfSSL | 0:9d17e4342598 | 6414 | |
wolfSSL | 0:9d17e4342598 | 6415 | |
wolfSSL | 0:9d17e4342598 | 6416 | /* prase crl buffer into decoded state, 0 on success */ |
wolfSSL | 0:9d17e4342598 | 6417 | int ParseCRL(DecodedCRL* dcrl, const byte* buff, word32 sz, void* cm) |
wolfSSL | 0:9d17e4342598 | 6418 | { |
wolfSSL | 0:9d17e4342598 | 6419 | int version, len; |
wolfSSL | 0:9d17e4342598 | 6420 | word32 oid, idx = 0; |
wolfSSL | 0:9d17e4342598 | 6421 | Signer* ca = NULL; |
wolfSSL | 0:9d17e4342598 | 6422 | |
wolfSSL | 0:9d17e4342598 | 6423 | CYASSL_MSG("ParseCRL"); |
wolfSSL | 0:9d17e4342598 | 6424 | |
wolfSSL | 0:9d17e4342598 | 6425 | /* raw crl hash */ |
wolfSSL | 0:9d17e4342598 | 6426 | /* hash here if needed for optimized comparisons |
wolfSSL | 0:9d17e4342598 | 6427 | * Sha sha; |
wolfSSL | 0:9d17e4342598 | 6428 | * InitSha(&sha); |
wolfSSL | 0:9d17e4342598 | 6429 | * ShaUpdate(&sha, buff, sz); |
wolfSSL | 0:9d17e4342598 | 6430 | * ShaFinal(&sha, dcrl->crlHash); */ |
wolfSSL | 0:9d17e4342598 | 6431 | |
wolfSSL | 0:9d17e4342598 | 6432 | if (GetSequence(buff, &idx, &len, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6433 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6434 | |
wolfSSL | 0:9d17e4342598 | 6435 | dcrl->certBegin = idx; |
wolfSSL | 0:9d17e4342598 | 6436 | |
wolfSSL | 0:9d17e4342598 | 6437 | if (GetSequence(buff, &idx, &len, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6438 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6439 | dcrl->sigIndex = len + idx; |
wolfSSL | 0:9d17e4342598 | 6440 | |
wolfSSL | 0:9d17e4342598 | 6441 | /* may have version */ |
wolfSSL | 0:9d17e4342598 | 6442 | if (buff[idx] == ASN_INTEGER) { |
wolfSSL | 0:9d17e4342598 | 6443 | if (GetMyVersion(buff, &idx, &version) < 0) |
wolfSSL | 0:9d17e4342598 | 6444 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6445 | } |
wolfSSL | 0:9d17e4342598 | 6446 | |
wolfSSL | 0:9d17e4342598 | 6447 | if (GetAlgoId(buff, &idx, &oid, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6448 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6449 | |
wolfSSL | 0:9d17e4342598 | 6450 | if (GetNameHash(buff, &idx, dcrl->issuerHash, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6451 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6452 | |
wolfSSL | 0:9d17e4342598 | 6453 | if (GetBasicDate(buff, &idx, dcrl->lastDate, &dcrl->lastDateFormat, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6454 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6455 | |
wolfSSL | 0:9d17e4342598 | 6456 | if (GetBasicDate(buff, &idx, dcrl->nextDate, &dcrl->nextDateFormat, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6457 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6458 | |
wolfSSL | 0:9d17e4342598 | 6459 | if (!XVALIDATE_DATE(dcrl->nextDate, dcrl->nextDateFormat, AFTER)) { |
wolfSSL | 0:9d17e4342598 | 6460 | CYASSL_MSG("CRL after date is no longer valid"); |
wolfSSL | 0:9d17e4342598 | 6461 | return ASN_AFTER_DATE_E; |
wolfSSL | 0:9d17e4342598 | 6462 | } |
wolfSSL | 0:9d17e4342598 | 6463 | |
wolfSSL | 0:9d17e4342598 | 6464 | if (idx != dcrl->sigIndex && buff[idx] != CRL_EXTENSIONS) { |
wolfSSL | 0:9d17e4342598 | 6465 | if (GetSequence(buff, &idx, &len, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6466 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6467 | |
wolfSSL | 0:9d17e4342598 | 6468 | len += idx; |
wolfSSL | 0:9d17e4342598 | 6469 | |
wolfSSL | 0:9d17e4342598 | 6470 | while (idx < (word32)len) { |
wolfSSL | 0:9d17e4342598 | 6471 | if (GetRevoked(buff, &idx, dcrl, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6472 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6473 | } |
wolfSSL | 0:9d17e4342598 | 6474 | } |
wolfSSL | 0:9d17e4342598 | 6475 | |
wolfSSL | 0:9d17e4342598 | 6476 | if (idx != dcrl->sigIndex) |
wolfSSL | 0:9d17e4342598 | 6477 | idx = dcrl->sigIndex; /* skip extensions */ |
wolfSSL | 0:9d17e4342598 | 6478 | |
wolfSSL | 0:9d17e4342598 | 6479 | if (GetAlgoId(buff, &idx, &dcrl->signatureOID, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6480 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6481 | |
wolfSSL | 0:9d17e4342598 | 6482 | if (GetCRL_Signature(buff, &idx, dcrl, sz) < 0) |
wolfSSL | 0:9d17e4342598 | 6483 | return ASN_PARSE_E; |
wolfSSL | 0:9d17e4342598 | 6484 | |
wolfSSL | 0:9d17e4342598 | 6485 | /* openssl doesn't add skid by default for CRLs cause firefox chokes |
wolfSSL | 0:9d17e4342598 | 6486 | we're not assuming it's available yet */ |
wolfSSL | 0:9d17e4342598 | 6487 | #if !defined(NO_SKID) && defined(CRL_SKID_READY) |
wolfSSL | 0:9d17e4342598 | 6488 | if (dcrl->extAuthKeyIdSet) |
wolfSSL | 0:9d17e4342598 | 6489 | ca = GetCA(cm, dcrl->extAuthKeyId); |
wolfSSL | 0:9d17e4342598 | 6490 | if (ca == NULL) |
wolfSSL | 0:9d17e4342598 | 6491 | ca = GetCAByName(cm, dcrl->issuerHash); |
wolfSSL | 0:9d17e4342598 | 6492 | #else /* NO_SKID */ |
wolfSSL | 0:9d17e4342598 | 6493 | ca = GetCA(cm, dcrl->issuerHash); |
wolfSSL | 0:9d17e4342598 | 6494 | #endif /* NO_SKID */ |
wolfSSL | 0:9d17e4342598 | 6495 | CYASSL_MSG("About to verify CRL signature"); |
wolfSSL | 0:9d17e4342598 | 6496 | |
wolfSSL | 0:9d17e4342598 | 6497 | if (ca) { |
wolfSSL | 0:9d17e4342598 | 6498 | CYASSL_MSG("Found CRL issuer CA"); |
wolfSSL | 0:9d17e4342598 | 6499 | /* try to confirm/verify signature */ |
wolfSSL | 0:9d17e4342598 | 6500 | if (!ConfirmSignature(buff + dcrl->certBegin, |
wolfSSL | 0:9d17e4342598 | 6501 | dcrl->sigIndex - dcrl->certBegin, |
wolfSSL | 0:9d17e4342598 | 6502 | ca->publicKey, ca->pubKeySize, ca->keyOID, |
wolfSSL | 0:9d17e4342598 | 6503 | dcrl->signature, dcrl->sigLength, dcrl->signatureOID, NULL)) { |
wolfSSL | 0:9d17e4342598 | 6504 | CYASSL_MSG("CRL Confirm signature failed"); |
wolfSSL | 0:9d17e4342598 | 6505 | return ASN_CRL_CONFIRM_E; |
wolfSSL | 0:9d17e4342598 | 6506 | } |
wolfSSL | 0:9d17e4342598 | 6507 | } |
wolfSSL | 0:9d17e4342598 | 6508 | else { |
wolfSSL | 0:9d17e4342598 | 6509 | CYASSL_MSG("Did NOT find CRL issuer CA"); |
wolfSSL | 0:9d17e4342598 | 6510 | return ASN_CRL_NO_SIGNER_E; |
wolfSSL | 0:9d17e4342598 | 6511 | } |
wolfSSL | 0:9d17e4342598 | 6512 | |
wolfSSL | 0:9d17e4342598 | 6513 | return 0; |
wolfSSL | 0:9d17e4342598 | 6514 | } |
wolfSSL | 0:9d17e4342598 | 6515 | |
wolfSSL | 0:9d17e4342598 | 6516 | #endif /* HAVE_CRL */ |
wolfSSL | 0:9d17e4342598 | 6517 | #endif |
wolfSSL | 0:9d17e4342598 | 6518 | |
wolfSSL | 0:9d17e4342598 | 6519 | #ifdef CYASSL_SEP |
wolfSSL | 0:9d17e4342598 | 6520 | |
wolfSSL | 0:9d17e4342598 | 6521 | |
wolfSSL | 0:9d17e4342598 | 6522 | |
wolfSSL | 0:9d17e4342598 | 6523 | #endif /* CYASSL_SEP */ |
wolfSSL | 0:9d17e4342598 | 6524 |