Fully Verified JAVA CARD API Reference Implementation



Similar documents
Java Card Applet Firewall Exploration and Exploitation

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Java Interview Questions and Answers

Malicious Code on Java Card Smartcards: Attacks and Countermeasures

Lecture 9 verifying temporal logic

JavaCard. Java Card - old vs new

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Software Engineering Techniques

StaRVOOrS: A Tool for Combined Static and Runtime Verification of Java

Testing the Java Card Applet Firewall

Lecture Notes on Linear Search

RMI Client Application Programming Interface

Regression Verification: Status Report

Rigorous Software Development CSCI-GA

Habanero Extreme Scale Software Research Project

Design by Contract beyond class modelling

Unit-testing with JML

Java Applet and Terminal Application for Financial transactions

Java Card. Smartcards. Demos. . p.1/30

Java Card TM Open Platform for Smart Cards

C++ INTERVIEW QUESTIONS

CS 111 Classes I 1. Software Organization View to this point:

Smart Cards a(s) Safety Critical Systems

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Explain the relationship between a class and an object. Which is general and which is specific?

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Integrated Error-Detection Techniques: Find More Bugs in Java Applications

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Transparent Redirection of Network Sockets 1

Not agree with bug 3, precision actually was. 8,5 not set in the code. Not agree with bug 3, precision actually was

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Checking Access to Protected Members in the Java Virtual Machine

2 SYSTEM DESCRIPTION TECHNIQUES

Part I. Multiple Choice Questions (2 points each):

Paillier Threshold Encryption Toolbox

Code Qualities and Coding Practices

An Overview of Java. overview-1

Fully Abstract Operation Contracts

Rigorous Software Engineering Hoare Logic and Design by Contracts

Lecture 32: The Java Virtual Machine. The Java Virtual Machine

Software Testing. Quality & Testing. Software Testing

Formal Specification and Verification of Avionics Software

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Common Data Structures

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Examples of Design by Contract in Java

Merkle Hash Trees for Distributed Audit Logs

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Experimental Comparison of Concolic and Random Testing for Java Card Applets

Java Memory Model: Content

Using mobile phones to access Web Services in a secure way. Dan Marinescu

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Mathematical Induction

3 Improving the Crab more sophisticated programming

Semantic Analysis: Types and Type Checking

Boogie: A Modular Reusable Verifier for Object-Oriented Programs

Quotes from Object-Oriented Software Construction

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Introduction to Computers and Programming. Testing

Stack Allocation. Run-Time Data Structures. Static Structures

Java Programming Fundamentals

ECE 122. Engineering Problem Solving with Java

Hoare-Style Monitors for Java

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

3. Mathematical Induction

Basic Programming and PC Skills: Basic Programming and PC Skills:

Automated Theorem Proving - summary of lecture 1

Java and Java Virtual Machine security vulnerabilities and their exploitation techniques

Introduction to Java

Malicious Code on Java Card Smartcards: Attacks and Countermeasures

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Product: DQ Order Manager Release Notes

Statements and Control Flow

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Chapter 2 Introduction to Java programming

Test Automation Architectures: Planning for Test Automation

Remote Method Invocation

public static void main(string[] args) { System.out.println("hello, world"); } }

Programming Languages

MUSCLE Cryptographic Card Edge Definition for Java 1 Enabled Smartcards

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Common Beginner C++ Programming Mistakes

JCAT. Java Card TM. An environment for attack and test on. Serge Chaumette, Iban Hatchondo, Damien Sauveron CCCT 03 & ISAS 03

Chapter 1 Java Program Design and Development

TATJA: A Test Automation Tool for Java Applets

1 of 1 24/05/ :23 AM

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Serialization in Java (Binary and XML)

Realizing Enterprise Integration Patterns in WebSphere

Java Card Development Kit. 1 Table of Contents. 2 Introduction. Release Notes for Java Card 3 Platform, Classic Edition, Version 3.0.

HybriDroid: Analysis Framework for Android Hybrid Applications

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Capabilities of a Java Test Execution Framework by Erick Griffin

Transcription:

Fully Verified JAVA CARD API Reference Implementation Wojciech Mostowski woj@cs.ru.nl Radboud University Nijmegen

Background & Motivation Full specification and implementation for the verification of JAVA CARD applets reasoning on the level of interfaces very efficient (in 99.9% of the cases)...... but not always possible/feasible: strong invariants and transaction mechanism full functional verification of contrived applets that prevent fault injections on the source code level Builds on top of earlier work: Sun Reference Implementation, Daniel s OCL work, Nijmegen gang s JML work Impl. + Spec. Verification extra confidence Covers the whole of the latest API used in practice (2.2.1) Exercise for KeY performance & JAVA CARD compliance Study of the specs to identify hot spots

In This Talk Implementation structure Implementation coverage The choice of the specification language JAVA CARD native interface in KeY Examples Experience & Discussion

Implementation Structure JAVA CARD API implementation: JAVA + Native Code Native code: Smart card operating system JAVA CARD simulator interface, e.g. JCWDE JAVA CARD formal model, in KeY set of axiomatic rules full symbolic execution of the API code

JAVA CARD Features Applets APDUs AID registry PIN objects transactions Object sharing across firewall Remote Method Invocation (RMI) Cryptographic keys and ciphers Utilities

What Is Not Covered? Low-level APDU communication not necessary Cipher logic tedious to implement and specify, but possible (possible future work) RMI dispatching on the edge of dynamic class loading, hence not possible to verify, yet not necessary Smart card memory consumption verification possible for transient memory, for persistent memory need extra support from KeY

What Is Covered? Cryptographic routines (almost) everything except the actual ciphering Lightweight AID registry Inter applet object sharing across the firewall Transaction mechanism (obviously) PIN objects, applets, utilities, etc. Applet firewall: firewall checks on two levels: JVM and API the API checks included in the implementation, but transparent during verification JVM checks require an extension of the JAVA CARD execution model Official documentation followed closely

The Specification Language OCL... JML suitable, but currently lack of sufficient control over the generated POs, wait for the JML front-end rewrite to complete JAVA CARD DL more tedious than JML, but: full control possible to take specification shortcuts, improve on verification performance surprisingly almost no applicable technical limits (but few small bugs that needed fixing) an almost exact JML would-be

JAVA CARD Native Interface Dedicated KeY specific class, KeYJCSystem: public static native byte jvmistransient(object theobj); public static native byte[] jvmmaketransientbytearray( short length, byte event); public static native void jvmbegintransaction(); KeYJCSystem: native methods and the card operating system Dedicated logic rules: \< transtype = KeYJCSystem.jvmIsTransient(obj);...\>... {transtype := obj.<transient>}\<... \>... The actual API implementation: public class JCSystem { public static byte istransient(object theobj){ if(theobj == null) return NOT A TRANSIENT OBJECT; return KeYJCSystem.jvmIsTransient(theObj); }}

Example 1 AID.partialEquals, Implementation public final boolean partialequals(byte[] barray, short offset, byte length) throws SecurityException, ArrayIndexOutOfBoundsException { if (barray == null) return false; // resp. documentation if(length > theaid.length) return false; // resp. documentation // Firewall check: if (KeYJCSystem.jvmGetContext(KeYJCSystem.jvmGetOwner(bArray))!= KeYJCSystem.jvmGetContext( KeYJCSystem.jvmGetOwner(KeYJCSystem.previousActiveObject)) && KeYJCSystem.jvmGetPrivs(bArray)!= KeYJCSystem.P GLOBAL ARRAY) throw KeYJCSystem.se; // System owned singleton instance // Actual comparison: return Util.arrayCompare(bArray, offset, theaid, (short)0, length)==0; }

Example 1 AID.partialEquals, Specification \programvariables {AID aidinst; boolean result; byte[] barray; short offset; byte length; } (barray!= null -> length >= 0 & offset >= 0 & offset + length <= barray.length) & {\subst AID aid; aidinst}(\includefile "AID inv.key";) -> \<{ result = aidinst.partialequals(barray, offset, length)@aid; }\> ( (barray = null length > aidinst. theaid.length -> result = FALSE) & (barray!= null & length <= aidinst. theaid.length -> (result = TRUE <-> \forall int i; ( i >= 0 & i < length -> aidinst. theaid[i] = barray[offset+i]))) & {\subst AID aid; aidinst}(\includefile "AID inv.key";)) \modifies {}

Example 2 TransactionException.throwIt Implementation: public static void throwit(short reason) throws TransactionException { instance.setreason(reason); throw instance; } Specification: (\includefile "TransactionException static inv.key";) & {\subst TransactionException exc; TransactionException. instance} (\includefile "TransactionException inv.key";) -> \<{#catchall(transactionexception t) { TransactionException.throwIt(reason)@TransactionException; }}\> ( t = TransactionException. instance & t. reason[0] = reason & (\includefile "TransactionException static inv.key";) & {\subst TransactionException exc; TransactionException. instance} (\includefile "TransactionException inv.key";)) \modifies {TransactionException. instance. reason[0] }

Verification Figures: 60 classes, 205K JAVA code, 395K KeY specifications all methods with code verified Interaction: 10 loops, 2 far from obvious (invariant depends on a logic variable created during the proof) otherwise practically automatic (but see next), no Simplify! verification purely contract based (no method in-lining) the only way to go... Total effort: over 2 man-months

A Loop Removing an object from a table: All non-null services[i] different & s!= null -> \<{ int i = 0; while (i<maxservices) { if(services[i] == s) break; i++ } if(i!= maxservices) services[i] = null; }\> \forall int i; (i>=0 & i < maxservices -> services[i]!= s) What are: variant useful invariant

A Loop Solution Two cases The object that we look for exists in the table or not: \exists int i; (i>=0 & i<maxservices & services[i] = s) 1. Object s is not there Variant: maxservices - i Invariant: i>=0 & i<=maxservices & (i < maxservices -> services[i]!= s) 2. Object s is there Variant: i 0 - i Invariant: i>=0 & i<=i 0

A Loop Solution Two cases The object that we look for exists in the table or not: \exists int i; (i>=0 & i<maxservices & services[i] = s) 1. Object s is not there Variant: maxservices - i Invariant: i>=0 & i<=maxservices & (i < maxservices -> services[i]!= s) 2. Object s is there Variant: i 0 - i Invariant: i>=0 & i<=i 0

The Goods,The Bads, and the Surprises Good KeY can do it Surprise KeY has hidden functionality: it is possible to write JAVA CARD DL contracts for method constructors, despite a reported bug, just write a contract for the <init>() method it is possible to use an at pre array access operator (JML fails here!) one non-rigid function solves the problem in JAVA CARD DL (Quote from Richard: Nice trick ) Bad one bug in the contract application mechanism that makes life difficult, now fixed for DL contracts...

The Goods,The Bads, and the Surprises Bad performance!!! propositional splitting rules (orleft, impleft) make the proofs grow out of reasonable limits simply switching them off can reduce the proof size by a factor of 100 if not more, down sides: occasional manual interaction to split the proof (when it is really needed) is required with the splitting rules switched off the quantifier instantiation heuristics do not work (well) large specifications + large implementations choke KeY large amount of references in the implementation cause lots of update splits, together with large terms KeY dies if-cascades for method binding not always a good idea the last method verified only last Monday Good quantifier instantiation heuristics work well, but...

The Goods,The Bads, and the Surprises Good non-rigid function symbols are the best Surprise an obvious bug in collecting implementing classes of an interface overlooked for a very long time Surprise bad programming can ease up verification! Bad specifications and modifies clauses for object creating methods Bad other known issues and bugs, like treatment of queries (the proposed solution is to use non-rigid function symbols Philipp)

Details Bad Programming public class RSAPrivateCrtKeyImpl { private byte[] p; private byte[] q; private byte[] dp; private byte[] dq; private byte[] pq; } against public class RSAPrivateCrtKeyImpl { private byte[] keymaterial; private short poff, plen; private short qoff, qlen; private short dpoff, dplen; private short dqoff, dqlen; private short pqoff, pqlen; }

Details Bad Programming public class RSAPrivateCrtKeyImpl { private byte[] p; private byte[] q; private byte[] dp; private byte[] dq; private byte[] pq; } against public class RSAPrivateCrtKeyImpl { private byte[] keymaterial; // private short poff, plen; // private short qoff, qlen; // private short dpoff, dplen; // private short dqoff, dqlen; // private short pqoff, pqlen; private short size; }

Details Splitting Specifications Precondition: apdustate = STATE1 apdustate = STATE2 apdustate = STATE3... Possible to use a function symbol with suitable unfolding rule: validapdustate(apdustate) Why not e.g. wrap all invariants inside non-rigid functions: more readable, smaller formulas,... Obstacle: when done manually, the taclet mechanism stands in the way a bit not possible to use concrete attribute and class names in taclets Experimented here a bit this can potentially help, but there are downsides, e.g. no early closing of goals

Details Type if-cascades InterfaceType o=...; o.methodcall(); results in: if(o instanceof Type1) {o.methodcall()@type1; }else if (o instanceof Type2) {o.methodcall()@type2; }else... All types are subtypes of some SuperType which is the only implementor of methodcall The same method body will be expanded, or the same contract used, X times. This is a total waste! In the JAVA CARD API there are 18 classes implementing the Key interface with one common base class. Moreover, what if I want to use a method contract for the interface itself? Then the if-cascade is not necessary at all!

Details Methods Creating Objects public static byte[] maketransientbytearray(... In JML it is sufficient to say: ensures \fresh(\result); In DL however: result!= null & result.<created> = TRUE & result.<transient> =... & \forall int i; (i>=0 & i<result.length -> result[i] = 0) & result = jbyte[]::<get>(jbyte[]::<nexttocreate>@pre) & jbyte[].<nexttocreate> = jbyte[]::<nexttocreate>@pre + 1 \modifies { jbyte[].<nexttocreate>, jbyte[]::<get>(jbyte[].<nexttocreate>).<created>, jbyte[]::<get>(jbyte[].<nexttocreate>).<transient>, jbyte[]::<get>(jbyte[].<nexttocreate>).length, jbyte[]::<get>(jbyte[].<nexttocreate>)[*] }

Details Methods Creating Objects If the method allocates more than one object this gets out of hand! Moreover, the user has to know in which order the objects are created if they are of the same type. Is there an easy way to reflect JML \fresh in JAVA CARD DL?

Details Propositional Splitting orleft and impleft (also andright) can make things really bad: This is with no splitting (?!) Multiply this by 100 (at least) and you get the idea... Would simply postponing the splitting do the trick, so that the program is executed first on a single branch?

Details Propositional Splitting orleft and impleft (also andright) can make things really bad: This is with no splitting (?!) Multiply this by 100 (at least) and you get the idea... Would simply postponing the splitting do the trick, so that the program is executed first on a single branch?

Future Work Recreate this in KeYJML once the new front-end is available Clean up and optimise the code Put it up on the web Implement the firewall formalisation and verify again (any one interested?) Deal with JAVA CARD memory consumption Implement the cipher logic in an abstract way in terms of non-rigid function symbols