Skip to content

Commit

Permalink
Merge pull request #941 from /issues/932-backport-apns-certificate
Browse files Browse the repository at this point in the history
Backport #920 to 1.6.x
  • Loading branch information
romanstrobl authored Dec 20, 2024
2 parents 6083c06 + 0d6d20e commit fd1778b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>io.getlime.security</groupId>
<artifactId>powerauth-push-server-parent</artifactId>
<version>1.6.0</version>
<version>1.6.1</version>
<packaging>pom</packaging>

<parent>
Expand Down
2 changes: 1 addition & 1 deletion powerauth-push-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<artifactId>powerauth-push-server-parent</artifactId>
<groupId>io.getlime.security</groupId>
<version>1.6.0</version>
<version>1.6.1</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion powerauth-push-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<artifactId>powerauth-push-server-parent</artifactId>
<groupId>io.getlime.security</groupId>
<version>1.6.0</version>
<version>1.6.1</version>
</parent>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion powerauth-push-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>io.getlime.security</groupId>
<artifactId>powerauth-push-server-parent</artifactId>
<version>1.6.0</version>
<version>1.6.1</version>
</parent>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
package io.getlime.push.util;

import io.getlime.push.configuration.PushServiceConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
Expand All @@ -43,31 +40,25 @@
* @author Petr Dvorak, [email protected]
*/
@Service
@AllArgsConstructor
@Slf4j
public class CaCertUtil {

private static final Logger logger = LoggerFactory.getLogger(CaCertUtil.class);

// Include those constants to remove dependency on X509Factory.BEGIN_CERT and X509Factory.END_CERT.
private static final String BEGIN_CERT = "-----BEGIN CERTIFICATE-----";
private static final String END_CERT = "-----END CERTIFICATE-----";

private static final String[] embeddedCertificates = {
"cacert/GeoTrust_Global_CA.pem",
"cacert/AAACertificateServices.pem",
"cacert/COMODORSAAAACA.pem",
"cacert/USERTrustRSAAAACA.pem"
};
private static final List<String> EMBEDDED_CERTIFICATES = List.of(
"classpath:/cacert/GeoTrust_Global_CA.pem",
"classpath:/cacert/AAACertificateServices.pem",
"classpath:/cacert/COMODORSAAAACA.pem",
"classpath:/cacert/USERTrustRSAAAACA.pem",
"classpath:/cacert/USERTrustRSA_CA.pem"
);

private final PushServiceConfiguration pushServiceConfiguration;

/**
* Constructor with push service configuration.
* @param pushServiceConfiguration Push server configuration.
*/
@Autowired
public CaCertUtil(PushServiceConfiguration pushServiceConfiguration) {
this.pushServiceConfiguration = pushServiceConfiguration;
}
private final ResourceLoader resourceLoader;

/**
* Obtain all registered CA certificates.
Expand Down Expand Up @@ -98,13 +89,15 @@ public X509Certificate[] allCerts() {
}

// Add the locally stored CA certificates required by Apple for APNs
for (String certPath : embeddedCertificates) {
for (String certPath : EMBEDDED_CERTIFICATES) {
try {
logger.info("Importing embedded certificate: {}", certPath);
final File resource = new ClassPathResource(certPath).getFile();
final String certString = Files.readString(resource.toPath());
final X509Certificate cert = certificateFromPem(certString);
result.add(cert);
final Resource resource = resourceLoader.getResource(certPath);
try (final InputStream inputStream = resource.getInputStream()) {
final String certString = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
final X509Certificate cert = certificateFromPem(certString);
result.add(cert);
}
} catch (CertificateException | IOException e) {
logger.error("Certificate error: {}", e.getMessage(), e);
}
Expand All @@ -115,8 +108,8 @@ public X509Certificate[] allCerts() {

private X509Certificate certificateFromPem(String pem) throws CertificateException {
final byte[] decoded = Base64.getDecoder().decode(pem
.replaceAll(BEGIN_CERT, "")
.replaceAll(END_CERT, "")
.replace(BEGIN_CERT, "")
.replace(END_CERT, "")
.replaceAll("\\s", "")
);
return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(decoded));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-----BEGIN CERTIFICATE-----
MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB
iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl
cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV
BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw
MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMCVVMxEzARBgNV
BAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0plcnNleSBDaXR5MR4wHAYDVQQKExVU
aGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNVBAMTJVVTRVJUcnVzdCBSU0EgQ2Vy
dGlmaWNhdGlvbiBBdXRob3JpdHkwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIK
AoICAQCAEmUXNg7D2wiz0KxXDXbtzSfTTK1Qg2HiqiBNCS1kCdzOiZ/MPans9s/B
3PHTsdZ7NygRK0faOca8Ohm0X6a9fZ2jY0K2dvKpOyuR+OJv0OwWIJAJPuLodMkY
tJHUYm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