1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.security.util;
  27 
  28 import java.security.Key;
  29 import java.security.PrivilegedAction;
  30 import java.security.AccessController;
  31 import java.security.interfaces.ECKey;
  32 import java.security.interfaces.RSAKey;
  33 import java.security.interfaces.DSAKey;
  34 import javax.crypto.SecretKey;
  35 import javax.crypto.interfaces.DHKey;
  36 
  37 /**
  38  * A utility class to get key length
  39  */
  40 public final class KeyLength {
  41 
  42     /**
  43      * Returns the key size of the given key object in bits.
  44      *
  45      * @param key the key object, cannot be null
  46      * @return the key size of the given key object in bits, or -1 if the
  47      *       key size is not accessible
  48      */
  49     final public static int getKeySize(Key key) {
  50         int size = -1;
  51 
  52         if (key instanceof Length) {
  53             try {
  54                 Length ruler = (Length)key;
  55                 size = ruler.length();
  56             } catch (UnsupportedOperationException usoe) {
  57                 // ignore the exception
  58             }
  59 
  60             if (size >= 0) {
  61                 return size;
  62             }
  63         }
  64 
  65         // try to parse the length from key specification
  66         if (key instanceof SecretKey) {
  67             SecretKey sk = (SecretKey)key;
  68             String format = sk.getFormat();
  69             if ("RAW".equals(format) && sk.getEncoded() != null) {
  70                 size = (sk.getEncoded().length * 8);
  71             }   // Otherwise, it may be a unextractable key of PKCS#11, or
  72                 // a key we are not able to handle.
  73         } else if (key instanceof RSAKey) {
  74             RSAKey pubk = (RSAKey)key;
  75             size = pubk.getModulus().bitLength();
  76         } else if (key instanceof ECKey) {
  77             ECKey pubk = (ECKey)key;
  78             size = pubk.getParams().getOrder().bitLength();
  79         } else if (key instanceof DSAKey) {
  80             DSAKey pubk = (DSAKey)key;
  81             size = pubk.getParams().getP().bitLength();
  82         } else if (key instanceof DHKey) {
  83             DHKey pubk = (DHKey)key;
  84             size = pubk.getParams().getP().bitLength();
  85         }   // Otherwise, it may be a unextractable key of PKCS#11, or
  86             // a key we are not able to handle.
  87 
  88         return size;
  89     }
  90 }
  91