Package org.wso2.carbon.identity.provider.saml

Source Code of org.wso2.carbon.identity.provider.saml.SignKeyDataHolder

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.identity.provider.saml;

import org.apache.xml.security.signature.XMLSignature;
import org.opensaml.xml.security.credential.Credential;
import org.opensaml.xml.security.credential.CredentialContextSet;
import org.opensaml.xml.security.credential.UsageType;
import org.opensaml.xml.security.x509.X509Credential;
import org.wso2.carbon.base.ServerConfiguration;
import org.wso2.carbon.core.util.KeyStoreManager;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
import org.wso2.carbon.identity.provider.IdentityProviderException;
import org.wso2.carbon.security.keystore.KeyStoreAdmin;

import javax.crypto.SecretKey;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.X509CRL;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collection;

public class SignKeyDataHolder implements X509Credential {

  private String signatureAlgorithm = null;

  private static SignKeyDataHolder instance = null;

  private X509Certificate[] issuerCerts = null;

  private PrivateKey issuerPK = null;

  public static SignKeyDataHolder getInstance() throws IdentityProviderException {
    if (instance == null) {
      instance = new SignKeyDataHolder();
    }
    return instance;
  }

  private SignKeyDataHolder() throws IdentityProviderException {
    // do once - because this is a expensive operation
    String keyAlias = null;
    KeyStoreAdmin keyAdmin = null;
    KeyStoreManager keyMan = null;
    Certificate[] certificates = null;

    try {

      keyAlias = ServerConfiguration.getInstance().getFirstProperty(
          "Security.KeyStore.KeyAlias");

      keyAdmin = new KeyStoreAdmin(IdentityTenantUtil.getRegistry(null,null));
      keyMan = KeyStoreManager.getInstance(null);

      issuerPK = (PrivateKey) keyAdmin.getPrivateKey(keyAlias);;
      certificates = keyMan.getPrimaryKeyStore().getCertificateChain(keyAlias);

      issuerCerts = new X509Certificate[certificates.length];

      int i = 0;
      for (Certificate certificate : certificates) {
        issuerCerts[i++] = (X509Certificate) certificate;
      }

      signatureAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA;

      String pubKeyAlgo = issuerCerts[0].getPublicKey().getAlgorithm();
      if (pubKeyAlgo.equalsIgnoreCase("DSA")) {
        signatureAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_DSA;
      }

    } catch (Exception e) {
      throw new IdentityProviderException(e.getMessage(), e);
    }

  }

  public String getSignatureAlgorithm() {
    return signatureAlgorithm;
  }

  public void setSignatureAlgorithm(String signatureAlgorithm) {
    this.signatureAlgorithm = signatureAlgorithm;
  }

  public Collection<X509CRL> getCRLs() {
    return null;
  }

  public X509Certificate getEntityCertificate() {
    return issuerCerts[0];
  }

  public Collection<X509Certificate> getEntityCertificateChain() {
    return Arrays.asList(issuerCerts);
  }

  public CredentialContextSet getCredentalContextSet() {
    // TODO Auto-generated method stub
    return null;
  }

  public Class<? extends Credential> getCredentialType() {
    // TODO Auto-generated method stub
    return null;
  }

  public String getEntityId() {
    // TODO Auto-generated method stub
    return null;
  }

  public Collection<String> getKeyNames() {
    // TODO Auto-generated method stub
    return null;
  }

  public PrivateKey getPrivateKey() {
    return issuerPK;
  }

  public PublicKey getPublicKey() {
    return issuerCerts[0].getPublicKey();
  }

  public SecretKey getSecretKey() {
    // TODO Auto-generated method stub
    return null;
  }

  public UsageType getUsageType() {
    // TODO Auto-generated method stub
    return null;
  }

}
TOP

Related Classes of org.wso2.carbon.identity.provider.saml.SignKeyDataHolder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.