1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.car.encryptionrunner; 18 19 /** 20 * An encryption runner that doesn't actually do encryption. Useful for debugging out of band 21 * association. Do not use in production environments. 22 */ 23 public class OobFakeEncryptionRunner extends FakeEncryptionRunner { 24 25 @Override continueHandshake(byte[] response)26 public HandshakeMessage continueHandshake(byte[] response) throws HandshakeException { 27 if (getState() != HandshakeMessage.HandshakeState.IN_PROGRESS) { 28 throw new HandshakeException("not waiting for response but got one"); 29 } 30 31 @HandshakeMessage.HandshakeState int newState = 32 HandshakeMessage.HandshakeState.OOB_VERIFICATION_NEEDED; 33 switch (getMode()) { 34 case Mode.SERVER: 35 if (!CLIENT_RESPONSE.equals(new String(response))) { 36 throw new HandshakeException("unexpected response: " + new String(response)); 37 } 38 setState(newState); 39 return HandshakeMessage.newBuilder() 40 .setOobVerificationCode(VERIFICATION_CODE.getBytes()) 41 .setHandshakeState(newState) 42 .build(); 43 case Mode.CLIENT: 44 if (!INIT_RESPONSE.equals(new String(response))) { 45 throw new HandshakeException("unexpected response: " + new String(response)); 46 } 47 setState(newState); 48 return HandshakeMessage.newBuilder() 49 .setHandshakeState(newState) 50 .setNextMessage(CLIENT_RESPONSE.getBytes()) 51 .setOobVerificationCode(VERIFICATION_CODE.getBytes()) 52 .build(); 53 default: 54 throw new IllegalStateException("unexpected role: " + getMode()); 55 } 56 } 57 58 @Override verifyPin()59 public HandshakeMessage verifyPin() throws HandshakeException { 60 @HandshakeMessage.HandshakeState int state = getState(); 61 if (state != HandshakeMessage.HandshakeState.OOB_VERIFICATION_NEEDED) { 62 throw new IllegalStateException("asking to verify pin, state = " + state); 63 } 64 state = HandshakeMessage.HandshakeState.FINISHED; 65 return HandshakeMessage.newBuilder().setKey(new FakeKey()).setHandshakeState( 66 state).build(); 67 } 68 } 69