1 /* 2 * Copyright (c) 2009, 2019, 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 26 package sun.security.krb5.internal.util; 27 28 import java.io.IOException; 29 import sun.security.action.GetPropertyAction; 30 import sun.security.util.DerValue; 31 32 import static java.nio.charset.StandardCharsets.*; 33 34 /** 35 * Implements the ASN.1 KerberosString type. 36 * 37 * <pre> 38 * KerberosString ::= GeneralString (IA5String) 39 * </pre> 40 * 41 * This definition reflects the Network Working Group RFC 4120 42 * specification available at 43 * <a href="http://www.ietf.org/rfc/rfc4120.txt"> 44 * http://www.ietf.org/rfc/rfc4120.txt</a>. 45 */ 46 public final class KerberosString { 47 /** 48 * RFC 4120 defines KerberosString as GeneralString (IA5String), which 49 * only includes ASCII characters. However, most implementations have been 50 * known to use GeneralString to contain UTF-8 encoding. The following 51 * system property is defined. When set as true, KerberosString is encoded 52 * as UTF-8. Otherwise, it's ASCII. The default is true. 53 * 54 * Note that this only affects the byte encoding, the tag of the ASN.1 55 * type is still GeneralString. 56 */ 57 public static final boolean MSNAME; 58 59 static { 60 String prop = GetPropertyAction 61 .privilegedGetProperty("sun.security.krb5.msinterop.kstring", "true"); 62 MSNAME = Boolean.parseBoolean(prop); 63 } 64 65 private final String s; 66 KerberosString(String s)67 public KerberosString(String s) { 68 this.s = s; 69 } 70 KerberosString(DerValue der)71 public KerberosString(DerValue der) throws IOException { 72 if (der.tag != DerValue.tag_GeneralString) { 73 throw new IOException( 74 "KerberosString's tag is incorrect: " + der.tag); 75 } 76 s = new String(der.getDataBytes(), MSNAME ? UTF_8 : US_ASCII); 77 } 78 toString()79 public String toString() { 80 return s; 81 } 82 toDerValue()83 public DerValue toDerValue() { 84 // No need to cache the result since this method is 85 // only called once. 86 return new DerValue(DerValue.tag_GeneralString, 87 s.getBytes(MSNAME ? UTF_8 : US_ASCII)); 88 } 89 } 90