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      String algorithm =
51          conf.get(HConstants.CRYPTO_KEY_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
52      Key key = new SecretKeySpec(keyBytes, algorithm);
53  
54      // wrap the test key
55      byte[] wrappedKeyBytes = EncryptionUtil.wrapKey(conf, "hbase", key);
56      assertNotNull(wrappedKeyBytes);
57  
58      // unwrap
59      Key unwrappedKey = EncryptionUtil.unwrapKey(conf, "hbase", wrappedKeyBytes);
60      assertNotNull(unwrappedKey);
61      // only secretkeyspec supported for now
62      assertTrue(unwrappedKey instanceof SecretKeySpec);
63      // did we get back what we wrapped?
64      assertTrue("Unwrapped key bytes do not match original",
65        Bytes.equals(keyBytes, unwrappedKey.getEncoded()));
66  
67      // unwrap with an incorrect key
68      try {
69        EncryptionUtil.unwrapKey(conf, "other", wrappedKeyBytes);
70        fail("Unwrap with incorrect key did not throw KeyException");
71      } catch (KeyException e) {
72        // expected
73      }
74    }
75  
76    @Test
77    public void testWALKeyWrapping() throws Exception {
78      // set up the key provider for testing to resolve a key for our test subject
79      Configuration conf = new Configuration(); // we don't need HBaseConfiguration for this
80      conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
81  
82      // generate a test key
83      byte[] keyBytes = new byte[AES.KEY_LENGTH];
84      new SecureRandom().nextBytes(keyBytes);
85      String algorithm = conf.get(HConstants.CRYPTO_WAL_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
86      Key key = new SecretKeySpec(keyBytes, algorithm);
87  
88      // wrap the test key
89      byte[] wrappedKeyBytes = EncryptionUtil.wrapKey(conf, "hbase", key);
90      assertNotNull(wrappedKeyBytes);
91  
92      // unwrap
93      Key unwrappedKey = EncryptionUtil.unwrapWALKey(conf, "hbase", wrappedKeyBytes);
94      assertNotNull(unwrappedKey);
95      // only secretkeyspec supported for now
96      assertTrue(unwrappedKey instanceof SecretKeySpec);
97      // did we get back what we wrapped?
98      assertTrue("Unwrapped key bytes do not match original",
99        Bytes.equals(keyBytes, unwrappedKey.getEncoded()));
100   }
101 
102   @Test(expected = KeyException.class)
103   public void testWALKeyWrappingWithIncorrectKey() throws Exception {
104     // set up the key provider for testing to resolve a key for our test subject
105     Configuration conf = new Configuration(); // we don't need HBaseConfiguration for this
106     conf.set(HConstants.CRYPTO_KEYPROVIDER_CONF_KEY, KeyProviderForTesting.class.getName());
107 
108     // generate a test key
109     byte[] keyBytes = new byte[AES.KEY_LENGTH];
110     new SecureRandom().nextBytes(keyBytes);
111     String algorithm = conf.get(HConstants.CRYPTO_WAL_ALGORITHM_CONF_KEY, HConstants.CIPHER_AES);
112     Key key = new SecretKeySpec(keyBytes, algorithm);
113 
114     // wrap the test key
115     byte[] wrappedKeyBytes = EncryptionUtil.wrapKey(conf, "hbase", key);
116     assertNotNull(wrappedKeyBytes);
117 
118     // unwrap with an incorrect key
119     EncryptionUtil.unwrapWALKey(conf, "other", wrappedKeyBytes);
120   }
121 }