use of com.intel.mtwilson.datatypes.CertificateFilterCriteria 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;
}
Aggregations