view test/ota-smspp-envelope.c @ 9:b6331ae4eea9

Note-about-padding added
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 22 Feb 2021 02:19:43 +0000
parents c2de42994e57
children
line wrap: on
line source

/*
 * This test utility reads the output of ota-smswrap-* (should be run in a
 * pipeline) and prepends additional headers to transform this OTA message
 * into a byte string that can be fed to the SIM in an ENVELOPE command.
 * The intent is to test OTA RFM functionality of SIM cards using only
 * fc-simtool, without going through a GSM network.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#define	MAX_MSG_LEN	140
#define	ENVELOPE_BUF	255
#define	INIT_OFFSET	(ENVELOPE_BUF - MAX_MSG_LEN)

u_char full_buffer[ENVELOPE_BUF], *curhead;
unsigned msglen;

u_char header1[14] = {0x40, 0x04, 0x81, 0x44, 0x44, 0x7F, 0xF6,
		      0x12, 0x10, 0x71, 0x90, 0x04, 0x05, 0x2B};

u_char header2[5] = {0x82, 0x02, 0x83, 0x81, 0x8B};

read_input()
{
	int rc;

	curhead = full_buffer + INIT_OFFSET;
	rc = read_hex_from_stdin(curhead, MAX_MSG_LEN);
	if (rc < 0)
		exit(1);	/* error msg already printed */
	msglen = rc;
}

prepend_byte(newb)
{
	curhead--;
	*curhead = newb;
	msglen++;
}

prepend_simple_len()
{
	prepend_byte(msglen);
}

prepend_ber_len()
{
	if (msglen < 0x80)
		prepend_byte(msglen);
	else {
		prepend_byte(msglen);
		prepend_byte(0x81);
	}
}

prepend_header(hdr, hdrlen)
	u_char *hdr;
{
	curhead -= hdrlen;
	bcopy(hdr, curhead, hdrlen);
	msglen += hdrlen;
}

emit_output()
{
	u_char *dp;
	unsigned n;

	dp = curhead;
	for (n = 0; n < msglen; n++)
		printf("%02X", *dp++);
	putchar('\n');
}

main(argc, argv)
	char **argv;
{
	read_input();
	prepend_simple_len();
	prepend_header(header1, 14);
	prepend_ber_len();
	prepend_header(header2, 5);
	prepend_ber_len();
	prepend_byte(0xD1);
	emit_output();
	exit(0);
}