All code is illustrative and unexecuted.
Apple Wallet NFC configuration uses a Base64-encoded X.509 SubjectPublicKeyInfo (SPKI) containing an ECDH P-256 public key. An encoded EC point alone is not that structure. This article separates the formats and shows OpenSSL, dotnet-passbook and BouncyCastle examples.
Point encoding is not the whole SPKI
- An uncompressed P-256 point uses a prefix plus X and Y: 65 bytes.
- A compressed P-256 point uses a prefix plus X: 33 bytes. The prefix distinguishes the possible Y coordinates.
- SPKI also carries algorithm and curve identifiers. Its DER encoding is longer than the point it contains; Base64 adds another representation layer.
Apple's public NFC field documentation specifies SPKI and P-256, not a universal rule that every NFC terminal requires compression. Check the receiver's format requirements and Apple entitlement requirements. A 33-byte length check alone does not validate a public key.
1. Export with OpenSSL
The following illustrative commands read an existing P-256 private key. The first writes public SPKI as PEM; the second writes DER; the third encodes the complete DER for a field requiring Base64. private_key.pem is a local input filename, not a downloadable key. Choose separate output paths and protect the private input. These commands were not executed.
openssl ec -in private_key.pem -pubout -conv_form compressed -out compressed_public_key.pem
openssl ec -in private_key.pem -pubout -conv_form compressed -outform DER -out compressed_public_key.der
openssl base64 -A -in compressed_public_key.der2. Use the full SPKI in dotnet-passbook
The original library link is retained: dotnet-passbook. In an existing pass-generation context, the constructor takes the NFC message and encoded public key. The snippet assumes request is the application's existing pass request and the library namespace is imported. The DER file is the public output above, not a private key. Check the package API against your pinned version. NFC support also requires Apple's special entitlement; a key alone does not enable it.
string spkiBase64 = Convert.ToBase64String(
File.ReadAllBytes("compressed_public_key.der"));
request.Nfc = new Nfc("15407F6", spkiBase64);3. Construct SPKI in C# with BouncyCastle
This illustrative helper uses the BouncyCastle.Cryptography APIs and accepts a PUBLIC KEY PEM string, not a private key or certificate PEM. It checks the named curve, parses the EC key and puts the compressed point back inside SPKI. In contrast, Q.GetEncoded(true) alone returns only the point. No compilation, execution or device interoperability test is claimed.
using System;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.IO.Pem;
public static class CompressedSpki
{
public static string FromPublicPem(string publicPem)
{
using var text = new StringReader(publicPem);
using var reader = new PemReader(text);
var pem = reader.ReadPemObject()
?? throw new ArgumentException("Missing PUBLIC KEY PEM block.");
if (pem.Type != "PUBLIC KEY")
throw new ArgumentException("Expected public SPKI, not a private key.");
var spki = SubjectPublicKeyInfo.GetInstance(
Asn1Object.FromByteArray(pem.Content));
if (!spki.Algorithm.Algorithm.Equals(X9ObjectIdentifiers.IdECPublicKey)
|| !DerObjectIdentifier.GetInstance(spki.Algorithm.Parameters)
.Equals(X9ObjectIdentifiers.Prime256v1))
throw new ArgumentException("Expected named-curve P-256 EC SPKI.");
var key = (ECPublicKeyParameters)PublicKeyFactory.CreateKey(spki);
byte[] point = key.Q.GetEncoded(true);
if (point.Length != 33 || (point[0] != 2 && point[0] != 3))
throw new ArgumentException("Unexpected P-256 point encoding.");
var compressedSpki = new SubjectPublicKeyInfo(spki.Algorithm, point);
return Convert.ToBase64String(compressedSpki.GetDerEncoded());
}
}Summary
Keep point bytes, DER SPKI, PEM and Base64 distinct. Validate the curve and structure with a cryptographic parser, then check the exact consumer contract. Never substitute the 33-byte point for an SPKI field.
Primary sources
- Apple Pass.NFC
- OpenSSL ec
- RFC 5480
- BouncyCastle SubjectPublicKeyInfo source
- BouncyCastle PublicKeyFactory source
- BouncyCastle PemReader source
- BouncyCastle X9ObjectIdentifiers source
- dotnet-passbook primary repository
Original English article: Generating subject public key info in a compressed form. Original publication date: 2024-04-03T12:21:00.003+03:00.