001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.archivers.zip;
020
021import java.util.zip.ZipException;
022
023/**
024 * X.509 Certificate ID and Signature for individual file (0x0015).
025 *
026 * <p>
027 * 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
028 * field can appear multiple times, but can only appear once per certificate.
029 * </p>
030 *
031 * <p>
032 * Note: all fields stored in Intel low-byte/high-byte order.
033 * </p>
034 *
035 * <pre>
036 *         Value     Size     Description
037 *         -----     ----     -----------
038 * (CID)   0x0015    2 bytes  Tag for this "extra" block type
039 *         TSize     2 bytes  Size of data that follows
040 *         RCount    4 bytes  Number of recipients. (inferred)
041 *         HashAlg   2 bytes  Hash algorithm identifier. (inferred)
042 *         TData     TSize    Signature Data
043 * </pre>
044 *
045 * @NotThreadSafe
046 * @since 1.11
047 */
048public class X0015_CertificateIdForFile extends PKWareExtraHeader {
049
050    static final ZipShort HEADER_ID = new ZipShort(0x0015);
051
052    private int rcount;
053
054    private HashAlgorithm hashAlg;
055
056    /**
057     * Constructs a new instance.
058     */
059    public X0015_CertificateIdForFile() {
060        super(HEADER_ID);
061    }
062
063    /**
064     * Gets hash algorithm.
065     *
066     * @return the hash algorithm
067     */
068    public HashAlgorithm getHashAlgorithm() {
069        return hashAlg;
070    }
071
072    /**
073     * Gets record count.
074     *
075     * @return the record count
076     */
077    public int getRecordCount() {
078        return rcount;
079    }
080
081    @Override
082    public void parseFromCentralDirectoryData(final byte[] data, final int offset, final int length) throws ZipException {
083        assertMinimalLength(4, length);
084        super.parseFromCentralDirectoryData(data, offset, length);
085        this.rcount = ZipShort.getValue(data, offset);
086        this.hashAlg = HashAlgorithm.getAlgorithmByCode(ZipShort.getValue(data, offset + 2));
087    }
088}