com.helger.asic.ManifestVerifier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-asic Show documentation
Show all versions of ph-asic Show documentation
Generic implementation of ASiC-E archives in accordance with ETSI 102 918 v1.3.1.
/**
* Copyright (C) 2015-2017 difi (www.difi.no)
* Copyright (C) 2018 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed
* with this file, You can obtain one at
* https://mozilla.org/MPL/2.0/
*/
package com.helger.asic;
import java.io.Serializable;
import java.util.Arrays;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.helger.asic.jaxb.asic.AsicFile;
import com.helger.asic.jaxb.asic.AsicManifest;
import com.helger.asic.jaxb.asic.Certificate;
import com.helger.commons.collection.impl.CommonsHashMap;
import com.helger.commons.collection.impl.ICommonsMap;
public class ManifestVerifier implements Serializable
{
private final EMessageDigestAlgorithm m_eReferenceMD;
private final AsicManifest m_aAsicManifest = new AsicManifest ();
private final ICommonsMap m_aAsicManifestMap = new CommonsHashMap <> ();
public ManifestVerifier (@Nullable final EMessageDigestAlgorithm eReferenceMD)
{
m_eReferenceMD = eReferenceMD;
}
public void update (@Nonnull final String sFilename,
@Nonnull final byte [] aDigest,
@Nullable final String sSigReference)
{
update (sFilename, null, aDigest, null, sSigReference);
}
public void update (@Nonnull final String sFilename,
@Nullable final String sMimeType,
@Nonnull final byte [] aDigest,
@Nullable final String sDigestAlgorithm,
@Nullable final String sSigReference)
{
if (m_eReferenceMD != null && sDigestAlgorithm != null && !sDigestAlgorithm.equals (m_eReferenceMD.getUri ()))
throw new IllegalStateException ("Wrong digest method for file " + sFilename + ": " + sDigestAlgorithm);
AsicFile aAsicFile = m_aAsicManifestMap.get (sFilename);
if (aAsicFile == null)
{
aAsicFile = new AsicFile ();
aAsicFile.setName (sFilename);
aAsicFile.setDigest (aDigest);
aAsicFile.setVerified (false);
m_aAsicManifest.getFile ().add (aAsicFile);
m_aAsicManifestMap.put (sFilename, aAsicFile);
}
else
{
if (!Arrays.equals (aAsicFile.getDigest (), aDigest))
throw new IllegalStateException ("Mismatching digest for file " + sFilename);
aAsicFile.setVerified (true);
}
if (sMimeType != null)
aAsicFile.setMimetype (sMimeType);
if (sSigReference != null)
aAsicFile.getCertRef ().add (sSigReference);
}
public void addCertificate (@Nonnull final Certificate aCertificate)
{
m_aAsicManifest.addCertificate (aCertificate);
}
public void setRootFilename (final String sFilename)
{
m_aAsicManifest.setRootfile (sFilename);
}
public void verifyAllVerified () throws IllegalStateException
{
for (final AsicFile aAsicFile : m_aAsicManifest.getFile ())
if (!aAsicFile.isVerified ())
throw new IllegalStateException ("File not verified: " + aAsicFile.getName ());
}
@Nonnull
public AsicManifest getAsicManifest ()
{
return m_aAsicManifest;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy