static int CertDevSignedGenerate(ConsoleService c, string cmdName, string str) { ConsoleParam[] args = { new ConsoleParam("[filename]", ConsoleService.Prompt, "Output filename: ", ConsoleService.EvalNotEmpty, null), new ConsoleParam("cn", ConsoleService.Prompt, "Common name: ", ConsoleService.EvalNotEmpty, null), }; ConsoleParamValueList vl = c.ParseCommandList(cmdName, str, args); string path = vl.DefaultParam.StrValue; string cn = vl["cn"].StrValue; PkiUtil.GenerateRsaKeyPair(2048, out PrivKey newKey, out _); Certificate newCert = new Certificate(newKey, DevTools.CoresDebugCACert.PkiCertificateStore, new CertificateOptions(PkiAlgorithm.RSA, cn: cn.Trim(), c: "JP")); CertificateStore newCertStore = new CertificateStore(newCert, newKey); newCertStore.ExportPkcs12()._Save(path, FileFlags.AutoCreateDirectory); return(0); }
static void process(Options options) { PkiConfig.LoadLicense(Convert.FromBase64String(LicenseBase64)); var isTest = options.Test > 0; var testCount = options.Test; var documentsInputDir = options.SourceDir; var signedDocumentsOutputDir = options.DestinationDir; if (isTest) { Util.CheckTestDirectories(documentsInputDir, signedDocumentsOutputDir); DeleteFiles(documentsInputDir, signedDocumentsOutputDir); PdfGenerate(testCount, documentsInputDir); } else { if (!Directory.Exists(documentsInputDir) && string.IsNullOrWhiteSpace(options.File)) { Console.WriteLine($"Error! The directory was not found: {documentsInputDir}"); return; } if (!Directory.Exists(signedDocumentsOutputDir) && string.IsNullOrWhiteSpace(options.File)) { Directory.CreateDirectory(signedDocumentsOutputDir); } Console.WriteLine(); } // Signer certificate PKCertificateWithKey cert = null; var store = Pkcs11CertificateStore.Load("eTPKCS11.dll", new StaticLoginProvider(options.Pin)); if (string.IsNullOrEmpty(options.CertThumbprint)) { List <PKCertificateWithKey> certificates; Console.WriteLine(); Console.WriteLine("Listing Certificates..."); if (string.IsNullOrEmpty(options.Pin)) { certificates = WindowsCertificateStore.LoadPersonalCurrentUser().GetCertificatesWithKey(); } else { certificates = store.GetCertificatesWithKey(); } for (var i = 0; i < certificates.Count; i++) { Console.WriteLine($"[{i}] {certificates[i].Certificate.SubjectDisplayName} (Issued by {certificates[i].Certificate.IssuerDisplayName})"); } Console.WriteLine(); Console.Write("Select the signer certificate: "); var indexstring = Console.ReadLine(); if (!int.TryParse(indexstring, out var index)) { Console.WriteLine($"Error! Invalid index: {indexstring}"); return; } cert = certificates[index]; } else { var thumbprint = PkiUtil.DecodeHexString(options.CertThumbprint); if (string.IsNullOrEmpty(options.Pin)) { cert = WindowsCertificateStore.LoadPersonalCurrentUser().GetCertificatesWithKey().FirstOrDefault(c => c.Certificate.ThumbprintSHA1.SequenceEqual(thumbprint)); } else { cert = store.GetCertificatesWithKey().FirstOrDefault(c => c.Certificate.ThumbprintSHA1.SequenceEqual(thumbprint)); } if (cert == null) { Console.WriteLine($"Error! No certificate was found with thumbprint: {options.CertThumbprint}"); return; } } Console.WriteLine($"Signer: {cert.Certificate.SubjectDisplayName} (thumbprint: {Util.ToHex(cert.Certificate.ThumbprintSHA1)})"); Metadata metadata = null; if (!string.IsNullOrEmpty(options.Metadata) && Util.FileExists(options.Metadata)) { try { var metadataContent = File.ReadAllBytes(options.Metadata); var metadataJson = Encoding.UTF8.GetString(metadataContent); metadata = JsonConvert.DeserializeObject <MetadataModel>(metadataJson).ToEntity(); } catch (Exception ex) { Log(ex.ToString()); Console.WriteLine($"Error parsing metadata file: {ex}"); } } if (string.IsNullOrWhiteSpace(options.File)) { Console.WriteLine("Getting things ready."); Sign(cert, documentsInputDir, signedDocumentsOutputDir, options.Reprocess, options.VisualRep, metadata); } else { var visual = CreateVisualRepresentation(cert.Certificate, options.VisualRep, (metadata != null)); var policy = GetSignaturePolicy().GetPolicy(cert.Certificate); policy.SignerSpecs.AttributeGeneration.EnableLtv = false; if (!SignFile(options.File, cert, policy, visual, metadata, "", "Signed_" + options.File)) { Console.WriteLine($"Error signing file"); return; } else { Console.WriteLine($"File successfully signed."); } } store.Dispose(); }