src/java.security.sasl/share/classes/com/sun/security/sasl/PlainClient.java
changeset 59024 b046ba510bbc
parent 47216 71c04702a3d5
equal deleted inserted replaced
59023:f0dca628176c 59024:b046ba510bbc
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    24  */
    24  */
    25 
    25 
    26 package com.sun.security.sasl;
    26 package com.sun.security.sasl;
    27 
    27 
    28 import javax.security.sasl.*;
    28 import javax.security.sasl.*;
       
    29 
       
    30 import static java.nio.charset.StandardCharsets.UTF_8;
    29 
    31 
    30 /**
    32 /**
    31   * Implements the PLAIN SASL client mechanism.
    33   * Implements the PLAIN SASL client mechanism.
    32   * (<A
    34   * (<A
    33   * HREF="http://ftp.isi.edu/in-notes/rfc2595.txt">RFC 2595</A>)
    35   * HREF="http://ftp.isi.edu/in-notes/rfc2595.txt">RFC 2595</A>)
    87      * PLAIN is the concatenation of authorization ID, authentication ID
    89      * PLAIN is the concatenation of authorization ID, authentication ID
    88      * and password, with each component separated by the US-ASCII <NUL> byte.
    90      * and password, with each component separated by the US-ASCII <NUL> byte.
    89      *
    91      *
    90      * @param challengeData Ignored
    92      * @param challengeData Ignored
    91      * @return A non-null byte array containing the response to be sent to the server.
    93      * @return A non-null byte array containing the response to be sent to the server.
    92      * @throws SaslException If cannot encode ids in UTF-8
    94      * @throws IllegalStateException if authentication already completed
    93      * @throw IllegalStateException if authentication already completed
    95      */
    94      */
    96     public byte[] evaluateChallenge(byte[] challengeData) {
    95     public byte[] evaluateChallenge(byte[] challengeData) throws SaslException {
       
    96         if (completed) {
    97         if (completed) {
    97             throw new IllegalStateException(
    98             throw new IllegalStateException(
    98                 "PLAIN authentication already completed");
    99                 "PLAIN authentication already completed");
    99         }
   100         }
   100         completed = true;
   101         completed = true;
   101 
   102         byte[] authz = (authorizationID != null)
   102         try {
   103             ? authorizationID.getBytes(UTF_8)
   103             byte[] authz = (authorizationID != null)?
   104             : null;
   104                 authorizationID.getBytes("UTF8") :
   105         byte[] auth = authenticationID.getBytes(UTF_8);
   105                 null;
   106 
   106             byte[] auth = authenticationID.getBytes("UTF8");
   107         byte[] answer = new byte[pw.length + auth.length + 2 +
   107 
       
   108             byte[] answer = new byte[pw.length + auth.length + 2 +
       
   109                 (authz == null ? 0 : authz.length)];
   108                 (authz == null ? 0 : authz.length)];
   110 
   109 
   111             int pos = 0;
   110         int pos = 0;
   112             if (authz != null) {
   111         if (authz != null) {
   113                 System.arraycopy(authz, 0, answer, 0, authz.length);
   112             System.arraycopy(authz, 0, answer, 0, authz.length);
   114                 pos = authz.length;
   113             pos = authz.length;
   115             }
   114         }
   116             answer[pos++] = SEP;
   115         answer[pos++] = SEP;
   117             System.arraycopy(auth, 0, answer, pos, auth.length);
   116         System.arraycopy(auth, 0, answer, pos, auth.length);
   118 
   117 
   119             pos += auth.length;
   118         pos += auth.length;
   120             answer[pos++] = SEP;
   119         answer[pos++] = SEP;
   121 
   120 
   122             System.arraycopy(pw, 0, answer, pos, pw.length);
   121         System.arraycopy(pw, 0, answer, pos, pw.length);
   123 
   122 
   124             clearPassword();
   123         clearPassword();
   125             return answer;
   124         return answer;
   126         } catch (java.io.UnsupportedEncodingException e) {
       
   127             throw new SaslException("Cannot get UTF-8 encoding of ids", e);
       
   128         }
       
   129     }
   125     }
   130 
   126 
   131     /**
   127     /**
   132      * Determines whether this mechanism has completed.
   128      * Determines whether this mechanism has completed.
   133      * Plain completes after returning one response.
   129      * Plain completes after returning one response.