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;
}
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;
}
Aggregations