]>
git.saurik.com Git - apple/security.git/blob - Security/libsecurity_ocspd/common/ocspdUtils.cpp
cee4b180d375c9ea9497d8ba5fb272e600307a4e
   2  * Copyright (c) 2000,2002,2011-2012,2014 Apple Inc. All Rights Reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * This file contains Original Code and/or Modifications of Original Code 
   7  * as defined in and that are subject to the Apple Public Source License 
   8  * Version 2.0 (the 'License'). You may not use this file except in 
   9  * compliance with the License. Please obtain a copy of the License at 
  10  * http://www.opensource.apple.com/apsl/ and read it before using this 
  13  * The Original Code and all software distributed under the License are 
  14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  18  * Please see the License for the specific language governing rights and 
  19  * limitations under the License. 
  21  * @APPLE_LICENSE_HEADER_END@ 
  25  * ocspUtils.cpp - common utilities for OCSPD 
  28 #include "ocspdUtils.h" 
  29 #include <CoreFoundation/CoreFoundation.h> 
  32  * Compare two CSSM_DATAs, return CSSM_TRUE if identical. 
  34 CSSM_BOOL 
ocspdCompareCssmData( 
  35         const CSSM_DATA 
*data1
, 
  36         const CSSM_DATA 
*data2
) 
  38         if((data1 
== NULL
) || (data1
->Data 
== NULL
) ||  
  39            (data2 
== NULL
) || (data2
->Data 
== NULL
) || 
  40            (data1
->Length 
!= data2
->Length
)) { 
  43         if(data1
->Length 
!= data2
->Length
) { 
  46         if(memcmp(data1
->Data
, data2
->Data
, data1
->Length
) == 0) { 
  55  * Convert a generalized time string, with a 4-digit year and no trailing 
  56  * fractional seconds or time zone info, to a CFAbsoluteTime. Returns  
  57  * NULL_TIME (0.0) on error.  
  59 static CFAbsoluteTime 
parseGenTime( 
  63         if((str 
== NULL
) || (len 
== 0)) { 
  67         /* tolerate NULL terminated or not */ 
  68         if(str
[len 
- 1] == '\0') { 
  76         memset(&greg
, 0, sizeof(greg
)); 
  77         const uint8 
*cp 
= str
; 
  86         greg
.year 
= atoi(szTemp
); 
  88         /* MONTH - CFGregorianDate ranges 1..12, just like the string */ 
  96         greg
.month 
= atoi( szTemp 
); 
 105         greg
.day 
= atoi( szTemp 
); 
 113                 greg
.hour 
= atoi( szTemp 
); 
 121                 greg
.minute 
= atoi( szTemp 
); 
 129                 greg
.second 
= atoi( szTemp 
); 
 132         return CFGregorianDateGetAbsoluteTime(greg
, NULL
); 
 136  * Parse a GeneralizedTime string into a CFAbsoluteTime. Returns NULL on parse error. 
 137  * Fractional parts of a second are discarded.  
 139 CFAbsoluteTime 
genTimeToCFAbsTime( 
 140         const CSSM_DATA 
*strData
) 
 142         if((strData 
== NULL
) || (strData
->Data 
== NULL
) || (strData
->Length 
== 0)) { 
 146         uint8 
*timeStr 
= strData
->Data
; 
 147         size_t timeStrLen 
= strData
->Length
; 
 149         /* tolerate NULL terminated or not */ 
 150         if(timeStr
[timeStrLen 
- 1] == '\0') { 
 154         /* start with a fresh editable copy */ 
 155         uint8 
*str 
= (uint8 
*)malloc(timeStrLen
); 
 159          * If there is a decimal point, strip it and all trailing digits off 
 161         const uint8 
*inCp 
= timeStr
; 
 163         int foundDecimal 
= 0; 
 164         int minutesOffset 
= 0; 
 166         bool minusOffset 
= false; 
 168         size_t toGo 
= timeStrLen
; 
 173                                 /* only legal once */ { 
 180                         /* skip the decimal point... */ 
 187                         /* then all subsequent contiguous digits */ 
 188                         while(isdigit(*inCp
) && (toGo 
!= 0)) { 
 192                 }       /* decimal point processing */ 
 193                 else if((*inCp 
== '+') || (*inCp 
== '-')) { 
 194                         /* Time zone offset - handle 2 or 4 chars */ 
 195                         if((toGo 
!= 2) & (toGo 
!= 4)) { 
 203                         hoursOffset 
= (10 * (inCp
[0] - '0')) + (inCp
[1] - '0'); 
 206                                 minutesOffset 
= (10 * (inCp
[0] - '0')) + (inCp
[1] - '0'); 
 217         if(str
[strLen 
- 1] == 'Z') { 
 222         CFAbsoluteTime absTime
; 
 223         absTime 
= parseGenTime(str
, strLen
); 
 225         if(absTime 
== NULL_TIME
) { 
 229         /* post processing needed? */ 
 231                 /* Nope, string was in GMT */ 
 234         if((minutesOffset 
!= 0) || (hoursOffset 
!= 0)) { 
 235                 /* string contained explicit offset from GMT */ 
 237                         absTime 
-= (minutesOffset 
* 60); 
 238                         absTime 
-= (hoursOffset 
* 3600); 
 241                         absTime 
+= (minutesOffset 
* 60); 
 242                         absTime 
+= (hoursOffset 
* 3600); 
 246                 /* implciit offset = local */ 
 247                 CFTimeInterval tzDelta
; 
 248                 CFTimeZoneRef localZone 
= CFTimeZoneCopySystem(); 
 249                 tzDelta 
= CFTimeZoneGetSecondsFromGMT (localZone
, CFAbsoluteTimeGetCurrent()); 
 250                 CFRelease(localZone
); 
 257  * Convert CFAbsoluteTime to generalized time string, GMT format (4 digit year, 
 258  * trailing 'Z'). Caller allocated the output which is GENERAL_TIME_STRLEN+1 bytes. 
 260 void cfAbsTimeToGgenTime( 
 261         CFAbsoluteTime          absTime
, 
 264         /* time zone = GMT */ 
 265         CFTimeZoneRef tz 
= CFTimeZoneCreateWithTimeIntervalFromGMT(NULL
, 0.0); 
 266         CFGregorianDate greg 
= CFAbsoluteTimeGetGregorianDate(absTime
, tz
); 
 267         int seconds 
= (int)greg
.second
; 
 268         sprintf(genTime
, "%04d%02d%02d%02d%02d%02dZ", 
 269                                 (int)greg
.year
, greg
.month
, greg
.day
, greg
.hour
,  
 270                                 greg
.minute
, seconds
); 
 276         unsigned char   *md
)            // allocd by caller, CC_SHA1_DIGEST_LENGTH bytes 
 280         CC_SHA1_Update(&ctx
, data
, len
); 
 281         CC_SHA1_Final(md
, &ctx
); 
 287         unsigned char   *md
)            // allocd by caller, CC_MD5_DIGEST_LENGTH bytes 
 291         CC_MD5_Update(&ctx
, data
, len
); 
 292         CC_MD5_Final(md
, &ctx
); 
 298         unsigned char   *md
)            // allocd by caller, CC_MD4_DIGEST_LENGTH bytes 
 302         CC_MD4_Update(&ctx
, data
, len
); 
 303         CC_MD4_Final(md
, &ctx
); 
 309         unsigned char   *md
)            // allocd by caller, CC_SHA256_DIGEST_LENGTH bytes 
 312         CC_SHA256_Init(&ctx
); 
 313         CC_SHA256_Update(&ctx
, data
, len
); 
 314         CC_SHA256_Final(md
, &ctx
); 
 318  * How many items in a NULL-terminated array of pointers? 
 320 unsigned ocspdArraySize(