1 // Copyright 2022 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 //////////////////////////////////////////////////////////////////////////////// 16 17 package com.google.crypto.tink.internal; 18 19 import com.google.crypto.tink.KeyStatus; 20 import com.google.crypto.tink.PrimitiveSet; 21 import com.google.crypto.tink.monitoring.MonitoringClient; 22 import com.google.crypto.tink.monitoring.MonitoringKeysetInfo; 23 import com.google.crypto.tink.proto.KeyStatusType; 24 import java.security.GeneralSecurityException; 25 import java.util.List; 26 import javax.annotation.Nullable; 27 28 /** Some util functions needed to add monitoring to the Primitives. */ 29 public final class MonitoringUtil { 30 31 private static class DoNothingLogger implements MonitoringClient.Logger { 32 @Override log(int keyId, long numBytesAsInput)33 public void log(int keyId, long numBytesAsInput) {} 34 35 @Override logFailure()36 public void logFailure() {} 37 } 38 39 public static final MonitoringClient.Logger DO_NOTHING_LOGGER = new DoNothingLogger(); 40 parseStatus(KeyStatusType in)41 private static KeyStatus parseStatus(KeyStatusType in) { 42 switch (in) { 43 case ENABLED: 44 return KeyStatus.ENABLED; 45 case DISABLED: 46 return KeyStatus.DISABLED; 47 case DESTROYED: 48 return KeyStatus.DESTROYED; 49 default: 50 throw new IllegalStateException("Unknown key status"); 51 } 52 } 53 54 private static final String TYPE_URL_PREFIX = "type.googleapis.com/google.crypto."; 55 parseKeyTypeUrl(String keyTypeUrl)56 private static String parseKeyTypeUrl(String keyTypeUrl) { 57 if (!keyTypeUrl.startsWith(TYPE_URL_PREFIX)) { 58 return keyTypeUrl; 59 } 60 return keyTypeUrl.substring(TYPE_URL_PREFIX.length()); 61 } 62 getMonitoringKeysetInfo(PrimitiveSet<P> primitiveSet)63 public static <P> MonitoringKeysetInfo getMonitoringKeysetInfo(PrimitiveSet<P> primitiveSet) { 64 MonitoringKeysetInfo.Builder builder = MonitoringKeysetInfo.newBuilder(); 65 builder.setAnnotations(primitiveSet.getAnnotations()); 66 for (List<PrimitiveSet.Entry<P>> entries : primitiveSet.getAll()) { 67 for (PrimitiveSet.Entry<P> entry : entries) { 68 builder.addEntry( 69 parseStatus(entry.getStatus()), 70 entry.getKeyId(), 71 parseKeyTypeUrl(entry.getKeyType()), 72 entry.getOutputPrefixType().name()); 73 } 74 } 75 @Nullable PrimitiveSet.Entry<P> primary = primitiveSet.getPrimary(); 76 if (primary != null) { 77 builder.setPrimaryKeyId(primitiveSet.getPrimary().getKeyId()); 78 } 79 try { 80 return builder.build(); 81 } catch (GeneralSecurityException e) { 82 // This shouldn't happen, since for PrimitiveSets, the primary's key id is always in the 83 // entries list. 84 throw new IllegalStateException(e); 85 } 86 } 87 MonitoringUtil()88 private MonitoringUtil() {} 89 } 90