Signed OPC publication and Workbook Save
Publish a newly signed package, replace an authorized verified signature, or edit and re-sign a verified original directly to a caller-owned stream or file; a mutable TXLSXWorkbook can also save its actual current model through explicit signing methods
Each operation takes an explicit selection and a borrowed synchronous signer, verifies the complete supported signature and selected references privately, and reports destination publication separately from successful preparation
These are explicit operations; ordinary SaveAs, SaveAsChecked and verified preserve/invalidate saving retain their existing policies
Enterprise edition: Signing, re-signing and the six publication adapters on this page ship only with HotXLS Enterprise, in the lxSignatureOpcIssuer unit of the HotXLSEnterprise package (HotXLSLazEnterprise.lpk for Lazarus); Delphi and Lazarus applications add lxSignatureOpcIssuer to a uses clause and statically linked C++Builder applications call XlsEnableXlsxSigning() once. In the Single edition SaveSignedOpcPackage and SaveResignedVerifiedOpcPackage keep their signatures and raise EXLSEnterpriseRequired; trial builds sign through these Workbook methods, whose content is trial-limited, and reject the package-byte signing functions
Six package publication adapters
Use lxSignatureOpcPublication with the signing types from lxSignatureOpcIntegrity; all six adapters take source package bytes and a public DER certificate, while the destination is a stream or file
Issuing requires a supported unsigned package without existing signature/origin infrastructure; replacement requires the untouched verified original and explicit authorization, and the combined operation applies a complete ordinary-part mutation plan before issuing the new signature
function XlsIssueSignatureOpcPackageToStream(const Bytes,Certificate:TBytes;
const Options:TXLSSignatureOpcIssueOptions;Signer:TXLSSignatureOpcSignDigest;
Destination:TStream;
var Details:TXLSSignatureOpcIssueResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus;
function XlsIssueSignatureOpcPackageToFile(const Bytes,Certificate:TBytes;
const Options:TXLSSignatureOpcIssueOptions;Signer:TXLSSignatureOpcSignDigest;
const FileName:WideString;
var Details:TXLSSignatureOpcIssueResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus;
function XlsReplaceVerifiedSignatureOpcPackageToStream(const Bytes,Certificate:TBytes;
const Authorization:TXLSSignatureOpcReplaceAuthorization;
const Options:TXLSSignatureOpcIssueOptions;Signer:TXLSSignatureOpcReplaceSignDigest;
Destination:TStream;
var Details:TXLSSignatureOpcReplaceResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus;
function XlsReplaceVerifiedSignatureOpcPackageToFile(const Bytes,Certificate:TBytes;
const Authorization:TXLSSignatureOpcReplaceAuthorization;
const Options:TXLSSignatureOpcIssueOptions;Signer:TXLSSignatureOpcReplaceSignDigest;
const FileName:WideString;
var Details:TXLSSignatureOpcReplaceResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus;
function XlsMutateAndReplaceVerifiedSignatureOpcPackageToStream(const Bytes,Certificate:TBytes;
const Authorization:TXLSSignatureOpcReplaceAuthorization;
const Mutations:TXLSSignatureOpcMutations;
const Options:TXLSSignatureOpcIssueOptions;Signer:TXLSSignatureOpcMutateReplaceSignDigest;
Destination:TStream;
var Details:TXLSSignatureOpcMutateReplaceResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus;
function XlsMutateAndReplaceVerifiedSignatureOpcPackageToFile(const Bytes,Certificate:TBytes;
const Authorization:TXLSSignatureOpcReplaceAuthorization;
const Mutations:TXLSSignatureOpcMutations;
const Options:TXLSSignatureOpcIssueOptions;Signer:TXLSSignatureOpcMutateReplaceSignDigest;
const FileName:WideString;
var Details:TXLSSignatureOpcMutateReplaceResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus;
The new selection in Options is always explicit and does not inherit the old signature's coverage; raw parts, canonical XML and selected relationship transforms retain the authenticated selected-coverage contract
Capture a source stream or file
function XlsCaptureSignatureOpcInput(Source:TStream;var Bytes:TBytes):Boolean;overload;
function XlsCaptureSignatureOpcInput(const FileName:WideString;var Bytes:TBytes):Boolean;overload;
XlsCaptureSignatureOpcInput captures bounded owned bytes and preserves the old Bytes value on failure; stream capture reads from zero and restores the initial position, and file capture closes the read handle before returning
Complete capture before calling a publication adapter when source and destination are the same stream or file path; do not leave a source file handle open across publication
Prepared is different from Published
TXLSSignatureOpcPublicationStatus=(xsopuRejected,xsopuPublished,
xsopuDestinationUnavailable,xsopuRestored,xsopuRollbackFailed);
TXLSSignatureOpcPublicationResult=record
Status:TXLSSignatureOpcPublicationStatus;
Prepared,Published,RollbackAttempted,RollbackSucceeded:Boolean;
PackageSha256,Diagnostic:WideString;
end;
Prepared means that complete verified private output and the operation's pre-publication checks succeeded; Published means that the destination commit completed, and PackageSha256 is populated only for a published package
Check the returned TXLSSignatureOpcPublicationStatus and require xsopuPublished; nested Details can describe successful private signing even when a later model check or destination commit refused publication
| Status | Meaning |
|---|---|
xsopuRejected | Signature preparation or a pre-publication model/configuration check refused the operation |
xsopuPublished | The verified package reached the destination and the commit completed |
xsopuDestinationUnavailable | Destination preflight or file publication could not complete |
xsopuRestored | Stream publication failed and original bytes, size and position were restored |
xsopuRollbackFailed | Stream publication failed and destination recovery also failed |
RollbackAttempted, RollbackSucceeded and the bounded Diagnostic describe recovery; a rollback failure requires caller handling and must not be treated as an unchanged destination
The lower-level publication transaction's Commit writes supplied bytes and is not a signature verifier; the adapters and Workbook methods perform the required preparation and verification
Lower-level transactions and owned input snapshots
TXLSSignatureOpcPublicationTransaction = class
public
constructor CreateForStream(Destination: TStream);
constructor CreateForFile(const FileName: WideString);
destructor Destroy; override;
function Commit(const Bytes: TBytes;
var Publication: TXLSSignatureOpcPublicationResult): TXLSSignatureOpcPublicationStatus;
end;
procedure XlsResetSignatureOpcPublication(
var Publication: TXLSSignatureOpcPublicationResult);
function XlsSnapshotSignatureOpcPublicationInputs(const Bytes, Certificate: TBytes;
const Options: TXLSSignatureOpcIssueOptions;
const Mutations: TXLSSignatureOpcMutations;
var Input, Leaf: TBytes; var Plan: TXLSSignatureOpcIssueOptions;
var Edits: TXLSSignatureOpcMutations): Boolean;
The transaction constructors perform destination preparation and can raise on invalid or unavailable destinations; destroying a transaction releases its own backup or registered staging file without taking ownership of the caller's stream
Commit is single-use and neither validates a signature nor establishes that caller-supplied bytes are a verified package; callers implementing a lower-level publication path must perform the preparation and verification before marking their operation prepared
XlsResetSignatureOpcPublication resets the result to rejected, clears its flags and releases its previous diagnostic/hash values; XlsSnapshotSignatureOpcPublicationInputs provides the bounded owned package, certificate, selection and mutation snapshots shared by the adapters and Workbook methods
Stream prefix, truncation and recovery
The destination remains caller-owned and is never closed or freed; provide exclusive access to a readable, writable, seekable and resizable stream with known size and position
The complete existing destination is backed up before the signer runs; an unreadable backup, invalid position or existing size above 64 MiB rejects before signing
Successful publication preserves the prefix before the initial position, writes the package at that position, removes any old trailing bytes and sets Position to the package end; prefix length plus package length must fit within 64 MiB
The package hash and signature apply to the package slice only, excluding the prefix; use a zero initial position when the entire destination must be a standalone OPC ZIP package
A failed write, resize or final-position change attempts to restore all original bytes, size and position; a persistent destination failure can prevent recovery and returns xsopuRollbackFailed
Generic TStream cannot prove non-destructively that a later write, resize or seek will succeed; preflight also cannot recover arbitrary side effects of hostile virtual methods or a failed restoration of their own position
File staging and commit
A registered sibling temporary file is reserved before signing; the completed package is written and closed there before the existing sibling-file commit replaces the target
The target is not truncated to stage an unfinished package; failed stages release their temporary ownership, with existing registry retry behavior when deletion fails
Target replacement follows the existing filesystem helper's contract; this API does not add stronger crash durability, hardlink, reparse-point, permission or platform guarantees
Save the actual mutable Workbook
The following methods belong to TXLSXWorkbook in lxHandleX; use SaveSignedOpcPackage for a new or unsigned Workbook and SaveResignedVerifiedOpcPackage for a Workbook bound by OpenVerifiedOpcPackage
function SaveSignedOpcPackage(Destination:TStream;
const Certificate:TBytes;
const Options:TXLSSignatureOpcIssueOptions;
Signer:TXLSSignatureOpcSignDigest;
var Details:TXLSSignatureOpcIssueResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus; overload;
function SaveSignedOpcPackage(const FileName:WideString;
const Certificate:TBytes;
const Options:TXLSSignatureOpcIssueOptions;
Signer:TXLSSignatureOpcSignDigest;
var Details:TXLSSignatureOpcIssueResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus; overload;
function SaveResignedVerifiedOpcPackage(Destination:TStream;
const Certificate:TBytes;const Authorization:TXLSSignatureOpcReplaceAuthorization;
const Options:TXLSSignatureOpcIssueOptions;
Signer:TXLSSignatureOpcMutateReplaceSignDigest;
var Details:TXLSSignatureOpcMutateReplaceResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus; overload;
function SaveResignedVerifiedOpcPackage(const FileName:WideString;
const Certificate:TBytes;const Authorization:TXLSSignatureOpcReplaceAuthorization;
const Options:TXLSSignatureOpcIssueOptions;
Signer:TXLSSignatureOpcMutateReplaceSignDigest;
var Details:TXLSSignatureOpcMutateReplaceResult;
var Publication:TXLSSignatureOpcPublicationResult):TXLSSignatureOpcPublicationStatus; overload;
The unsigned signing method refuses an existing verified binding or a source declaring package signatures; use explicit verified re-signing instead of silently replacing them
Verified re-signing checks the actual original package before editing and requires all three fields of TXLSSignatureOpcReplaceAuthorization: ExpectedPackageSha256, ExpectedSignaturePart and ExpectedCertificateSha256
Both hashes contain exactly 64 hexadecimal characters and the signature location must match the actual verified result; these values select the intended old source and do not replace application access control or certificate trust
The ordinary OOXML writer serializes the actual current Workbook and derives the complete create/replace/remove plan, current content-type bindings and relationship dependencies; the untouched bound original remains the source of old-signature authority
The re-signing callback identifies the verified original, edited working package and actual unsigned signing input separately, and includes ordered mutation receipts, indirect media-type effects, the new certificate identity and the explicit new selection
Ordinary untouched content retains the existing raw/media/inventory audits; signature infrastructure and its root/content-type bindings are rebuilt under the combined editing and re-signing contract
Successful publication updates the verified source binding and current-model baseline without reopening the Workbook; existing sheet/cell objects and the SelectedSheets, ExternalWorkspace and QueryProviders facades keep their identities
File publication also updates the filename; new binding, baseline and filename values are prepared before commit and transferred after success, while refusal or publication failure leaves the previous binding and filename available for retry within the callback contract below
A subsequent unchanged verified-preserve save uses the new published binding; another explicit re-sign must authorize that new package, certificate and signature identity
Borrowed signer, real Self and nested saves
The caller retains the private key, provider and callback receiver throughout the synchronous operation; the library neither exports the private key nor selects or installs a certificate in a system store
Signing uses RSA PKCS#1 v1.5 with SHA-256; the borrowed callback receives an independently owned digest/request and returns the complete big-endian signature bytes under the issuing callback contract
A valid staged signing path calls its signer once without retries; invalid source/authorization/plan checks and failed destination backup call it zero times, and cancellation, exceptions or an incorrect signature cannot publish output
The visible ordinary serialization invokes write/progress callbacks with the real Workbook Self; private baseline and model checks suppress those callbacks rather than repeating the visible serialization's callback sequence
A callback-only projection that changes the represented output model is rejected before signing; covered changes to the live model, callbacks, recalculation mode, filename or verified binding during signing are checked before publication
Any nested explicit signed Workbook save on the same thread is refused, including a save on another Workbook; covered recursive ordinary save, verified save, Open and binding-clear entry points on the active Workbook are also refused
The Workbook write guard is not a mutex or full Save-state rollback mechanism; prevent concurrent model, configuration and destination access, including writes through retained objects
Arbitrary callback/provider/key effects, deliberate live-model edits, cache recalculation, writer-assigned identifiers and diagnostics are not promised complete rollback; destination virtual methods must not mutate the Workbook during commit
Ownership, bounds and trust
Source bytes, certificate bytes, selection arrays and mutation payloads are captured into bounded owned inputs before clearing nested result state; dynamic-array value aliases are supported, while overlapping writable result-record storage or one result field used as another writable parameter is outside the contract
Limits retain the supported profile's 64 MiB package buffers, 64 KiB certificate, 256 references/mutations, 4096 selectors, 16 MiB ordinary part payloads, 4096 UTF-16 units per part name or Reference URI and one Mi UTF-16 units of plan text where applicable; copied mutation payloads are capped at 64 MiB cumulatively
Destination backup and the visible/private model serializations have separate bounded lifetimes; simultaneous buffers can consume several package-sized allocations, so these limits do not constitute an aggregate process-memory cap
There is no hard callback, provider, operating-system or wall-clock deadline; recovery from arbitrary allocation failure is not promised
Successful issuing and re-signing retain legacy Trust = xsstUntrusted; a valid new signature authenticates its selected coverage and does not assert that every package part is signed or that its certificate is trusted
Apply the separate caller-owned selected-signature trust policy when required, with explicit anchors, UTC time and policy; signing does not enable trust automatically
Examples with caller-owned signing inputs
These procedures receive an already configured explicit selection, public certificate and matching borrowed signer; key creation and authorization remain application responsibilities
uses SysUtils, Classes, lxSignatureOpcIntegrity, lxSignatureOpcPublication;
procedure CaptureAndPublish(Source, Destination: TStream;
const Certificate: TBytes; const Options: TXLSSignatureOpcIssueOptions;
Signer: TXLSSignatureOpcSignDigest);
var
Bytes: TBytes;
Details: TXLSSignatureOpcIssueResult;
Publication: TXLSSignatureOpcPublicationResult;
begin
if not XlsCaptureSignatureOpcInput(Source, Bytes) then
Exit;
if XlsIssueSignatureOpcPackageToStream(Bytes, Certificate, Options,
Signer, Destination, Details, Publication) <> xsopuPublished then
Writeln(Publication.Diagnostic);
end;
The next procedure edits a verified Workbook and explicitly authorizes its actual old identity; configure Options to cover the intended final parts and relationships rather than copying old selectors implicitly
uses SysUtils, lxHandleX, lxHash, lxSignatureOpcIntegrity,
lxSignatureOpcPublication;
procedure EditAndResign(const OriginalBytes, Certificate: TBytes;
const FileName: WideString; const Options: TXLSSignatureOpcIssueOptions;
Signer: TXLSSignatureOpcMutateReplaceSignDigest);
var
Workbook: TXLSXWorkbook;
Original: TXLSSignatureOpcIntegrityResult;
Authorization: TXLSSignatureOpcReplaceAuthorization;
Details: TXLSSignatureOpcMutateReplaceResult;
Publication: TXLSSignatureOpcPublicationResult;
begin
Workbook:= TXLSXWorkbook.Create;
try
if Workbook.OpenVerifiedOpcPackage(OriginalBytes, Original) <> 1 then
Exit;
if Workbook.Sheets.Count = 0 then
Exit;
Authorization.ExpectedPackageSha256:= WideString(XLSComputeSHA256Hex(OriginalBytes));
Authorization.ExpectedSignaturePart:= Original.SignaturePart;
Authorization.ExpectedCertificateSha256:= WideString(XLSComputeSHA256Hex(Original.Certificate));
Workbook.Sheets[1].Cells.Item[1, 1].Value:= 'Approved';
if Workbook.SaveResignedVerifiedOpcPackage(FileName, Certificate,
Authorization, Options, Signer, Details, Publication) <> xsopuPublished then
Writeln(Publication.Diagnostic);
finally
Workbook.Free;
end;
end;
Qualification and remaining scope
Representative runtime qualification uses RAD Studio 37 on Win64, including actual mutable Workbook signing/re-signing, stream prefix/truncation and recoverable failures, file failure, callback/alias controls and subsequent verified saves; independent WindowsBase and mathematical checks verify the actual certificates and selected references
These Workbook and Stream/File operations retain their bounded single-signature RSA-SHA256 profile; the separate preserve-all byte-array append operation adds a signature to an admitted already signed package without widening Workbook load/save admission
The separate selected byte-array replacement operation uses explicit original package/name/certificate authority and preserves the other signatures; it does not change these Workbook publication profiles
Additional algorithms and signature profiles, multiple-signature ordinary mutation/re-signing, timestamp authorities, wider certificate/key-provider integration, packages beyond current bounds and remaining compiler/platform/trust combinations require separate implementation or qualification
There is no claim that every supported compiler or platform passed these representative checks, that arbitrary caller effects are rolled back, or that selected coverage signs the entire package