diff --git a/Source/Core/Common/Src/Crypto/ec.cpp b/Source/Core/Common/Src/Crypto/ec.cpp index 3f8e460ee1..08a632ff7a 100644 --- a/Source/Core/Common/Src/Crypto/ec.cpp +++ b/Source/Core/Common/Src/Crypto/ec.cpp @@ -303,7 +303,7 @@ static void point_add(u8 *r, u8 *p, u8 *q) elt_add(ry, s, rx); } -static void point_mul(u8 *d, u8 *a, u8 *b) // a is bignum +void point_mul(u8 *d, u8 *a, u8 *b) // a is bignum { u32 i; u8 mask; diff --git a/Source/Core/Common/Src/Crypto/tools.h b/Source/Core/Common/Src/Crypto/tools.h index a3946d66a1..e3b5dcce58 100644 --- a/Source/Core/Common/Src/Crypto/tools.h +++ b/Source/Core/Common/Src/Crypto/tools.h @@ -13,6 +13,7 @@ void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); void bn_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); void bn_inv(u8 *d, u8 *a, u8 *N, u32 n); // only for prime N void bn_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en); +void point_mul(u8 *d, u8 *a, u8 *b); void generate_ecdsa(u8 *R, u8 *S, u8 *k, u8 *hash); diff --git a/Source/Core/Common/Src/Log.h b/Source/Core/Common/Src/Log.h index df208b31db..fcbaf37f86 100644 --- a/Source/Core/Common/Src/Log.h +++ b/Source/Core/Common/Src/Log.h @@ -101,7 +101,7 @@ void GenericLog(LOGTYPES_LEVELS level, LOGTYPES_TYPE type, ; #if defined LOGGING || defined _DEBUG || defined DEBUGFAST -#define MAX_LOGLEVEL WARNING_LEVEL +#define MAX_LOGLEVEL DEBUG_LEVEL #else #ifndef MAX_LOGLEVEL #define MAX_LOGLEVEL DEBUG_LEVEL diff --git a/Source/Core/Core/CMakeLists.txt b/Source/Core/Core/CMakeLists.txt index ca304c18af..862a9b30ad 100644 --- a/Source/Core/Core/CMakeLists.txt +++ b/Source/Core/Core/CMakeLists.txt @@ -7,6 +7,7 @@ set(SRCS Src/ActionReplay.cpp Src/CoreParameter.cpp Src/CoreTiming.cpp Src/DSPEmulator.cpp + Src/ec_wii.cpp Src/GeckoCodeConfig.cpp Src/GeckoCode.cpp Src/MemTools.cpp diff --git a/Source/Core/Core/Core.vcxproj b/Source/Core/Core/Core.vcxproj index 7c50276e87..f0520955c6 100644 --- a/Source/Core/Core/Core.vcxproj +++ b/Source/Core/Core/Core.vcxproj @@ -341,7 +341,8 @@ - + + @@ -550,6 +551,7 @@ + diff --git a/Source/Core/Core/Core.vcxproj.filters b/Source/Core/Core/Core.vcxproj.filters index ad7ff5a17d..deb707ba68 100644 --- a/Source/Core/Core/Core.vcxproj.filters +++ b/Source/Core/Core/Core.vcxproj.filters @@ -533,6 +533,9 @@ NetPlay + + ec_wii + NetPlay @@ -1031,6 +1034,9 @@ NetPlay + + ec_wii + FifoPlayer diff --git a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_es.cpp b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_es.cpp index 0450ca5f67..d558ab5c13 100644 --- a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_es.cpp +++ b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_es.cpp @@ -60,11 +60,7 @@ #include "IPC_HLE/WII_IPC_HLE_Device_usb.h" #include "../Movie.h" -#include -#include -#include -#include -#include +#include #ifdef _WIN32 #include #endif @@ -872,6 +868,11 @@ bool CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress) { File::IOFile(path, "rb").ReadBytes(destination, size); } + else + { + ERROR_LOG(WII_IPC_ES, "IOCTL_ES_GETDEVICECERT failed: no cert found."); + + } Memory::Write_U32(0, _CommandAddress + 0x4); break; @@ -879,108 +880,15 @@ bool CWII_IPC_HLE_Device_es::IOCtlV(u32 _CommandAddress) case IOCTL_ES_SIGN: { - WARN_LOG(WII_IPC_ES, "IOCTL_ES_SIGN"); + WARN_LOG(WII_IPC_ES, "IOCTL_ES_SIGN"); + u8 *ap_cert_out = Memory::GetPointer(Buffer.PayloadBuffer[1].m_Address); + u8 *data = Memory::GetPointer(Buffer.InBuffer[0].m_Address); + u32 data_size = Buffer.InBuffer[0].m_Size; + u8 *sig_out = Memory::GetPointer(Buffer.PayloadBuffer[0].m_Address); - ecc_cert_t device_cert; - memset(&device_cert, 0, sizeof(ecc_cert_t)); - - std::string path = File::GetUserPath(D_WIIUSER_IDX) + "clientcert.bin"; - if (File::Exists(path)) - { - File::IOFile(path, "rb").ReadBytes((u8*)&device_cert, 0x180); - }else{ - WARN_LOG(WII_IPC_ES, "IOCTL_ES_SIGN: clientcert.bin not found."); - break; - } - - ecc_cert_t * ap_cert = (ecc_cert_t *)Memory::GetPointer(Buffer.PayloadBuffer[1].m_Address); - ap_cert->sig_type = Common::swap32(0x00010002); - ap_cert->key_type = Common::swap32(0x00000002); - ap_cert->ng_key_id = 0; - - snprintf((char*)ap_cert->issuer, - 0x40, - "%s-%s", - device_cert.issuer, - device_cert.key_name); + get_ap_sig_and_cert(sig_out, ap_cert_out, m_TitleID, data, data_size, NULL, 0); - snprintf((char*)ap_cert->key_name, - 0x40, - "AP%08x%08x", - (u32)(m_TitleID>>32), - (u32)(m_TitleID & 0xFFFFFFFF)); - - - u8 hash[SHA_DIGEST_LENGTH]; - SHA1(Memory::GetPointer(Buffer.InBuffer[0].m_Address), Buffer.InBuffer[0].m_Size, hash); - - BIGNUM *bn = BN_bin2bn(key_ecc_r, 0x1e, NULL); - EC_KEY *rand_key = EC_KEY_new_by_curve_name(NID_sect233r1); - EC_KEY_set_private_key(rand_key, bn); - - const EC_GROUP *group = EC_KEY_get0_group(rand_key); - - EC_POINT *pubkey = EC_POINT_new(group); - EC_POINT_mul(group, pubkey, bn, NULL, NULL, NULL); - - BIGNUM *x = BN_new(); - BIGNUM *y = BN_new(); - EC_POINT_get_affine_coordinates_GF2m(group, pubkey, x, y, NULL); - - int len = BN_num_bits(x); - BN_bn2bin(x, &ap_cert->ecc_pubkey[(240-len)/8]); - len = BN_num_bits(y); - BN_bn2bin(y, &ap_cert->ecc_pubkey[0x1e + (240-len)/8]); - - EC_KEY_set_public_key(rand_key, pubkey); - - - unsigned int buf_len = ECDSA_size(rand_key); - - unsigned char * sign_me = Memory::GetPointer(Buffer.PayloadBuffer[0].m_Address); - - ECDSA_SIG *rand_sig = ECDSA_do_sign(hash, SHA_DIGEST_LENGTH, rand_key); - - len = BN_num_bits(rand_sig->r); - BN_bn2bin(rand_sig->r, &sign_me[(240-len)/8]); - len = BN_num_bits(rand_sig->s); - BN_bn2bin(rand_sig->s, &sign_me[0x1e + (240-len)/8]); - - EC_POINT_free(pubkey); - BN_clear_free(bn); - BN_clear_free(x); - BN_clear_free(y); - ECDSA_SIG_free(rand_sig); - EC_KEY_free(rand_key); - - - SHA1(&ap_cert->issuer[0], 0x180 - 0x80, hash); - - bn = BN_bin2bn(key_ecc, 0x1e, NULL); - EC_KEY *ecc_key = EC_KEY_new_by_curve_name(NID_sect233r1); - EC_KEY_set_private_key(ecc_key, bn); - - group = EC_KEY_get0_group(ecc_key); - pubkey = EC_POINT_new(group); - - EC_POINT_mul(group, pubkey, bn, NULL, NULL, NULL); - - EC_KEY_set_public_key(ecc_key, pubkey); - - ECDSA_SIG *ecc_sig = ECDSA_do_sign(hash, SHA_DIGEST_LENGTH, ecc_key); - - len = BN_num_bits(ecc_sig->r); - BN_bn2bin(ecc_sig->r, &ap_cert->sig[(240-len)/8]); - len = BN_num_bits(ecc_sig->s); - BN_bn2bin(ecc_sig->s, &ap_cert->sig[0x1e + (240-len)/8]); - - - EC_POINT_free(pubkey); - ECDSA_SIG_free(ecc_sig); - BN_clear_free(bn); - EC_KEY_free(ecc_key); - break; } case IOCTL_ES_GETBOOT2VERSION: diff --git a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_net.cpp b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_net.cpp index 24f1d80bb7..8829f13bc8 100644 --- a/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_net.cpp +++ b/Source/Core/Core/Src/IPC_HLE/WII_IPC_HLE_Device_net.cpp @@ -254,8 +254,6 @@ u8 CWII_IPC_HLE_Device_net_kd_request::GetAreaCode( const char * area ) u32 i; u8 regions_[] = {0,1,2,2,1,3,3,4,5,5,1,2,6,7}; const char* regions[] = {"JPN", "USA", "EUR", "AUS", "BRA", "TWN", "ROC", "KOR", "HKG", "ASI", "LTN", "SAF", "CHN", ""}; - - u8 region_code = 0xff; for (i=0; i0){ + if(ret>=0){ +#ifdef _WIN32 u32 millis = 3000; - - setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,(char *)&millis,4); +#else + struct timeval millis; + millis.tv_sec = 3; + millis.tv_usec = 0; +#endif + setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,&millis,sizeof(millis)); } + + + return ret; break; } @@ -1059,7 +1065,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command, } else if (cmd == F_SETFL) { - int posix_flags = 0; + int posix_flags = O_NONBLOCK; //0; for (unsigned int i = 0; i < sizeof (mapping) / sizeof (mapping[0]); ++i) { @@ -1191,9 +1197,9 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command, for (int i = 0; i < nfds; i++) { - ufds[i].fd = Memory::Read_U32(_BufferOut + 0xc*i); - int events = Memory::Read_U32(_BufferOut + 0xc*i + 4); - ufds[i].revents = Memory::Read_U32(_BufferOut + 0xc*i + 8); + ufds[i].fd = Memory::Read_U32(_BufferOut + 0xc*i); //fd + int events = Memory::Read_U32(_BufferOut + 0xc*i + 4); //events + ufds[i].revents = Memory::Read_U32(_BufferOut + 0xc*i + 8); //revents // Translate Wii to native events int unhandled_events = events; @@ -1221,9 +1227,7 @@ u32 CWII_IPC_HLE_Device_net_ip_top::ExecuteCommand(u32 _Command, for (int i = 0; i +// Licensed under the terms of the GNU GPL, version 2 +// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt +#include +#include +#include "Common.h" +#include "Crypto/aes.h" +#include "Crypto/sha1.h" +#include "Crypto/tools.h" +#include "ec_wii.h" + +static u32 default_NG_id = 0x0403AC68; +static u32 default_NG_key_id = 0x6AAB8C59; + +static u8 default_NG_priv[] = { + 0x00, 0xAB, 0xEE, 0xC1, 0xDD, 0xB4, 0xA6, 0x16, 0x6B, 0x70, 0xFD, 0x7E, 0x56, 0x67, 0x70, + 0x57, 0x55, 0x27, 0x38, 0xA3, 0x26, 0xC5, 0x46, 0x16, 0xF7, 0x62, 0xC9, 0xED, 0x73, 0xF2, +}; + +static u8 default_NG_sig[] = { + // R + 0x00, 0xD8, 0x81, 0x63, 0xB2, 0x00, 0x6B, 0x0B, 0x54, 0x82, 0x88, 0x63, 0x81, 0x1C, 0x00, + 0x71, 0x12, 0xED, 0xB7, 0xFD, 0x21, 0xAB, 0x0E, 0x50, 0x0E, 0x1F, 0xBF, 0x78, 0xAD, 0x37, + // S + 0x00, 0x71, 0x8D, 0x82, 0x41, 0xEE, 0x45, 0x11, 0xC7, 0x3B, 0xAC, 0x08, 0xB6, 0x83, 0xDC, + 0x05, 0xB8, 0xA8, 0x90, 0x1F, 0xA8, 0x2A, 0x0E, 0x4E, 0x76, 0xEF, 0x44, 0x72, 0x99, 0xF8, +}; + + +// get_ng_cert + +// ng_cert_out is a pointer to a 0x180 byte buffer that will contain the device-unique certificate +// NG_id is the device-unique id to use +// NG_key_id is the device-unique key_id to use +// NG_priv is the device-unique private key to use +// NG_sig is the device-unique signature blob (from issuer) to use +// if NG_priv iis NULL or NG_sig is NULL or NG_id is 0 or NG_key_id is 0, default values +// will be used for all of them +void get_ng_cert(u8* ng_cert_out, u32 NG_id, u32 NG_key_id, u8* NG_priv, u8* NG_sig) +{ + char name[64]; + if((NG_id==0)||(NG_key_id==0)||(NG_priv==NULL)||(NG_sig==NULL)) + { + NG_id = default_NG_id; + NG_key_id = default_NG_key_id; + NG_priv = default_NG_priv; + NG_sig = default_NG_sig; + } + + sprintf(name, "NG%08x", NG_id); + make_blanksig_ec_cert(ng_cert_out, "Root-CA00000001-MS00000002", name, NG_priv, NG_key_id); + memcpy(ng_cert_out + 4, NG_sig, 60); +} + + +// get_ap_sig_and_cert + +// sig_out is a pointer to a 0x3c byte buffer which will be filled with the data payload's signature +// ap_cert_out is a pointer to a 0x180 byte buffer which will be filled with the temporal AP certificate +// title_id is the title responsible for the signing +// data is a pointer to the buffer of data to sign +// data_size is the length of the buffer +// NG_priv is the device-unique private key to use +// NG_id is the device-unique id to use +// if NG_priv is NULL or NG_id is 0, it will use builtin defaults +void get_ap_sig_and_cert(u8 *sig_out, u8 *ap_cert_out, u64 title_id, u8 *data, u32 data_size, u8 *NG_priv, u32 NG_id) +{ + u8 hash[20]; + u8 ap_priv[30]; + char signer[64]; + char name[64]; + + if((NG_id==0)||(NG_priv == NULL)) + { + NG_priv = default_NG_priv; + NG_id = default_NG_id; + } + + + memset(ap_priv, 0, 0x1e); + ap_priv[0x1d] = 1; + // setup random ap_priv here if desired + // get_rand_bytes(ap_priv, 0x1e); + // ap_priv[0] &= 1; + + memset(ap_cert_out+4, 0, 60); + + sprintf(signer, "Root-CA00000001-MS00000002-NG%08x", NG_id); + sprintf(name, "AP%08x%08x", (u32)(title_id>>32), (u32)(title_id&0xffffffff)); + make_blanksig_ec_cert(ap_cert_out, signer, name, ap_priv, 0); + + sha1(ap_cert_out + 0x80, 0x100, hash); + generate_ecdsa(ap_cert_out+4, ap_cert_out+34, NG_priv, hash); + + + sha1(data, data_size, hash); + sha1(hash, 20, hash); + + generate_ecdsa(sig_out, sig_out + 30, ap_priv, hash); +} + +void make_blanksig_ec_cert(u8 *cert_out, const char *signer, const char *name, u8 *private_key, u32 key_id) +{ + memset(cert_out, 0, 0x180); + *(u32*)cert_out = Common::swap32(0x10002); + + strncpy((char*)cert_out + 0x80, signer, 0x40); + *(u32*)(cert_out + 0xc0) = Common::swap32(2); + strncpy((char*)cert_out + 0xc4, name, 0x40); + *(u32*)(cert_out + 0x104) = Common::swap32(key_id); + ec_priv_to_pub(private_key, cert_out + 0x108); +} + + +// get_shared_secret + +// shared_secret_out is a pointer to 0x3c long buffer +// remote_public_key is a pointer to the remote party's public key (0x3c bytes) +// NG_priv is the device-unique private key to use +// if NG_priv is NULL, default builtin will be used +void get_shared_secret(u8* shared_secret_out, u8* remote_public_key, u8* NG_priv) +{ + if(NG_priv==NULL) + { + NG_priv = default_NG_priv; + } + + // required point_mul in Source/Core/Common/Src/Crypto/ec.cpp + // to be made non-static + + point_mul(shared_secret_out, NG_priv, remote_public_key); + +} diff --git a/Source/Core/Core/Src/ec_wii.h b/Source/Core/Core/Src/ec_wii.h new file mode 100644 index 0000000000..9cb9dcab1d --- /dev/null +++ b/Source/Core/Core/Src/ec_wii.h @@ -0,0 +1,10 @@ +// Based off of twintig http://git.infradead.org/?p=users/segher/wii.git +// Copyright 2007,2008 Segher Boessenkool +// Licensed under the terms of the GNU GPL, version 2 +// http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt + +void get_ng_cert(u8* ng_cert_out, u32 NG_id, u32 NG_key_id, u8* NG_priv, u8* NG_sig); +void get_ap_sig_and_cert(u8 *sig_out, u8 *ap_cert_out, u64 title_id, u8 *data, u32 data_size, u8 *NG_priv, u32 NG_id); + +void make_blanksig_ec_cert(u8 *cert_out, const char *signer, const char *name, u8 *private_key, u32 key_id); +void get_shared_secret(u8* shared_secret_out, u8* remote_public_key, u8* NG_priv);