Search in sources :

Example 46 with CodedException

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

the class AsyncHttpSender method waitForResponse.

/**
 * Will block until response becomes available in the future.
 * @param timeoutSec number of seconds before a timeout exception is thrown
 * @throws Exception if response could not be retrieved in the alloted time
 */
public void waitForResponse(int timeoutSec) throws Exception {
    if (futureResponse == null) {
        throw new CodedException(X_INTERNAL_ERROR, "Request uninitialized");
    }
    LOG.trace("waitForResponse()");
    try {
        HttpResponse response = futureResponse.get(timeoutSec, TimeUnit.SECONDS);
        handleResponse(response);
    } catch (TimeoutException e) {
        cancelRequest();
        throw new CodedException(X_NETWORK_ERROR, "Connection timed out");
    } catch (Exception e) {
        handleFailure(e);
    } finally {
        futureResponse = null;
        PerformanceLogger.log(LOG, "waitForResponse() done");
    }
}
Also used : CodedException(ee.ria.xroad.common.CodedException) HttpResponse(org.apache.http.HttpResponse) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) CodedException(ee.ria.xroad.common.CodedException) ErrorCodes.translateException(ee.ria.xroad.common.ErrorCodes.translateException) TimeoutException(java.util.concurrent.TimeoutException)

Example 47 with CodedException

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

the class CertUtils method readKeyPairFromPemFile.

/**
 * Read private and public keys from PEM file
 * @param filename file containing the keypair
 * @return KeyPair
 * @throws NoSuchAlgorithmException when algorithm for decoding is not available
 * @throws InvalidKeySpecException when key file is invalid
 * @throws IOException when I/O error occurs
 */
public static KeyPair readKeyPairFromPemFile(String filename) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    File pkFile = new File(filename);
    try (PEMParser pemParser = new PEMParser(new FileReader(pkFile))) {
        Object o = pemParser.readObject();
        if (o == null || !(o instanceof PrivateKeyInfo)) {
            throw new CodedException(X_INTERNAL_ERROR, "Could not read key from '%s'", filename);
        }
        PrivateKeyInfo pki = (PrivateKeyInfo) o;
        KeyFactory kf = KeyFactory.getInstance("RSA");
        final PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(pki.getEncoded());
        final PrivateKey privateKey = kf.generatePrivate(ks);
        final RSAPrivateKey rpk = RSAPrivateKey.getInstance(pki.parsePrivateKey());
        final PublicKey publicKey = kf.generatePublic(new RSAPublicKeySpec(rpk.getModulus(), rpk.getPublicExponent()));
        KeyPair kp = new KeyPair(publicKey, privateKey);
        return kp;
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPrivateKey(org.bouncycastle.asn1.pkcs.RSAPrivateKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) PEMParser(org.bouncycastle.openssl.PEMParser) CodedException(ee.ria.xroad.common.CodedException) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) FileReader(java.io.FileReader) CryptoUtils.toDERObject(ee.ria.xroad.common.util.CryptoUtils.toDERObject) File(java.io.File) RSAPrivateKey(org.bouncycastle.asn1.pkcs.RSAPrivateKey) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) KeyFactory(java.security.KeyFactory)

Example 48 with CodedException

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

the class CertUtils method getPrincipalCommonName.

/**
 * return common name for a certificate principal
 * @param principal principal for which to get the issuer common name
 * @return short name of the certificate principal.
 */
private static String getPrincipalCommonName(X500Principal principal) {
    X500Name x500name = new X500Name(principal.getName());
    String cn = getRDNValue(x500name, BCStyle.CN);
    if (cn == null) {
        throw new CodedException(ErrorCodes.X_INCORRECT_CERTIFICATE, "Certificate subject name does not contain common name");
    }
    return cn;
}
Also used : CodedException(ee.ria.xroad.common.CodedException) X500Name(org.bouncycastle.asn1.x500.X500Name) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 49 with CodedException

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

the class CertUtils method getSubjectAlternativeNames.

/**
 * Reads subject alternative names from certificate and returns its string representation
 * @param cert certificate for which to get the subject alternative names
 * @return string representation of the subject alternative names
 */
public static String getSubjectAlternativeNames(X509Certificate cert) {
    StringBuilder builder = new StringBuilder();
    Collection<List<?>> subjectAlternativeNames;
    try {
        subjectAlternativeNames = cert.getSubjectAlternativeNames();
    } catch (CertificateParsingException e) {
        throw new CodedException(ErrorCodes.X_INCORRECT_CERTIFICATE, "Failed parsing the certificate information");
    }
    if (subjectAlternativeNames != null) {
        for (final List<?> sanItem : subjectAlternativeNames) {
            final Integer itemType = (Integer) sanItem.get(0);
            if (itemType >= 0 && itemType <= MAX_IDX) {
                if (builder.length() > 0)
                    builder.append(", ");
                builder.append(FIELD_NAMES.get(itemType));
                builder.append(':');
                builder.append(UNSUPPORTED_FIELDS.contains(itemType) ? "<unsupported>" : (String) sanItem.get(1));
            }
        }
    }
    return builder.length() == 0 ? null : builder.toString();
}
Also used : CertificateParsingException(java.security.cert.CertificateParsingException) CodedException(ee.ria.xroad.common.CodedException) List(java.util.List) DEROctetString(org.bouncycastle.asn1.DEROctetString)

Example 50 with CodedException

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

the class CertUtils method getSubjectClientId.

/**
 * @param cert certificate from which to construct the client ID
 * @return a fully constructed Client identifier from DN of the certificate.
 */
public static ClientId getSubjectClientId(X509Certificate cert) {
    X500Principal principal = cert.getSubjectX500Principal();
    X500Name x500name = new X500Name(principal.getName());
    String c = getRDNValue(x500name, BCStyle.C);
    if (c == null) {
        throw new CodedException(ErrorCodes.X_INCORRECT_CERTIFICATE, "Certificate subject name does not contain country code");
    }
    String o = getRDNValue(x500name, BCStyle.O);
    if (o == null) {
        throw new CodedException(ErrorCodes.X_INCORRECT_CERTIFICATE, "Certificate subject name does not contain organization");
    }
    String cn = getRDNValue(x500name, BCStyle.CN);
    if (cn == null) {
        throw new CodedException(ErrorCodes.X_INCORRECT_CERTIFICATE, "Certificate subject name does not contain common name");
    }
    return ClientId.create(c, o, cn);
}
Also used : CodedException(ee.ria.xroad.common.CodedException) X500Principal(javax.security.auth.x500.X500Principal) X500Name(org.bouncycastle.asn1.x500.X500Name) DEROctetString(org.bouncycastle.asn1.DEROctetString)

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