/ keychain / SecureObjectSync / SOSKeyedPubKeyIdentifier.c
SOSKeyedPubKeyIdentifier.c
 1  /*
 2   * Copyright (c) 2016 Apple Inc. All Rights Reserved.
 3   *
 4   * @APPLE_LICENSE_HEADER_START@
 5   *
 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
11   * file.
12   *
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.
20   *
21   * @APPLE_LICENSE_HEADER_END@
22   */
23  
24  //
25  //  SOSKeyedPubKeyIdentifier.c
26  //  Security
27  //
28  
29  #include "SOSKeyedPubKeyIdentifier.h"
30  #include "AssertMacros.h"
31  #include "keychain/SecureObjectSync/SOSInternal.h"
32  #include <utilities/debugging.h>
33  
34  #define SEPARATOR CFSTR("-")
35  #define SEPLOC 2
36  
37  bool SOSKeyedPubKeyIdentifierIsPrefixed(CFStringRef kpkid) {
38      CFRange seploc = CFStringFind(kpkid, SEPARATOR, 0);
39      return seploc.location == SEPLOC;
40  }
41  
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);
50  errOut:
51      return retval;
52  }
53  
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);
59      CFReleaseNull(id);
60      CFReleaseNull(localError);
61      return retval;
62  }
63  
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);
69      CFReleaseNull(id);
70      CFReleaseNull(localError);
71      return retval;
72  }
73  
74  
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);;
80  }
81  
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);;
87  }
88