1 /*
2 * The contents of this file are subject to the terms of the Common Development and
3 * Distribution License (the License). You may not use this file except in compliance with the
4 * License.
5 *
6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7 * specific language governing permission and limitations under the License.
8 *
9 * When distributing Covered Software, include this CDDL Header Notice in each file and include
10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11 * Header, with the fields enclosed by brackets [] replaced by your own identifying
12 * information: "Portions Copyrighted [year] [name of copyright owner]".
13 *
14 * Copyright 2011-2016 ForgeRock AS.
15 */
16
17 package org.forgerock.json.crypto;
18
19 import static org.forgerock.json.JsonValue.field;
20 import static org.forgerock.json.JsonValue.object;
21 import static org.forgerock.util.crypto.CryptoConstants.CRYPTO;
22 import static org.forgerock.util.crypto.CryptoConstants.CRYPTO_TYPE;
23 import static org.forgerock.util.crypto.CryptoConstants.CRYPTO_VALUE;
24
25 import java.util.Map;
26
27 import org.forgerock.json.JsonValue;
28 import org.forgerock.json.JsonValueException;
29 import org.forgerock.util.crypto.CryptoConstants;
30
31 /**
32 * Represents a JSON {@code $crypto} object.
33 *
34 * For example:
35 * <pre>
36 * "$crypto":{
37 * "value":{
38 * "data":"wfoQJXXXXTa551pKTMjZ/Q==",
39 * "cipher":"AES/CBC/PKCS5Padding",
40 * "iv":"OXHdtVBURv6fAuRa88CDnA==",
41 * "key":"openidm-sym-default"
42 * },
43 * "type":"x-simple-encryption"
44 * }
45 * </pre>
46 *
47 * @see CryptoConstants for constants used to build the json.
48 */
49 public class JsonCrypto {
50
51 /** The type of JSON cryptographic representation. */
52 private String type;
53
54 /** The JSON cryptographic value. */
55 private JsonValue value;
56
57 /**
58 * Constructs an empty JSON cryptographic object.
59 */
60 public JsonCrypto() {
61 // empty
62 }
63
64 /**
65 * Constructs a new JSON cryptographic object, initializing from a JSON value.
66 *
67 * @param value a JSON value containing a {@code $crypto} JSON object value.
68 * @throws JsonValueException if the specified value is malformed.
69 */
70 public JsonCrypto(JsonValue value) throws JsonValueException {
71 fromJsonValue(value);
72 }
73
74 /**
75 * Constructs a new JSON cryptographic object, initializing with the specified type
76 * and cryptographic value.
77 *
78 * @param type the type of JSON cryptographic representation.
79 * @param value the JSON cryptographic value.
80 */
81 public JsonCrypto(String type, JsonValue value) {
82 setType(type);
83 setValue(value);
84 }
85
86 /**
87 * Returns {@code true} if the specified JSON value contains a valid {@code $crypto}
88 * JSON object structure.
89 *
90 * @param value The JSON to check.
91 * @return The result.
92 */
93 public static boolean isJsonCrypto(JsonValue value) {
94 boolean result = false;
95 if (value.isDefined(CRYPTO)) { // avoid transformer endless loops
96 JsonValue crypto = value.get(CRYPTO);
97 result = (crypto.get(CRYPTO_TYPE).isString() && crypto.isDefined(CRYPTO_VALUE));
98 }
99 return result;
100 }
101
102 /**
103 * Returns the type of JSON cryptographic representation.
104 * @return The type.
105 */
106 public String getType() {
107 return type;
108 }
109
110 /**
111 * Sets the type of JSON cryptographic representation.
112 *
113 * @param type the type of JSON cryptographic representation.
114 */
115 public void setType(String type) {
116 this.type = type;
117 }
118
119 /**
120 * Returns the JSON cryptographic value.
121 * @return The value.
122 */
123 public JsonValue getValue() {
124 return value;
125 }
126
127 /**
128 * Sets the JSON cryptographic value.
129 *
130 * @param value the JSON cryptographic value.
131 */
132 public void setValue(JsonValue value) {
133 this.value = value;
134 }
135
136 /**
137 * Initializes this object from the specified {@code $crypto} JSON object value.
138 *
139 * @param value a JSON value containing a {@code $crypto} JSON object value.
140 * @throws JsonValueException if the specified value is malformed.
141 */
142 public void fromJsonValue(JsonValue value) throws JsonValueException {
143 JsonValue crypto = value.get(CRYPTO).required();
144 this.type = crypto.get(CRYPTO_TYPE).required().asString();
145 this.value = crypto.get(CRYPTO_VALUE).required();
146 }
147
148 /**
149 * Returns this object as a {@code $crypto} JSON object value.
150 * @return The value.
151 */
152 public JsonValue toJsonValue() {
153 Map<String, Object> object = object(field(CRYPTO, object(
154 field(CRYPTO_TYPE, type),
155 field(CRYPTO_VALUE, value == null ? null : value.getObject())))
156 );
157 return new JsonValue(object, value == null ? null : value.getPointer());
158 }
159 }