tickcheck.c 2.62 KB
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/unistd.h>
 
#include <util.h>
#include <PR/bbticket.h>

void usage()
{
    printf("ticketcheck <ticket.sys> <cert.sys>\n");
}

int main(int argc, char **argv)
{
    FILE *fp;
    u8 *tickets=NULL,*certs=NULL;
    u32 numTickets,numCerts;
    BbTicket *ticket;
    BbRsaCert *signer;
    int i,j,notok;

    if(argc!=3){
        usage();
        return 1;
    }

    /* read ticket file */
    if((fp = fopen(argv[1], "r"))==NULL){
        fprintf(stderr,"Failed to open ticket file, %s\n",argv[1]);
        goto exit;
    }
    fread(&numTickets,1,4,fp);
    numTickets = ntohl(numTickets);
    tickets = malloc(numTickets*sizeof(BbTicket));
    fread(tickets,1,numTickets*sizeof(BbTicket),fp);
    fclose(fp);

    /* read certs file */
    if((fp = fopen(argv[2], "r"))==NULL){
        fprintf(stderr,"Failed to open cert file, %s\n",argv[2]);
        goto exit;
    }
    fread(&numCerts,1,4,fp);
    numCerts = ntohl(numCerts);
    certs = malloc(numCerts*sizeof(BbRsaCert));
    fread(certs,1,numCerts*sizeof(BbRsaCert),fp);
    fclose(fp);

    printf("\n");
    for(j=0, ticket=(BbTicket *)tickets; j<numTickets; j++, ticket++){
        printf("bb id: 0x%x\n", ntohl(ticket->head.bbId));
        printf("ticket id: 0x%x\n", ntohs(ticket->head.tid));
        printf("code: 0x%x\n", ntohs(ticket->head.code));
        printf("limit: 0x%x\n", ntohs(ticket->head.limit));
        printf("ts: %s\n", ticket->head.issuer);
        printf("ts crlv: 0x%x\n", ntohl(ticket->head.tsCrlVersion));
        printf("content id: 0x%x\n", ntohl(ticket->cmd.head.id));
        printf("content size: 0x%x\n", ntohl(ticket->cmd.head.size));
        printf("content bb id: 0x%x\n", ntohl(ticket->cmd.head.bbid));
        printf("content exec: 0x%x\n", ntohl(ticket->cmd.head.execFlags));
        printf("content hw rights: 0x%x\n", ntohl(ticket->cmd.head.hwAccessRights));
        printf("content sk rights: 0x%x\n", ntohl(ticket->cmd.head.secureKernelRights));
        printf("cp: %s\n", ticket->cmd.head.issuer);
        printf("cp crlv: %x\n", ntohl(ticket->cmd.head.cpCrlVersion));
        printf("cp ca crlv: %x\n", ntohl(ticket->cmd.head.caCrlVersion));

        for(i=0, signer=(BbRsaCert *)certs; i<numCerts; i++, signer++){
            if(strcmp(ticket->head.issuer+18,signer->certId.name.server)==0)
                break; 
        }
        notok = verifyTicketSig(ticket, signer);
        printf("sig check result: ");
        if(notok)
            printf("BAD\n\n");
        else
            printf("GOOD\n\n");
    }


exit:    
    if(certs)free(certs);
    if(tickets)free(tickets);
    return notok;
}