Skip to content

Commit

Permalink
Write keys to file
Browse files Browse the repository at this point in the history
  • Loading branch information
george-hopkins committed Jan 24, 2018
1 parent eec2a90 commit aaa8bcc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
33 changes: 25 additions & 8 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ public class Program {

public static int Main(string[] args) {
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), @"Motorola\MOTOTRBO CPS\cpservices.dll");
if (args.Length > 0) {
if (args.Length >= 2) {
path = args[0];
}

var output = "codeplug.cfg";
if (args.Length >= 1) {
output = args[args.Length - 1];
}

var certificate = File.ReadAllBytes(Path.GetFullPath(Path.Combine(path, @"..\resources\mototrbocps")));

var module = ModuleDefMD.Load(path);
foreach (var type in module.Types) {
var constructor = type.FindStaticConstructor();
Expand All @@ -32,18 +39,28 @@ public static int Main(string[] args) {
if (!loadedStrings.Contains("mototrbocps")) {
continue;
}
Console.WriteLine("Prepare your environment:");
Console.WriteLine("export CTB_KEY='{0}='", loadedStrings.Find(s => s.Length == 43));
Console.WriteLine("export CTB_IV='{0}=='", loadedStrings.Find(s => s.Length == 22));
Console.WriteLine();
var encodedPassword = loadedStrings.FindAll(s => s.Length == 22)[loadedStrings.Count(s => s.Length == 43)] + "==";
Console.WriteLine("Extract the private key (enter '{0}' when asked for the password):", Encoding.ASCII.GetString(Convert.FromBase64String(encodedPassword)));
Console.WriteLine("openssl pkcs12 -in '{0}' -nocerts -nodes -out yourkey.pem", Path.GetFullPath(Path.Combine(path, @"..\resources\mototrbocps")));
using (var writer = new StreamWriter(output))
{
writer.WriteLine("[codeplug]");
writer.WriteLine("key = {0}=", loadedStrings.Find(s => s.Length == 43));
writer.WriteLine("iv = {0}==", loadedStrings.Find(s => s.Length == 22));
writer.WriteLine("signing_password = {0}==", loadedStrings.FindAll(s => s.Length == 22)[loadedStrings.Count(s => s.Length == 43)]);
var certificateLines = Split(Convert.ToBase64String(certificate), 64);
writer.WriteLine("signing_key = {0}", String.Join(Environment.NewLine + " ", certificateLines));
}
Console.WriteLine("Extracted keys to {0}", output);
return 0;
}

Console.Error.WriteLine("Could not find keys.");
return 1;
}

private static IEnumerable<string> Split(string text, int chunkSize) {
for (int offset = 0; offset < text.Length; offset += chunkSize) {
int size = Math.Min(chunkSize, text.Length - offset);
yield return text.Substring(offset, size);
}
}
}
}
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ Usage
-----

* Download the pre-built binaries (link above) or build the project with MSBuild
* Download [OpenSSL](https://www.openssl.org/) (e.g. from [indy.fulgan.com](https://indy.fulgan.com/SSL/)) and extract it into the project folder
* Open a terminal (press <kbd>Win</kbd>+<kbd>R</kbd> and enter `cmd`)
* Navigate to the project folder (e.g. `cd C:\Users\Example\Downloads\codeplug-prepare`)
* Start the tool by entering `CodeplugPrepare`
* Execute the command involving `openssl`
* All done! You can now use the file `yourkey.pem` and the values of `CTB_KEY` and `CTB_IV` to [read and write codeplugs][0].
* All done! You can now use the file `codeplug.cfg` to [read and write codeplugs][0].


[0]: https://github.com/george-hopkins/codeplug

0 comments on commit aaa8bcc

Please sign in to comment.