Use the issuer and serial number from a CMS signer identifier to find the corresponding public certificate in the current Windows user's personal store. The lookup also works for a certificate whose private key is held elsewhere.
uses
Winapi.Windows, System.Classes, System.SysUtils, stCryptoAPI, stDERUtils,
stDERCoder, stGC, StreamSec.DSI.Arith, StreamSec.DSI.CMS, StreamSec.DSI.PkixCert;
const CERT_STORE_PROV_SYSTEM_W = 10; CERT_SYSTEM_STORE_CURRENT_USER = $00010000; CERT_STORE_OPEN_EXISTING_FLAG = $4000; CERT_STORE_READONLY_FLAG = $8000; CERT_FIND_SUBJECT_CERT = 11 shl 16; X509_ASN_ENCODING = 1; PKCS_7_ASN_ENCODING = $10000; CRYPT_E_NOT_FOUND = DWORD($80092004); function CertOpenStore(aProvider: PAnsiChar; aEncoding: DWORD; aLegacyProvider: NativeUInt; aFlags: DWORD; aName: Pointer): HCERTSTORE; stdcall; external 'crypt32.dll'; function CertFindCertificateInStore(aStore: HCERTSTORE; aEncoding, aFlags, aFindType: DWORD; aFind: Pointer; aPrevious: PCCERT_CONTEXT): PCCERT_CONTEXT; stdcall; external 'crypt32.dll';
MY is the personal store of the Windows account running the process. Pass a different existing store name to search another store for that account. The open flags request read-only access and require the named store to exist.
For a certificate manager that needs to index a whole Windows store, tKeyStorageWinCert.SystemStore selects the store. The lookup below requests one public certificate directly from Windows and returns it as a StreamSec iCertificate.
function FindUserCertificate(const aId: iIssuerAndSerialNumber; const aStoreName: string): iCertificate; var lStore: HCERTSTORE; lContext: PCCERT_CONTEXT; lQuery: CERT_INFO; lIssuer, lSerial, lLittleEndian, lDer: TBytes; lInput: TBytesStream; lError: DWORD; i: Integer; begin Result := nil; if (aId = nil) or (aStoreName = '') then raise EArgumentException.Create('Supply the store and the issuer and serial number'); lIssuer := aId.issuer.AsRdnSequence.GetStruct.EncodedContents(tDERCoder.DefaultCoder, True); lSerial := OSToBytes(aId.serialNumber.ContentAsOctetString); if (Length(lIssuer) = 0) or (Length(lSerial) = 0) then raise EArgumentException.Create('Empty certificate issuer or serial number'); SetLength(lLittleEndian, Length(lSerial)); for i := 0 to High(lSerial) do lLittleEndian[i] := lSerial[High(lSerial) - i]; lQuery := Default(CERT_INFO); lQuery.Issuer.cbData := Length(lIssuer); lQuery.Issuer.pbData := @lIssuer[0]; lQuery.SerialNumber.cbData := Length(lLittleEndian); lQuery.SerialNumber.pbData := @lLittleEndian[0]; lStore := CertOpenStore(PAnsiChar(CERT_STORE_PROV_SYSTEM_W), 0, 0, CERT_SYSTEM_STORE_CURRENT_USER or CERT_STORE_OPEN_EXISTING_FLAG or CERT_STORE_READONLY_FLAG, PWideChar(aStoreName)); if lStore = nil then RaiseLastOSError; try lContext := CertFindCertificateInStore(lStore, X509_ASN_ENCODING or PKCS_7_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, @lQuery, nil); if lContext = nil then begin lError := GetLastError; if lError <> CRYPT_E_NOT_FOUND then RaiseLastOSError(lError); Exit; end; try lDer := CopyUntypedToBytes(lContext.pbCertEncoded^, lContext.cbCertEncoded); finally CertFreeCertificateContext(lContext); end; finally CertCloseStore(lStore, 0); end; lInput := TBytesStream.Create(lDer); try Result := tCertificate.Create(gcamFirstAssignment); Result.GetStruct.LoadFromStream(lInput, fmtDER); Result.GetStruct.ReadOnly := True; Result.GetStruct.ExpandFull; finally lInput.Free; end; end;
For a CMS SignerInfo whose sid.Choice is siIssuerAndSerialNumber, call FindUserCertificate(SignerInfo.sid.AsIssuerAndSerialNumber). The default store is MY. The lookup passes the entire DER-encoded issuer name to Windows and reverses the ASN.1 serial bytes for the Windows integer blob.
A match returns the decoded certificate. A missing match returns nil. An error opening the store raises EOSError. The returned certificate remains usable after the store closes. To decide whether to trust the signer, apply your certificate chain and revocation policy and verify the detached signature.
StreamSec.DSI.PkixCert · StreamSec.DSI.CMS · StreamSec.DSI.PkixKeyStorageWinCert · stCryptoAPI