Anjali K K | 4a694f6 | 2018-07-12 19:09:19 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 org.onlab.packet; |
| 18 | |
| 19 | import java.nio.ByteBuffer; |
| 20 | |
| 21 | /** |
| 22 | * Class representing MKPDU MACSec SAK Use Parameter Set (GCM-AES 128). |
| 23 | * IEEE 802.1X Clause 11; Figure 11-11 |
| 24 | */ |
| 25 | public class EAPOLMkpduDistributedSAKParameterSet extends BasePacket implements EAPOLMkpduParameterSet { |
| 26 | |
| 27 | // Various fields. |
| 28 | private byte distributedAN; |
| 29 | private byte confidentialityOffset; |
| 30 | private int keyNumber; |
| 31 | private byte[] sak; |
| 32 | |
| 33 | /* Body Length is fixed in Distribute SAK Parameter Set. |
| 34 | * Still variable kept for de-serialization purpose. |
| 35 | */ |
| 36 | private short bodyLength; |
| 37 | |
| 38 | // Various Constants. |
| 39 | public static final short TOTAL_DSAKPS_BODY_LENGTH = 32; |
| 40 | public static final short SAK_FIELD_LENGTH = 24; |
| 41 | public static final byte DSAKPS_GENERAL_MASK = 0x03; |
| 42 | public static final byte DISTRIBUTED_AN_OFFSET = (byte) 0x06; |
| 43 | public static final byte CONFIDENTIALITY_OFFSET = (byte) 0x04; |
| 44 | |
| 45 | // Key wrapping support. |
| 46 | @FunctionalInterface |
| 47 | public interface KeyWrapper { |
| 48 | byte[] wrap(byte[] message); |
| 49 | } |
| 50 | |
| 51 | KeyWrapper sakWrapper; |
| 52 | |
| 53 | @Override |
| 54 | public byte getParameterSetType() { |
| 55 | return PARAMETERSET_TYPE_DISTRIBUTED_SAK; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public short getTotalLength() { |
| 60 | return TOTAL_DSAKPS_BODY_LENGTH; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public short getBodyLength() { |
| 65 | return bodyLength; |
| 66 | } |
| 67 | |
| 68 | public void setBodyLength(short bodyLength) { |
| 69 | this.bodyLength = bodyLength; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public byte[] serialize() { |
| 74 | short length = getTotalLength(); |
| 75 | |
| 76 | // Serialize Distribute SAK Parameter Set. IEEE 802.1x, Figure 11.10 |
| 77 | ByteBuffer data = ByteBuffer.wrap(new byte[length]); |
| 78 | |
| 79 | /* |
| 80 | *Populate fields |
| 81 | * Octet 1 |
| 82 | */ |
| 83 | data.put(EAPOLMkpduParameterSet.PARAMETERSET_TYPE_DISTRIBUTED_SAK); |
| 84 | |
| 85 | // Octet 2 |
| 86 | byte octet = 0x00; |
| 87 | octet = (byte) ((DSAKPS_GENERAL_MASK & distributedAN) << DISTRIBUTED_AN_OFFSET); |
| 88 | octet |= (byte) ((DSAKPS_GENERAL_MASK & confidentialityOffset) << CONFIDENTIALITY_OFFSET); |
| 89 | data.put(octet); |
| 90 | |
| 91 | // Octet 3 & 4 |
| 92 | length -= EAPOLMkpduParameterSet.BODY_LENGTH_OCTET_OFFSET; |
| 93 | octet |= (byte) (length >> BODY_LENGTH_MSB_SHIFT & BODY_LENGTH_MSB_MASK); |
| 94 | data.put(octet); |
| 95 | data.put((byte) length); |
| 96 | |
| 97 | // Octet 5 |
| 98 | data.putInt(keyNumber); |
| 99 | |
| 100 | // AES Key Wrap of SAK |
| 101 | data.put(sakWrapper.wrap(sak)); |
| 102 | |
| 103 | return data.array(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Deserializer function for Distributed SAK Parameter Set. |
| 108 | * |
| 109 | * @return deserializer function |
| 110 | */ |
| 111 | public static Deserializer<EAPOLMkpduDistributedSAKParameterSet> deserializer() { |
| 112 | return (data, offset, length) -> { |
| 113 | |
| 114 | // Needed components. |
| 115 | final ByteBuffer bb = ByteBuffer.wrap(data, offset, length); |
| 116 | EAPOLMkpduDistributedSAKParameterSet dps = new EAPOLMkpduDistributedSAKParameterSet(); |
| 117 | |
| 118 | /* |
| 119 | * Extract fields. |
| 120 | * Octet 2 |
| 121 | */ |
| 122 | byte[] mbField = new byte[1]; |
| 123 | mbField[0] = bb.get(); |
| 124 | dps.setDistributedAN((byte) ((mbField[0] >> DISTRIBUTED_AN_OFFSET) & DSAKPS_GENERAL_MASK)); |
| 125 | dps.setConfidentialityOffset((byte) ((mbField[0] >> CONFIDENTIALITY_OFFSET) & DSAKPS_GENERAL_MASK)); |
| 126 | |
| 127 | // Octet 3 & 4 |
| 128 | mbField[0] = bb.get(); |
| 129 | short bodyLength = (short) (((short) (mbField[0] & EAPOLMkpduParameterSet.BODY_LENGTH_MSB_MASK)) |
| 130 | << EAPOLMkpduParameterSet.BODY_LENGTH_MSB_SHIFT); |
| 131 | bodyLength |= (short) (bb.get()); |
| 132 | dps.setBodyLength(bodyLength); |
| 133 | |
| 134 | // Octet 5 |
| 135 | dps.setKeyNumber(bb.getInt()); |
| 136 | |
| 137 | // SAK |
| 138 | mbField = new byte[EAPOLMkpduDistributedSAKParameterSet.SAK_FIELD_LENGTH]; |
| 139 | bb.get(mbField, 0, EAPOLMkpduDistributedSAKParameterSet.SAK_FIELD_LENGTH); |
| 140 | dps.setSAK(mbField); |
| 141 | |
| 142 | return dps; |
| 143 | }; |
| 144 | } |
| 145 | |
| 146 | // Distributed AN |
| 147 | public void setDistributedAN(byte distributedAN) { |
| 148 | this.distributedAN = distributedAN; |
| 149 | } |
| 150 | |
| 151 | // Confidentiality Offset |
| 152 | public void setConfidentialityOffset(byte confidentialityOffset) { |
| 153 | this.confidentialityOffset = confidentialityOffset; |
| 154 | } |
| 155 | |
| 156 | // Key Number |
| 157 | public void setKeyNumber(int keyNumber) { |
| 158 | this.keyNumber = keyNumber; |
| 159 | } |
| 160 | |
| 161 | // SAK |
| 162 | public void setSAK(byte[] sak) { |
| 163 | this.sak = sak; |
| 164 | } |
| 165 | |
| 166 | // Key Wrapper |
| 167 | public void setKeyWrapper(KeyWrapper sakWrapper) { |
| 168 | this.sakWrapper = sakWrapper; |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |