use of ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo in project X-Road by nordic-institute.
the class KeysApiController method downloadCsr.
@Override
@PreAuthorize("hasAnyAuthority('GENERATE_AUTH_CERT_REQ', 'GENERATE_SIGN_CERT_REQ')")
public ResponseEntity<Resource> downloadCsr(String keyId, String csrId, CsrFormat csrFormat) {
// squid:S3655 throwing NoSuchElementException if there is no value present is
// fine since csr format is mandatory parameter
CertificateRequestFormat certificateRequestFormat = CsrFormatMapping.map(csrFormat).get();
GeneratedCertRequestInfo csrInfo;
try {
csrInfo = tokenCertificateService.regenerateCertRequest(keyId, csrId, certificateRequestFormat);
} catch (KeyNotFoundException | CsrNotFoundException e) {
throw new ResourceNotFoundException(e);
} catch (ActionNotPossibleException e) {
throw new ConflictException(e);
}
String filename = csrFilenameCreator.createCsrFilename(csrInfo.getKeyUsage(), certificateRequestFormat, csrInfo.getMemberId(), serverConfService.getSecurityServerId());
return ControllerUtil.createAttachmentResourceResponse(csrInfo.getCertRequest(), filename);
}
use of ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo in project X-Road by nordic-institute.
the class KeyAndCertificateRequestService method addKeyAndCertRequest.
/**
* Add a new key and create a csr for it
* @param tokenId
* @param keyLabel
* @param memberId
* @param keyUsageInfo
* @param caName
* @param subjectFieldValues
* @param csrFormat
* @return
* @throws ActionNotPossibleException if add key or generate csr was not possible
* @throws ClientNotFoundException if client with {@code memberId} id was not found
* @throws CertificateAuthorityNotFoundException if ca authority with name {@code caName} does not exist
* @throws TokenNotFoundException if token with {@code tokenId} was not found
* @throws DnFieldHelper.InvalidDnParameterException if required dn parameters were missing, or if there
* were some extra parameters
*/
public KeyAndCertRequestInfo addKeyAndCertRequest(String tokenId, String keyLabel, ClientId memberId, KeyUsageInfo keyUsageInfo, String caName, Map<String, String> subjectFieldValues, CertificateRequestFormat csrFormat) throws ActionNotPossibleException, ClientNotFoundException, CertificateAuthorityNotFoundException, TokenNotFoundException, DnFieldHelper.InvalidDnParameterException {
KeyInfo keyInfo = keyService.addKey(tokenId, keyLabel);
GeneratedCertRequestInfo csrInfo;
boolean csrGenerateSuccess = false;
Exception csrGenerateException = null;
try {
csrInfo = tokenCertificateService.generateCertRequest(keyInfo.getId(), memberId, keyUsageInfo, caName, subjectFieldValues, csrFormat);
csrGenerateSuccess = true;
} catch (KeyNotFoundException | WrongKeyUsageException e) {
csrGenerateException = e;
// create key & generateCertRequest
throw new DeviationAwareRuntimeException(e, e.getErrorDeviation());
} catch (Exception e) {
csrGenerateException = e;
throw e;
} finally {
// In case of Errors, we do not want to attempt rollback
if (csrGenerateException != null) {
tryRollbackCreateKey(csrGenerateException, keyInfo.getId());
} else if (!csrGenerateSuccess) {
log.error("csr generate failed -create key rollback was not attempted since failure " + "was not due to an Exception (we do not catch Errors)");
}
}
// get a new keyInfo that contains the csr
KeyInfo refreshedKeyInfo;
try {
refreshedKeyInfo = keyService.getKey(keyInfo.getId());
} catch (KeyNotFoundException e) {
throw new DeviationAwareRuntimeException(e, e.getErrorDeviation());
}
KeyAndCertRequestInfo info = new KeyAndCertRequestInfo(refreshedKeyInfo, csrInfo.getCertReqId(), csrInfo.getCertRequest(), csrInfo.getFormat(), csrInfo.getMemberId(), csrInfo.getKeyUsage());
return info;
}
Aggregations