HSMのリセットはHSMユーザしか行えないため、今のところオフに。

git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@92 ff987cc8-cf2f-4642-8568-d52cce064691
This commit is contained in:
kubodera_yuichi 2009-12-25 02:32:21 +00:00
parent 8fca7addd9
commit 796b29938d
2 changed files with 406 additions and 409 deletions

View File

@ -1,408 +1,403 @@
/* ==================================================================== /* ====================================================================
* Copyright (c) 1998-2008 The OpenSSL Project. All rights reserved. * Copyright (c) 1998-2008 The OpenSSL Project. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* *
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* *
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the * the documentation and/or other materials provided with the
* distribution. * distribution.
* *
* 3. All advertising materials mentioning features or use of this * 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment: * software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project * "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)" * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
* *
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without * endorse or promote products derived from this software without
* prior written permission. For written permission, please contact * prior written permission. For written permission, please contact
* openssl-core@openssl.org. * openssl-core@openssl.org.
* *
* 5. Products derived from this software may not be called "OpenSSL" * 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written * nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project. * permission of the OpenSSL Project.
* *
* 6. Redistributions of any form whatsoever must retain the following * 6. Redistributions of any form whatsoever must retain the following
* acknowledgment: * acknowledgment:
* "This product includes software developed by the OpenSSL Project * "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.openssl.org/)" * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
* *
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
* ==================================================================== * ====================================================================
* *
* This product includes cryptographic software written by Eric Young * This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim * (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com). * Hudson (tjh@cryptsoft.com).
* *
*/ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved. * All rights reserved.
* *
* This package is an SSL implementation written * This package is an SSL implementation written
* by Eric Young (eay@cryptsoft.com). * by Eric Young (eay@cryptsoft.com).
* The implementation was written so as to conform with Netscapes SSL. * The implementation was written so as to conform with Netscapes SSL.
* *
* This library is free for commercial and non-commercial use as long as * This library is free for commercial and non-commercial use as long as
* the following conditions are aheared to. The following conditions * the following conditions are aheared to. The following conditions
* apply to all code found in this distribution, be it the RC4, RSA, * apply to all code found in this distribution, be it the RC4, RSA,
* lhash, DES, etc., code; not just the SSL code. The SSL documentation * lhash, DES, etc., code; not just the SSL code. The SSL documentation
* included with this distribution is covered by the same copyright terms * included with this distribution is covered by the same copyright terms
* except that the holder is Tim Hudson (tjh@cryptsoft.com). * except that the holder is Tim Hudson (tjh@cryptsoft.com).
* *
* Copyright remains Eric Young's, and as such any Copyright notices in * Copyright remains Eric Young's, and as such any Copyright notices in
* the code are not to be removed. * the code are not to be removed.
* If this package is used in a product, Eric Young should be given attribution * If this package is used in a product, Eric Young should be given attribution
* as the author of the parts of the library used. * as the author of the parts of the library used.
* This can be in the form of a textual message at program startup or * This can be in the form of a textual message at program startup or
* in documentation (online or textual) provided with the package. * in documentation (online or textual) provided with the package.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* 1. Redistributions of source code must retain the copyright * 1. Redistributions of source code must retain the copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software * 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement: * must display the following acknowledgement:
* "This product includes cryptographic software written by * "This product includes cryptographic software written by
* Eric Young (eay@cryptsoft.com)" * Eric Young (eay@cryptsoft.com)"
* The word 'cryptographic' can be left out if the rouines from the library * The word 'cryptographic' can be left out if the rouines from the library
* being used are not cryptographic related :-). * being used are not cryptographic related :-).
* 4. If you include any Windows specific code (or a derivative thereof) from * 4. If you include any Windows specific code (or a derivative thereof) from
* the apps directory (application code) you must include an acknowledgement: * the apps directory (application code) you must include an acknowledgement:
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
* *
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* The licence and distribution terms for any publically available version or * The licence and distribution terms for any publically available version or
* derivative of this code cannot be changed. i.e. this code cannot simply be * derivative of this code cannot be changed. i.e. this code cannot simply be
* copied and put under another distribution licence * copied and put under another distribution licence
* [including the GNU Public Licence.] * [including the GNU Public Licence.]
*/ */
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <time.h> #include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <string.h> #include <string.h>
#ifdef USE_HSM #ifdef USE_HSM
#include "cr_hsm_code.h" #include "cr_hsm_code.h"
#endif // USE_HSM #endif // USE_HSM
// openssl // openssl
#include <openssl/sha.h> #include <openssl/sha.h>
#include <openssl/x509.h> #include <openssl/x509.h>
#include "cr_generate_id.h" #include "cr_generate_id.h"
#include "cr_generate_id_private.h" #include "cr_generate_id_private.h"
#include "cr_alloc.h" #include "cr_alloc.h"
static u64 generatingCount = 0; // 現在生成中のID(起動時からの通算) static u64 generatingCount = 0; // 現在生成中のID(起動時からの通算)
int cr_print_flag = 0; int cr_print_flag = 0;
// generate_id関数のイニシャライズ // generate_id関数のイニシャライズ
int cr_generate_id_initialize( u8 id_buf[CR_ID_BUF_SIZE] ) int cr_generate_id_initialize( u8 id_buf[CR_ID_BUF_SIZE] )
{ {
int ret_code = CR_GENID_SUCCESS; int ret_code = CR_GENID_INITIALIZE_OK;
// 生成カウンタの初期化 // 生成カウンタの初期化
generatingCount = 0; generatingCount = 0;
#ifdef USE_HSM // init for error info
ret_code = hsm_initialize(); InitErrorInfo();
if ( ret_code != CR_GENID_SUCCESS )
{ #ifdef USE_HSM
CR_ERR_BUFFER *cr_err_buf = (CR_ERR_BUFFER *)id_buf; ret_code = hsm_initialize();
memset( cr_err_buf, 0, sizeof( CR_ERR_BUFFER ) ); if ( ret_code != CR_GENID_SUCCESS )
cr_err_buf->errorCode = ret_code; {
GetErrorInfo( cr_err_buf->callStack, &cr_err_buf->recordSize ); CR_ERR_BUFFER *cr_err_buf = (CR_ERR_BUFFER *)id_buf;
#ifdef DEBUG_PRINT memset( cr_err_buf, 0, sizeof( CR_ERR_BUFFER ) );
printf( "CALL_STACK : %s\n", cr_err_buf->callStack ); cr_err_buf->errorCode = ret_code;
#endif GetErrorInfo( cr_err_buf->callStack, &cr_err_buf->recordSize );
return 0; #ifdef DEBUG_PRINT
} printf( "error(%d), CALL_STACK : %s\n", (int)cr_err_buf->errorCode, cr_err_buf->callStack );
#endif #endif
return 0;
ret_code = CR_GENID_INITIALIZE_OK; }
#endif
return ( ret_code );
} // cr_generate_id_initialize return ret_code;
} // cr_generate_id_initialize
// generate_id関数のファイナライズ
int cr_generate_id_finalize( void ) // generate_id関数のファイナライズ
{ int cr_generate_id_finalize( void )
int ok = CR_GENID_SUCCESS; {
int ok = CR_GENID_SUCCESS;
// openssl TOOD: ここでファイナライズ不要なら、削除
ERR_remove_state(0); // openssl TOOD: ここでファイナライズ不要なら、削除
EVP_cleanup(); ERR_remove_state(0);
CRYPTO_cleanup_all_ex_data(); EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
// HSM
#ifdef USE_HSM // HSM
hsm_finalize(); #ifdef USE_HSM
#endif // USE_HSM hsm_finalize();
#endif // USE_HSM
ok = CR_GENID_FINALIZE_OK;
ok = CR_GENID_FINALIZE_OK;
return ( ok );
} // cr_generate_id_finalize return ( ok );
} // cr_generate_id_finalize
/*
unsigned long ERR_get_error(void); // generate_id 関数
id_buf int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 bondingOption )
*/ {
int i;
int ret_code = CR_GENID_SUCCESS;
// generate_id 関数 CR_ID_BUFFER *cr_id_buf;
int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 bondingOption ) EC_KEY *deviceKeyPair = NULL;
{
int i; // エラー発生時に備えて、エラーバッファの初期化とトータルのID生成カウントセット
int ret_code = CR_GENID_SUCCESS; InitErrorInfo();
CR_ID_BUFFER *cr_id_buf; generatingCount++;
EC_KEY *deviceKeyPair = NULL;
#ifdef DEBUG_PRINT
// エラー発生時に備えて、エラーバッファの初期化とトータルのID生成カウントセット if( sizeof(CR_ID_BUFFER) != 256 ) {
InitErrorInfo(); printf( "CR_ID_BUFFER size error. %d\n", sizeof(CR_ID_BUFFER) );
generatingCount++; }
if( sizeof(CR_ERR_BUFFER) != 256 ) {
#ifdef DEBUG_PRINT printf( "CR_ERR_BUFFER size error. %d\n", sizeof(CR_ERR_BUFFER) );
if( sizeof(CR_ID_BUFFER) != 256 ) { }
printf( "CR_ID_BUFFER size error. %d\n", sizeof(CR_ID_BUFFER) ); #endif
}
if( sizeof(CR_ERR_BUFFER) != 256 ) { // ダイジェストアルゴリズムを追加する
printf( "CR_ERR_BUFFER size error. %d\n", sizeof(CR_ERR_BUFFER) ); OpenSSL_add_all_digests();
}
#endif //--------------------------------------------------------------
// 暗号処理初期化
// ダイジェストアルゴリズムを追加する //--------------------------------------------------------------
OpenSSL_add_all_digests();
// メモリリーク防止のため、オリジナルのアロケータを使用。
//-------------------------------------------------------------- CRYPTO_set_mem_functions(cr_mem_malloc, cr_mem_realloc, cr_mem_free);
// 暗号処理初期化 cr_mem_bufmgr_initialize();
//--------------------------------------------------------------
#ifdef MY_CRYPTO_DEBUG
// メモリリーク防止のため、オリジナルのアロケータを使用。 ERR_load_crypto_strings();
CRYPTO_set_mem_functions(cr_mem_malloc, cr_mem_realloc, cr_mem_free); #endif /* MY_CRYPTO_DEBUG */
cr_mem_bufmgr_initialize();
//--------------------------------------------------------------
#ifdef MY_CRYPTO_DEBUG // FuseIDバッファに固定データセット
ERR_load_crypto_strings(); //--------------------------------------------------------------
#endif /* MY_CRYPTO_DEBUG */ memset(id_buf, 0, CR_ID_BUF_SIZE);
//-------------------------------------------------------------- cr_id_buf = (CR_ID_BUFFER *)id_buf;
// FuseIDバッファに固定データセット cr_id_buf->magic_number = CR_GEN_ID_MAGICCODE; // HSM使用未使用でマジックコードが変わる。
//-------------------------------------------------------------- cr_id_buf->version = CR_GEN_ID_VERSION;
memset(id_buf, 0, CR_ID_BUF_SIZE);
//--------------------------------------------------------------
cr_id_buf = (CR_ID_BUFFER *)id_buf; // 引数のボンディングオプションをセット
cr_id_buf->magic_number = CR_GEN_ID_MAGICCODE; // HSM使用未使用でマジックコードが変わる。 //--------------------------------------------------------------
cr_id_buf->version = CR_GEN_ID_VERSION; cr_id_buf->bondingOption = bondingOption;
//-------------------------------------------------------------- //--------------------------------------------------------------
// 引数のボンディングオプションをセット // serialNo.セット
//-------------------------------------------------------------- //--------------------------------------------------------------
cr_id_buf->bondingOption = bondingOption; for( i = 0 ; i < CR_NUM_OF_SERIAL ; i++ ) {
cr_id_buf->serial[i] = serial[i]; /* serial[0] => ec priv key */
//-------------------------------------------------------------- }
// serialNo.セット
//-------------------------------------------------------------- #ifdef DEBUG_PRINT
for( i = 0 ; i < CR_NUM_OF_SERIAL ; i++ ) { if( cr_print_flag ) {
cr_id_buf->serial[i] = serial[i]; /* serial[0] => ec priv key */ printf("serialNo:\n");
} printf(" 0x%08x\n", (unsigned int)serial[0] );
printf(" 0x%08x%08x\n", (unsigned int)serial[2], (unsigned int)serial[1] );
#ifdef DEBUG_PRINT printf(" 0x%08x%08x\n", (unsigned int)serial[4], (unsigned int)serial[3] );
if( cr_print_flag ) { printf("\n");
printf("serialNo:\n"); }
printf(" 0x%08x\n", (unsigned int)serial[0] ); #endif /* DEBUG_PRINT */
printf(" 0x%08x%08x\n", (unsigned int)serial[2], (unsigned int)serial[1] );
printf(" 0x%08x%08x\n", (unsigned int)serial[4], (unsigned int)serial[3] ); //--------------------------------------------------------------
printf("\n"); // タイムスタンプセット
} //--------------------------------------------------------------
#endif /* DEBUG_PRINT */ ret_code = GetTimestamp( &cr_id_buf->year,
&cr_id_buf->month,
//-------------------------------------------------------------- &cr_id_buf->mday,
// タイムスタンプセット &cr_id_buf->hour,
//-------------------------------------------------------------- &cr_id_buf->min,
ret_code = GetTimestamp( &cr_id_buf->year, &cr_id_buf->sec,
&cr_id_buf->month, &cr_id_buf->expiryDate ); // デバイス証明書期限の元データもついでにセットしておく
&cr_id_buf->mday, if ( ret_code != CR_GENID_SUCCESS ) {
&cr_id_buf->hour, SetErrorInfo( __FUNCTION__, __LINE__ );
&cr_id_buf->min, goto end;
&cr_id_buf->sec, }
&cr_id_buf->expiryDate ); // デバイス証明書期限の元データもついでにセットしておく
if ( ret_code != CR_GENID_SUCCESS ) { //--------------------------------------------------------------
SetErrorInfo( __FUNCTION__, __LINE__ ); // 乱数を生成してセット
goto end; //--------------------------------------------------------------
} ret_code = GenerateRandom( cr_id_buf->random, CR_RANDOM_LENGTH );
if ( ret_code != CR_GENID_SUCCESS ) {
//-------------------------------------------------------------- SetErrorInfo( __FUNCTION__, __LINE__ );
// 乱数を生成してセット goto end;
//-------------------------------------------------------------- }
ret_code = GenerateRandom( cr_id_buf->random, CR_RANDOM_LENGTH ); DEBUG_PRINT_ARRAY( "rand:", (const char *)cr_id_buf->random, CR_RANDOM_LENGTH );
if ( ret_code != CR_GENID_SUCCESS ) {
SetErrorInfo( __FUNCTION__, __LINE__ ); //--------------------------------------------------------------
goto end; // 楕円曲線鍵ペアを生成
} //--------------------------------------------------------------
DEBUG_PRINT_ARRAY( "rand:", (const char *)cr_id_buf->random, CR_RANDOM_LENGTH ); ret_code = GenarateECCKeyPair( &deviceKeyPair, cr_id_buf->devicePrivKey );
if ( ret_code != CR_GENID_SUCCESS ) {
//-------------------------------------------------------------- SetErrorInfo( __FUNCTION__, __LINE__ );
// 楕円曲線鍵ペアを生成 goto end;
//-------------------------------------------------------------- }
ret_code = GenarateECCKeyPair( &deviceKeyPair, cr_id_buf->devicePrivKey );
if ( ret_code != CR_GENID_SUCCESS ) { //--------------------------------------------------------------
SetErrorInfo( __FUNCTION__, __LINE__ ); // 生成した鍵ペアをECDSAで動作確認
goto end; //--------------------------------------------------------------
} ret_code = TestECDSA( deviceKeyPair );
if ( ret_code != CR_GENID_SUCCESS ) {
//-------------------------------------------------------------- goto end;
// 生成した鍵ペアをECDSAで動作確認 }
//--------------------------------------------------------------
ret_code = TestECDSA( deviceKeyPair ); //--------------------------------------------------------------
if ( ret_code != CR_GENID_SUCCESS ) { // デバイス証明書生成 + 署名の付与 + 証明書期限セット
goto end; //--------------------------------------------------------------
} ret_code = GenerateCTRDeviceCert( deviceKeyPair,
cr_id_buf->serial[0],
//-------------------------------------------------------------- cr_id_buf->bondingOption,
// デバイス証明書生成 + 署名の付与 + 証明書期限セット cr_id_buf->deviceCertSign,
//-------------------------------------------------------------- &cr_id_buf->expiryDate );
ret_code = GenerateCTRDeviceCert( deviceKeyPair, if ( ret_code != CR_GENID_SUCCESS ) {
cr_id_buf->serial[0], SetErrorInfo( __FUNCTION__, __LINE__ );
cr_id_buf->bondingOption, goto end;
cr_id_buf->deviceCertSign, }
&cr_id_buf->expiryDate );
if ( ret_code != CR_GENID_SUCCESS ) { #if 0
SetErrorInfo( __FUNCTION__, __LINE__ ); DEBUG_PRINT_ARRAY( "deviceCertSign:", (const char *)cr_id_buf->deviceCertSign, ECDSA_SIGN_LENGTH );
goto end; #endif
}
//--------------------------------------------------------------
#if 0 // FuseIDバッファ全体のSHA256ハッシュを算出してセット
DEBUG_PRINT_ARRAY( "deviceCertSign:", (const char *)cr_id_buf->deviceCertSign, ECDSA_SIGN_LENGTH ); //--------------------------------------------------------------
#endif SHA256(id_buf, CR_ID_BUF_SIZE - SHA256_DIGEST_LENGTH, cr_id_buf->hash);
DEBUG_PRINT_ARRAY( "SHA256 Digest:", (const char *)cr_id_buf->hash, SHA256_DIGEST_LENGTH );
//--------------------------------------------------------------
// FuseIDバッファ全体のSHA256ハッシュを算出してセット //--------------------------------------------------------------
//-------------------------------------------------------------- // FuseID RAWデータ完成
SHA256(id_buf, CR_ID_BUF_SIZE - SHA256_DIGEST_LENGTH, cr_id_buf->hash); //--------------------------------------------------------------
DEBUG_PRINT_ARRAY( "SHA256 Digest:", (const char *)cr_id_buf->hash, SHA256_DIGEST_LENGTH ); DEBUG_PRINT_ARRAY( "RAW eFuseID:", (const char *)id_buf, CR_ID_BUF_SIZE );
#ifdef DEBUG_EFUSE_ID_OUTPUT_FILE
//-------------------------------------------------------------- DebugFileOutput( serial[ 0 ], "raw", id_buf, CR_ID_BUF_SIZE );
// FuseID RAWデータ完成 #endif // DEBUG_EFUSE_ID_OUTPUT_FILE
//--------------------------------------------------------------
DEBUG_PRINT_ARRAY( "RAW eFuseID:", (const char *)id_buf, CR_ID_BUF_SIZE ); //--------------------------------------------------------------
#ifdef DEBUG_EFUSE_ID_OUTPUT_FILE // FuseIDバッファ全体をAES or RSAで暗号化
DebugFileOutput( serial[ 0 ], "raw", id_buf, CR_ID_BUF_SIZE ); //--------------------------------------------------------------
#endif // DEBUG_EFUSE_ID_OUTPUT_FILE ret_code = EncryptID( id_buf, id_buf, bondingOption );
if( ret_code != CR_GENID_SUCCESS ) {
//-------------------------------------------------------------- SetErrorInfo( __FUNCTION__, __LINE__ );
// FuseIDバッファ全体をAES or RSAで暗号化 goto end;
//-------------------------------------------------------------- }
ret_code = EncryptID( id_buf, id_buf, bondingOption );
if( ret_code != CR_GENID_SUCCESS ) { DEBUG_PRINT_ARRAY( "ENC eFuseID:", (const char *)id_buf, CR_ID_BUF_SIZE );
SetErrorInfo( __FUNCTION__, __LINE__ ); #ifdef DEBUG_EFUSE_ID_OUTPUT_FILE
goto end; DebugFileOutput( serial[ 0 ], "enc", id_buf, CR_ID_BUF_SIZE );
} #endif // DEBUG_EFUSE_ID_OUTPUT_FILE
DEBUG_PRINT_ARRAY( "ENC eFuseID:", (const char *)id_buf, CR_ID_BUF_SIZE ); //--------------------------------------------------------------
#ifdef DEBUG_EFUSE_ID_OUTPUT_FILE // 終了処理
DebugFileOutput( serial[ 0 ], "enc", id_buf, CR_ID_BUF_SIZE ); //--------------------------------------------------------------
#endif // DEBUG_EFUSE_ID_OUTPUT_FILE end:
//-------------------------------------------------------------- /* id_buf[]にエラーログを書き込む。 */
// 終了処理 // TODO: 仕様をFIXする必要あり -> TORIAEZU version
//-------------------------------------------------------------- if ( ret_code != CR_GENID_SUCCESS )
end: {
CR_ERR_BUFFER *cr_err_buf = (CR_ERR_BUFFER *)id_buf;
/* id_buf[]にエラーログを書き込む。 */ memset( cr_err_buf, 0, sizeof( CR_ERR_BUFFER ) );
// TODO: 仕様をFIXする必要あり -> TORIAEZU version cr_err_buf->totalCount = generatingCount;
if ( ret_code != CR_GENID_SUCCESS ) cr_err_buf->magic_number = 0x01234567;
{ cr_err_buf->serial0 = serial[0];
CR_ERR_BUFFER *cr_err_buf = (CR_ERR_BUFFER *)id_buf; cr_err_buf->errorCode = ret_code;
memset( cr_err_buf, 0, sizeof( CR_ERR_BUFFER ) ); GetErrorInfo( cr_err_buf->callStack, &cr_err_buf->recordSize );
cr_err_buf->totalCount = generatingCount; cr_err_buf->bondingOption = bondingOption;
cr_err_buf->magic_number = 0x01234567; #ifdef DEBUG_PRINT
cr_err_buf->serial0 = serial[0]; printf( "CALL_STACK : %s\n", cr_err_buf->callStack );
cr_err_buf->errorCode = ret_code; #endif
GetErrorInfo( cr_err_buf->callStack, &cr_err_buf->recordSize ); }
cr_err_buf->bondingOption = bondingOption;
#ifdef DEBUG_PRINT // リソースの解放
printf( "CALL_STACK : %s\n", cr_err_buf->callStack ); if ( deviceKeyPair ) EC_KEY_free( deviceKeyPair );
#endif
} ERR_remove_state(0);
EVP_cleanup();
// リソースの解放 CRYPTO_cleanup_all_ex_data();
if ( deviceKeyPair ) EC_KEY_free( deviceKeyPair );
#ifdef MY_CRYPTO_DEBUG
ERR_remove_state(0); ERR_free_strings();
EVP_cleanup(); #endif /* MY_CRYPTO_DEBUG */
CRYPTO_cleanup_all_ex_data();
return ret_code; /* success */
#ifdef MY_CRYPTO_DEBUG }
ERR_free_strings();
#endif /* MY_CRYPTO_DEBUG */
#ifdef DEBUG_PRINT
return ret_code; /* success */ void DebugPrintArray( char *pStr, const u8 *pData, int length )
} {
int i;
if( cr_print_flag ) {
#ifdef DEBUG_PRINT printf( "%s", pStr );
void DebugPrintArray( char *pStr, const u8 *pData, int length ) for( i = 0 ; i < length; i++ ) {
{ if( (i % 16) == 0 ) printf("\n ");
int i; printf("%02X ", pData[ i ] );
if( cr_print_flag ) { }
printf( "%s", pStr ); printf("\n");
for( i = 0 ; i < length; i++ ) { }
if( (i % 16) == 0 ) printf("\n "); }
printf("%02X ", pData[ i ] ); #endif
}
printf("\n"); void DebugFileOutput( u32 deviceId, char *pSuffix, const u8 *pSrc, int length )
} {
} if ( cr_print_flag )
#endif {
// 証明書の書き込みテスト
void DebugFileOutput( u32 deviceId, char *pSuffix, const u8 *pSrc, int length ) FILE *fp;
{ char fn[256];
if ( cr_print_flag ) sprintf( fn, "output/0x%08x.%s", (unsigned int)deviceId, pSuffix );
{ fp = fopen( fn, "wb" );
// 証明書の書き込みテスト fwrite( pSrc, length, 1, fp );
FILE *fp; fclose( fp );
char fn[256]; }
sprintf( fn, "output/0x%08x.%s", (unsigned int)deviceId, pSuffix ); }
fp = fopen( fn, "wb" );
fwrite( pSrc, length, 1, fp );
fclose( fp );
}
}

View File

@ -155,13 +155,15 @@ int hsm_initialize( void )
{ {
int ret_code = CR_GENID_SUCCESS; int ret_code = CR_GENID_SUCCESS;
// TODO: HSMリセットを入れる。 // HSM のリセット処理
#ifdef EXE_AUTH__OK
ret_code = hsm_reset_module(); ret_code = hsm_reset_module();
if ( ret_code != CR_GENID_SUCCESS ) if ( ret_code != CR_GENID_SUCCESS )
{ {
SetErrorInfo( __FUNCTION__, __LINE__ ); SetErrorInfo( __FUNCTION__, __LINE__ );
return ret_code; return ret_code;
} }
#endif
// init HSM // init HSM
ret_code = NFastApp_InitEx( &hsmHandle, NULL, NULL ); ret_code = NFastApp_InitEx( &hsmHandle, NULL, NULL );