/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include <netinet/in.h>

#include "esl.h"
#include "es_int.h"
#include "csl.h"
#include "rsautil.h"
#include "aes.h"
#include "utils.h"

int
utilFillSig(const char* rsaFile, int sigSize, u8* data, int dataSize)
{
    int rv = ES_ERR_OK;
    int hsize;
    CSLOSRsaSig4096 sig;
    u8* rsa=NULL;
    FILE *fptr;

    /* sign */
    fptr = fopen(rsaFile, "rb");
    if (fptr==NULL) {
        fprintf(stderr, "unable to open file %s\n", rsaFile); 
        return ES_ERR_FILE_OPEN_FAILED;
    }
    rsa = RUTIL_ReadRsaKeyPair(fptr, NULL);
    fclose(fptr);
    hsize = sizeof(IOSCCertSigType) + sigSize + sizeof(IOSCSigDummy);
    RUTIL_SignData(rsa, data+hsize, dataSize-hsize, sig);
    switch (sigSize) {
        case (sizeof(CSLOSRsaSig4096)): 
            memcpy(((IOSCSigRsa4096*)data)->sig, sig, sizeof(CSLOSRsaSig4096));
            break;
        case (sizeof(CSLOSRsaSig2048)):
            memcpy(((IOSCSigRsa2048*)data)->sig, sig, sizeof(CSLOSRsaSig2048));
            break;
        default:
            rv = ES_ERR_INCORRECT_SIG_TYPE;
            goto out;
    }

out:
    RUTIL_FreeRsaKeyPair(rsa);

    return rv;
}

int 
utilVerify(const char* certFile, int sigSize, u8* data, int dataSize)
{
    int rv = ES_ERR_OK;
    struct stat statBuf;
    IOSCRootCert cert;

    stat(certFile, &statBuf);
    utilFread(certFile, (u8*)(&cert), statBuf.st_size);
    rv = utilVerify2(&cert, statBuf.st_size, sigSize, data, dataSize, 1);

    return rv;
}

int 
utilVerify2(const void* certData, int certSize, int sigSize, u8* data, int dataSize, int useRootCert)
{
    int rv = ES_ERR_OK;
    CSLOSSha1Hash hash;
    SHA1Context sha;
    int hsize;
    u32 expSize = sizeof(CSLOSRsaExponent);
    IOSCRootCert* cert = (IOSCRootCert*)certData;
    CSLOSRsaExponent exp = {0x01, 0x00, 0x01, 0x00};

    if (sigSize==sizeof(CSLOSEccSig)) {
        hsize = sizeof(IOSCCertSigType) + sigSize + sizeof(IOSCEccPublicPad) 
                + sizeof(IOSCSigDummy);
    } else {
        hsize = sizeof(IOSCCertSigType) + sigSize + sizeof(IOSCSigDummy);
    }
    SHA1Reset(&sha);
    SHA1Input(&sha, data+hsize , dataSize-hsize);
    SHA1Result(&sha, hash);

    if (sigSize==sizeof(CSLOSRsaSig4096)) {
        if (useRootCert) {
            rv=CSL_VerifyRsaSig4096(hash, cert->pubKey, ((IOSCSigRsa4096*)data)->sig, cert->exponent, expSize);
        } else {
            rv=CSL_VerifyRsaSig4096(hash, (u8*)certData, ((IOSCSigRsa4096*)data)->sig, exp, 3);
        }
    } else if (sigSize==sizeof(CSLOSRsaSig2048)) {
        if (certSize==sizeof(IOSCRsa4096RsaCert)) {
            rv=CSL_VerifyRsaSig2048(hash, 
                ((IOSCRsa4096RsaCert*)(cert))->pubKey, 
                ((IOSCSigRsa2048*)data)->sig,  
                ((IOSCRsa4096RsaCert*)(cert))->exponent, 
                expSize); 
        } else if (certSize==sizeof(IOSCRsa2048RsaCert)) {
            rv=CSL_VerifyRsaSig2048(hash, 
                ((IOSCRsa2048RsaCert*)(cert))->pubKey, 
                ((IOSCSigRsa2048*)data)->sig,  
                ((IOSCRsa2048RsaCert*)(cert))->exponent, 
                expSize); 
        } else {
            return ES_ERR_INCORRECT_CERT_LENGTH;
        }
    } else if (sigSize==sizeof(CSLOSEccSig)) {
        if (certSize==sizeof(IOSCRsa2048EccCert)) {
            rv = CSL_VerifyEccSig(hash, 
                 ((IOSCRsa2048EccCert*)(cert))->pubKey, 
                 ((IOSCSigEcc*)data)->sig);  
        }
        else{
            printf("Case Not Handled\n");
            return ES_ERR_INCORRECT_CERT_LENGTH;
        }

    } else {
        return ES_ERR_INCORRECT_SIG_LENGTH;
    }
    return rv;
}

int
utilFillEccSig(const char* pvtKeyFile, u8* data, int dataSize)
{
    int rv = ES_ERR_OK;
    int hsize, i;
    CSLOSEccPrivateKey pvtKey;
    CSLOSEccSigRand randN;
    CSLOSEccSig sig;
    CSLOSSha1Hash hash;
    SHA1Context sha;

    utilFread(pvtKeyFile, pvtKey, sizeof(CSLOSEccPrivateKey));
    hsize = sizeof(IOSCCertSigType) + sizeof(CSLOSEccSig) + sizeof(IOSCEccPublicPad) + sizeof(IOSCSigDummy);

    SHA1Reset(&sha);
    SHA1Input(&sha, data+hsize , dataSize-hsize);
    SHA1Result(&sha, hash);
    for (i=0; i<sizeof(CSLOSEccSigRand); i++) {
         randN[i] = rand();
    }

    rv = CSL_ComputeEccSig(hash, pvtKey, sig, randN);
    if (rv!=0) {
        printf("CSL_ComputeEccSig failed %d\n", rv);
        goto out;
    }
    memcpy(((IOSCSigEcc*)data)->sig, sig, sizeof(CSLOSEccSig));

out:
    return rv;
}
void utilFread(const char*name, u8* data, int size) 
{
    FILE * fptr;

    fptr = fopen(name, "rb"); 

    if (fptr==NULL) { 
        fprintf(stderr, "file open failed %s\n", name); 
        exit(1); \
    }

    if ((fread(data, sizeof(u8), size, fptr))!=size) { 
        fclose(fptr);
        fprintf(stderr, "file read failed %s\n", name); 
        exit(1); 
    } 

    fclose(fptr);
}

void utilFwrite(const char*name, u8* data, int size) 
{
    FILE * fptr;

    fptr = fopen(name, "wb"); 

    if (fptr==NULL) { 
        fprintf(stderr, "file open failed %s\n", name); 
        exit(1); \
    }

    if ((fwrite(data, sizeof(u8), size, fptr))!=size) { 
        fclose(fptr);
        fprintf(stderr, "file write failed %s\n", name); 
        exit(1); 
    } 

    fclose(fptr);
}

