Commit Graph

596 Commits

Author SHA1 Message Date
David Korth
468efd44db [nusresign] print-info.cpp: The content index filenames always use lowercase hex. 2020-07-08 20:18:25 -04:00
David Korth
dbd23445d8 [nusresign] main.c: Uncomment help for commands that are now available. 2020-07-08 20:16:46 -04:00
David Korth
94fe24d827 [nusresign] print-info.cpp: Implement H3 table verification.
The .h3 file has one SHA-1 hash for every 256 MB of data.

The final H4 hash is stored in the TMD content entry and is equal to
the SHA-1 hash of the .h3 file.
2020-07-08 20:14:16 -04:00
David Korth
0a80471626 [nusresign] print-info.cpp: Another fopen() -> _tfopen() fix. 2020-07-08 19:44:57 -04:00
David Korth
50b96d33d5 [nusresign] CMakeLists.txt: We need to link to GMP and Nettle.
Otherwise, the Windows build fails.
2020-07-08 19:43:58 -04:00
David Korth
eae936bd05 [nusresign] Fix some Windows build errors related to TCHAR. 2020-07-08 19:32:05 -04:00
David Korth
f68150f56e [nusresign] print-info.cpp: Implemented the 'verify' command.
FIXME: Contents with an H3 table aren't handled correctly.
Need to figure out how to handle these...
2020-07-08 19:17:26 -04:00
David Korth
26aa36379d [nusresign] print-info.cpp: Print the contents table. 2020-07-08 19:05:54 -04:00
David Korth
7437fc2bf4 [wadresign] print-info.c: Minor changes, including unexporting issuer_type(). 2020-07-08 19:01:17 -04:00
David Korth
e080894eca [nusresign] Initial print-info command.
Based on wadresign.

For TMD signature verification, the TMD's content info table hashes are
checked in addition to the TMD header.
2020-07-08 18:59:11 -04:00
David Korth
cb474fcb66 [libwiicrypto] cert_store.c: Use switch/case here.
This optimizes into a lookup table.
2020-07-08 18:15:59 -04:00
David Korth
e0c93941d7 [wadresign] Minor changes I made during the previous nusresign commit. 2020-07-08 18:14:08 -04:00
David Korth
4fc0b6d8d7 [resign-nus] Refactored; use NUS directories instead of individual files.
Copied over the main getopt_long() parser from wadresign.

Moved the actual resigning code to resign-nus.cpp.

Allow resigning from debug to retail. This won't use fakesigning; instead,
the signature will be overwritten with 0xD15EA5ED, and the ECDH data will
be overwritten with 0xFEEDFACE.
2020-07-08 18:12:32 -04:00
David Korth
62458e7643 Fix the Windows build.
- Moved RVTH_CDECL to common.h

[nusresign] main.c: Use RVTH_CDECL and tcharx.h.
2020-07-07 22:11:50 -04:00
David Korth
544f1b405a [nusresign] New tool for re-signing Wii U NUS packages.
Syntax: `nusresign title.tik title.tmd`

The title.cert file must be replaced manually at the moment.

Tickets and TMDs downloaded directly from the Nintendo CDN have a
partial certificate chain (CA and CP/XS) appended to them. These
certificates are replaced with the correct ones if they're found.

This uses the existing libwiicrypto library, which has been extended
to support Wii U:

- Added the Wii U debug and retail common keys.

- Added the vWii debug common key, though it isn't used by anything
  right now. (Some infrastructure has been added to detect it, but I
  can't really test it because vWii isn't officially supported on
  Wii U devkits.)

- cert_realsign_ticket() and cert_realsign_ticketOrTMD() have been
  combined into a single function, cert_realsign_ticketOrTMD(), since
  they're basically the same (except for the data size).

- cert_realsign_ticketOrTMD() now checks the signature type field to
  determine what type of hash to use in the signature.

- cert_fakesign_*(): Verify that the signature is RSA2048 with SHA-1.
  Wii U doesn't have the fakesign bug, so fake signing is useless there.
  For re-signing to retail when using CFW, we should just zero out the
  signature without doing any sort of fakesigning brute-forcing.

- Renamed rsaw_sha1_sign() to rsaw_rsa2048_sign() and added a parameter
  to indicate use of SHA-256 instead of SHA-1.

- sig_recrypt_ticket(): Added Wii U issuers and keys.

- sig_recrypt_ticket_WUP(): Wrapper that casts WUP_Ticket* to
  RVL_Ticket*. The tickets aren't exactly the same, but the title key
  and issuer fields are in the same place in both of them.

- wii_structs.h:
  - Removed unnecessary struct packing.
  - Changed `uint8_t ticket_id[0x08]` to `uint64_t`.
  - Changed `uint8_t console_id[4]` to `uint32_t console_id`.
2020-07-07 21:39:51 -04:00
David Korth
25af63a6a5 [libwiicrypto] priv_key_store.c: rvth_privkey_WUP_dpki_tmd had p and q swapped. 2020-07-07 21:39:34 -04:00
David Korth
1eefbf1cc5 [libwiicrypto] priv_key_store.c: Added Wii U dpki private keys.
Not currently used for anything, though...
2020-07-05 22:18:46 -04:00
David Korth
ce7b1b93c2 [libwiicrypto] rsaw_nettle.c: Calculate a, b, and c at runtime.
The Wii U tools have p and q, but not a, b, and c. These can be calculated
at runtime, so we don't need to have them here.

Tested by converting a retail WAD to debug and verifying the signature.
2020-07-05 21:59:53 -04:00
David Korth
3e1d4c177e [libwiicrypto] cert_store.c: Missing "Root-CA00000004-SP0000000e".
Root-CA00000003 ended up being tested twice, which caused a Google Test
assertion failure due to duplicate test names.
2020-07-05 20:59:16 -04:00
David Korth
36930b7169 [libwiicrypto] cert_store.h: Added the Wii U disc flag for the signature type.
TODO: Handle this flag when implementing Wii U support at some point.
Flipping this flag is needed in order to convert a disc title to an
installable title and vice-versa.
2020-07-05 18:38:32 -04:00
David Korth
789fd33156 [libwiicrypto] Added definitions for Wii U ppki (Retail) certificates.
Wii U ppki is apparently identical to 3DS ppki. 🤔
2020-07-05 18:36:05 -04:00
David Korth
96d7ed6a78 [libwiicrypto] The RVL Root certificates are also used on CTR and WUP. 2020-07-05 18:27:15 -04:00
David Korth
49d655e08e [libwiicrypto] Added 3DS dpki certificates.
NOTE: The 3DS uses the same CA certificates as Wii U for both
ppki and dpki.
2020-07-05 18:19:58 -04:00
David Korth
fe21dc0946 [libwiicrypto] Added 3DS prod certs (except for Root).
NOTE: 3DS and Wii U might share Root and CA.
2020-07-05 17:58:47 -04:00
David Korth
4910f0cb02 [libwiicrypto] cert_store.c: Split the certificate data into separate files.
This reduces the clutter in cert_store.c.

bin2h.c: Based on bin2h from Nintendont, but with some changes to make
it work better for libwiicrypto.

The .cert files are converted from binary format to .h format during
the build.

NOTE: All certificates aside from the root certificate are used in the
original format found on their respective Nintendo systems. The root
certificates use a dummy format that's similar, but different. (The
original systems had the moduluses and exponents stored directly without
using a certificate format.)
2020-07-05 17:47:13 -04:00
David Korth
caf6467f89 [libwiicrypto] cert_store.c: Make sure we assign max for WUP_PKI_DPKI. 2020-07-05 17:05:56 -04:00
David Korth
dca2ad1b3d [libwiicrypto] cert_store.c: Added missing break; statements. 2020-07-05 17:05:23 -04:00
David Korth
5917f9651e [libwiicrypto] cert_store.c: Added Wii U DPKI certificates.
The constants for Wii U certificates use WUP_ prefixes, though they're
still part of the same RVL_ enums.

Wii U uses new certificate types with SHA-256 hashes. The corresponding
functions have been updated to support SHA-256.

cert_store.c: Reordered certificates such that RVL Debug is first,
followed by RVL Retail, then WUP Debug.

CertVerifyTest: Added tests for the ticket, TMD, and SP certs.

TODO:
- Add the Root Wii U DPKI certificate.
- Add the Wii U DPKI keys.
- Add the PPKI certificates.
- Add 3DS certificates? (Same CAs as Wii U.)
- What is the SP certificate used for?
2020-07-05 16:42:43 -04:00
David Korth
b8b691e370 [libwiicrypto] cert_store.c: Added the correct modulus for the Root DPKI certificate.
This fixes CertVerifyTest for Root-CA00000002.
2020-07-05 15:59:52 -04:00
David Korth
8a8e4ad546 [libwiicrypto] Added two new certs: CA02-XS04 and CA02-CP05.
These are present in my RVT-H Reader's cert.sys. I have no idea what
they're used for.
2020-07-05 15:50:46 -04:00
David Korth
ba151cbb35 Reworked the certificate store to support multiple Root certificates.
dpki and ppki have unique root certificates, so in order to properly
verify certificates, we have to store each of them. They both have
the name "Root", so we use other methods to distinguish them.

Renamed "Retail" issuers to "ppki" and "Debug" issuers to "dpki"
internally, which matches how Nintendo refers to them.
(devel pki, prod pki)

Renamed the "dev" certificate to "MS" (Mastering Server).

cert_verify(): Split the actual verification code into a separate
function. This makes it easier to verify Root certificates, since we
can't get the PKI specification here. Both Root certificates will
be tested, and if one of them succeeds, it'll verify.
- TODO: Maybe add a PKI specification to prevent cases where it's
  signed by the wrong Root certificate? This isn't likely, since
  the Root keys for both PKIs aren't public.

Added a placeholder for the DPKI Root certificate.

CertVerifyTest: Added a test for DPKI Root CA. Note that it currently
fails because we don't have the DPKI Root certificate.
2020-07-05 14:49:00 -04:00
David Korth
af1c0d389a CMakeLists.txt: Check for the existence of the CMAKE_PROJECT variables instead of the CMake version. 2020-06-25 23:04:01 -04:00
David Korth
4302058aee CMakeLists.txt: CMAKE_PROJECT_VERSION was introduced in 3.12.
Set the variables manually for older versions.
2020-06-25 23:01:05 -04:00
David Korth
49e9aa76a7 [wadresign] resign-wad.c: We need to check for recrypt_key == -1.
Otherwise, if the output format is specified, then recrypt key is ignored.

This fixes e.g. converting Devkit Menu 1.00 from BWF Debug to WAD Retail
in a single step.
2020-06-23 19:16:16 -04:00
David Korth
c3714d869a appveyor.yml: Disable the MinGW-w64 builds for now.
- It ends up using MinGW-w64's getopt_long() instead of our version,
  which doesn't support TCHAR.

- Linking is failing due to multiple definitions of various inline
  functions in the Windows headers.
2020-06-21 23:07:16 -04:00
David Korth
7e72ea998f AppVeyor: Package artifacts for MSVC 2013 Release builds. 2020-06-21 22:54:05 -04:00
David Korth
9171d4c3c3 [librvth] RefFile: Use O_SYNC when writing on Linux.
Otherwise, it might end up writing gigabytes nearly "instantaneously",
but the actual writes only occur when the program exits (rvthtool) or
the RVT-H Reader is closed (qrvthtool).

TODO: Windows equivalent; check other systems.
2020-06-21 22:52:53 -04:00
David Korth
b84d437caf [rvthtool] main.c: FIXME: gcc doesn't support printf attributes for wide strings. 2020-06-20 19:00:35 -04:00
David Korth
ea1142abbc appveyor.cmd: Disable gtest in MinGW-w64 builds for now.
In file included from C:/projects/rvthtool/extlib/googletest/googletest/include/gtest/internal/gtest-internal.h:40:0,
                 from C:/projects/rvthtool/extlib/googletest/googletest/include/gtest/gtest.h:58,
                 from C:\projects\rvthtool\extlib\googletest\googletest\src\gtest-all.cc:39:
C:/projects/rvthtool/extlib/googletest/googletest/include/gtest/internal/gtest-port.h:1782:3: error: 'AutoHandle' does not name a type
   AutoHandle thread_;
   ^~~~~~~~~~
2020-06-20 18:08:09 -04:00
David Korth
013c2c939f [wadresign] verify: Detect incorrectly-encrypted vWii WAD files.
TODO: Add a way to fix this by recrypting the contents with the
correct key.
2020-06-20 14:56:24 -04:00
David Korth
b6a9cde7f7 Added support for the vWii common key.
It's technically supported in both wadresign and rvthtool, but rvthtool
doesn't allow using the vWii common key for encryption because it doesn't
make sense to recrypt a disc image to vWii.

FIXME: Is there a debug vWii common key?

TODO: A lot of vWii WADs are incorrectly encrypted using the retail
common key, even though the ticket's key index is set correctly.
Add an option to fix the encryption.
2020-06-20 14:50:37 -04:00
David Korth
d467b413db NEWS.md: Update wadresign details. 2020-06-20 14:27:06 -04:00
David Korth
899ea23c31 Merge branch 'feature/wadresign-convert-to-bwf'
Still need to properly copy over the meta and CRL sections, but that's
basically the same as before for bwf->wad.
2020-06-20 14:25:18 -04:00
David Korth
e7ce689b14 AppVeyor: Build with MinGW-w64 in addition to MSVC 2013.
May need to adjust things for Qt...
2020-06-20 14:06:31 -04:00
David Korth
6ca6180f43 Copied various CMake module updates from rom-properties.
- Changed several macros to functions, e.g. C/C++ language version checks.
- Improved CPU architecture detection.
- Improved LFS detection on some *BSD platforms.
- Preliminary support for Windows on ARM.
- Added toolchain files for i686 and x86_64 MinGW-w64.
2020-06-20 14:01:52 -04:00
David Korth
556ced2327 [rvthtool] Copied the VERSION changes from rom-properties.
The CMake minimum version in the base project was set to 3.1, so I'm
keeping that instead of reducing it to 3.0. Not sure why I used 3.1,
though...
2020-06-20 13:48:38 -04:00
David Korth
a6cbd897c3 [wadresign] Change the default behavior of format conversion.
If no parameters are specified, the encryption key is changed, but
the format is kept the same. This also applies if the encrpytion key
is specified but the format is not.

If the format is specified but the key is not specified, then only
the format is changed.
2020-06-18 20:02:36 -04:00
David Korth
c91fee0e84 [wadresign] Added the CRL field; renamed "footer" and "name" to "meta".
NOTE: CRL is not currently copied for either format, and metadata is
only copied when the destination format is WAD.
2020-06-18 19:53:14 -04:00
David Korth
e32c53544e [wadresign] resign-wad.c: Use a bool for isDestBwf; fix a bunch of logic errors.
Stupid logic errors for wad->wad, wad->bwf, etc. due to hard-to-understand
comparisons instead of using boolean logic.

All of my local tests work now.

TODO: Handle the meta and crl sections better.
2020-06-18 19:36:11 -04:00
David Korth
35f1d5eade [wadresign] resign-wad.c: Allow converting BWF to WAD without recryption.
Tested with a devel-signed BootMii and it worked.
2020-06-18 19:27:55 -04:00