File: Authentication.java

package info (click to toggle)
libsapdbc-java 5567-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,840 kB
  • ctags: 6,242
  • sloc: java: 49,887; makefile: 83
file content (208 lines) | stat: -rw-r--r-- 8,348 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.sap.dbtech.util.security;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import com.sap.dbtech.jdbc.exceptions.SQLExceptionSapDB;
import com.sap.dbtech.jdbc.packet.DataPartVariable;
import com.sap.dbtech.util.MessageKey;
import com.sap.dbtech.util.MessageTranslator;
import com.sap.dbtech.util.StructuredBytes;
import com.sap.dbtech.util.Tracer;
import com.sap.dbtech.vsp001.Packet;

/*
 
 created by D031096
 
 ========== licence begin GPL
    Copyright (C) 2002-2003 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end
 
 
 */
public class Authentication {
    
    private byte[] salt;
    
    private byte[] clientchallenge;
    
    private byte[] serverchallenge;
    
    private int maxpasswordLen = 0;
    
    private static SecureRandom srnd = new SecureRandom();
    
    public Authentication() throws java.security.NoSuchAlgorithmException {
        clientchallenge = new byte[64];
        int retrycount = 10; //workaround for buggy vm on IBM J9SE VM (build 2.2, J2RE 1.4.2 IBM J9 2.2 Linux amd64-64)
        while (true) {
            try {
                srnd.nextBytes(
                        getClientchallenge());
                return;
            } catch (Exception e) {
                if (--retrycount <= 0) {
                  throw new java.security.NoSuchAlgorithmException(e.toString());
                }
            }
        }
    }
    
    /**
     * @return Returns the clientchallenge.
     */
    public byte[] getClientchallenge() {
        return clientchallenge;
    }
    
    /**
     * @return Returns the clientchallenge.
     */
    public byte[] getClientProof(byte[] password) throws SQLExceptionSapDB {
        try {
            return SCRAMMD5.scrammMD5(salt, password, clientchallenge,
                    serverchallenge);
        } catch (NoSuchAlgorithmException ex) {
            throw new SQLExceptionSapDB
			(MessageTranslator.translate
				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
				 "NoSuchAlgorithmException - algorithm \"MD5\" not supported by the java vm"));		

        }
    }

    /**
     * @param serverchallenge
     *            Parses the serverchallenge and split it into salt and real
     *            server challenge.
     */
    public void parseServerChallenge(byte[] vData)
    throws SQLExceptionSapDB {
        DataPartVariable vd = new DataPartVariable( new StructuredBytes (
                vData), 1);
        if (!vd.nextRow() || !vd.nextField()) {
            throw new SQLExceptionSapDB
			(MessageTranslator.translate
				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
				 Tracer.Hex2String(vData)));		
        }
        this.salt = vd.getBytes(vd.getCurrentOffset(),vd.getCurrentFieldLen());
        if (!vd.nextField()) {
            throw new SQLExceptionSapDB
			(MessageTranslator.translate
				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
				 Tracer.Hex2String(vData)));		
        }
        this.serverchallenge = vd.getBytes(vd.getCurrentOffset(),vd.getCurrentFieldLen());

    }
    /**
     * @param serverchallenge
     *            Parses the serverchallenge and split it into salt and real
     *            server challenge.
     */
    public void parseServerChallengeReply(DataPartVariable vData)
    throws SQLExceptionSapDB {
        if (!vData.nextRow() || !vData.nextField()) {
            throw new SQLExceptionSapDB
			(MessageTranslator.translate
				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
        }
        String algo = vData.getString(vData.getCurrentOffset(), vData
                .getCurrentFieldLen());
        if (!algo.equals(SCRAMMD5.algorithmname)) {
            throw new SQLExceptionSapDB
			(MessageTranslator.translate
				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
        }
        if (!vData.nextField() || vData.getCurrentFieldLen() < 8) {
            throw new SQLExceptionSapDB
			(MessageTranslator.translate
				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
        }
        if (vData.getCurrentFieldLen() == 40){
	        /*first version of challenge response 
	         *should only occurs with database version 7.6.0.0 <= kernel <= 7.6.0.7*/
	        this.salt = vData.getBytes(vData.getCurrentOffset(), 8);
	        this.serverchallenge = vData.getBytes(vData.getCurrentOffset() + 8,
	                vData.getCurrentFieldLen() - 8);
       } else {
           DataPartVariable vd = new DataPartVariable( new StructuredBytes (
                   vData.getBytes(vData.getCurrentOffset(), vData.getCurrentFieldLen())), 1);
           if (!vd.nextRow() || !vd.nextField()) {
               throw new SQLExceptionSapDB
   			(MessageTranslator.translate
   				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
   				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
           }
           this.salt = vd.getBytes(vd.getCurrentOffset(),vd.getCurrentFieldLen());
           if (!vd.nextField()) {
               throw new SQLExceptionSapDB
   			(MessageTranslator.translate
   				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
   				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
           }
           this.serverchallenge = vd.getBytes(vd.getCurrentOffset(),vd.getCurrentFieldLen());
           
           /*from Version 7.6.0.10 on also the max password length will be delivered*/
           if (vData.nextField()){
               DataPartVariable mp_vd = new DataPartVariable( new StructuredBytes (
                       vData.getBytes(vData.getCurrentOffset(), vData.getCurrentFieldLen())), 1);
               if (!mp_vd.nextRow() || !mp_vd.nextField()) {
                   throw new SQLExceptionSapDB
       			(MessageTranslator.translate
       				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
       				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
               }
               do {
                 if (mp_vd.getString(mp_vd.getCurrentOffset(),mp_vd.getCurrentFieldLen()).equals(Packet.csp1_maxpasswordlen_tag_C)){
                     if (!mp_vd.nextField()) {
                         throw new SQLExceptionSapDB
             			(MessageTranslator.translate
             				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
             				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
                     }else{
                        try {
                            this.maxpasswordLen = Integer.parseInt(mp_vd.getString(mp_vd.getCurrentOffset(),mp_vd.getCurrentFieldLen())) ;
                        } catch (NumberFormatException e) {
                            throw new SQLExceptionSapDB
                 			(MessageTranslator.translate
                 				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
                 				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
                        } 
                     }
                 } else {
                     if (!mp_vd.nextField()) {
                         throw new SQLExceptionSapDB
             			(MessageTranslator.translate
             				(MessageKey.ERROR_CONNECTION_WRONGSERVERCHALLENGERECEIVED,
             				 Tracer.Hex2String(vData.getBytes(0,vData.size()))));		
                     }
                 }     
               } while (mp_vd.nextField());
           }
       }  
    }
    
    public int getMaxpasswordLen() {
        return maxpasswordLen;
    }
}