Search in sources :

Example 1 with GeneratedCertRequestInfo

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);
}
Also used : ActionNotPossibleException(org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException) GeneratedCertRequestInfo(ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo) CsrNotFoundException(org.niis.xroad.securityserver.restapi.service.CsrNotFoundException) CertificateRequestFormat(ee.ria.xroad.signer.protocol.message.CertificateRequestFormat) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) KeyNotFoundException(org.niis.xroad.securityserver.restapi.service.KeyNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 2 with GeneratedCertRequestInfo

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;
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) DeviationAwareRuntimeException(org.niis.xroad.restapi.exceptions.DeviationAwareRuntimeException) GeneratedCertRequestInfo(ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo) DeviationAwareRuntimeException(org.niis.xroad.restapi.exceptions.DeviationAwareRuntimeException)

Aggregations

GeneratedCertRequestInfo (ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo)2 KeyInfo (ee.ria.xroad.signer.protocol.dto.KeyInfo)1 CertificateRequestFormat (ee.ria.xroad.signer.protocol.message.CertificateRequestFormat)1 DeviationAwareRuntimeException (org.niis.xroad.restapi.exceptions.DeviationAwareRuntimeException)1 ResourceNotFoundException (org.niis.xroad.restapi.openapi.ResourceNotFoundException)1 ActionNotPossibleException (org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException)1 CsrNotFoundException (org.niis.xroad.securityserver.restapi.service.CsrNotFoundException)1 KeyNotFoundException (org.niis.xroad.securityserver.restapi.service.KeyNotFoundException)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1