tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

sectime.c (4330B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "prtime.h"
      6 #include "secder.h"
      7 #include "secitem.h"
      8 #include "secerr.h"
      9 
     10 static char *DecodeUTCTime2FormattedAscii(SECItem *utcTimeDER, char *format);
     11 static char *DecodeGeneralizedTime2FormattedAscii(SECItem *generalizedTimeDER, char *format);
     12 
     13 /* convert DER utc time to ascii time string */
     14 char *
     15 DER_UTCTimeToAscii(SECItem *utcTime)
     16 {
     17    return (DecodeUTCTime2FormattedAscii(utcTime, "%a %b %d %H:%M:%S %Y"));
     18 }
     19 
     20 /* convert DER utc time to ascii time string, only include day, not time */
     21 char *
     22 DER_UTCDayToAscii(SECItem *utctime)
     23 {
     24    return (DecodeUTCTime2FormattedAscii(utctime, "%a %b %d, %Y"));
     25 }
     26 
     27 /* convert DER generalized time to ascii time string, only include day,
     28   not time */
     29 char *
     30 DER_GeneralizedDayToAscii(SECItem *gentime)
     31 {
     32    return (DecodeGeneralizedTime2FormattedAscii(gentime, "%a %b %d, %Y"));
     33 }
     34 
     35 /* convert DER generalized or UTC time to ascii time string, only include
     36   day, not time */
     37 char *
     38 DER_TimeChoiceDayToAscii(SECItem *timechoice)
     39 {
     40    switch (timechoice->type) {
     41 
     42        case siUTCTime:
     43            return DER_UTCDayToAscii(timechoice);
     44 
     45        case siGeneralizedTime:
     46            return DER_GeneralizedDayToAscii(timechoice);
     47 
     48        default:
     49            PORT_Assert(0);
     50            PORT_SetError(SEC_ERROR_INVALID_ARGS);
     51            return NULL;
     52    }
     53 }
     54 
     55 char *
     56 CERT_UTCTime2FormattedAscii(PRTime utcTime, char *format)
     57 {
     58    PRExplodedTime printableTime;
     59    char *timeString;
     60 
     61    /* Converse time to local time and decompose it into components */
     62    PR_ExplodeTime(utcTime, PR_LocalTimeParameters, &printableTime);
     63 
     64    timeString = (char *)PORT_Alloc(256);
     65 
     66    if (timeString) {
     67        if (!PR_FormatTime(timeString, 256, format, &printableTime)) {
     68            PORT_Free(timeString);
     69            timeString = NULL;
     70        }
     71    }
     72 
     73    return (timeString);
     74 }
     75 
     76 char *
     77 CERT_GenTime2FormattedAscii(PRTime genTime, char *format)
     78 {
     79    PRExplodedTime printableTime;
     80    char *timeString;
     81 
     82    /* Decompose time into components */
     83    PR_ExplodeTime(genTime, PR_GMTParameters, &printableTime);
     84 
     85    timeString = (char *)PORT_Alloc(256);
     86 
     87    if (timeString) {
     88        if (!PR_FormatTime(timeString, 256, format, &printableTime)) {
     89            PORT_Free(timeString);
     90            timeString = NULL;
     91            PORT_SetError(SEC_ERROR_OUTPUT_LEN);
     92        }
     93    }
     94 
     95    return (timeString);
     96 }
     97 
     98 /* convert DER utc time to ascii time string, The format of the time string
     99   depends on the input "format"
    100 */
    101 static char *
    102 DecodeUTCTime2FormattedAscii(SECItem *utcTimeDER, char *format)
    103 {
    104    PRTime utcTime;
    105    int rv;
    106 
    107    rv = DER_UTCTimeToTime(&utcTime, utcTimeDER);
    108    if (rv) {
    109        return (NULL);
    110    }
    111    return (CERT_UTCTime2FormattedAscii(utcTime, format));
    112 }
    113 
    114 /* convert DER utc time to ascii time string, The format of the time string
    115   depends on the input "format"
    116 */
    117 static char *
    118 DecodeGeneralizedTime2FormattedAscii(SECItem *generalizedTimeDER, char *format)
    119 {
    120    PRTime generalizedTime;
    121    int rv;
    122 
    123    rv = DER_GeneralizedTimeToTime(&generalizedTime, generalizedTimeDER);
    124    if (rv) {
    125        return (NULL);
    126    }
    127    return (CERT_GeneralizedTime2FormattedAscii(generalizedTime, format));
    128 }
    129 
    130 /* decode a SECItem containing either a SEC_ASN1_GENERALIZED_TIME
    131   or a SEC_ASN1_UTC_TIME */
    132 
    133 SECStatus
    134 DER_DecodeTimeChoice(PRTime *output, const SECItem *input)
    135 {
    136    switch (input->type) {
    137        case siGeneralizedTime:
    138            return DER_GeneralizedTimeToTime(output, input);
    139 
    140        case siUTCTime:
    141            return DER_UTCTimeToTime(output, input);
    142 
    143        default:
    144            PORT_SetError(SEC_ERROR_INVALID_ARGS);
    145            PORT_Assert(0);
    146            return SECFailure;
    147    }
    148 }
    149 
    150 /* encode a PRTime to an ASN.1 DER SECItem containing either a
    151   SEC_ASN1_GENERALIZED_TIME or a SEC_ASN1_UTC_TIME */
    152 
    153 SECStatus
    154 DER_EncodeTimeChoice(PLArenaPool *arena, SECItem *output, PRTime input)
    155 {
    156    SECStatus rv;
    157 
    158    rv = DER_TimeToUTCTimeArena(arena, output, input);
    159    if (rv == SECSuccess || PORT_GetError() != SEC_ERROR_INVALID_ARGS) {
    160        return rv;
    161    }
    162    return DER_TimeToGeneralizedTimeArena(arena, output, input);
    163 }