Search in sources :

Example 1 with SelectionType

use of com.intel.mtwilson.tag.selection.xml.SelectionType in project OpenAttestation by OpenAttestation.

the class TagSelectionJsonTest method printSelections.

private void printSelections(SelectionsType selections) {
    if (selections.getSelection() != null) {
        log.debug("SELECTIONS");
        List<SelectionType> selectionList = selections.getSelection();
        for (SelectionType selection : selectionList) {
            printSelection(selection);
        }
    }
    if (selections.getDefault() != null) {
        log.debug("DEFAULT");
        List<SelectionType> defaultSelectionList = selections.getDefault().getSelection();
        for (SelectionType selection : defaultSelectionList) {
            printSelection(selection);
        }
    }
    if (selections.getOptions() != null) {
        log.debug("OPTIONS");
        if (selections.getOptions().getCache() != null) {
            log.debug("cache mode {}", (selections.getOptions().getCache().getMode() == null ? "null" : selections.getOptions().getCache().getMode().value()));
        }
    }
}
Also used : SelectionType(com.intel.mtwilson.tag.selection.xml.SelectionType)

Example 2 with SelectionType

use of com.intel.mtwilson.tag.selection.xml.SelectionType in project OpenAttestation by OpenAttestation.

the class SelectionUtil method copySelectionsValidOn.

/**
     * Returns a new SelectionsType instance with only the selections and
     * default selections that are
     * valid today. The options section is copied to the new selections instance
     * for convenience.
     * The copy is a "shallow" copy - the copy refers to the same selections,
     * default selections, and options in the input.
     * To make a "deep" copy you can first serialize the selections, then
     * de-serialize into a new deep copy, and then call this method with the 
     * deep copy as input.
     */
public static SelectionsType copySelectionsValidOn(SelectionsType selections, Date validOn) {
    GregorianCalendar today = new GregorianCalendar();
    // http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html
    today.setTime(validOn);
    SelectionsType copy = new SelectionsType();
    for (SelectionType selection : selections.getSelection()) {
        // skip if the selection is not currently valid (notBefore<today<notAfter) ; if notBefore or notAfter are not defined, then validity is assumed
        if (selection.getNotBefore() != null && today.before(selection.getNotBefore().toGregorianCalendar())) {
            log.debug("skipping selection because of notBefore date {}", selection.getNotBefore().toString());
            continue;
        }
        if (selection.getNotAfter() != null && today.after(selection.getNotAfter().toGregorianCalendar())) {
            log.debug("skipping selection because of notAfter date {}", selection.getNotAfter().toString());
            continue;
        }
        copy.getSelection().add(selection);
    }
    // repeat the same action for the default selections
    if (selections.getDefault() != null) {
        copy.setDefault(new DefaultType());
        for (SelectionType selection : selections.getDefault().getSelection()) {
            // skip if the selection is not currently valid (notBefore<today<notAfter) ; if notBefore or notAfter are not defined, then validity is assumed
            if (selection.getNotBefore() != null && today.before(selection.getNotBefore().toGregorianCalendar())) {
                log.debug("skipping default selection because of notBefore date {}", selection.getNotBefore().toString());
                continue;
            }
            if (selection.getNotAfter() != null && today.after(selection.getNotAfter().toGregorianCalendar())) {
                log.debug("skipping default selection because of notAfter date {}", selection.getNotAfter().toString());
                continue;
            }
            copy.getDefault().getSelection().add(selection);
        }
    }
    // copy the options
    copy.setOptions(selections.getOptions());
    return copy;
}
Also used : SelectionsType(com.intel.mtwilson.tag.selection.xml.SelectionsType) SelectionType(com.intel.mtwilson.tag.selection.xml.SelectionType) DefaultType(com.intel.mtwilson.tag.selection.xml.DefaultType) GregorianCalendar(java.util.GregorianCalendar)

Example 3 with SelectionType

use of com.intel.mtwilson.tag.selection.xml.SelectionType in project OpenAttestation by OpenAttestation.

the class ProvisionTagCertificate method createOne.

//    
//    /**
//     * Returns the tag certificate bytes or null if one was not generated
//     * 
//     * @param subject
//     * @param selection may be null; the default selection will be used, if configured
//     * @param request
//     * @param response
//     * @return
//     * @throws IOException
//     */
public Certificate createOne(String subject, SelectionsType selections, HttpServletRequest request, HttpServletResponse response) throws IOException, ApiException, SignatureException, SQLException, IllegalArgumentException {
    //        TagConfiguration configuration = new TagConfiguration(My.configuration().getConfiguration());
    //        TagCertificateAuthority ca = new TagCertificateAuthority(configuration);
    TagConfiguration configuration = new TagConfiguration(ASConfig.getConfiguration());
    TagCertificateAuthority ca = new TagCertificateAuthority(configuration);
    // if the subject is an ip address or hostname, resolve it to a hardware uuid with mtwilson - if the host isn't registered in mtwilson we can't get the hardware uuid so we have to reject the request
    if (!UUID.isValid(subject)) {
        String subjectUuid = findSubjectHardwareUuid(subject);
        if (subjectUuid == null) {
            log.error("Cannot find hardware uuid for subject: {}", subject);
            throw new IllegalArgumentException("Invalid subject specified in the call");
        }
        subject = subjectUuid;
    }
    if (selections == null) {
        log.error("Selection input is null");
        throw new IllegalArgumentException("Invalid selections specified.");
    }
    // if external ca is configured then we only save the request to the database and indicate async processing in our response
    //        if( configuration.isTagProvisionExternal() || isAsync(request) ) {
    //            // requires async processing - we store the request, and an external ca will poll for requests, generate certs, and post the certs back to us; the client can periodically check the status and then download the cert when it's available
    //            storeAsyncRequest(subject, selections, response);
    //            return null;
    //        }
    // if always-generate/no-cache (cache mode off) is enabled then generate it right now and return it - no need to check database for existing certs etc. 
    String cacheMode = "on";
    if (selections.getOptions() != null && selections.getOptions().getCache() != null && selections.getOptions().getCache().getMode() != null) {
        cacheMode = selections.getOptions().getCache().getMode().value();
    }
    // first figure out which selection will be used for the given subject - also filters selections to ones that are currently valid or not marked with validity period
    // throws exception if there is no matching selection and no matching default selection
    SelectionType targetSelection = ca.findCurrentSelectionForSubject(UUID.valueOf(subject), selections);
    log.debug("Cache mode {}", cacheMode);
    if ("off".equals(cacheMode) && targetSelection != null) {
        byte[] certificateBytes = ca.createTagCertificate(UUID.valueOf(subject), targetSelection);
        Certificate certificate = storeTagCertificate(subject, certificateBytes);
        return certificate;
    }
    // if there is an existing currently valid certificate we return it
    CertificateFilterCriteria criteria = new CertificateFilterCriteria();
    criteria.subjectEqualTo = subject;
    criteria.revoked = false;
    criteria.validOn = new Iso8601Date(new Date());
    CertificateCollection results = certificateRepository.search(criteria);
    Date today = new Date();
    Certificate latestCert = null;
    BigInteger latestCreateTime = BigInteger.ZERO;
    //  pick the most recently created cert that is currently valid and has the same attributes specified in the selection.  we evaluate the notBefore and notAfter fields of the certificate itself even though we already narrowed the search to currently valid certs using the search criteria. 
    if (!results.getCertificates().isEmpty()) {
        for (Certificate certificate : results.getCertificates()) {
            X509AttributeCertificate attributeCertificate = X509AttributeCertificate.valueOf(certificate.getCertificate());
            if (today.before(attributeCertificate.getNotBefore())) {
                continue;
            }
            if (today.after(attributeCertificate.getNotAfter())) {
                continue;
            }
            if (targetSelection != null && !certificateAttributesEqual(attributeCertificate, targetSelection)) {
                continue;
            }
            // And here we want to return the latest certificate so we keep track as we look through the results.
            if (latestCreateTime.compareTo(attributeCertificate.getSerialNumber()) <= 0) {
                latestCreateTime = attributeCertificate.getSerialNumber();
                latestCert = certificate;
            }
        }
    }
    // Check if a valid certificate was found during the search.
    if (latestCert != null) {
        X509AttributeCertificate attributeCertificate = X509AttributeCertificate.valueOf(latestCert.getCertificate());
        AssetTagCertAssociateRequest atca = new AssetTagCertAssociateRequest();
        atca.setSha1OfAssetCert(Sha1Digest.digestOf(attributeCertificate.getEncoded()).toByteArray());
        AssetTagCertBO object = new AssetTagCertBO();
        try {
            object.mapAssetTagCertToHost(atca);
        } catch (CryptographyException ex) {
            java.util.logging.Logger.getLogger(ProvisionTagCertificate.class.getName()).log(Level.SEVERE, null, ex);
        }
        //            ca.mapTagCertificate(UUID.valueOf(subject), attributeCertificate.);
        return latestCert;
    }
    // no cached certificate so generate a new certificate
    if (targetSelection == null) {
        throw new IllegalArgumentException("No cached certificate and no default selection provided");
    }
    byte[] certificateBytes = ca.createTagCertificate(UUID.valueOf(subject), targetSelection);
    Certificate certificate = storeTagCertificate(subject, certificateBytes);
    return certificate;
}
Also used : CertificateCollection(com.intel.mtwilson.datatypes.CertificateCollection) AssetTagCertBO(com.intel.mtwilson.as.business.AssetTagCertBO) X509AttributeCertificate(com.intel.mtwilson.datatypes.X509AttributeCertificate) Date(java.util.Date) Iso8601Date(com.intel.mtwilson.util.io.Iso8601Date) TagConfiguration(com.intel.mtwilson.tag.TagConfiguration) CryptographyException(com.intel.mtwilson.crypto.CryptographyException) TagCertificateAuthority(com.intel.mtwilson.tag.TagCertificateAuthority) SelectionType(com.intel.mtwilson.tag.selection.xml.SelectionType) CertificateFilterCriteria(com.intel.mtwilson.datatypes.CertificateFilterCriteria) BigInteger(java.math.BigInteger) Iso8601Date(com.intel.mtwilson.util.io.Iso8601Date) Certificate(com.intel.mtwilson.datatypes.Certificate) X509AttributeCertificate(com.intel.mtwilson.datatypes.X509AttributeCertificate) AssetTagCertAssociateRequest(com.intel.mtwilson.datatypes.AssetTagCertAssociateRequest)

Aggregations

SelectionType (com.intel.mtwilson.tag.selection.xml.SelectionType)3 AssetTagCertBO (com.intel.mtwilson.as.business.AssetTagCertBO)1 CryptographyException (com.intel.mtwilson.crypto.CryptographyException)1 AssetTagCertAssociateRequest (com.intel.mtwilson.datatypes.AssetTagCertAssociateRequest)1 Certificate (com.intel.mtwilson.datatypes.Certificate)1 CertificateCollection (com.intel.mtwilson.datatypes.CertificateCollection)1 CertificateFilterCriteria (com.intel.mtwilson.datatypes.CertificateFilterCriteria)1 X509AttributeCertificate (com.intel.mtwilson.datatypes.X509AttributeCertificate)1 TagCertificateAuthority (com.intel.mtwilson.tag.TagCertificateAuthority)1 TagConfiguration (com.intel.mtwilson.tag.TagConfiguration)1 DefaultType (com.intel.mtwilson.tag.selection.xml.DefaultType)1 SelectionsType (com.intel.mtwilson.tag.selection.xml.SelectionsType)1 Iso8601Date (com.intel.mtwilson.util.io.Iso8601Date)1 BigInteger (java.math.BigInteger)1 Date (java.util.Date)1 GregorianCalendar (java.util.GregorianCalendar)1