1/*
2 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.net.www.protocol.http.ntlm;
27
28import java.io.IOException;
29import java.util.Base64;
30
31/*
32 * Hooks into Windows implementation of NTLM.
33 * This class will be replaced if a cross-platform version of NTLM
34 * is implemented in the future.
35 */
36
37public class NTLMAuthSequence {
38
39    private String username;
40    private String password;
41    private String ntdomain;
42    private int state;
43    private long crdHandle;
44    private long ctxHandle;
45
46    static {
47        initFirst(Status.class);
48    }
49
50    // Used by native code to indicate when a particular protocol sequence is completed
51    // and must not be re-used.
52
53    class Status {
54        boolean sequenceComplete;
55    }
56
57    Status status;
58
59    NTLMAuthSequence (String username, String password, String ntdomain)
60    throws IOException
61    {
62        this.username = username;
63        this.password = password;
64        this.ntdomain = ntdomain;
65        this.status = new Status();
66        state = 0;
67        crdHandle = getCredentialsHandle (username, ntdomain, password);
68        if (crdHandle == 0) {
69            throw new IOException ("could not get credentials handle");
70        }
71    }
72
73    public String getAuthHeader (String token) throws IOException {
74        byte[] input = null;
75
76        assert !status.sequenceComplete;
77
78        if (token != null)
79            input = Base64.getDecoder().decode(token);
80        byte[] b = getNextToken (crdHandle, input, status);
81        if (b == null)
82            throw new IOException ("Internal authentication error");
83        return Base64.getEncoder().encodeToString(b);
84    }
85
86    public boolean isComplete() {
87        return status.sequenceComplete;
88    }
89
90    private static native void initFirst (Class<NTLMAuthSequence.Status> clazz);
91
92    private native long getCredentialsHandle (String user, String domain, String password);
93
94    private native byte[] getNextToken (long crdHandle, byte[] lastToken, Status returned);
95}
96
97