Home | History | Annotate | Download | only in x509
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /**
     19 * @author Alexander Y. Kleymenov
     20 * @version $Revision$
     21 */
     22 
     23 package org.apache.harmony.security.x509;
     24 
     25 import org.apache.harmony.security.asn1.ASN1Any;
     26 import org.apache.harmony.security.asn1.ASN1Explicit;
     27 import org.apache.harmony.security.asn1.ASN1Oid;
     28 import org.apache.harmony.security.asn1.ASN1Sequence;
     29 import org.apache.harmony.security.asn1.ASN1Type;
     30 import org.apache.harmony.security.asn1.BerInputStream;
     31 import org.apache.harmony.security.asn1.ObjectIdentifier;
     32 
     33 /**
     34  * The class encapsulates the ASN.1 DER encoding/decoding work
     35  * with OtherName structure which is a subpart of GeneralName
     36  * (as specified in RFC 3280 -
     37  *  Internet X.509 Public Key Infrastructure.
     38  *  Certificate and Certificate Revocation List (CRL) Profile.
     39  *  http://www.ietf.org/rfc/rfc3280.txt):
     40  *
     41  * <pre>
     42  *   OtherName ::= SEQUENCE {
     43  *        type-id    OBJECT IDENTIFIER,
     44  *        value      [0] EXPLICIT ANY DEFINED BY type-id
     45  *   }
     46  * </pre>
     47  */
     48 public final class OtherName {
     49     /** the value of typeID field of the structure */
     50     private String typeID;
     51     /** the value of value field of the structure */
     52     private byte[] value;
     53     /** the ASN.1 encoded form of OtherName */
     54     private byte[] encoding;
     55 
     56     public OtherName(String typeID, byte[] value) {
     57         this(typeID, value, null);
     58     }
     59 
     60     private OtherName(String typeID, byte[] value, byte[] encoding) {
     61         this.typeID = typeID;
     62         this.value = value;
     63         this.encoding = encoding;
     64     }
     65 
     66     /**
     67      * Returns the value of value field of the structure.
     68      */
     69     public byte[] getValue() {
     70         return value;
     71     }
     72 
     73     /**
     74      * Returns ASN.1 encoded form of this X.509 OtherName value.
     75      */
     76     public byte[] getEncoded() {
     77         if (encoding == null) {
     78             encoding = ASN1.encode(this);
     79         }
     80         return encoding;
     81     }
     82 
     83     /**
     84      * ASN.1 DER X.509 OtherName encoder/decoder class.
     85      */
     86     public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
     87             ASN1Oid.getInstance(),
     88             new ASN1Explicit(0, ASN1Any.getInstance()) }) {
     89 
     90         @Override protected Object getDecodedObject(BerInputStream in) {
     91             Object[] values = (Object[]) in.content;
     92             return new OtherName(ObjectIdentifier.toString((int[]) values[0]),
     93                     (byte[]) values[1], in.getEncoded());
     94         }
     95 
     96         @Override protected void getValues(Object object, Object[] values) {
     97             OtherName on = (OtherName) object;
     98             values[0] = ObjectIdentifier.toIntArray(on.typeID);
     99             values[1] = on.value;
    100         }
    101     };
    102 }
    103 
    104