1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Portable receipt signing.
//!
//! Wraps `chio_core_types::receipt::body::ChioReceipt::sign_with_backend` so the kernel core
//! can produce signed receipts without depending on the `chio-kernel` full
//! crate's keypair-based helper. Using the `SigningBackend` trait keeps
//! the FIPS-capable signing path available on every adapter.
use ToString;
use Vec;
use SigningBackend;
use ReceiptSigningHandle;
use ;
/// Errors raised by [`sign_receipt`].
/// WYSIWYS receipt signing at the kernel-core trust boundary. **This is the
/// production signing primitive: every signature minted from evaluated content
/// flows through here, and it never trusts the caller's `content_hash`.**
///
/// `canonical_content` is the exact byte preimage the receipt's `content_hash`
/// was derived from (for a value output the RFC 8785 canonical JSON; for a
/// stream receipt the concatenated per-chunk digest preimage; for an empty
/// output the literal `null` canonicalization). The signer recomputes
/// `sha256_hex(canonical_content)` *inside the trust boundary* and refuses to
/// sign when it disagrees with `body.content_hash` ([`ReceiptSigningError::ContentHashMismatch`],
/// fail-closed). This closes the render-A / sign-B forgery on the production
/// path: a caller can no longer render content `A` to a human while submitting
/// a body claiming the hash of content `B`.
///
/// The recompute runs **before** the kernel-key check and before any signing
/// work, so a hash mismatch can never reach the signer.
///
/// For the move-only, one-time API that binds a signature to a single evaluated
/// artifact (and cannot be replayed), see [`sign_receipt_with_handle`]; it
/// delegates here after consuming its handle. For the thin transport adapters
/// that relay an already-minted body across an FFI/WASM boundary and therefore
/// do **not** hold the content preimage, see
/// [`sign_receipt_relaying_trusted_body`].
///
/// The `body.kernel_key` must equal `backend.public_key()`; otherwise we fail
/// fast with [`ReceiptSigningError::KernelKeyMismatch`] so the caller doesn't
/// produce a receipt whose signature cannot be verified.
///
/// # Errors
///
/// - [`ReceiptSigningError::ContentHashMismatch`] when the body's claimed
/// `content_hash` does not match `sha256_hex(canonical_content)`.
/// - [`ReceiptSigningError::KernelKeyMismatch`] when `body.kernel_key` does not
/// match `backend.public_key()`.
/// - [`ReceiptSigningError::SigningFailed`] when canonical signing fails.
/// Sign a receipt body using the given [`SigningBackend`] **without**
/// recomputing `content_hash` from a content preimage.
///
/// This trusts the caller-supplied `body.content_hash`. It exists **only** for
/// the thin transport adapters (mobile FFI / browser WASM / C++ FFI) that
/// receive an already-assembled, serialized [`ChioReceiptBody`] over their
/// boundary and therefore do **not** hold the content preimage needed to
/// re-derive the hash. Those adapters relay a body minted by an upstream
/// trusted producer (the kernel), where the WYSIWYS recompute already ran
/// through [`sign_receipt`] / [`sign_receipt_with_handle`].
///
/// Every path that *does* hold the evaluated content -- i.e. the production
/// kernel signing path (`chio_kernel::kernel::responses::build_and_sign_receipt`
/// and the mpsc signing task) -- MUST instead call [`sign_receipt`] (or
/// [`sign_receipt_with_handle`]) so `content_hash` is recomputed over the
/// canonical content inside the trust boundary and signing is refused on
/// mismatch (fail-closed). Threading the content preimage across the FFI/WASM
/// boundary so these adapters can recompute too is the larger follow-up tracked
/// as ; until then this entrypoint is the explicit, auditable seam where
/// caller `content_hash` is trusted, rather than that trust being the silent
/// default of `sign_receipt`.
///
/// The `body.kernel_key` must equal `backend.public_key()`; otherwise we
/// fail fast with [`ReceiptSigningError::KernelKeyMismatch`] so the caller
/// doesn't produce a receipt whose signature cannot be verified.
///
/// # Errors
///
/// - [`ReceiptSigningError::KernelKeyMismatch`] when `body.kernel_key` does not
/// match `backend.public_key()`.
/// - [`ReceiptSigningError::SigningFailed`] when canonical signing fails.
/// WYSIWYS receipt signing bound to a *specific* evaluated artifact via a
/// one-time [`ReceiptSigningHandle`]. **This is the strongest signing API.**
///
/// Like [`sign_receipt`], this recomputes `content_hash` over canonical content
/// inside the trust boundary and refuses to sign on mismatch (fail-closed). It
/// adds the one-time, move-only guarantee: the handle is consumed by value, so
/// a single handle backs at most one signature and cannot be replayed. The
/// handle recomputed `content_hash` over the artifact's canonical content when
/// it was constructed; here we forward that handle's canonical bytes to
/// [`sign_receipt`], which refuses to sign unless the body's claimed
/// `content_hash` equals the recomputed hash.
///
/// The kernel `build_and_sign_receipt` path and the mpsc signing task both route
/// through here, so every signature the kernel mints recomputes
/// `content_hash` inside the trust boundary and is bound to a single artifact.
/// The kernel key check from [`sign_receipt`] still applies.
///
/// # Seam note ( follow-up / )
///
/// Producers currently build the handle from canonical content they supply
/// (see [`ReceiptSigningHandle::from_content_preimage`] / [`ReceiptSigningHandle::from_content`]).
/// On the kernel path that preimage is the exact bytes
/// `receipt_content_for_output` hashed to produce `content_hash`, so the gate
/// is non-tautological there. The intended end state is for `evaluate()` to
/// return the handle so the only way to obtain one is to have actually run an
/// evaluation; threading the handle out of [`crate::evaluate`] is a larger
/// follow-up, but the recompute + refuse gate here already closes the
/// render-A / sign-B regression on the production path.
///
/// # Errors
///
/// - [`ReceiptSigningError::ContentHashMismatch`] when the body's claimed
/// `content_hash` does not match the handle's recomputed hash.
/// - [`ReceiptSigningError::KernelKeyMismatch`] when `body.kernel_key` does not
/// match `backend.public_key()`.
/// - [`ReceiptSigningError::SigningFailed`] when canonical signing fails.