2  * Copyright (c) 2000-2006,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@ 
  26 // Miscellaneous CSSM PODWrappers 
  28 #include <security_cdsa_utilities/cssmpods.h> 
  29 #include <security_cdsa_utilities/cssmbridge.h> 
  30 #include <security_utilities/endian.h> 
  33 // GUID <-> string conversions. 
  34 // Note that we DO check for {} on input and insist on rigid formatting. 
  35 // We don't require a terminating null byte on input, but generate it on output. 
  37 char *Guid::toString(char buffer
[stringRepLength
+1]) const 
  39     sprintf(buffer
, "{%8.8x-%4.4hx-%4.4hx-", 
  40             int(n2h(Data1
)), n2h(Data2
), n2h(Data3
)); 
  41     for (int n 
= 0; n 
< 2; n
++) 
  42         sprintf(buffer 
+ 20 + 2*n
, "%2.2hhx", Data4
[n
]); 
  44     for (int n 
= 2; n 
< 8; n
++) 
  45         sprintf(buffer 
+ 21 + 2*n
, "%2.2hhx", Data4
[n
]); 
  51 string 
Guid::toString() const 
  53         char buffer
[stringRepLength
+1]; 
  54         return toString(buffer
); 
  57 Guid::Guid(const char *s
) 
  62 Guid::Guid(const string 
&s
) 
  67 void Guid::parseGuid(const char *string
) 
  69         // Arguably, we should be more flexible on input. But exactly what 
  70         // padding rules should we follow, and how should we try to interprete 
  71         // "doubtful" variations? Given that GUIDs are essentially magic 
  72         // cookies, everybody's better off if we just cut-and-paste them 
  73         // around the universe... 
  75         // do sanity checking, don't assume that what's passed in makes sense 
  78         CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
); 
  81         // what follows had better be big enough 
  82         if (strlen(string
) < 37) // needed because the code hard codes the length 
  84         CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
); 
  89     if (sscanf(string
, "{%8x-%4hx-%4hx-", &d1
, &d2
, &d3
) != 3) 
  90         CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
); 
  91         Data1 
= h2n(uint32(d1
)); 
  94         // once, we did not expect the - after byte 2 of Data4 
  95         bool newForm 
= string
[24] == '-'; 
  96     for (int n 
= 0; n 
< 8; n
++) { 
  98         if (sscanf(string 
+ 20 + 2*n 
+ (newForm 
&& n 
>= 2), "%2hhx", &dn
) != 1) 
  99             CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
); 
 102         if (string
[37 - !newForm
] != '}') 
 103                 CssmError::throwMe(CSSM_ERRCODE_INVALID_GUID
); 
 107 CssmGuidData::CssmGuidData(const CSSM_GUID 
&guid
) : CssmData(buffer
, sizeof(buffer
)) 
 109         Guid::overlay(guid
).toString(buffer
); 
 114 // CssmSubserviceUids. 
 115 // Note that for comparison, we ignore the version field. 
 116 // This is not necessarily the Right Choice, but suits certain 
 117 // constraints in the Sec* layer. Perhaps we might reconsider 
 118 // this after a thorough code review to determine the intended 
 119 // (by the standard) semantics and proper use. Yeah, right. 
 121 CssmSubserviceUid::CssmSubserviceUid(const CSSM_GUID 
&guid
, 
 122         const CSSM_VERSION 
*version
, uint32 subserviceId
, CSSM_SERVICE_TYPE subserviceType
) 
 125         SubserviceId 
= subserviceId
; 
 126         SubserviceType 
= subserviceType
; 
 130                 Version
.Major 
= Version
.Minor 
= 0; 
 134 bool CssmSubserviceUid::operator == (const CSSM_SUBSERVICE_UID 
&otherUid
) const 
 136     // make sure we don't crash if we get bad data 
 137 #pragma clang diagnostic push 
 138 #pragma clang diagnostic ignored "-Wtautological-undefined-compare" 
 139     if (&otherUid 
== 0x0) { return false; } 
 140 #pragma clang diagnostic pop 
 142         const CssmSubserviceUid 
&other 
= CssmSubserviceUid::overlay(otherUid
); 
 143         return subserviceId() == other
.subserviceId() 
 144                 && subserviceType() == other
.subserviceType() 
 145                 && guid() == other
.guid(); 
 148 bool CssmSubserviceUid::operator < (const CSSM_SUBSERVICE_UID 
&otherUid
) const 
 150 #pragma clang diagnostic push 
 151 #pragma clang diagnostic ignored "-Wtautological-undefined-compare" 
 152     if (&otherUid 
== 0x0) { return false; } 
 153 #pragma clang diagnostic pop 
 155         const CssmSubserviceUid 
&other 
= CssmSubserviceUid::overlay(otherUid
); 
 156         if (subserviceId() < other
.subserviceId()) 
 158         if (subserviceId() > other
.subserviceId()) 
 160         if (subserviceType() < other
.subserviceType()) 
 162         if (subserviceType() > other
.subserviceType()) 
 164         return guid() < other
.guid(); 
 169 // CryptoData & friends 
 171 CryptoDataClass::~CryptoDataClass() 
 174 CSSM_RETURN 
CryptoDataClass::callbackShim(CSSM_DATA 
*output
, void *ctx
) 
 177         *output 
= reinterpret_cast<CryptoDataClass 
*>(ctx
)->yield();