summaryrefslogtreecommitdiffstats
path: root/csarvalidation/src/main/java/org/onap/cvc/csar/security/CmsSignatureValidator.java
blob: 5d7b8793a64fe690786da5c5acdd1d49e1806d17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright 2019
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.onap.cvc.csar.security;

import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSSignerDigestMismatchException;
import org.bouncycastle.cms.jcajce.JcaSimpleSignerInfoVerifierBuilder;
import org.bouncycastle.operator.OperatorCreationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.cert.X509Certificate;
import java.util.Optional;

public class CmsSignatureValidator {

    private static final Logger LOG = LoggerFactory.getLogger(CmsSignatureValidator.class);

    public boolean verifySignedData(
            final byte[] cmsSignature,
            final Optional<byte[]> certificate,
            final byte[] fileContent) throws CmsSignatureValidatorException {

        try {
            CmsSignatureData signatureData = new CmsSignatureDataFactory().createForFirstSigner(cmsSignature, fileContent);
            if( signatureData.getCertificate().isEmpty() ) {
                signatureData.loadCertificate(certificate.orElseThrow(() -> new CmsSignatureValidatorException("No certificate found in cms signature and ETSI-Entry-Certificate doesn't exist")));
            }
            return verifySignedData(signatureData);
        } catch ( CmsSignatureLoadingException e) {
            throw new CmsSignatureValidatorException("Unexpected error occurred during signature validation!", e);
        }
    }

    public boolean verifySignedData(final CmsSignatureData signatureData) throws CmsSignatureValidatorException {
        try {
            X509Certificate certificate = signatureData.getCertificate().orElseThrow(() -> new CMSException("No certificate found in signature data!"));
            return signatureData.getSignerInformation().verify(new JcaSimpleSignerInfoVerifierBuilder().build(certificate));
        } catch (CMSSignerDigestMismatchException e){
            //message-digest attribute value does not match calculated value
            LOG.warn("CMS signer digest mismatch.", e);
            return false;
        }
        catch (OperatorCreationException | CMSException e) {
            throw new CmsSignatureValidatorException("Unexpected error occurred during signature validation!", e);
        }
    }

}