Search in sources :

Example 1 with FSSignatureException

use of com.sun.identity.federation.services.util.FSSignatureException in project OpenAM by OpenRock.

the class FSIntersiteTransferService method signAndReturnQueryString.

private String signAndReturnQueryString(String queryString, String certAlias) {
    if (FSUtils.debug.messageEnabled()) {
        FSUtils.debug.message("FSIntersiteTransferService." + "signAndReturnQueryString: Called");
    }
    if (queryString == null || queryString.length() == 0) {
        FSUtils.debug.error("FSIntersiteTransferService." + "signAndReturnQueryString: " + FSUtils.bundle.getString("nullInput"));
        return null;
    } else {
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("FSIntersiteTransferService." + "signAndReturnQueryString: certAlias: " + certAlias);
        }
    }
    if (queryString == null || queryString.length() == 0) {
        FSUtils.debug.error("FSIntersiteTransferService." + "signAndReturnQueryString: " + FSUtils.bundle.getString("nullInput"));
        return null;
    }
    FSSignatureManager manager = FSSignatureManager.getInstance();
    String sigAlg = IFSConstants.ALGO_ID_SIGNATURE_RSA_JCA;
    if (manager.getKeyProvider().getPrivateKey(certAlias).getAlgorithm().equals(IFSConstants.KEY_ALG_RSA)) {
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("FSIntersiteTransferService.signAndReturnQueryString: " + "private key algorithm is: RSA");
        }
        sigAlg = IFSConstants.ALGO_ID_SIGNATURE_RSA_JCA;
    } else if (manager.getKeyProvider().getPrivateKey(certAlias).getAlgorithm().equals(IFSConstants.KEY_ALG_DSA)) {
        if (FSUtils.debug.messageEnabled()) {
            FSUtils.debug.message("FSIntersiteTransferService.signAndReturnQueryString: " + "private key algorithm is: DSA");
        }
        sigAlg = IFSConstants.ALGO_ID_SIGNATURE_DSA_JCA;
    } else {
        FSUtils.debug.error("FSIntersiteTransferService.signAndReturnQueryString: " + "private key algorithm is not supported");
        return null;
    }
    byte[] signature = null;
    if (sigAlg == null || sigAlg.length() == 0) {
        sigAlg = IFSConstants.DEF_SIG_ALGO_JCA;
    }
    if (queryString.charAt(queryString.length() - 1) != '&') {
        queryString = queryString + "&";
    }
    String algoId = null;
    if (sigAlg.equals(IFSConstants.ALGO_ID_SIGNATURE_DSA_JCA)) {
        algoId = IFSConstants.ALGO_ID_SIGNATURE_DSA;
    } else if (sigAlg.equals(IFSConstants.ALGO_ID_SIGNATURE_RSA_JCA)) {
        algoId = IFSConstants.ALGO_ID_SIGNATURE_RSA;
    } else {
        FSUtils.debug.error("FSIntersiteTransferService.signAndReturnQueryString: " + "Invalid signature algorithim");
        return null;
    }
    queryString = queryString + "SigAlg=" + URLEncDec.encode(algoId);
    if (FSUtils.debug.messageEnabled()) {
        FSUtils.debug.message("FSIntersiteTransferService.signAndReturnQueryString: " + "Querystring to be signed: " + queryString);
    }
    try {
        signature = manager.signBuffer(queryString, certAlias, sigAlg);
    } catch (FSSignatureException se) {
        FSUtils.debug.error("FSIntersiteTransferService." + "signAndReturnQueryString: FSSignatureException occured " + "while signing query string: " + se.getMessage());
        return null;
    }
    if (signature == null) {
        FSUtils.debug.error("FSIntersiteTransferService." + "signAndReturnQueryString: Signature generated is null");
        return null;
    }
    String encodedSig = Base64.encode(signature);
    queryString = queryString + "&" + "Signature=" + URLEncDec.encode(encodedSig);
    if (FSUtils.debug.messageEnabled()) {
        FSUtils.debug.message("FSIntersiteTransferService." + "signAndReturnQueryString:Signed Querystring: " + queryString);
    }
    return queryString;
}
Also used : FSSignatureManager(com.sun.identity.federation.services.util.FSSignatureManager) FSSignatureException(com.sun.identity.federation.services.util.FSSignatureException)

Aggregations

FSSignatureException (com.sun.identity.federation.services.util.FSSignatureException)1 FSSignatureManager (com.sun.identity.federation.services.util.FSSignatureManager)1