]> git.saurik.com Git - apple/libsecurity_codesigning.git/blob - lib/reqmaker.h
9a72e8b41ffd71536d0b3161cb0d670a9b40ffe7
[apple/libsecurity_codesigning.git] / lib / reqmaker.h
1 /*
2 * Copyright (c) 2006 Apple Computer, 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 // reqmaker - Requirement assembler
26 //
27 #ifndef _H_REQMAKER
28 #define _H_REQMAKER
29
30 #include <security_codesigning/requirement.h>
31
32 namespace Security {
33 namespace CodeSigning {
34
35
36 //
37 // A Requirement::Maker is a tool for creating a Requirement blob.
38 // It's primarily an assember for the binary requirements (exprOp) language.
39 // Initialize it, call put() methods to generate the exprOp program, then
40 // call make() to get the assembled Requirement blob, malloc'ed for you.
41 // The Maker is not reusable.
42 //
43 class Requirement::Maker {
44 public:
45 Maker(Kind k = exprForm);
46 ~Maker() { free(mBuffer); }
47
48 template <class T>
49 T *alloc(size_t size) { return reinterpret_cast<T *>(alloc(size)); }
50
51 template <class T>
52 void put(const T &value) { *alloc<Endian<T> >(sizeof(T)) = value; }
53 void put(ExprOp op) { put(uint32_t(op)); }
54 void put(MatchOperation op) { put(uint32_t(op)); }
55 void put(const std::string &s) { putData(s.data(), s.size()); }
56 void put(const char *s) { putData(s, strlen(s)); }
57 void putData(const void *data, size_t length);
58
59 void anchor(int slot, SHA1::Digest digest); // given slot/digest
60 void anchor(int slot, const void *cert, size_t length); // given slot/cert
61 void anchor(); // canonical Apple anchor
62
63 void trustedAnchor();
64 void trustedAnchor(int slot);
65
66 void infoKey(const std::string &key, const std::string &value);
67 void ident(const std::string &identHash);
68 void cdhash(SHA1::Digest digest);
69
70 //
71 // Keep labels into exprOp code, and allow for "shifting in"
72 // prefix code as needed (exprOp is a prefix-code language).
73 //
74 struct Label {
75 const Offset pos;
76 Label(const Maker &maker) : pos(maker.length()) { }
77 };
78 void *insert(const Label &label, size_t length = sizeof(uint32_t));
79
80 template <class T>
81 Endian<T> &insert(const Label &label, size_t length = sizeof(T))
82 { return *reinterpret_cast<Endian<T>*>(insert(label, length)); }
83
84 void kind(Kind k) { mBuffer->kind(k); }
85 size_t length() const { return mPC; }
86 Requirement *make();
87 Requirement *operator () () { return make(); }
88
89 protected:
90 void require(size_t size);
91 void *alloc(size_t size);
92
93 private:
94 Requirement *mBuffer;
95 Offset mSize;
96 Offset mPC;
97 };
98
99
100 } // CodeSigning
101 } // Security
102
103 #endif //_H_REQMAKER