blob: bfb480645949550d454d2019cc49c705758cd7d2 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 */
24
25/*
26 *
27 * (C) Copyright IBM Corp. 1999 All Rights Reserved.
28 * Copyright 1997 The Open Group Research Institute. All rights reserved.
29 */
30
31package sun.security.krb5.internal;
32
33import sun.security.krb5.EncryptedData;
34import sun.security.krb5.Asn1Exception;
35import sun.security.util.*;
36import java.io.IOException;
37import java.math.BigInteger;
38
39/**
40 * Implements the ASN.1 KRB-PRIV type.
41 *
42 * <xmp>
43 * KRB-PRIV ::= [APPLICATION 21] SEQUENCE {
44 * pvno [0] INTEGER (5),
45 * msg-type [1] INTEGER (21),
46 * -- NOTE: there is no [2] tag
47 * enc-part [3] EncryptedData -- EncKrbPrivPart
48 * }
49 * </xmp>
50 *
51 * <p>
52 * This definition reflects the Network Working Group RFC 4120
53 * specification available at
54 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
55 * http://www.ietf.org/rfc/rfc4120.txt</a>.
56 */
57
58public class KRBPriv {
59 public int pvno;
60 public int msgType;
61 public EncryptedData encPart;
62
63 public KRBPriv(EncryptedData new_encPart) {
64 pvno = Krb5.PVNO;
65 msgType = Krb5.KRB_PRIV;
66 encPart = new_encPart;
67 }
68
69 public KRBPriv(byte[] data) throws Asn1Exception,
70 KrbApErrException, IOException {
71 init(new DerValue(data));
72 }
73
74 public KRBPriv(DerValue encoding) throws Asn1Exception,
75 KrbApErrException, IOException {
76 init(encoding);
77 }
78
79
80 /**
81 * Initializes an KRBPriv object.
82 * @param encoding a single DER-encoded value.
83 * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
84 * @exception IOException if an I/O error occurs while reading encoded data.
85 * @exception KrbApErrException if the value read from the DER-encoded data
86 * stream does not match the pre-defined value.
87 */
88 private void init(DerValue encoding) throws Asn1Exception,
89 KrbApErrException, IOException {
90 DerValue der, subDer;
91 if (((encoding.getTag() & (byte)0x1F) != (byte)0x15)
92 || (encoding.isApplication() != true)
93 || (encoding.isConstructed() != true))
94 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
95 der = encoding.getData().getDerValue();
96 if (der.getTag() != DerValue.tag_Sequence)
97 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
98 subDer = der.getData().getDerValue();
99 if ((subDer.getTag() & 0x1F) == 0x00) {
100 pvno = subDer.getData().getBigInteger().intValue();
101 if (pvno != Krb5.PVNO) {
102 throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
103 }
104 }
105 else
106 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
107 subDer = der.getData().getDerValue();
108 if ((subDer.getTag() & 0x1F) == 0x01) {
109 msgType = subDer.getData().getBigInteger().intValue();
110 if (msgType != Krb5.KRB_PRIV)
111 throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
112 }
113 else
114 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
115 encPart = EncryptedData.parse(der.getData(), (byte)0x03, false);
116 if (der.getData().available() >0)
117 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
118 }
119
120 /**
121 * Encodes an KRBPriv object.
122 * @return byte array of encoded EncAPRepPart object.
123 * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
124 * @exception IOException if an I/O error occurs while reading encoded data.
125 */
126 public byte[] asn1Encode() throws Asn1Exception, IOException {
127 DerOutputStream temp, bytes;
128 temp = new DerOutputStream();
129 temp.putInteger(BigInteger.valueOf(pvno));
130 bytes = new DerOutputStream();
131 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
132 temp = new DerOutputStream();
133 temp.putInteger(BigInteger.valueOf(msgType));
134 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
135 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), encPart.asn1Encode());
136 temp = new DerOutputStream();
137 temp.write(DerValue.tag_Sequence, bytes);
138 bytes = new DerOutputStream();
139 bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x15), temp);
140 return bytes.toByteArray();
141 }
142
143}