Search in sources :

Example 6 with MwAssetTagCertificateJpaController

use of com.intel.mtwilson.as.controller.MwAssetTagCertificateJpaController in project OpenAttestation by OpenAttestation.

the class AssetTagCertBO method mapAssetTagCertToHost.

/**
     * This function would be used to associate a asset tag certificate with the host for which it is 
     * provisioned for.  It does not require you know the ID of the host you are associating to.  
     * Here you are giving the hash of the cert to the code and letting it find a matching host
     * @param atagObj
     * @return true if host was found, false if not
     */
public boolean mapAssetTagCertToHost(AssetTagCertAssociateRequest atagObj) throws CryptographyException {
    boolean result = false;
    log.debug("mapAssetTagCertToHost");
    AssetTagCertAssociateRequest request = new AssetTagCertAssociateRequest();
    if (atagObj.getSha1OfAssetCert() != null) {
        log.debug("trying to associate tag to existing host using " + Hex.encodeHexString(atagObj.getSha1OfAssetCert()));
        //List<MwAssetTagCertificate> atagCerts = My.jpa().mwAssetTagCertificate().findAssetTagCertificateBySha1Hash(atagObj.getSha1OfAssetCert());
        MwAssetTagCertificateJpaController mwAssetTagCertificateJpaController = new MwAssetTagCertificateJpaController(getEntityManagerFactory());
        List<MwAssetTagCertificate> atagCerts = mwAssetTagCertificateJpaController.findAssetTagCertificateBySha1Hash(atagObj.getSha1OfAssetCert());
        // List<MwAssetTagCertificate> atagCerts = My.jpa().mwAssetTagCertificate().findAssetTagCertificatesByHostUUID("494cb5dc-a3e1-4e46-9b52-e694349b1654");
        if (atagCerts.isEmpty()) {
            log.error("mapAssetTagCertToHost: The asset tag certificate does not exist");
            throw new ASException(ErrorCode.AS_INVALID_ASSET_TAG_CERTIFICATE_HASH);
        } else if (atagCerts.size() > 1) {
            log.error("mapAssetTagCertToHost: There were multiple matches for the specified hash");
            throw new ASException(ErrorCode.AS_INVALID_ASSET_TAG_CERTIFICATE_HASH);
        } else {
            MwAssetTagCertificate atagCert = atagCerts.get(0);
            request.setSha1OfAssetCert(atagCert.getSHA1Hash());
            String uuid = atagCert.getUuid().toLowerCase().trim();
            log.debug("searching using " + uuid);
            //TblHosts tblHost = My.jpa().mwHosts().findByHwUUID(uuid);
            TblHostsJpaController tblHostsJpaController = new TblHostsJpaController(getEntityManagerFactory());
            TblHosts tblHost = tblHostsJpaController.findByHwUUID(uuid);
            if (tblHost != null) {
                log.debug("found host matching uuid of cert, going to assoicate with host id = " + tblHost.getId());
                request.setHostID(tblHost.getId());
                //atagObj.setHostID(tblHost.getId());
                result = mapAssetTagCertToHostById(request);
            } else {
                log.debug("found no matching uuid of cert");
                result = false;
            }
        }
    }
    return result;
}
Also used : TblHostsJpaController(com.intel.mtwilson.as.controller.TblHostsJpaController) TblHosts(com.intel.mtwilson.as.data.TblHosts) MwAssetTagCertificateJpaController(com.intel.mtwilson.as.controller.MwAssetTagCertificateJpaController) MwAssetTagCertificate(com.intel.mtwilson.as.data.MwAssetTagCertificate) ASException(com.intel.mountwilson.as.common.ASException) AssetTagCertAssociateRequest(com.intel.mtwilson.datatypes.AssetTagCertAssociateRequest)

Example 7 with MwAssetTagCertificateJpaController

use of com.intel.mtwilson.as.controller.MwAssetTagCertificateJpaController in project OpenAttestation by OpenAttestation.

the class AssetTagCertBO method mapAssetTagCertToHostById.

/**
     * This function would be used to associate a asset tag certificate with the host for which it is 
     * provisioned for.  It requires you know the ID of the host it is to be associated with 
     * @param atagObj
     * @return 
     */
public boolean mapAssetTagCertToHostById(AssetTagCertAssociateRequest atagObj) {
    boolean result;
    log.debug("mapAssetTagCertToHostById");
    // Before we map the asset tag cert to the host, we first need to unmap any associations if it already exists
    try {
        unmapAssetTagCertFromHostById(atagObj);
        log.debug("Successfully unmapped the asset tag certificate assocation with host {}. ", atagObj.getHostID());
    } catch (Exception ex) {
        log.error("Error during unmap of asset tag cert from host with id {}. {}", atagObj.getHostID(), ex.getMessage());
    }
    try {
        // Find the asset tag certificate for the specified Sha256Hash value
        if (atagObj.getSha1OfAssetCert() != null) {
            //List<MwAssetTagCertificate> atagCerts = My.jpa().mwAssetTagCertificate().findAssetTagCertificateBySha1Hash(atagObj.getSha1OfAssetCert());
            MwAssetTagCertificateJpaController mwAssetTagCertificateJpaController = new MwAssetTagCertificateJpaController(getEntityManagerFactory());
            List<MwAssetTagCertificate> atagCerts = mwAssetTagCertificateJpaController.findAssetTagCertificateBySha1Hash(atagObj.getSha1OfAssetCert());
            // List<MwAssetTagCertificate> atagCerts = My.jpa().mwAssetTagCertificate().findAssetTagCertificatesByHostUUID("494cb5dc-a3e1-4e46-9b52-e694349b1654");
            if (atagCerts.isEmpty()) {
                log.error("mapAssetTagCertToHostById: The asset tag certificate does not exist");
                throw new ASException(ErrorCode.AS_INVALID_ASSET_TAG_CERTIFICATE_HASH);
            } else if (atagCerts.size() > 1) {
                log.error("mapAssetTagCertToHostById: There were multiple matches for the specified hash");
                throw new ASException(ErrorCode.AS_INVALID_ASSET_TAG_CERTIFICATE_HASH);
            } else {
                // Now that we have the asset tag identified, let us update the entry with the host ID for which it has
                // to be associated.
                MwAssetTagCertificate atagCert = atagCerts.get(0);
                atagCert.setHostID(atagObj.getHostID());
                // Now that the mapping is done, we need to calculate what the expected PCR value should be and put it in
                // the PCREvent column.
                Sha1Digest tag = Sha1Digest.digestOf(atagCert.getCertificate());
                log.debug("mapAssetTagCertToHostById : Sha1 Hash of the certificate with UUID {} is {}.", atagCert.getUuid(), tag.toString());
                Sha1Digest expectedHash = Sha1Digest.ZERO.extend(tag);
                log.debug("mapAssetTagCertToHostById : Final expected PCR for the certificate with UUID {} is {}.", atagCert.getUuid(), expectedHash.toString());
                atagCert.setPCREvent(expectedHash.toByteArray());
                //My.jpa().mwAssetTagCertificate().edit(atagCert);
                MwAssetTagCertificateJpaController asert_tag = new MwAssetTagCertificateJpaController(getEntityManagerFactory());
                asert_tag.edit(atagCert);
                result = true;
            }
        } else {
            log.error("Sha1Hash for the asset tag is not specified.");
            throw new ASException(ErrorCode.AS_INVALID_ASSET_TAG_CERTIFICATE_HASH);
        }
    } catch (ASException ase) {
        log.error("Error during mapping of host to the asset tag certificate. Error Details - {}:{}.", ase.getErrorCode(), ase.getErrorMessage());
        throw ase;
    } catch (Exception ex) {
        log.error("Unexpected error during mapping of host by id to the asset tag certificate. Error Details - {}.", ex.getMessage());
        throw new ASException(ex);
    }
    return result;
}
Also used : Sha1Digest(com.intel.mtwilson.util.crypto.Sha1Digest) MwAssetTagCertificateJpaController(com.intel.mtwilson.as.controller.MwAssetTagCertificateJpaController) ASException(com.intel.mountwilson.as.common.ASException) ApiException(com.intel.mtwilson.ApiException) CryptographyException(com.intel.mtwilson.crypto.CryptographyException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MwAssetTagCertificate(com.intel.mtwilson.as.data.MwAssetTagCertificate) ASException(com.intel.mountwilson.as.common.ASException)

Aggregations

ASException (com.intel.mountwilson.as.common.ASException)7 MwAssetTagCertificateJpaController (com.intel.mtwilson.as.controller.MwAssetTagCertificateJpaController)7 MwAssetTagCertificate (com.intel.mtwilson.as.data.MwAssetTagCertificate)7 ApiException (com.intel.mtwilson.ApiException)6 CryptographyException (com.intel.mtwilson.crypto.CryptographyException)6 IOException (java.io.IOException)6 KeyManagementException (java.security.KeyManagementException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 SignatureException (java.security.SignatureException)6 CertificateException (java.security.cert.CertificateException)6 AssetTagCertAssociateRequest (com.intel.mtwilson.datatypes.AssetTagCertAssociateRequest)2 Sha1Digest (com.intel.mtwilson.util.crypto.Sha1Digest)2 TblHostsJpaController (com.intel.mtwilson.as.controller.TblHostsJpaController)1 TblHosts (com.intel.mtwilson.as.data.TblHosts)1 X509AttributeCertificate (com.intel.mtwilson.datatypes.X509AttributeCertificate)1 UUID (com.intel.mtwilson.util.io.UUID)1