org.apache.tomcat.util.security.ConcurrentMessageDigest Java Examples

The following examples show how to use org.apache.tomcat.util.security.ConcurrentMessageDigest. 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 File: TesterDigestAuthenticatorPerformance.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

    ConcurrentMessageDigest.init("MD5");

    // Configure the Realm
    MapRealm realm = new MapRealm();
    realm.addUser(USER, PWD);
    realm.addUserRole(USER, ROLE);

    // Add the Realm to the Context
    Context context = new StandardContext();
    context.setName(CONTEXT_PATH);
    context.setRealm(realm);

    // Make the Context and Realm visible to the Authenticator
    authenticator.setContainer(context);
    authenticator.setNonceCountWindowSize(8 * 1024);

    authenticator.start();
}
 
Example #2
Source File: MessageDigestCredentialHandler.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
protected String mutate(String inputCredentials, byte[] salt, int iterations) {
    if (algorithm == null) {
        return inputCredentials;
    } else {
        byte[] userDigest;
        if (salt == null) {
            userDigest = ConcurrentMessageDigest.digest(algorithm, iterations,
                    inputCredentials.getBytes(encoding));
        } else {
            userDigest = ConcurrentMessageDigest.digest(algorithm, iterations,
                    salt, inputCredentials.getBytes(encoding));
        }
        return HexUtils.toHexString(userDigest);
    }
}
 
Example #3
Source File: RealmBase.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Return the digest associated with given principal's user name.
 * @param username the user name
 * @param realmName the realm name
 * @return the digest for the specified user
 */
protected String getDigest(String username, String realmName) {
    if (hasMessageDigest()) {
        // Use pre-generated digest
        return getPassword(username);
    }

    String digestValue = username + ":" + realmName + ":"
        + getPassword(username);

    byte[] valueBytes = null;
    try {
        valueBytes = digestValue.getBytes(getDigestCharset());
    } catch (UnsupportedEncodingException uee) {
        log.error("Illegal digestEncoding: " + getDigestEncoding(), uee);
        throw new IllegalArgumentException(uee.getMessage());
    }

    return MD5Encoder.encode(ConcurrentMessageDigest.digestMD5(valueBytes));
}
 
Example #4
Source File: TesterDigestAuthenticatorPerformance.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {

    ConcurrentMessageDigest.init("MD5");

    // Configure the Realm
    TesterMapRealm realm = new TesterMapRealm();
    realm.addUser(USER, PWD);
    realm.addUserRole(USER, ROLE);

    // Add the Realm to the Context
    Context context = new StandardContext();
    context.setName(CONTEXT_PATH);
    context.setRealm(realm);

    // Configure the Login config
    LoginConfig config = new LoginConfig();
    config.setRealmName(REALM);
    context.setLoginConfig(config);

    // Make the Context and Realm visible to the Authenticator
    authenticator.setContainer(context);
    authenticator.setNonceCountWindowSize(8 * 1024);

    authenticator.start();
}
 
Example #5
Source File: TesterDigestAuthenticatorPerformance.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {

    ConcurrentMessageDigest.init("MD5");

    // Configure the Realm
    MapRealm realm = new MapRealm();
    realm.addUser(USER, PWD);
    realm.addUserRole(USER, ROLE);

    // Add the Realm to the Context
    Context context = new StandardContext();
    context.setName(CONTEXT_PATH);
    context.setRealm(realm);

    // Make the Context and Realm visible to the Authenticator
    authenticator.setContainer(context);
    authenticator.setNonceCountWindowSize(8 * 1024);

    authenticator.start();
}
 
Example #6
Source File: DigestAuthenticator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public Principal authenticate(Realm realm) {
    // Second MD5 digest used to calculate the digest :
    // MD5(Method + ":" + uri)
    String a2 = method + ":" + uri;

    byte[] buffer = ConcurrentMessageDigest.digestMD5(
            a2.getBytes(StandardCharsets.ISO_8859_1));
    String md5a2 = MD5Encoder.encode(buffer);

    return realm.authenticate(userName, response, nonce, nc, cnonce,
            qop, realmName, md5a2);
}
 
Example #7
Source File: TesterDigestAuthenticatorPerformance.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private String buildDigestResponse(String nonce) {

            String ncString = String.format("%1$08x",
                    Integer.valueOf(nonceCount.incrementAndGet()));
            String cnonce = "cnonce";

            String response = MD5A1 + ":" + nonce + ":" + ncString + ":" +
                    cnonce + ":" + QOP + ":" + MD5A2;

            String md5response = MD5Encoder.encode(
                    ConcurrentMessageDigest.digest("MD5", response.getBytes()));

            StringBuilder auth = new StringBuilder();
            auth.append("Digest username=\"");
            auth.append(USER);
            auth.append("\", realm=\"");
            auth.append(REALM);
            auth.append("\", nonce=\"");
            auth.append(nonce);
            auth.append("\", uri=\"");
            auth.append(CONTEXT_PATH + URI);
            auth.append("\", opaque=\"");
            auth.append(authenticator.getOpaque());
            auth.append("\", response=\"");
            auth.append(md5response);
            auth.append("\"");
            auth.append(", qop=");
            auth.append(QOP);
            auth.append(", nc=");
            auth.append(ncString);
            auth.append(", cnonce=\"");
            auth.append(cnonce);
            auth.append("\"");

            return auth.toString();
        }
 
Example #8
Source File: DigestAuthenticator.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public Principal authenticate(Realm realm) {
    // Second MD5 digest used to calculate the digest :
    // MD5(Method + ":" + uri)
    String a2 = method + ":" + uri;

    byte[] buffer = ConcurrentMessageDigest.digestMD5(
            a2.getBytes(B2CConverter.ISO_8859_1));
    String md5a2 = MD5Encoder.encode(buffer);

    return realm.authenticate(userName, response, nonce, nc, cnonce,
            qop, realmName, md5a2);
}
 
Example #9
Source File: DigestAuthenticator.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a unique token. The token is generated according to the
 * following pattern. NOnceToken = Base64 ( MD5 ( client-IP ":"
 * time-stamp ":" private-key ) ).
 *
 * @param request HTTP Servlet request
 */
protected String generateNonce(Request request) {

    long currentTime = System.currentTimeMillis();

    synchronized (lastTimestampLock) {
        if (currentTime > lastTimestamp) {
            lastTimestamp = currentTime;
        } else {
            currentTime = ++lastTimestamp;
        }
    }

    String ipTimeKey =
        request.getRemoteAddr() + ":" + currentTime + ":" + getKey();

    byte[] buffer = ConcurrentMessageDigest.digestMD5(
            ipTimeKey.getBytes(B2CConverter.ISO_8859_1));
    String nonce = currentTime + ":" + MD5Encoder.encode(buffer);

    NonceInfo info = new NonceInfo(currentTime, getNonceCountWindowSize());
    synchronized (nonces) {
        nonces.put(nonce, info);
    }

    return nonce;
}
 
Example #10
Source File: TesterDigestAuthenticatorPerformance.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private String buildDigestResponse(String nonce) {

            String ncString = String.format("%1$08x",
                    Integer.valueOf(nonceCount.incrementAndGet()));
            String cnonce = "cnonce";

            String response = MD5A1 + ":" + nonce + ":" + ncString + ":" +
                    cnonce + ":" + QOP + ":" + MD5A2;

            String md5response = MD5Encoder.encode(
                    ConcurrentMessageDigest.digest("MD5", response.getBytes()));

            StringBuilder auth = new StringBuilder();
            auth.append("Digest username=\"");
            auth.append(USER);
            auth.append("\", realm=\"");
            auth.append(REALM);
            auth.append("\", nonce=\"");
            auth.append(nonce);
            auth.append("\", uri=\"");
            auth.append(CONTEXT_PATH + URI);
            auth.append("\", opaque=\"");
            auth.append(authenticator.getOpaque());
            auth.append("\", response=\"");
            auth.append(md5response);
            auth.append("\"");
            auth.append(", qop=");
            auth.append(QOP);
            auth.append(", nc=");
            auth.append(ncString);
            auth.append(", cnonce=\"");
            auth.append(cnonce);
            auth.append("\"");

            return auth.toString();
        }
 
Example #11
Source File: DigestAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public Principal authenticate(Realm realm) {
    // Second MD5 digest used to calculate the digest :
    // MD5(Method + ":" + uri)
    String a2 = method + ":" + uri;

    byte[] buffer = ConcurrentMessageDigest.digestMD5(
            a2.getBytes(B2CConverter.ISO_8859_1));
    String md5a2 = MD5Encoder.encode(buffer);

    return realm.authenticate(userName, response, nonce, nc, cnonce,
            qop, realmName, md5a2);
}
 
Example #12
Source File: DigestAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a unique token. The token is generated according to the
 * following pattern. NOnceToken = Base64 ( MD5 ( client-IP ":"
 * time-stamp ":" private-key ) ).
 *
 * @param request HTTP Servlet request
 */
protected String generateNonce(Request request) {

    long currentTime = System.currentTimeMillis();

    synchronized (lastTimestampLock) {
        if (currentTime > lastTimestamp) {
            lastTimestamp = currentTime;
        } else {
            currentTime = ++lastTimestamp;
        }
    }

    String ipTimeKey =
        request.getRemoteAddr() + ":" + currentTime + ":" + getKey();

    byte[] buffer = ConcurrentMessageDigest.digestMD5(
            ipTimeKey.getBytes(B2CConverter.ISO_8859_1));
    String nonce = currentTime + ":" + MD5Encoder.encode(buffer);

    NonceInfo info = new NonceInfo(currentTime, getNonceCountWindowSize());
    synchronized (nonces) {
        nonces.put(nonce, info);
    }

    return nonce;
}
 
Example #13
Source File: TesterDigestAuthenticatorPerformance.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private String buildDigestResponse(String nonce) {

            String ncString = String.format("%1$08x",
                    Integer.valueOf(nonceCount.incrementAndGet()));
            String cnonce = "cnonce";

            String response = MD5A1 + ":" + nonce + ":" + ncString + ":" +
                    cnonce + ":" + QOP + ":" + MD5A2;

            String md5response = MD5Encoder.encode(
                    ConcurrentMessageDigest.digest("MD5", response.getBytes()));

            StringBuilder auth = new StringBuilder();
            auth.append("Digest username=\"");
            auth.append(USER);
            auth.append("\", realm=\"");
            auth.append(REALM);
            auth.append("\", nonce=\"");
            auth.append(nonce);
            auth.append("\", uri=\"");
            auth.append(CONTEXT_PATH + URI);
            auth.append("\", opaque=\"");
            auth.append(authenticator.getOpaque());
            auth.append("\", response=\"");
            auth.append(md5response);
            auth.append("\"");
            auth.append(", qop=");
            auth.append(QOP);
            auth.append(", nc=");
            auth.append(ncString);
            auth.append(", cnonce=\"");
            auth.append(cnonce);
            auth.append("\"");

            return auth.toString();
        }
 
Example #14
Source File: DigestAuthenticator.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Generate a unique token. The token is generated according to the
 * following pattern. NOnceToken = Base64 ( MD5 ( client-IP ":"
 * time-stamp ":" private-key ) ).
 *
 * @param request HTTP Servlet request
 * @return The generated nonce
 */
protected String generateNonce(Request request) {

    long currentTime = System.currentTimeMillis();

    synchronized (lastTimestampLock) {
        if (currentTime > lastTimestamp) {
            lastTimestamp = currentTime;
        } else {
            currentTime = ++lastTimestamp;
        }
    }

    String ipTimeKey =
        request.getRemoteAddr() + ":" + currentTime + ":" + getKey();

    byte[] buffer = ConcurrentMessageDigest.digestMD5(
            ipTimeKey.getBytes(StandardCharsets.ISO_8859_1));
    String nonce = currentTime + ":" + MD5Encoder.encode(buffer);

    NonceInfo info = new NonceInfo(currentTime, getNonceCountWindowSize());
    synchronized (nonces) {
        nonces.put(nonce, info);
    }

    return nonce;
}
 
Example #15
Source File: TestDigestAuthenticator.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private static String digest(String input) {
    return MD5Encoder.encode(
            ConcurrentMessageDigest.digestMD5(input.getBytes()));
}
 
Example #16
Source File: UpgradeUtil.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private static String getWebSocketAccept(String key) {
    byte[] digest = ConcurrentMessageDigest.digestSHA1(
            key.getBytes(StandardCharsets.ISO_8859_1), WS_ACCEPT);
    return Base64.encodeBase64String(digest);
}
 
Example #17
Source File: TestSSOnonLoginAndDigestAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private static String digest(String input) {
    return MD5Encoder.encode(
            ConcurrentMessageDigest.digestMD5(input.getBytes()));
}
 
Example #18
Source File: TestSSOnonLoginAndDigestAuthenticator.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private static String digest(String input) {
    return MD5Encoder.encode(
            ConcurrentMessageDigest.digestMD5(input.getBytes()));
}
 
Example #19
Source File: TestDigestAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private static String digest(String input) {
    return MD5Encoder.encode(
            ConcurrentMessageDigest.digestMD5(input.getBytes()));
}
 
Example #20
Source File: UpgradeUtil.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private static String getWebSocketAccept(String key) {
    byte[] digest = ConcurrentMessageDigest.digestSHA1(
            key.getBytes(StandardCharsets.ISO_8859_1), WS_ACCEPT);
    return Base64.encodeBase64String(digest);
}
 
Example #21
Source File: RealmBase.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Try to authenticate with the specified username, which
 * matches the digest calculated using the given parameters using the
 * method described in RFC 2617 (which is a superset of RFC 2069).
 *
 * @param username Username of the Principal to look up
 * @param clientDigest Digest which has been submitted by the client
 * @param nonce Unique (or supposedly unique) token which has been used
 * for this request
 * @param nc the nonce counter
 * @param cnonce the client chosen nonce
 * @param qop the "quality of protection" (<code>nc</code> and <code>cnonce</code>
 *        will only be used, if <code>qop</code> is not <code>null</code>).
 * @param realm Realm name
 * @param md5a2 Second MD5 digest used to calculate the digest :
 * MD5(Method + ":" + uri)
 * @return the associated principal, or <code>null</code> if there is none.
 */
@Override
public Principal authenticate(String username, String clientDigest,
                              String nonce, String nc, String cnonce,
                              String qop, String realm,
                              String md5a2) {

    // In digest auth, digests are always lower case
    String md5a1 = getDigest(username, realm);
    if (md5a1 == null)
        return null;
    md5a1 = md5a1.toLowerCase(Locale.ENGLISH);
    String serverDigestValue;
    if (qop == null) {
        serverDigestValue = md5a1 + ":" + nonce + ":" + md5a2;
    } else {
        serverDigestValue = md5a1 + ":" + nonce + ":" + nc + ":" +
                cnonce + ":" + qop + ":" + md5a2;
    }

    byte[] valueBytes = null;
    try {
        valueBytes = serverDigestValue.getBytes(getDigestCharset());
    } catch (UnsupportedEncodingException uee) {
        log.error("Illegal digestEncoding: " + getDigestEncoding(), uee);
        throw new IllegalArgumentException(uee.getMessage());
    }

    String serverDigest = MD5Encoder.encode(ConcurrentMessageDigest.digestMD5(valueBytes));

    if (log.isDebugEnabled()) {
        log.debug("Digest : " + clientDigest + " Username:" + username
                + " ClientDigest:" + clientDigest + " nonce:" + nonce
                + " nc:" + nc + " cnonce:" + cnonce + " qop:" + qop
                + " realm:" + realm + "md5a2:" + md5a2
                + " Server digest:" + serverDigest);
    }

    if (serverDigest.equals(clientDigest)) {
        return getPrincipal(username);
    }

    return null;
}
 
Example #22
Source File: UpgradeUtil.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private static String getWebSocketAccept(String key) {
    byte[] digest = ConcurrentMessageDigest.digestSHA1(
            key.getBytes(StandardCharsets.ISO_8859_1), WS_ACCEPT);
    return Base64.encodeBase64String(digest);
}
 
Example #23
Source File: TestSSOnonLoginAndDigestAuthenticator.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private static String digest(String input) {
    return MD5Encoder.encode(
            ConcurrentMessageDigest.digestMD5(input.getBytes()));
}
 
Example #24
Source File: MessageDigestCredentialHandler.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void setAlgorithm(String algorithm) throws NoSuchAlgorithmException {
    ConcurrentMessageDigest.init(algorithm);
    this.algorithm = algorithm;
}
 
Example #25
Source File: TestDigestAuthenticator.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private static String digest(String input) {
    return MD5Encoder.encode(
            ConcurrentMessageDigest.digestMD5(input.getBytes()));
}