X0015_CertificateIdForFile.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 individual file (0x0015).
  23.  *
  24.  * <p>
  25.  * This field contains the information about which certificate in the PKCS#7 store was used to sign a particular file. It also contains the signature data. This
  26.  * field can appear multiple times, but can only appear once per certificate.
  27.  * </p>
  28.  *
  29.  * <p>
  30.  * Note: all fields stored in Intel low-byte/high-byte order.
  31.  * </p>
  32.  *
  33.  * <pre>
  34.  *         Value     Size     Description
  35.  *         -----     ----     -----------
  36.  * (CID)   0x0015    2 bytes  Tag for this "extra" block type
  37.  *         TSize     2 bytes  Size of data that follows
  38.  *         RCount    4 bytes  Number of recipients. (inferred)
  39.  *         HashAlg   2 bytes  Hash algorithm identifier. (inferred)
  40.  *         TData     TSize    Signature Data
  41.  * </pre>
  42.  *
  43.  * @NotThreadSafe
  44.  * @since 1.11
  45.  */
  46. public class X0015_CertificateIdForFile extends PKWareExtraHeader {

  47.     static final ZipShort HEADER_ID = new ZipShort(0x0015);

  48.     private int rcount;

  49.     private HashAlgorithm hashAlg;

  50.     public X0015_CertificateIdForFile() {
  51.         super(HEADER_ID);
  52.     }

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

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

  69.     @Override
  70.     public void parseFromCentralDirectoryData(final byte[] data, final int offset, final int length) throws ZipException {
  71.         assertMinimalLength(4, length);
  72.         super.parseFromCentralDirectoryData(data, offset, length);
  73.         this.rcount = ZipShort.getValue(data, offset);
  74.         this.hashAlg = HashAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + 2));
  75.     }
  76. }