From f4eee481fe4c0a40065b4e27f639b41f1681b148 Mon Sep 17 00:00:00 2001 From: Xpl0itU <24777100+Xpl0itU@users.noreply.github.com> Date: Sat, 20 Apr 2024 17:44:14 +0200 Subject: [PATCH] Rewrite cert generation and fix bad cert --- certificate.go | 27 +++++++++++++++++---------- cmd/WiiUDownloader/mainwindow.go | 14 +------------- downloader.go | 16 +--------------- tmd.go | 9 +++++---- 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/certificate.go b/certificate.go index ec8d896..be6f0d5 100644 --- a/certificate.go +++ b/certificate.go @@ -1,7 +1,7 @@ package wiiudownloader import ( - "bytes" + "encoding/binary" "fmt" "net/http" "os" @@ -33,21 +33,28 @@ func getDefaultCert(progressReporter ProgressReporter, client *http.Client) ([]b return nil, fmt.Errorf("failed to download OSv10 cetk, length: %d", len(cetkData)) } -func GenerateCert(tmd *TMD, progressReporter ProgressReporter, client *http.Client) (bytes.Buffer, error) { - cert := bytes.Buffer{} +func GenerateCert(tmd *TMD, outputPath string, progressReporter ProgressReporter, client *http.Client) error { + cert, err := os.Create(outputPath) + if err != nil { + return err + } + defer cert.Close() - if _, err := cert.Write(tmd.Certificate1); err != nil { - return bytes.Buffer{}, err + if err := binary.Write(cert, binary.BigEndian, tmd.Certificate1); err != nil { + return err } - if _, err := cert.Write(tmd.Certificate2); err != nil { - return bytes.Buffer{}, err + if err := binary.Write(cert, binary.BigEndian, tmd.Certificate2); err != nil { + return err } defaultCert, err := getDefaultCert(progressReporter, client) if err != nil { - return bytes.Buffer{}, err + return err } - cert.Write(defaultCert) - return cert, nil + + if err := binary.Write(cert, binary.BigEndian, defaultCert); err != nil { + return err + } + return nil } diff --git a/cmd/WiiUDownloader/mainwindow.go b/cmd/WiiUDownloader/mainwindow.go index 09828d1..0807807 100644 --- a/cmd/WiiUDownloader/mainwindow.go +++ b/cmd/WiiUDownloader/mainwindow.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/binary" "fmt" "log" "net/http" @@ -251,20 +250,9 @@ func (mw *MainWindow) ShowAll() { wiiudownloader.GenerateTicket(filepath.Join(parentDir, "title.tik"), tmd.TitleID, titleKey, tmd.TitleVersion) - cert, err := wiiudownloader.GenerateCert(tmd, mw.progressWindow, http.DefaultClient) - if err != nil { + if err := wiiudownloader.GenerateCert(tmd, filepath.Join(parentDir, "title.cert"), mw.progressWindow, http.DefaultClient); err != nil { return } - - certPath := filepath.Join(parentDir, "title.cert") - certFile, err := os.Create(certPath) - if err != nil { - return - } - if err := binary.Write(certFile, binary.BigEndian, cert.Bytes()); err != nil { - return - } - defer certFile.Close() }) toolsSubMenu.Append(generateFakeTicketCert) diff --git a/downloader.go b/downloader.go index e1f8345..ac6e0c0 100644 --- a/downloader.go +++ b/downloader.go @@ -2,10 +2,8 @@ package wiiudownloader import ( "context" - "encoding/binary" "fmt" "io" - "log" "net/http" "net/url" "os" @@ -211,25 +209,13 @@ func DownloadTitle(titleID, outputDirectory string, doDecryption bool, progressR progressReporter.SetDownloadSize(int64(titleSize)) - cert, err := GenerateCert(tmd, progressReporter, client) - if err != nil { + if err := GenerateCert(tmd, filepath.Join(outputDir, "title.cert"), progressReporter, client); err != nil { if progressReporter.Cancelled() { return nil } return err } - certPath := filepath.Join(outputDir, "title.cert") - certFile, err := os.Create(certPath) - if err != nil { - return err - } - if err := binary.Write(certFile, binary.BigEndian, cert.Bytes()); err != nil { - return err - } - certFile.Close() - log.Printf("Certificate saved to %v \n", certPath) - g, ctx := errgroup.WithContext(context.Background()) g.SetLimit(maxConcurrentDownloads) sem := semaphore.NewWeighted(maxConcurrentDownloads) diff --git a/tmd.go b/tmd.go index 09bed25..d6f8a86 100644 --- a/tmd.go +++ b/tmd.go @@ -72,7 +72,7 @@ func ParseTMD(data []byte) (*TMD, error) { return nil, err } - tmd.Contents[i].Hash = make([]byte, 0x14) + tmd.Contents[i].Hash = make([]byte, 0x20) if err := binary.Read(reader, binary.BigEndian, &tmd.Contents[i].Hash); err != nil { return nil, err } @@ -126,17 +126,18 @@ func ParseTMD(data []byte) (*TMD, error) { } reader.Seek(0xB14+(0x30*int64(c)), io.SeekStart) - tmd.Contents[c].Hash = make([]byte, 0x14) + tmd.Contents[c].Hash = make([]byte, 0x20) if err := binary.Read(reader, binary.BigEndian, &tmd.Contents[c].Hash); err != nil { return nil, err } } tmd.Certificate1 = make([]byte, 0x400) - if _, err := io.ReadFull(reader, tmd.Certificate1); err != nil { + if err := binary.Read(reader, binary.BigEndian, &tmd.Certificate1); err != nil { return nil, err } + tmd.Certificate2 = make([]byte, 0x300) - if _, err := io.ReadFull(reader, tmd.Certificate2); err != nil { + if err := binary.Read(reader, binary.BigEndian, &tmd.Certificate2); err != nil { return nil, err } default: