Search in sources :

Example 16 with KeyInfo

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

the class CertificateAuthoritiesApiController method getSubjectFieldDescriptions.

// see reason below
@SuppressWarnings("squid:S3655")
@Override
@PreAuthorize("(hasAuthority('GENERATE_AUTH_CERT_REQ') and " + " (#keyUsageType == T(org.niis.xroad.securityserver.restapi.openapi.model.KeyUsageType).AUTHENTICATION))" + " or (hasAuthority('GENERATE_SIGN_CERT_REQ') and " + "(#keyUsageType == T(org.niis.xroad.securityserver.restapi.openapi.model.KeyUsageType).SIGNING))")
public ResponseEntity<Set<CsrSubjectFieldDescription>> getSubjectFieldDescriptions(String caName, KeyUsageType keyUsageType, String keyId, String encodedMemberId, Boolean isNewMember) {
    // squid:S3655 throwing NoSuchElementException if there is no value present is
    // fine since keyUsageInfo is mandatory parameter
    KeyUsageInfo keyUsageInfo = KeyUsageTypeMapping.map(keyUsageType).get();
    // memberId is mandatory for sign csrs
    if (keyUsageInfo == KeyUsageInfo.SIGNING) {
        if (StringUtils.isBlank(encodedMemberId)) {
            throw new BadRequestException("memberId is mandatory for sign csrs");
        }
    }
    try {
        if (!StringUtils.isBlank(keyId)) {
            // validate that key.usage matches keyUsageType
            KeyInfo keyInfo = keyService.getKey(keyId);
            if (keyInfo.getUsage() != null) {
                if (keyInfo.getUsage() != keyUsageInfo) {
                    throw new BadRequestException("key is for different usage", new ErrorDeviation("wrong_key_usage"));
                }
            }
        }
        ClientId memberId = null;
        if (!StringUtils.isBlank(encodedMemberId)) {
            memberId = clientConverter.convertId(encodedMemberId);
        }
        CertificateProfileInfo profileInfo;
        profileInfo = certificateAuthorityService.getCertificateProfile(caName, keyUsageInfo, memberId, isNewMember);
        Set<CsrSubjectFieldDescription> converted = subjectConverter.convert(profileInfo.getSubjectFields());
        return new ResponseEntity<>(converted, HttpStatus.OK);
    } catch (WrongKeyUsageException | KeyNotFoundException | ClientNotFoundException e) {
        throw new BadRequestException(e);
    } catch (CertificateAuthorityNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (CertificateProfileInstantiationException e) {
        throw new InternalServerErrorException(e);
    }
}
Also used : ClientNotFoundException(org.niis.xroad.securityserver.restapi.service.ClientNotFoundException) CertificateAuthorityNotFoundException(org.niis.xroad.securityserver.restapi.service.CertificateAuthorityNotFoundException) CertificateProfileInfo(ee.ria.xroad.common.certificateprofile.CertificateProfileInfo) ErrorDeviation(org.niis.xroad.restapi.exceptions.ErrorDeviation) ResponseEntity(org.springframework.http.ResponseEntity) CertificateProfileInstantiationException(org.niis.xroad.securityserver.restapi.service.CertificateProfileInstantiationException) KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) WrongKeyUsageException(org.niis.xroad.securityserver.restapi.service.WrongKeyUsageException) BadRequestException(org.niis.xroad.restapi.openapi.BadRequestException) ClientId(ee.ria.xroad.common.identifier.ClientId) CsrSubjectFieldDescription(org.niis.xroad.securityserver.restapi.openapi.model.CsrSubjectFieldDescription) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) KeyUsageInfo(ee.ria.xroad.signer.protocol.dto.KeyUsageInfo) KeyNotFoundException(org.niis.xroad.securityserver.restapi.service.KeyNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 17 with KeyInfo

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

the class KeysApiController method updateKey.

@Override
@PreAuthorize("hasAuthority('EDIT_KEY_FRIENDLY_NAME')")
@AuditEventMethod(event = RestApiAuditEvent.UPDATE_KEY_NAME)
public ResponseEntity<Key> updateKey(String id, KeyName keyName) {
    KeyInfo keyInfo = null;
    try {
        keyInfo = keyService.updateKeyFriendlyName(id, keyName.getName());
    } catch (KeyNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (ActionNotPossibleException e) {
        throw new ConflictException(e);
    }
    Key key = keyConverter.convert(keyInfo);
    return new ResponseEntity<>(key, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) ActionNotPossibleException(org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) KeyNotFoundException(org.niis.xroad.securityserver.restapi.service.KeyNotFoundException) Key(org.niis.xroad.securityserver.restapi.openapi.model.Key) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) AuditEventMethod(org.niis.xroad.restapi.config.audit.AuditEventMethod)

Example 18 with KeyInfo

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

the class TokensApiController method addKey.

@PreAuthorize("hasAuthority('GENERATE_KEY')")
@Override
@AuditEventMethod(event = RestApiAuditEvent.GENERATE_KEY)
public ResponseEntity<Key> addKey(String tokenId, KeyLabel keyLabel) {
    try {
        KeyInfo keyInfo = keyService.addKey(tokenId, keyLabel.getLabel());
        Key key = keyConverter.convert(keyInfo);
        return ControllerUtil.createCreatedResponse("/api/keys/{keyId}", key, key.getId());
    } catch (TokenNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (ActionNotPossibleException e) {
        throw new ConflictException(e);
    }
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) ActionNotPossibleException(org.niis.xroad.securityserver.restapi.service.ActionNotPossibleException) ResourceNotFoundException(org.niis.xroad.restapi.openapi.ResourceNotFoundException) Key(org.niis.xroad.securityserver.restapi.openapi.model.Key) TokenNotFoundException(org.niis.xroad.securityserver.restapi.service.TokenNotFoundException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) AuditEventMethod(org.niis.xroad.restapi.config.audit.AuditEventMethod)

Example 19 with KeyInfo

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

the class RegenerateCertRequestRequestHandler method findTokenAndKeyForCsrId.

private TokenAndKey findTokenAndKeyForCsrId(String certRequestId) {
    TokenInfoAndKeyId tokenInfoAndKeyId = TokenManager.findTokenAndKeyIdForCertRequestId(certRequestId);
    KeyInfo keyInfo = TokenManager.getKeyInfo(tokenInfoAndKeyId.getKeyId());
    return new TokenAndKey(tokenInfoAndKeyId.getTokenInfo().getId(), keyInfo);
}
Also used : TokenInfoAndKeyId(ee.ria.xroad.signer.protocol.dto.TokenInfoAndKeyId) KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) TokenAndKey(ee.ria.xroad.signer.util.TokenAndKey)

Example 20 with KeyInfo

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

the class ClientsApiControllerIntegrationTest method createMockTokenInfos.

/**
 * @param certificateInfo one certificate to put inside this tokenInfo
 * structure
 * @return
 */
private List<TokenInfo> createMockTokenInfos(CertificateInfo certificateInfo) {
    List<TokenInfo> mockTokens = new ArrayList<>();
    List<CertificateInfo> certificates = new ArrayList<>();
    if (certificateInfo != null) {
        certificates.add(certificateInfo);
    }
    KeyInfo keyInfo = new KeyInfo(false, null, "friendlyName", "id", "label", "publicKey", certificates, new ArrayList<CertRequestInfo>(), "signMecchanismName");
    TokenInfo tokenInfo = new TokenInfo("type", "friendlyName", "id", false, false, false, "serialNumber", "label", -1, null, Arrays.asList(keyInfo), null);
    mockTokens.add(tokenInfo);
    return mockTokens;
}
Also used : KeyInfo(ee.ria.xroad.signer.protocol.dto.KeyInfo) ArrayList(java.util.ArrayList) CertificateInfo(ee.ria.xroad.signer.protocol.dto.CertificateInfo) TokenInfo(ee.ria.xroad.signer.protocol.dto.TokenInfo) CertRequestInfo(ee.ria.xroad.signer.protocol.dto.CertRequestInfo)

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