• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ebourg / jsign / #397

27 Mar 2026 08:06AM UTC coverage: 80.72% (+0.04%) from 80.685%
#397

push

ebourg
Fixed the documentation of the --verbose and --debug options

4978 of 6167 relevant lines covered (80.72%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.69
/jsign-core/src/main/java/net/jsign/SignerHelper.java
1
/*
2
 * Copyright 2017 Emmanuel Bourg and contributors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package net.jsign;
18

19
import java.io.File;
20
import java.io.FileWriter;
21
import java.io.IOException;
22
import java.nio.charset.Charset;
23
import java.nio.file.Files;
24
import java.security.KeyStore;
25
import java.security.KeyStoreException;
26
import java.security.PrivateKey;
27
import java.security.Provider;
28
import java.security.cert.Certificate;
29
import java.util.ArrayList;
30
import java.util.Base64;
31
import java.util.Collection;
32
import java.util.Collections;
33
import java.util.Date;
34
import java.util.LinkedHashSet;
35
import java.util.List;
36
import java.util.Set;
37
import java.util.logging.Logger;
38

39
import org.bouncycastle.asn1.ASN1Encodable;
40
import org.bouncycastle.asn1.ASN1InputStream;
41
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
42
import org.bouncycastle.asn1.DEROctetString;
43
import org.bouncycastle.asn1.DERSet;
44
import org.bouncycastle.asn1.DERUTF8String;
45
import org.bouncycastle.asn1.cms.AttributeTable;
46
import org.bouncycastle.asn1.cms.ContentInfo;
47
import org.bouncycastle.cert.X509CertificateHolder;
48
import org.bouncycastle.cms.CMSException;
49
import org.bouncycastle.cms.CMSProcessable;
50
import org.bouncycastle.cms.CMSSignedData;
51
import org.bouncycastle.cms.SignerId;
52
import org.bouncycastle.cms.SignerInformation;
53
import org.bouncycastle.cms.SignerInformationStore;
54
import org.bouncycastle.operator.DefaultAlgorithmNameFinder;
55
import org.bouncycastle.util.encoders.Hex;
56

57
import net.jsign.asn1.authenticode.AuthenticodeObjectIdentifiers;
58
import net.jsign.timestamp.Timestamper;
59
import net.jsign.timestamp.TimestampingMode;
60

61
/**
62
 * Helper class to create AuthenticodeSigner instances with untyped parameters.
63
 * This is used internally to share the parameter validation logic
64
 * between the Ant task, the Maven/Gradle plugins and the CLI tool.
65
 *
66
 * @since 2.0
67
 */
68
class SignerHelper {
69
    public static final String PARAM_COMMAND = "command";
70
    public static final String PARAM_KEYSTORE = "keystore";
71
    public static final String PARAM_STOREPASS = "storepass";
72
    public static final String PARAM_STORETYPE = "storetype";
73
    public static final String PARAM_ALIAS = "alias";
74
    public static final String PARAM_KEYPASS = "keypass";
75
    public static final String PARAM_KEYFILE = "keyfile";
76
    public static final String PARAM_CERTFILE = "certfile";
77
    public static final String PARAM_ALG = "alg";
78
    public static final String PARAM_TSAURL = "tsaurl";
79
    public static final String PARAM_TSMODE = "tsmode";
80
    public static final String PARAM_TSRETRIES = "tsretries";
81
    public static final String PARAM_TSRETRY_WAIT = "tsretrywait";
82
    public static final String PARAM_NAME = "name";
83
    public static final String PARAM_URL = "url";
84
    public static final String PARAM_PROXY_URL = "proxyUrl";
85
    public static final String PARAM_PROXY_USER = "proxyUser";
86
    public static final String PARAM_PROXY_PASS = "proxyPass";
87
    public static final String PARAM_REPLACE = "replace";
88
    public static final String PARAM_ENCODING = "encoding";
89
    public static final String PARAM_DETACHED = "detached";
90
    public static final String PARAM_FORMAT = "format";
91
    public static final String PARAM_VALUE = "value";
92

93
    private final Logger log = Logger.getLogger(getClass().getName());
1✔
94

95
    /** The name used to refer to a configuration parameter */
96
    private final String parameterName;
97

98
    /** The command to execute */
99
    private String command = "sign";
1✔
100

101
    private final KeyStoreBuilder ksparams;
102
    private String alias;
103
    private String tsaurl;
104
    private String tsmode;
105
    private int tsretries = -1;
1✔
106
    private int tsretrywait = -1;
1✔
107
    private String alg;
108
    private String name;
109
    private String url;
110
    private final ProxySettings proxySettings = new ProxySettings();
1✔
111
    private boolean replace;
112
    private Charset encoding;
113
    private boolean detached;
114
    private String format;
115
    private String value;
116

117
    private AuthenticodeSigner signer;
118

119
    public SignerHelper(String parameterName) {
1✔
120
        this.parameterName = parameterName;
1✔
121
        this.ksparams = new KeyStoreBuilder(parameterName);
1✔
122
    }
1✔
123

124
    public SignerHelper command(String command) {
125
        this.command = command;
1✔
126
        return this;
1✔
127
    }
128

129
    public SignerHelper keystore(String keystore) {
130
        ksparams.keystore(keystore);
1✔
131
        signer = null;
1✔
132
        return this;
1✔
133
    }
134

135
    public SignerHelper storepass(String storepass) {
136
        ksparams.storepass(storepass);
1✔
137
        signer = null;
1✔
138
        return this;
1✔
139
    }
140

141
    public SignerHelper storetype(String storetype) {
142
        ksparams.storetype(storetype);
1✔
143
        signer = null;
1✔
144
        return this;
1✔
145
    }
146

147
    public SignerHelper alias(String alias) {
148
        this.alias = alias;
1✔
149
        signer = null;
1✔
150
        return this;
1✔
151
    }
152

153
    public SignerHelper keypass(String keypass) {
154
        ksparams.keypass(keypass);
1✔
155
        signer = null;
1✔
156
        return this;
1✔
157
    }
158

159
    public SignerHelper keyfile(String keyfile) {
160
        ksparams.keyfile(keyfile);
1✔
161
        signer = null;
1✔
162
        return this;
1✔
163
    }
164

165
    public SignerHelper keyfile(File keyfile) {
166
        ksparams.keyfile(keyfile);
1✔
167
        signer = null;
1✔
168
        return this;
1✔
169
    }
170

171
    public SignerHelper certfile(String certfile) {
172
        ksparams.certfile(certfile);
1✔
173
        signer = null;
1✔
174
        return this;
1✔
175
    }
176

177
    public SignerHelper certfile(File certfile) {
178
        ksparams.certfile(certfile);
1✔
179
        signer = null;
1✔
180
        return this;
1✔
181
    }
182

183
    public SignerHelper alg(String alg) {
184
        this.alg = alg;
1✔
185
        signer = null;
1✔
186
        return this;
1✔
187
    }
188

189
    public SignerHelper tsaurl(String tsaurl) {
190
        this.tsaurl = tsaurl;
1✔
191
        signer = null;
1✔
192
        return this;
1✔
193
    }
194

195
    public SignerHelper tsmode(String tsmode) {
196
        this.tsmode = tsmode;
1✔
197
        signer = null;
1✔
198
        return this;
1✔
199
    }
200

201
    public SignerHelper tsretries(int tsretries) {
202
        this.tsretries = tsretries;
1✔
203
        signer = null;
1✔
204
        return this;
1✔
205
    }
206

207
    public SignerHelper tsretrywait(int tsretrywait) {
208
        this.tsretrywait = tsretrywait;
1✔
209
        signer = null;
1✔
210
        return this;
1✔
211
    }
212

213
    public SignerHelper name(String name) {
214
        this.name = name;
1✔
215
        signer = null;
1✔
216
        return this;
1✔
217
    }
218

219
    public SignerHelper url(String url) {
220
        this.url = url;
1✔
221
        signer = null;
1✔
222
        return this;
1✔
223
    }
224

225
    public SignerHelper proxyUrl(String proxyUrl) {
226
        this.proxySettings.url = proxyUrl;
1✔
227
        signer = null;
1✔
228
        return this;
1✔
229
    }
230

231
    public SignerHelper proxyUser(String proxyUser) {
232
        this.proxySettings.username = proxyUser;
1✔
233
        signer = null;
1✔
234
        return this;
1✔
235
    }
236

237
    public SignerHelper proxyPass(String proxyPass) {
238
        this.proxySettings.password = proxyPass;
1✔
239
        signer = null;
1✔
240
        return this;
1✔
241
    }
242

243
    public SignerHelper replace(boolean replace) {
244
        this.replace = replace;
1✔
245
        signer = null;
1✔
246
        return this;
1✔
247
    }
248

249
    public SignerHelper encoding(String encoding) {
250
        this.encoding = Charset.forName(encoding);
1✔
251
        return this;
1✔
252
    }
253

254
    public SignerHelper detached(boolean detached) {
255
        this.detached = detached;
1✔
256
        return this;
1✔
257
    }
258

259
    public SignerHelper format(String format) {
260
        this.format = format;
1✔
261
        return this;
1✔
262
    }
263

264
    public SignerHelper value(String value) {
265
        this.value = value;
1✔
266
        return this;
1✔
267
    }
268

269
    public SignerHelper param(String key, String value) {
270
        if (value == null) {
1✔
271
            return this;
1✔
272
        }
273
        
274
        switch (key) {
1✔
275
            case PARAM_COMMAND:    return command(value);
×
276
            case PARAM_KEYSTORE:   return keystore(value);
1✔
277
            case PARAM_STOREPASS:  return storepass(value);
1✔
278
            case PARAM_STORETYPE:  return storetype(value);
1✔
279
            case PARAM_ALIAS:      return alias(value);
1✔
280
            case PARAM_KEYPASS:    return keypass(value);
1✔
281
            case PARAM_KEYFILE:    return keyfile(value);
1✔
282
            case PARAM_CERTFILE:   return certfile(value);
1✔
283
            case PARAM_ALG:        return alg(value);
1✔
284
            case PARAM_TSAURL:     return tsaurl(value);
1✔
285
            case PARAM_TSMODE:     return tsmode(value);
1✔
286
            case PARAM_TSRETRIES:  return tsretries(Integer.parseInt(value));
1✔
287
            case PARAM_TSRETRY_WAIT: return tsretrywait(Integer.parseInt(value));
1✔
288
            case PARAM_NAME:       return name(value);
1✔
289
            case PARAM_URL:        return url(value);
1✔
290
            case PARAM_PROXY_URL:  return proxyUrl(value);
1✔
291
            case PARAM_PROXY_USER: return proxyUser(value);
1✔
292
            case PARAM_PROXY_PASS: return proxyPass(value);
1✔
293
            case PARAM_REPLACE:    return replace("true".equalsIgnoreCase(value));
×
294
            case PARAM_ENCODING:   return encoding(value);
1✔
295
            case PARAM_DETACHED:   return detached("true".equalsIgnoreCase(value));
×
296
            case PARAM_FORMAT:     return format(value);
×
297
            case PARAM_VALUE:      return value(value);
1✔
298
            default:
299
                throw new IllegalArgumentException("Unknown " + parameterName + ": " + key);
×
300
        }
301
    }
302

303
    void setBaseDir(File basedir) {
304
        ksparams.setBaseDir(basedir);
1✔
305
    }
1✔
306

307
    public void execute(String file) throws SignerException {
308
        execute(ksparams.createFile(file));
×
309
    }
×
310

311
    public void execute(File file) throws SignerException {
312
        switch (command) {
1✔
313
            case "sign":
314
                sign(file);
1✔
315
                break;
1✔
316
            case "timestamp":
317
                timestamp(file);
1✔
318
                break;
1✔
319
            case "extract":
320
                extract(file);
1✔
321
                break;
1✔
322
            case "remove":
323
                remove(file);
1✔
324
                break;
1✔
325
            case "tag":
326
                tag(file);
1✔
327
                break;
1✔
328
            default:
329
                throw new SignerException("Unknown command '" + command + "'");
1✔
330
        }
331
    }
1✔
332

333
    private AuthenticodeSigner build() throws SignerException {
334
        try {
335
            proxySettings.initializeProxy();
1✔
336
        } catch (Exception e) {
×
337
            throw new SignerException("Couldn't initialize proxy", e);
×
338
        }
1✔
339

340
        KeyStore ks;
341
        try {
342
            ks = ksparams.build();
1✔
343
        } catch (KeyStoreException e) {
1✔
344
            throw new SignerException("Failed to load the keystore " + (ksparams.keystore() != null ? ksparams.keystore() : ""), e);
1✔
345
        }
1✔
346
        KeyStoreType storetype = ksparams.storetype();
1✔
347
        Provider provider = ksparams.provider();
1✔
348

349
        Set<String> aliases = null;
1✔
350
        if (alias == null) {
1✔
351
            // guess the alias if there is only one in the keystore
352
            try {
353
                aliases = storetype.getAliases(ks);
1✔
354
            } catch (KeyStoreException e) {
×
355
                throw new SignerException(e.getMessage(), e);
×
356
            }
1✔
357

358
            if (aliases.isEmpty()) {
1✔
359
                throw new SignerException("No certificate found in the keystore " + (provider != null ? provider.getName() : ksparams.keystore()));
×
360
            } else if (aliases.size() == 1) {
1✔
361
                alias = aliases.iterator().next();
1✔
362
            } else {
363
                throw new SignerException("alias " + parameterName + " must be set to select a certificate (available aliases: " + String.join(", ", aliases) + ")");
1✔
364
            }
365
        }
366

367
        Certificate[] chain;
368
        if (ksparams.certfile() != null) {
1✔
369
            // replace the certificate chain from the keystore with the complete chain from file
370
            try {
371
                chain = CertificateUtils.loadCertificateChain(ksparams.certfile());
1✔
372
            } catch (Exception e) {
×
373
                throw new SignerException("Failed to load the certificate from " + ksparams.certfile(), e);
×
374
            }
1✔
375
        } else {
376
            try {
377
                chain = ks.getCertificateChain(alias);
1✔
378
            } catch (KeyStoreException e) {
×
379
                throw new SignerException(e.getMessage(), e);
×
380
            }
1✔
381
            if (chain == null) {
1✔
382
                String message = "No certificate found under the alias '" + alias + "' in the keystore " + (provider != null ? provider.getName() : ksparams.keystore());
1✔
383
                if (aliases == null) {
1✔
384
                    try {
385
                        aliases = new LinkedHashSet<>(Collections.list(ks.aliases()));
1✔
386
                        if (aliases.isEmpty()) {
1✔
387
                            message = "No certificate found in the keystore " + (provider != null ? provider.getName() : ksparams.keystore());
1✔
388
                        } else if (aliases.contains(alias)) {
1✔
389
                            message = "The keystore password must be specified";
1✔
390
                        } else {
391
                            message += " (available aliases: " + String.join(", ", aliases) + ")";
1✔
392
                        }
393
                    } catch (KeyStoreException e) {
×
394
                        message += " (couldn't load the list of available aliases: " + e.getMessage() + ")";
×
395
                    }
1✔
396
                }
397
                throw new SignerException(message);
1✔
398
            }
399
        }
400

401
        String keypass = ksparams.keypass();
1✔
402
        char[] password = keypass != null ? keypass.toCharArray() : new char[0];
1✔
403

404
        PrivateKey privateKey;
405
        try {
406
            privateKey = (PrivateKey) ks.getKey(alias, password);
1✔
407
        } catch (Exception e) {
1✔
408
            throw new SignerException("Failed to retrieve the private key from the keystore", e);
1✔
409
        }
1✔
410

411
        if (alg != null && DigestAlgorithm.of(alg) == null) {
1✔
412
            throw new SignerException("The digest algorithm " + alg + " is not supported");
1✔
413
        }
414

415
        // enable timestamping with Azure Trusted Signing
416
        if (tsaurl == null && storetype == KeyStoreType.TRUSTEDSIGNING) {
1✔
417
            tsaurl = "http://timestamp.acs.microsoft.com/";
×
418
            tsmode = TimestampingMode.RFC3161.name();
×
419
            tsretries = 3;
×
420
        }
421
        
422
        // configure the signer
423
        return new AuthenticodeSigner(chain, privateKey)
1✔
424
                .withProgramName(name)
1✔
425
                .withProgramURL(url)
1✔
426
                .withDigestAlgorithm(DigestAlgorithm.of(alg))
1✔
427
                .withSignatureProvider(provider)
1✔
428
                .withSignaturesReplaced(replace)
1✔
429
                .withTimestamping(tsaurl != null || tsmode != null)
1✔
430
                .withTimestampingMode(tsmode != null ? TimestampingMode.of(tsmode) : TimestampingMode.AUTHENTICODE)
1✔
431
                .withTimestampingRetries(tsretries)
1✔
432
                .withTimestampingRetryWait(tsretrywait)
1✔
433
                .withTimestampingAuthority(tsaurl != null ? tsaurl.split(",") : null);
1✔
434
    }
435

436
    public void sign(String file) throws SignerException {
437
        sign(ksparams.createFile(file));
×
438
    }
×
439

440
    public void sign(File file) throws SignerException {
441
        if (file == null) {
1✔
442
            throw new SignerException("No file specified");
1✔
443
        }
444
        if (!file.exists()) {
1✔
445
            throw new SignerException("The file " + file + " couldn't be found");
1✔
446
        }
447
        
448
        try (Signable signable = Signable.of(file, encoding)) {
1✔
449
            File detachedSignature = getDetachedSignature(file);
1✔
450
            if (detached && detachedSignature.exists()) {
1✔
451
                try {
452
                    log.info("Attaching Authenticode signature to " + file);
1✔
453
                    attach(signable, detachedSignature);
1✔
454
                } catch (Exception e) {
×
455
                    throw new SignerException("Couldn't attach the signature to " + file, e);
×
456
                }
1✔
457

458
            } else {
459
                if (signer == null) {
1✔
460
                    signer = build();
1✔
461
                }
462

463
                log.info("Adding Authenticode signature to " + file);
1✔
464
                signer.sign(signable);
1✔
465

466
                if (detached) {
1✔
467
                    detach(signable, detachedSignature);
1✔
468
                }
469
            }
470

471
        } catch (UnsupportedOperationException | IllegalArgumentException e) {
1✔
472
            throw new SignerException(e.getMessage(), e);
1✔
473
        } catch (SignerException e) {
1✔
474
            throw e;
1✔
475
        } catch (Exception e) {
1✔
476
            throw new SignerException("Couldn't sign " + file, e);
1✔
477
        }
1✔
478
    }
1✔
479

480
    private void attach(Signable signable, File detachedSignature) throws IOException, CMSException {
481
        byte[] signatureBytes = Files.readAllBytes(detachedSignature.toPath());
1✔
482
        CMSSignedData signedData = new CMSSignedData((CMSProcessable) null, ContentInfo.getInstance(new ASN1InputStream(signatureBytes).readObject()));
1✔
483

484
        signable.setSignatures(SignatureUtils.getSignatures(signedData));
1✔
485
        signable.save();
1✔
486
        // todo warn if the hashes don't match
487
    }
1✔
488

489
    private void detach(Signable signable, File detachedSignature) throws IOException {
490
        List<CMSSignedData> signatures = signable.getSignatures();
1✔
491

492
        // ensure the secondary signatures are nested in the first one (for EFI files)
493
        CMSSignedData signedData = signatures.get(0);
1✔
494
        if (signatures.size() > 1) {
1✔
495
            List<CMSSignedData> nestedSignatures = signatures.subList(1, signatures.size());
1✔
496
            signedData = SignatureUtils.addNestedSignature(signedData, true, nestedSignatures.toArray(new CMSSignedData[0]));
1✔
497
        }
498

499
        byte[] content = signedData.toASN1Structure().getEncoded("DER");
1✔
500
        if (format == null || "DER".equalsIgnoreCase(format)) {
1✔
501
            Files.write(detachedSignature.toPath(), content);
1✔
502
        } else if ("PEM".equalsIgnoreCase(format)) {
1✔
503
            try (FileWriter out = new FileWriter(detachedSignature)) {
1✔
504
                String encoded = Base64.getEncoder().encodeToString(content);
1✔
505
                out.write("-----BEGIN PKCS7-----\n");
1✔
506
                for (int i = 0; i < encoded.length(); i += 64) {
1✔
507
                    out.write(encoded.substring(i, Math.min(i + 64, encoded.length())));
1✔
508
                    out.write('\n');
1✔
509
                }
510
                out.write("-----END PKCS7-----\n");
1✔
511
            }
512
        } else {
513
            throw new IllegalArgumentException("Unknown output format '" + format + "'");
1✔
514
        }
515
    }
1✔
516

517
    private File getDetachedSignature(File file) {
518
        return new File(file.getParentFile(), file.getName() + ".sig");
1✔
519
    }
520

521
    private void extract(File file) throws SignerException {
522
        if (!file.exists()) {
1✔
523
            throw new SignerException("Couldn't find " + file);
1✔
524
        }
525

526
        try (Signable signable = Signable.of(file)) {
1✔
527
            List<CMSSignedData> signatures = signable.getSignatures();
1✔
528
            if (signatures.isEmpty()) {
1✔
529
                throw new SignerException("No signature found in " + file);
1✔
530
            }
531

532
            File detachedSignature = getDetachedSignature(file);
1✔
533
            if ("PEM".equalsIgnoreCase(format)) {
1✔
534
                detachedSignature = new File(detachedSignature.getParentFile(), detachedSignature.getName() + ".pem");
1✔
535
            }
536
            log.info("Extracting signature to " + detachedSignature);
1✔
537
            detach(signable, detachedSignature);
1✔
538
        } catch (UnsupportedOperationException | IllegalArgumentException e) {
1✔
539
            throw new SignerException(e.getMessage(), e);
1✔
540
        } catch (SignerException e) {
1✔
541
            throw e;
1✔
542
        } catch (Exception e) {
×
543
            throw new SignerException("Couldn't extract the signature from " + file, e);
×
544
        }
1✔
545
    }
1✔
546

547
    private void remove(File file) throws SignerException {
548
        if (!file.exists()) {
1✔
549
            throw new SignerException("Couldn't find " + file);
1✔
550
        }
551

552
        try (Signable signable = Signable.of(file)) {
1✔
553
            List<CMSSignedData> signatures = signable.getSignatures();
1✔
554
            if (signatures.isEmpty()) {
1✔
555
                log.severe("No signature found in " + file);
1✔
556
                return;
1✔
557
            }
558

559
            log.info("Removing " + signatures.size() + " signature" + (signatures.size() > 1 ? "s" : "") + " from " + file);
1✔
560
            signable.setSignatures(null);
1✔
561
            signable.save();
1✔
562
        } catch (UnsupportedOperationException | IllegalArgumentException e) {
1✔
563
            throw new SignerException(e.getMessage(), e);
×
564
        } catch (Exception e) {
×
565
            throw new SignerException("Couldn't remove the signature from " + file, e);
×
566
        }
1✔
567
    }
1✔
568

569
    private void tag(File file) throws SignerException {
570
        if (!file.exists()) {
1✔
571
            throw new SignerException("Couldn't find " + file);
1✔
572
        }
573

574
        try (Signable signable = Signable.of(file)) {
1✔
575
            List<CMSSignedData> signatures = signable.getSignatures();
1✔
576
            if (signatures.isEmpty()) {
1✔
577
                throw new SignerException("No signature found in " + file);
1✔
578
            }
579

580
            log.info("Adding tag to " + file);
1✔
581
            signatures.set(0, addUnsignedAttribute(signatures.get(0), AuthenticodeObjectIdentifiers.JSIGN_UNSIGNED_DATA_OBJID, getTagValue()));
1✔
582
            signable.setSignatures(signatures);
1✔
583
            signable.save();
1✔
584
        } catch (SignerException e) {
1✔
585
            throw e;
1✔
586
        } catch (Exception e) {
1✔
587
            throw new SignerException("Couldn't modify the signature of " + file, e);
1✔
588
        }
1✔
589
    }
1✔
590

591
    private CMSSignedData addUnsignedAttribute(CMSSignedData signature, ASN1ObjectIdentifier oid, ASN1Encodable value) {
592
        SignerInformationStore store = signature.getSignerInfos();
1✔
593
        Collection<SignerInformation> signers = store.getSigners();
1✔
594
        SignerInformation signer = signers.iterator().next();
1✔
595

596
        AttributeTable attributes = signer.getUnsignedAttributes();
1✔
597
        if (attributes == null) {
1✔
598
            attributes = new AttributeTable(new DERSet());
1✔
599
        }
600
        attributes = attributes.add(oid, value);
1✔
601

602
        signers.remove(signer);
1✔
603
        signers.add(SignerInformation.replaceUnsignedAttributes(signer, attributes));
1✔
604
        return CMSSignedData.replaceSigners(signature, new SignerInformationStore(signers));
1✔
605
    }
606

607
    private ASN1Encodable getTagValue() throws IOException {
608
        if (value == null) {
1✔
609
            byte[] array = new byte[1024];
1✔
610
            String begin = "-----BEGIN TAG-----";
1✔
611
            System.arraycopy(begin.getBytes(), 0, array, 0, begin.length());
1✔
612
            String end = "-----END TAG-----";
1✔
613
            System.arraycopy(end.getBytes(), 0, array, array.length - end.length(), end.length());
1✔
614
            return new DEROctetString(array);
1✔
615

616
        } else if (value.startsWith("0x")) {
1✔
617
            byte[] array = Hex.decode(value.substring(2));
1✔
618
            return new DEROctetString(array);
1✔
619

620
        } else if (value.startsWith("file:")) {
1✔
621
            byte[] array = Files.readAllBytes(new File(value.substring("file:".length())).toPath());
1✔
622
            return new DEROctetString(array);
1✔
623

624
        } else {
625
            return new DERUTF8String(value);
1✔
626
        }
627
    }
628

629
    private void timestamp(File file) throws SignerException {
630
        if (!file.exists()) {
1✔
631
            throw new SignerException("Couldn't find " + file);
1✔
632
        }
633

634
        try {
635
            proxySettings.initializeProxy();
1✔
636
        } catch (Exception e) {
×
637
            throw new SignerException("Couldn't initialize proxy", e);
×
638
        }
1✔
639

640
        try (Signable signable = Signable.of(file)) {
1✔
641
            if (signable.getSignatures().isEmpty()) {
1✔
642
                throw new SignerException("No signature found in " + file);
1✔
643
            }
644

645
            Timestamper timestamper = Timestamper.create(tsmode != null ? TimestampingMode.of(tsmode) : TimestampingMode.AUTHENTICODE);
1✔
646
            timestamper.setRetries(tsretries);
1✔
647
            timestamper.setRetryWait(tsretrywait);
1✔
648
            if (tsaurl != null) {
1✔
649
                timestamper.setURLs(tsaurl.split(","));
1✔
650
            }
651
            DigestAlgorithm digestAlgorithm = alg != null ? DigestAlgorithm.of(alg) : DigestAlgorithm.getDefault();
1✔
652

653
            List<CMSSignedData> signatures = new ArrayList<>();
1✔
654
            for (CMSSignedData signature : signable.getSignatures()) {
1✔
655
                SignerInformation signerInformation = signature.getSignerInfos().iterator().next();
1✔
656
                SignerId signerId = signerInformation.getSID();
1✔
657
                X509CertificateHolder certificate = (X509CertificateHolder) signature.getCertificates().getMatches(signerId).iterator().next();
1✔
658

659
                String digestAlgorithmName = new DefaultAlgorithmNameFinder().getAlgorithmName(signerInformation.getDigestAlgorithmID()); 
1✔
660
                String keyAlgorithmName = new DefaultAlgorithmNameFinder().getAlgorithmName(new ASN1ObjectIdentifier(signerInformation.getEncryptionAlgOID()));
1✔
661
                String name = digestAlgorithmName + "/" + keyAlgorithmName + " signature from '" + certificate.getSubject() + "'";
1✔
662

663
                if (SignatureUtils.isTimestamped(signature) && !replace) {
1✔
664
                    log.fine(name + " already timestamped");
1✔
665
                    signatures.add(signature);
1✔
666
                    continue;
1✔
667
                }
668

669
                boolean expired = certificate.getNotAfter().before(new Date());
1✔
670
                if (expired) {
1✔
671
                    log.fine(name + " is expired, skipping");
×
672
                    signatures.add(signature);
×
673
                    continue;
×
674
                }
675

676
                log.info("Adding timestamp to " + name);
1✔
677
                signature = SignatureUtils.removeTimestamp(signature);
1✔
678
                signature = timestamper.timestamp(digestAlgorithm, signature);
1✔
679

680
                signatures.add(signature);
1✔
681
            }
1✔
682

683
            signable.setSignatures(signatures);
1✔
684
            signable.save();
1✔
685
        } catch (IOException | CMSException e) {
×
686
            throw new SignerException("Couldn't timestamp " + file, e);
×
687
        }
1✔
688
    }
1✔
689
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc