001 /**
002 * Copyright 2005-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ksb.security.service.impl;
017
018 import java.io.IOException;
019 import java.security.GeneralSecurityException;
020 import java.security.KeyException;
021 import java.security.PublicKey;
022 import java.security.Signature;
023 import java.security.cert.Certificate;
024 import java.security.cert.CertificateException;
025
026 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
027 import org.kuali.rice.ksb.security.admin.service.JavaSecurityManagementService;
028 import org.kuali.rice.ksb.security.service.DigitalSignatureService;
029 import org.kuali.rice.ksb.util.KSBConstants;
030
031 public class DigitalSignatureServiceImpl implements DigitalSignatureService {
032
033 public Signature getSignatureForSigning() throws IOException, GeneralSecurityException {
034 Signature signature = getSignature();
035 signature.initSign(getJavaSecurityManagementService().getModulePrivateKey());
036 return signature;
037 }
038
039 public Signature getSignatureForVerification(String verificationAlias) throws IOException, GeneralSecurityException {
040 Certificate cert = getJavaSecurityManagementService().getCertificate(verificationAlias);
041 return getSignatureForVerification(cert);
042 }
043
044 public Signature getSignatureForVerification(Certificate certificate) throws IOException, GeneralSecurityException {
045 if (certificate == null) {
046 throw new CertificateException("Could not find certificate");
047 }
048 PublicKey publicKey = certificate.getPublicKey();
049 if (publicKey == null) {
050 throw new KeyException("Could not find the public key from valid certificate");
051 }
052 Signature signature = getSignature();
053 signature.initVerify(publicKey);
054 return signature;
055 }
056
057 protected Signature getSignature() throws GeneralSecurityException {
058 return Signature.getInstance(getJavaSecurityManagementService().getModuleSignatureAlgorithm());
059 }
060
061 protected JavaSecurityManagementService getJavaSecurityManagementService() {
062 return (JavaSecurityManagementService)GlobalResourceLoader.getService(KSBConstants.ServiceNames.JAVA_SECURITY_MANAGEMENT_SERVICE);
063 }
064
065
066 }