2  * Copyright (c) 2001-2002,2004,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  *  AuthorizationPlugin.h 
  27  *  AuthorizationPlugin -- APIs for implementing authorization plugins. 
  30 #ifndef _SECURITY_AUTHORIZATIONPLUGIN_H_ 
  31 #define _SECURITY_AUTHORIZATIONPLUGIN_H_ 
  33 #include <Security/Authorization.h> 
  35 #if defined(__cplusplus) 
  39 CF_ASSUME_NONNULL_BEGIN
 
  42         @header AuthorizationPlugin 
  44         The AuthorizationPlugin API allows the creation of plugins that can participate 
  45         in authorization decisions.  Using the AuthorizationDB API the system can be configured 
  46         to use these plugins.  Plugins are loaded into a separate process, the pluginhost, to  
  47         isolate the process of authorization from the client.  There are two types of pluginhosts. 
  48         One runs as an anonymous user and can be used to communicate with the user, for example 
  49         to ask for a password.  Another one runs with root privileges to perform privileged 
  50         operations that may be required. 
  52     A typical use is to implement additional policies that cannot be expressed in the 
  53     authorization configuration. 
  55     Plugins implement a handshake function called AuthorizationPluginCreate with which 
  56     their interface (AuthorizationPluginInterface) and the engine's interface 
  57     (AuthorizationCallbacks) are exchanged.  Plugins are asked to create  
  58     Mechanisms, which are the basic element as authorizations are performed.   
  60     Mechanisms are invoked when it is time for them to make a decision.  A decision is  
  61     made by setting a single result (AuthorizationResult).  Mechanisms in the  
  62     authorization can communicate auxiliary information by setting and/or getting hints  
  63     and setting and/or getting context data.  Hints are advisory and don't need to be 
  64     looked at, nor are they preserved as part of the authorization result. Context data 
  65     becomes part of the result of the authorization. 
  67     Context data is tagged with a flag that describes whether the information is returned 
  68     to the authorization client upon request (AuthorizationCopyInfo() in Authorization.h) 
  69     or whether it's private to the mechanisms making a decision. 
  75         @typedef AuthorizationValue 
  76     Auxiliary data is passed between the engine and the mechanism as AuthorizationValues 
  78 typedef struct AuthorizationValue
 
  81     void * __nullable data
; 
  85     @typedef AuthorizationValueVector 
  86     A vector of AuthorizationValues.  Used to communicate arguments passed from the  
  87     configuration file <code>authorization(5)</code>. 
  89 typedef struct AuthorizationValueVector
 
  92         AuthorizationValue 
*values
; 
  93 } AuthorizationValueVector
; 
  97     Data produced as context during the authorization evaluation is tagged.   
  98     If data is set to be extractable (kAuthorizationContextFlagExtractable), it will be possible for the client of authorization to obtain the value of this attribute using AuthorizationCopyInfo(). 
  99     If data is marked as volatile (kAuthorizationContextFlagVolatile), this value will not be remembered in the AuthorizationRef. 
 100     Sticky data (kAuthorizationContextFlagSticky) persists through a failed or interrupted evaluation. It can be used to propagate an error condition from a downstream plugin to an upstream one. It is not remembered in the AuthorizationRef. 
 102 typedef CF_OPTIONS(UInt32
, AuthorizationContextFlags
) 
 104     kAuthorizationContextFlagExtractable 
= (1 << 0), 
 105     kAuthorizationContextFlagVolatile 
= (1 << 1), 
 106     kAuthorizationContextFlagSticky 
= (1 << 2) 
 111         @typedef AuthorizationMechanismId 
 112     The mechanism id specified in the configuration is passed to the plugin to create the appropriate mechanism. 
 114 typedef const AuthorizationString AuthorizationMechanismId
; 
 117     @typedef AuthorizationPluginId 
 118         Not used by plugin writers.  Loaded plugins are identified by their name. 
 120 typedef const AuthorizationString AuthorizationPluginId
; 
 123         @typedef AuthorizationPluginRef 
 124         Handle passed back by the plugin writer when creating a plugin.  Any pluginhost will only instantiate one instance.  The handle is used when creating mechanisms. 
 126 typedef void *AuthorizationPluginRef
; 
 129         @typedef AuthorizationMechanismRef 
 130         Handle passed back by the plugin writer when creating an an instance of a mechanism in a plugin.  One instance will be created for any authorization. 
 132 typedef void *AuthorizationMechanismRef
; 
 135         @typedef AuthorizationEngineRef 
 136         Handle passed from the engine to an instance of a mechanism in a plugin (corresponds to a particular AuthorizationMechanismRef). 
 138 typedef struct __OpaqueAuthorizationEngine 
*AuthorizationEngineRef
; 
 141         @typedef AuthorizationSessionId 
 142         A unique value for an AuthorizationSession being evaluated, provided by the authorization engine. 
 143     A session is represented by a top level call to an Authorization API. 
 145 typedef void *AuthorizationSessionId
; 
 148     @enum AuthorizationResult 
 149         Possible values for SetResult() in AuthorizationCallbacks. 
 151     @constant kAuthorizationResultAllow the operation succeeded and authorization should be granted as far as this mechanism is concerned. 
 152     @constant kAuthorizationResultDeny the operation succeeded but authorization should be denied as far as this mechanism is concerned. 
 153     @constant kAuthorizationResultUndefined the operation failed for some reason and should not be retried for this session. 
 154     @constant kAuthorizationResultUserCanceled the user has requested that the evaluation be terminated. 
 156 typedef CF_ENUM(UInt32
, AuthorizationResult
) { 
 157     kAuthorizationResultAllow
, 
 158     kAuthorizationResultDeny
, 
 159     kAuthorizationResultUndefined
, 
 160     kAuthorizationResultUserCanceled
, 
 165     Version of the interface (AuthorizationPluginInterface) implemented by the plugin. 
 166     The value is matched to the definition in this file. 
 169     kAuthorizationPluginInterfaceVersion 
= 0 
 174     Version of the callback structure (AuthorizationCallbacks) passed to the plugin. 
 175     The value is matched to the definition in this file.  The engine may provide a newer 
 179     kAuthorizationCallbacksVersion 
= 3 
 185     Callback API provided by the AuthorizationEngine.  
 187     @field version      Engine callback version. 
 188     @field SetResult    Set a result after a call to AuthorizationSessionInvoke. 
 189     @field RequestInterrupt Request authorization engine to interrupt all mechamisms invoked after this mechamism has called SessionSetResult and then call AuthorizationSessionInvoke again. 
 190     @field DidDeactivate    Respond to the Deactivate request. 
 191     @field GetContextValue  Read value from context.  AuthorizationValue does not own data. 
 192     @field SetContextValue  Write value to context.  AuthorizationValue and data are copied. 
 193     @field GetHintValue     Read value from hints. AuthorizationValue does not own data. 
 194     @field SetHintValue     Write value to hints.  AuthorizationValue and data are copied. 
 195     @field GetArguments     Read arguments passed.  AuthorizationValueVector does not own data. 
 196     @field GetSessionId     Read SessionId. 
 197     @field GetLAContext     Returns LAContext which will have LACredentialCTKPIN credential set if PIN is available otherwise context without credentials is returned. LAContext can be used for operations with Tokens which would normally require PIN. Caller owns returned context and is responsible for release. 
 198     @field GetTokenIdentities   Returns array of identities. Caller owns returned array and is reponsible for release. 
 199     @field GetTKTokenWatcher    Returns TKTokenWatcher object. Caller owns returned context and is responsible for release. 
 201 typedef struct AuthorizationCallbacks 
{ 
 203     /* Engine callback version. */ 
 206     /* Set a result after a call to AuthorizationSessionInvoke. */ 
 207     OSStatus (*SetResult
)(AuthorizationEngineRef inEngine
, AuthorizationResult inResult
); 
 209     /* Request authorization engine to interrupt all mechamisms invoked after  
 210         this mechamism has called SessionSetResult and then call  
 211         AuthorizationSessionInvoke again. */ 
 212     OSStatus (*RequestInterrupt
)(AuthorizationEngineRef inEngine
); 
 214     /* Respond to the Deactivate request. */ 
 215     OSStatus (*DidDeactivate
)(AuthorizationEngineRef inEngine
); 
 217     /* Read value from context.  AuthorizationValue does not own data. */ 
 218     OSStatus (*GetContextValue
)(AuthorizationEngineRef inEngine
, 
 219         AuthorizationString inKey
, 
 220         AuthorizationContextFlags 
* __nullable outContextFlags
, 
 221         const AuthorizationValue 
* __nullable 
* __nullable outValue
); 
 223     /* Write value to context.  AuthorizationValue and data are copied. */ 
 224     OSStatus (*SetContextValue
)(AuthorizationEngineRef inEngine
, 
 225         AuthorizationString inKey
, 
 226         AuthorizationContextFlags inContextFlags
, 
 227         const AuthorizationValue 
*inValue
); 
 229     /* Read value from hints. AuthorizationValue does not own data. */ 
 230     OSStatus (*GetHintValue
)(AuthorizationEngineRef inEngine
, 
 231         AuthorizationString inKey
, 
 232         const AuthorizationValue 
* __nullable 
* __nullable outValue
); 
 234     /* Write value to hints.  AuthorizationValue and data are copied. */ 
 235     OSStatus (*SetHintValue
)(AuthorizationEngineRef inEngine
, 
 236         AuthorizationString inKey
, 
 237         const AuthorizationValue 
*inValue
); 
 239     /* Read arguments passed.  AuthorizationValueVector does not own data. */ 
 240     OSStatus (*GetArguments
)(AuthorizationEngineRef inEngine
, 
 241         const AuthorizationValueVector 
* __nullable 
* __nonnull outArguments
); 
 243     /* Read SessionId. */ 
 244     OSStatus (*GetSessionId
)(AuthorizationEngineRef inEngine
, 
 245         AuthorizationSessionId __nullable 
* __nullable outSessionId
); 
 247     /* Read value from hints. AuthorizationValue does not own data. */ 
 248     OSStatus (*GetImmutableHintValue
)(AuthorizationEngineRef inEngine
, 
 249         AuthorizationString inKey
, 
 250         const AuthorizationValue 
* __nullable 
* __nullable outValue
); 
 253          Available only on systems with callback version 2 or higher 
 254          Constructs LAContext object based od actual user credentials, 
 255          userful for kSecUseAuthenticationContext for SecItem calls. 
 256      Caller is responsible for outValue release  */ 
 257     OSStatus (*GetLAContext
)(AuthorizationEngineRef inEngine
, 
 258         CFTypeRef __nullable 
* __nullable outValue
) __OSX_AVAILABLE_STARTING(__MAC_10_13
, __PHONE_NA
); 
 261          Available only on systems with callback version 2 or higher 
 262          Returns array of available identities available on tokens. Each array item consists of two 
 263      elements. The first one is SecIdentityRef and the second one is textual description of that identity 
 264          context parameter may contain CFTypeRef returned by GetLAContext. 
 265      Caller is responsible for outValue release */ 
 266     OSStatus (*GetTokenIdentities
)(AuthorizationEngineRef inEngine
, 
 268         CFArrayRef __nullable 
* __nullable outValue
) __OSX_AVAILABLE_STARTING(__MAC_10_13
, __PHONE_NA
); 
 271      Available only on systems with callback version 3 or higher 
 272      Constructs TKTokenWatcher object. 
 273      Caller is responsible for outValue release     */ 
 274     OSStatus (*GetTKTokenWatcher
)(AuthorizationEngineRef inEngine
, 
 275         CFTypeRef __nullable 
* __nullable outValue
)  __OSX_AVAILABLE_STARTING(__MAC_10_13_4
, __PHONE_NA
); 
 277 } AuthorizationCallbacks
; 
 282     Interface that must be implemented by each plugin.  
 284     @field version  Must be set to kAuthorizationPluginInterfaceVersion 
 285     @field PluginDestroy    Plugin should clean up and release any resources it is holding. 
 286     @field MechanismCreate  The plugin should create a mechanism named mechanismId.  The mechanism needs to use the AuthorizationEngineRef for the callbacks and pass back a   AuthorizationMechanismRef for itself.  MechanismDestroy will be called when it is no longer needed. 
 287     @field MechanismInvoke  Invoke an instance of a mechanism.  It should call SetResult during or after returning from this function. 
 288     @field MechanismDeactivate  Mechanism should respond with a DidDeactivate as soon as possible 
 289     @field MechanismDestroy Mechanism should clean up and release any resources it is holding 
 291 typedef struct AuthorizationPluginInterface
 
 293     /* Must be set to kAuthorizationPluginInterfaceVersion. */ 
 296     /* Notify a plugin that it is about to be unloaded so it get a chance to clean up and release any resources it is holding.  */ 
 297     OSStatus (*PluginDestroy
)(AuthorizationPluginRef inPlugin
); 
 299     /* The plugin should create a mechanism named mechanismId.  The mechanism needs to use the 
 300         AuthorizationEngineRef for the callbacks and pass back an AuthorizationMechanismRef for 
 301         itself.  MechanismDestroy will be called when it is no longer needed. */ 
 302     OSStatus (*MechanismCreate
)(AuthorizationPluginRef inPlugin
, 
 303         AuthorizationEngineRef inEngine
, 
 304         AuthorizationMechanismId mechanismId
, 
 305         AuthorizationMechanismRef __nullable 
* __nonnull outMechanism
); 
 307     /* Invoke an instance of a mechanism.  It should call SetResult during or after returning from this function.  */ 
 308     OSStatus (*MechanismInvoke
)(AuthorizationMechanismRef inMechanism
); 
 310     /* Mechanism should respond with a DidDeactivate as soon as possible. */ 
 311     OSStatus (*MechanismDeactivate
)(AuthorizationMechanismRef inMechanism
); 
 313     /* Mechanism should clean up and release any resources it is holding. */ 
 314     OSStatus (*MechanismDestroy
)(AuthorizationMechanismRef inMechanism
); 
 316 } AuthorizationPluginInterface
; 
 320     @function AuthorizationPluginCreate 
 322     Initialize a plugin after it gets loaded.  This is the main entry point to a plugin.  This function will only be called once.   
 323     After all Mechanism instances have been destroyed outPluginInterface->PluginDestroy will be called. 
 325     @param callbacks (input) A pointer to an AuthorizationCallbacks which contains the callbacks implemented by the AuthorizationEngine. 
 326     @param outPlugin (output) On successful completion should contain a valid AuthorizationPluginRef.  This will be passed in to any subsequent calls the engine makes to  outPluginInterface->MechanismCreate and outPluginInterface->PluginDestroy. 
 327     @param outPluginInterface (output) On successful completion should contain a pointer to a AuthorizationPluginInterface that will stay valid until outPluginInterface->PluginDestroy is called. */ 
 328 OSStatus 
AuthorizationPluginCreate(const AuthorizationCallbacks 
*callbacks
, 
 329     AuthorizationPluginRef __nullable 
* __nonnull outPlugin
, 
 330     const AuthorizationPluginInterface 
* __nullable 
* __nonnull outPluginInterface
); 
 332 CF_ASSUME_NONNULL_END
 
 334 #if defined(__cplusplus) 
 338 #endif /* _SECURITY_AUTHORIZATIONPLUGIN_H_ */