private void WriteRelevanceKeys(JsonWriter writer, PassGeneratorRequest request) { if (request.RelevantDate.HasValue) { writer.WritePropertyName("relevantDate"); writer.WriteValue(request.RelevantDate.Value.ToString("yyyy-MM-ddTHH:mm:ssZ")); } if (RelevantLocations.Count > 0) { writer.WritePropertyName("locations"); writer.WriteStartArray(); foreach (var location in RelevantLocations) { location.Write(writer); } writer.WriteEndArray(); } if (RelevantBeacons.Count > 0) { writer.WritePropertyName("beacons"); writer.WriteStartArray(); foreach (var beacon in RelevantBeacons) { beacon.Write(writer); } writer.WriteEndArray(); } }
private void WriteStandardKeys(JsonWriter writer, PassGeneratorRequest request) { writer.WritePropertyName("passTypeIdentifier"); writer.WriteValue(request.Identifier); writer.WritePropertyName("formatVersion"); writer.WriteValue(request.FormatVersion); writer.WritePropertyName("serialNumber"); writer.WriteValue(request.SerialNumber); writer.WritePropertyName("description"); writer.WriteValue(request.Description); writer.WritePropertyName("organizationName"); writer.WriteValue(request.OrganizationName); writer.WritePropertyName("teamIdentifier"); writer.WriteValue(request.TeamIdentifier); if (request.LogoText != null) { writer.WritePropertyName("logoText"); writer.WriteValue(request.LogoText); } if (request.LabelColor != null) { writer.WritePropertyName("labelColor"); writer.WriteValue(request.LabelColor); } }
private static X509Certificate2 GetCertificate(PassGeneratorRequest request) { Trace.TraceInformation("Fetching Pass Certificate..."); try { if (request.Certificate == null) { if (string.IsNullOrEmpty(request.CertThumbprint)) { return(GetSpecifiedCertificateFromCertStore(request.PassTypeIdentifier, StoreName.My, X509FindType.FindBySubjectName)); } else { return(GetSpecifiedCertificateFromCertStore(request.CertThumbprint, StoreName.My, X509FindType.FindByThumbprint)); } } else { return(GetCertificateFromBytes(request.Certificate, request.CertificatePassword)); } } catch (Exception exp) { Trace.TraceError("Failed to fetch Pass Certificate: [{0}]", exp.Message); throw; } }
private void SignManigestFile(PassGeneratorRequest request, string manifestFileAndPath) { byte[] dataToSign = File.ReadAllBytes(manifestFileAndPath); X509Certificate2 card = GetCertificate(request); Org.BouncyCastle.X509.X509Certificate cert = DotNetUtilities.FromX509Certificate(card); Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateKey = DotNetUtilities.GetKeyPair(card.PrivateKey).Private; X509Certificate2 appleCA = GetAppleCertificate(); Org.BouncyCastle.X509.X509Certificate appleCert = DotNetUtilities.FromX509Certificate(appleCA); ArrayList intermediateCerts = new ArrayList(); intermediateCerts.Add(appleCert); intermediateCerts.Add(cert); Org.BouncyCastle.X509.Store.X509CollectionStoreParameters PP = new Org.BouncyCastle.X509.Store.X509CollectionStoreParameters(intermediateCerts); Org.BouncyCastle.X509.Store.IX509Store st1 = Org.BouncyCastle.X509.Store.X509StoreFactory.Create("CERTIFICATE/COLLECTION", PP); CmsSignedDataGenerator generator = new CmsSignedDataGenerator(); generator.AddSigner(privateKey, cert, CmsSignedDataGenerator.DigestSha1); generator.AddCertificates(st1); CmsProcessable content = new CmsProcessableByteArray(dataToSign); CmsSignedData signedData = generator.Generate(content, false); string outputDirectory = Path.GetDirectoryName(manifestFileAndPath); string signatureFileAndPath = Path.Combine(outputDirectory, "signature"); File.WriteAllBytes(signatureFileAndPath, signedData.GetEncoded()); }
private void OpenStyleSpecificKey(JsonWriter writer, PassGeneratorRequest request) { switch (request.Style) { case PassStyle.EventTicket: writer.WritePropertyName("eventTicket"); writer.WriteStartObject(); break; case PassStyle.StoreCard: writer.WritePropertyName("storeCard"); writer.WriteStartObject(); break; case PassStyle.BoardingPass: writer.WritePropertyName("boardingPass"); writer.WriteStartObject(); break; case PassStyle.Generic: writer.WritePropertyName("generic"); writer.WriteStartObject(); break; case PassStyle.Coupon: writer.WritePropertyName("coupon"); writer.WriteStartObject(); break; default: throw new InvalidOperationException("Unsupported pass style specified"); } }
private void WriteOther(JsonWriter writer, PassGeneratorRequest request) { if (request.Locations != null && request.Locations.Count > 0) { writer.WritePropertyName("locations"); writer.WriteStartArray(); foreach (var locals in request.Locations) { locals.Write(writer); } writer.WriteEndArray(); } if (request.RelevantDate != null) { writer.WritePropertyName("relevantDate"); //writer.WriteValue(request.RelevantDate.Value.ToString("yyyy-MM-ddTHH:mmZ")); var iso8601DateTime = XmlConvert.ToString(request.RelevantDate.Value, XmlDateTimeSerializationMode.Local); writer.WriteValue(iso8601DateTime); } if (request.IsRelative != null) { writer.WritePropertyName("isRelative"); writer.WriteValue(request.IsRelative); } }
private void SignManifestFile(PassGeneratorRequest request) { Trace.TraceInformation("Signing the manifest file..."); try { ContentInfo contentInfo = new ContentInfo(manifestFile); SignedCms signing = new SignedCms(contentInfo, true); CmsSigner signer = new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, passCert) { IncludeOption = X509IncludeOption.None }; Trace.TraceInformation("Fetching Apple Certificate for signing.."); Trace.TraceInformation("Constructing the certificate chain.."); signer.Certificates.Add(appleCert); signer.Certificates.Add(passCert); signer.SignedAttributes.Add(new Pkcs9SigningTime()); Trace.TraceInformation("Processing the signature.."); signing.ComputeSignature(signer); signatureFile = signing.Encode(); Trace.TraceInformation("The file has been successfully signed!"); } catch (Exception exp) { Trace.TraceError("Failed to sign the manifest file: [{0}]", exp.Message); throw new ManifestSigningException("Failed to sign manifest", exp); } }
public override Passbook.Generator.PassGeneratorRequest GetPass(string serialNumber) { PassGeneratorRequest request = new PassGeneratorRequest(); request.PassTypeIdentifier = PassTypeIdentifier; request.SerialNumber = Guid.NewGuid().ToString("D"); TemplateModel parameters = new TemplateModel(); parameters.AddField("origin", FieldAttribute.Label, "San Francisco"); parameters.AddField("origin", FieldAttribute.Value, "SFO"); parameters.AddField("destination", FieldAttribute.Label, "London Heathrow"); parameters.AddField("destination", FieldAttribute.Value, "LHR"); parameters.AddField("seat", FieldAttribute.Value, "7A"); parameters.AddField("boarding-gate", FieldAttribute.Value, "F12"); parameters.AddField("passenger-name", FieldAttribute.Value, "John Appleseed"); request.AddBarCode("M1APPLESEED/JMR EZQ7O92 GVALHRBA 00723319C002F00009100", BarcodeType.PKBarcodeFormatPDF417, "iso-8859-1"); request.LoadTemplate("BoardingPass", parameters); return request; }
private void WriteAppearanceKeys(JsonWriter writer, PassGeneratorRequest request) { if (request.ForegroundColor != null) { writer.WritePropertyName("foregroundColor"); writer.WriteValue(ConvertColor(request.ForegroundColor)); } if (request.BackgroundColor != null) { writer.WritePropertyName("backgroundColor"); writer.WriteValue(ConvertColor(request.BackgroundColor)); } if (request.LabelColor != null) { writer.WritePropertyName("labelColor"); writer.WriteValue(ConvertColor(request.LabelColor)); } if (request.SuppressStripShine) { writer.WritePropertyName("suppressStripShine"); writer.WriteValue(true); } }
private void SignManigestFile(PassGeneratorRequest request) { X509Certificate2 card = GetCertificate(request); if (card == null) { throw new FileNotFoundException("Certificate could not be found. Please ensure the thumbprint and cert location values are correct."); } Org.BouncyCastle.X509.X509Certificate cert = DotNetUtilities.FromX509Certificate(card); Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateKey = DotNetUtilities.GetKeyPair(card.PrivateKey).Private; X509Certificate2 appleCA = GetAppleCertificate(); Org.BouncyCastle.X509.X509Certificate appleCert = DotNetUtilities.FromX509Certificate(appleCA); ArrayList intermediateCerts = new ArrayList(); intermediateCerts.Add(appleCert); intermediateCerts.Add(cert); Org.BouncyCastle.X509.Store.X509CollectionStoreParameters PP = new Org.BouncyCastle.X509.Store.X509CollectionStoreParameters(intermediateCerts); Org.BouncyCastle.X509.Store.IX509Store st1 = Org.BouncyCastle.X509.Store.X509StoreFactory.Create("CERTIFICATE/COLLECTION", PP); CmsSignedDataGenerator generator = new CmsSignedDataGenerator(); generator.AddSigner(privateKey, cert, CmsSignedDataGenerator.DigestSha1); generator.AddCertificates(st1); CmsProcessable content = new CmsProcessableByteArray(manifestFile); CmsSignedData signedData = generator.Generate(content, false); signatureFile = signedData.GetEncoded(); }
private void CopyImageFiles(PassGeneratorRequest request, string tempPath) { // get and copy all files in given directory if (request.ImagesPath != null) { foreach (var file in Directory.GetFiles(request.ImagesPath)) { string fileName = file.Split('\\').Last(), temporaryPathAndFile = Path.Combine(tempPath, fileName); // copy from origin to temp destination File.Copy(file, temporaryPathAndFile); } } // override path if (request.ImagesList != null && request.ImagesList.Count > 0) { foreach (var image in request.ImagesList) { string temporaryPathAndFile = Path.Combine(tempPath, StringEnum.GetStringValue(image.Key)); // copy from origin to temp destination File.Copy(image.Value, temporaryPathAndFile, true); } } }
private void CreatePackage(PassGeneratorRequest request) { ValidateCertificates(request); CreatePassFile(request); GenerateLocalizationFiles(request); GenerateManifestFile(request); }
private void GenerateManifestFile(PassGeneratorRequest request, string tempPath) { string manifestFileAndPath = Path.Combine(tempPath, "manifest.json"); string[] filesToInclude = Directory.GetFiles(tempPath); using (StreamWriter sw = new StreamWriter(File.Open(manifestFileAndPath, FileMode.Create))) { using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); foreach (var fileNameWithPath in filesToInclude) { string fileName = Path.GetFileName(fileNameWithPath); string hash = GetHashForFile(fileNameWithPath); jsonWriter.WritePropertyName(fileName); jsonWriter.WriteValue(hash.ToLower()); } } } SignManigestFile(request, manifestFileAndPath); }
private void WriteAppearanceKeys(JsonWriter writer, PassGeneratorRequest request) { writer.WritePropertyName("foregroundColor"); writer.WriteValue(request.ForegroundColor); writer.WritePropertyName("backgroundColor"); writer.WriteValue(request.BackgroundColor); }
private void SignManigestFile(PassGeneratorRequest request) { Trace.TraceInformation("Signing the manifest file..."); X509Certificate2 card = GetCertificate(request); if (card == null) { throw new FileNotFoundException("Certificate could not be found. Please ensure the thumbprint and cert location values are correct."); } X509Certificate2 appleCA = GetAppleCertificate(request); if (appleCA == null) { throw new FileNotFoundException("Apple Certficate could not be found. Please downloaad from http://www.apple.com/certificateauthority/ and install into your LOCAL MACHINE certificate store."); } try { Org.BouncyCastle.X509.X509Certificate cert = DotNetUtilities.FromX509Certificate(card); Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateKey = DotNetUtilities.GetKeyPair(card.PrivateKey).Private; Trace.TraceInformation("Fetching Apple Certificate for signing.."); Org.BouncyCastle.X509.X509Certificate appleCert = DotNetUtilities.FromX509Certificate(appleCA); Trace.TraceInformation("Constructing the certificate chain.."); ArrayList intermediateCerts = new ArrayList(); intermediateCerts.Add(appleCert); intermediateCerts.Add(cert); Org.BouncyCastle.X509.Store.X509CollectionStoreParameters PP = new Org.BouncyCastle.X509.Store.X509CollectionStoreParameters(intermediateCerts); Org.BouncyCastle.X509.Store.IX509Store st1 = Org.BouncyCastle.X509.Store.X509StoreFactory.Create("CERTIFICATE/COLLECTION", PP); CmsSignedDataGenerator generator = new CmsSignedDataGenerator(); generator.AddSigner(privateKey, cert, CmsSignedDataGenerator.DigestSha1); generator.AddCertificates(st1); Trace.TraceInformation("Processing the signature.."); CmsProcessable content = new CmsProcessableByteArray(manifestFile); CmsSignedData signedData = generator.Generate(content, false); signatureFile = signedData.GetEncoded(); Trace.TraceInformation("The file has been successfully signed!"); } catch (Exception exp) { Trace.TraceError("Failed to sign the manifest file: [{0}]", exp.Message); throw new ManifestSigningException("Failed to sign manifest", exp); } }
private void WriteUrls(JsonWriter writer, PassGeneratorRequest request) { if (!string.IsNullOrEmpty(request.AuthenticationToken)) { writer.WritePropertyName("authenticationToken"); writer.WriteValue(request.AuthenticationToken); writer.WritePropertyName("webServiceURL"); writer.WriteValue(request.WebServiceUrl); } }
private void ValidateCertificates(PassGeneratorRequest request) { passCert = GetCertificate(request); if (passCert == null) { throw new FileNotFoundException("Certificate could not be found. Please ensure the thumbprint and cert location values are correct."); } appleCert = GetAppleCertificate(request); if (appleCert == null) { throw new FileNotFoundException("Apple Certificate could not be found. Please download it from http://www.apple.com/certificateauthority/ and install it into your PERSONAL or LOCAL MACHINE certificate store."); } string passTypeIdentifier = passCert.GetNameInfo(X509NameType.SimpleName, false); if (passTypeIdentifier.StartsWith(passTypePrefix, StringComparison.OrdinalIgnoreCase)) { passTypeIdentifier = passTypeIdentifier.Substring(passTypePrefix.Length); if (!string.IsNullOrEmpty(passTypeIdentifier) && !string.Equals(request.PassTypeIdentifier, passTypeIdentifier, StringComparison.Ordinal)) { if (!string.IsNullOrEmpty(request.PassTypeIdentifier)) { Trace.TraceWarning("Configured passTypeIdentifier {0} does not match pass certificate {1}, correcting.", request.PassTypeIdentifier, passTypeIdentifier); } request.PassTypeIdentifier = passTypeIdentifier; } } Dictionary <string, string> nameParts = Regex.Matches(passCert.SubjectName.Name, @"(?<key>[^=,\s]+)\=*(?<value>("".+""|[^,])+)") .Cast <Match>() .ToDictionary( m => m.Groups["key"].Value, m => m.Groups["value"].Value); string teamIdentifier; if (nameParts.TryGetValue("OU", out teamIdentifier)) { if (!string.IsNullOrEmpty(teamIdentifier) && !string.Equals(request.TeamIdentifier, teamIdentifier, StringComparison.Ordinal)) { if (!string.IsNullOrEmpty(request.TeamIdentifier)) { Trace.TraceWarning("Configured teamidentifier {0} does not match pass certificate {1}, correcting.", request.TeamIdentifier, teamIdentifier); } request.TeamIdentifier = teamIdentifier; } } }
private X509Certificate2 GetAppleCertificate(PassGeneratorRequest request) { if (request.AppleWWDRCACertificate == null) { return(GetSpecifiedCertificateFromCertStore(APPLE_CERTIFICATE_THUMBPRINT, StoreName.CertificateAuthority, StoreLocation.LocalMachine)); } else { return(GetCertificateFromBytes(request.AppleWWDRCACertificate, null)); } }
private string CreatePackage(PassGeneratorRequest request) { string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), "contents"); Directory.CreateDirectory(tempPath); CopyImageFiles(request, tempPath); CreatePassFile(request, tempPath); GenerateManifestFile(request, tempPath); return tempPath; }
public static X509Certificate2 GetCertificate(PassGeneratorRequest request) { if (request.Certificate == null) { return(GetSpecifiedCertificateFromCertStore(request.CertThumbprint, StoreName.My, request.CertLocation)); } else { return(GetCertificateFromBytes(request.Certificate, request.CertificatePassword)); } }
private void ValidateCertificates(PassGeneratorRequest request) { passCert = GetCertificate(request); if (passCert == null) { throw new FileNotFoundException("Certificate could not be found. Please ensure the thumbprint and cert location values are correct."); } appleCert = GetAppleCertificate(request); if (appleCert == null) { throw new FileNotFoundException("Apple Certificate could not be found. Please download it from http://www.apple.com/certificateauthority/ and install it into your PERSONAL or LOCAL MACHINE certificate store."); } string passTypeIdentifier = passCert.GetNameInfo(X509NameType.SimpleName, false); if (passTypeIdentifier.StartsWith(passTypePrefix, StringComparison.OrdinalIgnoreCase)) { passTypeIdentifier = passTypeIdentifier.Substring(passTypePrefix.Length); if (!string.IsNullOrEmpty(passTypeIdentifier) && !string.Equals(request.PassTypeIdentifier, passTypeIdentifier, StringComparison.Ordinal)) { if (!string.IsNullOrEmpty(request.PassTypeIdentifier)) { Trace.TraceWarning("Configured passTypeIdentifier {0} does not match pass certificate {1}, correcting.", request.PassTypeIdentifier, passTypeIdentifier); } request.PassTypeIdentifier = passTypeIdentifier; } } Dictionary <string, string> nameParts = passCert.SubjectName.Name .Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries) .ToDictionary(k => k.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[0], e => e.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1]); string teamIdentifier; if (nameParts.TryGetValue("OU", out teamIdentifier)) { if (!string.IsNullOrEmpty(teamIdentifier) && !string.Equals(request.TeamIdentifier, teamIdentifier, StringComparison.Ordinal)) { if (!string.IsNullOrEmpty(request.TeamIdentifier)) { Trace.TraceWarning("Configured teamidentifier {0} does not match pass certificate {1}, correcting.", request.TeamIdentifier, teamIdentifier); } request.TeamIdentifier = teamIdentifier; } } }
private void ZipPackage(PassGeneratorRequest request) { using (MemoryStream zipToOpen = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update, true)) { foreach (KeyValuePair <PassbookImage, byte[]> image in request.Images) { ZipArchiveEntry imageEntry = archive.CreateEntry(image.Key.ToFilename()); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(image.Value); writer.Flush(); } } foreach (KeyValuePair <string, byte[]> localization in localizationFiles) { ZipArchiveEntry localizationEntry = archive.CreateEntry(string.Format("{0}.lproj/pass.strings", localization.Key.ToLower())); using (BinaryWriter writer = new BinaryWriter(localizationEntry.Open())) { writer.Write(localization.Value); writer.Flush(); } } ZipArchiveEntry PassJSONEntry = archive.CreateEntry(@"pass.json"); using (BinaryWriter writer = new BinaryWriter(PassJSONEntry.Open())) { writer.Write(passFile); writer.Flush(); } ZipArchiveEntry ManifestJSONEntry = archive.CreateEntry(@"manifest.json"); using (BinaryWriter writer = new BinaryWriter(ManifestJSONEntry.Open())) { writer.Write(manifestFile); writer.Flush(); } ZipArchiveEntry SignatureEntry = archive.CreateEntry(@"signature"); using (BinaryWriter writer = new BinaryWriter(SignatureEntry.Open())) { writer.Write(signatureFile); writer.Flush(); } } pkPassFile = zipToOpen.ToArray(); zipToOpen.Flush(); } }
private void ZipPackage(PassGeneratorRequest request) { using (MemoryStream zipToOpen = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update, true)) { foreach (KeyValuePair<PassbookImage, byte[]> image in request.Images) { ZipArchiveEntry imageEntry = archive.CreateEntry(image.Key.ToFilename()); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(image.Value); writer.Flush(); } } foreach (KeyValuePair<string, byte[]> localization in localizationFiles) { ZipArchiveEntry localizationEntry = archive.CreateEntry(string.Format ("{0}.lproj/pass.strings", localization.Key.ToLower())); using (BinaryWriter writer = new BinaryWriter(localizationEntry.Open())) { writer.Write(localization.Value); writer.Flush(); } } ZipArchiveEntry PassJSONEntry = archive.CreateEntry(@"pass.json"); using (BinaryWriter writer = new BinaryWriter(PassJSONEntry.Open())) { writer.Write(passFile); writer.Flush(); } ZipArchiveEntry ManifestJSONEntry = archive.CreateEntry(@"manifest.json"); using (BinaryWriter writer = new BinaryWriter(ManifestJSONEntry.Open())) { writer.Write(manifestFile); writer.Flush(); } ZipArchiveEntry SignatureEntry = archive.CreateEntry(@"signature"); using (BinaryWriter writer = new BinaryWriter(SignatureEntry.Open())) { writer.Write(signatureFile); writer.Flush(); } } pkPassFile = zipToOpen.ToArray(); zipToOpen.Flush(); } }
public static X509Certificate2 GetCertificate(PassGeneratorRequest request) { if (request.Certificate == null) { return GetSpecifiedCertificateFromCertStore(request.CertThumbprint, StoreName.My, request.CertLocation); } else { return GetCertificateFromBytes(request.Certificate, request.CertificatePassword); } }
private string CreatePackage(PassGeneratorRequest request) { string tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), "contents"); Directory.CreateDirectory(tempPath); CopyImageFiles(request, tempPath); CreatePassFile(request, tempPath); GenerateManifestFile(request, tempPath); return(tempPath); }
public byte[] Generate(PassGeneratorRequest request) { if (request == null) { throw new ArgumentNullException("request", "You must pass an instance of PassGeneratorRequest"); } CreatePackage(request); ZipPackage(request); return(pkPassFile); }
private void WriteLocationKeys(JsonWriter writer, PassGeneratorRequest passGeneratorRequest) { writer.WritePropertyName("locations"); writer.WriteStartArray(); foreach (var location in RelevantLocations) { location.Write(writer); } writer.WriteEndArray(); }
private void CreatePassFile(PassGeneratorRequest request, string tempPath) { string passFileAndPath = Path.Combine(tempPath, "pass.json"); using (StreamWriter sr = File.CreateText(passFileAndPath)) { using (JsonWriter writer = new JsonTextWriter(sr)) { request.Write(writer); } } }
public byte[] Generate(PassGeneratorRequest request) { if (request == null) { throw new ArgumentNullException("request", "You must pass an instance of PassGeneratorRequest"); } CreatePackage(request); ZipPackage(request); return pkPassFile; }
public Pass Generate(PassGeneratorRequest request) { if (request == null) { throw new ArgumentNullException("request"); } string pathToPackage = CreatePackage(request); string pathToZip = ZipPackage(pathToPackage); return new Pass(pathToZip); }
public Pass Generate(PassGeneratorRequest request) { if (request == null) { throw new ArgumentNullException("request"); } string pathToPackage = CreatePackage(request); string pathToZip = ZipPackage(pathToPackage); return(new Pass(pathToZip)); }
private void WriteExpirationKeys(JsonWriter writer, PassGeneratorRequest request) { if (request.ExpirationDate.HasValue) { writer.WritePropertyName("expirationDate"); writer.WriteValue(request.ExpirationDate.Value.ToString("yyyy-MM-ddTHH:mm:ssZ")); } if (request.Voided.HasValue) { writer.WritePropertyName("voided"); writer.WriteValue(request.Voided.Value); } }
private void SignManifestFile(PassGeneratorRequest request) { Trace.TraceInformation("Signing the manifest file..."); X509Certificate2 card = GetCertificate(request); if (card == null) { throw new FileNotFoundException("Certificate could not be found. Please ensure the thumbprint and cert location values are correct."); } X509Certificate2 appleCA = GetAppleCertificate(request); if (appleCA == null) { throw new FileNotFoundException("Apple Certificate could not be found. Please download it from http://www.apple.com/certificateauthority/ and install it into your LOCAL MACHINE certificate store."); } try { ContentInfo contentInfo = new ContentInfo(manifestFile); SignedCms signing = new SignedCms(contentInfo, true); CmsSigner signer = new CmsSigner(SubjectIdentifierType.SubjectKeyIdentifier, card) { IncludeOption = X509IncludeOption.None }; Trace.TraceInformation("Fetching Apple Certificate for signing.."); Trace.TraceInformation("Constructing the certificate chain.."); signer.Certificates.Add(appleCA); signer.Certificates.Add(card); signer.SignedAttributes.Add(new Pkcs9SigningTime()); Trace.TraceInformation("Processing the signature.."); signing.ComputeSignature(signer); signatureFile = signing.Encode(); Trace.TraceInformation("The file has been successfully signed!"); } catch (Exception exp) { Trace.TraceError("Failed to sign the manifest file: [{0}]", exp.Message); throw new ManifestSigningException("Failed to sign manifest", exp); } }
private void CreatePassFile(PassGeneratorRequest request) { using (MemoryStream ms = new MemoryStream()) { using (StreamWriter sr = new StreamWriter(ms)) { using (JsonWriter writer = new JsonTextWriter(sr)) { request.Write(writer); } passFile = ms.ToArray(); } } }
private void WriteBarcode(JsonWriter writer, PassGeneratorRequest request) { writer.WritePropertyName("barcode"); writer.WriteStartObject(); writer.WritePropertyName("format"); writer.WriteValue(request.Barcode.Type.ToString()); writer.WritePropertyName("message"); writer.WriteValue(request.Barcode.Message); writer.WritePropertyName("messageEncoding"); writer.WriteValue(request.Barcode.Encoding); writer.WritePropertyName("altText"); writer.WriteValue(request.Barcode.AlternateText); writer.WriteEndObject(); }
public byte[] Generate(PassGeneratorRequest request) { if (request == null) throw new ArgumentNullException("request", "You must pass an instance of PassGeneratorRequest"); if (request.IsValid) { CreatePackage(request); ZipPackage(request); return pkPassFile; } else throw new Exception("PassGeneratorRequest is not valid"); }
public override Passbook.Generator.PassGeneratorRequest GetPass(string serialNumber) { PassGeneratorRequest request = new PassGeneratorRequest(); request.PassTypeIdentifier = PassTypeIdentifier; request.SerialNumber = Guid.NewGuid().ToString("D"); TemplateModel parameters = new TemplateModel(); request.AddBarCode("01927847623423234234", BarcodeType.PKBarcodeFormatPDF417, "UTF-8", "01927847623423234234"); request.LoadTemplate("Coupon", parameters); return request; }
private void CreatePassFile(PassGeneratorRequest request) { using (MemoryStream ms = new MemoryStream()) { using (StreamWriter sr = new StreamWriter(ms)) { using (JsonWriter writer = new JsonTextWriter(sr)) { Trace.TraceInformation("Writing JSON..."); request.Write(writer); } passFile = ms.ToArray(); } } }
private static X509Certificate2 GetCertificate(PassGeneratorRequest request) { Trace.TraceInformation("Fetching Pass Certificate..."); try { if (request.Certificate == null) return GetSpecifiedCertificateFromCertStore(request.CertThumbprint, StoreName.My); else return GetCertificateFromBytes(request.Certificate, request.CertificatePassword); } catch (Exception exp) { Trace.TraceError("Failed to fetch Pass Certificate: [{0}]", exp.Message); throw; } }
private void CopyImageFiles(PassGeneratorRequest request, string tempPath) { string targetIconFileAndPath = Path.Combine(tempPath, Path.GetFileName(request.IconFile)); string targetIconRetinaFileAndPath = Path.Combine(tempPath, Path.GetFileName(request.IconRetinaFile)); File.Copy(request.IconFile, targetIconFileAndPath); File.Copy(request.IconRetinaFile, targetIconRetinaFileAndPath); string targetLogoFileAndPath = Path.Combine(tempPath, Path.GetFileName(request.LogoFile)); string targetLogoRetinaFileAndPath = Path.Combine(tempPath, Path.GetFileName(request.LogoRetinaFile)); File.Copy(request.LogoFile, targetLogoFileAndPath); File.Copy(request.LogoRetinaFile, targetLogoRetinaFileAndPath); string targetBackgroundFileAndPath = Path.Combine(tempPath, Path.GetFileName(request.BackgroundFile)); string targetBackgroundRetinaFileAndPath = Path.Combine(tempPath, Path.GetFileName(request.BackgroundRetinaFile)); File.Copy(request.BackgroundFile, targetBackgroundFileAndPath); File.Copy(request.BackgroundRetinaFile, targetBackgroundRetinaFileAndPath); }
public override Passbook.Generator.PassGeneratorRequest GetPass(string serialNumber) { PassGeneratorRequest request = new PassGeneratorRequest(); request.PassTypeIdentifier = PassTypeIdentifier; request.SerialNumber = Guid.NewGuid().ToString("D"); TemplateModel parameters = new TemplateModel(); parameters.AddField("event", FieldAttribute.Value, "Jeff Wayne's War of the Worlds"); DateTime eventDate = DateTime.Now.AddDays(1); parameters.AddField("doors-open", FieldAttribute.Value, new DateTime(eventDate.Year, eventDate.Month, eventDate.Day, 20, 30, 00)); parameters.AddField("seating-section", FieldAttribute.Value, 10); request.AddBarCode("01927847623423234234", BarcodeType.PKBarcodeFormatPDF417, "iso-8859-1", "01927847623423234234"); request.LoadTemplate("Event", parameters); return request; }
public static X509Certificate2 GetCertificate(PassGeneratorRequest request) { X509Store store = new X509Store(StoreName.My, request.CertLocation); store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certs = store.Certificates; if (certs.Count > 0) { for (int i = 0; i < certs.Count; i++) { X509Certificate2 cert = certs[i]; Debug.WriteLine(cert.Thumbprint); if (string.Compare(cert.Thumbprint, request.CertThumbprint, true) == 0) { return certs[i]; } } } return null; }
private void GenerateManifestFile(PassGeneratorRequest request) { using (MemoryStream ms = new MemoryStream()) { using (StreamWriter sw = new StreamWriter(ms)) { using (JsonWriter jsonWriter = new JsonTextWriter(sw)) { jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); string hash = GetHashForBytes(request.Images[PassbookImage.Icon]); jsonWriter.WritePropertyName(@"icon.png"); jsonWriter.WriteValue(hash.ToLower()); hash = GetHashForBytes(request.Images[PassbookImage.IconRetina]); jsonWriter.WritePropertyName(@"*****@*****.**"); jsonWriter.WriteValue(hash.ToLower()); if (request.Images.ContainsKey(PassbookImage.Logo)) { hash = GetHashForBytes(request.Images[PassbookImage.Logo]); jsonWriter.WritePropertyName(@"logo.png"); jsonWriter.WriteValue(hash.ToLower()); hash = GetHashForBytes(request.Images[PassbookImage.LogoRetina]); jsonWriter.WritePropertyName(@"*****@*****.**"); jsonWriter.WriteValue(hash.ToLower()); } if (request.Images.ContainsKey(PassbookImage.Background)) { hash = GetHashForBytes(request.Images[PassbookImage.Background]); jsonWriter.WritePropertyName(@"background.png"); jsonWriter.WriteValue(hash.ToLower()); hash = GetHashForBytes(request.Images[PassbookImage.BackgroundRetina]); jsonWriter.WritePropertyName(@"*****@*****.**"); jsonWriter.WriteValue(hash.ToLower()); } if (request.Images.ContainsKey(PassbookImage.Strip)) { hash = GetHashForBytes(request.Images[PassbookImage.Strip]); jsonWriter.WritePropertyName(@"strip.png"); jsonWriter.WriteValue(hash.ToLower()); hash = GetHashForBytes(request.Images[PassbookImage.StripRetina]); jsonWriter.WritePropertyName(@"*****@*****.**"); jsonWriter.WriteValue(hash.ToLower()); } if (request.Images.ContainsKey(PassbookImage.Thumbnail)) { hash = GetHashForBytes(request.Images[PassbookImage.Thumbnail]); jsonWriter.WritePropertyName(@"thumbnail.png"); jsonWriter.WriteValue(hash.ToLower()); hash = GetHashForBytes(request.Images[PassbookImage.ThumbnailRetina]); jsonWriter.WritePropertyName(@"*****@*****.**"); jsonWriter.WriteValue(hash.ToLower()); } hash = GetHashForBytes(passFile); jsonWriter.WritePropertyName(@"pass.json"); jsonWriter.WriteValue(hash.ToLower()); } manifestFile = ms.ToArray(); } SignManigestFile(request); } }
private X509Certificate2 GetAppleCertificate(PassGeneratorRequest request) { if (request.AppleWWDRCACertificate == null) { return GetSpecifiedCertificateFromCertStore(APPLE_CERTIFICATE_THUMBPRINT, StoreName.CertificateAuthority, StoreLocation.LocalMachine); } else { return GetCertificateFromBytes(request.AppleWWDRCACertificate, null); } }
public static X509Certificate2 GetCertificate(PassGeneratorRequest request) { return GetSpecifiedCertificate(request.CertThumbprint, StoreName.My, request.CertLocation); }
private void SignManigestFile(PassGeneratorRequest request) { X509Certificate2 card = GetCertificate(request); if (card == null) { throw new FileNotFoundException("Certificate could not be found. Please ensure the thumbprint and cert location values are correct."); } Org.BouncyCastle.X509.X509Certificate cert = DotNetUtilities.FromX509Certificate(card); Org.BouncyCastle.Crypto.AsymmetricKeyParameter privateKey = DotNetUtilities.GetKeyPair(card.PrivateKey).Private; X509Certificate2 appleCA = GetAppleCertificate(request); Org.BouncyCastle.X509.X509Certificate appleCert = DotNetUtilities.FromX509Certificate(appleCA); ArrayList intermediateCerts = new ArrayList(); intermediateCerts.Add(appleCert); intermediateCerts.Add(cert); Org.BouncyCastle.X509.Store.X509CollectionStoreParameters PP = new Org.BouncyCastle.X509.Store.X509CollectionStoreParameters(intermediateCerts); Org.BouncyCastle.X509.Store.IX509Store st1 = Org.BouncyCastle.X509.Store.X509StoreFactory.Create("CERTIFICATE/COLLECTION", PP); CmsSignedDataGenerator generator = new CmsSignedDataGenerator(); generator.AddSigner(privateKey, cert, CmsSignedDataGenerator.DigestSha1); generator.AddCertificates(st1); CmsProcessable content = new CmsProcessableByteArray(manifestFile); CmsSignedData signedData = generator.Generate(content, false); signatureFile = signedData.GetEncoded(); }
private void WriteBarcode(JsonWriter writer, PassGeneratorRequest request) { if (Barcode != null) { writer.WritePropertyName("barcode"); writer.WriteStartObject(); writer.WritePropertyName("format"); writer.WriteValue(request.Barcode.Type.ToString()); writer.WritePropertyName("message"); writer.WriteValue(request.Barcode.Message); writer.WritePropertyName("messageEncoding"); writer.WriteValue(request.Barcode.Encoding); if (request.Barcode.AlternateText != null) { writer.WritePropertyName("altText"); writer.WriteValue(request.Barcode.AlternateText); } writer.WriteEndObject(); } }
private void WriteStandardKeys(JsonWriter writer, PassGeneratorRequest request) { writer.WritePropertyName("passTypeIdentifier"); writer.WriteValue(request.Identifier); writer.WritePropertyName("formatVersion"); writer.WriteValue(request.FormatVersion); writer.WritePropertyName("serialNumber"); writer.WriteValue(request.SerialNumber); writer.WritePropertyName("description"); writer.WriteValue(request.Description); writer.WritePropertyName("organizationName"); writer.WriteValue(request.OrganizationName); writer.WritePropertyName("teamIdentifier"); writer.WriteValue(request.TeamIdentifier); if (request.LogoText != null) { writer.WritePropertyName("logoText"); writer.WriteValue(request.LogoText); } if (this.AssociatedStoreIdentifiers.Count > 0) { writer.WritePropertyName("associatedStoreIdentifiers"); writer.WriteStartArray(); foreach (int storeIdentifier in this.AssociatedStoreIdentifiers) { writer.WriteValue(storeIdentifier); } writer.WriteEndArray(); } }
private void WriteLocationKeys(JsonWriter writer, PassGeneratorRequest passGeneratorRequest) { writer.WritePropertyName("locations"); writer.WriteStartArray(); foreach (var location in Locations) { location.Write(writer); } writer.WriteEndArray(); }
private void ZipPackage(PassGeneratorRequest request) { using (MemoryStream zipToOpen = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update, true)) { ZipArchiveEntry imageEntry = archive.CreateEntry(@"icon.png"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.Icon]); writer.Flush(); } imageEntry = archive.CreateEntry(@"*****@*****.**"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.IconRetina]); writer.Flush(); } if (request.Images.ContainsKey(PassbookImage.Logo)) { imageEntry = archive.CreateEntry(@"logo.png"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.Logo]); writer.Flush(); } imageEntry = archive.CreateEntry(@"*****@*****.**"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.LogoRetina]); writer.Flush(); } } if (request.Images.ContainsKey(PassbookImage.Background)) { imageEntry = archive.CreateEntry(@"background.png"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.Background]); writer.Flush(); } imageEntry = archive.CreateEntry(@"*****@*****.**"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.BackgroundRetina]); writer.Flush(); } } if (request.Images.ContainsKey(PassbookImage.Strip)) { imageEntry = archive.CreateEntry(@"strip.png"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.Strip]); writer.Flush(); } imageEntry = archive.CreateEntry(@"*****@*****.**"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.StripRetina]); writer.Flush(); } } if (request.Images.ContainsKey(PassbookImage.Thumbnail)) { imageEntry = archive.CreateEntry(@"thumbnail.png"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.Thumbnail]); writer.Flush(); } imageEntry = archive.CreateEntry(@"*****@*****.**"); using (BinaryWriter writer = new BinaryWriter(imageEntry.Open())) { writer.Write(request.Images[PassbookImage.ThumbnailRetina]); writer.Flush(); } } ZipArchiveEntry PassJSONEntry = archive.CreateEntry(@"pass.json"); using (BinaryWriter writer = new BinaryWriter(PassJSONEntry.Open())) { writer.Write(passFile); writer.Flush(); } ZipArchiveEntry ManifestJSONEntry = archive.CreateEntry(@"manifest.json"); using (BinaryWriter writer = new BinaryWriter(ManifestJSONEntry.Open())) { writer.Write(manifestFile); writer.Flush(); } ZipArchiveEntry SignatureEntry = archive.CreateEntry(@"signature"); using (BinaryWriter writer = new BinaryWriter(SignatureEntry.Open())) { writer.Write(signatureFile); writer.Flush(); } } pkPassFile = zipToOpen.ToArray(); zipToOpen.Flush(); } }