[android] add HMAC support (currently optional)

This commit is contained in:
Michael Rash 2013-12-23 22:29:51 -05:00
parent dc19e07d65
commit 204bc6e58f
3 changed files with 59 additions and 2 deletions

View File

@ -45,7 +45,7 @@ jstring Java_com_max2idea_android_fwknop_Fwknop_sendSPAPacket(JNIEnv* env,
fko_ctx_t ctx; fko_ctx_t ctx;
fwknop_options_t opts; fwknop_options_t opts;
int res; int res, hmac_str_len = 0;
char res_msg[MSG_BUFSIZE+1] = {0}; char res_msg[MSG_BUFSIZE+1] = {0};
char spa_msg[MSG_BUFSIZE+1] = {0}; char spa_msg[MSG_BUFSIZE+1] = {0};
@ -72,6 +72,10 @@ jstring Java_com_max2idea_android_fwknop_Fwknop_sendSPAPacket(JNIEnv* env,
jstring jpasswd = (*env)->GetObjectField(env, thiz, fid); jstring jpasswd = (*env)->GetObjectField(env, thiz, fid);
const char *passwd_str = (*env)->GetStringUTFChars(env, jpasswd, 0); const char *passwd_str = (*env)->GetStringUTFChars(env, jpasswd, 0);
fid = (*env)->GetFieldID(env, c, "hmac_str", "Ljava/lang/String;");
jstring jhmac = (*env)->GetObjectField(env, thiz, fid);
const char *hmac_str = (*env)->GetStringUTFChars(env, jhmac, 0);
fid = (*env)->GetFieldID(env, c, "fw_timeout_str", "Ljava/lang/String;"); fid = (*env)->GetFieldID(env, c, "fw_timeout_str", "Ljava/lang/String;");
jstring jfwtimeout = (*env)->GetObjectField(env, thiz, fid); jstring jfwtimeout = (*env)->GetObjectField(env, thiz, fid);
const char *fw_timeout_str = (*env)->GetStringUTFChars(env, jfwtimeout, 0); const char *fw_timeout_str = (*env)->GetStringUTFChars(env, jfwtimeout, 0);
@ -99,6 +103,12 @@ jstring Java_com_max2idea_android_fwknop_Fwknop_sendSPAPacket(JNIEnv* env,
goto cleanup2; goto cleanup2;
} }
/* Using an HMAC is optional (currently)
*/
if(hmac_str != NULL) {
hmac_str_len = (int)strlen(hmac_str);
}
/* Set our spa server info /* Set our spa server info
*/ */
opts.spa_server_str = (char*)destip_str; opts.spa_server_str = (char*)destip_str;
@ -130,9 +140,20 @@ jstring Java_com_max2idea_android_fwknop_Fwknop_sendSPAPacket(JNIEnv* env,
goto cleanup; goto cleanup;
} }
/* Set the HMAC mode if necessary
*/
if (hmac_str_len > 0) {
res = fko_set_spa_hmac_type(ctx, FKO_DEFAULT_HMAC_MODE);
if (res != FKO_SUCCESS) {
strcpy(res_msg, fko_errmsg("Error setting SPA HMAC type", res));
goto cleanup;
}
}
/* Finalize the context data (Encrypt and encode). /* Finalize the context data (Encrypt and encode).
*/ */
res = fko_spa_data_final(ctx, (char*)passwd_str); res = fko_spa_data_final(ctx, (char*)passwd_str,
(int)strlen(passwd_str), (char *)hmac_str, hmac_str_len);
if (res != FKO_SUCCESS) { if (res != FKO_SUCCESS) {
strcpy(res_msg, fko_errmsg("Error generating SPA data", res)); strcpy(res_msg, fko_errmsg("Error generating SPA data", res));
goto cleanup; goto cleanup;
@ -173,6 +194,7 @@ cleanup2:
(*env)->ReleaseStringUTFChars(env, jallowip, allowip_str); (*env)->ReleaseStringUTFChars(env, jallowip, allowip_str);
(*env)->ReleaseStringUTFChars(env, jdestip, destip_str); (*env)->ReleaseStringUTFChars(env, jdestip, destip_str);
(*env)->ReleaseStringUTFChars(env, jpasswd, passwd_str); (*env)->ReleaseStringUTFChars(env, jpasswd, passwd_str);
(*env)->ReleaseStringUTFChars(env, jhmac, hmac_str);
(*env)->ReleaseStringUTFChars(env, jfwtimeout, fw_timeout_str); (*env)->ReleaseStringUTFChars(env, jfwtimeout, fw_timeout_str);
/* Log and return a string of success or error message. /* Log and return a string of success or error message.

View File

@ -133,6 +133,28 @@
android:textSize="20dip" android:textSize="20dip"
/> />
</LinearLayout> </LinearLayout>
<LinearLayout android:id="@+id/hmacl"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/hmacStr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HMAC Key: "
android:textSize="20dip"
/>
<EditText
android:id="@+id/hmac"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:singleLine="true"
android:password="true"
android:textSize="20dip"
/>
</LinearLayout>
<RelativeLayout android:id="@+id/startAppl" <RelativeLayout android:id="@+id/startAppl"
android:orientation="horizontal" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_width="fill_parent"

View File

@ -94,6 +94,7 @@ public class Fwknop extends Activity {
private String output; private String output;
private Spinner mAllowip; private Spinner mAllowip;
private EditText mPasswd; private EditText mPasswd;
private EditText mHmac;
private EditText mDestip; private EditText mDestip;
private Spinner mAccessProto; private Spinner mAccessProto;
private EditText mAccessPort; private EditText mAccessPort;
@ -102,6 +103,7 @@ public class Fwknop extends Activity {
private String access_str; private String access_str;
private String allowip_str; private String allowip_str;
private String passwd_str; private String passwd_str;
private String hmac_str;
private String destip_str; private String destip_str;
private String fw_timeout_str; private String fw_timeout_str;
private CheckBox mCheck; private CheckBox mCheck;
@ -303,6 +305,15 @@ public class Fwknop extends Activity {
return; return;
} }
if (this.mHmac != null && !this.mHmac.getText().toString().trim().equals("")) {
this.hmac_str = mHmac.getText().toString();
edit.putString("hmac_str", mHmac.getText().toString());
} else {
// the HMAC is currently optional
this.hmac_str = "";
edit.putString("hmac_str", this.hmac_str);
}
if (this.mDestip != null && !this.mDestip.getText().toString().trim().equals("")) { if (this.mDestip != null && !this.mDestip.getText().toString().trim().equals("")) {
this.destip_str = mDestip.getText().toString(); this.destip_str = mDestip.getText().toString();
edit.putString("destip_str", mDestip.getText().toString()); edit.putString("destip_str", mDestip.getText().toString());
@ -367,6 +378,8 @@ public class Fwknop extends Activity {
this.mPasswd = (EditText) findViewById(R.id.passwd); this.mPasswd = (EditText) findViewById(R.id.passwd);
this.mOutput = (TextView) findViewById(R.id.output); this.mOutput = (TextView) findViewById(R.id.output);
this.mHmac = (EditText) findViewById(R.id.hmac);
mUnlock = (ImageButton) findViewById(R.id.unlock); mUnlock = (ImageButton) findViewById(R.id.unlock);
mUnlock.setOnClickListener(new OnClickListener() { mUnlock.setOnClickListener(new OnClickListener() {