Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitab551fb

Browse files
author
Wolfgang Glas
committed
Allow the creation of the application directory on hardware tokens.
1 parentb6a00db commitab551fb

File tree

8 files changed

+141
-29
lines changed

8 files changed

+141
-29
lines changed

‎pkcs15/src/main/java/org/opensc/pkcs15/PKCS15Exception.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public class PKCS15Exception extends IOException {
101101
* @param errorCode The error code as returned by the token.
102102
*/
103103
publicPKCS15Exception(interrorCode) {
104-
super();
104+
super("card error [0x"+Integer.toHexString(errorCode)+"]");
105105
this.errorCode =errorCode;
106106
}
107107

@@ -111,7 +111,7 @@ public PKCS15Exception(int errorCode) {
111111
* @param errorCode The error code as returned by the token.
112112
*/
113113
publicPKCS15Exception(Stringmsg,Throwablecause,interrorCode) {
114-
super(msg,cause);
114+
super(msg +" [0x"+Integer.toHexString(errorCode)+"]",cause);
115115
this.errorCode =errorCode;
116116
}
117117

@@ -120,7 +120,7 @@ public PKCS15Exception(String msg, Throwable cause, int errorCode) {
120120
* @param errorCode The error code as returned by the token.
121121
*/
122122
publicPKCS15Exception(Stringmsg,interrorCode) {
123-
super(msg);
123+
super(msg +" [0x"+Integer.toHexString(errorCode)+"]");
124124
this.errorCode =errorCode;
125125
}
126126

@@ -129,7 +129,7 @@ public PKCS15Exception(String msg, int errorCode) {
129129
* @param errorCode The error code as returned by the token.
130130
*/
131131
publicPKCS15Exception(Throwablecause,interrorCode) {
132-
super(cause);
132+
super("card error [0x"+Integer.toHexString(errorCode)+"]",cause);
133133
this.errorCode =errorCode;
134134
}
135135

‎pkcs15/src/main/java/org/opensc/pkcs15/application/impl/ApplicationFactoryImpl.java‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
importorg.bouncycastle.asn1.ASN1InputStream;
3333
importorg.bouncycastle.asn1.ASN1OutputStream;
3434
importorg.opensc.pkcs15.AIDs;
35+
importorg.opensc.pkcs15.PKCS15Exception;
3536
importorg.opensc.pkcs15.application.Application;
3637
importorg.opensc.pkcs15.application.ApplicationFactory;
3738
importorg.opensc.pkcs15.asn1.ISO7816ApplicationTemplate;
3839
importorg.opensc.pkcs15.asn1.ISO7816Applications;
40+
importorg.opensc.pkcs15.token.EF;
3941
importorg.opensc.pkcs15.token.Token;
4042
importorg.opensc.pkcs15.token.TokenFileAcl;
4143
importorg.opensc.pkcs15.token.impl.EFAclImpl;
@@ -97,7 +99,16 @@ protected Application constructApplication(Token token, byte[] aid)
9799
protectedISO7816ApplicationsreadApplications(Tokentoken)throwsIOException
98100
{
99101
token.selectMF();
100-
if (token.selectEF(DIR_PATH) ==null)returnnull;
102+
103+
try {
104+
if (token.selectEF(DIR_PATH) ==null)returnnull;
105+
}catch (PKCS15Exceptione) {
106+
107+
if (e.getErrorCode() ==PKCS15Exception.ERROR_FILE_NOT_FOUND)
108+
returnnull;
109+
else
110+
throwe;
111+
}
101112

102113
InputStreamis =token.readEFData();
103114

@@ -128,9 +139,18 @@ protected void writeApplications(Token token, ISO7816Applications apps) throws I
128139
{
129140
token.selectMF();
130141

131-
if (token.selectEF(DIR_PATH) ==null) {
142+
EFef =null;
143+
144+
try {
145+
ef =token.selectEF(DIR_PATH);
146+
}catch (PKCS15Exceptione) {
147+
if (e.getErrorCode() !=PKCS15Exception.ERROR_FILE_NOT_FOUND)
148+
throwe;
149+
}
150+
151+
if (ef ==null) {
132152
token.createEF(DIR_PATH,
133-
0L,newEFAclImpl(TokenFileAcl.AC_ALWAYS,
153+
512L,newEFAclImpl(TokenFileAcl.AC_ALWAYS,
134154
TokenFileAcl.AC_ALWAYS,
135155
TokenFileAcl.AC_ALWAYS,
136156
TokenFileAcl.AC_ALWAYS,
@@ -141,7 +161,7 @@ protected void writeApplications(Token token, ISO7816Applications apps) throws I
141161
TokenFileAcl.AC_ALWAYS
142162
));
143163

144-
token.selectEF(DIR_PATH);
164+
ef =token.selectEF(DIR_PATH);
145165
}
146166

147167
OutputStreamos =token.writeEFData();

‎pkcs15/src/main/java/org/opensc/pkcs15/token/Token.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
*/
3636
publicinterfaceTokenextendsCloseable {
3737

38+
/**
39+
* Reset the token to the state, where only the master file (MF) exists.
40+
*
41+
* Hardware token implementations should take care to execute a script,
42+
* which undertakes the necessary steps on the card depending on the state
43+
* of the card.
44+
*/
45+
voidreset()throwsIOException;
46+
3847
TokenFilegetCurrentFile()throwsIOException;
3948

4049
TokenFileselect(intpath)throwsIOException;

‎pkcs15/src/main/java/org/opensc/pkcs15/token/impl/CardOSToken.java‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
importorg.apache.commons.logging.Log;
4141
importorg.apache.commons.logging.LogFactory;
4242
importorg.opensc.pkcs15.PKCS15Exception;
43+
importorg.opensc.pkcs15.script.Command;
44+
importorg.opensc.pkcs15.script.ScriptParser;
45+
importorg.opensc.pkcs15.script.ScriptParserFactory;
46+
importorg.opensc.pkcs15.script.ScriptResource;
47+
importorg.opensc.pkcs15.script.ScriptResourceFactory;
4348
importorg.opensc.pkcs15.token.DF;
4449
importorg.opensc.pkcs15.token.DFAcl;
4550
importorg.opensc.pkcs15.token.EF;
@@ -70,6 +75,9 @@ public class CardOSToken implements Token {
7075
privatestaticfinalintDEFAULT_LE =252;
7176
privatestaticfinalintDEFAULT_EXTENDED_LE =65532;
7277

78+
privatestaticfinalStringDEFAULT_RESET_RESOURCE ="classpath:org/opensc/pkcs15/scripts/cardos/v43b_reset.ser";
79+
privatestaticfinalStringRESET_SCRIPT_PROPERTY ="org.opensc.pkcs15.scripts.cardos.v43b_reset";
80+
7381
privateCardChannelchannel;
7482
privateTokenFilecurrentFile;
7583

@@ -81,6 +89,34 @@ public CardOSToken(CardChannel channel) {
8189
this.channel =channel;
8290
}
8391

92+
/* (non-Javadoc)
93+
* @see org.opensc.pkcs15.token.Token#reset()
94+
*/
95+
@Override
96+
publicvoidreset()throwsIOException {
97+
98+
Stringres =System.getProperty(RESET_SCRIPT_PROPERTY);
99+
100+
if (res ==null)
101+
res =DEFAULT_RESET_RESOURCE;
102+
103+
ScriptResourceFactoryscriptResourceFactory =ScriptResourceFactory.getInstance();
104+
ScriptResourcer =scriptResourceFactory.getScriptResource(res);
105+
106+
ScriptParserFactoryscriptParserFactory =ScriptParserFactory.getInstance();
107+
ScriptParserparser =scriptParserFactory.getScriptParser(res.substring(res.lastIndexOf('.')+1));
108+
109+
Commandcmd =parser.parseScript(r);
110+
111+
try {
112+
while (cmd !=null) {
113+
cmd =cmd.execute(this.channel);
114+
}
115+
}catch (CardExceptione) {
116+
thrownewPKCS15Exception("Error executing reset script ["+res+"].",e);
117+
}
118+
}
119+
84120
/* (non-Javadoc)
85121
* @see org.opensc.pkcs15.token.Token#close()
86122
*/
@@ -150,7 +186,7 @@ public DF createDF(int path, long size, DFAcl acl) throws IOException {
150186
data[1] = (byte)(data.length -2);
151187

152188
// CREATE FILE, P1=0x00, P2=0x00, ID -> read current EF from position 0.
153-
CommandAPDUcmd =newCommandAPDU(0x00,0xE4,0x00,0x00,data,DEFAULT_LE);
189+
CommandAPDUcmd =newCommandAPDU(0x00,0xE0,0x00,0x00,data,DEFAULT_LE);
154190

155191
try {
156192
ResponseAPDUresp =this.channel.transmit(cmd);
@@ -223,7 +259,7 @@ public EF createEF(int path, long size, EFAcl acl) throws IOException {
223259
data[1] = (byte)(data.length -2);
224260

225261
// CREATE FILE, P1=0x00, P2=0x00, ID -> read current EF from position 0.
226-
CommandAPDUcmd =newCommandAPDU(0x00,0xE4,0x00,0x00,data,DEFAULT_LE);
262+
CommandAPDUcmd =newCommandAPDU(0x00,0xE0,0x00,0x00,data,DEFAULT_LE);
227263

228264
try {
229265
ResponseAPDUresp =this.channel.transmit(cmd);

‎pkcs15/src/main/java/org/opensc/pkcs15/token/impl/SoftwareToken.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
importorg.opensc.pkcs15.token.Token;
4040
importorg.opensc.pkcs15.token.TokenFile;
4141
importorg.opensc.pkcs15.token.TokenFileAcl;
42+
importorg.opensc.pkcs15.util.Util;
4243

4344
/**
4445
* @author wglas
@@ -70,6 +71,21 @@ public SoftwareToken(File directory) {
7071
if (!this.mfFile.exists())
7172
this.mfFile.mkdir();
7273
}
74+
75+
/* (non-Javadoc)
76+
* @see org.opensc.pkcs15.token.Token#reset()
77+
*/
78+
@Override
79+
publicvoidreset()throwsIOException {
80+
81+
this.currentFile =this.mfFile;
82+
this.currentPath =PathHelper.MF_PATH;
83+
84+
if (this.mfFile.exists())
85+
Util.rmdirRecursive(this.mfFile);
86+
87+
this.mfFile.mkdir();
88+
}
7389

7490
/* (non-Javadoc)
7591
* @see org.opensc.pkcs15.token.Token#close()

‎pkcs15/src/main/java/org/opensc/pkcs15/util/Util.java‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
packageorg.opensc.pkcs15.util;
2424

25+
importjava.io.File;
26+
importjava.io.IOException;
27+
2528
/**
2629
* Static helper functions.
2730
*
@@ -94,4 +97,30 @@ static public String asHexMask(int[] b) {
9497

9598
returnsb.toString();
9699
}
100+
101+
/**
102+
* Recursively delete a directory.
103+
*
104+
* @param dir The directory to delete.
105+
* @throws IOException If the directory could not be deleted.
106+
*/
107+
publicstaticvoidrmdirRecursive(Filedir)throwsIOException {
108+
109+
File[]children =dir.listFiles();
110+
111+
for (Filechild :children) {
112+
113+
if (child.isDirectory()) {
114+
if (!child.getName().equals(".") && !child.getName().equals(".."))
115+
rmdirRecursive(child);
116+
}
117+
else {
118+
if (!child.delete())
119+
thrownewIOException("Cannot delete file ["+child+"].");
120+
}
121+
}
122+
123+
if (!dir.delete())
124+
thrownewIOException("Cannot delete directory ["+dir+"].");
125+
}
97126
}

‎pkcs15/src/test/java/test/org/opensc/pkcs15/TestHardwareToken.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,22 @@ public void testApplicationFactory() throws IOException
2626
assertEquals(AIDs.PKCS15_AID,apps.get(0).getAID());
2727
}
2828

29+
publicvoidtestApplicationCreation()throwsIOException
30+
{
31+
Tokentoken =tokenFactory.newHardwareToken(this.card);
32+
33+
token.reset();
34+
35+
Applicationapp =applicationFactory.createApplication(token,AIDs.PKCS15_AID);
36+
37+
assertNotNull(app);
38+
39+
List<Application>apps =applicationFactory.listApplications(token);
40+
41+
assertNotNull(apps);
42+
assertEquals(1,apps.size());
43+
assertEquals(AIDs.PKCS15_AID,apps.get(0).getAID());
44+
45+
}
46+
2947
}

‎pkcs15/src/test/java/test/org/opensc/pkcs15/TestSoftwareToken.java‎

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
importorg.opensc.pkcs15.token.Token;
3535
importorg.opensc.pkcs15.token.TokenContext;
3636
importorg.opensc.pkcs15.token.TokenFactory;
37+
importorg.opensc.pkcs15.util.Util;
3738

3839
publicclassTestSoftwareTokenextendsTestCase {
3940

@@ -45,23 +46,6 @@ public class TestSoftwareToken extends TestCase {
4546
privateFiletokenDir;
4647
privateFiletokenDir2;
4748

48-
staticvoidrmDirForce(Filedir)
49-
{
50-
File []entries =dir.listFiles();
51-
52-
for (Fileentry:entries)
53-
{
54-
if (entry.isDirectory()) {
55-
if (!entry.getName().equals(".") && !entry.getName().equals(".."))
56-
rmDirForce(entry);
57-
}
58-
else {
59-
entry.delete();
60-
}
61-
}
62-
dir.delete();
63-
}
64-
6549
privateZipInputStreamgetTestZip() {
6650
returnnewZipInputStream(TestSoftwareToken.class.getClassLoader().
6751
getResourceAsStream("test/org/opensc/pkcs15/test-ca.zip"));
@@ -73,12 +57,12 @@ protected void setUp() throws Exception {
7357

7458
this.tokenDir2 =newFile(targetDir,"test-create");
7559
if (this.tokenDir2.exists())
76-
rmDirForce(this.tokenDir2);
60+
Util.rmdirRecursive(this.tokenDir2);
7761
this.tokenDir2.mkdir();
7862

7963
this.tokenDir =newFile(targetDir,"test-ca");
8064
if (this.tokenDir.exists())
81-
rmDirForce(this.tokenDir);
65+
Util.rmdirRecursive(this.tokenDir);
8266
this.tokenDir.mkdir();
8367

8468
ZipInputStreamzis =this.getTestZip();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp