ruipmi 0.5.0

An asynchronous IPMI client library implemented in Rust using Tokio.
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
use crate::cipher::{CipherSuite, CryptAlg, IntegrityAlg};
use crate::constants::*;
use crate::crypto::{decrypt, encrypt, hmac_auth, hmac_integrity};
use crate::error::{IpmiError, Result};

/// Build Open Session Request packet.
pub fn build_open_session_request(cipher: &CipherSuite, console_id: &[u8; 4]) -> Vec<u8> {
    let mut buf = vec![
        RMCP_VERSION_1, 0x00, 0xFF, RMCP_CLASS_IPMI,
        SESSION_AUTHTYPE_RMCP_PLUS,
        PAYLOAD_TYPE_RMCP_OPEN_REQUEST,
        0, 0, 0, 0, 0, 0, 0, 0,
        0x20, 0x00,
    ];
    buf.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
    buf.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
    buf.extend_from_slice(&[0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00]);
    buf.extend_from_slice(&[0x01, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00]);
    buf.extend_from_slice(&[0x02, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00]);

    buf[0x14..0x18].copy_from_slice(console_id);
    buf[0x1C] = cipher.auth_byte();
    buf[0x24] = cipher.integrity_byte();
    buf[0x2C] = cipher.conf_byte();

    let plen = buf.len() - IPMI_LANPLUS_HEADER_LEN;
    buf[OFF_PAYLOAD_SIZE] = (plen & 0xFF) as u8;
    buf[OFF_PAYLOAD_SIZE + 1] = ((plen >> 8) & 0xFF) as u8;
    buf
}

/// Build RAKP Message 1 packet.
pub fn build_rakp1(
    bmc_id: &[u8; 4],
    console_rand: &[u8; 16],
    username: &[u8],
) -> Vec<u8> {
    let mut buf = vec![
        RMCP_VERSION_1, 0x00, 0xFF, RMCP_CLASS_IPMI,
        SESSION_AUTHTYPE_RMCP_PLUS,
        PAYLOAD_TYPE_RAKP_1,
        0, 0, 0, 0, 0, 0, 0, 0,
        0, 0,
        0, 0, 0, 0,
    ];
    buf.extend_from_slice(bmc_id);
    buf.extend_from_slice(console_rand);
    buf.extend_from_slice(&[0x14, 0x00, 0x00, username.len() as u8]);
    buf.extend_from_slice(username);

    let plen = buf.len() - IPMI_LANPLUS_HEADER_LEN;
    buf[OFF_PAYLOAD_SIZE] = (plen & 0xFF) as u8;
    buf[OFF_PAYLOAD_SIZE + 1] = ((plen >> 8) & 0xFF) as u8;
    buf
}

/// Verify RAKP2 auth code from BMC.
/// HMAC(password, SIDm || SIDc || Rm || Rc || GUIDc || ROLEm || ULENm || UNAMEm)
pub fn verify_rakp2(
    cipher: &CipherSuite,
    password: &[u8],
    console_id: &[u8; 4],
    bmc_id: &[u8; 4],
    console_rand: &[u8; 16],
    bmc_rand: &[u8; 16],
    bmc_guid: &[u8; 16],
    username: &[u8],
    received_auth_code: &[u8],
) -> Result<()> {
    let mut data = Vec::with_capacity(58 + username.len());
    data.extend_from_slice(console_id);  // SIDm
    data.extend_from_slice(bmc_id);      // SIDc
    data.extend_from_slice(console_rand); // Rm
    data.extend_from_slice(bmc_rand);    // Rc
    data.extend_from_slice(bmc_guid);    // GUIDc
    data.push(0x14);                     // ROLEm
    data.push(username.len() as u8);     // ULENGTHm
    data.extend_from_slice(username);    // UNAMEm

    let expected = hmac_auth(cipher, password, &data);
    if expected.len() < received_auth_code.len() {
        return Err(IpmiError::AuthFailed);
    }
    if expected[..received_auth_code.len()] != *received_auth_code {
        return Err(IpmiError::AuthFailed);
    }
    Ok(())
}

/// Verify RAKP4 integrity check value.
/// HMAC(SIK, Rm || SIDc || GUIDc) truncated per auth algorithm.
pub fn verify_rakp4(
    cipher: &CipherSuite,
    sik: &[u8],
    console_rand: &[u8; 16],
    bmc_id: &[u8; 4],
    bmc_guid: &[u8; 16],
    received_icv: &[u8],
) -> Result<()> {
    let mut data = Vec::with_capacity(36);
    data.extend_from_slice(console_rand); // Rm
    data.extend_from_slice(bmc_id);       // SIDc (BMC session ID)
    data.extend_from_slice(bmc_guid);     // GUIDc

    let full = hmac_auth(cipher, sik, &data);
    // Truncate: SHA1->12, MD5->16, SHA256->16
    let trunc_len = match cipher.authentication {
        crate::cipher::AuthAlg::HmacSha1 => 12,
        crate::cipher::AuthAlg::HmacMd5 => 16,
        crate::cipher::AuthAlg::HmacSha256 => 16,
        crate::cipher::AuthAlg::None => 0,
    };
    if trunc_len == 0 {
        return Ok(());
    }
    if received_icv.len() != trunc_len || full[..trunc_len] != *received_icv {
        return Err(IpmiError::AuthFailed);
    }
    Ok(())
}

/// Build RAKP Message 3 and derive session keys (SIK, K1, K2).
/// Returns (rakp3_packet, sik, k1, k2).
pub fn build_rakp3(
    cipher: &CipherSuite,
    password: &[u8],
    username: &[u8],
    bmc_id: &[u8; 4],
    console_id: &[u8; 4],
    console_rand: &[u8; 16],
    bmc_rand: &[u8; 16],
) -> Result<(Vec<u8>, Vec<u8>, Vec<u8>, [u8; 16])> {
    // AuthCode = HMAC(password, Rm + SIDm + ROLEm + ULEN + USERNAME)
    let mut material = Vec::with_capacity(16 + 4 + 2 + username.len());
    material.extend_from_slice(bmc_rand);
    material.extend_from_slice(console_id);
    material.push(0x14);
    material.push(username.len() as u8);
    material.extend_from_slice(username);
    let auth = hmac_auth(cipher, password, &material);

    // SIK = HMAC(password, Rc + Rm + ROLEm + ULEN + USERNAME)
    let mut sikm = Vec::with_capacity(32 + 2 + username.len());
    sikm.extend_from_slice(console_rand);
    sikm.extend_from_slice(bmc_rand);
    sikm.push(0x14);
    sikm.push(username.len() as u8);
    sikm.extend_from_slice(username);
    let sik = hmac_auth(cipher, password, &sikm);

    // K1 = HMAC(SIK, const_1), K2 = first 16 bytes of HMAC(SIK, const_2)
    let k1 = hmac_auth(cipher, &sik, &[0x01u8; 20]);
    let k2full = hmac_auth(cipher, &sik, &[0x02u8; 20]);
    let mut k2 = [0u8; 16];
    k2.copy_from_slice(&k2full[..16]);

    // Build packet
    let mut buf = vec![
        RMCP_VERSION_1, 0x00, 0xFF, RMCP_CLASS_IPMI,
        SESSION_AUTHTYPE_RMCP_PLUS,
        PAYLOAD_TYPE_RAKP_3,
        0, 0, 0, 0, 0, 0, 0, 0,
        0, 0,
        0, 0, 0, 0,
    ];
    buf.extend_from_slice(bmc_id);
    buf.extend_from_slice(&auth);

    let plen = buf.len() - IPMI_LANPLUS_HEADER_LEN;
    buf[OFF_PAYLOAD_SIZE] = (plen & 0xFF) as u8;
    buf[OFF_PAYLOAD_SIZE + 1] = ((plen >> 8) & 0xFF) as u8;

    Ok((buf, sik, k1, k2))
}

/// Build an encrypted IPMI v2 message with integrity trailer.
pub fn build_v2_encrypted_msg(
    cipher: &CipherSuite,
    raw: &[u8],
    bmc_id: &[u8; 4],
    out_seq: u32,
    rq_seq: u8,
    k1: &[u8],
    k2: &[u8; 16],
) -> Result<Vec<u8>> {
    let payload = pack_ipmi_inner(raw, rq_seq);

    let mut hdr = vec![0u8; IPMI_LANPLUS_HEADER_LEN];
    hdr[0] = RMCP_VERSION_1;
    hdr[2] = 0xFF;
    hdr[3] = RMCP_CLASS_IPMI;
    hdr[OFF_AUTHTYPE] = SESSION_AUTHTYPE_RMCP_PLUS;

    let encrypt_bit = match cipher.confidentiality {
        CryptAlg::None => 0x00,
        _ => 0x80,
    };
    let auth_bit = match cipher.integrity {
        IntegrityAlg::None => 0x00,
        _ => 0x40,
    };
    hdr[OFF_PAYLOAD_TYPE] = PAYLOAD_TYPE_IPMI | encrypt_bit | auth_bit;
    hdr[OFF_SESSION_ID..OFF_SESSION_ID + 4].copy_from_slice(bmc_id);
    hdr[OFF_SEQUENCE_NUM..OFF_SEQUENCE_NUM + 4].copy_from_slice(&out_seq.to_le_bytes());

    let enc = encrypt(cipher, &payload, k2)?;

    let mut msg = hdr;
    msg.extend_from_slice(&enc);

    let plen = enc.len();
    msg[OFF_PAYLOAD_SIZE] = (plen & 0xFF) as u8;
    msg[OFF_PAYLOAD_SIZE + 1] = ((plen >> 8) & 0xFF) as u8;

    // Integrity padding
    let length_before_auth = 12 + plen + 2;
    let pad_size = (4 - (length_before_auth % 4)) % 4;
    if pad_size > 0 {
        msg.extend(std::iter::repeat(0xFF).take(pad_size));
    }
    msg.push(pad_size as u8);
    msg.push(0x07);

    // HMAC integrity
    let to_auth = &msg[OFF_AUTHTYPE..];
    let mut auth = hmac_integrity(cipher, k1, to_auth);
    auth.truncate(cipher.integrity_truncate_len());
    msg.extend_from_slice(&auth);

    Ok(msg)
}

/// Decode and decrypt an incoming IPMI v2 response. Returns (sequence_number, payload).
pub fn decode_and_decrypt(
    cipher: &CipherSuite,
    data: &[u8],
    k1: &[u8],
    k2: &[u8; 16],
) -> Result<(u32, Vec<u8>)> {
    if data.get(OFF_AUTHTYPE).copied() != Some(SESSION_AUTHTYPE_RMCP_PLUS) {
        return Err(IpmiError::BadResponse);
    }

    let auth_len = cipher.integrity_truncate_len();
    if data.len() < auth_len + IPMI_LANPLUS_HEADER_LEN {
        return Err(IpmiError::BadResponse);
    }

    // Verify HMAC
    let auth_recv = &data[data.len() - auth_len..];
    let to_auth = &data[OFF_AUTHTYPE..data.len() - auth_len];
    let mut auth_calc = hmac_integrity(cipher, k1, to_auth);
    auth_calc.truncate(auth_len);
    if auth_calc != auth_recv {
        return Err(IpmiError::AuthFailed);
    }

    // Extract sequence number
    let seq = u32::from_le_bytes([
        data[OFF_SEQUENCE_NUM],
        data[OFF_SEQUENCE_NUM + 1],
        data[OFF_SEQUENCE_NUM + 2],
        data[OFF_SEQUENCE_NUM + 3],
    ]);

    // Decrypt
    let msglen = (data[OFF_PAYLOAD_SIZE] as usize) | ((data[OFF_PAYLOAD_SIZE + 1] as usize) << 8);
    let enc = &data[IPMI_LANPLUS_HEADER_LEN..IPMI_LANPLUS_HEADER_LEN + msglen];
    let dec = decrypt(cipher, enc, k2)?;

    // Strip confidentiality padding only when encrypted
    let ipmi_payload = match cipher.confidentiality {
        CryptAlg::None => dec,
        _ => {
            if dec.is_empty() {
                return Err(IpmiError::DecryptFailed);
            }
            let pad_len = *dec.last().unwrap() as usize;
            if dec.len() < pad_len + 1 {
                return Err(IpmiError::DecryptFailed);
            }
            if pad_len > 0 {
                let pad = &dec[dec.len() - 1 - pad_len..dec.len() - 1];
                for (i, b) in pad.iter().enumerate() {
                    if *b != (i as u8 + 1) {
                        return Err(IpmiError::DecryptFailed);
                    }
                }
            }
            let payload_size = dec.len() - pad_len - 1;
            dec[..payload_size].to_vec()
        }
    };

    // IPMI response: [rsAddr, netfn, csum1, rqAddr, rqSeq, cmd, ...data..., csum2]
    if ipmi_payload.len() < 7 {
        return Err(IpmiError::BadResponse);
    }
    Ok((seq, ipmi_payload[6..ipmi_payload.len() - 1].to_vec()))
}

/// Pack raw [netfn, cmd, data...] into IPMI inner message format.
fn pack_ipmi_inner(raw: &[u8], rq_seq: u8) -> Vec<u8> {
    let netfn = raw[0];
    let cmd = raw[1];
    let data = &raw[2..];

    let mut part1 = vec![IPMI_BMC_SLAVE_ADDR, netfn << 2];
    let csum1 = ipmi_checksum(&part1);
    part1.push(csum1);

    let mut part2 = vec![IPMI_REMOTE_SWID, rq_seq << 2, cmd];
    part2.extend_from_slice(data);
    let csum2 = ipmi_checksum(&part2);
    part2.push(csum2);

    [part1, part2].concat()
}

/// Build IPMB Send Message wrapper.
pub fn build_ipmb_send_message(raw: &[u8], channel: u8, target: u8) -> Vec<u8> {
    let ch = channel | 0x40;
    let raw_netfn = raw[0] << 2;
    let raw_cmd = raw[1];
    let raw_data = &raw[2..];

    let chk1 = ipmb_checksum(&[target, raw_netfn]);
    let mut body = vec![IPMI_REMOTE_SWID, 0x00, raw_cmd];
    body.extend_from_slice(raw_data);
    let chk2 = ipmb_checksum(&body);

    let mut out = vec![
        IPMI_NETFN_APP, 0x34, ch, target, raw_netfn, chk1,
        IPMI_REMOTE_SWID, 0x00, raw_cmd,
    ];
    out.extend_from_slice(raw_data);
    out.push(chk2);
    out
}

fn ipmi_checksum(buf: &[u8]) -> u8 {
    let s: u16 = buf.iter().map(|&v| v as u16).sum();
    ((0x100 - (s & 0xFF)) & 0xFF) as u8
}

fn ipmb_checksum(data: &[u8]) -> u8 {
    ((256u16 - (data.iter().fold(0u16, |a, &b| a + b as u16) % 256)) % 256) as u8
}

/// Build SOL data packet (payload type 0x01).
pub fn build_sol_packet(
    cipher: &CipherSuite,
    data: &[u8],
    bmc_id: &[u8; 4],
    out_seq: u32,
    sol_seq: u8,
    ack_seq: u8,
    accepted_chars: u8,
    k1: &[u8],
    k2: &[u8; 16],
) -> Result<Vec<u8>> {
    // SOL payload: [seq, ack_seq, accepted_chars, operation/status, data...]
    let mut payload = Vec::with_capacity(4 + data.len());
    payload.push(sol_seq);       // packet sequence number
    payload.push(ack_seq);       // ack/nack sequence number
    payload.push(accepted_chars); // accepted character count
    payload.push(0x00);          // operation: no flush, no break, no CTS/DCD/DSR
    payload.extend_from_slice(data);

    let mut hdr = vec![0u8; IPMI_LANPLUS_HEADER_LEN];
    hdr[0] = RMCP_VERSION_1;
    hdr[2] = 0xFF;
    hdr[3] = RMCP_CLASS_IPMI;
    hdr[OFF_AUTHTYPE] = SESSION_AUTHTYPE_RMCP_PLUS;

    let encrypt_bit = match cipher.confidentiality {
        CryptAlg::None => 0x00,
        _ => 0x80,
    };
    let auth_bit = match cipher.integrity {
        IntegrityAlg::None => 0x00,
        _ => 0x40,
    };
    hdr[OFF_PAYLOAD_TYPE] = PAYLOAD_TYPE_SOL | encrypt_bit | auth_bit;
    hdr[OFF_SESSION_ID..OFF_SESSION_ID + 4].copy_from_slice(bmc_id);
    hdr[OFF_SEQUENCE_NUM..OFF_SEQUENCE_NUM + 4].copy_from_slice(&out_seq.to_le_bytes());

    let enc = encrypt(cipher, &payload, k2)?;

    let mut msg = hdr;
    msg.extend_from_slice(&enc);

    let plen = enc.len();
    msg[OFF_PAYLOAD_SIZE] = (plen & 0xFF) as u8;
    msg[OFF_PAYLOAD_SIZE + 1] = ((plen >> 8) & 0xFF) as u8;

    // Integrity padding + HMAC
    let length_before_auth = 12 + plen + 2;
    let pad_size = (4 - (length_before_auth % 4)) % 4;
    if pad_size > 0 {
        msg.extend(std::iter::repeat(0xFF).take(pad_size));
    }
    msg.push(pad_size as u8);
    msg.push(0x07);

    let to_auth = &msg[OFF_AUTHTYPE..];
    let mut auth = hmac_integrity(cipher, k1, to_auth);
    auth.truncate(cipher.integrity_truncate_len());
    msg.extend_from_slice(&auth);

    Ok(msg)
}

/// Decode SOL response payload. Returns (sol_seq, ack_seq, accepted_chars, status, data).
pub fn decode_sol_payload(decrypted: &[u8]) -> (u8, u8, u8, u8, Vec<u8>) {
    if decrypted.len() < 4 {
        return (0, 0, 0, 0, vec![]);
    }
    let sol_seq = decrypted[0];
    let ack_seq = decrypted[1];
    let accepted = decrypted[2];
    let status = decrypted[3];
    let data = decrypted[4..].to_vec();
    (sol_seq, ack_seq, accepted, status, data)
}

/// Decode an incoming SOL or IPMI v2 packet. Returns (payload_type, sequence_number, raw_decrypted_payload).
pub fn decode_v2_payload(
    cipher: &CipherSuite,
    data: &[u8],
    k1: &[u8],
    k2: &[u8; 16],
) -> Result<(u8, u32, Vec<u8>)> {
    if data.get(OFF_AUTHTYPE).copied() != Some(SESSION_AUTHTYPE_RMCP_PLUS) {
        return Err(IpmiError::BadResponse);
    }

    let payload_type_raw = data.get(OFF_PAYLOAD_TYPE).copied().unwrap_or(0);
    let payload_type = payload_type_raw & 0x3F;

    let auth_len = cipher.integrity_truncate_len();
    if data.len() < auth_len + IPMI_LANPLUS_HEADER_LEN {
        return Err(IpmiError::BadResponse);
    }

    // Verify HMAC
    let auth_recv = &data[data.len() - auth_len..];
    let to_auth = &data[OFF_AUTHTYPE..data.len() - auth_len];
    let mut auth_calc = hmac_integrity(cipher, k1, to_auth);
    auth_calc.truncate(auth_len);
    if auth_calc != auth_recv {
        return Err(IpmiError::AuthFailed);
    }

    let seq = u32::from_le_bytes([
        data[OFF_SEQUENCE_NUM],
        data[OFF_SEQUENCE_NUM + 1],
        data[OFF_SEQUENCE_NUM + 2],
        data[OFF_SEQUENCE_NUM + 3],
    ]);

    let msglen = (data[OFF_PAYLOAD_SIZE] as usize) | ((data[OFF_PAYLOAD_SIZE + 1] as usize) << 8);
    let enc = &data[IPMI_LANPLUS_HEADER_LEN..IPMI_LANPLUS_HEADER_LEN + msglen];
    let dec = decrypt(cipher, enc, k2)?;

    // Strip confidentiality padding for encrypted payloads
    let raw = match cipher.confidentiality {
        CryptAlg::None => dec,
        _ => {
            if dec.is_empty() {
                return Err(IpmiError::DecryptFailed);
            }
            let pad_len = *dec.last().unwrap() as usize;
            if dec.len() < pad_len + 1 {
                return Err(IpmiError::DecryptFailed);
            }
            let payload_size = dec.len() - pad_len - 1;
            dec[..payload_size].to_vec()
        }
    };

    Ok((payload_type, seq, raw))
}