]> git.saurik.com Git - apple/security.git/blobdiff - OSX/libsecurity_cdsa_utils/lib/cuTimeStr.cpp
Security-59306.80.4.tar.gz
[apple/security.git] / OSX / libsecurity_cdsa_utils / lib / cuTimeStr.cpp
index fe246994e7e9462d3dd51ec8ebff247e8ff37233..4eccabc88c15f19525ceeac27d886653e091d8b6 100644 (file)
@@ -1,12 +1,12 @@
 /*
- * Copyright (c) 2002,2011-2012,2014 Apple Inc. All Rights Reserved.
- * 
+ * Copyright (c) 2002,2011-2012,2014-2019 Apple Inc. All Rights Reserved.
+ *
  * The contents of this file constitute Original Code as defined in and are
  * subject to the Apple Public Source License Version 1.2 (the 'License').
  * You may not use this file except in compliance with the License. Please obtain
  * a copy of the License at http://www.apple.com/publicsource and read it before
  * using this file.
- * 
+ *
  * This Original Code and all software distributed under the License are
  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
  * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
  * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
  * specific language governing rights and limitations under the License.
  */
+
 /*
  * cuTimeStr.cpp - time string routines
  */
-#include "cuTimeStr.h" 
+#include "cuTimeStr.h"
 #include "cuCdsaUtils.h"
 #include <string.h>
 #include <stdlib.h>
@@ -29,7 +29,7 @@
 /*
  * Given a string containing either a UTC-style or "generalized time"
  * time string, convert to a struct tm (in GMT/UTC). Returns nonzero on
- * error. 
+ * error.
  */
 int cuTimeStringToTm(
        const char                      *str,
@@ -46,7 +46,7 @@ int cuTimeStringToTm(
        if((str == NULL) || (len == 0) || (tmp == NULL)) {
        return 1;
        }
-       
+
        /* tolerate NULL terminated or not */
        if(str[len - 1] == '\0') {
                len--;
@@ -61,12 +61,12 @@ int cuTimeStringToTm(
                        break;
                case GENERALIZED_TIME_STRLEN:   // 4-digit year
                        break;
-               default:                                                // unknown format 
+               default:                                                // unknown format
                        return 1;
        }
-       
+
        cp = (char *)str;
-       
+
        /* check that all characters except last are digits */
        for(i=0; i<(len - 1); i++) {
                if ( !(isdigit(cp[i])) ) {
@@ -88,13 +88,13 @@ int cuTimeStringToTm(
                szTemp[3] = *cp++;
                szTemp[4] = '\0';
        }
-       else { 
+       else {
                szTemp[2] = '\0';
        }
        x = atoi( szTemp );
        if(isUtc) {
-               /* 
-                * 2-digit year. 
+               /*
+                * 2-digit year.
                 *   0  <= year <  50 : assume century 21
                 *   50 <= year <  70 : illegal per PKIX, though we tolerate
                 *   70 <  year <= 99 : assume century 20
@@ -109,7 +109,7 @@ int cuTimeStringToTm(
                */
                else {
                        /* century 20 */
-                       x += 1900;                      
+                       x += 1900;
                }
        }
        /* by definition - tm_year is year - 1900 */
@@ -178,25 +178,37 @@ int cuTimeStringToTm(
 #define MAX_TIME_STR_LEN       30
 
 /* protects time(), gmtime() */
-static pthread_mutex_t timeMutex = PTHREAD_MUTEX_INITIALIZER;  
+static pthread_mutex_t timeMutex = PTHREAD_MUTEX_INITIALIZER;
 
-char *cuTimeAtNowPlus(int secFromNow, 
+char *cuTimeAtNowPlus(int secFromNow,
        timeSpec spec)
 {
        struct tm utc;
        char *outStr;
        time_t baseTime;
-       
+
        pthread_mutex_lock(&timeMutex);
        baseTime = time(NULL);
        baseTime += (time_t)secFromNow;
        utc = *gmtime(&baseTime);
        pthread_mutex_unlock(&timeMutex);
-       
+
        outStr = (char *)APP_MALLOC(MAX_TIME_STR_LEN);
-       
+
+       /* Check for legacy value of 1 */
+       if (spec == CU_TIME_LEGACY) {
+#if (TIME_UTC == 1)
+               /* time.h has defined TIME_UTC=1, so legacy caller
+                  actually wants TIME_UTC, not TIME_CSSM. */
+               spec = CU_TIME_UTC;
+#else
+               spec = CU_TIME_CSSM;
+#endif
+       }
+
        switch(spec) {
-               case TIME_UTC:
+               case CU_TIME_UTC:
+               case CU_TIME_LEGACY:
                        /* UTC - 2 year digits - code which parses this assumes that
                         * (2-digit) years between 0 and 49 are in century 21 */
                        if(utc.tm_year >= 100) {
@@ -206,16 +218,16 @@ char *cuTimeAtNowPlus(int secFromNow,
                                utc.tm_year /* + 1900 */, utc.tm_mon + 1,
                                utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec);
                        break;
-               case TIME_GEN:
+               case CU_TIME_GEN:
                        sprintf(outStr, "%04d%02d%02d%02d%02d%02dZ",
-                               /* note year is relative to 1900, hopefully it'll 
+                               /* note year is relative to 1900, hopefully it'll
                                 * have four valid digits! */
                                utc.tm_year + 1900, utc.tm_mon + 1,
                                utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec);
                        break;
-               case TIME_CSSM:
+               case CU_TIME_CSSM:
                        sprintf(outStr, "%04d%02d%02d%02d%02d%02d",
-                               /* note year is relative to 1900, hopefully it'll have 
+                               /* note year is relative to 1900, hopefully it'll have
                                 * four valid digits! */
                                utc.tm_year + 1900, utc.tm_mon + 1,
                                utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec);
@@ -227,17 +239,17 @@ char *cuTimeAtNowPlus(int secFromNow,
 /*
  * Convert a CSSM_X509_TIME, which can be in any of three forms (UTC,
  * generalized, or CSSM_TIMESTRING) into a CSSM_TIMESTRING. Caller
- * must free() the result. Returns NULL if x509time is badly formed. 
+ * must free() the result. Returns NULL if x509time is badly formed.
  */
 char *cuX509TimeToCssmTimestring(
        const CSSM_X509_TIME    *x509Time,
        unsigned                                *rtnLen)                // for caller's convenience
 {
        int len = (int)x509Time->time.Length;
-       const char *inStr = (char *)x509Time->time.Data;        
+       const char *inStr = (char *)x509Time->time.Data;
                                                                                        // not NULL terminated!
        char *rtn;
-       
+
        *rtnLen = 0;
        if((len == 0) || (inStr == NULL)) {
                return NULL;
@@ -254,8 +266,8 @@ char *cuX509TimeToCssmTimestring(
                        tmp[1] = inStr[1];
                        tmp[2] = '\0';
                        year = atoi(tmp);
-                       
-                       /* 
+
+                       /*
                         *   0  <= year <  50 : assume century 21
                         *   50 <= year <  70 : illegal per PKIX
                         *   70 <  year <= 99 : assume century 20
@@ -281,7 +293,7 @@ char *cuX509TimeToCssmTimestring(
                case GENERALIZED_TIME_STRLEN:
                        memmove(rtn, inStr, len - 1);                   // don't copy the Z
                        break;
-               
+
                default:
                        free(rtn);
                        return NULL;