View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.security;
19  
20  import static org.junit.Assert.*;
21  
22  import java.security.Key;
23  import java.security.KeyException;
24  import java.security.SecureRandom;
25  
26  import javax.crypto.spec.SecretKeySpec;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.SmallTests;
31  import org.apache.hadoop.hbase.io.crypto.KeyProviderForTesting;
32  import org.apache.hadoop.hbase.io.crypto.aes.AES;
33  import org.apache.hadoop.hbase.security.EncryptionUtil;
34  import org.apache.hadoop.hbase.util.Bytes;
35  
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  @Category(SmallTests.class)
40  public class TestEncryptionUtil {
41  
42    @Test
43    public void testKeyWrapping() throws Exception {
44      // set up the key provider for testing to resolve a key for our test subject
45      Configuration conf = new Configuration(); // we don't need HBaseConfiguration for this
46      conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
47  
48      // generate a test key
49      byte[] keyBytes = new byte[AES.KEY_LENGTH];
50      new SecureRandom().nextBytes(keyBytes);
51      Key key = new SecretKeySpec(keyBytes, "AES");
52  
53      // wrap the test key
54      byte[] wrappedKeyBytes = EncryptionUtil.wrapKey(conf, "hbase", key);
55      assertNotNull(wrappedKeyBytes);
56  
57      // unwrap
58      Key unwrappedKey = EncryptionUtil.unwrapKey(conf, "hbase", wrappedKeyBytes);
59      assertNotNull(unwrappedKey);
60      // only secretkeyspec supported for now
61      assertTrue(unwrappedKey instanceof SecretKeySpec);
62      // did we get back what we wrapped?
63      assertTrue("Unwrapped key bytes do not match original",
64        Bytes.equals(keyBytes, unwrappedKey.getEncoded()));
65  
66      // unwrap with an incorrect key
67      try {
68        EncryptionUtil.unwrapKey(conf, "other", wrappedKeyBytes);
69        fail("Unwrap with incorrect key did not throw KeyException");
70      } catch (KeyException e) {
71        // expected
72      }
73    }
74  
75  }