jdk/src/share/classes/sun/security/krb5/internal/APReq.java
changeset 73 cf334423502b
parent 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
72:f24a98c3df49 73:cf334423502b
    52  * This definition reflects the Network Working Group RFC 4120
    52  * This definition reflects the Network Working Group RFC 4120
    53  * specification available at
    53  * specification available at
    54  * <a href="http://www.ietf.org/rfc/rfc4120.txt">
    54  * <a href="http://www.ietf.org/rfc/rfc4120.txt">
    55  * http://www.ietf.org/rfc/rfc4120.txt</a>.
    55  * http://www.ietf.org/rfc/rfc4120.txt</a>.
    56  */
    56  */
       
    57 public class APReq {
    57 
    58 
    58 public class APReq {
    59     public int pvno;
    59         public int pvno;
    60     public int msgType;
    60         public int msgType;
    61     public APOptions apOptions;
    61         public APOptions apOptions;
    62     public Ticket ticket;
    62         public Ticket ticket;
    63     public EncryptedData authenticator;
    63         public EncryptedData authenticator;
       
    64 
    64 
    65         public APReq(
    65     public APReq(
    66                 APOptions new_apOptions,
    66             APOptions new_apOptions,
    67                 Ticket new_ticket,
    67             Ticket new_ticket,
    68                 EncryptedData new_authenticator
    68             EncryptedData new_authenticator) {
    69         ) {
    69         pvno = Krb5.PVNO;
    70                 pvno = Krb5.PVNO;
    70         msgType = Krb5.KRB_AP_REQ;
    71                 msgType = Krb5.KRB_AP_REQ;
    71         apOptions = new_apOptions;
    72                 apOptions = new_apOptions;
    72         ticket = new_ticket;
    73                 ticket = new_ticket;
    73         authenticator = new_authenticator;
    74                 authenticator = new_authenticator;
    74     }
    75         }
       
    76 
    75 
    77         public APReq(byte[] data) throws Asn1Exception,IOException, KrbApErrException, RealmException {
    76     public APReq(byte[] data) throws Asn1Exception, IOException, KrbApErrException, RealmException {
    78         init(new DerValue(data));
    77         init(new DerValue(data));
    79         }
    78     }
    80 
    79 
    81     public APReq(DerValue encoding) throws Asn1Exception, IOException, KrbApErrException, RealmException {
    80     public APReq(DerValue encoding) throws Asn1Exception, IOException, KrbApErrException, RealmException {
    82                 init(encoding);
    81         init(encoding);
       
    82     }
       
    83 
       
    84     /**
       
    85      * Initializes an APReq object.
       
    86      * @param encoding a single DER-encoded value.
       
    87      * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
       
    88      * @exception IOException if an I/O error occurs while reading encoded data.
       
    89      * @exception KrbApErrException if the value read from the DER-encoded data stream does not match the pre-defined value.
       
    90      * @exception RealmException if an error occurs while parsing a Realm object.
       
    91      */
       
    92     private void init(DerValue encoding) throws Asn1Exception,
       
    93             IOException, KrbApErrException, RealmException {
       
    94         DerValue der, subDer;
       
    95         if (((encoding.getTag() & (byte) 0x1F) != Krb5.KRB_AP_REQ)
       
    96                 || (encoding.isApplication() != true)
       
    97                 || (encoding.isConstructed() != true)) {
       
    98             throw new Asn1Exception(Krb5.ASN1_BAD_ID);
    83         }
    99         }
       
   100         der = encoding.getData().getDerValue();
       
   101         if (der.getTag() != DerValue.tag_Sequence) {
       
   102             throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   103         }
       
   104         subDer = der.getData().getDerValue();
       
   105         if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x00) {
       
   106             throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   107         }
       
   108         pvno = subDer.getData().getBigInteger().intValue();
       
   109         if (pvno != Krb5.PVNO) {
       
   110             throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
       
   111         }
       
   112         subDer = der.getData().getDerValue();
       
   113         if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x01) {
       
   114             throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   115         }
       
   116         msgType = subDer.getData().getBigInteger().intValue();
       
   117         if (msgType != Krb5.KRB_AP_REQ) {
       
   118             throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
       
   119         }
       
   120         apOptions = APOptions.parse(der.getData(), (byte) 0x02, false);
       
   121         ticket = Ticket.parse(der.getData(), (byte) 0x03, false);
       
   122         authenticator = EncryptedData.parse(der.getData(), (byte) 0x04, false);
       
   123         if (der.getData().available() > 0) {
       
   124             throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   125         }
       
   126     }
    84 
   127 
    85         /**
   128     /**
    86          * Initializes an APReq object.
   129      * Encodes an APReq object.
    87          * @param encoding a single DER-encoded value.
   130      * @return byte array of encoded APReq object.
    88          * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
   131      * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
    89          * @exception IOException if an I/O error occurs while reading encoded data.
   132      * @exception IOException if an I/O error occurs while reading encoded data.
    90          * @exception KrbApErrException if the value read from the DER-encoded data stream does not match the pre-defined value.
   133      */
    91          * @exception RealmException if an error occurs while parsing a Realm object.
   134     public byte[] asn1Encode() throws Asn1Exception, IOException {
    92          */
       
    93         private void init(DerValue encoding) throws Asn1Exception,
       
    94            IOException, KrbApErrException, RealmException {
       
    95                 DerValue der, subDer;
       
    96         if (((encoding.getTag() & (byte)0x1F) != Krb5.KRB_AP_REQ)
       
    97                         || (encoding.isApplication() != true)
       
    98                         || (encoding.isConstructed() != true))
       
    99                         throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   100                 der = encoding.getData().getDerValue();
       
   101                 if (der.getTag() != DerValue.tag_Sequence)
       
   102                    throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   103                 subDer = der.getData().getDerValue();
       
   104         if ((subDer.getTag() & (byte)0x1F) != (byte)0x00)
       
   105             throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   106         pvno = subDer.getData().getBigInteger().intValue();
       
   107         if (pvno != Krb5.PVNO)
       
   108                                 throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
       
   109                 subDer = der.getData().getDerValue();
       
   110                 if ((subDer.getTag() & (byte)0x1F) != (byte)0x01)
       
   111                         throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   112                 msgType = subDer.getData().getBigInteger().intValue();
       
   113                 if (msgType != Krb5.KRB_AP_REQ)
       
   114                          throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
       
   115                 apOptions = APOptions.parse(der.getData(), (byte)0x02, false);
       
   116                 ticket = Ticket.parse(der.getData(), (byte)0x03, false);
       
   117                 authenticator = EncryptedData.parse(der.getData(), (byte)0x04, false);
       
   118                 if (der.getData().available() > 0)
       
   119                         throw new Asn1Exception(Krb5.ASN1_BAD_ID);
       
   120         }
       
   121 
       
   122         /**
       
   123          * Encodes an APReq object.
       
   124          * @return byte array of encoded APReq object.
       
   125          * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
       
   126          * @exception IOException if an I/O error occurs while reading encoded data.
       
   127          */
       
   128         public byte[] asn1Encode() throws Asn1Exception, IOException {
       
   129         DerOutputStream bytes = new DerOutputStream();
   135         DerOutputStream bytes = new DerOutputStream();
   130             DerOutputStream temp = new DerOutputStream();
   136         DerOutputStream temp = new DerOutputStream();
   131                 temp.putInteger(BigInteger.valueOf(pvno));
   137         temp.putInteger(BigInteger.valueOf(pvno));
   132                 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
   138         bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);
   133                 temp = new DerOutputStream();
   139         temp = new DerOutputStream();
   134                 temp.putInteger(BigInteger.valueOf(msgType));
   140         temp.putInteger(BigInteger.valueOf(msgType));
   135                 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
   141         bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);
   136                 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), apOptions.asn1Encode());
   142         bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), apOptions.asn1Encode());
   137                 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), ticket.asn1Encode());
   143         bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x03), ticket.asn1Encode());
   138                 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), authenticator.asn1Encode());
   144         bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x04), authenticator.asn1Encode());
   139             temp = new DerOutputStream();
   145         temp = new DerOutputStream();
   140                 temp.write(DerValue.tag_Sequence, bytes);
   146         temp.write(DerValue.tag_Sequence, bytes);
   141                 DerOutputStream apreq = new DerOutputStream();
   147         DerOutputStream apreq = new DerOutputStream();
   142                 apreq.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x0E), temp);
   148         apreq.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x0E), temp);
   143                 return apreq.toByteArray();
   149         return apreq.toByteArray();
   144 
   150     }
   145         }
       
   146 
       
   147 }
   151 }