You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
5.5 KiB
C

// Procedures for digital signature processing
#include <stdio.h>
#include <stdint.h>
#ifndef WIN32
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <termios.h>
#include <unistd.h>
#include <arpa/inet.h>
#else
#include <windows.h>
#include "getopt.h"
#include "printf.h"
#include "buildno.h"
#endif
#include "hdlcio.h"
#include "ptable.h"
#include "flasher.h"
#include "util.h"
#include "zlib.h"
// table of key parameters -g
struct {
uint8_t type;
uint32_t len;
char* descr;
} signbase[] = {
{1,2958,"Basic Firmware"},
{1,2694,"E3372s-Stick Firmware"},
{2,1110,"Web-Interface + ISO for HiLink-Modem"},
{6,1110,"Web-Interface + ISO for HiLink-Modem"},
{2,846,"ISO (Dashboard) for Stick-Modem"},
{7,3750,"Firmware + ISO + Web-Interface"},
};
#define signbaselen 6
// table of signature types
char* fwtypes[]={
"UNKNOWN", // 0
"ONLY_FW", // 1
"ONLY_ISO", // 2
"FW_ISO", // 3
"ONLY_WEBUI", // 4
"FW_WEBUI", // 5
"ISO_WEBUI", // 6
"FW_ISO_WEBUI" // 7
};
// result string ^signver-command
uint8_t signver[200];
// digital type signature flag
extern int gflag;
// firmware type flag
extern int dflag;
// parameters of the current digital signature
uint32_t signtype; // firmware type
uint32_t signlen; // signature length
int32_t serach_sign();
// public key hash for ^signver
char signver_hash[100]="778A8D175E602B7B779D9E05C330B5279B0661BF2EED99A20445B366D63DD697";
//****************************************************
//* Receiving of firmware type description by code
//****************************************************
char* fw_description(uint8_t code) {
return fwtypes[code&0x7];
}
//****************************************************
//* Receiving of firmware type list
//****************************************************
void dlist() {
int i;
printf("\n # Description\n--------------------------------------");
for(i=1;i<8;i++) {
printf("\n %i %s",i,fw_description(i));
}
printf("\n\n");
exit(0);
}
//***************************************************
//* Processing of key parameters -d
//***************************************************
void dparm(char* sparm) {
if (dflag != 0) {
printf("\n Duplicate option -d\n\n");
exit(-1);
}
if (sparm[0] == 'l') {
dlist();
exit(0);
}
sscanf(sparm,"%x",&dload_id);
if ((dload_id == 0) || (dload_id >7)) {
printf("\n Incorrect value for option -d\n\n");
exit(-1);
}
dflag=1;
}
//****************************************************
//* Get a list of -g key parameters
//****************************************************
void glist() {
int i;
printf("\n # Length Type Description \n--------------------------------------");
for (i=0; i<signbaselen; i++) {
printf("\n%1i %5i %2i %s",i,signbase[i].len,signbase[i].type,signbase[i].descr);
}
printf("\n\n You can also specify arbitrary signature parameters in the format:\n -g *,type,len\n\n");
exit(0);
}
//***************************************************
//* Processing -g option parameters
//***************************************************
void gparm(char* sparm) {
int index;
char* sptr;
char parm[100];
if (gflag != 0) {
printf("\n Duplicate option -g\n\n");
exit(-1);
}
strcpy(parm,sparm); // local copy of parameters
if (parm[0] == 'l') {
glist();
exit(0);
}
if (parm[0] == 'd') {
// disable signature auto-detection
gflag = -1;
return;
}
if (strncmp(parm,"*,",2) == 0) {
// arbitrary parameters
// select length
sptr=strrchr(parm,',');
if (sptr == 0) goto perror;
signlen=atoi(sptr+1);
*sptr=0;
// select partition type
sptr=strrchr(parm,',');
if (sptr == 0) goto perror;
signtype=atoi(sptr+1);
if (fw_description(signtype) == 0) {
printf("\n Option -g: Unknown firmware type - %i\n",signtype);
exit(-1);
}
}
else {
index=atoi(parm);
if (index >= signbaselen) goto perror;
signlen=signbase[index].len;
signtype=signbase[index].type;
}
gflag=1;
// printf("\nstr - %s",signver);
return;
perror:
printf("\n Incorrect value in option -g\n");
exit(-1);
}
//***************************************************
//* Sending digital signature
//***************************************************
void send_signver() {
uint32_t res;
// response to ^signver
unsigned char SVrsp[]={0x0d, 0x0a, 0x30, 0x0d, 0x0a, 0x0d, 0x0a, 0x4f, 0x4b, 0x0d, 0x0a};
uint8_t replybuf[200];
if (gflag == 0) {
// auto-detect digital signature
signtype=dload_id&0x7;
signlen=serach_sign();
if (signlen == -1) return; // no signature found in the file
}
printf("\n Digital Signature Type: %s (%i bytes)",fw_description(signtype),signlen);
sprintf(signver,"^SIGNVER=%i,0,%s,%i",signtype,signver_hash,signlen);
res=atcmd(signver,replybuf);
if ( (res<sizeof(SVrsp)) || (memcmp(replybuf,SVrsp,sizeof(SVrsp)) != 0) ) {
printf("\n ! Error checking digital signature - %02x\n",replybuf[2]);
exit(-2);
}
}
//***************************************************
//* Search for digital signature in firmware
//***************************************************
int32_t serach_sign() {
int i,j;
uint32_t pt;
uint32_t signsize;
for (i=0;i<2;i++) {
if (i == npart) break;
pt=*((uint32_t*)&ptable[i].pimage[ptable[i].hd.psize-4]);
if (pt == 0xffaaaffa) {
// signature found
signsize=*((uint32_t*)&ptable[i].pimage[ptable[i].hd.psize-12]);
// select public key hash
for(j=0;j<32;j++) {
sprintf(signver_hash+2*j,"%02X",ptable[i].pimage[ptable[i].hd.psize-signsize+6+j]);
}
return signsize;
}
}
// not found
return -1;
}