001package com.nimbusds.jose.jwk; 002 003 004import java.text.ParseException; 005import java.util.LinkedHashSet; 006import java.util.List; 007import java.util.Set; 008 009 010/** 011 * Enumeration of key operations. Represents the {@code key_ops} parameter in a 012 * JSON Web Key (JWK). 013 * 014 * <p>JWK operation values: 015 * 016 * <ul> 017 * <li>{@link #SIGN sign} 018 * <li>{@link #VERIFY verify} 019 * <li>{@link #ENCRYPT encrypt} 020 * <li>{@link #DECRYPT decrypt} 021 * <li>{@link #WRAP_KEY wrapKey} 022 * <li>{@link #UNWRAP_KEY unwrapKey} 023 * <li>{@link #DERIVE_KEY deriveKey} 024 * <li>{@link #DERIVE_BITS deriveBits} 025 * </ul> 026 * 027 * @author Vladimir Dzhuvinov 028 * @version 2014-04-02 029 */ 030public enum KeyOperation { 031 032 033 /** 034 * Compute signature or MAC. 035 */ 036 SIGN("sign"), 037 038 039 /** 040 * Verify signature or MAC. 041 */ 042 VERIFY("verify"), 043 044 045 /** 046 * Encrypt content. 047 */ 048 ENCRYPT("encrypt"), 049 050 051 /** 052 * Decrypt content and validate decryption, if applicable. 053 */ 054 DECRYPT("decrypt"), 055 056 057 /** 058 * Encrypt key. 059 */ 060 WRAP_KEY("wrapKey"), 061 062 063 /** 064 * Decrypt key and validate decryption, if applicable. 065 */ 066 UNWRAP_KEY("unwrapKey"), 067 068 069 /** 070 * Derive key. 071 */ 072 DERIVE_KEY("deriveKey"), 073 074 075 /** 076 * Derive bits not to be used as a key. 077 */ 078 DERIVE_BITS("deriveBits"); 079 080 081 /** 082 * The key operation identifier. 083 */ 084 private final String identifier; 085 086 087 /** 088 * Creates a new key operation with the specified identifier. 089 * 090 * @param identifier The key operation identifier. Must not be 091 * {@code null}. 092 */ 093 KeyOperation(final String identifier) { 094 095 if (identifier == null) 096 throw new IllegalArgumentException("The key operation identifier must not be null"); 097 098 this.identifier = identifier; 099 } 100 101 102 /** 103 * Returns the identifier of this public key use. 104 * 105 * @return The identifier. 106 */ 107 public String identifier() { 108 109 return identifier; 110 } 111 112 113 /** 114 * @see #identifier() 115 */ 116 @Override 117 public String toString() { 118 119 return identifier(); 120 } 121 122 123 /** 124 * Parses a key operation set from the specified JWK {@code key_ops} 125 * parameter value. 126 * 127 * @param sl The string list to parse. May be {@code null}. 128 * 129 * @return The key operation set, {@code null} if none. 130 * 131 * @throws ParseException If the string list couldn't be parsed to a 132 * valid key operation list. 133 */ 134 public static Set<KeyOperation> parse(final List<String> sl) 135 throws ParseException { 136 137 if (sl == null) { 138 return null; 139 } 140 141 Set<KeyOperation> keyOps = new LinkedHashSet<>(); 142 143 for (String s: sl) { 144 145 if (s == null) { 146 // skip 147 continue; 148 } 149 150 KeyOperation parsedOp = null; 151 152 for (KeyOperation op: KeyOperation.values()) { 153 154 if (s.equals(op.identifier())) { 155 parsedOp = op; 156 break; 157 } 158 } 159 160 if (parsedOp != null) { 161 keyOps.add(parsedOp); 162 } 163 else { 164 throw new ParseException("Invalid JWK operation: " + s, 0); 165 } 166 } 167 168 return keyOps; 169 } 170}