JavaCryptoTools.java

1
package org.europa.together.application;
2
3
import java.io.FileOutputStream;
4
import java.io.OutputStream;
5
import java.io.Serializable;
6
import java.nio.file.Files;
7
import java.nio.file.Path;
8
import java.nio.file.Paths;
9
import java.security.KeyFactory;
10
import java.security.KeyPair;
11
import java.security.KeyPairGenerator;
12
import java.security.MessageDigest;
13
import java.security.NoSuchAlgorithmException;
14
import java.security.PrivateKey;
15
import java.security.PublicKey;
16
import java.security.SecureRandom;
17
import java.security.Security;
18
import java.security.spec.PKCS8EncodedKeySpec;
19
import java.security.spec.X509EncodedKeySpec;
20
import org.europa.together.business.CryptoTools;
21
import org.europa.together.business.Logger;
22
import org.europa.together.domain.CipherAlgorithm;
23
import org.europa.together.domain.HashAlgorithm;
24
import org.europa.together.domain.LogLevel;
25
import org.europa.together.domain.RNGAlgorithm;
26
import org.europa.together.utils.Constraints;
27
import org.europa.together.utils.StringUtils;
28
import org.springframework.stereotype.Repository;
29
30
/**
31
 * Implementation of Java cryptography.
32
 */
33
@Repository
34
public class JavaCryptoTools implements CryptoTools, Serializable {
35
36
    private static final long serialVersionUID = 14L;
37
    private static final Logger LOGGER = new LogbackLogger(JavaCryptoTools.class);
38
39
    /**
40
     * Constructor.
41
     */
42
    public JavaCryptoTools() {
43 1 1. <init> : removed call to java/security/Security::setProperty → SURVIVED
        Security.setProperty("crypto.policy", "unlimited");
44
        LOGGER.log("instance class", LogLevel.INFO);
45
    }
46
47
    @Override
48
    public String calculateHash(final String plainText,
49
            final HashAlgorithm algorithm) {
50
        String hash = null;
51
        try {
52
            String hashAlgorithm = algorithm.getValue();
53
54
            MessageDigest md = MessageDigest.getInstance(hashAlgorithm);
55 1 1. calculateHash : removed call to java/security/MessageDigest::reset → SURVIVED
            md.reset();
56
            hash = StringUtils.byteToString(md.digest(plainText.getBytes("UTF-8")));
57
            String msg = "Utils.calculateHash(" + algorithm.toString() + ")"
58
                    + " plaintext: " + plainText + " hash: " + hash;
59
60
            LOGGER.log(msg, LogLevel.DEBUG);
61
        } catch (Exception ex) {
62
            LOGGER.catchException(ex);
63
64
        }
65 1 1. calculateHash : replaced return value with "" for org/europa/together/application/JavaCryptoTools::calculateHash → KILLED
        return hash;
66
    }
67
68
    @Override
69
    public int getMaxKeySize(final CipherAlgorithm cipher)
70
            throws NoSuchAlgorithmException {
71 1 1. getMaxKeySize : replaced int return with 0 for org/europa/together/application/JavaCryptoTools::getMaxKeySize → KILLED
        return javax.crypto.Cipher.getMaxAllowedKeyLength(cipher.toString());
72
    }
73
74
    @Override
75
    public KeyPair generateCipherKeyPair(final CipherAlgorithm cipher) {
76
        KeyPair pair = null;
77
        try {
78
            int lenght = Constraints.INT_4096;
79
            KeyPairGenerator keyring = KeyPairGenerator.getInstance(cipher.toString());
80 1 1. generateCipherKeyPair : removed call to java/security/KeyPairGenerator::initialize → SURVIVED
            keyring.initialize(lenght, new SecureRandom());
81
            pair = keyring.generateKeyPair();
82
        } catch (Exception ex) {
83
            LOGGER.catchException(ex);
84
        }
85 1 1. generateCipherKeyPair : replaced return value with null for org/europa/together/application/JavaCryptoTools::generateCipherKeyPair → KILLED
        return pair;
86
    }
87
88
    @Override
89
    public void saveKeyPairToFile(final String path, final KeyPair keyRing) {
90
        //FALLBACK
91
        String destination = Constraints.SYSTEM_USER_HOME_DIR;
92 1 1. saveKeyPairToFile : negated conditional → SURVIVED
        if (!StringUtils.isEmpty(path)) {
93
            destination = path;
94
        }
95
        OutputStream privateFile = null;
96
        OutputStream publicFile = null;
97
        try {
98
            byte[] publicKey = keyRing.getPublic().getEncoded();
99
            byte[] privateKey = keyRing.getPrivate().getEncoded();
100
            String privateCipher = keyRing.getPrivate().getAlgorithm();
101
            privateFile = new FileOutputStream(destination + "/"
102
                    + privateCipher + ".key");
103 1 1. saveKeyPairToFile : removed call to java/io/OutputStream::write → SURVIVED
            privateFile.write(privateKey);
104 1 1. saveKeyPairToFile : removed call to java/io/OutputStream::close → SURVIVED
            privateFile.close();
105
            LOGGER.log("Private Key stored in PKCS#8 format.", LogLevel.DEBUG);
106
            String publicCipher = keyRing.getPublic().getAlgorithm();
107
            publicFile = new FileOutputStream(destination + "/"
108
                    + publicCipher + ".pub");
109 1 1. saveKeyPairToFile : removed call to java/io/OutputStream::write → SURVIVED
            publicFile.write(publicKey);
110 1 1. saveKeyPairToFile : removed call to java/io/OutputStream::close → SURVIVED
            publicFile.close();
111
            LOGGER.log("Public Kex stored in X.509 format.", LogLevel.DEBUG);
112
        } catch (Exception ex) {
113
            LOGGER.catchException(ex);
114
        }
115
    }
116
117
    @Override
118
    public PrivateKey loadPrivateKeyFile(final String keyFile, final CipherAlgorithm algorithm) {
119
        PrivateKey key = null;
120
        try {
121
            Path path = Paths.get(keyFile);
122
            byte[] bytes = Files.readAllBytes(path);
123
            PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(bytes);
124
            KeyFactory kf = KeyFactory.getInstance(algorithm.toString());
125
            key = kf.generatePrivate(ks);
126
        } catch (Exception ex) {
127
            LOGGER.catchException(ex);
128
        }
129 1 1. loadPrivateKeyFile : replaced return value with null for org/europa/together/application/JavaCryptoTools::loadPrivateKeyFile → KILLED
        return key;
130
    }
131
132
    @Override
133
    public PublicKey loadPublicKeyFile(final String keyFile, final CipherAlgorithm algorithm) {
134
        PublicKey key = null;
135
        try {
136
            Path path = Paths.get(keyFile);
137
            byte[] bytes = Files.readAllBytes(path);
138
            X509EncodedKeySpec ks = new X509EncodedKeySpec(bytes);
139
            KeyFactory kf = KeyFactory.getInstance(algorithm.toString());
140
            key = kf.generatePublic(ks);
141
        } catch (Exception ex) {
142
            LOGGER.catchException(ex);
143
        }
144 1 1. loadPublicKeyFile : replaced return value with null for org/europa/together/application/JavaCryptoTools::loadPublicKeyFile → KILLED
        return key;
145
    }
146
147
    @Override
148
    public double generateRandomNumber(final RNGAlgorithm algortihm) {
149
150
        double random = .0;
151
        try {
152
            SecureRandom genrator
153
                    = SecureRandom.getInstance(algortihm.name());
154
155
            random = genrator.nextDouble();
156
            LOGGER.log("Random Value: " + random, LogLevel.DEBUG);
157
158
        } catch (Exception ex) {
159
            LOGGER.catchException(ex);
160
        }
161 1 1. generateRandomNumber : replaced double return with 0.0d for org/europa/together/application/JavaCryptoTools::generateRandomNumber → KILLED
        return random;
162
    }
163
}

Mutations

43

1.1
Location : <init>
Killed by : none
removed call to java/security/Security::setProperty → SURVIVED
Covering tests

55

1.1
Location : calculateHash
Killed by : none
removed call to java/security/MessageDigest::reset → SURVIVED
Covering tests

65

1.1
Location : calculateHash
Killed by : org.europa.together.application.JavaCryptoToolsTest.[engine:junit-jupiter]/[class:org.europa.together.application.JavaCryptoToolsTest]/[method:calculateMD5Hashes()]
replaced return value with "" for org/europa/together/application/JavaCryptoTools::calculateHash → KILLED

71

1.1
Location : getMaxKeySize
Killed by : org.europa.together.application.JavaCryptoToolsTest.[engine:junit-jupiter]/[class:org.europa.together.application.JavaCryptoToolsTest]/[method:getMaxKeySize()]
replaced int return with 0 for org/europa/together/application/JavaCryptoTools::getMaxKeySize → KILLED

80

1.1
Location : generateCipherKeyPair
Killed by : none
removed call to java/security/KeyPairGenerator::initialize → SURVIVED
Covering tests

85

1.1
Location : generateCipherKeyPair
Killed by : org.europa.together.application.JavaCryptoToolsTest.[engine:junit-jupiter]/[class:org.europa.together.application.JavaCryptoToolsTest]/[method:calculateRsaKeyPair()]
replaced return value with null for org/europa/together/application/JavaCryptoTools::generateCipherKeyPair → KILLED

92

1.1
Location : saveKeyPairToFile
Killed by : none
negated conditional → SURVIVED
Covering tests

103

1.1
Location : saveKeyPairToFile
Killed by : none
removed call to java/io/OutputStream::write → SURVIVED
Covering tests

104

1.1
Location : saveKeyPairToFile
Killed by : none
removed call to java/io/OutputStream::close → SURVIVED
Covering tests

109

1.1
Location : saveKeyPairToFile
Killed by : none
removed call to java/io/OutputStream::write → SURVIVED
Covering tests

110

1.1
Location : saveKeyPairToFile
Killed by : none
removed call to java/io/OutputStream::close → SURVIVED
Covering tests

129

1.1
Location : loadPrivateKeyFile
Killed by : org.europa.together.application.JavaCryptoToolsTest.[engine:junit-jupiter]/[class:org.europa.together.application.JavaCryptoToolsTest]/[method:loadPrivateRsaKeyFromFile()]
replaced return value with null for org/europa/together/application/JavaCryptoTools::loadPrivateKeyFile → KILLED

144

1.1
Location : loadPublicKeyFile
Killed by : org.europa.together.application.JavaCryptoToolsTest.[engine:junit-jupiter]/[class:org.europa.together.application.JavaCryptoToolsTest]/[method:loadPublicKeyRsaFromFile()]
replaced return value with null for org/europa/together/application/JavaCryptoTools::loadPublicKeyFile → KILLED

161

1.1
Location : generateRandomNumber
Killed by : org.europa.together.application.JavaCryptoToolsTest.[engine:junit-jupiter]/[class:org.europa.together.application.JavaCryptoToolsTest]/[method:generateRandomDoubleValue()]
replaced double return with 0.0d for org/europa/together/application/JavaCryptoTools::generateRandomNumber → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.0