]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_codesigning/lib/reqparser.cpp
Security-57337.20.44.tar.gz
[apple/security.git] / OSX / libsecurity_codesigning / lib / reqparser.cpp
1 /*
2 * Copyright (c) 2006-2007,2011 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 // reqparser - interface to Requirement language parser/compiler
26 //
27 #include "reqparser.h"
28 #include "antlrplugin.h"
29 #include "cserror.h"
30 #include "codesigning_dtrace.h"
31 #include <CoreFoundation/CoreFoundation.h>
32 #include <security_utilities/osxcode.h>
33
34 namespace Security {
35 namespace CodeSigning {
36
37
38 struct PluginHost {
39 PluginHost();
40 RefPointer<LoadableBundle> plugin;
41 AntlrPlugin *antlr;
42 };
43
44 ModuleNexus<PluginHost> plugin;
45
46
47 //
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.
50 //
51 PluginHost::PluginHost()
52 {
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());
57 plugin->load();
58 CODESIGN_LOAD_ANTLR();
59 antlr = reinterpret_cast<FindAntlrPlugin *>(plugin->lookupSymbol(FINDANTLRPLUGIN))();
60 return;
61 }
62
63 // can't load plugin - fail
64 MacOSError::throwMe(errSecCSInternalError);
65 }
66
67
68 //
69 // Drive a parsing function through the plugin harness and translate any errors
70 // into a CFError exception.
71 //
72 template <class Result, class Source>
73 const Result *parse(Source source, const Result *(*AntlrPlugin::*func)(Source, string &))
74 {
75 string errors;
76 if (const Result *result = (plugin().antlr->*func)(source, errors))
77 return result;
78 else
79 CSError::throwMe(errSecCSReqInvalid, kSecCFErrorRequirementSyntax, CFTempString(errors));
80 }
81
82
83 //
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.
86 //
87 template <>
88 const Requirement *RequirementParser<Requirement>::operator () (std::FILE *source)
89 {
90 return parse(source, &AntlrPlugin::fileRequirement);
91 }
92
93 template <>
94 const Requirement *RequirementParser<Requirement>::operator () (const std::string &source)
95 {
96 return parse(source, &AntlrPlugin::stringRequirement);
97 }
98
99 template <>
100 const Requirements *RequirementParser<Requirements>::operator () (std::FILE *source)
101 {
102 return parse(source, &AntlrPlugin::fileRequirements);
103 }
104
105 template <>
106 const Requirements *RequirementParser<Requirements>::operator () (const std::string &source)
107 {
108 return parse(source, &AntlrPlugin::stringRequirements);
109 }
110
111 template <>
112 const BlobCore *RequirementParser<BlobCore>::operator () (std::FILE *source)
113 {
114 return parse(source, &AntlrPlugin::fileGeneric);
115 }
116
117 template <>
118 const BlobCore *RequirementParser<BlobCore>::operator () (const std::string &source)
119 {
120 return parse(source, &AntlrPlugin::stringGeneric);
121 }
122
123
124 } // CodeSigning
125 } // Security