code: private String getCertCN(X509Certificate x509)
          throws CertificateParsingException {
  
          X500Principal principal = x509.getSubjectX500Principal();
          String subjectName = principal.getName();
  
          String[] fields = subjectName.split(",");
          for (String field : fields) {
              if (field.startsWith("CN=")) {
                  String serverName = field.substring(3);
                  return serverName.toLowerCase();
              }
          }
  
          throw new CertificateParsingException("Certificate CN not found");
      }
docs: Read the CN out of the cert