Search in sources :

Example 41 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class AbstractTimestampRequest method getTimestampDer.

protected byte[] getTimestampDer(TimeStampResponse tsResponse) throws Exception {
    X509Certificate signerCertificate = TimestampVerifier.getSignerCertificate(tsResponse.getTimeStampToken(), GlobalConf.getTspCertificates());
    if (signerCertificate == null) {
        throw new CodedException(X_INTERNAL_ERROR, "Could not find signer certificate");
    }
    TimeStampToken token = addSignerCertificate(tsResponse, signerCertificate);
    return token.getEncoded();
}
Also used : CodedException(ee.ria.xroad.common.CodedException) TimeStampToken(org.bouncycastle.tsp.TimeStampToken) X509Certificate(java.security.cert.X509Certificate)

Example 42 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class GenerateSelfSignedCertRequestHandler method handle.

@Override
protected Object handle(GenerateSelfSignedCert message) throws Exception {
    TokenAndKey tokenAndKey = TokenManager.findTokenAndKey(message.getKeyId());
    if (!TokenManager.isKeyAvailable(tokenAndKey.getKeyId())) {
        throw keyNotAvailable(tokenAndKey.getKeyId());
    }
    if (tokenAndKey.getKey().getPublicKey() == null) {
        throw new CodedException(X_INTERNAL_ERROR, "Key '%s' has no public key", message.getKeyId());
    }
    PublicKey pk = readX509PublicKey(decodeBase64(tokenAndKey.getKey().getPublicKey()));
    String signAlgoId = CryptoUtils.getSignatureAlgorithmId(SIGNATURE_DIGEST_ALGORITHM, tokenAndKey.getSignMechanism());
    X509Certificate cert = new DummyCertBuilder().build(tokenAndKey, message, pk, signAlgoId);
    byte[] certData = cert.getEncoded();
    importCert(new ImportCert(certData, CertificateInfo.STATUS_REGISTERED, message.getMemberId()));
    return new GenerateSelfSignedCertResponse(certData);
}
Also used : CodedException(ee.ria.xroad.common.CodedException) CryptoUtils.readX509PublicKey(ee.ria.xroad.common.util.CryptoUtils.readX509PublicKey) PublicKey(java.security.PublicKey) TokenAndKey(ee.ria.xroad.signer.util.TokenAndKey) GenerateSelfSignedCertResponse(ee.ria.xroad.signer.protocol.message.GenerateSelfSignedCertResponse) X509Certificate(java.security.cert.X509Certificate) ImportCert(ee.ria.xroad.signer.protocol.message.ImportCert)

Example 43 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class AbstractGenerateCertRequest method buildSignedCertRequest.

PKCS10CertificationRequest buildSignedCertRequest(TokenAndKey tokenAndKey, String subjectName) throws Exception {
    if (tokenAndKey.getKey().getPublicKey() == null) {
        throw new CodedException(X_INTERNAL_ERROR, "Key '%s' has no public key", tokenAndKey.getKeyId());
    }
    PublicKey publicKey = readPublicKey(tokenAndKey.getKey().getPublicKey());
    JcaPKCS10CertificationRequestBuilder certRequestBuilder = new JcaPKCS10CertificationRequestBuilder(new X500Name(subjectName), publicKey);
    ContentSigner signer = new TokenContentSigner(tokenAndKey, this);
    PKCS10CertificationRequest request = certRequestBuilder.build(signer);
    return request;
}
Also used : PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) JcaPKCS10CertificationRequestBuilder(org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder) CodedException(ee.ria.xroad.common.CodedException) CryptoUtils.readX509PublicKey(ee.ria.xroad.common.util.CryptoUtils.readX509PublicKey) PublicKey(java.security.PublicKey) ContentSigner(org.bouncycastle.operator.ContentSigner) X500Name(org.bouncycastle.asn1.x500.X500Name)

Example 44 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class ExceptionTranslator method toResponseEntity.

/**
 * Create ResponseEntity<ErrorInfo> from an Exception.
 * Use provided status or override it with value from
 * Exception's ResponseStatus annotation if one exists
 * @param e
 * @param defaultStatus
 * @return
 */
public ResponseEntity<ErrorInfo> toResponseEntity(Exception e, HttpStatus defaultStatus) {
    HttpStatus status = getAnnotatedResponseStatus(e, defaultStatus);
    ErrorInfo errorDto = new ErrorInfo();
    errorDto.setStatus(status.value());
    if (e instanceof DeviationAware) {
        // add information about errors and warnings
        DeviationAware errorCodedException = (DeviationAware) e;
        if (errorCodedException.getErrorDeviation() != null) {
            errorDto.setError(convert(errorCodedException.getErrorDeviation()));
        }
        if (errorCodedException.getWarningDeviations() != null) {
            for (Deviation warning : errorCodedException.getWarningDeviations()) {
                errorDto.addWarningsItem(convert(warning));
            }
        }
    } else if (e instanceof CodedException) {
        // map fault code and string from core CodedException
        CodedException c = (CodedException) e;
        Deviation deviation = new Deviation(CORE_CODED_EXCEPTION_PREFIX + c.getFaultCode(), c.getFaultString());
        errorDto.setError(convert(deviation));
    } else if (e instanceof MethodArgumentNotValidException) {
        errorDto.setError(validationErrorHelper.createError((MethodArgumentNotValidException) e));
    }
    return new ResponseEntity<>(errorDto, status);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) CodedException(ee.ria.xroad.common.CodedException) HttpStatus(org.springframework.http.HttpStatus) ErrorInfo(org.niis.xroad.restapi.openapi.model.ErrorInfo) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException)

Example 45 with CodedException

use of ee.ria.xroad.common.CodedException in project X-Road by nordic-institute.

the class HibernateUtil method getSessionFactory.

/**
 * Returns the session factory for the given session factory name.
 * If the session factory has not been already created, it is created and stored in the cache.
 *
 * @param name        the name of the session factory
 * @param interceptor the interceptor to use on sessions created with this factory
 * @return the session factory
 */
public static synchronized SessionFactory getSessionFactory(String name, Interceptor interceptor) {
    if (sessionFactoryCache.containsKey(name)) {
        return sessionFactoryCache.get(name).getSessionFactory();
    } else {
        try {
            SessionFactoryCtx ctx = createSessionFactoryCtx(name, interceptor);
            sessionFactoryCache.put(name, ctx);
            return ctx.getSessionFactory();
        } catch (Exception e) {
            log.error("Failed to create session factory", e);
            throw new CodedException(X_DATABASE_ERROR, e);
        }
    }
}
Also used : CodedException(ee.ria.xroad.common.CodedException) IOException(java.io.IOException) CodedException(ee.ria.xroad.common.CodedException) HibernateException(org.hibernate.HibernateException)

Aggregations

CodedException (ee.ria.xroad.common.CodedException)131 X509Certificate (java.security.cert.X509Certificate)28 IOException (java.io.IOException)17 ErrorCodes.translateException (ee.ria.xroad.common.ErrorCodes.translateException)15 SignerNotReachableException (org.niis.xroad.restapi.service.SignerNotReachableException)14 TokenInfo (ee.ria.xroad.signer.protocol.dto.TokenInfo)12 OCSPResp (org.bouncycastle.cert.ocsp.OCSPResp)11 ServiceException (org.niis.xroad.restapi.service.ServiceException)11 ClientId (ee.ria.xroad.common.identifier.ClientId)10 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 KeyInfo (ee.ria.xroad.signer.protocol.dto.KeyInfo)8 InputStream (java.io.InputStream)8 URISyntaxException (java.net.URISyntaxException)7 Date (java.util.Date)7 SoapFault (ee.ria.xroad.common.message.SoapFault)6 ServiceId (ee.ria.xroad.common.identifier.ServiceId)5 Soap (ee.ria.xroad.common.message.Soap)5 SoapMessageImpl (ee.ria.xroad.common.message.SoapMessageImpl)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5