diff --git a/cr_deviceCert.c b/cr_deviceCert.c index fadc8ee..20c3da9 100644 --- a/cr_deviceCert.c +++ b/cr_deviceCert.c @@ -164,7 +164,7 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 deviceId, u8 bondingOption, u8 *p if ( sizeof( CR_DeviceCert ) > 384 ) { ret_code = CR_GENID_ERROR_CERT_BUF_SIZE; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -246,7 +246,7 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 deviceId, u8 bondingOption, u8 *p test_ret = ECDSA_sign( 0, sha256buf, 233/8, ecdsasig, &signLen, NintendoCTR2 ); if (test_ret == 0) { ret_code = CR_GENID_ERROR_ECDSA_SIGN; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } DEBUG_PRINT_ARRAY( "ECDSA:", (const char *)ecdsasig, signLen ); @@ -255,7 +255,7 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 deviceId, u8 bondingOption, u8 *p test_ret = ECDSA_verify( 0, sha256buf, 233/8, ecdsasig, signLen, NintendoCTR2 ); if( test_ret != 1) { ret_code = CR_GENID_ERROR_ECDSA_VERIFY; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -263,7 +263,7 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 deviceId, u8 bondingOption, u8 *p sig = d2i_ECDSA_SIG( NULL, &pECDSAsig, signLen ); if( sig == NULL ) { ret_code = CR_GENID_ERROR_ECDSA_DECODE; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } #if 0 diff --git a/cr_enc_id.c b/cr_enc_id.c index 1e7458e..1be95ae 100644 --- a/cr_enc_id.c +++ b/cr_enc_id.c @@ -176,7 +176,7 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf, u8 bondi ret_code = hsm_aes_encrypt( local_buf_1, org_buf, CR_ID_BUF_SIZE, bondingOption ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -184,7 +184,7 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf, u8 bondi ret_code = hsm_aes_decrypt( local_buf_2, local_buf_1, CR_ID_BUF_SIZE, bondingOption ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -194,7 +194,7 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf, u8 bondi if( org_buf[i] != local_buf_2[i] ) { ret_code = CR_GENID_ERROR_AES_VERIFY; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } } @@ -221,13 +221,13 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf, u8 bondi if ( AES_set_encrypt_key( pAesKey, 128, &aesEncKey ) != 0 ) { - SetErrorInfo( CR_GENID_ERROR_AES_ENC, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_AES_ENC; } if ( AES_set_decrypt_key( pAesKey, 128, &aesDecKey ) != 0 ) { - SetErrorInfo( CR_GENID_ERROR_AES_DEC, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_AES_DEC; } @@ -242,7 +242,7 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf, u8 bondi { if( org_buf[i] != local_buf_2[i] ) { - SetErrorInfo( CR_GENID_ERROR_AES_VERIFY, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_AES_VERIFY; } } @@ -268,7 +268,7 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondin ret_code = hsm_rsa_encrypt( local_buf_1, org_buf, CR_ID_BUF_SIZE, bondingOption ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -276,7 +276,7 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondin ret_code = hsm_rsa_decrypt( local_buf_2, local_buf_1, CR_ID_BUF_SIZE, bondingOption ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -286,7 +286,7 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondin if( org_buf[i] != local_buf_2[i] ) { ret_code = CR_GENID_ERROR_RSA_VERIFY; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } } @@ -321,14 +321,14 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondin rsa_privkey = d2i_RSAPrivateKey( NULL, &der_priv, priv_len ); if( rsa_privkey == NULL ) { ret_code = CR_GENID_ERROR_RSA_READ_PRIVATE_KEY; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } // コマンドラインのopensslが出力する公開鍵は、SubjectPublicKeyInfo形式なので、この関数を使う。 rsa_pubkey = d2i_RSA_PUBKEY( NULL, &der_pub, pub_len ); if( rsa_pubkey == NULL ) { ret_code = CR_GENID_ERROR_RSA_READ_PUBLIC_KEY; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } } @@ -336,14 +336,14 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondin if( (rsa_outlen = RSA_private_encrypt(CR_ID_BUF_SIZE, org_buf, local_buf_1, rsa_privkey, RSA_NO_PADDING)) == -1) { ret_code = CR_GENID_ERROR_RSA_ENC; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } else { if((rsa_outlen = RSA_public_decrypt(rsa_outlen, local_buf_1, local_buf_2, rsa_pubkey, RSA_NO_PADDING)) == -1) { ret_code = CR_GENID_ERROR_RSA_DEC; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } else { @@ -356,7 +356,7 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondin } if( error_flag ) { ret_code = CR_GENID_ERROR_RSA_VERIFY; - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } } diff --git a/cr_generate_id.c b/cr_generate_id.c index 03d3263..2051b27 100644 --- a/cr_generate_id.c +++ b/cr_generate_id.c @@ -185,15 +185,19 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 CR_ID_BUFFER *cr_id_buf; EC_KEY *deviceKeyPair = NULL; + // 繧ィ繝ゥ繝シ逋コ逕滓凾縺ォ蛯吶∴縺ヲ縲√お繝ゥ繝シ繝舌ャ繝輔ぃ縺ョ蛻晄悄蛹悶→繝医シ繧ソ繝ォ縺ョID逕滓舌き繧ヲ繝ウ繝医そ繝繝 + InitErrorInfo(); generatingCount++; if( sizeof(CR_ID_BUFFER) != 256 ) { ret_code = CR_GENID_ERROR_ID_BUF_SIZE; + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } if( sizeof(CR_ERR_BUFFER) > 256 ) { ret_code = CR_GENID_ERROR_ERR_BUF_SIZE; + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -254,7 +258,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 &cr_id_buf->sec, &cr_id_buf->expiryDate ); // 繝繝舌う繧ケ險シ譏取嶌譛滄剞縺ョ蜈繝繝シ繧ソ繧ゅ▽縺縺ァ縺ォ繧サ繝繝医@縺ヲ縺翫¥ if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -263,7 +267,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 //-------------------------------------------------------------- ret_code = GenerateRandom( cr_id_buf->random, CR_RANDOM_LENGTH ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } DEBUG_PRINT_ARRAY( "rand:", (const char *)cr_id_buf->random, CR_RANDOM_LENGTH ); @@ -273,7 +277,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 //-------------------------------------------------------------- ret_code = GenarateECCKeyPair( &deviceKeyPair, cr_id_buf->devicePrivKey ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -294,7 +298,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 cr_id_buf->deviceCertSign, &cr_id_buf->expiryDate ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -321,7 +325,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 //-------------------------------------------------------------- ret_code = EncryptID( id_buf, id_buf, bondingOption ); if( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -344,10 +348,10 @@ end: cr_err_buf->totalCount = generatingCount; cr_err_buf->magic_number = 0x01234567; cr_err_buf->serial0 = serial[0]; - GetErrorInfo( &(cr_err_buf->errorCode), - cr_err_buf->fileName, - &cr_err_buf->lineNumber ); + cr_err_buf->errorCode = ret_code; + GetErrorInfo( cr_err_buf->callStack, &cr_err_buf->recordSize ); cr_err_buf->bondingOption = bondingOption; + printf( "%s\n", cr_err_buf->callStack ); } // 繝ェ繧ス繝シ繧ケ縺ョ隗」謾セ diff --git a/cr_generate_id_private.h b/cr_generate_id_private.h index 30cbf2e..db04061 100644 --- a/cr_generate_id_private.h +++ b/cr_generate_id_private.h @@ -184,23 +184,24 @@ typedef struct { } CR_ID_BUFFER; /* 合計256bytes = 2048bit */ // TODO: 仕様FIXする必要あり -> 仮 version +#define CALL_STACK_SIZE (234) typedef struct { u64 totalCount; /* 0x00 - 0x07 */ u32 magic_number; /* 0x08 - 0x0b 0x01234567 確定!*/ u32 serial0; /* 0x0c - 0x0f */ s32 errorCode; /* 0x10 - 0x13 */ - u32 lineNumber; /* 0x14 - 0x17 */ - char fileName[ 64 ]; /* 0x18 - 0x57 */ - u8 bondingOption; /* 0x58 - 0x58 */ - u8 reserve[ 167 ]; /* 0x59 - 0xff */ + u8 bondingOption; // TORIAEZU + u8 recordSize; // コールスタックの記録サイズ + u8 callStack[ CALL_STACK_SIZE ]; } CR_ERR_BUFFER; extern int GetTimestamp( u8 *pYear, u8 *pMonth, u8 *pMday, u8 *pHour, u8 *pMin, u8 *pSec, time_t *pTime); extern int GenerateRandom( u8 *pDst, int length ); extern int GenarateECCKeyPair( EC_KEY **ppECkey, u8 *pECPrivkey ); -extern void SetErrorInfo( s32 errorCode, char *fileName, u32 line ); -extern void GetErrorInfo( s32 *errorCode, char *fileName, u32 *line ); +extern void InitErrorInfo( void ); +extern void SetErrorInfo( const char *funcName, u32 line ); +extern void GetErrorInfo( char *stack, u8 *size ); extern int TestECDSA( EC_KEY *pECkey ); extern int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 deviceId, u8 bondingOption, u8 *pDevCertSign, u32 *pExpiryDate ); diff --git a/cr_hsm_code.c b/cr_hsm_code.c index 240afcd..306428f 100644 --- a/cr_hsm_code.c +++ b/cr_hsm_code.c @@ -173,7 +173,7 @@ int hsm_initialize( void ) NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -181,7 +181,7 @@ int hsm_initialize( void ) ret_code = NFastApp_Connect( hsmHandle, &hsmConnection, 0 /* flag */, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -189,7 +189,7 @@ int hsm_initialize( void ) ret_code = NFKM_getinfo( hsmHandle, &hsmWorld, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -198,7 +198,7 @@ int hsm_initialize( void ) ret_code = hsm_aes_load_key( hsmAesKeyidentDev, &hsmAesKeyidDev ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -206,7 +206,7 @@ int hsm_initialize( void ) ret_code = hsm_aes_load_key( hsmAesKeyidentProd, &hsmAesKeyidProd ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } #else // !ENCRYPT_AES @@ -214,7 +214,7 @@ int hsm_initialize( void ) ret_code = hsm_rsa_load_keypair( hsmRsaKeyidentDev, &hsmRsaPrivkeyidDev, &hsmRsaPubkeyidDev ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -222,7 +222,7 @@ int hsm_initialize( void ) ret_code = hsm_rsa_load_keypair( hsmRsaKeyidentProd, &hsmRsaPrivkeyidProd, &hsmRsaPubkeyidProd ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } #endif // ENCRYPT_AES @@ -254,13 +254,13 @@ int hsm_generate_random( unsigned char *buf, int bytes ) ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } ret_code = reply.status; if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -291,13 +291,13 @@ int hsm_get_rtc( time_t *time ) ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } ret_code = reply.status; if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -322,7 +322,7 @@ int hsm_aes_load_key( NFKM_KeyIdent keyident, M_KeyID *keyid ) ret_code = NFKM_findkey( hsmHandle, keyident, &keyinfo, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -331,7 +331,7 @@ int hsm_aes_load_key( NFKM_KeyIdent keyident, M_KeyID *keyid ) ret_code = NFKM_getusablemodule( hsmWorld, HSM_MODULE_ID, &moduleinfo ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -342,7 +342,7 @@ int hsm_aes_load_key( NFKM_KeyIdent keyident, M_KeyID *keyid ) 0, keyid, "loading aes key blob", NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -386,13 +386,13 @@ int hsm_aes_encrypt( unsigned char *dst_buf, unsigned char *org_buf, int size, u ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } ret_code = reply.status; if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -439,13 +439,13 @@ int hsm_aes_decrypt( unsigned char *dst_buf, unsigned char *org_buf, int size, u ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } ret_code = reply.status; if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -471,7 +471,7 @@ int hsm_rsa_load_keypair( NFKM_KeyIdent keyident, M_KeyID *privKeyid, M_KeyID *p ret_code = NFKM_findkey( hsmHandle, keyident, &keyinfo, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -480,7 +480,7 @@ int hsm_rsa_load_keypair( NFKM_KeyIdent keyident, M_KeyID *privKeyid, M_KeyID *p ret_code = NFKM_getusablemodule( hsmWorld, HSM_MODULE_ID, &moduleinfo ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -492,7 +492,7 @@ int hsm_rsa_load_keypair( NFKM_KeyIdent keyident, M_KeyID *privKeyid, M_KeyID *p "loading priv key blob", NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -504,7 +504,7 @@ int hsm_rsa_load_keypair( NFKM_KeyIdent keyident, M_KeyID *privKeyid, M_KeyID *p "loading pub key blob", NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); goto end; } @@ -541,13 +541,13 @@ int hsm_rsa_encrypt( unsigned char *dst_buf, unsigned char *org_buf, int size, u ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } ret_code = reply.status; if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -588,13 +588,13 @@ int hsm_rsa_decrypt( unsigned char *dst_buf, unsigned char *org_buf, int size, u ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } ret_code = reply.status; if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } diff --git a/cr_hsm_util.c b/cr_hsm_util.c index c5d056b..9686aa7 100644 --- a/cr_hsm_util.c +++ b/cr_hsm_util.c @@ -126,9 +126,10 @@ typedef struct { - s32 errorCode; - u32 lineNumber; - char fileName[64]; + u8 stop; + u8 position; + u8 emptySize; + u8 stack[ CALL_STACK_SIZE ]; } errorInfoStruct; @@ -236,34 +237,45 @@ int GenerateRandom( u8 *pDst, int length ) return ret_code; } -// エラー情報の記録 -void SetErrorInfo( s32 errorCode, char fileName[], u32 line ) +void InitErrorInfo( void ) { - static u8 stored = 0; - - if ( !stored ) + memset( &errorInfo, 0, sizeof( errorInfo ) ); + errorInfo.emptySize = CALL_STACK_SIZE; +} // InitErrorInfo + +// エラー情報の記録 +void SetErrorInfo( const char *funcName, u32 line ) +{ + if ( !errorInfo.stop ) { - stored = 1; + int len; + char str[64]; - memset( &errorInfo, 0, sizeof( errorInfo ) ); + sprintf( str, "%s:%d ", funcName, (int)line ); + len = strlen( str ); - errorInfo.errorCode = errorCode; - strncpy( errorInfo.fileName, fileName, strlen( fileName ) ); - errorInfo.lineNumber = line; + if ( len > errorInfo.emptySize ) + { + errorInfo.stop = 1; + return; + } + + memcpy( &errorInfo.stack[ errorInfo.position ], str, len ); + errorInfo.position += len; + errorInfo.emptySize -= len; } #if 0 else { - printf( "Error info was already stored!\n" ); + printf( "Stack is full!\n" ); } -#endif +#endif } // StoreErrorInfo // エラー情報の取得 -void GetErrorInfo( s32 *errorCode, char *fileName, u32 *line ) +void GetErrorInfo( char *stack, u8 *size ) { - *errorCode = errorInfo.errorCode; - strncpy( fileName, errorInfo.fileName, strlen( errorInfo.fileName ) ); - *line = errorInfo.lineNumber; + memcpy( stack, errorInfo.stack, CALL_STACK_SIZE ); + *size = CALL_STACK_SIZE - errorInfo.emptySize; } // GetErrorInfo diff --git a/cr_keyPair.c b/cr_keyPair.c index 0eb96b6..8e8c3d9 100644 --- a/cr_keyPair.c +++ b/cr_keyPair.c @@ -137,7 +137,7 @@ int GenarateECCKeyPair( EC_KEY **ppECkey, u8 *pECPrivkey ) *ppECkey = EC_KEY_new_by_curve_name( NID_sect233r1 ); if( *ppECkey == NULL ) { - SetErrorInfo( CR_GENID_ERROR_EC_KEY_NEW_1, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_EC_KEY_NEW_1; } @@ -145,7 +145,7 @@ int GenarateECCKeyPair( EC_KEY **ppECkey, u8 *pECPrivkey ) openssl_result = generate_EC_private_key( *ppECkey, pECPrivkey ); if( openssl_result != 0 ) { - SetErrorInfo( CR_GENID_ERROR_EC_GENERATE_PRIVATE_KEY, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_EC_GENERATE_PRIVATE_KEY; } @@ -153,7 +153,7 @@ int GenarateECCKeyPair( EC_KEY **ppECkey, u8 *pECPrivkey ) openssl_result = generate_EC_public_key( *ppECkey ); if ( openssl_result == 0 ) { - SetErrorInfo( CR_GENID_ERROR_EC_GENERATE_PUBLIC_KEY, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_EC_GENERATE_PUBLIC_KEY; } @@ -174,7 +174,7 @@ static int generate_EC_private_key( EC_KEY *eckey, u8 *privKey ) // 荵ア謨ー繧貞叙蠕励@縺ヲ縲∫ァ伜ッ骰オ縺ォ縺吶k縲 ret_code = GenerateRandom( privKey, EC_PRIVATE_KEY_LENGTH ); if ( ret_code != CR_GENID_SUCCESS ) { - SetErrorInfo( ret_code, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return ret_code; } @@ -190,7 +190,7 @@ static int generate_EC_private_key( EC_KEY *eckey, u8 *privKey ) bn_privkey = BN_new(); if( bn_privkey == NULL ) { - SetErrorInfo( CR_GENID_ERROR_BN_NEW_0, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_BN_NEW_0; } BN_init( bn_privkey ); /* memset(a,0,sizeof(BIGNUM)); */ @@ -296,13 +296,13 @@ int TestECDSA( EC_KEY *pECkey ) openssl_result = ECDSA_sign( 0, ecdsa_test_buf, CR_ECDSA_BUF_SIZE, ecdsasig, &ecdsasiglen, pECkey ); if (openssl_result == 0) { - SetErrorInfo( CR_GENID_ERROR_ECDSA_SIGN, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_ECDSA_SIGN; } openssl_result = ECDSA_verify( 0, ecdsa_test_buf, CR_ECDSA_BUF_SIZE, ecdsasig, ecdsasiglen, pECkey ); if( openssl_result != 1) { - SetErrorInfo( CR_GENID_ERROR_ECDSA_VERIFY, __FILE__, __LINE__ ); + SetErrorInfo( __FUNCTION__, __LINE__ ); return CR_GENID_ERROR_ECDSA_VERIFY; }