Search in sources :

Example 56 with KeyInfo

use of ee.ria.xroad.signer.protocol.dto.KeyInfo 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)

Example 57 with KeyInfo

use of ee.ria.xroad.signer.protocol.dto.KeyInfo in project X-Road by nordic-institute.

the class KeyService method updateKeyFriendlyName.

/**
 * Updates key friendly name
 * @throws KeyNotFoundException if key was not found
 * @throws ActionNotPossibleException if friendly name could not be updated for this key
 */
public KeyInfo updateKeyFriendlyName(String id, String friendlyName) throws KeyNotFoundException, ActionNotPossibleException {
    // check that updating friendly name is possible
    TokenInfo tokenInfo = tokenService.getTokenForKeyId(id);
    KeyInfo keyInfo = getKey(tokenInfo, id);
    auditDataHelper.put(RestApiAuditProperty.KEY_ID, keyInfo.getId());
    auditDataHelper.put(RestApiAuditProperty.KEY_FRIENDLY_NAME, friendlyName);
    possibleActionsRuleEngine.requirePossibleKeyAction(PossibleActionEnum.EDIT_FRIENDLY_NAME, tokenInfo, keyInfo);
    try {
        signerProxyFacade.setKeyFriendlyName(id, friendlyName);
        keyInfo = getKey(id);
    } catch (KeyNotFoundException e) {
        throw e;
    } catch (CodedException e) {
        if (isCausedByKeyNotFound(e)) {
            throw new KeyNotFoundException(e);
        } else {
            throw e;
        }
    } catch (Exception e) {
        throw new SignerNotReachableException("Update key friendly name failed", e);
    }
    return keyInfo;
}
Also used : CodedException(ee.ria.xroad.common.CodedException) KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) TokenInfo(ee.ria.xroad.signer.protocol.dto.TokenInfo) SignerNotReachableException(org.niis.xroad.restapi.service.SignerNotReachableException) NoSuchElementException(java.util.NoSuchElementException) UnhandledWarningsException(org.niis.xroad.restapi.service.UnhandledWarningsException) CodedException(ee.ria.xroad.common.CodedException) SignerNotReachableException(org.niis.xroad.restapi.service.SignerNotReachableException)

Example 58 with KeyInfo

use of ee.ria.xroad.signer.protocol.dto.KeyInfo in project X-Road by nordic-institute.

the class KeyService method addKey.

/**
 * Generate a new key for selected token
 * @param tokenId
 * @param keyLabel
 * @return {@link KeyInfo}
 * @throws TokenNotFoundException if token was not found
 * @throws ActionNotPossibleException if generate key was not possible for this token
 */
public KeyInfo addKey(String tokenId, String keyLabel) throws TokenNotFoundException, ActionNotPossibleException {
    // check that adding a key is possible
    TokenInfo tokenInfo = tokenService.getToken(tokenId);
    auditDataHelper.put(tokenInfo);
    possibleActionsRuleEngine.requirePossibleTokenAction(PossibleActionEnum.GENERATE_KEY, tokenInfo);
    KeyInfo keyInfo = null;
    try {
        keyInfo = signerProxyFacade.generateKey(tokenId, keyLabel);
    } catch (CodedException e) {
        throw e;
    } catch (Exception other) {
        throw new SignerNotReachableException("adding a new key failed", other);
    }
    auditDataHelper.put(RestApiAuditProperty.KEY_ID, keyInfo.getId());
    auditDataHelper.put(RestApiAuditProperty.KEY_LABEL, keyInfo.getLabel());
    auditDataHelper.put(RestApiAuditProperty.KEY_FRIENDLY_NAME, keyInfo.getFriendlyName());
    return keyInfo;
}
Also used : CodedException(ee.ria.xroad.common.CodedException) KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) TokenInfo(ee.ria.xroad.signer.protocol.dto.TokenInfo) SignerNotReachableException(org.niis.xroad.restapi.service.SignerNotReachableException) NoSuchElementException(java.util.NoSuchElementException) UnhandledWarningsException(org.niis.xroad.restapi.service.UnhandledWarningsException) CodedException(ee.ria.xroad.common.CodedException) SignerNotReachableException(org.niis.xroad.restapi.service.SignerNotReachableException)

Aggregations

KeyInfo (ee.ria.xroad.signer.protocol.dto.KeyInfo)58 TokenInfo (ee.ria.xroad.signer.protocol.dto.TokenInfo)32 CertificateInfo (ee.ria.xroad.signer.protocol.dto.CertificateInfo)17 Test (org.junit.Test)16 CodedException (ee.ria.xroad.common.CodedException)12 TokenTestUtils (org.niis.xroad.securityserver.restapi.util.TokenTestUtils)12 CertRequestInfo (ee.ria.xroad.signer.protocol.dto.CertRequestInfo)9 TokenInfoAndKeyId (ee.ria.xroad.signer.protocol.dto.TokenInfoAndKeyId)9 Before (org.junit.Before)9 ArrayList (java.util.ArrayList)7 ClientId (ee.ria.xroad.common.identifier.ClientId)6 DeviationAwareRuntimeException (org.niis.xroad.restapi.exceptions.DeviationAwareRuntimeException)6 SignerNotReachableException (org.niis.xroad.restapi.service.SignerNotReachableException)6 KeyUsageInfo (ee.ria.xroad.signer.protocol.dto.KeyUsageInfo)5 HashMap (java.util.HashMap)5 ResourceNotFoundException (org.niis.xroad.restapi.openapi.ResourceNotFoundException)5 AuthKeyInfo (ee.ria.xroad.signer.protocol.dto.AuthKeyInfo)4 TokenManager.getKeyInfo (ee.ria.xroad.signer.tokenmanager.TokenManager.getKeyInfo)4 CertificateTestUtils (org.niis.xroad.securityserver.restapi.util.CertificateTestUtils)4 GeneratedCertRequestInfo (ee.ria.xroad.commonui.SignerProxy.GeneratedCertRequestInfo)3