/src/openssl30/crypto/asn1/a_time.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | /*- | 
| 11 |  |  * This is an implementation of the ASN1 Time structure which is: | 
| 12 |  |  *    Time ::= CHOICE { | 
| 13 |  |  *      utcTime        UTCTime, | 
| 14 |  |  *      generalTime    GeneralizedTime } | 
| 15 |  |  */ | 
| 16 |  |  | 
| 17 |  | #include <stdio.h> | 
| 18 |  | #include <time.h> | 
| 19 |  | #include "crypto/asn1.h" | 
| 20 |  | #include "crypto/ctype.h" | 
| 21 |  | #include "internal/cryptlib.h" | 
| 22 |  | #include <openssl/asn1t.h> | 
| 23 |  | #include "asn1_local.h" | 
| 24 |  |  | 
| 25 |  | IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME) | 
| 26 |  |  | 
| 27 |  | IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME) | 
| 28 |  | IMPLEMENT_ASN1_DUP_FUNCTION(ASN1_TIME) | 
| 29 |  |  | 
| 30 |  | static int is_utc(const int year) | 
| 31 | 332 | { | 
| 32 | 332 |     if (50 <= year && year <= 149) | 
| 33 | 332 |         return 1; | 
| 34 | 0 |     return 0; | 
| 35 | 332 | } | 
| 36 |  |  | 
| 37 |  | static int leap_year(const int year) | 
| 38 | 98.3k | { | 
| 39 | 98.3k |     if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) | 
| 40 | 17.1k |         return 1; | 
| 41 | 81.2k |     return 0; | 
| 42 | 98.3k | } | 
| 43 |  |  | 
| 44 |  | /* | 
| 45 |  |  * Compute the day of the week and the day of the year from the year, month | 
| 46 |  |  * and day.  The day of the year is straightforward, the day of the week uses | 
| 47 |  |  * a form of Zeller's congruence.  For this months start with March and are | 
| 48 |  |  * numbered 4 through 15. | 
| 49 |  |  */ | 
| 50 |  | static void determine_days(struct tm *tm) | 
| 51 | 145k | { | 
| 52 | 145k |     static const int ydays[12] = { | 
| 53 | 145k |         0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 | 
| 54 | 145k |     }; | 
| 55 | 145k |     int y = tm->tm_year + 1900; | 
| 56 | 145k |     int m = tm->tm_mon; | 
| 57 | 145k |     int d = tm->tm_mday; | 
| 58 | 145k |     int c; | 
| 59 |  |  | 
| 60 | 145k |     tm->tm_yday = ydays[m] + d - 1; | 
| 61 | 145k |     if (m >= 2) { | 
| 62 |  |         /* March and onwards can be one day further into the year */ | 
| 63 | 79.3k |         tm->tm_yday += leap_year(y); | 
| 64 | 79.3k |         m += 2; | 
| 65 | 79.3k |     } else { | 
| 66 |  |         /* Treat January and February as part of the previous year */ | 
| 67 | 65.6k |         m += 14; | 
| 68 | 65.6k |         y--; | 
| 69 | 65.6k |     } | 
| 70 | 145k |     c = y / 100; | 
| 71 | 145k |     y %= 100; | 
| 72 |  |     /* Zeller's congruence */ | 
| 73 | 145k |     tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7; | 
| 74 | 145k | } | 
| 75 |  |  | 
| 76 |  | int ossl_asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d) | 
| 77 | 151k | { | 
| 78 | 151k |     static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 }; | 
| 79 | 151k |     static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 }; | 
| 80 | 151k |     static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; | 
| 81 | 151k |     char *a; | 
| 82 | 151k |     int n, i, i2, l, o, min_l = 11, strict = 0, end = 6, btz = 5, md; | 
| 83 | 151k |     struct tm tmp; | 
| 84 |  | #if defined(CHARSET_EBCDIC) | 
| 85 |  |     const char upper_z = 0x5A, num_zero = 0x30, period = 0x2E, minus = 0x2D, plus = 0x2B; | 
| 86 |  | #else | 
| 87 | 151k |     const char upper_z = 'Z', num_zero = '0', period = '.', minus = '-', plus = '+'; | 
| 88 | 151k | #endif | 
| 89 |  |     /* | 
| 90 |  |      * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280 | 
| 91 |  |      * time string format, in which: | 
| 92 |  |      * | 
| 93 |  |      * 1. "seconds" is a 'MUST' | 
| 94 |  |      * 2. "Zulu" timezone is a 'MUST' | 
| 95 |  |      * 3. "+|-" is not allowed to indicate a time zone | 
| 96 |  |      */ | 
| 97 | 151k |     if (d->type == V_ASN1_UTCTIME) { | 
| 98 | 45.9k |         if (d->flags & ASN1_STRING_FLAG_X509_TIME) { | 
| 99 | 0 |             min_l = 13; | 
| 100 | 0 |             strict = 1; | 
| 101 | 0 |         } | 
| 102 | 105k |     } else if (d->type == V_ASN1_GENERALIZEDTIME) { | 
| 103 | 105k |         end = 7; | 
| 104 | 105k |         btz = 6; | 
| 105 | 105k |         if (d->flags & ASN1_STRING_FLAG_X509_TIME) { | 
| 106 | 0 |             min_l = 15; | 
| 107 | 0 |             strict = 1; | 
| 108 | 105k |         } else { | 
| 109 | 105k |             min_l = 13; | 
| 110 | 105k |         } | 
| 111 | 105k |     } else { | 
| 112 | 0 |         return 0; | 
| 113 | 0 |     } | 
| 114 |  |  | 
| 115 | 151k |     l = d->length; | 
| 116 | 151k |     a = (char *)d->data; | 
| 117 | 151k |     o = 0; | 
| 118 | 151k |     memset(&tmp, 0, sizeof(tmp)); | 
| 119 |  |  | 
| 120 |  |     /* | 
| 121 |  |      * GENERALIZEDTIME is similar to UTCTIME except the year is represented | 
| 122 |  |      * as YYYY. This stuff treats everything as a two digit field so make | 
| 123 |  |      * first two fields 00 to 99 | 
| 124 |  |      */ | 
| 125 |  |  | 
| 126 | 151k |     if (l < min_l) | 
| 127 | 47.5k |         goto err; | 
| 128 | 675k |     for (i = 0; i < end; i++) { | 
| 129 | 623k |         if (!strict && (i == btz) && ((a[o] == upper_z) || (a[o] == plus) || (a[o] == minus))) { | 
| 130 | 15.1k |             i++; | 
| 131 | 15.1k |             break; | 
| 132 | 15.1k |         } | 
| 133 | 608k |         if (!ossl_ascii_isdigit(a[o])) | 
| 134 | 10.5k |             goto err; | 
| 135 | 597k |         n = a[o] - num_zero; | 
| 136 |  |         /* incomplete 2-digital number */ | 
| 137 | 597k |         if (++o == l) | 
| 138 | 2.34k |             goto err; | 
| 139 |  |  | 
| 140 | 595k |         if (!ossl_ascii_isdigit(a[o])) | 
| 141 | 7.36k |             goto err; | 
| 142 | 588k |         n = (n * 10) + a[o] - num_zero; | 
| 143 |  |         /* no more bytes to read, but we haven't seen time-zone yet */ | 
| 144 | 588k |         if (++o == l) | 
| 145 | 6.77k |             goto err; | 
| 146 |  |  | 
| 147 | 581k |         i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i; | 
| 148 |  |  | 
| 149 | 581k |         if ((n < min[i2]) || (n > max[i2])) | 
| 150 | 7.03k |             goto err; | 
| 151 | 574k |         switch (i2) { | 
| 152 | 76.0k |         case 0: | 
| 153 |  |             /* UTC will never be here */ | 
| 154 | 76.0k |             tmp.tm_year = n * 100 - 1900; | 
| 155 | 76.0k |             break; | 
| 156 | 98.1k |         case 1: | 
| 157 | 98.1k |             if (d->type == V_ASN1_UTCTIME) | 
| 158 | 23.4k |                 tmp.tm_year = n < 50 ? n + 100 : n; | 
| 159 | 74.7k |             else | 
| 160 | 74.7k |                 tmp.tm_year += n; | 
| 161 | 98.1k |             break; | 
| 162 | 93.2k |         case 2: | 
| 163 | 93.2k |             tmp.tm_mon = n - 1; | 
| 164 | 93.2k |             break; | 
| 165 | 90.7k |         case 3: | 
| 166 |  |             /* check if tm_mday is valid in tm_mon */ | 
| 167 | 90.7k |             if (tmp.tm_mon == 1) { | 
| 168 |  |                 /* it's February */ | 
| 169 | 11.3k |                 md = mdays[1] + leap_year(tmp.tm_year + 1900); | 
| 170 | 79.4k |             } else { | 
| 171 | 79.4k |                 md = mdays[tmp.tm_mon]; | 
| 172 | 79.4k |             } | 
| 173 | 90.7k |             if (n > md) | 
| 174 | 2.07k |                 goto err; | 
| 175 | 88.7k |             tmp.tm_mday = n; | 
| 176 | 88.7k |             determine_days(&tmp); | 
| 177 | 88.7k |             break; | 
| 178 | 83.0k |         case 4: | 
| 179 | 83.0k |             tmp.tm_hour = n; | 
| 180 | 83.0k |             break; | 
| 181 | 80.3k |         case 5: | 
| 182 | 80.3k |             tmp.tm_min = n; | 
| 183 | 80.3k |             break; | 
| 184 | 52.5k |         case 6: | 
| 185 | 52.5k |             tmp.tm_sec = n; | 
| 186 | 52.5k |             break; | 
| 187 | 574k |         } | 
| 188 | 574k |     } | 
| 189 |  |  | 
| 190 |  |     /* | 
| 191 |  |      * Optional fractional seconds: decimal point followed by one or more | 
| 192 |  |      * digits. | 
| 193 |  |      */ | 
| 194 | 67.7k |     if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == period) { | 
| 195 | 38.6k |         if (strict) | 
| 196 |  |             /* RFC 5280 forbids fractional seconds */ | 
| 197 | 0 |             goto err; | 
| 198 | 38.6k |         if (++o == l) | 
| 199 | 599 |             goto err; | 
| 200 | 38.0k |         i = o; | 
| 201 | 164k |         while ((o < l) && ossl_ascii_isdigit(a[o])) | 
| 202 | 126k |             o++; | 
| 203 |  |         /* Must have at least one digit after decimal point */ | 
| 204 | 38.0k |         if (i == o) | 
| 205 | 1.18k |             goto err; | 
| 206 |  |         /* no more bytes to read, but we haven't seen time-zone yet */ | 
| 207 | 36.8k |         if (o == l) | 
| 208 | 436 |             goto err; | 
| 209 | 36.8k |     } | 
| 210 |  |  | 
| 211 |  |     /* | 
| 212 |  |      * 'o' will never point to '\0' at this point, the only chance | 
| 213 |  |      * 'o' can point to '\0' is either the subsequent if or the first | 
| 214 |  |      * else if is true. | 
| 215 |  |      */ | 
| 216 | 65.4k |     if (a[o] == upper_z) { | 
| 217 | 50.1k |         o++; | 
| 218 | 50.1k |     } else if (!strict && ((a[o] == plus) || (a[o] == minus))) { | 
| 219 | 13.0k |         int offsign = a[o] == minus ? 1 : -1; | 
| 220 | 13.0k |         int offset = 0; | 
| 221 |  |  | 
| 222 | 13.0k |         o++; | 
| 223 |  |         /* | 
| 224 |  |          * if not equal, no need to do subsequent checks | 
| 225 |  |          * since the following for-loop will add 'o' by 4 | 
| 226 |  |          * and the final return statement will check if 'l' | 
| 227 |  |          * and 'o' are equal. | 
| 228 |  |          */ | 
| 229 | 13.0k |         if (o + 4 != l) | 
| 230 | 2.58k |             goto err; | 
| 231 | 26.7k |         for (i = end; i < end + 2; i++) { | 
| 232 | 18.9k |             if (!ossl_ascii_isdigit(a[o])) | 
| 233 | 882 |                 goto err; | 
| 234 | 18.0k |             n = a[o] - num_zero; | 
| 235 | 18.0k |             o++; | 
| 236 | 18.0k |             if (!ossl_ascii_isdigit(a[o])) | 
| 237 | 927 |                 goto err; | 
| 238 | 17.1k |             n = (n * 10) + a[o] - num_zero; | 
| 239 | 17.1k |             i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i; | 
| 240 | 17.1k |             if ((n < min[i2]) || (n > max[i2])) | 
| 241 | 813 |                 goto err; | 
| 242 |  |             /* if tm is NULL, no need to adjust */ | 
| 243 | 16.3k |             if (tm != NULL) { | 
| 244 | 16.3k |                 if (i == end) | 
| 245 | 8.48k |                     offset = n * 3600; | 
| 246 | 7.81k |                 else if (i == end + 1) | 
| 247 | 7.81k |                     offset += n * 60; | 
| 248 | 16.3k |             } | 
| 249 | 16.3k |             o++; | 
| 250 | 16.3k |         } | 
| 251 | 7.81k |         if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign)) | 
| 252 | 656 |             goto err; | 
| 253 | 7.81k |     } else { | 
| 254 |  |         /* not Z, or not +/- in non-strict mode */ | 
| 255 | 2.31k |         goto err; | 
| 256 | 2.31k |     } | 
| 257 | 57.3k |     if (o == l) { | 
| 258 |  |         /* success, check if tm should be filled */ | 
| 259 | 54.3k |         if (tm != NULL) | 
| 260 | 37.2k |             *tm = tmp; | 
| 261 | 54.3k |         return 1; | 
| 262 | 54.3k |     } | 
| 263 | 96.9k |  err: | 
| 264 | 96.9k |     return 0; | 
| 265 | 57.3k | } | 
| 266 |  |  | 
| 267 |  | ASN1_TIME *ossl_asn1_time_from_tm(ASN1_TIME *s, struct tm *ts, int type) | 
| 268 | 29.7k | { | 
| 269 | 29.7k |     char* p; | 
| 270 | 29.7k |     ASN1_TIME *tmps = NULL; | 
| 271 | 29.7k |     const size_t len = 20; | 
| 272 |  |  | 
| 273 | 29.7k |     if (type == V_ASN1_UNDEF) { | 
| 274 | 222 |         if (is_utc(ts->tm_year)) | 
| 275 | 222 |             type = V_ASN1_UTCTIME; | 
| 276 | 0 |         else | 
| 277 | 0 |             type = V_ASN1_GENERALIZEDTIME; | 
| 278 | 29.5k |     } else if (type == V_ASN1_UTCTIME) { | 
| 279 | 0 |         if (!is_utc(ts->tm_year)) | 
| 280 | 0 |             goto err; | 
| 281 | 29.5k |     } else if (type != V_ASN1_GENERALIZEDTIME) { | 
| 282 | 0 |         goto err; | 
| 283 | 0 |     } | 
| 284 |  |  | 
| 285 | 29.7k |     if (s == NULL) | 
| 286 | 222 |         tmps = ASN1_STRING_new(); | 
| 287 | 29.5k |     else | 
| 288 | 29.5k |         tmps = s; | 
| 289 | 29.7k |     if (tmps == NULL) | 
| 290 | 0 |         return NULL; | 
| 291 |  |  | 
| 292 | 29.7k |     if (!ASN1_STRING_set(tmps, NULL, len)) | 
| 293 | 0 |         goto err; | 
| 294 |  |  | 
| 295 | 29.7k |     tmps->type = type; | 
| 296 | 29.7k |     p = (char*)tmps->data; | 
| 297 |  |  | 
| 298 | 29.7k |     if (type == V_ASN1_GENERALIZEDTIME) | 
| 299 | 29.5k |         tmps->length = BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", | 
| 300 | 29.5k |                                     ts->tm_year + 1900, ts->tm_mon + 1, | 
| 301 | 29.5k |                                     ts->tm_mday, ts->tm_hour, ts->tm_min, | 
| 302 | 29.5k |                                     ts->tm_sec); | 
| 303 | 222 |     else | 
| 304 | 222 |         tmps->length = BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ", | 
| 305 | 222 |                                     ts->tm_year % 100, ts->tm_mon + 1, | 
| 306 | 222 |                                     ts->tm_mday, ts->tm_hour, ts->tm_min, | 
| 307 | 222 |                                     ts->tm_sec); | 
| 308 |  |  | 
| 309 |  | #ifdef CHARSET_EBCDIC | 
| 310 |  |     ebcdic2ascii(tmps->data, tmps->data, tmps->length); | 
| 311 |  | #endif | 
| 312 | 29.7k |     return tmps; | 
| 313 | 0 |  err: | 
| 314 | 0 |     if (tmps != s) | 
| 315 | 0 |         ASN1_STRING_free(tmps); | 
| 316 | 0 |     return NULL; | 
| 317 | 29.7k | } | 
| 318 |  |  | 
| 319 |  | ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t) | 
| 320 | 0 | { | 
| 321 | 0 |     return ASN1_TIME_adj(s, t, 0, 0); | 
| 322 | 0 | } | 
| 323 |  |  | 
| 324 |  | ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t, | 
| 325 |  |                          int offset_day, long offset_sec) | 
| 326 | 332 | { | 
| 327 | 332 |     struct tm *ts; | 
| 328 | 332 |     struct tm data; | 
| 329 |  |  | 
| 330 | 332 |     ts = OPENSSL_gmtime(&t, &data); | 
| 331 | 332 |     if (ts == NULL) { | 
| 332 | 0 |         ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_GETTING_TIME); | 
| 333 | 0 |         return NULL; | 
| 334 | 0 |     } | 
| 335 | 332 |     if (offset_day || offset_sec) { | 
| 336 | 0 |         if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) | 
| 337 | 0 |             return NULL; | 
| 338 | 0 |     } | 
| 339 | 332 |     return ossl_asn1_time_from_tm(s, ts, V_ASN1_UNDEF); | 
| 340 | 332 | } | 
| 341 |  |  | 
| 342 |  | int ASN1_TIME_check(const ASN1_TIME *t) | 
| 343 | 0 | { | 
| 344 | 0 |     if (t->type == V_ASN1_GENERALIZEDTIME) | 
| 345 | 0 |         return ASN1_GENERALIZEDTIME_check(t); | 
| 346 | 0 |     else if (t->type == V_ASN1_UTCTIME) | 
| 347 | 0 |         return ASN1_UTCTIME_check(t); | 
| 348 | 0 |     return 0; | 
| 349 | 0 | } | 
| 350 |  |  | 
| 351 |  | /* Convert an ASN1_TIME structure to GeneralizedTime */ | 
| 352 |  | ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t, | 
| 353 |  |                                                    ASN1_GENERALIZEDTIME **out) | 
| 354 | 0 | { | 
| 355 | 0 |     ASN1_GENERALIZEDTIME *ret = NULL; | 
| 356 | 0 |     struct tm tm; | 
| 357 |  | 
 | 
| 358 | 0 |     if (!ASN1_TIME_to_tm(t, &tm)) | 
| 359 | 0 |         return NULL; | 
| 360 |  |  | 
| 361 | 0 |     if (out != NULL) | 
| 362 | 0 |         ret = *out; | 
| 363 |  | 
 | 
| 364 | 0 |     ret = ossl_asn1_time_from_tm(ret, &tm, V_ASN1_GENERALIZEDTIME); | 
| 365 |  | 
 | 
| 366 | 0 |     if (out != NULL && ret != NULL) | 
| 367 | 0 |         *out = ret; | 
| 368 |  | 
 | 
| 369 | 0 |     return ret; | 
| 370 | 0 | } | 
| 371 |  |  | 
| 372 |  | int ASN1_TIME_set_string(ASN1_TIME *s, const char *str) | 
| 373 | 0 | { | 
| 374 |  |     /* Try UTC, if that fails, try GENERALIZED */ | 
| 375 | 0 |     if (ASN1_UTCTIME_set_string(s, str)) | 
| 376 | 0 |         return 1; | 
| 377 | 0 |     return ASN1_GENERALIZEDTIME_set_string(s, str); | 
| 378 | 0 | } | 
| 379 |  |  | 
| 380 |  | int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str) | 
| 381 | 0 | { | 
| 382 | 0 |     ASN1_TIME t; | 
| 383 | 0 |     struct tm tm; | 
| 384 | 0 |     int rv = 0; | 
| 385 |  | 
 | 
| 386 | 0 |     t.length = strlen(str); | 
| 387 | 0 |     t.data = (unsigned char *)str; | 
| 388 | 0 |     t.flags = ASN1_STRING_FLAG_X509_TIME; | 
| 389 |  | 
 | 
| 390 | 0 |     t.type = V_ASN1_UTCTIME; | 
| 391 |  | 
 | 
| 392 | 0 |     if (!ASN1_TIME_check(&t)) { | 
| 393 | 0 |         t.type = V_ASN1_GENERALIZEDTIME; | 
| 394 | 0 |         if (!ASN1_TIME_check(&t)) | 
| 395 | 0 |             goto out; | 
| 396 | 0 |     } | 
| 397 |  |  | 
| 398 |  |     /* | 
| 399 |  |      * Per RFC 5280 (section 4.1.2.5.), the valid input time | 
| 400 |  |      * strings should be encoded with the following rules: | 
| 401 |  |      * | 
| 402 |  |      * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ | 
| 403 |  |      * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ | 
| 404 |  |      * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ | 
| 405 |  |      * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ | 
| 406 |  |      * | 
| 407 |  |      * Only strings of the 4th rule should be reformatted, but since a | 
| 408 |  |      * UTC can only present [1950, 2050), so if the given time string | 
| 409 |  |      * is less than 1950 (e.g. 19230419000000Z), we do nothing... | 
| 410 |  |      */ | 
| 411 |  |  | 
| 412 | 0 |     if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) { | 
| 413 | 0 |         if (!ossl_asn1_time_to_tm(&tm, &t)) | 
| 414 | 0 |             goto out; | 
| 415 | 0 |         if (is_utc(tm.tm_year)) { | 
| 416 | 0 |             t.length -= 2; | 
| 417 |  |             /* | 
| 418 |  |              * it's OK to let original t.data go since that's assigned | 
| 419 |  |              * to a piece of memory allocated outside of this function. | 
| 420 |  |              * new t.data would be freed after ASN1_STRING_copy is done. | 
| 421 |  |              */ | 
| 422 | 0 |             t.data = OPENSSL_zalloc(t.length + 1); | 
| 423 | 0 |             if (t.data == NULL) { | 
| 424 | 0 |                 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | 
| 425 | 0 |                 goto out; | 
| 426 | 0 |             } | 
| 427 | 0 |             memcpy(t.data, str + 2, t.length); | 
| 428 | 0 |             t.type = V_ASN1_UTCTIME; | 
| 429 | 0 |         } | 
| 430 | 0 |     } | 
| 431 |  |  | 
| 432 | 0 |     if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t)) | 
| 433 | 0 |         rv = 1; | 
| 434 |  | 
 | 
| 435 | 0 |     if (t.data != (unsigned char *)str) | 
| 436 | 0 |         OPENSSL_free(t.data); | 
| 437 | 0 | out: | 
| 438 | 0 |     return rv; | 
| 439 | 0 | } | 
| 440 |  |  | 
| 441 |  | int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm) | 
| 442 | 709 | { | 
| 443 | 709 |     if (s == NULL) { | 
| 444 | 0 |         time_t now_t; | 
| 445 |  | 
 | 
| 446 | 0 |         time(&now_t); | 
| 447 | 0 |         memset(tm, 0, sizeof(*tm)); | 
| 448 | 0 |         if (OPENSSL_gmtime(&now_t, tm) != NULL) | 
| 449 | 0 |             return 1; | 
| 450 | 0 |         return 0; | 
| 451 | 0 |     } | 
| 452 |  |  | 
| 453 | 709 |     return ossl_asn1_time_to_tm(tm, s); | 
| 454 | 709 | } | 
| 455 |  |  | 
| 456 |  | int ASN1_TIME_diff(int *pday, int *psec, | 
| 457 |  |                    const ASN1_TIME *from, const ASN1_TIME *to) | 
| 458 | 415 | { | 
| 459 | 415 |     struct tm tm_from, tm_to; | 
| 460 |  |  | 
| 461 | 415 |     if (!ASN1_TIME_to_tm(from, &tm_from)) | 
| 462 | 121 |         return 0; | 
| 463 | 294 |     if (!ASN1_TIME_to_tm(to, &tm_to)) | 
| 464 | 14 |         return 0; | 
| 465 | 280 |     return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to); | 
| 466 | 294 | } | 
| 467 |  |  | 
| 468 |  | static const char _asn1_mon[12][4] = { | 
| 469 |  |     "Jan", "Feb", "Mar", "Apr", "May", "Jun", | 
| 470 |  |     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | 
| 471 |  | }; | 
| 472 |  |  | 
| 473 |  | /* prints the time with the default date format (RFC 822) */ | 
| 474 |  | int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm) | 
| 475 | 100k | { | 
| 476 | 100k |     return ASN1_TIME_print_ex(bp, tm, ASN1_DTFLGS_RFC822); | 
| 477 | 100k | } | 
| 478 |  |  | 
| 479 |  | /* returns 1 on success, 0 on BIO write error or parse failure */ | 
| 480 |  | int ASN1_TIME_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags) | 
| 481 | 100k | { | 
| 482 | 100k |     return ossl_asn1_time_print_ex(bp, tm, flags) > 0; | 
| 483 | 100k | } | 
| 484 |  |  | 
| 485 |  |  | 
| 486 |  | /* prints the time with the date format of ISO 8601 */ | 
| 487 |  | /* returns 0 on BIO write error, else -1 in case of parse failure, else 1 */ | 
| 488 |  | int ossl_asn1_time_print_ex(BIO *bp, const ASN1_TIME *tm, unsigned long flags) | 
| 489 | 123k | { | 
| 490 | 123k |     char *v; | 
| 491 | 123k |     int gmt = 0, l; | 
| 492 | 123k |     struct tm stm; | 
| 493 | 123k |     const char upper_z = 0x5A, period = 0x2E; | 
| 494 |  |  | 
| 495 |  |     /* ossl_asn1_time_to_tm will check the time type */ | 
| 496 | 123k |     if (!ossl_asn1_time_to_tm(&stm, tm)) | 
| 497 | 86.2k |         return BIO_write(bp, "Bad time value", 14) ? -1 : 0; | 
| 498 |  |  | 
| 499 | 36.8k |     l = tm->length; | 
| 500 | 36.8k |     v = (char *)tm->data; | 
| 501 | 36.8k |     if (v[l - 1] == upper_z) | 
| 502 | 29.7k |         gmt = 1; | 
| 503 |  |  | 
| 504 | 36.8k |     if (tm->type == V_ASN1_GENERALIZEDTIME) { | 
| 505 | 25.3k |         char *f = NULL; | 
| 506 | 25.3k |         int f_len = 0; | 
| 507 |  |  | 
| 508 |  |         /* | 
| 509 |  |          * Try to parse fractional seconds. '14' is the place of | 
| 510 |  |          * 'fraction point' in a GeneralizedTime string. | 
| 511 |  |          */ | 
| 512 | 25.3k |         if (tm->length > 15 && v[14] == period) { | 
| 513 | 18.6k |             f = &v[14]; | 
| 514 | 18.6k |             f_len = 1; | 
| 515 | 87.8k |             while (14 + f_len < l && ossl_ascii_isdigit(f[f_len])) | 
| 516 | 69.2k |                 ++f_len; | 
| 517 | 18.6k |         } | 
| 518 |  |  | 
| 519 | 25.3k |         if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) { | 
| 520 | 0 |             return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02d%.*s%s", | 
| 521 | 0 |                           stm.tm_year + 1900, stm.tm_mon + 1, | 
| 522 | 0 |                           stm.tm_mday, stm.tm_hour, | 
| 523 | 0 |                           stm.tm_min, stm.tm_sec, f_len, f, | 
| 524 | 0 |                           (gmt ? "Z" : "")) > 0; | 
| 525 | 0 |         } | 
| 526 | 25.3k |         else { | 
| 527 | 25.3k |             return BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s", | 
| 528 | 25.3k |                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour, | 
| 529 | 25.3k |                           stm.tm_min, stm.tm_sec, f_len, f, stm.tm_year + 1900, | 
| 530 | 25.3k |                           (gmt ? " GMT" : "")) > 0; | 
| 531 | 25.3k |         } | 
| 532 | 25.3k |     } else { | 
| 533 | 11.4k |         if ((flags & ASN1_DTFLGS_TYPE_MASK) == ASN1_DTFLGS_ISO8601) { | 
| 534 | 0 |             return BIO_printf(bp, "%4d-%02d-%02d %02d:%02d:%02d%s", | 
| 535 | 0 |                           stm.tm_year + 1900, stm.tm_mon + 1, | 
| 536 | 0 |                           stm.tm_mday, stm.tm_hour, | 
| 537 | 0 |                           stm.tm_min, stm.tm_sec, | 
| 538 | 0 |                           (gmt ? "Z" : "")) > 0; | 
| 539 | 0 |         } | 
| 540 | 11.4k |         else { | 
| 541 | 11.4k |             return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s", | 
| 542 | 11.4k |                           _asn1_mon[stm.tm_mon], stm.tm_mday, stm.tm_hour, | 
| 543 | 11.4k |                           stm.tm_min, stm.tm_sec, stm.tm_year + 1900, | 
| 544 | 11.4k |                           (gmt ? " GMT" : "")) > 0; | 
| 545 | 11.4k |         } | 
| 546 | 11.4k |     } | 
| 547 | 36.8k | } | 
| 548 |  |  | 
| 549 |  | int ASN1_TIME_cmp_time_t(const ASN1_TIME *s, time_t t) | 
| 550 | 0 | { | 
| 551 | 0 |     struct tm stm, ttm; | 
| 552 | 0 |     int day, sec; | 
| 553 |  | 
 | 
| 554 | 0 |     if (!ASN1_TIME_to_tm(s, &stm)) | 
| 555 | 0 |         return -2; | 
| 556 |  |  | 
| 557 | 0 |     if (!OPENSSL_gmtime(&t, &ttm)) | 
| 558 | 0 |         return -2; | 
| 559 |  |  | 
| 560 | 0 |     if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm)) | 
| 561 | 0 |         return -2; | 
| 562 |  |  | 
| 563 | 0 |     if (day > 0 || sec > 0) | 
| 564 | 0 |         return 1; | 
| 565 | 0 |     if (day < 0 || sec < 0) | 
| 566 | 0 |         return -1; | 
| 567 | 0 |     return 0; | 
| 568 | 0 | } | 
| 569 |  |  | 
| 570 |  | int ASN1_TIME_normalize(ASN1_TIME *t) | 
| 571 | 0 | { | 
| 572 | 0 |     struct tm tm; | 
| 573 |  | 
 | 
| 574 | 0 |     if (!ASN1_TIME_to_tm(t, &tm)) | 
| 575 | 0 |         return 0; | 
| 576 |  |  | 
| 577 | 0 |     return ossl_asn1_time_from_tm(t, &tm, V_ASN1_UNDEF) != NULL; | 
| 578 | 0 | } | 
| 579 |  |  | 
| 580 |  | int ASN1_TIME_compare(const ASN1_TIME *a, const ASN1_TIME *b) | 
| 581 | 83 | { | 
| 582 | 83 |     int day, sec; | 
| 583 |  |  | 
| 584 | 83 |     if (!ASN1_TIME_diff(&day, &sec, b, a)) | 
| 585 | 47 |         return -2; | 
| 586 | 36 |     if (day > 0 || sec > 0) | 
| 587 | 9 |         return 1; | 
| 588 | 27 |     if (day < 0 || sec < 0) | 
| 589 | 26 |         return -1; | 
| 590 | 1 |     return 0; | 
| 591 | 27 | } |