static #endif AVP_dword DSCLAPI DSKMCheckSign( HDSKM hDSKM, DSKMSign *pSign, DSKMHash *pHash, void *pKey, AVP_dword dwKeySize ) { AVP_byte sign[SIGN_FULL_LEN + 2]; TextToBin( pSign->sign, sizeof(pSign->sign)/*SIGN_TXT_LEN*/, sign, SIGN_FULL_LEN + 2 ); #ifdef DSKM_EMUL { int i; HPROP hProp = DATA_Find_Prop( (HDATA)hDSKM, 0, DSKM_OBJECT_ERROR_ID ); if ( !hProp ) { DATA_Add_Prop( (HDATA)hDSKM, 0, DSKM_OBJECT_ERROR_ID, DSKM_ERR_INVALID_SIGN, 0 ); } for ( i=0; i<SIGN_BIN_LEN; ) { unsigned long j; for ( j=0; j<dwKeySize && i<SIGN_BIN_LEN; i++,j++ ) { sign[i] ^= ((unsigned char *)pKey)[j]; } } AVP_dword dwResult = !ds_mcmp(pHash->hash, pSign->sign, sizeof(pSign->sign)) == RET_OK ? DSKM_ERR_OK : DSKM_ERR_INVALID_SIGN; DATA_Set_Val( (HDATA)hDSKM, 0, DSKM_OBJECT_ERROR_ID, dwResult, 0 ); } #else { //DWORD dwErrors[2] = { DSKM_ERR_OK, DSKM_ERR_INVALID_SIGN }; struct LPoint rcPub; HPROP hProp = DATA_Find_Prop( (HDATA)hDSKM, 0, DSKM_OBJECT_ERROR_ID ); ds_mset( &rcPub, 0, sizeof(rcPub) ); //ds_mcpy( &rcPub.Z, dwErrors, sizeof(dwErrors) ); if ( !hProp ) { DATA_Add_Prop( (HDATA)hDSKM, 0, DSKM_OBJECT_ERROR_ID, rcPub.Type = DSKM_ERR_INVALID_SIGN, 0 ); } hProp = DATA_Find_Prop( (HDATA)hDSKM, 0, DSKM_CLCONT_C_ID ); if ( hProp && sign[0] == SIGN_VERSION && sign[1] == SIGN_BIN_LEN ) { struct LR34_10_2001 *pCContext = 0; PROP_Get_Val( hProp, &pCContext, sizeof(pCContext) ); if ( pCContext ) { ds_mcpy( &rcPub, pKey, sizeof(rcPub.X) + sizeof(rcPub.Y) ); rcPub.Type = Affi; if ( CrypC_LR34_10_2001_set_public_key(pCContext, &rcPub) == RET_OK ) { AVP_dword dwPid = DSKM_OBJECT_ERROR_IDA(CrypC_LR34_10_2001_verify(pCContext, pHash->hash, sign + SIGN_AUX_SIZE)); MAKE_ADDR3(eAddr, DSKM_CLFUNC_TABLE_ID, DSKM_OBJECT_ERROR_ID, dwPid ); DATA_Get_Val( (HDATA)hDSKM, eAddr, 0, &rcPub.Type, sizeof(rcPub.Type) ); //rcPub.Type = rcPub.Z[!!CrypC_LR34_10_2001_verify(pCContext, pHash->hash, sign + SIGN_AUX_SIZE)]; CrypC_LR34_10_2001_clear_public_key( pCContext ); } } } DATA_Set_Val( (HDATA)hDSKM, 0, DSKM_OBJECT_ERROR_ID, rcPub.Type, 0 ); ds_mset(&rcPub, 0, sizeof(rcPub)); ds_mset(pKey, 0, dwKeySize); } #endif return 0; }
int CEuc::UniToEucjp( const wchar_t* pSrc, const int nSrcLen, char* pDst, bool* pbError ) { int nclen; const unsigned short *pr, *pr_end; unsigned char* pw; bool berror=false, berror_tmp; ECharSet echarset; pr = reinterpret_cast<const unsigned short*>(pSrc); pr_end = reinterpret_cast<const unsigned short*>(pSrc + nSrcLen); pw = reinterpret_cast<unsigned char*>(pDst); while( (nclen = CheckUtf16leChar(reinterpret_cast<const wchar_t*>(pr), pr_end-pr, &echarset, 0)) > 0 ){ // 保護コード switch( echarset ){ case CHARSET_UNI_NORMAL: nclen = 1; break; case CHARSET_UNI_SURROG: nclen = 2; break; default: echarset = CHARSET_BINARY; nclen = 1; } if( echarset != CHARSET_BINARY ){ pw += _UniToEucjp_char( pr, pw, echarset, &berror_tmp ); // 保護コード if( berror_tmp == true ){ berror = true; } pr += nclen; }else{ if( nclen == 1 && IsBinaryOnSurrogate(static_cast<wchar_t>(*pr)) ){ *pw = static_cast<unsigned char>( TextToBin(*pr) & 0x00ff ); ++pw; }else{ // 保護コード berror = true; *pw = '?'; ++pw; } ++pr; } } if( pbError ){ *pbError = berror; } return pw - reinterpret_cast<unsigned char*>(pDst); }
// --- int sign_check_buffer( void* buffer, int buff_len, int* out_len, int kasp_key, void* keys[], int key_count, int all_of ) { int i; int found_one_of; CTN_Notary* not = 0; CTN_Hash hash; int sign_count = MAX_NOT_COUNT; void* notaries[MAX_NOT_COUNT]; char conv_buffer[ NOTARY_VALUE_BIN_LEN+2 ]; s_mset( notaries, 0, sizeof(notaries) ); MY_TRY { int j, res, local_len; if ( !buff_len ) return free_not( not, SIGN_BAD_PARAMETERS ); if ( keys && !key_count ) return free_not( not, SIGN_BAD_PARAMETERS ); if ( !keys && !kasp_key ) return free_not( not, SIGN_BAD_PARAMETERS ); if ( !out_len ) out_len = &local_len; res = sign_collect_notaries( buffer, buff_len, notaries, &sign_count, out_len ); if ( SIGN_OK != res ) return free_not( not, res ); if ( !sign_count ) return free_not( not, SIGN_NOTARY_NOT_FOUND ); not = (CTN_Notary*)ptrAMemAlloc( sizeof(CTN_Notary) ); if ( !not ) return free_not( not, SIGN_NOT_ENOUGH_MEMORY ); CTN_NotInitVariables( not ); sign_hash_buffer( ¬->Hash, buffer, *out_len ); s_mcpy( &hash, ¬->Hash, sizeof(hash) ); found_one_of = 0; kasp_key = !!kasp_key; for( i=0; i<key_count + kasp_key; i++ ) { void* key; if ( kasp_key && i==0 ) key = kl_ok; else key = keys[i-kasp_key]; //void* key = (i<key_count) ? keys[i] : kl_ok; if ( !key ) return free_not( not, SIGN_BAD_PARAMETERS ); CTN_NotInitVariables( not ); s_mcpy( ¬->Hash, &hash, sizeof(hash) ); res = sign_load_open_key( not, key, 0 ); if ( res == SIGN_OK ) { int found_all_of = 0; res = CT_ERR_BAD_FILE; for( j=0; j<sign_count; j++ ) { char* p = ((char*)notaries[j]) + NOTARY_TXT_BEG; if ( !s_mcmp(not->Public.UserName,p,STR_GUID_TXT_LEN) ) { found_one_of = 1; found_all_of = 1; s_mset( not->RS.r, 0, CT_N_LEN_MAXRS ); s_mset( not->RS.s, 0, CT_N_LEN_MAXRS ); TextToBin( p + STR_GUID_TXT_LEN, NOTARY_VALUE_TXT_LEN, conv_buffer, NOTARY_VALUE_BIN_LEN+2 ); s_mcpy( not->RS.r, conv_buffer, NOTARY_LEN_R ); s_mcpy( not->RS.s, conv_buffer+NOTARY_LEN_R, NOTARY_LEN_S ); res = CTN_LANSNot( not ); break; } } if ( all_of ) { if ( res != CT_ERR_OK ) { if ( found_all_of ) return free_not( not, SIGN_NOT_MATCH ); else return free_not( not, SIGN_NOTARY_NOT_FOUND ); } } else if ( res == CT_ERR_OK ) return free_not( not, SIGN_OK ); } else return res/*SIGN_BAD_PARAMETERS*/; } if ( all_of ) return free_not( not, SIGN_OK ); else { if ( found_one_of ) return free_not( not, SIGN_NOT_MATCH ); else return free_not( not, SIGN_NOTARY_NOT_FOUND ); } } MY_EXCEPT }