]> andersk Git - openssh.git/blob - scard/Ssh.java
- (djm) Sync scard/ stuff
[openssh.git] / scard / Ssh.java
1 // $Id$
2 //
3 // Ssh.java
4 // SSH / smartcard integration project, smartcard side
5 //
6 // Tomoko Fukuzawa, created, Feb., 2000
7 //
8 // Naomaru Itoi, modified, Apr., 2000
9 //
10
11 // copyright 2000
12 // the regents of the university of michigan
13 // all rights reserved
14 //
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
24 // also be included.
25 //
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
37 // such damages.
38
39 import javacard.framework.*;
40 import javacardx.framework.*;
41 import javacardx.crypto.*;
42
43 public class Ssh extends javacard.framework.Applet
44 {
45     /* constants declaration */
46     // code of CLA byte in the command APDU header
47     static final byte Ssh_CLA =(byte)0x05;
48
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;
54
55     /* instance variables declaration */
56     static final short keysize = 1024;
57
58     //RSA_CRT_PrivateKey rsakey;
59     AsymKey rsakey;
60     CyberflexFile file;
61     CyberflexOS os;
62
63     byte buffer[];
64
65     static byte[] keyHdr = {(byte)0xC2, (byte)0x01, (byte)0x05};
66
67     private Ssh()
68     {
69         file = new CyberflexFile();
70         os = new CyberflexOS();
71
72         rsakey = new RSA_CRT_PrivateKey (keysize);
73
74         if ( ! rsakey.isSupportedLength (keysize) )
75             ISOException.throwIt (ISO.SW_WRONG_LENGTH);
76
77         register();
78     } // end of the constructor
79
80     public boolean select() {
81         if (!rsakey.isInitialized())
82             rsakey.setKeyInstance ((short)0xc8, (short)0x10);
83
84         return true;
85     }
86
87     public static void install(APDU apdu)
88     {
89         new Ssh();      // create a Ssh applet instance (card)
90     } // end of install method
91
92     public static void main(String args[]) {
93         ISOException.throwIt((short) 0x9000);
94     }
95
96     public void process(APDU apdu)
97     {
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();
102
103         // verify that if the applet can accept this
104         // APDU message
105         // NI: change suggested by Wayne Dyksen, Purdue
106         if (buffer[ISO.OFFSET_INS] == ISO.INS_SELECT)
107             ISOException.throwIt(ISO.SW_NO_ERROR);
108
109         switch (buffer[ISO.OFFSET_INS]) {
110         case DECRYPT:
111             if (buffer[ISO.OFFSET_CLA] != Ssh_CLA)
112                 ISOException.throwIt(ISO.SW_CLA_NOT_SUPPORTED);
113             //decrypt (apdu);
114             short size = (short) (buffer[ISO.OFFSET_LC] & 0x00FF);
115
116             if (apdu.setIncomingAndReceive() != size)
117                 ISOException.throwIt (ISO.SW_WRONG_LENGTH);
118
119             rsakey.cryptoUpdate (buffer, (short) ISO.OFFSET_CDATA, size,
120                                  buffer, (short) ISO.OFFSET_CDATA);
121
122             apdu.setOutgoingAndSend ((short) ISO.OFFSET_CDATA, size);
123             return;
124         case GET_PUBKEY:
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));
129             return;
130         case GET_KEYLENGTH:
131             buffer[0] = (byte)((keysize >> 8) & 0xff);
132             buffer[1] = (byte)(keysize & 0xff);
133             apdu.setOutgoingAndSend ((short)0, (short)2);
134             return;
135         case GET_RESPONSE:
136             return;
137         default:
138             ISOException.throwIt (ISO.SW_INS_NOT_SUPPORTED);
139         }
140
141     } // end of process method
142
143 } // end of class Ssh
This page took 0.0564 seconds and 5 git commands to generate.