Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #53 from catenax-ng/ski-check
Browse files Browse the repository at this point in the history
Recalculate SKI to check certificate before registration
  • Loading branch information
SebastianBezold authored May 12, 2023
2 parents 1490b4b + ad20498 commit 8752c07
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed
- make DapsManager methods synchronized
- check if a client was registered before creating new registration
- check if SKI was spoofed before creating a new registration

### Added
- add attributes validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public synchronized ResponseEntity<Map<String, Object>> createClientPost(String
String securityProfile) {
var cert = Certutil.loadCertificate(new String(file.getBytes()));
var clientId = Certutil.getClientId(cert);
if (!Certutil.createSki(cert).equals(Certutil.getSki(cert))) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Certificate problem");
}
if (dapsClient.getClient(clientId).isPresent()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Client exists");
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/eclipse/tractusx/dapsreg/util/Certutil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier;
import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
import org.bouncycastle.cert.jcajce.JcaX509ExtensionUtils;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
Expand All @@ -56,6 +58,12 @@ public static String getSki(X509Certificate cert) {
return BaseEncoding.base16().upperCase().withSeparator(":", 2).encode(keyIdentifier);
}

public static String createSki(X509Certificate cert) throws NoSuchAlgorithmException {
var publicKey = cert.getPublicKey();
var r = new JcaX509ExtensionUtils().createSubjectKeyIdentifier(publicKey).getKeyIdentifier();
return BaseEncoding.base16().upperCase().withSeparator(":", 2).encode(r);
}

public static X509Certificate loadCertificate(String pem) throws IOException, CertificateException {
try(var ts = new ByteArrayInputStream(pem.getBytes(Charsets.UTF_8))) {
CertificateFactory fac = CertificateFactory.getInstance("X509");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import jakarta.annotation.PostConstruct;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -53,12 +54,14 @@ public void init() {
}

@Test
void utilTest() throws IOException, CertificateException {
void utilTest() throws IOException, CertificateException, NoSuchAlgorithmException {
try (var pemStream = Resources.getResource("test.crt").openStream()) {
var pem = new String(pemStream.readAllBytes());
var cert = Certutil.loadCertificate(pem);
var clientId = Certutil.getClientId(cert);
var ski = Certutil.createSki(cert);
assertThat(clientId).isEqualTo("65:FA:DE:C2:6A:58:98:D8:EA:FC:70:27:76:A0:75:D5:A1:C4:89:F9:keyid:65:FA:DE:C2:6A:58:98:D8:EA:FC:70:27:76:A0:75:D5:A1:C4:89:F9");
assertThat(ski).isEqualTo(Certutil.getSki(cert));
var certPem = Certutil.getCertificate(cert);
System.out.println(certPem);
var certJson = jsonUtil.getCertificateJson(cert);
Expand Down

0 comments on commit 8752c07

Please sign in to comment.