phplang 0.2.17

PHP as a fusevm frontend: lexer, parser, and compiler to fusevm::Chunk on a PhpHost heap — the first compiled standalone PHP runtime
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! PHP standard-library `hashext` functions: file digests, constant-time
//! comparison, PBKDF2 key derivation, random bytes, and the incremental
//! `hash_init` … `hash_final` family over a `HashContext`. Part of the `stdlib`
//! chain; see `src/stdlib/mod.rs`. `dispatch` returns `None` for names it does
//! not handle.
//!
//! Every digest, the HMAC construction and the `$options` seeding come from
//! `src/stdlib/hash.rs` (`ALGOS`, `hmac`, `read_options`, `digest_with`); the
//! only construction local to this module is the PBKDF2 loop (RFC 2898).
//!
//! Byte model: file contents are hashed as the raw bytes on disk (`std::fs::read`),
//! which is byte-faithful for binary files — unlike routing the bytes back
//! through phplang's UTF-8 `String` type, which would re-encode any byte >= 0x80.
//! A `raw_output` digest is mapped byte→codepoint (Latin-1), matching the core
//! `chr`/`ord` byte handling and `src/stdlib/hash.rs`; the hex form (default) is
//! fully faithful. See `wrap` for the `strlen` caveat on raw binary output.

use crate::host::with_host;
use crate::stdlib::common::{arg, int_arg, str_arg, throws};
use crate::stdlib::fileres::{read as stream_read, strerror};
use crate::stdlib::hash::{digest_with, find, hmac, md5_bytes, read_options, sha1_bytes, Init};
use fusevm::Value;

/// The digest function paired with its HMAC block size.
type HmacAlgo = (fn(&[u8]) -> Vec<u8>, usize);

/// `(digest fn, HMAC block size)` for the cryptographic algorithms of
/// `hash::ALGOS`; `None` for an unknown name or a plain checksum.
fn hmac_algo(algo: &str) -> Option<HmacAlgo> {
    find(algo).and_then(|a| a.block.map(|b| (a.digest, b)))
}

/// Dispatch a `hashext`-category PHP function by lowercased name.
pub fn dispatch(name: &str, args: &[Value]) -> Option<Result<Value, String>> {
    Some(match name {
        "md5_file" => file_digest_fn("md5_file", md5_bytes, args),
        "sha1_file" => file_digest_fn("sha1_file", sha1_bytes, args),
        "hash_file" => hash_file(args),
        "hash_hmac_file" => hash_hmac_file(args),
        "hash_equals" => Ok(Value::bool(hash_equals(args))),
        "hash_pbkdf2" => hash_pbkdf2(args),
        "random_bytes" => random_bytes(args),
        "hash_init" => hash_init(args),
        "hash_update" => hash_update(args),
        "hash_update_stream" => hash_update_stream(args),
        "hash_update_file" => hash_update_file(args),
        "hash_final" => hash_final(args),
        "hash_copy" => hash_copy(args),
        _ => return None,
    })
}

/// PHP boolean truthiness of the `i`-th argument, defaulting to false.
fn bool_arg(args: &[Value], i: usize) -> bool {
    with_host(|h| h.is_truthy(&arg(args, i)))
}

/// Wrap raw digest bytes into a PHP `Value`: lowercase hex, or the binary string
/// itself when `raw` is set.
fn wrap(bytes: Vec<u8>, raw: bool) -> Value {
    if raw {
        Value::str(crate::bytes::from_bytes(&bytes))
    } else {
        Value::str(hex::encode(bytes))
    }
}

/// Read a file's raw bytes for `fname` as the plain-files stream wrapper
/// reports it: an empty path is its `ValueError`; an unopenable one is its
/// `Failed to open stream` warning and `None`; a directory opens but cannot be
/// read, which is the `Read of 8192 bytes failed` notice and `None`.
fn read_file(fname: &str, path: &str) -> Result<Option<Vec<u8>>, String> {
    if path.is_empty() {
        return Err(throws("ValueError", "Path must not be empty"));
    }
    match std::fs::read(path) {
        Ok(bytes) => Ok(Some(bytes)),
        Err(e) if std::path::Path::new(path).is_dir() => {
            with_host(|h| {
                h.notice(format!(
                    "{fname}(): Read of 8192 bytes failed with errno=21 {}",
                    strerror(&e)
                ))
            });
            Ok(None)
        }
        Err(e) => {
            with_host(|h| {
                h.warn(format!(
                    "{fname}({path}): Failed to open stream: {}",
                    strerror(&e)
                ))
            });
            Ok(None)
        }
    }
}

/// Shared body of `md5_file`/`sha1_file`: read arg 0, hash, honor `raw_output`
/// at arg 1. Returns PHP `false` when the file cannot be read (PHP emits a
/// warning and returns `false`).
fn file_digest_fn(fname: &str, f: fn(&[u8]) -> Vec<u8>, args: &[Value]) -> Result<Value, String> {
    let path = str_arg(args, 0);
    let raw = bool_arg(args, 1);
    Ok(match read_file(fname, &path)? {
        Some(bytes) => wrap(f(&bytes), raw),
        None => Value::bool(false),
    })
}

/// `hash_file(algo, filename, binary = false)`. Validates the algorithm before
/// touching the filesystem (PHP throws the ValueError regardless of the file),
/// then reads and digests the raw file bytes.
fn hash_file(args: &[Value]) -> Result<Value, String> {
    let algo = str_arg(args, 0).to_ascii_lowercase();
    let path = str_arg(args, 1);
    let raw = bool_arg(args, 2);
    let Some(a) = find(&algo) else {
        // PHP 8 ValueError text (mirrors the core `hash()` wording).
        return Err(throws(
            "ValueError",
            "hash_file(): Argument #1 ($algo) must be a valid hashing algorithm",
        ));
    };
    let bytes = match read_file("hash_file", &path)? {
        Some(b) => b,
        None => return Ok(Value::bool(false)),
    };
    // `php_hash_do_hash` opens the stream before it initialises the context,
    // so an unreadable file is `false` before any option is looked at.
    let init = read_options("hash_file", a.name, &arg(args, 3))?;
    Ok(wrap(digest_with(a, &bytes, &init), raw))
}

/// `hash_hmac_file(algo, filename, key, binary = false)`. Validates the
/// algorithm first, then HMACs the raw file bytes under `key`.
fn hash_hmac_file(args: &[Value]) -> Result<Value, String> {
    let algo = str_arg(args, 0).to_ascii_lowercase();
    let path = str_arg(args, 1);
    let key = crate::bytes::to_bytes(&str_arg(args, 2)).into_owned();
    let raw = bool_arg(args, 3);
    let (f, block_size) = match hmac_algo(&algo) {
        Some(pair) => pair,
        // PHP 8 ValueError text for an unknown HMAC algorithm.
        None => {
            return Err(throws(
                "ValueError",
                "hash_hmac_file(): Argument #1 ($algo) must be a valid cryptographic \
             hashing algorithm",
            ));
        }
    };
    let bytes = match read_file("hash_hmac_file", &path)? {
        Some(b) => b,
        None => return Ok(Value::bool(false)),
    };
    Ok(wrap(hmac(block_size, f, &key, &bytes), raw))
}

/// `hash_equals(known, user)` — length-independent (constant-time over equal
/// lengths) comparison. Returns PHP `false` when the lengths differ (length is
/// not treated as secret, matching PHP), otherwise accumulates a byte XOR so the
/// running time does not depend on the position of the first difference.
fn hash_equals(args: &[Value]) -> bool {
    let known = crate::bytes::to_bytes(&str_arg(args, 0)).into_owned();
    let user = crate::bytes::to_bytes(&str_arg(args, 1)).into_owned();
    if known.len() != user.len() {
        return false;
    }
    let mut diff: u8 = 0;
    for (a, b) in known.iter().zip(user.iter()) {
        diff |= a ^ b;
    }
    diff == 0
}

/// `hash_pbkdf2(algo, password, salt, iterations, length = 0, binary = false)`.
/// Standard PBKDF2 (RFC 2898) over HMAC of the named algorithm.
///
/// `length` is the length of the OUTPUT string: for `binary = true` it is the
/// byte-length of the derived key; for hex output it is the number of hex chars
/// (half a byte each). `length = 0` uses the full digest (`digest_size` bytes,
/// rendered as `2 * digest_size` hex chars).
fn hash_pbkdf2(args: &[Value]) -> Result<Value, String> {
    let algo = str_arg(args, 0).to_ascii_lowercase();
    let password = crate::bytes::to_bytes(&str_arg(args, 1)).into_owned();
    let salt = crate::bytes::to_bytes(&str_arg(args, 2)).into_owned();
    let iterations = int_arg(args, 3);
    let length = int_arg(args, 4);
    let raw = bool_arg(args, 5);

    let (f, block_size) = match hmac_algo(&algo) {
        Some(pair) => pair,
        None => {
            return Err(throws(
                "ValueError",
                "hash_pbkdf2(): Argument #1 ($algo) must be a valid cryptographic \
                 hashing algorithm",
            ));
        }
    };
    if iterations <= 0 {
        return Err(throws(
            "ValueError",
            "hash_pbkdf2(): Argument #4 ($iterations) must be greater than 0",
        ));
    }
    if length < 0 {
        return Err(throws(
            "ValueError",
            "hash_pbkdf2(): Argument #5 ($length) must be greater than or equal to 0",
        ));
    }
    // Cap the derived-key length so a pathological request cannot abort the
    // process on allocation.
    if length > (1 << 24) {
        return Err("hash_pbkdf2(): Argument #5 ($length) is too large".to_string());
    }

    let hlen = f(&[]).len();
    let length = length as usize;
    // Bytes of derived key to produce, and whether we return the full digest.
    let (dk_len, full) = if length == 0 {
        (hlen, true)
    } else if raw {
        (length, false)
    } else {
        // Hex output: each byte is two chars, so ceil(length / 2) bytes.
        (length.div_ceil(2), false)
    };
    let dk = pbkdf2(f, block_size, &password, &salt, iterations as usize, dk_len);
    if raw {
        Ok(wrap(dk, true))
    } else {
        let hexed = hex::encode(dk);
        if full {
            Ok(Value::str(hexed))
        } else {
            // Truncate to exactly `length` hex chars (handles odd lengths).
            Ok(Value::str(hexed[..length].to_string()))
        }
    }
}

/// `random_bytes(length)` — `length` bytes from the OS CSPRNG
/// (`stdlib::csprng`), as a binary string.
fn random_bytes(args: &[Value]) -> Result<Value, String> {
    let len = int_arg(args, 0);
    if len < 1 {
        // PHP 8 ValueError text.
        return Err(throws(
            "ValueError",
            "random_bytes(): Argument #1 ($length) must be greater than 0",
        ));
    }
    // Cap the request so a pathological length cannot abort the process on
    // allocation (16 MiB is far beyond any real key/token size).
    if len > (1 << 24) {
        return Err("random_bytes(): Argument #1 ($length) is too large".to_string());
    }
    let mut out = vec![0u8; len as usize];
    crate::stdlib::csprng::random_bytes_throw(&mut out)?;
    Ok(wrap(out, true))
}

// ---- PBKDF2 ------------------------------------------------------------------

/// PBKDF2 (RFC 2898 §5.2) producing `dk_len` bytes of derived key using
/// HMAC-`f`. Each block T_i = U_1 ⊕ U_2 ⊕ … ⊕ U_c with
/// U_1 = HMAC(pass, salt ‖ INT_BE(i)) and U_j = HMAC(pass, U_{j-1}).
fn pbkdf2(
    f: fn(&[u8]) -> Vec<u8>,
    block_size: usize,
    password: &[u8],
    salt: &[u8],
    iterations: usize,
    dk_len: usize,
) -> Vec<u8> {
    let hlen = f(&[]).len();
    let n_blocks = dk_len.div_ceil(hlen);
    let mut out = Vec::with_capacity(n_blocks * hlen);
    for i in 1..=n_blocks as u32 {
        let mut msg = salt.to_vec();
        msg.extend_from_slice(&i.to_be_bytes());
        let mut u = hmac(block_size, f, password, &msg);
        let mut t = u.clone();
        for _ in 1..iterations {
            u = hmac(block_size, f, password, &u);
            for (tb, ub) in t.iter_mut().zip(u.iter()) {
                *tb ^= *ub;
            }
        }
        out.extend_from_slice(&t);
    }
    out.truncate(dk_len);
    out
}

// ── Incremental hashing: `hash_init` … `hash_final` over a `HashContext` ──────
//
// The reference keeps a live digest state inside a `HashContext` object
// (`ext/hash/hash.c`, `php_hashcontext_object`). phplang's digests are one-shot
// `fn(&[u8]) -> Vec<u8>` (see `hash::ALGOS`), so the context accumulates the
// bytes it is fed and digests them once at `hash_final`. Every observable result
// — the digest, the HMAC, `hash_copy` independence, the refusals and their
// wording — is the reference's; only the memory profile differs.
//
// The state lives in the private properties of the prelude's `HashContext`
// class (`HASH_PRELUDE` in `src/lib.rs`): `algo`, `key` (the HMAC key, or null),
// `data` and `finalized`. Keeping it in properties rather than a host-side
// table is what makes `clone $ctx` behave as the reference's clone handler
// does — the copy has its own state — and lets `__debugInfo` show the `algo`
// the reference's `var_dump` shows.
//
// `data` holds BYTES, one per `char` (Latin-1), the same byte→codepoint model
// `wrap` uses for a raw digest. A PHP string fed to `hash_update` contributes
// its UTF-8 bytes — exactly what `hash()` digests — while `hash_update_file`
// and `hash_update_stream` contribute the raw bytes they read, so a binary file
// hashes byte-faithfully instead of being re-encoded.

/// The flag bit `hash_init` reads from `$flags`: the context HMACs under `$key`.
pub(crate) const HASH_HMAC: i64 = 1;

/// The class every context is an instance of.
const CONTEXT_CLASS: &str = "HashContext";

/// `hash_init(algo, flags = 0, key = "", options = [])`.
///
/// The three refusals are `php_hash_init`'s, in its order: an unknown
/// algorithm, a checksum where `HASH_HMAC` asks for a cryptographic digest, and
/// an empty key with `HASH_HMAC` set. A key given without the flag is ignored.
fn hash_init(args: &[Value]) -> Result<Value, String> {
    let algo_name = str_arg(args, 0).to_ascii_lowercase();
    let hmac = int_arg(args, 1) & HASH_HMAC != 0;
    let Some(algo) = find(&algo_name) else {
        return Err(throws(
            "ValueError",
            "hash_init(): Argument #1 ($algo) must be a valid hashing algorithm",
        ));
    };
    let key = if hmac {
        if algo.block.is_none() {
            return Err(throws(
                "ValueError",
                "hash_init(): Argument #1 ($algo) must be a cryptographic hashing \
                 algorithm if HMAC is requested",
            ));
        }
        let key = str_arg(args, 2);
        if key.is_empty() {
            return Err(throws(
                "ValueError",
                "hash_init(): Argument #3 ($key) must not be empty when HMAC is requested",
            ));
        }
        Value::str(key)
    } else {
        Value::Undef
    };
    let (seed, secret) = match read_options("hash_init", algo.name, &arg(args, 3))? {
        Init::Default => (Value::Undef, Value::Undef),
        Init::Seed(n) => (Value::int(n), Value::Undef),
        Init::Secret(s) => (Value::Undef, Value::str(crate::bytes::from_bytes(&s))),
    };
    let props = vec![
        ("algo".to_string(), Value::str(algo.name.to_string())),
        ("key".to_string(), key),
        ("seed".to_string(), seed),
        ("secret".to_string(), secret),
        ("data".to_string(), Value::str(String::new())),
        ("finalized".to_string(), Value::bool(false)),
    ];
    Ok(with_host(|h| h.new_object_bare(CONTEXT_CLASS, props)))
}

/// The `$context` argument of `fname`, checked the way the reference checks it:
/// a `TypeError` naming the type given for anything but a `HashContext`, and
/// the `valid, non-finalized` `TypeError` for a context `hash_final` has closed.
fn context_arg(fname: &str, args: &[Value]) -> Result<Value, String> {
    let ctx = arg(args, 0);
    let (is_context, finalized) = with_host(|h| {
        let is_context = h
            .object_class(&ctx)
            .is_some_and(|c| c.eq_ignore_ascii_case(CONTEXT_CLASS));
        let finalized = is_context && h.is_truthy(&h.prop_get(&ctx, "finalized"));
        (is_context, finalized)
    });
    if !is_context {
        let given = with_host(|h| h.type_name_for_error(&ctx));
        return Err(throws(
            "TypeError",
            format!("{fname}(): Argument #1 ($context) must be of type HashContext, {given} given"),
        ));
    }
    if finalized {
        return Err(throws(
            "TypeError",
            format!("{fname}(): Argument #1 ($context) must be a valid, non-finalized HashContext"),
        ));
    }
    Ok(ctx)
}

/// Append raw bytes to the context's buffered input (one Latin-1 `char` each).
fn context_feed(ctx: &Value, bytes: &[u8]) {
    with_host(|h| {
        let mut data = h.to_str(&h.prop_get(ctx, "data"));
        data.extend(bytes.iter().map(|&b| char::from(b)));
        h.prop_set(ctx, "data", Value::str(data));
    });
}

/// `hash_update(context, data)`: feed a PHP string's bytes. Always `true`.
fn hash_update(args: &[Value]) -> Result<Value, String> {
    let ctx = context_arg("hash_update", args)?;
    context_feed(&ctx, &crate::bytes::to_bytes(&str_arg(args, 1)));
    Ok(Value::bool(true))
}

/// `hash_update_stream(context, stream, length = -1)`: feed up to `length`
/// bytes from the stream's cursor (everything left when negative), returning
/// how many were read.
fn hash_update_stream(args: &[Value]) -> Result<Value, String> {
    let ctx = context_arg("hash_update_stream", args)?;
    let stream = arg(args, 1);
    if !with_host(|h| h.is_resource(&stream)) {
        let given = with_host(|h| h.type_name_for_error(&stream));
        return Err(throws(
            "TypeError",
            format!("hash_update_stream(): Argument #2 ($stream) must be of type resource, {given} given"),
        ));
    }
    let length = match arg(args, 2) {
        Value::Undef => -1,
        _ => int_arg(args, 2),
    };
    let want = usize::try_from(length).unwrap_or(usize::MAX);
    let bytes =
        stream_read("hash_update_stream", &stream, |s| Some(s.read(want))).unwrap_or_default();
    context_feed(&ctx, &bytes);
    Ok(Value::int(bytes.len() as i64))
}

/// `hash_update_file(context, filename, stream_context = null)`: feed a file's
/// raw bytes. An empty path is the stream layer's `ValueError`; an unopenable
/// one is its `Failed to open stream` warning and `false`; a directory opens
/// but cannot be read, which is the reference's `Notice` and `false`.
fn hash_update_file(args: &[Value]) -> Result<Value, String> {
    let ctx = context_arg("hash_update_file", args)?;
    let path = str_arg(args, 1);
    Ok(match read_file("hash_update_file", &path)? {
        Some(bytes) => {
            context_feed(&ctx, &bytes);
            Value::bool(true)
        }
        None => Value::bool(false),
    })
}

/// `hash_final(context, binary = false)`: the digest — or the HMAC, when the
/// context was opened with `HASH_HMAC` — of everything fed so far. The context
/// is finalized: every later `hash_update`/`hash_final`/`hash_copy` on it is
/// refused, and its buffered input is released.
fn hash_final(args: &[Value]) -> Result<Value, String> {
    let ctx = context_arg("hash_final", args)?;
    let raw = bool_arg(args, 1);
    let (algo_name, key, init, data) = with_host(|h| {
        let key = match h.prop_get(&ctx, "key") {
            Value::Undef => None,
            k => Some(crate::bytes::to_bytes(&h.to_str(&k)).into_owned()),
        };
        let data: Vec<u8> = h
            .to_str(&h.prop_get(&ctx, "data"))
            .chars()
            .map(|c| c as u32 as u8)
            .collect();
        h.prop_set(&ctx, "data", Value::str(String::new()));
        h.prop_set(&ctx, "finalized", Value::bool(true));
        let init = match (h.prop_get(&ctx, "seed"), h.prop_get(&ctx, "secret")) {
            (Value::Int(n), _) => Init::Seed(n),
            (_, Value::Str(s)) => Init::Secret(crate::bytes::to_bytes(&s).into_owned()),
            _ => Init::Default,
        };
        (h.to_str(&h.prop_get(&ctx, "algo")), key, init, data)
    });
    let algo = find(&algo_name).expect("hash_init only stores a registered algorithm");
    let digest = match (key, algo.block) {
        (Some(key), Some(block)) => hmac(block, algo.digest, &key, &data),
        _ => digest_with(algo, &data, &init),
    };
    Ok(wrap(digest, raw))
}

/// `hash_copy(context)`: an independent context in the same state, as the
/// reference's `php_hashcontext_clone` produces — later updates to either side
/// do not reach the other.
fn hash_copy(args: &[Value]) -> Result<Value, String> {
    let ctx = context_arg("hash_copy", args)?;
    Ok(with_host(|h| {
        let props = ["algo", "key", "seed", "secret", "data", "finalized"]
            .iter()
            .map(|name| (name.to_string(), h.prop_get(&ctx, name)))
            .collect();
        h.new_object_bare(CONTEXT_CLASS, props)
    }))
}