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>
33 #include <security_utilities/logging.h>
36 namespace CodeSigning
{
41 RefPointer
<LoadableBundle
> plugin
;
45 ModuleNexus
<PluginHost
> plugin
;
49 // The PluginHost constructor runs under the protection of ModuleNexus's constructor,
50 // so it doesn't have to worry about thread safety and such.
52 PluginHost::PluginHost()
54 if (CFBundleRef securityFramework
= CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security")))
55 if (CFRef
<CFURLRef
> plugins
= CFBundleCopyBuiltInPlugInsURL(securityFramework
))
56 if (CFRef
<CFURLRef
> pluginURL
= makeCFURL("csparser.bundle", true, plugins
)) {
57 plugin
= new LoadableBundle(cfString(pluginURL
).c_str());
59 CODESIGN_LOAD_ANTLR();
60 antlr
= reinterpret_cast<FindAntlrPlugin
*>(plugin
->lookupSymbol(FINDANTLRPLUGIN
))();
64 // can't load plugin - fail
65 Syslog::warning("code signing problem: unable to load csparser plug-in");
66 MacOSError::throwMe(errSecCSInternalError
);
71 // Drive a parsing function through the plugin harness and translate any errors
72 // into a CFError exception.
74 template <class Result
, class Source
>
75 const Result
*parse(Source source
, const Result
*(*AntlrPlugin::*func
)(Source
, string
&))
78 if (const Result
*result
= (plugin().antlr
->*func
)(source
, errors
))
81 CSError::throwMe(errSecCSReqInvalid
, kSecCFErrorRequirementSyntax
, CFTempString(errors
));
86 // Implement the template instances by passing them through the plugin's eye-of-the-needle.
87 // Any other combination of input and output types will cause linker errors.
90 const Requirement
*RequirementParser
<Requirement
>::operator () (std::FILE *source
)
92 return parse(source
, &AntlrPlugin::fileRequirement
);
96 const Requirement
*RequirementParser
<Requirement
>::operator () (const std::string
&source
)
98 return parse(source
, &AntlrPlugin::stringRequirement
);
102 const Requirements
*RequirementParser
<Requirements
>::operator () (std::FILE *source
)
104 return parse(source
, &AntlrPlugin::fileRequirements
);
108 const Requirements
*RequirementParser
<Requirements
>::operator () (const std::string
&source
)
110 return parse(source
, &AntlrPlugin::stringRequirements
);
114 const BlobCore
*RequirementParser
<BlobCore
>::operator () (std::FILE *source
)
116 return parse(source
, &AntlrPlugin::fileGeneric
);
120 const BlobCore
*RequirementParser
<BlobCore
>::operator () (const std::string
&source
)
122 return parse(source
, &AntlrPlugin::stringGeneric
);