2  * Copyright (c) 2016 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 //  SOSKeyedPubKeyIdentifier.c 
  29 #include "SOSKeyedPubKeyIdentifier.h" 
  30 #include "AssertMacros.h" 
  31 #include "SOSInternal.h" 
  32 #include <utilities/debugging.h> 
  34 #define SEPARATOR CFSTR("-") 
  37 bool SOSKeyedPubKeyIdentifierIsPrefixed(CFStringRef kpkid
) { 
  38     CFRange seploc 
= CFStringFind(kpkid
, SEPARATOR
, 0); 
  39     return seploc
.location 
== SEPLOC
; 
  42 static CFStringRef 
SOSKeyedPubKeyIdentifierCreateWithPrefixAndID(CFStringRef prefix
, CFStringRef id
) { 
  43     CFMutableStringRef retval 
= NULL
; 
  44     require_quiet(prefix
, errOut
); 
  45     require_quiet(id
, errOut
); 
  46     require_quiet(CFStringGetLength(prefix
) == SEPLOC
, errOut
); 
  47     retval 
= CFStringCreateMutableCopy(kCFAllocatorDefault
, 50, prefix
); 
  48     CFStringAppend(retval
, SEPARATOR
); 
  49     CFStringAppend(retval
, id
); 
  54 CFStringRef 
SOSKeyedPubKeyIdentifierCreateWithData(CFStringRef prefix
, CFDataRef pubKeyData
) { 
  55     CFErrorRef localError 
= NULL
; 
  56     CFStringRef id 
= SOSCopyIDOfDataBuffer(pubKeyData
, &localError
); 
  57     CFStringRef retval 
= SOSKeyedPubKeyIdentifierCreateWithPrefixAndID(prefix
, id
); 
  58     if(!id
) secnotice("kpid", "Couldn't create kpid: %@", localError
); 
  60     CFReleaseNull(localError
); 
  64 CFStringRef 
SOSKeyedPubKeyIdentifierCreateWithSecKey(CFStringRef prefix
, SecKeyRef pubKey
) { 
  65     CFErrorRef localError 
= NULL
; 
  66     CFStringRef id 
= SOSCopyIDOfKey(pubKey
, &localError
); 
  67     CFStringRef retval 
= SOSKeyedPubKeyIdentifierCreateWithPrefixAndID(prefix
, id
); 
  68     if(!id
) secnotice("kpid", "Couldn't create kpid: %@", localError
); 
  70     CFReleaseNull(localError
); 
  75 CFStringRef 
SOSKeyedPubKeyIdentifierCopyPrefix(CFStringRef kpkid
) { 
  76     CFRange seploc 
= CFStringFind(kpkid
, SEPARATOR
, 0); 
  77     if(seploc
.location 
!= SEPLOC
) return NULL
; 
  78     CFRange prefloc 
= CFRangeMake(0, SEPLOC
); 
  79     return CFStringCreateWithSubstring(kCFAllocatorDefault
, kpkid
, prefloc
);; 
  82 CFStringRef 
SOSKeyedPubKeyIdentifierCopyHpub(CFStringRef kpkid
) { 
  83     CFRange seploc 
= CFStringFind(kpkid
, SEPARATOR
, 0); 
  84     if(seploc
.location 
!= SEPLOC
) return NULL
; 
  85     CFRange idloc 
= CFRangeMake(seploc
.location
+1, CFStringGetLength(kpkid
) - (SEPLOC
+1)); 
  86     return CFStringCreateWithSubstring(kCFAllocatorDefault
, kpkid
, idloc
);;