]>
git.saurik.com Git - apple/security.git/blob - libsecurity_codesigning/lib/reqdumper.cpp
2 * Copyright (c) 2006-2007 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 // reqdumper - Requirement un-parsing (disassembly)
27 #include "reqdumper.h"
28 #include <security_cdsa_utilities/cssmdata.h> // OID encoder
32 namespace CodeSigning
{
34 using namespace UnixPlusPlus
;
38 // Table of reserved words (keywords), generated by ANTLR
40 static const char * const keywords
[] = {
41 #include "RequirementKeywords.h"
48 // Printf to established output channel
50 void Dumper::print(const char *format
, ...)
54 va_start(args
, format
);
55 vsnprintf(buffer
, sizeof(buffer
), format
, args
);
62 // Dump the underlying Requirement program
68 // remove any initial space
69 if (mOutput
[0] == ' ')
70 mOutput
= mOutput
.substr(1);
75 // Dump an entire Requirements set, using temporary Dumper objects.
77 // This detects single Requirement inputs and dumps them successfully (using
78 // single-requirement syntax). No indication of error is returned in this case.
80 string
Dumper::dump(const Requirements
*reqs
, bool debug
/* = false */)
83 return "# no requirement(s)";
84 } else if (reqs
->magic() == Requirement::typeMagic
) { // single requirement
85 return dump((const Requirement
*)reqs
) + "\n";
88 for (unsigned n
= 0; n
< reqs
->count(); n
++) {
90 if (reqs
->type(n
) < kSecRequirementTypeCount
)
91 snprintf(prefix
, sizeof(prefix
),
92 "%s => ", Requirement::typeNames
[reqs
->type(n
)]);
94 snprintf(prefix
, sizeof(prefix
), "/*unknown type*/ %d => ", reqs
->type(n
));
95 Dumper
dumper(reqs
->blob
<Requirement
>(n
), debug
);
97 result
+= prefix
+ dumper
.value() + "\n";
103 string
Dumper::dump(const Requirement
*req
, bool debug
/* = false */)
105 Dumper
dumper(req
, debug
);
109 } catch (const CommonError
&err
) {
112 snprintf(errstr
, sizeof(errstr
), " !! error %ld !!", (unsigned long)err
.osStatus());
113 return dumper
.value() + errstr
;
119 string
Dumper::dump(const BlobCore
*req
, bool debug
/* = false */)
121 switch (req
->magic()) {
122 case Requirement::typeMagic
:
123 return dump(static_cast<const Requirement
*>(req
), debug
);
125 case Requirements::typeMagic
:
126 return dump(static_cast<const Requirements
*>(req
), debug
);
129 return "invalid data type";
135 // Element dumpers. Output accumulates in internal buffer.
137 void Dumper::expr(SyntaxLevel level
)
140 print("/*@0x%x*/", pc());
141 ExprOp op
= ExprOp(get
<uint32_t>());
142 switch (op
& ~opFlagMask
) {
150 print("identifier ");
154 print("anchor apple");
156 case opAppleGenericAnchor
:
157 print("anchor apple generic");
160 print("certificate"); certSlot(); print(" = "); hashData();
165 print("info["); dotString(); print("] = "); data();
194 print("info["); dotString(); print("]"); match();
196 case opEntitlementField
:
197 print("entitlement["); dotString(); print("]"); match();
200 print("certificate"); certSlot(); print("["); dotString(); print("]"); match();
203 print("certificate"); certSlot(); print("[");
205 const unsigned char *data
; size_t length
;
206 getData(data
, length
);
207 print("field.%s", CssmOid((unsigned char *)data
, length
).toOid().c_str());
212 print("certificate"); certSlot(); print("[");
214 const unsigned char *data
; size_t length
;
215 getData(data
, length
);
216 print("policy.%s", CssmOid((unsigned char *)data
, length
).toOid().c_str());
221 print("certificate"); certSlot(); print("trusted");
224 print("anchor trusted");
227 print("anchor apple "); data();
230 print("("); data(); print(")");
233 if (op
& opGenericFalse
) {
234 print(" false /* opcode %d */", op
& ~opFlagMask
);
236 } else if (op
& opGenericSkip
) {
237 print(" /* opcode %d */", op
& ~opFlagMask
);
240 print("OPCODE %d NOT UNDERSTOOD (ending print)", op
);
246 void Dumper::certSlot()
248 switch (int32_t slot
= get
<int32_t>()) {
249 case Requirement::anchorCert
:
252 case Requirement::leafCert
:
263 switch (MatchOperation op
= MatchOperation(get
<uint32_t>())) {
265 print(" /* exists */");
268 print(" = "); data();
271 print(" ~ "); data();
273 case matchBeginsWith
:
274 print(" = "); data(); print("*");
277 print(" = *"); data();
280 print(" < "); data();
282 case matchGreaterEqual
:
283 print(" >= "); data();
286 print(" <= "); data();
288 case matchGreaterThan
:
289 print(" > "); data();
292 print("MATCH OPCODE %d NOT UNDERSTOOD", op
);
297 void Dumper::hashData()
300 const unsigned char *data
; size_t length
;
301 getData(data
, length
);
302 printBytes(data
, length
);
306 void Dumper::data(PrintMode bestMode
/* = isSimple */, bool dotOkay
/* = false */)
308 const unsigned char *data
; size_t length
;
309 getData(data
, length
);
310 for (unsigned n
= 0; n
< length
; n
++)
311 if ((isalnum(data
[n
]) || (data
[n
] == '.' && dotOkay
))) { // simple
312 if (n
== 0 && isdigit(data
[n
])) // unquoted idents can't start with a digit
313 bestMode
= isPrintable
;
314 } else if (isgraph(data
[n
]) || isspace(data
[n
])) {
315 if (bestMode
== isSimple
)
316 bestMode
= isPrintable
;
322 if (bestMode
== isSimple
) {
323 string
s((const char *)data
, length
);
324 for (const char * const * k
= keywords
; *k
; k
++)
326 bestMode
= isPrintable
; // reserved word; need quotes
333 print("%.*s", length
, data
);
336 print("\"%.*s\"", length
, data
);
340 printBytes(data
, length
);
345 void Dumper::printBytes(const Byte
*data
, size_t length
)
347 for (unsigned n
= 0; n
< length
; n
++)
348 print("%02.2x", data
[n
]);