Skip to content

Commit 492edb6

Browse files
authored
Allow to specify User-Agent info (#594)
IB-8023 Signed-off-by: Raul Metsma <[email protected]>
1 parent 357f40c commit 492edb6

29 files changed

+156
-90
lines changed

examples/DigiDocCSharp/DigiDocCSharp.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
<PropertyGroup>
33
<TargetFramework>net472</TargetFramework>
44
<OutputType>Exe</OutputType>
5-
<AssemblyVersion>0.5.0.0</AssemblyVersion>
6-
<FileVersion>0.5.0.0</FileVersion>
5+
<Version>0.6.0.0</FileVersion>
76
<Copyright>Copyright © 2015</Copyright>
87
</PropertyGroup>
98
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
10-
<DefineConstants>_WINDOWS</DefineConstants>
9+
<DefineConstants>$(DefineConstants);_WINDOWS</DefineConstants>
1110
</PropertyGroup>
1211
<ItemGroup>
1312
<Folder Include="digidoc\" />

examples/DigiDocCSharp/Program.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private static void Add(string[] args)
4040
try
4141
{
4242
Console.WriteLine("Creating file: " + args[args.Length - 1]);
43-
Container b = Container.create(args[args.Length - 1]);
43+
var b = Container.create(args[args.Length - 1]);
4444
for (int i = 1; i < args.Length - 1; ++i)
4545
{
4646
b.addDataFile(args[i], "application/octet-stream");
@@ -60,9 +60,9 @@ private static void Extract(int index, string file)
6060
try
6161
{
6262
Console.WriteLine("Opening file: " + file);
63-
Container b = Container.open(file);
64-
DataFile d = b.dataFiles()[index];
65-
string dest = Path.Combine(Directory.GetCurrentDirectory(), d.fileName());
63+
var b = Container.open(file);
64+
var d = b.dataFiles()[index];
65+
var dest = Path.Combine(Directory.GetCurrentDirectory(), d.fileName());
6666
Console.WriteLine("Extracting file {0} to {1}", d.fileName(), dest);
6767
try
6868
{
@@ -114,7 +114,7 @@ private static void Sign(string[] args)
114114
try
115115
{
116116
Console.WriteLine("Creating file: " + args[args.Length - 1]);
117-
Container b = Container.create(args[args.Length - 1]);
117+
var b = Container.create(args[args.Length - 1]);
118118
#if _WINDOWS
119119
for (int i = 1; i < args.Length - 1; ++i)
120120
#else
@@ -148,26 +148,27 @@ private static void Websign(string[] args)
148148
try
149149
{
150150
Console.WriteLine("Creating file: " + args[args.Length - 1]);
151-
Container b = Container.create(args[args.Length - 1]);
151+
var b = Container.create(args[args.Length - 1]);
152152
for (int i = 1; i < args.Length - 2; ++i)
153153
{
154154
b.addDataFile(args[i], "application/octet-stream");
155155
}
156156

157157
var cert = new X509Certificate(args[args.Length - 2]);
158-
Signature c = b.prepareWebSignature(cert.Export(X509ContentType.Cert), "time-stamp");
158+
var signer = new ExternalSigner(cert.Export(X509ContentType.Cert));
159+
var c = b.prepareSignature(signer);
159160
Console.WriteLine("Signature method: " + c.signatureMethod());
160161
Console.WriteLine("Digest to sign: " + BitConverter.ToString(c.dataToSign()).Replace("-", string.Empty));
161162
Console.WriteLine("Please enter signed digest in hex: ");
162163

163-
byte[] inputBuffer = new byte[1024];
164-
Stream inputStream = Console.OpenStandardInput(inputBuffer.Length);
164+
var inputBuffer = new byte[1024];
165+
var inputStream = Console.OpenStandardInput(inputBuffer.Length);
165166
Console.SetIn(new StreamReader(inputStream, Console.InputEncoding, false, inputBuffer.Length));
166-
string hex = Console.ReadLine();
167+
var hex = Console.ReadLine();
167168

168-
byte[] signature = Enumerable.Range(0, hex.Length / 2).Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16)).ToArray();
169+
var signature = Enumerable.Range(0, hex.Length / 2).Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16)).ToArray();
169170
c.setSignatureValue(signature);
170-
c.extendSignatureProfile("time-stamp");
171+
c.extendSignatureProfile(signer);
171172
b.save();
172173
}
173174
catch (Exception e)
@@ -184,7 +185,7 @@ private static void Verify(string file)
184185
{
185186
Console.WriteLine("Opening file: " + file);
186187
var cb = new ContainerOpen();
187-
Container b = Container.open(file, cb);
188+
var b = Container.open(file, cb);
188189

189190
Console.WriteLine("Files:");
190191
foreach (DataFile d in b.dataFiles())

examples/java/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

examples/java/src/main/java/ee/ria/libdigidocpp/libdigidocpp.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ static void websign(String[] args) {
109109
b.addDataFile(args[i], "application/octet-stream");
110110

111111
X509Certificate cert = toX509(Files.readAllBytes(Paths.get(args[args.length - 2])));
112-
Signature c = b.prepareWebSignature(cert.getEncoded(), "time-stamp");
112+
ExternalSigner signer = new ExternalSigner(cert.getEncoded());
113+
Signature c = b.prepareSignature(signer);
113114
System.out.println("Signature method: " + c.signatureMethod());
114115
System.out.println("Digest to sign: " + HexFormat.of().formatHex(c.dataToSign()));
115116
System.out.println("Please enter signed digest in hex: ");
116117

117118
String signature = scanner.nextLine();
118119
c.setSignatureValue(HexFormat.of().parseHex(signature));
119-
c.extendSignatureProfile("time-stamp");
120+
c.extendSignatureProfile(signer);
120121
b.save();
121122
}
122123
catch (Exception e)
@@ -172,7 +173,7 @@ static void verify(String file) {
172173
}
173174

174175
static void version() {
175-
System.out.println("DigiDocJAVA 0.4 libdigidocpp " + digidoc.version());
176+
System.out.println("DigiDocJAVA 0.5 libdigidocpp " + digidoc.version());
176177
}
177178

178179
static X509Certificate toX509(byte[] der) throws CertificateException {

libdigidocpp.dox

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ signature->setSignatureValue(signatureValue);
928928

929929
5. Add time-stamp and OCSP data to Signature object, according to the signature's profile (see also section \ref API-sign-profile for more information):
930930
\code{.cpp}
931-
signature->extendSignatureProfile(signer->profile());
931+
signature->extendSignatureProfile(signer);
932932
\endcode
933933

934934
6. Write the document to output, as specified in section \ref containeropen

libdigidocpp.i

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -274,17 +274,7 @@ def transfer(self):
274274
const std::string &city = {}, const std::string &state = {},
275275
const std::string &postalCode = {}, const std::string &country = {})
276276
{
277-
class final: public digidoc::Signer
278-
{
279-
public:
280-
digidoc::X509Cert cert() const final { return _cert; }
281-
std::vector<unsigned char> sign(const std::string &, const std::vector<unsigned char> &) const final
282-
{
283-
THROW("Not implemented");
284-
}
285-
digidoc::X509Cert _cert;
286-
} signer;
287-
signer._cert = digidoc::X509Cert(cert, digidoc::X509Cert::Der);
277+
digidoc::ExternalSigner signer(cert);
288278
signer.setProfile(profile);
289279
signer.setSignatureProductionPlace(city, state, postalCode, country);
290280
signer.setSignerRoles(roles);

src/ASiC_E.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ Signature *ASiC_E::sign(Signer* signer)
302302
try
303303
{
304304
s->setSignatureValue(signer->sign(s->signatureMethod(), s->dataToSign()));
305-
s->extendSignatureProfile(signer->profile());
305+
s->extendSignatureProfile(signer);
306306
}
307307
catch(const Exception& e)
308308
{

src/Signature.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "Signature.h"
2121

2222
#include "Exception.h"
23+
#include "crypto/Signer.h"
2324
#include "crypto/X509Cert.h"
2425

2526
#include <algorithm>
@@ -166,7 +167,22 @@ void Signature::validate(const std::string & /*policy*/) const { validate(); }
166167
*
167168
* @param profile Target profile
168169
*/
169-
void Signature::extendSignatureProfile(const string & /*profile*/) {}
170+
void Signature::extendSignatureProfile(const string &profile) {
171+
struct ProfileSigner: public Signer
172+
{
173+
X509Cert cert() const { return X509Cert(); }
174+
vector<unsigned char> sign(const string &/*method*/, const vector<unsigned char> &/*digest*/) const { return {}; }
175+
} signer;
176+
signer.setProfile(profile);
177+
extendSignatureProfile(&signer);
178+
}
179+
180+
/**
181+
* Extends signature to selected profile
182+
*
183+
* @param signer Signer parameters
184+
*/
185+
void Signature::extendSignatureProfile(Signer * /*signer*/) {}
170186

171187
/**
172188
* Returns signature policy when it is available or empty string.

src/Signature.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace digidoc
2828
{
29+
class Signer;
2930
class X509Cert;
3031
class DIGIDOCPP_EXPORT Signature
3132
{
@@ -73,7 +74,7 @@ namespace digidoc
7374
virtual void validate() const = 0;
7475
virtual std::vector<unsigned char> dataToSign() const = 0;
7576
virtual void setSignatureValue(const std::vector<unsigned char> &signatureValue) = 0;
76-
virtual void extendSignatureProfile(const std::string &profile);
77+
DIGIDOCPP_DEPRECATED virtual void extendSignatureProfile(const std::string &profile);
7778

7879
// Xades properties
7980
virtual std::string policy() const;
@@ -110,6 +111,9 @@ namespace digidoc
110111
// Other
111112
virtual std::vector<unsigned char> messageImprint() const;
112113

114+
// DSig properties
115+
virtual void extendSignatureProfile(Signer *signer);
116+
113117
protected:
114118
Signature();
115119

src/SignatureXAdES_LT.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "Conf.h"
2424
#include "crypto/Digest.h"
2525
#include "crypto/OCSP.h"
26+
#include "crypto/Signer.h"
2627
#include "crypto/TS.h"
2728
#include "crypto/X509Cert.h"
2829
#include "crypto/X509CertStore.h"
@@ -202,10 +203,10 @@ void SignatureXAdES_LT::validate(const string &policy) const
202203
*
203204
* @throws SignatureException
204205
*/
205-
void SignatureXAdES_LT::extendSignatureProfile(const string &profile)
206+
void SignatureXAdES_LT::extendSignatureProfile(Signer *signer)
206207
{
207-
SignatureXAdES_T::extendSignatureProfile(profile);
208-
if(profile.find(ASiC_E::ASIC_TS_PROFILE) == string::npos)
208+
SignatureXAdES_T::extendSignatureProfile(signer);
209+
if(signer->profile().find(ASiC_E::ASIC_TS_PROFILE) == string::npos)
209210
return;
210211

211212
// Get issuer certificate from certificate store.
@@ -217,7 +218,7 @@ void SignatureXAdES_LT::extendSignatureProfile(const string &profile)
217218
THROW("Could not find certificate issuer '%s' in certificate store or from AIA.",
218219
cert.issuerName().c_str());
219220

220-
OCSP ocsp(cert, issuer);
221+
OCSP ocsp(cert, issuer, signer->userAgent());
221222
ocsp.verifyResponse(cert);
222223

223224
addCertificateValue(id() + "-CA-CERT", issuer);

0 commit comments

Comments
 (0)