[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [iaik-jce] ASN1 CHOICE class



The CHOICE type is used for selecting from a list of several alternatives.
The final tag is preticted form the selected type. There is no general
CHOICE type in IAIK-JCE since there is no general way to know in advance
what kind of types may be form the alternatives of a specific CHOICE
selection. When implementing a specific CHOICE type you know of, you may
write a particular class for this CHOICE type.
At least you will need:
-A constructor for setting the value of a specific choice representant (and
also the type if it cannot be non-ambiguous obtained from the value)
-A toASN1Object() method that gives an ASN.1 representation of the specific
CHOICE representant
-A second constructor that expects an ASN.1 object to be converted to Java
notation

Imagine, for instance, a CHOICE consisting of two alternatives, one of type
PrintableString, and the other of type OCTET_STRING:

 CHOICE ::= {
     type1 PrintableString
     type2 OCTET_STRING
 }

When implementing this CHOICE type, the first constructor may expect a Java
String value or a byte array value, depending on which type (PrintableString
or OCTET_STRING) the value represents. You may provide the type, too, or you
may derive it from the value. The toASN1Object() method will return either a
PrintableString or an OCTET_STRING, also depending on the value given when
creating the CHOICE object. And the second constructor which parses the
ASN.1 representation will expect a PrintableString or an OCTET_STRING.

The whole class representing such a CHOICE type may look like:

import iaik.asn1.*;

/**
 * Represents a ASN.1 CHOICE structure.
 * The following CHOICE structure is implemented:
 * <pre>
 * CHOICE ::= {
 *     type1 PrintableString
 *     type2 OCTET_STRING
 * }
 * </pre>
 */
public class ChoiceTest {

    // the value (String when PrintableString), byte[] when OCTET_STRING
    Object value = null;
    // the ASN.1 type, either PrintableString or OCTET_STRING
    ASN asn1Type = null;


    /**
     * Creates a new CHOICE object for given type and value.
     *
     * @param type the ASN.1 type, either PrintableString or OCTET_STRING
     * @param value the value
     *
     * @exception IllegalArgumentException if the type is not
PrintableString or OCTET_STRING
     */
    ChoiceTest(ASN type, Object value) throws IllegalArgumentException {
      if ((!type.equals(ASN.PrintableString)) &&
(!type.equals(ASN.OCTET_STRING))) {
        throw new IllegalArgumentException("Type " + type + " not valid for
this CHOICE!");
      }
      asn1Type = type;
      this.value = value;

    }

    /**
     * Creates a new CHOICE object from the given ASN.1 object.
     *
     * @param obj the ASN.1 object of type PrintableString or OCTET_STRING
     *
     * @exception CodingException if the type is not PrintableString or
OCTET_STRING
     */
    ChoiceTest(ASN1Object obj) throws CodingException {
      asn1Type = obj.getAsnType();

      if (asn1Type.equals(ASN.PrintableString)) {
         value = (String)((PrintableString)obj).getValue();
      } else if (asn1Type.equals(ASN.OCTET_STRING))

         value = (byte[])((OCTET_STRING)obj).getValue();
      } else {
         throw new CodingException("Type " + asn1Type + " not valid for this
CHOICE!");
      }


    }


    /**
     * Returns the ASN.1 representation of this CHOICE structure.
     * Depending on the inherent type, either a PrintableString or
     * a OCTET_STRING is returned.
     *
     * @return a PrintableString or OCTET_STRING
     * @exception CodingExceptin if an invalid type is requested
     */
    public ASN1Object toASN1Object() throws CodingException {
      ASN1Object obj = null;
      if (asn1Type.equals(ASN.PrintableString)) {
         obj = new PrintableString((String)value);
      } else if (asn1Type.equals(ASN.OCTET_STRING))

          obj = new OCTET_STRING((byte[])value);
      } else {
          // not really necessary if already ensured by the constructors
          throw new CodingException("Type " + asn1Type + " not valid for
this CHOICE!");
      }
      return obj;
    }

    /**
     * Performs a test.
     */
    public static void main(String[] args) {
       try {
          ChoiceTest cT = new ChoiceTest(ASN.PrintableString, "a test
String");
          ASN1Object obj = cT.toASN1Object();
          cT = new ChoiceTest(obj);

          cT = new ChoiceTest(ASN.OCTET_STRING, new byte[] {(byte)0x02 });
          obj = cT.toASN1Object();
          cT = new ChoiceTest(obj);

          cT = new ChoiceTest(ASN.IA5String, "a test String");

       } catch (Exception ex) {
          System.out.println(ex.getMessage());
       }

    }

}

Regards,
Dieter Bratko



----- Original Message -----
From: Domenico Aquilino <aquilino@dreamteam.it>
To: <iaik-jce@iaik.tu-graz.ac.at>
Sent: Monday, October 25, 1999 6:35 PM
Subject: [iaik-jce] ASN1 CHOICE class


> We need to define the ASN1 for an Object which needs CHOICE.
> We've not been able to find out how to do this with IAIK-JCE. The
> iaik.asn1.CHOICE class is missing, but We
> noticed CHOICE is used to define other asn1 objects (i.e.
> X509certificate).
> Is there something we are missing here?
>
> Regards
> Domenico
>
>
> --
> Mailinglist-archive at
http://jcewww.iaik.tu-graz.ac.at/mailarchive/iaik-jce/maillist.html
>
> To unsubscribe send an email to listserv@iaik.tu-graz.ac.at with the
folowing content: UNSUBSCRIBE iaik-jce
>
>
>


smime.p7s