Swisscom cryptographic provider
The SwisscomSigSrv
cryptographic provider enables access to the Swisscom Signing Service. The service can then be used to perform cryptographic functions such as sign a document.
This provider requires a Swisscom Signing Service account.
Accounts with static and on-demand identities are supported.
Download the full sample now in C# and Java.
Interested in C or other language samples? Let us know on the contact page and we'll add it to our samples backlog.
The Swisscom Signing Service provides signing certificates using CMS (PKCS#7) signatures. Additional certificates (for example, issuer certificates) are stored in the Certificates directory. These certificates are required when adding validation information to signatures that do not have the full trust chain embedded. The Certificates directory may contain certificates in either PEM (.pem, ASCII text) or DER (.cer, binary) form.
In this example, the SwisscomSigSrv
cryptographic provider is used to apply a document approval signature to a PDF document.
The signing certificate is loaded from the Swisscom Signing Service using a static identity string provided by Swisscom.
Information required for long-term signature validation (LTV) is embedded in the output PDF.
Steps to sign a document:
- Configure the HTTP client handler.
- Connect to Swisscom Signing Service.
- Create the document signature.
- (Optional) Add long-term validation information.
- Open and sign the document.
You need to initialize the library.
Configuring the HTTP client handler
When using the SwisscomSigSrv
cryptographic provider, you need to configure the HttpClientHandler
with the SSL client certificate and password from your Swisscom account.
- .NET
- Java
// Configure the SSL client certificate to connect to the Swisscom Signing Service
var httpClientHandler = new HttpClientHandler();
using (var sslClientCert = File.OpenRead(@"C:\path\to\clientcert.p12"))
httpClientHandler.SetClientCertificate(sslClientCert, password);
// Configure the SSL client certificate to connect to the service
HttpClientHandler httpClientHandler = new HttpClientHandler();
try (FileStream sslClientCert = new FileStream("C:/path/to/clientcert.p12", FileStream.Mode.READ_ONLY))
{
httpClientHandler.setClientCertificate(sslClientCert, password);
}
Connecting to Swisscom Signing Service
The next step is to open a Session
to the Swisscom Signing Service.
The Session
object provides access to the certificates and private keys stored by Swisscom.
In this example, the default URI for the Swisscom Signing Service is used.
- .NET
- Java
// Connect to the Swisscom Signing Service
using var session = new SwisscomSigSrv.Session(new Uri("https://ais.swisscom.com"), httpClientHandler);
Session session = new Session(new URI("https://ais.swisscom.com"), httpClientHandler);
Creating the document signature
In this step, the Session
object is used to create a signature configuration for a Swisscom Signing Service account using a static identity string provided by Swisscom.
With a static identity, the common name is used for the signature appearance and for the signature description stored in the PDF document.
The signature configuration may be used to sign one or more documents.
To sign with Mobile ID, you use the CreateSignatureForDynamicIdentity
method and pass StepUp
authorization parameters.
- .NET
- Java
// Create a signing certificate for a static identity
var signature = session.CreateSignatureForStaticIdentity(identity, commonName);
// Create a signing certificate for a static identity
// This can be re-used to sign multiple documents
SignatureConfiguration signature = session.createSignatureForStaticIdentity(identity, commonName);
Adding long-term validation information
As an optional step, long-term validation information can be added to the output document. It embeds revocation information such as online certificate status response and certificate revocation lists. Revocation information is provided by a validation service at the time of signing and acts as proof that the certificate was valid at the time of signing.
- .NET
- Java
// Embed validation information to enable the long term validation (LTV) of the signature (default)
signature.EmbedValidationInformation = true;
// Embed validation information to enable the long-term validation (LTV) of the signature (default)
signature.setEmbedValidationInformation(true);
Opening and signing the document
After opening the Session
and creating the signature configuration, you are ready to apply the digital signature to a document.
The input and output PDF documents are created as streams (in this example, as file streams). The Signer
object is used to apply the digital document signature.
Non-critical processing errors raise a Warning
event. It is recommended to listen for these events, and review the WarningCategory
to determine if further action is needed.
- .NET
- Java
// Open the input document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Create a stream for the output file
using var outStr = File.Create(outPath);
// Create the Signer object
Signer signer = new Signer();
// Create an event listener to listen for warning events that are raised and write them to console
signer.Warning += (s, e) => Console.WriteLine("Warning - {0}: {1}: {2}", e.Category, e.Context, e.Message);
// Sign the output document
using var outDoc = signer.Sign(inDoc, signature, outStr);
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
Document inDoc = Document.open(inStr);
// Create output stream
FileStream outStr = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
// Create the Signer object
Signer signer = new Signer();
// (optional) Create an event listener to listen for warning events that are raised and write them to console
signer.addWarningListener((e) -> { System.out.format("Warning - %s: %s: %s", e.getCategory(), e.getContext(), e.getMessage()); });
// Sign the input document
Document outDoc = signer.sign(inDoc, signature, outStr);
Full example
- .NET
- Java
// Configure the SSL client certificate to connect to the Swisscom Signing Service
var httpClientHandler = new HttpClientHandler();
using (var sslClientCert = File.OpenRead(@"C:\path\to\clientcert.p12"))
httpClientHandler.SetClientCertificate(sslClientCert, password);
// Connect to the Swisscom Signing Service
using var session = new SwisscomSigSrv.Session(new Uri("https://ais.swisscom.com"), httpClientHandler);
// Create a signing certificate for a static identity
var signature = session.CreateSignatureForStaticIdentity(identity, commonName);
// Embed validation information to enable the long term validation (LTV) of the signature (default)
signature.EmbedValidationInformation = true;
// Open the input document
using var inStr = File.OpenRead(inPath);
using var inDoc = Document.Open(inStr);
// Create a stream for the output file
using var outStr = File.Create(outPath);
// Create the Signer object
Signer signer = new Signer();
// Create an event listener to listen for warning events that are raised and write them to console
signer.Warning += (s, e) => Console.WriteLine("Warning - {0}: {1}: {2}", e.Category, e.Context, e.Message);
// Sign the output document
using var outDoc = signer.Sign(inDoc, signature, outStr);
// Configure the SSL client certificate to connect to the service
HttpClientHandler httpClientHandler = new HttpClientHandler();
try (FileStream sslClientCert = new FileStream("C:/path/to/clientcert.p12", FileStream.Mode.READ_ONLY))
{
httpClientHandler.setClientCertificate(sslClientCert, password);
}
// Connect to the Swisscom Signing Service
try (Session session = new Session(new URI("https://ais.swisscom.com"), httpClientHandler))
{
// Create a signing certificate for a static identity
// This can be re-used to sign multiple documents
SignatureConfiguration signature = session.createSignatureForStaticIdentity(identity, commonName);
// Embed validation information to enable the long-term validation (LTV) of the signature (default)
signature.setEmbedValidationInformation(true);
// Create the Signer object
Signer signer = new Signer();
// (optional) Create an event listener to listen for warning events that are raised and write them to console
signer.addWarningListener((e) -> { System.out.format("Warning - %s: %s: %s", e.getCategory(), e.getContext(), e.getMessage()); });
try (
// Open input document
FileStream inStr = new FileStream(inPath, FileStream.Mode.READ_ONLY);
Document inDoc = Document.open(inStr);
// Create output stream
FileStream outStr = new FileStream(outPath, FileStream.Mode.READ_WRITE_NEW);
// Sign the input document
Document outDoc = signer.sign(inDoc, signature, outStr))
{
}
}