2 * Copyright (c) 2006-2007,2011 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 // reqparser - interface to Requirement language parser/compiler
27 #include "reqparser.h"
28 #include "antlrplugin.h"
30 #include "codesigning_dtrace.h"
31 #include <CoreFoundation/CoreFoundation.h>
32 #include <security_utilities/osxcode.h>
35 namespace CodeSigning
{
40 RefPointer
<LoadableBundle
> plugin
;
44 ModuleNexus
<PluginHost
> plugin
;
48 // The PluginHost constructor runs under the protection of ModuleNexus's constructor,
49 // so it doesn't have to worry about thread safety and such.
51 PluginHost::PluginHost()
53 if (CFBundleRef securityFramework
= CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security")))
54 if (CFRef
<CFURLRef
> plugins
= CFBundleCopyBuiltInPlugInsURL(securityFramework
))
55 if (CFRef
<CFURLRef
> pluginURL
= makeCFURL("csparser.bundle", true, plugins
)) {
56 plugin
= new LoadableBundle(cfString(pluginURL
).c_str());
58 CODESIGN_LOAD_ANTLR();
59 antlr
= reinterpret_cast<FindAntlrPlugin
*>(plugin
->lookupSymbol(FINDANTLRPLUGIN
))();
63 // can't load plugin - fail
64 MacOSError::throwMe(errSecCSInternalError
);
69 // Drive a parsing function through the plugin harness and translate any errors
70 // into a CFError exception.
72 template <class Result
, class Source
>
73 const Result
*parse(Source source
, const Result
*(*AntlrPlugin::*func
)(Source
, string
&))
76 if (const Result
*result
= (plugin().antlr
->*func
)(source
, errors
))
79 CSError::throwMe(errSecCSReqInvalid
, kSecCFErrorRequirementSyntax
, CFTempString(errors
));
84 // Implement the template instances by passing them through the plugin's eye-of-the-needle.
85 // Any other combination of input and output types will cause linker errors.
88 const Requirement
*RequirementParser
<Requirement
>::operator () (std::FILE *source
)
90 return parse(source
, &AntlrPlugin::fileRequirement
);
94 const Requirement
*RequirementParser
<Requirement
>::operator () (const std::string
&source
)
96 return parse(source
, &AntlrPlugin::stringRequirement
);
100 const Requirements
*RequirementParser
<Requirements
>::operator () (std::FILE *source
)
102 return parse(source
, &AntlrPlugin::fileRequirements
);
106 const Requirements
*RequirementParser
<Requirements
>::operator () (const std::string
&source
)
108 return parse(source
, &AntlrPlugin::stringRequirements
);
112 const BlobCore
*RequirementParser
<BlobCore
>::operator () (std::FILE *source
)
114 return parse(source
, &AntlrPlugin::fileGeneric
);
118 const BlobCore
*RequirementParser
<BlobCore
>::operator () (const std::string
&source
)
120 return parse(source
, &AntlrPlugin::stringGeneric
);