2024-01-26 01:16:37 +00:00
|
|
|
package app
|
2024-01-19 22:26:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/sigurn/crc16"
|
|
|
|
)
|
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
const apcHeaderLen = 228
|
|
|
|
|
2024-01-19 22:26:14 +00:00
|
|
|
// makeFileHeader generates the 228 byte header to prepend to the .p15
|
2024-02-04 15:59:58 +00:00
|
|
|
// as required by APC UPS NMC. Contrary to the apc_tools repo, it does
|
|
|
|
// mot appear the header changes based on key size.
|
2024-01-19 22:26:14 +00:00
|
|
|
func makeFileHeader(p15File []byte) ([]byte, error) {
|
|
|
|
// original reference code from: https://github.com/bbczeuz/apc_tools
|
|
|
|
// // add APC header
|
|
|
|
// *(uint32_t *)(buf + 0) = 1; // always 1
|
|
|
|
// *(uint32_t *)(buf + 4) = 1; // always 1
|
|
|
|
// strncpy((char *)(buf + 8), "SecurityWizard103", 0xC8); // apparently supposed to identify the creating tool, SecWiz v1.04 sill puts 103 here
|
|
|
|
// *(uint32_t *)(buf + 208) = 1; // always 1
|
|
|
|
// *(uint32_t *)(buf + 212) = 1; // always 1
|
|
|
|
// *(uint32_t *)(buf + 216) = fileSize; // size of the following data
|
|
|
|
// *(uint32_t *)(buf + 208) = keySize; // 1 for 1024 key, otherwise (2048 bit) 2
|
|
|
|
// // 16 bit checksums are moved to 32 bit int with sign-extension
|
|
|
|
// *(uint32_t *)(buf + 220) = (int32_t)calc_cksum(0, buf + 228, fileSize); // checksum of the original file
|
|
|
|
// *(uint32_t *)(buf + 224) = (int32_t)calc_cksum(0, buf, 224); // checksum of the APC header
|
|
|
|
|
2024-01-26 01:16:37 +00:00
|
|
|
// NOTE: This line is unused as it seems the APC CLI tool v1.0.0 code always writes this as a 1 (regardless of key length)
|
2024-01-19 22:26:14 +00:00
|
|
|
// *(uint32_t *)(buf + 208) = keySize; // 1 for 1024 key, otherwise (2048 bit) 2
|
|
|
|
// Unsure why this was in original code but seems irrelevant
|
|
|
|
|
2024-06-04 22:59:36 +00:00
|
|
|
header := make([]byte, apcHeaderLen)
|
2024-01-19 22:26:14 +00:00
|
|
|
|
|
|
|
// always 1
|
|
|
|
header[0] = 1
|
|
|
|
|
|
|
|
// always 1
|
|
|
|
header[4] = 1
|
|
|
|
|
|
|
|
// apparently supposed to identify the creating tool
|
|
|
|
toolName := "NMCSecurityWizardCLI100"
|
|
|
|
toolNameBytes := []byte(toolName)
|
|
|
|
if len(toolNameBytes) > 200 {
|
|
|
|
return nil, errors.New("tool name is too big to fit in header")
|
|
|
|
}
|
|
|
|
copy(header[8:], toolNameBytes)
|
|
|
|
|
|
|
|
// always 1
|
|
|
|
header[208] = 1
|
|
|
|
|
|
|
|
// always 1
|
|
|
|
header[212] = 1
|
|
|
|
|
|
|
|
// size of the data after the header (the actual p15 file)
|
|
|
|
size := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(size, uint32(len(p15File)))
|
|
|
|
copy(header[216:], size)
|
|
|
|
|
|
|
|
// check sums (CRC Table)
|
|
|
|
checksumTable := crc16.MakeTable(crc16.CRC16_XMODEM)
|
|
|
|
|
2024-01-27 16:35:36 +00:00
|
|
|
// NOTE: 16 bit checksums are moved to 32 bit int with sign-extension by converting
|
|
|
|
// to int16 and then to uint32
|
|
|
|
|
2024-01-19 22:26:14 +00:00
|
|
|
// file checksum
|
|
|
|
fileChecksum := make([]byte, 4)
|
2024-01-27 16:35:36 +00:00
|
|
|
binary.LittleEndian.PutUint32(fileChecksum, uint32(int16(crc16.Checksum(p15File, checksumTable))))
|
2024-01-19 22:26:14 +00:00
|
|
|
copy(header[220:], fileChecksum)
|
|
|
|
|
|
|
|
// header checksum
|
|
|
|
headerChecksum := make([]byte, 4)
|
2024-01-27 16:35:36 +00:00
|
|
|
binary.LittleEndian.PutUint32(headerChecksum, uint32(int16(crc16.Checksum(header[:224], checksumTable))))
|
2024-01-19 22:26:14 +00:00
|
|
|
copy(header[224:], headerChecksum)
|
|
|
|
|
|
|
|
return header, nil
|
|
|
|
}
|