]>
git.saurik.com Git - apple/libsecurity_codesigning.git/blob - lib/reqdumper.cpp
8fbd4446c424ad7d8b1bbe7a9345cc34848a3477
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 // Printf to established output channel
40 void Dumper::print(const char *format
, ...)
44 va_start(args
, format
);
45 vsnprintf(buffer
, sizeof(buffer
), format
, args
);
52 // Dump the underlying Requirement program
58 // remove any initial space
59 if (mOutput
[0] == ' ')
60 mOutput
= mOutput
.substr(1);
65 // Dump an entire Requirements set, using temporary Dumper objects.
67 // This detects single Requirement inputs and dumps them successfully (using
68 // single-requirement syntax). No indication of error is returned in this case.
70 string
Dumper::dump(const Requirements
*reqs
, bool debug
/* = false */)
73 return "# no requirement(s)";
74 } else if (reqs
->magic() == Requirement::typeMagic
) { // single requirement
75 return dump((const Requirement
*)reqs
) + "\n";
78 for (unsigned n
= 0; n
< reqs
->count(); n
++) {
80 if (reqs
->type(n
) < kSecRequirementTypeCount
)
81 snprintf(prefix
, sizeof(prefix
),
82 "%s => ", Requirement::typeNames
[reqs
->type(n
)]);
84 snprintf(prefix
, sizeof(prefix
), "/*unknown type*/ %d => ", reqs
->type(n
));
85 Dumper
dumper(reqs
->blob
<Requirement
>(n
), debug
);
87 result
+= prefix
+ dumper
.value() + "\n";
93 string
Dumper::dump(const Requirement
*req
, bool debug
/* = false */)
95 Dumper
dumper(req
, debug
);
99 } catch (const CommonError
&err
) {
102 snprintf(errstr
, sizeof(errstr
), " !! error %ld !!", err
.osStatus());
103 return dumper
.value() + errstr
;
109 string
Dumper::dump(const BlobCore
*req
, bool debug
/* = false */)
111 switch (req
->magic()) {
112 case Requirement::typeMagic
:
113 return dump(static_cast<const Requirement
*>(req
), debug
);
115 case Requirements::typeMagic
:
116 return dump(static_cast<const Requirements
*>(req
), debug
);
119 return "invalid data type";
125 // Element dumpers. Output accumulates in internal buffer.
127 void Dumper::expr(SyntaxLevel level
)
130 print("/*@0x%x*/", pc());
131 ExprOp op
= ExprOp(get
<uint32_t>());
132 switch (op
& ~opFlagMask
) {
140 print("identifier ");
144 print("anchor apple");
146 case opAppleGenericAnchor
:
147 print("anchor apple generic");
150 print("anchor"); certSlot(); print(" = "); hashData();
155 print("info["); dotString(); print("] = "); data();
184 print("info["); dotString(); print("]"); match();
186 case opEntitlementField
:
187 print("entitlement["); dotString(); print("]"); match();
190 print("certificate"); certSlot(); print("["); dotString(); print("]"); match();
193 print("certificate"); certSlot(); print("[");
195 const unsigned char *data
; size_t length
;
196 getData(data
, length
);
197 print("field.%s", CssmOid((unsigned char *)data
, length
).toOid().c_str());
202 print("certificate"); certSlot(); print("trusted");
205 print("anchor trusted");
208 if (op
& opGenericFalse
) {
209 print(" false /* opcode %d */", op
& ~opFlagMask
);
211 } else if (op
& opGenericSkip
) {
212 print(" /* opcode %d */", op
& ~opFlagMask
);
215 print("OPCODE %d NOT UNDERSTOOD (ending print)", op
);
221 void Dumper::certSlot()
223 switch (uint32_t slot
= get
<uint32_t>()) {
224 case Requirement::anchorCert
:
227 case Requirement::leafCert
:
238 switch (MatchOperation op
= MatchOperation(get
<uint32_t>())) {
243 print(" = "); data();
246 print(" ~ "); data();
248 case matchBeginsWith
:
249 print(" = "); data(); print("*");
252 print(" = *"); data();
255 print(" < "); data();
257 case matchGreaterEqual
:
258 print(" >= "); data();
261 print(" <= "); data();
263 case matchGreaterThan
:
264 print(" > "); data();
267 print("MATCH OPCODE %d NOT UNDERSTOOD", op
);
272 void Dumper::hashData()
275 const unsigned char *data
; size_t length
;
276 getData(data
, length
);
277 printBytes(data
, length
);
281 void Dumper::data(PrintMode bestMode
/* = isSimple */, bool dotOkay
/* = false */)
283 const unsigned char *data
; size_t length
;
284 getData(data
, length
);
285 for (unsigned n
= 0; n
< length
; n
++)
286 if ((isalnum(data
[n
]) || (data
[n
] == '.' && dotOkay
))) { // simple
287 if (n
== 0 && isdigit(data
[n
])) // unquoted idents can't start with a digit
288 bestMode
= isPrintable
;
289 } else if (isgraph(data
[n
]) || isspace(data
[n
])) {
290 if (bestMode
== isSimple
)
291 bestMode
= isPrintable
;
297 if (length
== 0 && bestMode
== isSimple
)
298 bestMode
= isPrintable
; // force quotes for empty string
302 print("%.*s", length
, data
);
305 print("\"%.*s\"", length
, data
);
309 printBytes(data
, length
);
314 void Dumper::printBytes(const Byte
*data
, size_t length
)
316 for (unsigned n
= 0; n
< length
; n
++)
317 print("%02.2x", data
[n
]);