// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.athenz;

import org.apache.http.conn.ssl.X509HostnameVerifier;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import java.security.cert.X509Certificate;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A {@link HostnameVerifier} / {@link X509HostnameVerifier} that validates
 * Athenz x509 certificates using the identity in the Common Name attribute.
 *
 * @author bjorncs
 */
// TODO Move to dedicated Athenz bundle
public class AthenzIdentityVerifier implements X509HostnameVerifier {

    private static final Logger log = Logger.getLogger(AthenzIdentityVerifier.class.getName());

    private final Set<AthenzIdentity> allowedIdentities;

    public AthenzIdentityVerifier(Set<AthenzIdentity> allowedIdentities) {
        this.allowedIdentities = allowedIdentities;
    }

    @Override
    public boolean verify(String hostname, SSLSession session) {
        try {
            X509Certificate cert = (X509Certificate) session.getPeerCertificates()[0];
            return isTrusted(AthenzUtils.createAthenzIdentity(cert));
        } catch (SSLPeerUnverifiedException e) {
            log.log(Level.WARNING, "Unverified client: " + hostname);
            return false;
        }
    }

    @Override
    public void verify(String host, SSLSocket ssl) {
        // all sockets allowed
    }

    @Override
    public void verify(String hostname, X509Certificate certificate) throws SSLException {
        AthenzIdentity identity = AthenzUtils.createAthenzIdentity(certificate);
        if (!isTrusted(identity)) {
            throw new SSLException("Athenz identity is not trusted: " + identity.getFullName());
        }
    }

    @Override
    public void verify(String hostname, String[] cns, String[] subjectAlts) throws SSLException {
        AthenzIdentity identity = AthenzUtils.createAthenzIdentity(cns[0]);
        if (!isTrusted(identity)) {
            throw new SSLException("Athenz identity is not trusted: " + identity.getFullName());
        }
    }

    private boolean isTrusted(AthenzIdentity identity) {
        return allowedIdentities.contains(identity);
    }
}


@@ -190,128 +190,38 @@
 ort 
-org.apache.http.conn.ssl.X509HostnameVerifier;%0A%0Aimport javax.net.ssl.HostnameVerifier;%0Aimport javax.net.ssl.SSLException
+javax.net.ssl.HostnameVerifier
 ;%0Aim
@@ -304,40 +304,8 @@
 on;%0A
-import javax.net.ssl.SSLSocket;%0A
 impo
@@ -468,39 +468,8 @@
 ier%7D
- / %7B@link X509HostnameVerifier%7D
  tha
@@ -479,19 +479,16 @@
 alidates
-%0A *
  Athenz 
@@ -667,20 +667,16 @@
 lements 
-X509
 Hostname
@@ -1172,25 +1172,45 @@
     
-return isTrusted(
+AthenzIdentity certificateIdentity = 
 Athe
@@ -1243,16 +1243,83 @@
 ty(cert)
+;%0A            return allowedIdentities.contains(certificateIdentity
 );%0A     
@@ -1479,902 +1479,7 @@
  %7D%0A%0A
-    @Override%0A    public void verify(String host, SSLSocket ssl) %7B%0A        // all sockets allowed%0A    %7D%0A%0A    @Override%0A    public void verify(String hostname, X509Certificate certificate) throws SSLException %7B%0A        AthenzIdentity identity = AthenzUtils.createAthenzIdentity(certificate);%0A        if (!isTrusted(identity)) %7B%0A            throw new SSLException(%22Athenz identity is not trusted: %22 + identity.getFullName());%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void verify(String hostname, String%5B%5D cns, String%5B%5D subjectAlts) throws SSLException %7B%0A        AthenzIdentity identity = AthenzUtils.createAthenzIdentity(cns%5B0%5D);%0A        if (!isTrusted(identity)) %7B%0A            throw new SSLException(%22Athenz identity is not trusted: %22 + identity.getFullName());%0A        %7D%0A    %7D%0A%0A    private boolean isTrusted(AthenzIdentity identity) %7B%0A        return allowedIdentities.contains(identity);%0A    %7D%0A
 %7D%0A%0A

Revert "Implement Apache Http verifier interface in AthenzIdentityVerifier"