Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Authenticator/B6870935.java
38812 views
/*1* Copyright (c) 2001, 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 687093526* @run main/othervm -Dhttp.nonProxyHosts="" -Dhttp.auth.digest.validateProxy=true B687093527*/2829import java.io.*;30import java.util.*;31import java.net.*;32import java.security.*;33import sun.net.www.*;3435/* This is one simple test of the RFC2617 digest authentication behavior36* It specifically tests that the client correctly checks the returned37* Authentication-Info header field from the server and throws an exception38* if the password is wrong39*/4041public class B6870935 {4243static char[] passwd = "password".toCharArray();44static String username = "user";45static String nonce = "abcdefghijklmnopqrstuvwxyz";46static String realm = "wallyworld";47static String uri = "http://www.ibm.com";48static volatile boolean error = false;4950static class DigestServer extends Thread {5152ServerSocket s;53InputStream is;54OutputStream os;55int port;5657String reply1 = "HTTP/1.1 407 Proxy Authentication Required\r\n"+58"Proxy-Authenticate: Digest realm=\""+realm+"\" domain=/ "+59"nonce=\""+nonce+"\" qop=\"auth\"\r\n\r\n";6061String reply2 = "HTTP/1.1 200 OK\r\n" +62"Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +63"Server: Apache/1.3.14 (Unix)\r\n" +64"Content-Type: text/html; charset=iso-8859-1\r\n" +65"Transfer-encoding: chunked\r\n\r\n"+66"B\r\nHelloWorld1\r\n"+67"B\r\nHelloWorld2\r\n"+68"B\r\nHelloWorld3\r\n"+69"B\r\nHelloWorld4\r\n"+70"B\r\nHelloWorld5\r\n"+71"0\r\n"+72"Proxy-Authentication-Info: ";7374DigestServer (ServerSocket y) {75s = y;76port = s.getLocalPort();77}7879public void run () {80try {81Socket s1 = s.accept ();82is = s1.getInputStream ();83os = s1.getOutputStream ();84is.read ();85os.write (reply1.getBytes());86Thread.sleep (2000);87s1.close ();8889s1 = s.accept ();90is = s1.getInputStream ();91os = s1.getOutputStream ();92is.read ();93// need to get the cnonce out of the response94MessageHeader header = new MessageHeader (is);95String raw = header.findValue ("Proxy-Authorization");96HeaderParser parser = new HeaderParser (raw);97String cnonce = parser.findValue ("cnonce");98String cnstring = parser.findValue ("nc");99String clientrsp = parser.findValue ("response");100String expected = computeDigest(101true, username,passwd,realm,102"GET", uri, nonce, cnonce, cnstring103);104if (!expected.equals(clientrsp)) {105s1.close ();106s.close ();107error = true;108return;109}110111String reply = reply2 + getAuthorization (112realm, false, uri, "GET", cnonce,113cnstring, passwd, username114) +"\r\n";115os.write (reply.getBytes());116Thread.sleep (2000);117s1.close ();118}119catch (Exception e) {120System.out.println (e);121e.printStackTrace();122}123}124125private String getAuthorization (String realm, boolean isRequest, String uri, String method, String cnonce, String cnstring, char[] password, String username) {126String response;127128try {129response = computeDigest(isRequest, username,passwd,realm,130method, uri, nonce, cnonce, cnstring);131} catch (NoSuchAlgorithmException ex) {132return null;133}134135String value = "Digest"136+ " qop=\"auth"137+ "\", cnonce=\"" + cnonce138+ "\", rspauth=\"" + response139+ "\", nc=\"" + cnstring + "\"";140return (value+ "\r\n");141}142143private String computeDigest(144boolean isRequest, String userName, char[] password,145String realm, String connMethod,146String requestURI, String nonceString,147String cnonce, String ncValue148) throws NoSuchAlgorithmException149{150151String A1, HashA1;152153MessageDigest md = MessageDigest.getInstance("MD5");154155{156A1 = userName + ":" + realm + ":";157HashA1 = encode(A1, password, md);158}159160String A2;161if (isRequest) {162A2 = connMethod + ":" + requestURI;163} else {164A2 = ":" + requestURI;165}166String HashA2 = encode(A2, null, md);167String combo, finalHash;168169{ /* RRC2617 when qop=auth */170combo = HashA1+ ":" + nonceString + ":" + ncValue + ":" +171cnonce + ":auth:" +HashA2;172173}174finalHash = encode(combo, null, md);175return finalHash;176}177178private final static char charArray[] = {179'0', '1', '2', '3', '4', '5', '6', '7',180'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'181};182183private String encode(String src, char[] passwd, MessageDigest md) {184md.update(src.getBytes());185if (passwd != null) {186byte[] passwdBytes = new byte[passwd.length];187for (int i=0; i<passwd.length; i++)188passwdBytes[i] = (byte)passwd[i];189md.update(passwdBytes);190Arrays.fill(passwdBytes, (byte)0x00);191}192byte[] digest = md.digest();193194StringBuffer res = new StringBuffer(digest.length * 2);195for (int i = 0; i < digest.length; i++) {196int hashchar = ((digest[i] >>> 4) & 0xf);197res.append(charArray[hashchar]);198hashchar = (digest[i] & 0xf);199res.append(charArray[hashchar]);200}201return res.toString();202}203}204205206static class MyAuthenticator extends Authenticator {207public MyAuthenticator () {208super ();209}210211public PasswordAuthentication getPasswordAuthentication ()212{213return (new PasswordAuthentication (username, passwd));214}215}216217218public static void main(String[] args) throws Exception {219int nLoops = 1;220int nSize = 10;221int port, n =0;222byte b[] = new byte[nSize];223DigestServer server;224ServerSocket sock;225226try {227sock = new ServerSocket (0);228port = sock.getLocalPort ();229}230catch (Exception e) {231System.out.println ("Exception: " + e);232return;233}234235server = new DigestServer(sock);236server.start ();237238try {239240Authenticator.setDefault (new MyAuthenticator ());241SocketAddress addr = new InetSocketAddress ("127.0.0.1", port);242Proxy proxy = new Proxy (Proxy.Type.HTTP, addr);243String s = "http://www.ibm.com";244URL url = new URL(s);245java.net.URLConnection conURL = url.openConnection(proxy);246247InputStream in = conURL.getInputStream();248int c;249while ((c = in.read ()) != -1) {250}251in.close ();252}253catch(IOException e) {254e.printStackTrace();255error = true;256}257if (error) {258throw new RuntimeException ("Error in test");259}260}261}262263264