View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }