4 // SSH / smartcard integration project, smartcard side
6 // Tomoko Fukuzawa, created, Feb., 2000
8 // Naomaru Itoi, modified, Apr., 2000
12 // the regents of the university of michigan
13 // all rights reserved
15 // permission is granted to use, copy, create derivative works
16 // and redistribute this software and such derivative works
17 // for any purpose, so long as the name of the university of
18 // michigan is not used in any advertising or publicity
19 // pertaining to the use or distribution of this software
20 // without specific, written prior authorization. if the
21 // above copyright notice or any other identification of the
22 // university of michigan is included in any copy of any
23 // portion of this software, then the disclaimer below must
26 // this software is provided as is, without representation
27 // from the university of michigan as to its fitness for any
28 // purpose, and without warranty by the university of
29 // michigan of any kind, either express or implied, including
30 // without limitation the implied warranties of
31 // merchantability and fitness for a particular purpose. the
32 // regents of the university of michigan shall not be liable
33 // for any damages, including special, indirect, incidental, or
34 // consequential damages, with respect to any claim arising
35 // out of or in connection with the use of the software, even
36 // if it has been or is hereafter advised of the possibility of
39 import javacard.framework.*;
40 import javacardx.framework.*;
41 import javacardx.crypto.*;
43 public class Ssh extends javacard.framework.Applet
45 /* constants declaration */
46 // code of CLA byte in the command APDU header
47 static final byte Ssh_CLA =(byte)0x05;
49 // codes of INS byte in the command APDU header
50 static final byte DECRYPT = (byte) 0x10;
51 static final byte GET_KEYLENGTH = (byte) 0x20;
52 static final byte GET_PUBKEY = (byte) 0x30;
53 static final byte GET_RESPONSE = (byte) 0xc0;
55 /* instance variables declaration */
56 static final short keysize = 1024;
58 //RSA_CRT_PrivateKey rsakey;
65 static byte[] keyHdr = {(byte)0xC2, (byte)0x01, (byte)0x05};
69 file = new CyberflexFile();
70 os = new CyberflexOS();
72 rsakey = new RSA_CRT_PrivateKey (keysize);
74 if ( ! rsakey.isSupportedLength (keysize) )
75 ISOException.throwIt (ISO.SW_WRONG_LENGTH);
78 } // end of the constructor
80 public boolean select() {
81 if (!rsakey.isInitialized())
82 rsakey.setKeyInstance ((short)0xc8, (short)0x10);
87 public static void install(APDU apdu)
89 new Ssh(); // create a Ssh applet instance (card)
90 } // end of install method
92 public static void main(String args[]) {
93 ISOException.throwIt((short) 0x9000);
96 public void process(APDU apdu)
98 // APDU object carries a byte array (buffer) to
99 // transfer incoming and outgoing APDU header
100 // and data bytes between card and CAD
101 buffer = apdu.getBuffer();
103 // verify that if the applet can accept this
105 // NI: change suggested by Wayne Dyksen, Purdue
106 if (buffer[ISO.OFFSET_INS] == ISO.INS_SELECT)
107 ISOException.throwIt(ISO.SW_NO_ERROR);
109 switch (buffer[ISO.OFFSET_INS]) {
111 if (buffer[ISO.OFFSET_CLA] != Ssh_CLA)
112 ISOException.throwIt(ISO.SW_CLA_NOT_SUPPORTED);
114 short size = (short) (buffer[ISO.OFFSET_LC] & 0x00FF);
116 if (apdu.setIncomingAndReceive() != size)
117 ISOException.throwIt (ISO.SW_WRONG_LENGTH);
119 rsakey.cryptoUpdate (buffer, (short) ISO.OFFSET_CDATA, size,
120 buffer, (short) ISO.OFFSET_CDATA);
122 apdu.setOutgoingAndSend ((short) ISO.OFFSET_CDATA, size);
125 file.selectFile((short)(0x3f<<8)); // select root
126 file.selectFile((short)(('s'<<8)|'h')); // select public key file
127 os.readBinaryFile (buffer, (short)0, (short)0, (short)(keysize/8));
128 apdu.setOutgoingAndSend((short)0, (short)(keysize/8));
131 buffer[0] = (byte)((keysize >> 8) & 0xff);
132 buffer[1] = (byte)(keysize & 0xff);
133 apdu.setOutgoingAndSend ((short)0, (short)2);
138 ISOException.throwIt (ISO.SW_INS_NOT_SUPPORTED);
141 } // end of process method
143 } // end of class Ssh