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.testclassification.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.util.Bytes;
34  
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  @Category(SmallTests.class)
39  public class TestEncryptionUtil {
40  
41    @Test
42    public void testKeyWrapping() throws Exception {
43      // set up the key provider for testing to resolve a key for our test subject
44      Configuration conf = new Configuration(); // we don't need HBaseConfiguration for this
45      conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
46  
47      // generate a test key
48      byte[] keyBytes = new byte[AES.KEY_LENGTH];
49      new SecureRandom().nextBytes(keyBytes);
50      Key key = new SecretKeySpec(keyBytes, "AES");
51  
52      // wrap the test key
53      byte[] wrappedKeyBytes = EncryptionUtil.wrapKey(conf, "hbase", key);
54      assertNotNull(wrappedKeyBytes);
55  
56      // unwrap
57      Key unwrappedKey = EncryptionUtil.unwrapKey(conf, "hbase", wrappedKeyBytes);
58      assertNotNull(unwrappedKey);
59      // only secretkeyspec supported for now
60      assertTrue(unwrappedKey instanceof SecretKeySpec);
61      // did we get back what we wrapped?
62      assertTrue("Unwrapped key bytes do not match original",
63        Bytes.equals(keyBytes, unwrappedKey.getEncoded()));
64  
65      // unwrap with an incorrect key
66      try {
67        EncryptionUtil.unwrapKey(conf, "other", wrappedKeyBytes);
68        fail("Unwrap with incorrect key did not throw KeyException");
69      } catch (KeyException e) {
70        // expected
71      }
72    }
73  
74  }