예제 #1
0
 public static string DeriveTitlePassFromKey(byte[] titleIdBytes, byte[] titleKey)
 {
     foreach (string tryPass in KeyGenTryPasswords.Concat(new [] { new string(titleIdBytes.Slice(4, 4).AsChars()).ToUpperInvariant() }))
     {
         byte[] tryTitleKey = TitleKeyGen.Derive(titleIdBytes, tryPass);
         if (tryTitleKey == titleKey)
         {
             return(tryPass);
         }
     }
     return(null);
 }
예제 #2
0
        public static async Task <(TicketBooth.Ticket ticket, List <string> contentsList)> MakeTicketAndDecryptMetadataContents(byte[] titleIdBytes, TitleMetadata metadata, string titleDir, bool makeQolFiles = false)
        {
            if (metadata.NumContents <= 0)
            {
                return(null, new List <string>());
            }

            List <string> contentsList = new List <string>();

            TicketBooth.Ticket ticket = null;
            bool   success            = false;
            string contentPath        = Path.Combine(titleDir, metadata.ContentInfo[0].Id.ToString("x8"));
            string appPath            = "";

            // TODO: Not needed for any known DSiWare titles, but could be a decent idea to test the gamecode of the game as a password
            foreach (string tryPass in KeyGenTryPasswords)
            {
                byte[] titleKey = TitleKeyGen.Derive(titleIdBytes, tryPass);
                ticket  = new TicketBooth.Ticket(titleKey);
                appPath = await ticket.DecryptContent(metadata.ContentInfo[0].Index, contentPath);

                RomInfo cInfo = new RomInfo(appPath, makeQolFiles);
                success = cInfo.ValidContent;
                if (!success)
                {
                    continue;
                }
                Log.Instance.Trace($"'{tryPass}' is the password for the title key of '{contentPath}'!");

                // Somewhat redundant given the CRC validation of the decrypted ROM in RomInfo, but still good to check
                if (VerifyMetadataContent(metadata, appPath, 0, titleDir, metadata.ContentInfo[0].Id.ToString("x8")))
                {
                    await File.WriteAllTextAsync(Path.Combine(titleDir, TitlePasswordFileName), tryPass);

                    await File.WriteAllBytesAsync(Path.Combine(titleDir, TitleKeyFileName), titleKey);
                }

                contentsList.Add(metadata.ContentInfo[0].Id.ToString("x8"));
                if (makeQolFiles)
                {
                    MakeQolFiles(cInfo, titleDir);
                }
                break;
            }

            if (!success)
            {
                Log.Instance.Error($"Unable to find the password for the title key of '{contentPath}'.");
                File.Delete(appPath);
                return(null, new List <string>());
            }

            for (int i = 1; i < metadata.NumContents; i++)
            {
                string contentName = metadata.ContentInfo[i].Id.ToString("x8");
                contentsList.Add(contentName);
                contentPath = Path.Combine(titleDir, contentName);
                string decPath = await ticket.DecryptContent(metadata.ContentInfo[i].Index, contentPath);

                VerifyMetadataContent(metadata, decPath, i, titleDir, contentName);
            }

            return(ticket, contentsList);
        }