/*
 * Copyright 2002-2006 The Apache Software Foundation.
 *
 * Licensed 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
 * imitations under the License.
 */

#include <stdio.h>
#include <openssl/evp.h>
#include <openssl/dsa.h>
#include <openssl/bn.h>
#include <openssl/buffer.h>
#include <jni.h>
#include "org_apache_security_juice_provider_JDKDSASignerOpenSSL.h"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "openSSLNative.h"

/*
 * Class:     org_bouncycastle_jce_provider_JDKDSASignerOpenSSL
 * Method:    initOpenSSLDSA
 * Signature: ([BI[BI[BI[BI[BI)J
 */
JNIEXPORT jlong JNICALL Java_org_apache_security_juice_provider_JDKDSASignerOpenSSL_initOpenSSLDSA (JNIEnv *env, jclass jobj,
                                                                                                    jbyteArray p, jint plen,
                                                                                                    jbyteArray q, jint qlen, 
                                                                                                    jbyteArray g, jint glen,
                                                                                                    jbyteArray x, jint xlen,
                                                                                                    jbyteArray y, jint ylen) {

    jbyte *pb, *qb, *gb, *xb, *yb;
    jclass clazz;

    DSA *key = DSA_new();
    if (key == NULL) {
        JUICEerr(F_initOpenSSLDSA, ERR_R_MALLOC_FAILURE);
        clazz = (*env)->FindClass(env, OUT_OF_MEMORY_EX);
        (*env)->ThrowNew(env, clazz, getErrorStringOpenSSL(OUT_OF_MEMORY_MSG));
        return 0L;
    }
    
    if (p == NULL) {
        clazz = (*env)->FindClass(env, INVALID_KEY_EX);
        (*env)->ThrowNew(env, clazz, "JuICE OpenSSL: Mandatory DSA key parameter 'p' is NULL");
        return 0L;
    }
    else {
        pb = (*env)->GetByteArrayElements(env, p, 0);
        if (pb == NULL) {
            return 0L;
        }
        key->p = BN_bin2bn(pb, plen, NULL);
        (*env)->ReleaseByteArrayElements(env, p, pb, 0);
    }

    if (q == NULL) {
        clazz = (*env)->FindClass(env, INVALID_KEY_EX);
        (*env)->ThrowNew(env, clazz, "JuICE OpenSSL: Mandatory DSA key parameter 'q' is NULL");
        return 0L;
    }
    else {
        qb = (*env)->GetByteArrayElements(env, q, 0);
        if (qb == NULL) {
            return 0L;
        }
        key->q = BN_bin2bn(qb, qlen, NULL);
        (*env)->ReleaseByteArrayElements(env, q, qb, 0);
    }

    if (g == NULL) {
        clazz = (*env)->FindClass(env, INVALID_KEY_EX);
        (*env)->ThrowNew(env, clazz, "JuICE OpenSSL: Mandatory DSA key parameter 'g' is NULL");
        return 0L;
    }
    else {
        gb = (*env)->GetByteArrayElements(env, g, 0);
        if (gb == NULL) {
            return 0L;
        }
        key->g = BN_bin2bn(gb, glen, NULL);
        (*env)->ReleaseByteArrayElements(env, g, gb, 0);
    }

    if (x == NULL && y == NULL) {
        clazz = (*env)->FindClass(env, INVALID_KEY_EX);
        (*env)->ThrowNew(env, clazz, "JuICE OpenSSL: Mandatory DSA public and private key parts are NULL");
        return 0L;
    }
    
    // public key
    if (y != NULL) {
        yb = (*env)->GetByteArrayElements(env, y, 0);
        if (yb == NULL) {
            return 0L;
        }
        key->pub_key = BN_bin2bn(yb, ylen, NULL);
        (*env)->ReleaseByteArrayElements(env, y, yb, 0);
    }

    // private key
    if (x != NULL) {
        xb = (*env)->GetByteArrayElements(env, x, 0);
        if (xb == NULL) {
            return 0L;
        }
        key->priv_key = BN_bin2bn(xb, xlen, NULL);
        (*env)->ReleaseByteArrayElements(env, x, xb, 0);
    }
    return (unsigned long)key;
}

/*
 * Class:     org_bouncycastle_jce_provider_JDKDSASignerOpenSSL
 * Method:    openSSLDSASign
 * Signature: ([BI[BJ)I
 */
JNIEXPORT jint JNICALL Java_org_apache_security_juice_provider_JDKDSASignerOpenSSL_openSSLDSASign (JNIEnv *env, jclass jobj, 
                                                                                                   jbyteArray digest , jint diglen,
                                                                                                   jbyteArray sign, jlong handle) {
#if SIZEOF_INT_P == 4
    DSA *key = (DSA *)((int)(handle & 0xffffffff));
#else
    DSA *key = (DSA *)handle;
#endif
    jbyte *digestBytes, *signBytes;
    int ret, sigLen = 0;
    jclass clazz;

    if (key == NULL) {
        clazz = (*env)->FindClass(env, ILLEGAL_ARGUMENT_EX);
        (*env)->ThrowNew(env, clazz, getErrorStringOpenSSL(KEY_NULL_MSG));
    }
    digestBytes = (*env)->GetByteArrayElements(env, digest, 0);
    if (digestBytes == NULL) {
        return 0;
    }
    signBytes = (*env)->GetByteArrayElements(env, sign, 0);
    if (signBytes == NULL) {
        return 0;
    }

    ret = DSA_sign(0, digestBytes, diglen, signBytes, &sigLen, key);

    (*env)->ReleaseByteArrayElements(env, digest, digestBytes, 0);
    (*env)->ReleaseByteArrayElements(env, sign, signBytes, 0);

    if (ret == 0) {
        clazz = (*env)->FindClass(env, RUNTIME_EX);
        (*env)->ThrowNew(env, clazz, getErrorStringOpenSSL("JuiCE OpenSSL: Error during DSA sign"));
    }
    DSA_free(key);
    return sigLen;

}

/*
 * Class:     org_bouncycastle_jce_provider_JDKDSASignerOpenSSL
 * Method:    openSSLDSAVerify
 * Signature: ([BI[BIJ)I
 */
JNIEXPORT jint JNICALL Java_org_apache_security_juice_provider_JDKDSASignerOpenSSL_openSSLDSAVerify (JNIEnv *env, jclass jobj,
                                                                                                     jbyteArray digest , jint diglen,
                                                                                                     jbyteArray sign, jint sigLen, jlong handle) {
#if SIZEOF_INT_P == 4
    DSA *key = (DSA *)((int)(handle & 0xffffffff));
#else
    DSA *key = (DSA *)handle;
#endif
    jbyte *digestBytes, *signBytes;
    int ret;
    jclass clazz;

    if (key == NULL) {
        clazz = (*env)->FindClass(env, ILLEGAL_ARGUMENT_EX);
        (*env)->ThrowNew(env, clazz, getErrorStringOpenSSL(KEY_NULL_MSG));
    }
    digestBytes = (*env)->GetByteArrayElements(env, digest, 0);
    if (digestBytes == NULL) {
        return 0;
    }
    signBytes = (*env)->GetByteArrayElements(env, sign, 0);
    if (signBytes == NULL) {
        return 0;
    }

    ret = DSA_verify(0, digestBytes, diglen, signBytes, sigLen, key);

    (*env)->ReleaseByteArrayElements(env, digest, digestBytes, 0);
    (*env)->ReleaseByteArrayElements(env, sign, signBytes, 0);

    if (ret ==-1) {
        clazz = (*env)->FindClass(env, RUNTIME_EX);
        (*env)->ThrowNew(env, clazz, getErrorStringOpenSSL("JuiCE OpenSSL: Error during DSA verify"));
    }
    DSA_free(key);
    return ret;
}

/*
 * Class:     org_bouncycastle_jce_provider_JDKDSASignerOpenSSL
 * Method:    openSSLDSASize
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_org_apache_security_juice_provider_JDKDSASignerOpenSSL_openSSLDSASize (JNIEnv *env, jclass jobj,
                                                                                                   jlong handle) {
    jclass clazz;
#if SIZEOF_INT_P == 4
    DSA *key = (DSA *)((int)(handle & 0xffffffff));
#else
    DSA *key = (DSA *)handle;
#endif
    if (key == NULL) {
        clazz = (*env)->FindClass(env, ILLEGAL_ARGUMENT_EX);
        (*env)->ThrowNew(env, clazz, getErrorStringOpenSSL(KEY_NULL_MSG));
    }
    return DSA_size(key);
}

