X0016_CertificateIdForCentralDirectory.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.compress.archivers.zip;

  20. import java.util.zip.ZipException;

  21. /**
  22.  * X.509 Certificate ID and Signature for central directory (0x0016).
  23.  *
  24.  * <p>
  25.  * This field contains the information about which certificate in the PKCS#7 store was used to sign the central directory structure. When the Central Directory
  26.  * Encryption feature is enabled for a ZIP file, this record will appear in the Archive Extra Data Record, otherwise it will appear in the first central
  27.  * directory record.
  28.  * </p>
  29.  *
  30.  * <p>
  31.  * Note: all fields stored in Intel low-byte/high-byte order.
  32.  * </p>
  33.  *
  34.  * <pre>
  35.  *         Value     Size     Description
  36.  *         -----     ----     -----------
  37.  * (CDID)  0x0016    2 bytes  Tag for this "extra" block type
  38.  *         TSize     2 bytes  Size of data that follows
  39.  *         RCount    4 bytes  Number of recipients. (inferred)
  40.  *         HashAlg   2 bytes  Hash algorithm identifier. (inferred)
  41.  *         TData     TSize    Data
  42.  * </pre>
  43.  *
  44.  * @NotThreadSafe
  45.  * @since 1.11
  46.  */
  47. public class X0016_CertificateIdForCentralDirectory extends PKWareExtraHeader {

  48.     static final ZipShort HEADER_ID = new ZipShort(0x0016);

  49.     private int rcount;

  50.     private HashAlgorithm hashAlg;

  51.     public X0016_CertificateIdForCentralDirectory() {
  52.         super(HEADER_ID);
  53.     }

  54.     /**
  55.      * Gets hash algorithm.
  56.      *
  57.      * @return the hash algorithm
  58.      */
  59.     public HashAlgorithm getHashAlgorithm() {
  60.         return hashAlg;
  61.     }

  62.     /**
  63.      * Gets record count.
  64.      *
  65.      * @return the record count
  66.      */
  67.     public int getRecordCount() {
  68.         return rcount;
  69.     }

  70.     @Override
  71.     public void parseFromCentralDirectoryData(final byte[] data, final int offset, final int length) throws ZipException {
  72.         assertMinimalLength(4, length);
  73.         // TODO: double check we really do not want to call super here
  74.         this.rcount = ZipShort.getValue(data, offset);
  75.         this.hashAlg = HashAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + 2));
  76.     }
  77. }