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

pgpainless / pgpainless / #1048

09 Oct 2023 10:02AM CUT coverage: 89.108% (+0.04%) from 89.073%
#1048

push

github-actions

vanitasvitae
Allow generation of keys with empty key flags.

Forbid certification of thirdparty certificates if CERTIFY_OTHERS flag is missing

10 of 10 new or added lines in 5 files covered. (100.0%)

7085 of 7951 relevant lines covered (89.11%)

0.89 hits per line

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

96.55
/pgpainless-core/src/main/java/org/pgpainless/key/generation/KeySpecBuilder.java
1
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
2
//
3
// SPDX-License-Identifier: Apache-2.0
4

5
package org.pgpainless.key.generation;
6

7
import java.util.Arrays;
8
import java.util.Date;
9
import java.util.LinkedHashSet;
10
import java.util.Set;
11
import javax.annotation.Nonnull;
12

13
import org.pgpainless.PGPainless;
14
import org.pgpainless.algorithm.AlgorithmSuite;
15
import org.pgpainless.algorithm.CompressionAlgorithm;
16
import org.pgpainless.algorithm.Feature;
17
import org.pgpainless.algorithm.HashAlgorithm;
18
import org.pgpainless.algorithm.KeyFlag;
19
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
20
import org.pgpainless.key.generation.type.KeyType;
21
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets;
22
import org.pgpainless.signature.subpackets.SignatureSubpackets;
23
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil;
24

25
public class KeySpecBuilder implements KeySpecBuilderInterface {
26

27
    private final KeyType type;
28
    private final KeyFlag[] keyFlags;
29
    private final SelfSignatureSubpackets hashedSubpackets = new SignatureSubpackets();
1✔
30
    private final AlgorithmSuite algorithmSuite = PGPainless.getPolicy().getKeyGenerationAlgorithmSuite();
1✔
31
    private Set<CompressionAlgorithm> preferredCompressionAlgorithms = algorithmSuite.getCompressionAlgorithms();
1✔
32
    private Set<HashAlgorithm> preferredHashAlgorithms = algorithmSuite.getHashAlgorithms();
1✔
33
    private Set<SymmetricKeyAlgorithm> preferredSymmetricAlgorithms = algorithmSuite.getSymmetricKeyAlgorithms();
1✔
34
    private Date keyCreationDate;
35

36
    KeySpecBuilder(@Nonnull KeyType type, KeyFlag... flags) {
1✔
37
        if (flags == null) {
1✔
38
            this.keyFlags = new KeyFlag[0];
×
39
        } else {
40
            SignatureSubpacketsUtil.assureKeyCanCarryFlags(type, flags);
1✔
41
            this.keyFlags = flags;
1✔
42
        }
43
        this.type = type;
1✔
44
    }
1✔
45

46
    @Override
47
    public KeySpecBuilder overridePreferredCompressionAlgorithms(
48
            @Nonnull CompressionAlgorithm... compressionAlgorithms) {
49
        this.preferredCompressionAlgorithms = new LinkedHashSet<>(Arrays.asList(compressionAlgorithms));
1✔
50
        return this;
1✔
51
    }
52

53
    @Override
54
    public KeySpecBuilder overridePreferredHashAlgorithms(
55
            @Nonnull HashAlgorithm... preferredHashAlgorithms) {
56
        this.preferredHashAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredHashAlgorithms));
1✔
57
        return this;
1✔
58
    }
59

60
    @Override
61
    public KeySpecBuilder overridePreferredSymmetricKeyAlgorithms(
62
            @Nonnull SymmetricKeyAlgorithm... preferredSymmetricKeyAlgorithms) {
63
        for (SymmetricKeyAlgorithm algo : preferredSymmetricKeyAlgorithms) {
1✔
64
            if (algo == SymmetricKeyAlgorithm.NULL) {
1✔
65
                throw new IllegalArgumentException("NULL (unencrypted) is an invalid symmetric key algorithm preference.");
1✔
66
            }
67
        }
68
        this.preferredSymmetricAlgorithms = new LinkedHashSet<>(Arrays.asList(preferredSymmetricKeyAlgorithms));
1✔
69
        return this;
1✔
70
    }
71

72
    @Override
73
    public KeySpecBuilder setKeyCreationDate(@Nonnull Date creationDate) {
74
        this.keyCreationDate = creationDate;
1✔
75
        return this;
1✔
76
    }
77

78
    @Override
79
    public KeySpec build() {
80
        this.hashedSubpackets.setKeyFlags(keyFlags);
1✔
81
        this.hashedSubpackets.setPreferredCompressionAlgorithms(preferredCompressionAlgorithms);
1✔
82
        this.hashedSubpackets.setPreferredHashAlgorithms(preferredHashAlgorithms);
1✔
83
        this.hashedSubpackets.setPreferredSymmetricKeyAlgorithms(preferredSymmetricAlgorithms);
1✔
84
        this.hashedSubpackets.setFeatures(Feature.MODIFICATION_DETECTION);
1✔
85

86
        return new KeySpec(type, (SignatureSubpackets) hashedSubpackets, false, keyCreationDate);
1✔
87
    }
88
}
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

© 2025 Coveralls, Inc