/* gmttime must contains UTC time in micro-seconds unit. Note: the caller should make sure that Generalized time should only be used for certifiate validities after the year 2049. Otherwise, UTC time should be used. This routine does not check this case, since it can be used to encode certificate extension, which does not have this restriction. */ SECStatus DER_TimeToGeneralizedTimeArena(PRArenaPool* arenaOpt, SECItem *dst, int64 gmttime) { PRExplodedTime printableTime; unsigned char *d; if ( (gmttime<January1st1) || (gmttime>=January1st10000) ) { PORT_SetError(SEC_ERROR_INVALID_ARGS); return SECFailure; } dst->len = 15; if (arenaOpt) { dst->data = d = (unsigned char*) PORT_ArenaAlloc(arenaOpt, dst->len); } else { dst->data = d = (unsigned char*) PORT_Alloc(dst->len); } dst->type = siGeneralizedTime; if (!d) { return SECFailure; } /* Convert an int64 time to a printable format. */ PR_ExplodeTime(gmttime, PR_GMTParameters, &printableTime); /* The month in Generalized time is base one */ printableTime.tm_month++; d[0] = (printableTime.tm_year /1000) + '0'; d[1] = ((printableTime.tm_year % 1000) / 100) + '0'; d[2] = ((printableTime.tm_year % 100) / 10) + '0'; d[3] = (printableTime.tm_year % 10) + '0'; d[4] = HIDIGIT(printableTime.tm_month); d[5] = LODIGIT(printableTime.tm_month); d[6] = HIDIGIT(printableTime.tm_mday); d[7] = LODIGIT(printableTime.tm_mday); d[8] = HIDIGIT(printableTime.tm_hour); d[9] = LODIGIT(printableTime.tm_hour); d[10] = HIDIGIT(printableTime.tm_min); d[11] = LODIGIT(printableTime.tm_min); d[12] = HIDIGIT(printableTime.tm_sec); d[13] = LODIGIT(printableTime.tm_sec); d[14] = 'Z'; return SECSuccess; }
/* gmttime must contains UTC time in micro-seconds unit */ SECStatus DER_TimeToUTCTimeArena(PRArenaPool* arenaOpt, SECItem *dst, int64 gmttime) { PRExplodedTime printableTime; unsigned char *d; if ( (gmttime < January1st1950) || (gmttime >= January1st2050) ) { PORT_SetError(SEC_ERROR_INVALID_ARGS); return SECFailure; } dst->len = 13; if (arenaOpt) { dst->data = d = (unsigned char*) PORT_ArenaAlloc(arenaOpt, dst->len); } else { dst->data = d = (unsigned char*) PORT_Alloc(dst->len); } dst->type = siUTCTime; if (!d) { return SECFailure; } /* Convert an int64 time to a printable format. */ PR_ExplodeTime(gmttime, PR_GMTParameters, &printableTime); /* The month in UTC time is base one */ printableTime.tm_month++; /* remove the century since it's added to the tm_year by the PR_ExplodeTime routine, but is not needed for UTC time */ printableTime.tm_year %= 100; d[0] = HIDIGIT(printableTime.tm_year); d[1] = LODIGIT(printableTime.tm_year); d[2] = HIDIGIT(printableTime.tm_month); d[3] = LODIGIT(printableTime.tm_month); d[4] = HIDIGIT(printableTime.tm_mday); d[5] = LODIGIT(printableTime.tm_mday); d[6] = HIDIGIT(printableTime.tm_hour); d[7] = LODIGIT(printableTime.tm_hour); d[8] = HIDIGIT(printableTime.tm_min); d[9] = LODIGIT(printableTime.tm_min); d[10] = HIDIGIT(printableTime.tm_sec); d[11] = LODIGIT(printableTime.tm_sec); d[12] = 'Z'; return SECSuccess; }
static OSStatus DER_CFDateToUTCTime(CFAbsoluteTime date, CSSM_DATA_PTR utcTime) { CFGregorianDate gdate = CFAbsoluteTimeGetGregorianDate(date, NULL /* GMT */); unsigned char *d; SInt8 second; utcTime->Length = 13; utcTime->Data = d = PORT_Alloc(13); if (!utcTime->Data) return SECFailure; /* UTC time does not handle the years before 1950 */ if (gdate.year < 1950) return SECFailure; /* remove the century since it's added to the year by the CFAbsoluteTimeGetGregorianDate routine, but is not needed for UTC time */ gdate.year %= 100; second = gdate.second + 0.5; d[0] = HIDIGIT(gdate.year); d[1] = LODIGIT(gdate.year); d[2] = HIDIGIT(gdate.month); d[3] = LODIGIT(gdate.month); d[4] = HIDIGIT(gdate.day); d[5] = LODIGIT(gdate.day); d[6] = HIDIGIT(gdate.hour); d[7] = LODIGIT(gdate.hour); d[8] = HIDIGIT(gdate.minute); d[9] = LODIGIT(gdate.minute); d[10] = HIDIGIT(second); d[11] = LODIGIT(second); d[12] = 'Z'; return SECSuccess; }