Blame nss/lib/util/sectime.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
Packit 40b132
#include "prtime.h"
Packit 40b132
#include "secder.h"
Packit 40b132
#include "secitem.h"
Packit 40b132
#include "secerr.h"
Packit 40b132
Packit 40b132
static char *DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER, char *format);
Packit 40b132
static char *DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER, char *format);
Packit 40b132
Packit 40b132
/* convert DER utc time to ascii time string */
Packit 40b132
char *
Packit 40b132
DER_UTCTimeToAscii(SECItem *utcTime)
Packit 40b132
{
Packit 40b132
    return (DecodeUTCTime2FormattedAscii (utcTime, "%a %b %d %H:%M:%S %Y"));
Packit 40b132
}
Packit 40b132
Packit 40b132
/* convert DER utc time to ascii time string, only include day, not time */
Packit 40b132
char *
Packit 40b132
DER_UTCDayToAscii(SECItem *utctime)
Packit 40b132
{
Packit 40b132
    return (DecodeUTCTime2FormattedAscii (utctime, "%a %b %d, %Y"));
Packit 40b132
}
Packit 40b132
Packit 40b132
/* convert DER generalized time to ascii time string, only include day,
Packit 40b132
   not time */
Packit 40b132
char *
Packit 40b132
DER_GeneralizedDayToAscii(SECItem *gentime)
Packit 40b132
{
Packit 40b132
    return (DecodeGeneralizedTime2FormattedAscii (gentime, "%a %b %d, %Y"));
Packit 40b132
}
Packit 40b132
Packit 40b132
/* convert DER generalized or UTC time to ascii time string, only include
Packit 40b132
   day, not time */
Packit 40b132
char *
Packit 40b132
DER_TimeChoiceDayToAscii(SECItem *timechoice)
Packit 40b132
{
Packit 40b132
    switch (timechoice->type) {
Packit 40b132
Packit 40b132
    case siUTCTime:
Packit 40b132
        return DER_UTCDayToAscii(timechoice);
Packit 40b132
Packit 40b132
    case siGeneralizedTime:
Packit 40b132
        return DER_GeneralizedDayToAscii(timechoice);
Packit 40b132
Packit 40b132
    default:
Packit 40b132
        PORT_Assert(0);
Packit 40b132
        PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
        return NULL;
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
char *
Packit 40b132
CERT_UTCTime2FormattedAscii(PRTime utcTime, char *format)
Packit 40b132
{
Packit 40b132
    PRExplodedTime printableTime; 
Packit 40b132
    char *timeString;
Packit 40b132
   
Packit 40b132
    /* Converse time to local time and decompose it into components */
Packit 40b132
    PR_ExplodeTime(utcTime, PR_LocalTimeParameters, &printableTime);
Packit 40b132
    
Packit 40b132
    timeString = (char *)PORT_Alloc(256);
Packit 40b132
Packit 40b132
    if ( timeString ) {
Packit 40b132
        if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) {
Packit 40b132
            PORT_Free(timeString);
Packit 40b132
            timeString = NULL;
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
    
Packit 40b132
    return (timeString);
Packit 40b132
}
Packit 40b132
Packit 40b132
char *CERT_GenTime2FormattedAscii(PRTime genTime, char *format)
Packit 40b132
{
Packit 40b132
    PRExplodedTime printableTime; 
Packit 40b132
    char *timeString;
Packit 40b132
   
Packit 40b132
    /* Decompose time into components */
Packit 40b132
    PR_ExplodeTime(genTime, PR_GMTParameters, &printableTime);
Packit 40b132
    
Packit 40b132
    timeString = (char *)PORT_Alloc(256);
Packit 40b132
Packit 40b132
    if ( timeString ) {
Packit 40b132
        if ( ! PR_FormatTime( timeString, 256, format, &printableTime )) {
Packit 40b132
            PORT_Free(timeString);
Packit 40b132
            timeString = NULL;
Packit 40b132
            PORT_SetError(SEC_ERROR_OUTPUT_LEN);
Packit 40b132
        }
Packit 40b132
    }
Packit 40b132
    
Packit 40b132
    return (timeString);
Packit 40b132
}
Packit 40b132
Packit 40b132
Packit 40b132
/* convert DER utc time to ascii time string, The format of the time string
Packit 40b132
   depends on the input "format"
Packit 40b132
 */
Packit 40b132
static char *
Packit 40b132
DecodeUTCTime2FormattedAscii (SECItem *utcTimeDER,  char *format)
Packit 40b132
{
Packit 40b132
    PRTime utcTime;
Packit 40b132
    int rv;
Packit 40b132
   
Packit 40b132
    rv = DER_UTCTimeToTime(&utcTime, utcTimeDER);
Packit 40b132
    if (rv) {
Packit 40b132
        return(NULL);
Packit 40b132
    }
Packit 40b132
    return (CERT_UTCTime2FormattedAscii (utcTime, format));
Packit 40b132
}
Packit 40b132
Packit 40b132
/* convert DER utc time to ascii time string, The format of the time string
Packit 40b132
   depends on the input "format"
Packit 40b132
 */
Packit 40b132
static char *
Packit 40b132
DecodeGeneralizedTime2FormattedAscii (SECItem *generalizedTimeDER,  char *format)
Packit 40b132
{
Packit 40b132
    PRTime generalizedTime;
Packit 40b132
    int rv;
Packit 40b132
   
Packit 40b132
    rv = DER_GeneralizedTimeToTime(&generalizedTime, generalizedTimeDER);
Packit 40b132
    if (rv) {
Packit 40b132
        return(NULL);
Packit 40b132
    }
Packit 40b132
    return (CERT_GeneralizedTime2FormattedAscii (generalizedTime, format));
Packit 40b132
}
Packit 40b132
Packit 40b132
/* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME 
Packit 40b132
   or a SEC_ASN1_UTC_TIME */
Packit 40b132
Packit 40b132
SECStatus DER_DecodeTimeChoice(PRTime* output, const SECItem* input)
Packit 40b132
{
Packit 40b132
    switch (input->type) {
Packit 40b132
        case siGeneralizedTime:
Packit 40b132
            return DER_GeneralizedTimeToTime(output, input);
Packit 40b132
Packit 40b132
        case siUTCTime:
Packit 40b132
            return DER_UTCTimeToTime(output, input);
Packit 40b132
Packit 40b132
        default:
Packit 40b132
            PORT_SetError(SEC_ERROR_INVALID_ARGS);
Packit 40b132
            PORT_Assert(0);
Packit 40b132
            return SECFailure;
Packit 40b132
    }
Packit 40b132
}
Packit 40b132
Packit 40b132
/* encode a PRTime to an ASN.1 DER SECItem containing either a
Packit 40b132
   SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
Packit 40b132
Packit 40b132
SECStatus DER_EncodeTimeChoice(PLArenaPool* arena, SECItem* output, PRTime input)
Packit 40b132
{
Packit 40b132
    SECStatus rv;
Packit 40b132
Packit 40b132
    rv = DER_TimeToUTCTimeArena(arena, output, input);
Packit 40b132
    if (rv == SECSuccess || PORT_GetError() != SEC_ERROR_INVALID_ARGS) {
Packit 40b132
        return rv;
Packit 40b132
    }
Packit 40b132
    return DER_TimeToGeneralizedTimeArena(arena, output, input);
Packit 40b132
}