aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/standalone/output/XMLTestWriter.java
blob: 63838cb06d334fce6a7a2d0b3ab0b99057ce14df (plain) (blame)
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
125
126
127
128
129
package cz.crcs.ectester.standalone.output;

import cz.crcs.ectester.common.output.BaseXMLTestWriter;
import cz.crcs.ectester.common.test.TestSuite;
import cz.crcs.ectester.common.test.Testable;
import cz.crcs.ectester.common.util.ByteUtil;
import cz.crcs.ectester.standalone.test.KeyAgreementTestable;
import cz.crcs.ectester.standalone.test.KeyGeneratorTestable;
import cz.crcs.ectester.standalone.test.SignatureTestable;
import cz.crcs.ectester.standalone.test.StandaloneTestSuite;
import org.w3c.dom.Element;

import javax.xml.parsers.ParserConfigurationException;
import java.io.OutputStream;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class XMLTestWriter extends BaseXMLTestWriter {

    public XMLTestWriter(OutputStream output) throws ParserConfigurationException {
        super(output);
    }

    private Element pkeyElement(PublicKey pkey) {
        Element pubkey = doc.createElement("pubkey");
        if (pkey == null) {
            return pubkey;
        }
        pubkey.setAttribute("algorithm", pkey.getAlgorithm());
        pubkey.setAttribute("format", pkey.getFormat());
        pubkey.setTextContent(ByteUtil.bytesToHex(pkey.getEncoded()));
        return pubkey;
    }

    private Element skeyElement(PrivateKey skey) {
        Element privkey = doc.createElement("privkey");
        if (skey == null) {
            return privkey;
        }
        privkey.setAttribute("algorithm", skey.getAlgorithm());
        privkey.setAttribute("format", skey.getFormat());
        privkey.setTextContent(ByteUtil.bytesToHex(skey.getEncoded()));
        return privkey;
    }

    private Element kaElement(KeyAgreementTestable kat) {
        Element katElem = doc.createElement("key-agreement");
        katElem.setAttribute("algo", kat.getKa().getAlgorithm());

        Element secret = doc.createElement("secret");
        secret.setTextContent(ByteUtil.bytesToHex(kat.getSecret()));
        katElem.appendChild(secret);

        PublicKey pkey = kat.getPublicKey();
        Element pubkey = pkeyElement(pkey);
        katElem.appendChild(pubkey);

        PrivateKey skey = kat.getPrivateKey();
        Element privkey = skeyElement(skey);
        katElem.appendChild(privkey);

        return katElem;
    }

    private Element kgtElement(KeyGeneratorTestable kgt) {
        Element kgtElem = doc.createElement("key-pair-generator");
        kgtElem.setAttribute("algo", kgt.getKpg().getAlgorithm());

        Element keyPair = doc.createElement("key-pair");
        if (kgt.getKeyPair() != null) {
            PublicKey pkey = kgt.getKeyPair().getPublic();
            Element pubkey = pkeyElement(pkey);
            keyPair.appendChild(pubkey);

            PrivateKey skey = kgt.getKeyPair().getPrivate();
            Element privkey = skeyElement(skey);
            keyPair.appendChild(privkey);
        }

        kgtElem.appendChild(keyPair);
        return kgtElem;
    }

    private Element sigElement(SignatureTestable sig) {
        Element sigElem = doc.createElement("signature");
        sigElem.setAttribute("verified", sig.getVerified() ? "true" : "false");
        sigElem.setAttribute("algo", sig.getSig().getAlgorithm());

        Element raw = doc.createElement("raw");
        raw.setTextContent(ByteUtil.bytesToHex(sig.getSignature()));
        sigElem.appendChild(raw);

        return sigElem;
    }

    @Override
    protected Element testableElement(Testable t) {
        Element result = doc.createElement("test");
        if (t instanceof KeyGeneratorTestable) {
            result.setAttribute("type", "key-pair-generator");
            result.appendChild(kgtElement((KeyGeneratorTestable) t));
        } else if (t instanceof KeyAgreementTestable) {
            result.setAttribute("type", "key-agreement");
            result.appendChild(kaElement((KeyAgreementTestable) t));
        } else if (t instanceof SignatureTestable) {
            result.setAttribute("type", "signature");
            result.appendChild(sigElement((SignatureTestable) t));
        }
        return result;
    }

    @Override
    protected Element deviceElement(TestSuite suite) {
        if (suite instanceof StandaloneTestSuite) {
            StandaloneTestSuite standaloneSuite = (StandaloneTestSuite) suite;
            Element result = doc.createElement("device");
            result.setAttribute("type", "library");

            Element name = doc.createElement("name");
            name.setTextContent(standaloneSuite.getLibrary().name());
            result.appendChild(name);
            return result;
        }
        return null;
    }
}