// 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 javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import java.security.cert.X509Certificate;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A {@link HostnameVerifier} 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 HostnameVerifier {

    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];
            AthenzIdentity certificateIdentity = AthenzUtils.createAthenzIdentity(cert);
            return allowedIdentities.contains(certificateIdentity);
        } catch (SSLPeerUnverifiedException e) {
            log.log(Level.WARNING, "Unverified client: " + hostname);
            return false;
        }
    }

}


@@ -190,38 +190,128 @@
 ort 
-javax.net.ssl.HostnameVerifier
+org.apache.http.conn.ssl.X509HostnameVerifier;%0A%0Aimport javax.net.ssl.HostnameVerifier;%0Aimport javax.net.ssl.SSLException
 ;%0Aim
@@ -386,24 +386,56 @@
 SSLSession;%0A
+import javax.net.ssl.SSLSocket;%0A
 import java.
@@ -586,16 +586,47 @@
 erifier%7D
+ / %7B@link X509HostnameVerifier%7D
  that va
@@ -632,16 +632,19 @@
 alidates
+%0A *
  Athenz 
@@ -823,16 +823,20 @@
 lements 
+X509
 Hostname
@@ -1332,45 +1332,25 @@
     
-AthenzIdentity certificateIdentity = 
+return isTrusted(
 Athe
@@ -1387,75 +1387,8 @@
 ert)
-;%0A            return allowedIdentities.contains(certificateIdentity
 );%0A 
@@ -1548,11 +1548,906 @@
 %0A    %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

Implement Apache Http verifier interface in AthenzIdentityVerifier