Java Code Examples for java.security.Principal#equals()

The following examples show how to use java.security.Principal#equals() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Xpatch   File: V1SchemeVerifier.java    License: Apache License 2.0 6 votes vote down vote up
public static List<X509Certificate> getCertificateChain(
        List<X509Certificate> certs, X509Certificate leaf) {
    List<X509Certificate> unusedCerts = new ArrayList<>(certs);
    List<X509Certificate> result = new ArrayList<>(1);
    result.add(leaf);
    unusedCerts.remove(leaf);
    X509Certificate root = leaf;
    while (!root.getSubjectDN().equals(root.getIssuerDN())) {
        Principal targetDn = root.getIssuerDN();
        boolean issuerFound = false;
        for (int i = 0; i < unusedCerts.size(); i++) {
            X509Certificate unusedCert = unusedCerts.get(i);
            if (targetDn.equals(unusedCert.getSubjectDN())) {
                issuerFound = true;
                unusedCerts.remove(i);
                result.add(unusedCert);
                root = unusedCert;
                break;
            }
        }
        if (!issuerFound) {
            break;
        }
    }
    return result;
}
 
Example 2
Source Project: qpid-broker-j   File: GenericPrincipal.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean principalsEqual(final Principal p1, final Principal p2)
{
    if (p1 == null)
    {
        return p2 == null;
    }
    else if (p2 == null)
    {
        return false;
    }

    if (p1 instanceof GenericPrincipal)
    {
        return genericPrincipalEquals((GenericPrincipal) p1, p2);
    }
    if (p2 instanceof GenericPrincipal)
    {
        return genericPrincipalEquals((GenericPrincipal) p2, p1);
    }

    return p1.equals(p2);
}
 
Example 3
private X509Certificate findSignedCert(X509Certificate signingCert, List<X509Certificate> certificates) {
    X509Certificate signed = null;
    Iterator iterator = certificates.iterator();

    while (iterator.hasNext()) {
        X509Certificate cert = (X509Certificate) iterator.next();
        Principal signingCertSubjectDN = signingCert.getSubjectDN();
        Principal certIssuerDN = cert.getIssuerDN();
        if (certIssuerDN.equals(signingCertSubjectDN) && !cert.equals(signingCert)) {
            signed = cert;
            break;
        }
    }

    return signed;
}
 
Example 4
private X509Certificate findSigner(X509Certificate signedCert, List<X509Certificate> certificates) {
    X509Certificate signer = null;
    Iterator iterator = certificates.iterator();

    while (iterator.hasNext()) {
        X509Certificate cert = (X509Certificate) iterator.next();
        Principal certSubjectDN = cert.getSubjectDN();
        Principal issuerDN = signedCert.getIssuerDN();
        if (certSubjectDN.equals(issuerDN)) {
            signer = cert;
            break;
        }
    }

    return signer;
}
 
Example 5
Source Project: alpha-wallet-android   File: XMLDSigVerifier.java    License: MIT License 5 votes vote down vote up
private X509Certificate findSignedCert(X509Certificate signingCert, List<X509Certificate> certificates)
{
    X509Certificate signed = null;
    for (X509Certificate cert : certificates)
    {
        Principal signingCertSubjectDN = signingCert.getSubjectDN();
        Principal certIssuerDN = cert.getIssuerDN();
        if (certIssuerDN.equals(signingCertSubjectDN) && !cert.equals(signingCert))
        {
            signed = cert;
            break;
        }
    }
    return signed;
}
 
Example 6
Source Project: alpha-wallet-android   File: XMLDSigVerifier.java    License: MIT License 5 votes vote down vote up
private X509Certificate findSignerCertificate(X509Certificate signedCert, List<X509Certificate> certificates) {
    X509Certificate signer = null;
    for (X509Certificate cert : certificates) {
        Principal certSubjectDN = cert.getSubjectDN();
        Principal issuerDN = signedCert.getIssuerDN();
        if (certSubjectDN.equals(issuerDN)) {
            signer = cert;
            break;
        }
    }
    return signer;
}
 
Example 7
Source Project: hello-pinnedcerts   File: CustomTrustManager.java    License: MIT License 5 votes vote down vote up
/**
 * A helper method for certificate re-ordering.
 * Finds the first certificate in the list of certificates that is signed by the sigingCert.
 */
private X509Certificate findSignedCert(X509Certificate signingCert, List<X509Certificate> certificates) {
  X509Certificate signed = null;

  for(X509Certificate cert : certificates) {
    Principal signingCertSubjectDN = signingCert.getSubjectDN();
    Principal certIssuerDN = cert.getIssuerDN();
    if(certIssuerDN.equals(signingCertSubjectDN) && !cert.equals(signingCert)) {
      signed = cert;
      break;
    }
  }

  return signed;
}
 
Example 8
Source Project: hello-pinnedcerts   File: CustomTrustManager.java    License: MIT License 5 votes vote down vote up
/**
 * A helper method for certificate re-ordering.
 * Finds the certificate in the list of certificates that signed the signedCert.
 */
private X509Certificate findSigner(X509Certificate signedCert, List<X509Certificate> certificates) {
  X509Certificate signer = null;

  for(X509Certificate cert : certificates) {
    Principal certSubjectDN = cert.getSubjectDN();
    Principal issuerDN = signedCert.getIssuerDN();
    if(certSubjectDN.equals(issuerDN)) {
      signer = cert;
      break;
    }
  }

  return signer;
}
 
Example 9
Source Project: android-ssl   File: CustomTrustManager.java    License: MIT License 5 votes vote down vote up
/**
 * A helper method for certificate re-ordering.
 * Finds the first certificate in the list of certificates that is signed by the sigingCert.
 */
private X509Certificate findSignedCert(X509Certificate signingCert, List<X509Certificate> certificates) {
    X509Certificate signed = null;

    for(X509Certificate cert : certificates) {
        Principal signingCertSubjectDN = signingCert.getSubjectDN();
        Principal certIssuerDN = cert.getIssuerDN();
        if(certIssuerDN.equals(signingCertSubjectDN) && !cert.equals(signingCert)) {
            signed = cert;
            break;
        }
    }

    return signed;
}
 
Example 10
Source Project: android-ssl   File: CustomTrustManager.java    License: MIT License 5 votes vote down vote up
/**
 * A helper method for certificate re-ordering.
 * Finds the certificate in the list of certificates that signed the signedCert.
 */
private X509Certificate findSigner(X509Certificate signedCert, List<X509Certificate> certificates) {
    X509Certificate signer = null;

    for(X509Certificate cert : certificates) {
        Principal certSubjectDN = cert.getSubjectDN();
        Principal issuerDN = signedCert.getIssuerDN();
        if(certSubjectDN.equals(issuerDN)) {
            signer = cert;
            break;
        }
    }

    return signer;
}
 
Example 11
public ArrayList<X509Certificate> getCertificateChain(PKCS7 block)
    throws IOException
{
    X509Certificate userCert;
    userCert = block.getCertificate(certificateSerialNumber, issuerName);
    if (userCert == null)
        return null;

    ArrayList<X509Certificate> certList = new ArrayList<>();
    certList.add(userCert);

    X509Certificate[] pkcsCerts = block.getCertificates();
    if (pkcsCerts == null
        || userCert.getSubjectDN().equals(userCert.getIssuerDN())) {
        return certList;
    }

    Principal issuer = userCert.getIssuerDN();
    int start = 0;
    while (true) {
        boolean match = false;
        int i = start;
        while (i < pkcsCerts.length) {
            if (issuer.equals(pkcsCerts[i].getSubjectDN())) {
                // next cert in chain found
                certList.add(pkcsCerts[i]);
                // if selected cert is self-signed, we're done
                // constructing the chain
                if (pkcsCerts[i].getSubjectDN().equals(
                                        pkcsCerts[i].getIssuerDN())) {
                    start = pkcsCerts.length;
                } else {
                    issuer = pkcsCerts[i].getIssuerDN();
                    X509Certificate tmpCert = pkcsCerts[start];
                    pkcsCerts[start] = pkcsCerts[i];
                    pkcsCerts[i] = tmpCert;
                    start++;
                }
                match = true;
                break;
            } else {
                i++;
            }
        }
        if (!match)
            break;
    }

    return certList;
}
 
Example 12
public ArrayList<X509Certificate> getCertificateChain(PKCS7 block)
    throws IOException
{
    X509Certificate userCert;
    userCert = block.getCertificate(certificateSerialNumber, issuerName);
    if (userCert == null)
        return null;

    ArrayList<X509Certificate> certList = new ArrayList<>();
    certList.add(userCert);

    X509Certificate[] pkcsCerts = block.getCertificates();
    if (pkcsCerts == null
        || userCert.getSubjectDN().equals(userCert.getIssuerDN())) {
        return certList;
    }

    Principal issuer = userCert.getIssuerDN();
    int start = 0;
    while (true) {
        boolean match = false;
        int i = start;
        while (i < pkcsCerts.length) {
            if (issuer.equals(pkcsCerts[i].getSubjectDN())) {
                // next cert in chain found
                certList.add(pkcsCerts[i]);
                // if selected cert is self-signed, we're done
                // constructing the chain
                if (pkcsCerts[i].getSubjectDN().equals(
                                        pkcsCerts[i].getIssuerDN())) {
                    start = pkcsCerts.length;
                } else {
                    issuer = pkcsCerts[i].getIssuerDN();
                    X509Certificate tmpCert = pkcsCerts[start];
                    pkcsCerts[start] = pkcsCerts[i];
                    pkcsCerts[i] = tmpCert;
                    start++;
                }
                match = true;
                break;
            } else {
                i++;
            }
        }
        if (!match)
            break;
    }

    return certList;
}
 
Example 13
public ArrayList<X509Certificate> getCertificateChain(PKCS7 block)
    throws IOException
{
    X509Certificate userCert;
    userCert = block.getCertificate(certificateSerialNumber, issuerName);
    if (userCert == null)
        return null;

    ArrayList<X509Certificate> certList = new ArrayList<>();
    certList.add(userCert);

    X509Certificate[] pkcsCerts = block.getCertificates();
    if (pkcsCerts == null
        || userCert.getSubjectDN().equals(userCert.getIssuerDN())) {
        return certList;
    }

    Principal issuer = userCert.getIssuerDN();
    int start = 0;
    while (true) {
        boolean match = false;
        int i = start;
        while (i < pkcsCerts.length) {
            if (issuer.equals(pkcsCerts[i].getSubjectDN())) {
                // next cert in chain found
                certList.add(pkcsCerts[i]);
                // if selected cert is self-signed, we're done
                // constructing the chain
                if (pkcsCerts[i].getSubjectDN().equals(
                                        pkcsCerts[i].getIssuerDN())) {
                    start = pkcsCerts.length;
                } else {
                    issuer = pkcsCerts[i].getIssuerDN();
                    X509Certificate tmpCert = pkcsCerts[start];
                    pkcsCerts[start] = pkcsCerts[i];
                    pkcsCerts[i] = tmpCert;
                    start++;
                }
                match = true;
                break;
            } else {
                i++;
            }
        }
        if (!match)
            break;
    }

    return certList;
}
 
Example 14
public ArrayList<X509Certificate> getCertificateChain(PKCS7 block)
    throws IOException
{
    X509Certificate userCert;
    userCert = block.getCertificate(certificateSerialNumber, issuerName);
    if (userCert == null)
        return null;

    ArrayList<X509Certificate> certList = new ArrayList<>();
    certList.add(userCert);

    X509Certificate[] pkcsCerts = block.getCertificates();
    if (pkcsCerts == null
        || userCert.getSubjectDN().equals(userCert.getIssuerDN())) {
        return certList;
    }

    Principal issuer = userCert.getIssuerDN();
    int start = 0;
    while (true) {
        boolean match = false;
        int i = start;
        while (i < pkcsCerts.length) {
            if (issuer.equals(pkcsCerts[i].getSubjectDN())) {
                // next cert in chain found
                certList.add(pkcsCerts[i]);
                // if selected cert is self-signed, we're done
                // constructing the chain
                if (pkcsCerts[i].getSubjectDN().equals(
                                        pkcsCerts[i].getIssuerDN())) {
                    start = pkcsCerts.length;
                } else {
                    issuer = pkcsCerts[i].getIssuerDN();
                    X509Certificate tmpCert = pkcsCerts[start];
                    pkcsCerts[start] = pkcsCerts[i];
                    pkcsCerts[i] = tmpCert;
                    start++;
                }
                match = true;
                break;
            } else {
                i++;
            }
        }
        if (!match)
            break;
    }

    return certList;
}
 
Example 15
public ArrayList<X509Certificate> getCertificateChain(PKCS7 block)
    throws IOException
{
    X509Certificate userCert;
    userCert = block.getCertificate(certificateSerialNumber, issuerName);
    if (userCert == null)
        return null;

    ArrayList<X509Certificate> certList = new ArrayList<X509Certificate>();
    certList.add(userCert);

    X509Certificate[] pkcsCerts = block.getCertificates();
    if (pkcsCerts == null
        || userCert.getSubjectDN().equals(userCert.getIssuerDN())) {
        return certList;
    }

    Principal issuer = userCert.getIssuerDN();
    int start = 0;
    while (true) {
        boolean match = false;
        int i = start;
        while (i < pkcsCerts.length) {
            if (issuer.equals(pkcsCerts[i].getSubjectDN())) {
                // next cert in chain found
                certList.add(pkcsCerts[i]);
                // if selected cert is self-signed, we're done
                // constructing the chain
                if (pkcsCerts[i].getSubjectDN().equals(
                                        pkcsCerts[i].getIssuerDN())) {
                    start = pkcsCerts.length;
                } else {
                    issuer = pkcsCerts[i].getIssuerDN();
                    X509Certificate tmpCert = pkcsCerts[start];
                    pkcsCerts[start] = pkcsCerts[i];
                    pkcsCerts[i] = tmpCert;
                    start++;
                }
                match = true;
                break;
            } else {
                i++;
            }
        }
        if (!match)
            break;
    }

    return certList;
}
 
Example 16
Source Project: flex-blazeds   File: TomcatLoginCommand.java    License: Apache License 2.0 4 votes vote down vote up
private boolean principalMatchesWithRequest(Principal principal, HttpServletRequest request)
{
    return principal != null && principal.equals(request.getUserPrincipal());
}
 
Example 17
/**
 * Returns true if the passed principal is a member of the group. 
 * This method does a recursive search, so if a principal belongs to a 
 * group which is a member of this group, true is returned.
 *
 * @param member the principal whose membership is to be checked.
 *
 * @return true if the principal is a member of this group, 
 * false otherwise.
 */
public boolean isMember(Principal member)
{
    if( principalStack.size() == 0 )
        return false;

    Object activePrincipal = principalStack.getFirst();
    return member.equals(activePrincipal);
}