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 "ocspdDebug.h"
30 #include <Security/cssmerr.h>
31 #include <Security/keyTemplates.h>
32 #include <CoreFoundation/CoreFoundation.h>
35 * Compare two CSSM_DATAs, return CSSM_TRUE if identical.
37 CSSM_BOOL
ocspdCompareCssmData(
38 const CSSM_DATA
*data1
,
39 const CSSM_DATA
*data2
)
41 if((data1
== NULL
) || (data1
->Data
== NULL
) ||
42 (data2
== NULL
) || (data2
->Data
== NULL
) ||
43 (data1
->Length
!= data2
->Length
)) {
46 if(data1
->Length
!= data2
->Length
) {
49 if(memcmp(data1
->Data
, data2
->Data
, data1
->Length
) == 0) {
58 * Convert a generalized time string, with a 4-digit year and no trailing
59 * fractional seconds or time zone info, to a CFAbsoluteTime. Returns
60 * NULL_TIME (0.0) on error.
62 static CFAbsoluteTime
parseGenTime(
66 if((str
== NULL
) || (len
== 0)) {
70 /* tolerate NULL terminated or not */
71 if(str
[len
- 1] == '\0') {
79 memset(&greg
, 0, sizeof(greg
));
80 const uint8
*cp
= str
;
89 greg
.year
= atoi(szTemp
);
91 /* MONTH - CFGregorianDate ranges 1..12, just like the string */
99 greg
.month
= atoi( szTemp
);
108 greg
.day
= atoi( szTemp
);
116 greg
.hour
= atoi( szTemp
);
124 greg
.minute
= atoi( szTemp
);
132 greg
.second
= atoi( szTemp
);
135 return CFGregorianDateGetAbsoluteTime(greg
, NULL
);
139 * Parse a GeneralizedTime string into a CFAbsoluteTime. Returns NULL on parse error.
140 * Fractional parts of a second are discarded.
142 CFAbsoluteTime
genTimeToCFAbsTime(
143 const CSSM_DATA
*strData
)
145 if((strData
== NULL
) || (strData
->Data
== NULL
) || (strData
->Length
== 0)) {
149 uint8
*timeStr
= strData
->Data
;
150 size_t timeStrLen
= strData
->Length
;
152 /* tolerate NULL terminated or not */
153 if(timeStr
[timeStrLen
- 1] == '\0') {
157 /* start with a fresh editable copy */
158 uint8
*str
= (uint8
*)malloc(timeStrLen
);
162 * If there is a decimal point, strip it and all trailing digits off
164 const uint8
*inCp
= timeStr
;
166 int foundDecimal
= 0;
167 int minutesOffset
= 0;
169 bool minusOffset
= false;
171 size_t toGo
= timeStrLen
;
176 /* only legal once */ {
183 /* skip the decimal point... */
190 /* then all subsequent contiguous digits */
191 while(isdigit(*inCp
) && (toGo
!= 0)) {
195 } /* decimal point processing */
196 else if((*inCp
== '+') || (*inCp
== '-')) {
197 /* Time zone offset - handle 2 or 4 chars */
198 if((toGo
!= 2) & (toGo
!= 4)) {
206 hoursOffset
= (10 * (inCp
[0] - '0')) + (inCp
[1] - '0');
209 minutesOffset
= (10 * (inCp
[0] - '0')) + (inCp
[1] - '0');
220 if(str
[strLen
- 1] == 'Z') {
225 CFAbsoluteTime absTime
;
226 absTime
= parseGenTime(str
, strLen
);
228 if(absTime
== NULL_TIME
) {
232 /* post processing needed? */
234 /* Nope, string was in GMT */
237 if((minutesOffset
!= 0) || (hoursOffset
!= 0)) {
238 /* string contained explicit offset from GMT */
240 absTime
-= (minutesOffset
* 60);
241 absTime
-= (hoursOffset
* 3600);
244 absTime
+= (minutesOffset
* 60);
245 absTime
+= (hoursOffset
* 3600);
249 /* implciit offset = local */
250 CFTimeInterval tzDelta
;
251 CFTimeZoneRef localZone
= CFTimeZoneCopySystem();
252 tzDelta
= CFTimeZoneGetSecondsFromGMT (localZone
, CFAbsoluteTimeGetCurrent());
253 CFRelease(localZone
);
260 * Convert CFAbsoluteTime to generalized time string, GMT format (4 digit year,
261 * trailing 'Z'). Caller allocated the output which is GENERAL_TIME_STRLEN+1 bytes.
263 void cfAbsTimeToGgenTime(
264 CFAbsoluteTime absTime
,
267 /* time zone = GMT */
268 CFTimeZoneRef tz
= CFTimeZoneCreateWithTimeIntervalFromGMT(NULL
, 0.0);
269 CFGregorianDate greg
= CFAbsoluteTimeGetGregorianDate(absTime
, tz
);
270 int seconds
= (int)greg
.second
;
271 sprintf(genTime
, "%04d%02d%02d%02d%02d%02dZ",
272 (int)greg
.year
, greg
.month
, greg
.day
, greg
.hour
,
273 greg
.minute
, seconds
);
279 unsigned char *md
) // allocd by caller, CC_SHA1_DIGEST_LENGTH bytes
283 CC_SHA1_Update(&ctx
, data
, len
);
284 CC_SHA1_Final(md
, &ctx
);
290 unsigned char *md
) // allocd by caller, CC_MD5_DIGEST_LENGTH bytes
294 CC_MD5_Update(&ctx
, data
, len
);
295 CC_MD5_Final(md
, &ctx
);
301 unsigned char *md
) // allocd by caller, CC_MD4_DIGEST_LENGTH bytes
305 CC_MD4_Update(&ctx
, data
, len
);
306 CC_MD4_Final(md
, &ctx
);
312 unsigned char *md
) // allocd by caller, CC_SHA256_DIGEST_LENGTH bytes
315 CC_SHA256_Init(&ctx
);
316 CC_SHA256_Update(&ctx
, data
, len
);
317 CC_SHA256_Final(md
, &ctx
);
321 * How many items in a NULL-terminated array of pointers?
323 unsigned ocspdArraySize(
335 /* Fill out a CSSM_DATA with the subset of public key bytes from the given
336 * CSSM_KEY_PTR which should be hashed to produce the issuerKeyHash field
337 * of a CertID in an OCSP request.
339 * For RSA keys, this simply copies the input key pointer and length.
340 * For EC keys, we need to further deconstruct the SubjectPublicKeyInfo
341 * to obtain the key bytes (i.e. curve point) for hashing.
343 * Returns CSSM_OK on success, or non-zero error if the bytes could not
346 CSSM_RETURN
ocspdGetPublicKeyBytes(
347 SecAsn1CoderRef coder
, // optional
348 CSSM_KEY_PTR publicKey
, // input public key
349 CSSM_DATA
&publicKeyBytes
) // filled in by this function
351 CSSM_RETURN crtn
= CSSM_OK
;
352 SecAsn1CoderRef _coder
= NULL
;
354 if(publicKey
== NULL
) {
355 crtn
= CSSMERR_CSP_INVALID_KEY_POINTER
;
360 crtn
= SecAsn1CoderCreate(&_coder
);
367 publicKeyBytes
.Length
= publicKey
->KeyData
.Length
;
368 publicKeyBytes
.Data
= publicKey
->KeyData
.Data
;
370 if(publicKey
->KeyHeader
.AlgorithmId
== CSSM_ALGID_ECDSA
) {
372 * For an EC key, publicKey->KeyData is a SubjectPublicKeyInfo
373 * ASN.1 sequence that includes the algorithm identifier.
374 * We only want to return the bit string portion of the key here.
376 SecAsn1PubKeyInfo pkinfo
;
377 memset(&pkinfo
, 0, sizeof(pkinfo
));
378 if(SecAsn1Decode(coder
,
379 publicKey
->KeyData
.Data
,
380 publicKey
->KeyData
.Length
,
381 kSecAsn1SubjectPublicKeyInfoTemplate
,
383 if(pkinfo
.subjectPublicKey
.Length
&&
384 pkinfo
.subjectPublicKey
.Data
) {
385 publicKeyBytes
.Length
= pkinfo
.subjectPublicKey
.Length
>> 3;
386 publicKeyBytes
.Data
= pkinfo
.subjectPublicKey
.Data
;
388 * Important: if we allocated the SecAsn1Coder, the memory
389 * being pointed to by pkinfo.subjectPublicKey.Data will be
390 * deallocated when the coder is released below. We want to
391 * point to the identical data inside the caller's public key,
392 * now that the decoder has identified it for us.
394 if(publicKeyBytes
.Length
<= publicKey
->KeyData
.Length
) {
395 publicKeyBytes
.Data
= (uint8
*)((uintptr_t)publicKey
->KeyData
.Data
+
396 (publicKey
->KeyData
.Length
- publicKeyBytes
.Length
));
399 /* intentional fallthrough to error exit */
401 ocspdErrorLog("ocspdGetPublicKeyBytes: invalid SecAsn1PubKeyInfo\n");
402 crtn
= CSSMERR_CSP_INVALID_KEY_POINTER
;
405 /* Unable to decode using kSecAsn1SubjectPublicKeyInfoTemplate.
406 * This may or may not be an error; just return the unchanged key.
408 ocspdErrorLog("ocspdGetPublicKeyBytes: unable to decode SubjectPublicKeyInfo\n");
414 SecAsn1CoderRelease(_coder
);