|
| 1 | +/* |
| 2 | + * Copyright (c) 2020-2025 Estonian Information System Authority |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +package eu.webeid.example.service; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.fasterxml.jackson.databind.ObjectWriter; |
| 28 | +import eu.webeid.example.config.WebEidMobileProperties; |
| 29 | +import eu.webeid.example.security.WebEidAuthentication; |
| 30 | +import eu.webeid.example.service.dto.CertificateDTO; |
| 31 | +import eu.webeid.example.service.dto.DigestDTO; |
| 32 | +import eu.webeid.example.service.dto.SignatureAlgorithmDTO; |
| 33 | +import eu.webeid.security.authtoken.SupportedSignatureAlgorithm; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | +import org.springframework.stereotype.Component; |
| 37 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
| 38 | +import org.springframework.web.util.UriComponents; |
| 39 | +import org.springframework.web.util.UriComponentsBuilder; |
| 40 | + |
| 41 | +import java.io.IOException; |
| 42 | +import java.security.NoSuchAlgorithmException; |
| 43 | +import java.security.cert.CertificateException; |
| 44 | +import java.util.Base64; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +@Component |
| 48 | +public class MobleSigningService { |
| 49 | + public static final String CERTIFICATE_RESPONSE_PATH = "/sign/mobile/certificate"; |
| 50 | + public static final String SIGNATURE_RESPONSE_PATH = "/sign/mobile/signature"; |
| 51 | + public static final String WEB_EID_MOBILE_SIGN_PATH = "/sign"; |
| 52 | + public static final String WEB_EID_GET_CERT_PATH = "/cert"; |
| 53 | + private static final Logger LOG = LoggerFactory.getLogger(MobleSigningService.class); |
| 54 | + private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writerFor(RequestObject.class); |
| 55 | + private final SigningService signingService; |
| 56 | + private final WebEidMobileProperties webEidMobileProperties; |
| 57 | + |
| 58 | + public MobleSigningService(SigningService signingService, WebEidMobileProperties webEidMobileProperties) { |
| 59 | + this.signingService = signingService; |
| 60 | + this.webEidMobileProperties = webEidMobileProperties; |
| 61 | + } |
| 62 | + |
| 63 | + public MobileInitRequest initSigningRequest(WebEidAuthentication authentication) throws IOException, CertificateException, NoSuchAlgorithmException { |
| 64 | + String signingCertificate = authentication.getSigningCertificate(); |
| 65 | + List<SupportedSignatureAlgorithm> supportedSignatureAlgorithms = authentication.getSupportedSignatureAlgorithms(); |
| 66 | + if (signingCertificate == null || supportedSignatureAlgorithms == null) { |
| 67 | + return initSigningRequest(authentication, null); |
| 68 | + } |
| 69 | + CertificateDTO certificateDTO = new CertificateDTO(); |
| 70 | + certificateDTO.setCertificate(signingCertificate); |
| 71 | + certificateDTO.setSupportedSignatureAlgorithms(mapSupportedAlgorithms(supportedSignatureAlgorithms)); |
| 72 | + return initSigningRequest(authentication, certificateDTO); |
| 73 | + } |
| 74 | + |
| 75 | + @SuppressWarnings("javax.annotation.Tainted") |
| 76 | + public MobileInitRequest initSigningRequest(WebEidAuthentication authentication, CertificateDTO certificateDTO) throws IOException, CertificateException, NoSuchAlgorithmException { |
| 77 | + if (certificateDTO != null) { |
| 78 | + LOG.info("Initiating signing request"); |
| 79 | + DigestDTO digest = signingService.prepareContainer(certificateDTO, authentication); |
| 80 | + String responseUri = ServletUriComponentsBuilder.fromCurrentContextPath() |
| 81 | + .path(SIGNATURE_RESPONSE_PATH) |
| 82 | + .build() |
| 83 | + .toUriString(); |
| 84 | + RequestObject initRequest = new RequestObject(responseUri, certificateDTO.getCertificate(), digest.getHash(), digest.getHashFunction()); |
| 85 | + String payloadJson = OBJECT_WRITER.writeValueAsString(initRequest); |
| 86 | + String encoded = Base64.getUrlEncoder().withoutPadding().encodeToString(payloadJson.getBytes()); |
| 87 | + UriComponents requestUri = UriComponentsBuilder.fromUriString(webEidMobileProperties.baseRequestUri()) |
| 88 | + .path(WEB_EID_MOBILE_SIGN_PATH) |
| 89 | + .fragment(encoded) |
| 90 | + .build(); |
| 91 | + return new MobileInitRequest(requestUri.toUriString()); |
| 92 | + } else { |
| 93 | + LOG.info("Initiating get certificate request"); |
| 94 | + String responseUri = ServletUriComponentsBuilder.fromCurrentContextPath() |
| 95 | + .path(CERTIFICATE_RESPONSE_PATH) |
| 96 | + .build() |
| 97 | + .toUriString(); |
| 98 | + RequestObject initRequest = new RequestObject(responseUri, null, null, null); |
| 99 | + String payloadJson = OBJECT_WRITER.writeValueAsString(initRequest); |
| 100 | + String encoded = Base64.getUrlEncoder().withoutPadding().encodeToString(payloadJson.getBytes()); |
| 101 | + UriComponents requestUri = UriComponentsBuilder.fromUriString(webEidMobileProperties.baseRequestUri()) |
| 102 | + .path(WEB_EID_GET_CERT_PATH) |
| 103 | + .fragment(encoded) |
| 104 | + .build(); |
| 105 | + return new MobileInitRequest(requestUri.toUriString()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private List<SignatureAlgorithmDTO> mapSupportedAlgorithms(List<SupportedSignatureAlgorithm> algorithms) { |
| 110 | + return algorithms.stream().map(ssa -> { |
| 111 | + var dto = new SignatureAlgorithmDTO(); |
| 112 | + dto.setCryptoAlgorithm(ssa.getCryptoAlgorithm()); |
| 113 | + dto.setHashFunction(ssa.getHashFunction()); |
| 114 | + dto.setPaddingScheme(ssa.getPaddingScheme()); |
| 115 | + return dto; |
| 116 | + }).toList(); |
| 117 | + } |
| 118 | + |
| 119 | + public record MobileInitRequest( |
| 120 | + String requestUri) { |
| 121 | + } |
| 122 | + |
| 123 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 124 | + record RequestObject( |
| 125 | + String responseUri, |
| 126 | + String signingCertificate, |
| 127 | + String hash, |
| 128 | + String hashFunction) { |
| 129 | + } |
| 130 | +} |
0 commit comments