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

AW: [iaik-jce] CHOICE Implementation


sorry there was a problem with our mailing list. It should work now again.

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

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
    public 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
    public 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


     * 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
          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) {



Dieter Bratko

>  -----Ursprüngliche Nachricht-----
> Von: 	iaik-jce-owner@iaik.tu-graz.ac.at
> Gesendet:	Donnerstag, 14. Dezember 2000 13:09
> An:	iaik-jce@iaik.at
> Betreff:	[iaik-jce] CHOICE Implementation
> Hi,
> 	Has anyone done CHOICE implementation using IAIK ASN.1 Library. Any
sample or hints are highly appreciable. What happenned to the Mail-Archive.
I used to see many mails. But when I just tried to see whether this
information I can get from that I could hardly see 5 mails only in archive.
Are the old ones removed or some problem at my end? Regards
> ============================================
> P.K.B. Hari Gopal                Tel No:
> Senior Engineer                   Off: 3000401-2161 (extn)
> CMC Limited                       Res:3036834
> Gachibowli                          E-Mail:
> Hyderabad                          Off:hgopal@cmcltd.com
>                                           Per:hari_gopal@rocketmail.com
> ============================================
>  << Datei: Security Info - IAIK SMimeMapper.htm >>

Mailinglist-archive at http://jcewww.iaik.at/mailarchive/iaik-jce/jcethreads.html

To unsubscribe send an email to listserv@iaik.at with the folowing content: UNSUBSCRIBE iaik-jce