]>
git.saurik.com Git - apple/security.git/blob - libsecurity_ocspd/common/ocspdUtils.cpp
2 * Copyright (c) 2002,2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * ocspUtils.cpp - common utilities for OCSPD
27 #include "ocspdUtils.h"
28 #include <CoreFoundation/CoreFoundation.h>
31 * Compare two CSSM_DATAs, return CSSM_TRUE if identical.
33 CSSM_BOOL
ocspdCompareCssmData(
34 const CSSM_DATA
*data1
,
35 const CSSM_DATA
*data2
)
37 if((data1
== NULL
) || (data1
->Data
== NULL
) ||
38 (data2
== NULL
) || (data2
->Data
== NULL
) ||
39 (data1
->Length
!= data2
->Length
)) {
42 if(data1
->Length
!= data2
->Length
) {
45 if(memcmp(data1
->Data
, data2
->Data
, data1
->Length
) == 0) {
54 * Convert a generalized time string, with a 4-digit year and no trailing
55 * fractional seconds or time zone info, to a CFAbsoluteTime. Returns
56 * NULL_TIME (0.0) on error.
58 static CFAbsoluteTime
parseGenTime(
62 if((str
== NULL
) || (len
== 0)) {
66 /* tolerate NULL terminated or not */
67 if(str
[len
- 1] == '\0') {
75 memset(&greg
, 0, sizeof(greg
));
76 const uint8
*cp
= str
;
85 greg
.year
= atoi(szTemp
);
87 /* MONTH - CFGregorianDate ranges 1..12, just like the string */
95 greg
.month
= atoi( szTemp
);
104 greg
.day
= atoi( szTemp
);
112 greg
.hour
= atoi( szTemp
);
120 greg
.minute
= atoi( szTemp
);
128 greg
.second
= atoi( szTemp
);
131 return CFGregorianDateGetAbsoluteTime(greg
, NULL
);
135 * Parse a GeneralizedTime string into a CFAbsoluteTime. Returns NULL on parse error.
136 * Fractional parts of a second are discarded.
138 CFAbsoluteTime
genTimeToCFAbsTime(
139 const CSSM_DATA
*strData
)
141 if((strData
== NULL
) || (strData
->Data
== NULL
) || (strData
->Length
== 0)) {
145 uint8
*timeStr
= strData
->Data
;
146 uint32 timeStrLen
= strData
->Length
;
148 /* tolerate NULL terminated or not */
149 if(timeStr
[timeStrLen
- 1] == '\0') {
153 /* start with a fresh editable copy */
154 uint8
*str
= (uint8
*)malloc(timeStrLen
);
158 * If there is a decimal point, strip it and all trailing digits off
160 const uint8
*inCp
= timeStr
;
162 int foundDecimal
= 0;
163 int minutesOffset
= 0;
165 bool minusOffset
= false;
167 int toGo
= timeStrLen
;
172 /* only legal once */ {
179 /* skip the decimal point... */
186 /* then all subsequent contiguous digits */
187 while(isdigit(*inCp
) && (toGo
!= 0)) {
191 } /* decimal point processing */
192 else if((*inCp
== '+') || (*inCp
== '-')) {
193 /* Time zone offset - handle 2 or 4 chars */
194 if((toGo
!= 2) & (toGo
!= 4)) {
202 hoursOffset
= (10 * (inCp
[0] - '0')) + (inCp
[1] - '0');
205 minutesOffset
= (10 * (inCp
[0] - '0')) + (inCp
[1] - '0');
216 if(str
[strLen
- 1] == 'Z') {
221 CFAbsoluteTime absTime
;
222 absTime
= parseGenTime(str
, strLen
);
224 if(absTime
== NULL_TIME
) {
228 /* post processing needed? */
230 /* Nope, string was in GMT */
233 if((minutesOffset
!= 0) || (hoursOffset
!= 0)) {
234 /* string contained explicit offset from GMT */
236 absTime
-= (minutesOffset
* 60);
237 absTime
-= (hoursOffset
* 3600);
240 absTime
+= (minutesOffset
* 60);
241 absTime
+= (hoursOffset
* 3600);
245 /* implciit offset = local */
246 CFTimeInterval tzDelta
;
247 CFTimeZoneRef localZone
= CFTimeZoneCopySystem();
248 tzDelta
= CFTimeZoneGetSecondsFromGMT (localZone
, CFAbsoluteTimeGetCurrent());
249 CFRelease(localZone
);
256 * Convert CFAbsoluteTime to generalized time string, GMT format (4 digit year,
257 * trailing 'Z'). Caller allocated the output which is GENERAL_TIME_STRLEN+1 bytes.
259 void cfAbsTimeToGgenTime(
260 CFAbsoluteTime absTime
,
263 /* time zone = GMT */
264 CFTimeZoneRef tz
= CFTimeZoneCreateWithTimeIntervalFromGMT(NULL
, 0.0);
265 CFGregorianDate greg
= CFAbsoluteTimeGetGregorianDate(absTime
, tz
);
266 int seconds
= (int)greg
.second
;
267 sprintf(genTime
, "%04d%02d%02d%02d%02d%02dZ",
268 (int)greg
.year
, greg
.month
, greg
.day
, greg
.hour
,
269 greg
.minute
, seconds
);
275 unsigned char *md
) // allocd by caller, CC_SHA1_DIGEST_LENGTH bytes
279 CC_SHA1_Update(&ctx
, data
, len
);
280 CC_SHA1_Final(md
, &ctx
);
286 unsigned char *md
) // allocd by caller, CC_MD5_DIGEST_LENGTH bytes
290 CC_MD5_Update(&ctx
, data
, len
);
291 CC_MD5_Final(md
, &ctx
);
297 unsigned char *md
) // allocd by caller, CC_MD4_DIGEST_LENGTH bytes
301 CC_MD4_Update(&ctx
, data
, len
);
302 CC_MD4_Final(md
, &ctx
);
308 unsigned char *md
) // allocd by caller, CC_SHA256_DIGEST_LENGTH bytes
311 CC_SHA256_Init(&ctx
);
312 CC_SHA256_Update(&ctx
, data
, len
);
313 CC_SHA256_Final(md
, &ctx
);
317 * How many items in a NULL-terminated array of pointers?
319 unsigned ocspdArraySize(