aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identityprovider/client/IdentityDocumentSigner.java
blob: bfc1b3aad46971bf689d5a78cdb697773b9458c2 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.identityprovider.client;

import com.yahoo.security.SignatureUtils;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.athenz.identityprovider.api.ClusterType;
import com.yahoo.vespa.athenz.identityprovider.api.IdentityType;
import com.yahoo.vespa.athenz.identityprovider.api.SignedIdentityDocument;
import com.yahoo.vespa.athenz.identityprovider.api.VespaUniqueInstanceId;

import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.time.Instant;
import java.util.Base64;
import java.util.Set;
import java.util.TreeSet;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * Generates and validates the signature for a {@link SignedIdentityDocument}
 *
 * @author bjorncs
 */
public class IdentityDocumentSigner {

    public String generateSignature(VespaUniqueInstanceId providerUniqueId,
                                    AthenzService providerService,
                                    String configServerHostname,
                                    String instanceHostname,
                                    Instant createdAt,
                                    Set<String> ipAddresses,
                                    IdentityType identityType,
                                    ClusterType clusterType,
                                    PrivateKey privateKey) {
        try {
            Signature signer = SignatureUtils.createSigner(privateKey);
            signer.initSign(privateKey);
            writeToSigner(
                    signer, providerUniqueId, providerService, configServerHostname, instanceHostname, createdAt,
                    ipAddresses, identityType, clusterType);
            byte[] signature = signer.sign();
            return Base64.getEncoder().encodeToString(signature);
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean hasValidSignature(SignedIdentityDocument doc, PublicKey publicKey) {
        try {
            Signature signer = SignatureUtils.createVerifier(publicKey);
            signer.initVerify(publicKey);
            writeToSigner(
                    signer, doc.providerUniqueId(), doc.providerService(), doc.configServerHostname(),
                    doc.instanceHostname(), doc.createdAt(), doc.ipAddresses(), doc.identityType(), doc.clusterType());
            return signer.verify(Base64.getDecoder().decode(doc.signature()));
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }
    }

    private static void writeToSigner(Signature signer,
                                      VespaUniqueInstanceId providerUniqueId,
                                      AthenzService providerService,
                                      String configServerHostname,
                                      String instanceHostname,
                                      Instant createdAt,
                                      Set<String> ipAddresses,
                                      IdentityType identityType,
                                      ClusterType clusterType) throws SignatureException {
        signer.update(providerUniqueId.asDottedString().getBytes(UTF_8));
        signer.update(providerService.getFullName().getBytes(UTF_8));
        signer.update(configServerHostname.getBytes(UTF_8));
        signer.update(instanceHostname.getBytes(UTF_8));
        ByteBuffer timestampAsBuffer = ByteBuffer.allocate(Long.BYTES);
        timestampAsBuffer.putLong(createdAt.toEpochMilli());
        signer.update(timestampAsBuffer.array());
        for (String ipAddress : new TreeSet<>(ipAddresses)) {
            signer.update(ipAddress.getBytes(UTF_8));
        }
        signer.update(identityType.id().getBytes(UTF_8));
        if (clusterType != null) signer.update(clusterType.toConfigValue().getBytes(UTF_8));
    }
}