FreeRTOS-Plus/ThirdParty/WolfSSL-FIPS-Ready/wolfcrypt/src/asn.c
/* Set current time string, either UTC or GeneralizedTime.
 * (void*) tm should be a pointer to time_t, output is placed in buf.
 *
 * Return time string length placed in buf on success, negative on error */
Set current time string, either UTC or GeneralizedTime.
(void*) tm should be a pointer to time_t, output is placed in buf.

Return time string length placed in buf on success, negative on error
Set current time string either UTC or GeneralizedTime . ( void * ) tm should be a pointer to time_t output is placed in buf . Return time string length placed in buf on success negative on error
int GetAsnTimeString(void* currTime, byte* buf, word32 len)
{
    struct tm* ts      = NULL;
    struct tm* tmpTime = NULL;
#if defined(NEED_TMP_TIME)
    struct tm tmpTimeStorage;
    tmpTime = &tmpTimeStorage;
#else
    (void)tmpTime;
#endif
    byte* data_ptr  = buf;
    word32 data_len = 0;
    int year, mon, day, hour, mini, sec;
    WOLFSSL_ENTER("SetAsnTimeString");
    if (buf == NULL || len == 0)
        return BAD_FUNC_ARG;
    ts = (struct tm *)XGMTIME((time_t*)currTime, tmpTime);
    if (ts == NULL){
        WOLFSSL_MSG("failed to get time data.");
        return ASN_TIME_E;
    }
    if (ts->tm_year >= 50 && ts->tm_year < 150) {
        char utc_str[ASN_UTC_TIME_SIZE];
        data_len = ASN_UTC_TIME_SIZE - 1 + 2;
        if (len < data_len)
            return BUFFER_E;
        if (ts->tm_year >= 50 && ts->tm_year < 100) {
            year = ts->tm_year;
        } else if (ts->tm_year >= 100 && ts->tm_year < 150) {
            year = ts->tm_year - 100;
        }
        else {
            WOLFSSL_MSG("unsupported year range");
            return BAD_FUNC_ARG;
        }
        mon  = ts->tm_mon + 1;
        day  = ts->tm_mday;
        hour = ts->tm_hour;
        mini = ts->tm_min;
        sec  = ts->tm_sec;
        XSNPRINTF((char *)utc_str, ASN_UTC_TIME_SIZE,
                  "%02d%02d%02d%02d%02d%02dZ", year, mon, day, hour, mini, sec);
        *data_ptr = (byte) ASN_UTC_TIME; data_ptr++;
        *data_ptr = (byte) ASN_UTC_TIME_SIZE - 1; data_ptr++;
        XMEMCPY(data_ptr,(byte *)utc_str, ASN_UTC_TIME_SIZE - 1);
    } else {
        char gt_str[ASN_GENERALIZED_TIME_SIZE];
        data_len = ASN_GENERALIZED_TIME_SIZE - 1 + 2;
        if (len < data_len)
            return BUFFER_E;
        year = ts->tm_year + 1900;
        mon  = ts->tm_mon + 1;
        day  = ts->tm_mday;
        hour = ts->tm_hour;
        mini = ts->tm_min;
        sec  = ts->tm_sec;
        XSNPRINTF((char *)gt_str, ASN_GENERALIZED_TIME_SIZE,
                  "%4d%02d%02d%02d%02d%02dZ", year, mon, day, hour, mini, sec);
        *data_ptr = (byte) ASN_GENERALIZED_TIME; data_ptr++;
        *data_ptr = (byte) ASN_GENERALIZED_TIME_SIZE - 1; data_ptr++;
        XMEMCPY(data_ptr,(byte *)gt_str, ASN_GENERALIZED_TIME_SIZE - 1);
    }
    return data_len;
}
int GetAsnTimeString ( void * currTime , byte * buf , word32 len ) { struct tm * ts = NULL ; struct tm * tmpTime = NULL ; #if defined ( NEED_TMP_TIME ) struct tm tmpTimeStorage ; tmpTime = & tmpTimeStorage ; #else ( void ) tmpTime ; #endif byte * data_ptr = buf ; word32 data_len = 0 ; int year , mon , day , hour , mini , sec ; WOLFSSL_ENTER ( " " ) ; if ( buf == NULL || len == 0 ) return BAD_FUNC_ARG ; ts = ( struct tm * ) XGMTIME ( ( time_t * ) currTime , tmpTime ) ; if ( ts == NULL ) { WOLFSSL_MSG ( " " ) ; return ASN_TIME_E ; } if ( ts -> tm_year >= 50 && ts -> tm_year < 150 ) { char utc_str [ ASN_UTC_TIME_SIZE ] ; data_len = ASN_UTC_TIME_SIZE - 1 + 2 ; if ( len < data_len ) return BUFFER_E ; if ( ts -> tm_year >= 50 && ts -> tm_year < 100 ) { year = ts -> tm_year ; } else if ( ts -> tm_year >= 100 && ts -> tm_year < 150 ) { year = ts -> tm_year - 100 ; } else { WOLFSSL_MSG ( " " ) ; return BAD_FUNC_ARG ; } mon = ts -> tm_mon + 1 ; day = ts -> tm_mday ; hour = ts -> tm_hour ; mini = ts -> tm_min ; sec = ts -> tm_sec ; XSNPRINTF ( ( char * ) utc_str , ASN_UTC_TIME_SIZE , " " , year , mon , day , hour , mini , sec ) ; * data_ptr = ( byte ) ASN_UTC_TIME ; data_ptr ++ ; * data_ptr = ( byte ) ASN_UTC_TIME_SIZE - 1 ; data_ptr ++ ; XMEMCPY ( data_ptr , ( byte * ) utc_str , ASN_UTC_TIME_SIZE - 1 ) ; } else { char gt_str [ ASN_GENERALIZED_TIME_SIZE ] ; data_len = ASN_GENERALIZED_TIME_SIZE - 1 + 2 ; if ( len < data_len ) return BUFFER_E ; year = ts -> tm_year + 1900 ; mon = ts -> tm_mon + 1 ; day = ts -> tm_mday ; hour = ts -> tm_hour ; mini = ts -> tm_min ; sec = ts -> tm_sec ; XSNPRINTF ( ( char * ) gt_str , ASN_GENERALIZED_TIME_SIZE , " " , year , mon , day , hour , mini , sec ) ; * data_ptr = ( byte ) ASN_GENERALIZED_TIME ; data_ptr ++ ; * data_ptr = ( byte ) ASN_GENERALIZED_TIME_SIZE - 1 ; data_ptr ++ ; XMEMCPY ( data_ptr , ( byte * ) gt_str , ASN_GENERALIZED_TIME_SIZE - 1 ) ; } return data_len ; }
Set current time string, either UTC or GeneralizedTime.
Set current time string either UTC or GeneralizedTime .
/* Note ASN_UTC_TIME_SIZE and ASN_GENERALIZED_TIME_SIZE include space for
     * the null terminator. ASN encoded values leave off the terminator. */ /* UTC Time */ /* -1 below excludes null terminator */ /* GeneralizedTime */ /* -1 below excludes null terminator */