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");
}
}
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;
}
}
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;
}
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();
}
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);
}
Aggregations