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 package org.apache.harmony.security.x509;
     19 
     20 import java.io.IOException;
     21 import java.util.Collection;
     22 import java.util.List;
     23 import org.apache.harmony.security.asn1.ASN1SequenceOf;
     24 import org.apache.harmony.security.asn1.ASN1Type;
     25 import org.apache.harmony.security.asn1.BerInputStream;
     26 
     27 /**
     28  * The class encapsulates the ASN.1 DER encoding/decoding work
     29  * with the SubjectInfoAccessSyntax and AuthorityInfoAccessSyntax
     30  * which are a part of X.509 framework
     31  * (as specified in RFC 3280 -
     32  *  Internet X.509 Public Key Infrastructure.
     33  *  Certificate and Certificate Revocation List (CRL) Profile.
     34  *  http://www.ietf.org/rfc/rfc3280.txt):
     35  *
     36  *  SubjectInfoAccessSyntax  ::=
     37  *      SEQUENCE SIZE (1..MAX) OF AccessDescriptions
     38 
     39  *  AuthorityInfoAccessSyntax  ::=
     40  *      SEQUENCE SIZE (1..MAX) OF AccessDescriptions
     41  *
     42  *  AccessDescription  ::=  SEQUENCE {
     43  *      accessMethod          OBJECT IDENTIFIER,
     44  *      accessLocation        GeneralName  }
     45  *
     46  */
     47 public final class InfoAccessSyntax extends ExtensionValue {
     48     private final List<?> accessDescriptions;
     49 
     50     private InfoAccessSyntax(List<?> accessDescriptions, byte[] encoding) throws IOException {
     51         if (accessDescriptions == null || accessDescriptions.isEmpty()) {
     52             throw new IOException("AccessDescriptions list is null or empty");
     53         }
     54         this.accessDescriptions = accessDescriptions;
     55         this.encoding = encoding;
     56     }
     57 
     58     /**
     59      * Returns ASN.1 encoded form of this X.509 InfoAccessSyntax.
     60      */
     61     @Override public byte[] getEncoded() {
     62         if (encoding == null) {
     63             encoding = ASN1.encode(this);
     64         }
     65         return encoding;
     66     }
     67 
     68     public static InfoAccessSyntax decode(byte[] encoding) throws IOException {
     69         return ((InfoAccessSyntax) ASN1.decode(encoding));
     70     }
     71 
     72     @Override public String toString() {
     73         StringBuilder res = new StringBuilder();
     74         res.append("\n---- InfoAccessSyntax:");
     75         if (accessDescriptions != null) {
     76             for (Object accessDescription : accessDescriptions) {
     77                 res.append('\n');
     78                 res.append(accessDescription);
     79             }
     80         }
     81         res.append("\n---- InfoAccessSyntax END\n");
     82         return res.toString();
     83     }
     84 
     85     @Override public void dumpValue(StringBuilder sb, String prefix) {
     86         sb.append(prefix).append("AccessDescriptions:\n");
     87         if (accessDescriptions == null || accessDescriptions.isEmpty()) {
     88             sb.append("NULL\n");
     89         } else {
     90             for (Object accessDescription : accessDescriptions) {
     91                 sb.append(accessDescription.toString());
     92             }
     93         }
     94     }
     95 
     96 
     97     /**
     98      * ASN.1 DER X.509 AuthorityInfoAccessSyntax and SubjectInfoAccessSyntax
     99      * encoder/decoder class.
    100      */
    101     public static final ASN1Type ASN1 = new ASN1SequenceOf(AccessDescription.ASN1) {
    102         @Override public Object getDecodedObject(BerInputStream in) throws IOException {
    103             return new InfoAccessSyntax((List<?>) in.content, in.getEncoded());
    104         }
    105 
    106         @Override public Collection getValues(Object object) {
    107             return ((InfoAccessSyntax) object).accessDescriptions;
    108         }
    109     };
    110 
    111 }
    112