aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/test/KeyAgreementTestable.java
blob: de9356bb6e6613a73d9ca06e79941ae8cd7f4ad7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cz.crcs.ectester.standalone.test;

import cz.crcs.ectester.common.test.BaseTestable;
import cz.crcs.ectester.common.test.TestException;

import javax.crypto.KeyAgreement;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.ECParameterSpec;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class KeyAgreementTestable extends BaseTestable {
    private KeyAgreement ka;
    private ECPrivateKey privateKey;
    private ECPublicKey publicKey;
    private KeyGeneratorTestable kgtPrivate;
    private KeyGeneratorTestable kgtPublic;
    private AlgorithmParameterSpec spec;
    private byte[] secret;

    public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey) {
        this.ka = ka;
        this.privateKey = privateKey;
        this.publicKey = publicKey;
    }

    public KeyAgreementTestable(KeyAgreement ka, ECPrivateKey privateKey, ECPublicKey publicKey, ECParameterSpec spec) {
        this(ka, privateKey, publicKey);
        this.spec = spec;
    }

    public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable kgt, ECPrivateKey privateKey, ECParameterSpec spec) {
        this(ka, privateKey, null, spec);
        this.kgtPublic = kgt;
    }

    public KeyAgreementTestable(KeyAgreement ka, ECPublicKey publicKey, KeyGeneratorTestable kgt, ECParameterSpec spec) {
        this(ka, null, publicKey, spec);
        this.kgtPrivate = kgt;
    }

    public KeyAgreementTestable(KeyAgreement ka, KeyGeneratorTestable privKgt, KeyGeneratorTestable pubKgt, ECParameterSpec spec) {
        this(ka, (ECPrivateKey) null, null, spec);
        this.kgtPrivate = privKgt;
        this.kgtPublic = pubKgt;
    }

    public KeyAgreement getKa() {
        return ka;
    }

    public ECPublicKey getPublicKey() {
        return publicKey;
    }

    public ECPrivateKey getPrivateKey() {
        return privateKey;
    }

    public byte[] getSecret() {
        if (!hasRun) {
            return null;
        }
        return secret;
    }

    @Override
    public void run() throws TestException {
        if (kgtPrivate != null) {
            privateKey = (ECPrivateKey) kgtPrivate.getKeyPair().getPrivate();
        }

        if (kgtPublic != null) {
            publicKey = (ECPublicKey) kgtPublic.getKeyPair().getPublic();
        }

        try {
            if (spec != null) {
                ka.init(privateKey, spec);
            } else {
                ka.init(privateKey);
            }
        } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
            ok = false;
            error = true;
            hasRun = true;
            return;
        }

        try {
            ka.doPhase(publicKey, true);
        } catch (IllegalStateException e) {
            ok = false;
            hasRun = true;
            return;
        } catch (InvalidKeyException e) {
            ok = false;
            error = true;
            hasRun = true;
            return;
        }

        try {
            secret = ka.generateSecret();
        } catch (IllegalStateException isex) {
            ok = false;
            hasRun = true;
            return;
        } catch (UnsupportedOperationException uoe) {
            ok = false;
            error = true;
            hasRun = false;
            return;
        }

        ok = true;
        hasRun = true;
    }
}