Sunday, October 30, 2011

SYS.DBMS_SCHEDULER


BEGIN
    SYS.DBMS_SCHEDULER.CREATE_JOB (
            job_name => '"SYSTEM"."MULT2"',
            job_type => 'PLSQL_BLOCK',
            job_action => 'BEGIN 
 INSERT INTO HR.HEXA VALUES(''IND'',''KER'');
 INSERT INTO HR.HEXA VALUES(''NETHERLAND'',''HOLLAND'');
COMMIT;
END;',
            number_of_arguments => 0,
            start_date => SYSTIMESTAMP,
            repeat_interval => 'FREQ=MINUTELY',
            end_date => NULL,
            job_class => '"SYS"."DEFAULT_JOB_CLASS"',
            enabled => FALSE,
            auto_drop => FALSE,
            comments => 'MULT2 Descri');


    SYS.DBMS_SCHEDULER.SET_ATTRIBUTE( 
             name => '"SYSTEM"."MULT2"', 
             attribute => 'logging_level', value => DBMS_SCHEDULER.LOGGING_OFF);
  
  
    SYS.DBMS_SCHEDULER.enable(
             name => '"SYSTEM"."MULT2"');
END; 
/

Scheduled JOB, Oracle DB


BEGIN
dbms_scheduler.create_schedule(  
schedule_name  => 'INTERVAL_EVERY_1_MINUTES',  
  start_date    => trunc(sysdate),  
  repeat_interval => 'freq=MINUTELY',  
  comments     => 'Runtime: Every minut');
 
  dbms_scheduler.create_program  
(program_name=> 'PROG_DO_HR_INSERT',  
 program_type=> 'PLSQL_BLOCK',  
 program_action=> 'BEGIN  INSERT INTO HR.HEXA VALUES('actiont','FRM MINUT SCHEDULE-R'); COMMIT; END',  
 enabled=>true,  
 comments=>'Procedure to save sample Data to \hexa table every Minut'  
 );
 
  dbms_scheduler.create_job  
 (job_name => '30_OCT_2011_Job1',  
  program_name=> 'PROG_DO_HR_INSERT',  
  schedule_name=>'INTERVAL_EVERY_1_MINUTES',  
  enabled=>true,  
  auto_drop=>false,  
  comments=>'Job to  save sample Data to \hexa table every Minut');
 COMMIT; 
 END; 

SCHEDULER_JOB in oracle data base

SELECT JOB_NAME,JOB_TYPE,JOB_ACTION,SCHEDULE_NAME,SCHEDULE_TYPE,JOB_CLASS,STATE FROM  DBA_SCHEDULER_JOBS WHERE JOB_NAME='JobName';
--------------------------------------------------------------------------------------


SELECT owner, program_name, enabled FROM dba_scheduler_programs;

Saturday, October 29, 2011

Oracle Database Scheduler


BEGIN
    sys.DBMS_SCHEDULER.CREATE_SCHEDULE (
     
        repeat_interval  => 'FREQ=MINUTELY;INTERVAL=2',    
        start_date => TO_TIMESTAMP_TZ('2011-10-29 15:57:26 Asia/Riyadh','YYYY-MM-DD HH24.MI.SS TZR'),
        schedule_name  => '"EVERY2minut"');
       
END;

Monday, October 17, 2011

JAVA Card: Applet Code

From :Jan Vossaert`s Smart Card Tutorial

package be.msec.smartcard;

import com.sun.javacard.crypto.ad;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.OwnerPIN;

public class IdentityCard extends Applet {
private final static byte IDENTITY_CARD_CLA =(byte)0x80;

private static final byte VALIDATE_PIN_INS = 0x22;
private static final byte GET_SERIAL_INS = 0x26;

private static final byte GET_IDENTITY_NO = 0x25;

private final static byte PIN_TRY_LIMIT =(byte)0x03;
private final static byte PIN_SIZE =(byte)0x04;

private final static short SW_VERIFICATION_FAILED = 0x6300;
private final static short SW_PIN_VERIFICATION_REQUIRED = 0x6301;

private byte[] serial = new byte[]{(byte)0x4A, (byte)0x61, (byte)0x6e};
private OwnerPIN pin;

private IdentityCard() {
/*
* During instantiation of the applet, all objects are created.
* In this example, this is the 'pin' object.
*/
pin = new OwnerPIN(PIN_TRY_LIMIT,PIN_SIZE);
pin.update(new byte[]{0x01,0x02,0x03,0x04},(short) 0, PIN_SIZE);

/*
* This method registers the applet with the JCRE on the card.
*/
register();
}

/*
* This method is called by the JCRE when installing the applet on the card.
*/
public static void install(byte bArray[], short bOffset, byte bLength)
throws ISOException {
new IdentityCard();
}

/*
* If no tries are remaining, the applet refuses selection.
* The card can, therefore, no longer be used for identification.
*/
public boolean select() {
if (pin.getTriesRemaining()==0)
return false;
return true;
}

/*
* This method is called when the applet is selected and an APDU arrives.
*/
public void process(APDU apdu) throws ISOException {
//A reference to the buffer, where the APDU data is stored, is retrieved.
byte[] buffer = apdu.getBuffer();

//If the APDU selects the applet, no further processing is required.
if(this.selectingApplet())
return;

//Check whether the indicated class of instructions is compatible with this applet.
if (buffer[ISO7816.OFFSET_CLA] != IDENTITY_CARD_CLA)ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
//A switch statement is used to select a method depending on the instruction
switch(buffer[ISO7816.OFFSET_INS]){
case VALIDATE_PIN_INS:
validatePIN(apdu);
break;
case GET_SERIAL_INS:
getSerial(apdu);
break;
//If no matching instructions are found it is indicated in the status word of the response.
//This can be done by using this method. As an argument a short is given that indicates
//the type of warning. There are several predefined warnings in the 'ISO7816' class.
default: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}

/*
* This method is used to authenticate the owner of the card using a PIN code.
*/
private void validatePIN(APDU apdu){
byte[] buffer = apdu.getBuffer();
//The input data needs to be of length 'PIN_SIZE'.
//Note that the byte values in the Lc and Le fields represent values between
//0 and 255. Therefore, if a short representation is required, the following
//code needs to be used: short Lc = (short) (buffer[ISO7816.OFFSET_LC] & 0x00FF);
if(buffer[ISO7816.OFFSET_LC]==PIN_SIZE){
//This method is used to copy the incoming data in the APDU buffer.
apdu.setIncomingAndReceive();
//Note that the incoming APDU data size may be bigger than the APDU buffer
//size and may, therefore, need to be read in portions by the applet.
//Most recent smart cards, however, have buffers that can contain the maximum
//data size. This can be found in the smart card specifications.
//If the buffer is not large enough, the following method can be used:
//
//byte[] buffer = apdu.getBuffer();
//short bytesLeft = (short) (buffer[ISO7816.OFFSET_LC] & 0x00FF);
//Util.arrayCopy(buffer, START, storage, START, (short)5);
//short readCount = apdu.setIncomingAndReceive();
//short i = ISO7816.OFFSET_CDATA;
//while ( bytesLeft > 0){
// Util.arrayCopy(buffer, ISO7816.OFFSET_CDATA, storage, i, readCount);
// bytesLeft -= readCount;
// i+=readCount;
// readCount = apdu.receiveBytes(ISO7816.OFFSET_CDATA);
//}
if (pin.check(buffer, ISO7816.OFFSET_CDATA,PIN_SIZE)==false)
ISOException.throwIt(SW_VERIFICATION_FAILED);
}else ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}

/*
* This method checks whether the user is authenticated and sends
* the serial number.
*/
private void getSerial(APDU apdu){
//If the pin is not validated, a response APDU with the
//'SW_PIN_VERIFICATION_REQUIRED' status word is transmitted.
if(!pin.isValidated())ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED);
else{
//This sequence of three methods sends the data contained in
//'serial' with offset '0' and length 'serial.length'
//to the host application.
apdu.setOutgoing();
apdu.setOutgoingLength((short)serial.length);
apdu.sendBytesLong(serial,(short)0,(short)serial.length);

}
}
}


DOCS 
---------------------------------------------------------

Java Card Applet Deployment

JAVA CARD:Gemalto TOP IM GX4

GPShell script


mode_211
gemXpressoPro
enable_trace
establish_context
card_connect
select -AID A000000018434D00
open_sc -security 3 -keyind 0 -keyver 0 -key 47454d5850524553534f53414d504c45 // Open secure channel
delete -AID 0102030405060708090000
delete -AID 01020304050607080900
install -file smartcard.cap -priv 04 -sdAID A000000018434D00 -nvCodeLimit 4000
card_disconnect
release_context


------------------------------------------
Docs
http://sourceforge.net/apps/mediawiki/globalplatform/index.php?title=GPShell

Java Card compilation code

"C:\jdk1.6.0_27\bin\javac.exe" -source 1.2 -target 1.2 -g -classpath "C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\javacardframework.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\apduio.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\apdutool.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\jcwde.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\converter.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\scriptgen.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\offcardverifier.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\api.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\installer.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\lib\capdump.jar;"C:\Tutorial\eclipse\java_card_kit-2_2_1"\samples\classes;%CLASSPATH% -d .\target C:\Projects\JavaCard\src\sajjad\IdentityCard.java

Friday, October 14, 2011

TLV parser -2


 public static void parseTLV(String TAG,byte[] b) throws Exception
    {
     System.out.println("-------------------- TAG------------------------"+TAG);
    byte[] tagBA=hexStringToByteArray(TAG);
    boolean isTagExist=false;
    int tagStartLoc=0;
    for(int i=0;i<b.length;i++)
    {
        if(isTagExist)
            break;
           for(int j=0;j<tagBA.length;j++)
           {
           if(b[i+j]!=tagBA[j])
               break;
           if(j==tagBA.length-1)
           {
               isTagExist=true;
               tagStartLoc=i;
         
           }
           }
    }
    if(isTagExist)
    {
        for(int i=tagStartLoc;i<b.length;i++)
            System.out.println("  - "+b[i]);
       byte[] bx=new byte[b.length-tagStartLoc];
        System.out.println("Starting Location"+tagStartLoc);
        System.arraycopy(b, tagStartLoc, bx, 0,tagBA.length);
        byte[] length=new byte[1];
        System.arraycopy(b, tagStartLoc+tagBA.length,length, 0,1);
        System.out.println("-------------------- TAG------------------------");
        String lenStr=getHexString(length);
        Integer lenInt=Integer.parseInt(lenStr, 16);
        byte[] data= new byte[lenInt];
        System.arraycopy(b, tagStartLoc+tagBA.length+1,data, 0,lenInt);
        System.out.println(getHexString(data));
     
    }

Simple TLV parser


    public static String getHexString(byte[] b) throws Exception {
   String a="";
    for (int i = 0; i < b.length; i++) {
     a = a+Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    }
    return a;
}
   public static String[] getStringArray(String hexString)
   {
       String[] sr=new String[hexString.length()/2];
       
        for(int i=0;i<hexString.length()-1;i++)
        {
            if(i%2==0)
             
        sr[i/2]=hexString.substring(i, i+2);
                 
                }
       return sr;
   }
   public static String[] dataToStrArray(byte[] b) throws Exception
   {
     
       String s=getHexString(b);
       String[] sr=getStringArray(s);
       return sr;
     
   }
   public  static void parseTLV(String[] r, String tag)
   {
      if(tag.length()==2)
      {
        Integer len=new Integer(0) ;
         for(int i=0;i<r.length;i++)
   
          if(r[i].equalsIgnoreCase(tag))
          { //System.out.println("TAG"+tag+"\n Len:"+r[i+1]);
             len=Integer.parseInt(r[i+1], 16);
             System.out.println();
            for (Integer j=1;j<=len;j++)
            {
                System.out.print(r[i+j+1]+" \t");
            }
            System.out.println();
          }
      }
      else if(tag.length()==4)
      {
            Integer len=new Integer(0) ;
            String tagHandler;
         for(int i=0;i<r.length-1;i++)
         {    tagHandler=r[i]+r[i+1];
          if(tagHandler.equalsIgnoreCase(tag))
          {
              System.out.println("TAG"+tagHandler+"\n Len:"+r[i+2]);
             len=Integer.parseInt(r[i+2], 16);
             System.out.println();
            for (Integer j=1;j<=len;j++)
            {
                System.out.print(r[i+j+2]+" \t");
            }
            System.out.println();
          }
         }
      }
       
   
   }
 
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }