J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 31 | package sun.security.krb5.internal; |
| 32 | |
| 33 | import sun.security.util.*; |
| 34 | import sun.security.krb5.Asn1Exception; |
| 35 | import java.io.IOException; |
| 36 | |
| 37 | /** |
| 38 | * Implements the ASN.1 PA-DATA type. |
| 39 | * |
| 40 | * <xmp> |
| 41 | * PA-DATA ::= SEQUENCE { |
| 42 | * -- NOTE: first tag is [1], not [0] |
| 43 | * padata-type [1] Int32, |
| 44 | * padata-value [2] OCTET STRING -- might be encoded AP-REQ |
| 45 | * } |
| 46 | * </xmp> |
| 47 | * |
| 48 | * <p> |
| 49 | * This definition reflects the Network Working Group RFC 4120 |
| 50 | * specification available at |
| 51 | * <a href="http://www.ietf.org/rfc/rfc4120.txt"> |
| 52 | * http://www.ietf.org/rfc/rfc4120.txt</a>. |
| 53 | */ |
| 54 | |
| 55 | public class PAData { |
| 56 | private int pADataType; |
| 57 | private byte[] pADataValue = null; |
| 58 | private static final byte TAG_PATYPE = 1; |
| 59 | private static final byte TAG_PAVALUE = 2; |
| 60 | |
| 61 | private PAData() { |
| 62 | } |
| 63 | |
| 64 | public PAData(int new_pADataType, byte[] new_pADataValue) { |
| 65 | pADataType = new_pADataType; |
| 66 | if (new_pADataValue != null) { |
| 67 | pADataValue = new_pADataValue.clone(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public Object clone() { |
| 72 | PAData new_pAData = new PAData(); |
| 73 | new_pAData.pADataType = pADataType; |
| 74 | if (pADataValue != null) { |
| 75 | new_pAData.pADataValue = new byte[pADataValue.length]; |
| 76 | System.arraycopy(pADataValue, 0, new_pAData.pADataValue, |
| 77 | 0, pADataValue.length); |
| 78 | } |
| 79 | return new_pAData; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Constructs a PAData object. |
| 84 | * @param encoding a Der-encoded data. |
| 85 | * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
| 86 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 87 | */ |
| 88 | public PAData(DerValue encoding) throws Asn1Exception, IOException { |
| 89 | DerValue der = null; |
| 90 | if (encoding.getTag() != DerValue.tag_Sequence) { |
| 91 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 92 | } |
| 93 | der = encoding.getData().getDerValue(); |
| 94 | if ((der.getTag() & 0x1F) == 0x01) { |
| 95 | this.pADataType = der.getData().getBigInteger().intValue(); |
| 96 | } |
| 97 | else |
| 98 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 99 | der = encoding.getData().getDerValue(); |
| 100 | if ((der.getTag() & 0x1F) == 0x02) { |
| 101 | this.pADataValue = der.getData().getOctetString(); |
| 102 | } |
| 103 | if (encoding.getData().available() > 0) |
| 104 | throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Encodes this object to an OutputStream. |
| 109 | * |
| 110 | * @return byte array of the encoded data. |
| 111 | * @exception IOException if an I/O error occurs while reading encoded data. |
| 112 | * @exception Asn1Exception on encoding errors. |
| 113 | */ |
| 114 | public byte[] asn1Encode() throws Asn1Exception, IOException { |
| 115 | |
| 116 | DerOutputStream bytes = new DerOutputStream(); |
| 117 | DerOutputStream temp = new DerOutputStream(); |
| 118 | |
| 119 | temp.putInteger(pADataType); |
| 120 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_PATYPE), temp); |
| 121 | temp = new DerOutputStream(); |
| 122 | temp.putOctetString(pADataValue); |
| 123 | bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_PAVALUE), temp); |
| 124 | |
| 125 | temp = new DerOutputStream(); |
| 126 | temp.write(DerValue.tag_Sequence, bytes); |
| 127 | return temp.toByteArray(); |
| 128 | } |
| 129 | |
| 130 | // accessor methods |
| 131 | public int getType() { |
| 132 | return pADataType; |
| 133 | } |
| 134 | |
| 135 | public byte[] getValue() { |
| 136 | return ((pADataValue == null) ? null : pADataValue.clone()); |
| 137 | } |
| 138 | } |