Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/KrbAsReq.java
41159 views
/*1* Copyright (c) 2000, 2019, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*27* (C) Copyright IBM Corp. 1999 All Rights Reserved.28* Copyright 1997 The Open Group Research Institute. All rights reserved.29*/3031package sun.security.krb5;3233import sun.security.krb5.internal.*;34import sun.security.krb5.internal.crypto.Nonce;35import sun.security.krb5.internal.crypto.KeyUsage;36import java.io.IOException;37import java.time.Instant;38import java.util.Arrays;3940/**41* This class encapsulates the KRB-AS-REQ message that the client42* sends to the KDC.43*/44public class KrbAsReq {45private ASReq asReqMessg;4647private boolean DEBUG = Krb5.DEBUG;4849/**50* Constructs an AS-REQ message.51*/52// Can be null? has default?53public KrbAsReq(EncryptionKey pakey, // ok54KDCOptions options, // ok, new KDCOptions()55PrincipalName cname, // NO and must have realm56PrincipalName sname, // ok, krgtgt@CREALM57KerberosTime from, // ok58KerberosTime till, // ok, will use59KerberosTime rtime, // ok60int[] eTypes, // NO61HostAddresses addresses, // ok62PAData[] extraPAs // ok63)64throws KrbException, IOException {6566if (options == null) {67options = new KDCOptions();68}69// check if they are valid arguments. The optional fields should be70// consistent with settings in KDCOptions. Mar 17 200071if (options.get(KDCOptions.FORWARDED) ||72options.get(KDCOptions.PROXY) ||73options.get(KDCOptions.ENC_TKT_IN_SKEY) ||74options.get(KDCOptions.RENEW) ||75options.get(KDCOptions.VALIDATE)) {76// this option is only specified in a request to the77// ticket-granting server78throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);79}80if (options.get(KDCOptions.POSTDATED)) {81// if (from == null)82// throw new KrbException(Krb5.KRB_AP_ERR_REQ_OPTIONS);83} else {84if (from != null) from = null;85}8687PAData[] paData = null;88if (pakey != null) {89PAEncTSEnc ts = new PAEncTSEnc();90byte[] temp = ts.asn1Encode();91EncryptedData encTs = new EncryptedData(pakey, temp,92KeyUsage.KU_PA_ENC_TS);93paData = new PAData[1];94paData[0] = new PAData( Krb5.PA_ENC_TIMESTAMP,95encTs.asn1Encode());96}97if (extraPAs != null && extraPAs.length > 0) {98if (paData == null) {99paData = new PAData[extraPAs.length];100} else {101paData = Arrays.copyOf(paData, paData.length + extraPAs.length);102}103System.arraycopy(extraPAs, 0, paData,104paData.length - extraPAs.length, extraPAs.length);105}106107if (cname.getRealm() == null) {108throw new RealmException(Krb5.REALM_NULL,109"default realm not specified ");110}111112if (DEBUG) {113System.out.println(">>> KrbAsReq creating message");114}115116Config cfg = Config.getInstance();117118// check to use addresses in tickets119if (addresses == null && cfg.useAddresses()) {120addresses = HostAddresses.getLocalAddresses();121}122123if (sname == null) {124String realm = cname.getRealmAsString();125sname = PrincipalName.tgsService(realm, realm);126}127128if (till == null) {129String d = cfg.get("libdefaults", "ticket_lifetime");130if (d != null) {131till = new KerberosTime(Instant.now().plusSeconds(Config.duration(d)));132} else {133till = new KerberosTime(0); // Choose KDC maximum allowed134}135}136137if (rtime == null) {138String d = cfg.get("libdefaults", "renew_lifetime");139if (d != null) {140rtime = new KerberosTime(Instant.now().plusSeconds(Config.duration(d)));141}142}143144if (rtime != null) {145options.set(KDCOptions.RENEWABLE, true);146if (till.greaterThan(rtime)) {147rtime = till;148}149}150151// enc-authorization-data and additional-tickets never in AS-REQ152KDCReqBody kdc_req_body = new KDCReqBody(options,153cname,154sname,155from,156till,157rtime,158Nonce.value(),159eTypes,160addresses,161null,162null);163164asReqMessg = new ASReq(165paData,166kdc_req_body);167}168169byte[] encoding() throws IOException, Asn1Exception {170return asReqMessg.asn1Encode();171}172173// Used by KrbAsRep to validate AS-REP174ASReq getMessage() {175return asReqMessg;176}177}178179180