1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ksb.security.service.impl;
17
18 import java.io.IOException;
19 import java.security.GeneralSecurityException;
20 import java.security.KeyException;
21 import java.security.PublicKey;
22 import java.security.Signature;
23 import java.security.cert.Certificate;
24 import java.security.cert.CertificateException;
25
26 import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
27 import org.kuali.rice.ksb.security.admin.service.JavaSecurityManagementService;
28 import org.kuali.rice.ksb.security.service.DigitalSignatureService;
29 import org.kuali.rice.ksb.util.KSBConstants;
30
31 public class DigitalSignatureServiceImpl implements DigitalSignatureService {
32
33 public Signature getSignatureForSigning() throws IOException, GeneralSecurityException {
34 Signature signature = getSignature();
35 signature.initSign(getJavaSecurityManagementService().getModulePrivateKey());
36 return signature;
37 }
38
39 public Signature getSignatureForVerification(String verificationAlias) throws IOException, GeneralSecurityException {
40 Certificate cert = getJavaSecurityManagementService().getCertificate(verificationAlias);
41 return getSignatureForVerification(cert);
42 }
43
44 public Signature getSignatureForVerification(Certificate certificate) throws IOException, GeneralSecurityException {
45 if (certificate == null) {
46 throw new CertificateException("Could not find certificate");
47 }
48 PublicKey publicKey = certificate.getPublicKey();
49 if (publicKey == null) {
50 throw new KeyException("Could not find the public key from valid certificate");
51 }
52 Signature signature = getSignature();
53 signature.initVerify(publicKey);
54 return signature;
55 }
56
57 protected Signature getSignature() throws GeneralSecurityException {
58 return Signature.getInstance(getJavaSecurityManagementService().getModuleSignatureAlgorithm());
59 }
60
61 protected JavaSecurityManagementService getJavaSecurityManagementService() {
62 return (JavaSecurityManagementService)GlobalResourceLoader.getService(KSBConstants.ServiceNames.JAVA_SECURITY_MANAGEMENT_SERVICE);
63 }
64
65
66 }