andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "prtypes.h" andre@0: #include "prtime.h" andre@0: #include "secder.h" andre@0: #include "prlong.h" andre@0: #include "secerr.h" andre@0: andre@0: #define HIDIGIT(v) (((v) / 10) + '0') andre@0: #define LODIGIT(v) (((v) % 10) + '0') andre@0: andre@0: #define ISDIGIT(dig) (((dig) >= '0') && ((dig) <= '9')) andre@0: #define CAPTURE(var,p,label) \ andre@0: { \ andre@0: if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1])) goto label; \ andre@0: (var) = ((p)[0] - '0') * 10 + ((p)[1] - '0'); \ andre@0: p += 2; \ andre@0: } andre@0: andre@0: static const PRTime January1st1 = (PRTime) LL_INIT(0xff234001U, 0x00d44000U); andre@0: static const PRTime January1st1950 = (PRTime) LL_INIT(0xfffdc1f8U, 0x793da000U); andre@0: static const PRTime January1st2050 = LL_INIT(0x0008f81e, 0x1b098000); andre@0: static const PRTime January1st10000 = LL_INIT(0x0384440c, 0xcc736000); andre@0: andre@0: /* gmttime must contains UTC time in micro-seconds unit */ andre@0: SECStatus andre@0: DER_TimeToUTCTimeArena(PLArenaPool* arenaOpt, SECItem *dst, PRTime gmttime) andre@0: { andre@0: PRExplodedTime printableTime; andre@0: unsigned char *d; andre@0: andre@0: if ( (gmttime < January1st1950) || (gmttime >= January1st2050) ) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: dst->len = 13; andre@0: if (arenaOpt) { andre@0: dst->data = d = (unsigned char*) PORT_ArenaAlloc(arenaOpt, dst->len); andre@0: } else { andre@0: dst->data = d = (unsigned char*) PORT_Alloc(dst->len); andre@0: } andre@0: dst->type = siUTCTime; andre@0: if (!d) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* Convert a PRTime to a printable format. */ andre@0: PR_ExplodeTime(gmttime, PR_GMTParameters, &printableTime); andre@0: andre@0: /* The month in UTC time is base one */ andre@0: printableTime.tm_month++; andre@0: andre@0: /* remove the century since it's added to the tm_year by the andre@0: PR_ExplodeTime routine, but is not needed for UTC time */ andre@0: printableTime.tm_year %= 100; andre@0: andre@0: d[0] = HIDIGIT(printableTime.tm_year); andre@0: d[1] = LODIGIT(printableTime.tm_year); andre@0: d[2] = HIDIGIT(printableTime.tm_month); andre@0: d[3] = LODIGIT(printableTime.tm_month); andre@0: d[4] = HIDIGIT(printableTime.tm_mday); andre@0: d[5] = LODIGIT(printableTime.tm_mday); andre@0: d[6] = HIDIGIT(printableTime.tm_hour); andre@0: d[7] = LODIGIT(printableTime.tm_hour); andre@0: d[8] = HIDIGIT(printableTime.tm_min); andre@0: d[9] = LODIGIT(printableTime.tm_min); andre@0: d[10] = HIDIGIT(printableTime.tm_sec); andre@0: d[11] = LODIGIT(printableTime.tm_sec); andre@0: d[12] = 'Z'; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SECStatus andre@0: DER_TimeToUTCTime(SECItem *dst, PRTime gmttime) andre@0: { andre@0: return DER_TimeToUTCTimeArena(NULL, dst, gmttime); andre@0: } andre@0: andre@0: static SECStatus /* forward */ andre@0: der_TimeStringToTime(PRTime *dst, const char *string, int generalized, andre@0: const char **endptr); andre@0: andre@0: #define GEN_STRING 2 /* TimeString is a GeneralizedTime */ andre@0: #define UTC_STRING 0 /* TimeString is a UTCTime */ andre@0: andre@0: /* The caller of DER_AsciiToItem MUST ENSURE that either andre@0: ** a) "string" points to a null-terminated ASCII string, or andre@0: ** b) "string" points to a buffer containing a valid UTCTime, andre@0: ** whether null terminated or not, or andre@0: ** c) "string" contains at least 19 characters, with or without null char. andre@0: ** otherwise, this function may UMR and/or crash. andre@0: ** It suffices to ensure that the input "string" is at least 17 bytes long. andre@0: */ andre@0: SECStatus andre@0: DER_AsciiToTime(PRTime *dst, const char *string) andre@0: { andre@0: return der_TimeStringToTime(dst, string, UTC_STRING, NULL); andre@0: } andre@0: andre@0: SECStatus andre@0: DER_UTCTimeToTime(PRTime *dst, const SECItem *time) andre@0: { andre@0: /* Minimum valid UTCTime is yymmddhhmmZ which is 11 bytes. andre@0: ** Maximum valid UTCTime is yymmddhhmmss+0000 which is 17 bytes. andre@0: ** 20 should be large enough for all valid encoded times. andre@0: */ andre@0: unsigned int i; andre@0: char localBuf[20]; andre@0: const char *end = NULL; andre@0: SECStatus rv; andre@0: andre@0: if (!time || !time->data || time->len < 11 || time->len > 17) { andre@0: PORT_SetError(SEC_ERROR_INVALID_TIME); andre@0: return SECFailure; andre@0: } andre@0: andre@0: for (i = 0; i < time->len; i++) { andre@0: if (time->data[i] == '\0') { andre@0: PORT_SetError(SEC_ERROR_INVALID_TIME); andre@0: return SECFailure; andre@0: } andre@0: localBuf[i] = time->data[i]; andre@0: } andre@0: localBuf[i] = '\0'; andre@0: andre@0: rv = der_TimeStringToTime(dst, localBuf, UTC_STRING, &end); andre@0: if (rv == SECSuccess && *end != '\0') { andre@0: PORT_SetError(SEC_ERROR_INVALID_TIME); andre@0: return SECFailure; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: /* andre@0: gmttime must contains UTC time in micro-seconds unit. andre@0: Note: the caller should make sure that Generalized time andre@0: should only be used for certifiate validities after the andre@0: year 2049. Otherwise, UTC time should be used. This routine andre@0: does not check this case, since it can be used to encode andre@0: certificate extension, which does not have this restriction. andre@0: */ andre@0: SECStatus andre@0: DER_TimeToGeneralizedTimeArena(PLArenaPool* arenaOpt, SECItem *dst, PRTime gmttime) andre@0: { andre@0: PRExplodedTime printableTime; andre@0: unsigned char *d; andre@0: andre@0: if ( (gmttime=January1st10000) ) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: dst->len = 15; andre@0: if (arenaOpt) { andre@0: dst->data = d = (unsigned char*) PORT_ArenaAlloc(arenaOpt, dst->len); andre@0: } else { andre@0: dst->data = d = (unsigned char*) PORT_Alloc(dst->len); andre@0: } andre@0: dst->type = siGeneralizedTime; andre@0: if (!d) { andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* Convert a PRTime to a printable format. */ andre@0: PR_ExplodeTime(gmttime, PR_GMTParameters, &printableTime); andre@0: andre@0: /* The month in Generalized time is base one */ andre@0: printableTime.tm_month++; andre@0: andre@0: d[0] = (printableTime.tm_year /1000) + '0'; andre@0: d[1] = ((printableTime.tm_year % 1000) / 100) + '0'; andre@0: d[2] = ((printableTime.tm_year % 100) / 10) + '0'; andre@0: d[3] = (printableTime.tm_year % 10) + '0'; andre@0: d[4] = HIDIGIT(printableTime.tm_month); andre@0: d[5] = LODIGIT(printableTime.tm_month); andre@0: d[6] = HIDIGIT(printableTime.tm_mday); andre@0: d[7] = LODIGIT(printableTime.tm_mday); andre@0: d[8] = HIDIGIT(printableTime.tm_hour); andre@0: d[9] = LODIGIT(printableTime.tm_hour); andre@0: d[10] = HIDIGIT(printableTime.tm_min); andre@0: d[11] = LODIGIT(printableTime.tm_min); andre@0: d[12] = HIDIGIT(printableTime.tm_sec); andre@0: d[13] = LODIGIT(printableTime.tm_sec); andre@0: d[14] = 'Z'; andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SECStatus andre@0: DER_TimeToGeneralizedTime(SECItem *dst, PRTime gmttime) andre@0: { andre@0: return DER_TimeToGeneralizedTimeArena(NULL, dst, gmttime); andre@0: } andre@0: andre@0: andre@0: SECStatus andre@0: DER_GeneralizedTimeToTime(PRTime *dst, const SECItem *time) andre@0: { andre@0: /* Minimum valid GeneralizedTime is ccyymmddhhmmZ which is 13 bytes. andre@0: ** Maximum valid GeneralizedTime is ccyymmddhhmmss+0000 which is 19 bytes. andre@0: ** 20 should be large enough for all valid encoded times. andre@0: */ andre@0: unsigned int i; andre@0: char localBuf[20]; andre@0: const char *end = NULL; andre@0: SECStatus rv; andre@0: andre@0: if (!time || !time->data || time->len < 13 || time->len > 19) { andre@0: PORT_SetError(SEC_ERROR_INVALID_TIME); andre@0: return SECFailure; andre@0: } andre@0: andre@0: for (i = 0; i < time->len; i++) { andre@0: if (time->data[i] == '\0') { andre@0: PORT_SetError(SEC_ERROR_INVALID_TIME); andre@0: return SECFailure; andre@0: } andre@0: localBuf[i] = time->data[i]; andre@0: } andre@0: localBuf[i] = '\0'; andre@0: andre@0: rv = der_TimeStringToTime(dst, localBuf, GEN_STRING, &end); andre@0: if (rv == SECSuccess && *end != '\0') { andre@0: PORT_SetError(SEC_ERROR_INVALID_TIME); andre@0: return SECFailure; andre@0: } andre@0: return rv; andre@0: } andre@0: andre@0: static SECStatus andre@0: der_TimeStringToTime(PRTime *dst, const char *string, int generalized, andre@0: const char **endptr) andre@0: { andre@0: PRExplodedTime genTime; andre@0: long hourOff = 0, minOff = 0; andre@0: PRUint16 century; andre@0: char signum; andre@0: andre@0: if (string == NULL || dst == NULL) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); andre@0: return SECFailure; andre@0: } andre@0: andre@0: /* Verify time is formatted properly and capture information */ andre@0: memset(&genTime, 0, sizeof genTime); andre@0: andre@0: if (generalized == UTC_STRING) { andre@0: CAPTURE(genTime.tm_year, string, loser); andre@0: century = (genTime.tm_year < 50) ? 20 : 19; andre@0: } else { andre@0: CAPTURE(century, string, loser); andre@0: CAPTURE(genTime.tm_year, string, loser); andre@0: } andre@0: genTime.tm_year += century * 100; andre@0: andre@0: CAPTURE(genTime.tm_month, string, loser); andre@0: if ((genTime.tm_month == 0) || (genTime.tm_month > 12)) andre@0: goto loser; andre@0: andre@0: /* NSPR month base is 0 */ andre@0: --genTime.tm_month; andre@0: andre@0: CAPTURE(genTime.tm_mday, string, loser); andre@0: if ((genTime.tm_mday == 0) || (genTime.tm_mday > 31)) andre@0: goto loser; andre@0: andre@0: CAPTURE(genTime.tm_hour, string, loser); andre@0: if (genTime.tm_hour > 23) andre@0: goto loser; andre@0: andre@0: CAPTURE(genTime.tm_min, string, loser); andre@0: if (genTime.tm_min > 59) andre@0: goto loser; andre@0: andre@0: if (ISDIGIT(string[0])) { andre@0: CAPTURE(genTime.tm_sec, string, loser); andre@0: if (genTime.tm_sec > 59) andre@0: goto loser; andre@0: } andre@0: signum = *string++; andre@0: if (signum == '+' || signum == '-') { andre@0: CAPTURE(hourOff, string, loser); andre@0: if (hourOff > 23) andre@0: goto loser; andre@0: CAPTURE(minOff, string, loser); andre@0: if (minOff > 59) andre@0: goto loser; andre@0: if (signum == '-') { andre@0: hourOff = -hourOff; andre@0: minOff = -minOff; andre@0: } andre@0: } else if (signum != 'Z') { andre@0: goto loser; andre@0: } andre@0: andre@0: if (endptr) andre@0: *endptr = string; andre@0: andre@0: /* Convert the GMT offset to seconds and save it in genTime andre@0: * for the implode time call. andre@0: */ andre@0: genTime.tm_params.tp_gmt_offset = (PRInt32)((hourOff * 60L + minOff) * 60L); andre@0: *dst = PR_ImplodeTime(&genTime); andre@0: return SECSuccess; andre@0: andre@0: loser: andre@0: PORT_SetError(SEC_ERROR_INVALID_TIME); andre@0: return SECFailure; andre@0: }