250. EPMO Open Source Coordination Office Redaction File Detail Report

Produced by Araxis Merge on 9/25/2018 2:13:23 PM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.

250.1 Files compared

# Location File Last Modified
1 build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\sun\security\mscapi RSACipher.java Mon Jan 22 14:46:58 2018 UTC
2 build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\sun\security\mscapi RSACipher.java Wed Sep 12 17:52:56 2018 UTC

250.2 Comparison summary

Description Between
Files 1 and 2
Text Blocks Lines
Unchanged 5 990
Changed 4 8
Inserted 0 0
Removed 0 0

250.3 Comparison options

Whitespace
Character case Differences in character case are significant
Line endings Differences in line endings (CR and LF characters) are ignored
CR/LF characters Not shown in the comparison detail

250.4 Active regular expressions

No regular expressions were active.

250.5 Comparison detail

  1   /*
  2    * Copyrig ht (c) 200 5, 2012, O racle and/ or its aff iliates. A ll rights  reserved.
  3    * DO NOT  ALTER OR R EMOVE COPY RIGHT NOTI CES OR THI S FILE HEA DER.
  4    *
  5    * This co de is free  software;  you can r edistribut e it and/o r modify i t
  6    * under t he terms o f the GNU  General Pu blic Licen se version  2 only, a s
  7    * publish ed by the  Free Softw are Founda tion.  Ora cle design ates this
  8    * particu lar file a s subject  to the "Cl asspath" e xception a s provided
  9    * by Orac le in the  LICENSE fi le that ac companied  this code.
  10    *
  11    * This co de is dist ributed in  the hope  that it wi ll be usef ul, but WI THOUT
  12    * ANY WAR RANTY; wit hout even  the implie d warranty  of MERCHA NTABILITY  or
  13    * FITNESS  FOR A PAR TICULAR PU RPOSE.  Se e the GNU  General Pu blic Licen se
  14    * version  2 for mor e details  (a copy is  included  in the LIC ENSE file  that
  15    * accompa nied this  code).
  16    *
  17    * You sho uld have r eceived a  copy of th e GNU Gene ral Public  License v ersion
  18    * 2 along  with this  work; if  not, write  to the Fr ee Softwar e Foundati on,
  19    * Inc., 5 1 Franklin  St, Fifth  Floor, Bo ston, MA 0 2110-1301  USA.
  20    *
  21    * Please  contact Or acle, 500  Oracle Par kway, Redw ood Shores , CA 94065  USA
  22    * or visi t www.orac le.com if  you need a dditional  informatio n or have  any
  23    * questio ns.
  24    */
  25  
  26   package su n.security .mscapi;
  27  
  28   import jav a.math.Big Integer;
  29   import jav a.security .*;
  30   import jav a.security .Key;
  31   import jav a.security .interface s.*;
  32   import jav a.security .spec.*;
  33  
  34   import jav ax.crypto. *;
  35   import jav ax.crypto. spec.*;
  36  
  37   import sun .security. rsa.RSAKey Factory;
  38   import sun .security. internal.s pec.TlsRsa PremasterS ecretParam eterSpec;
  39   import sun .security. util.KeyUt il;
  40  
  41   /**
  42    * RSA cip her implem entation u sing the M icrosoft C rypto API.
  43    * Support s RSA en/d ecryption  and signin g/verifyin g using PK CS#1 v1.5  padding.
  44    *
  45    * Objects  should be  instantia ted by cal ling Ciphe r.getInsta nce() usin g the
  46    * followi ng algorit hm name:
  47    *
  48    *  . "RSA /ECB/PKCS1 Padding" ( or "RSA")  for PKCS#1  padding.  The mode ( blocktype)
  49    *    is s elected ba sed on the  en/decryp tion mode  and public /private k ey used.
  50    *
  51    * We only  do one RS A operatio n per doFi nal() call . If the a pplication  passes
  52    * more da ta via cal ls to upda te() or do Final(), w e throw an
  53    * Illegal BlockSizeE xception w hen doFina l() is cal led (see J CE API spe c).
  54    * Bulk en cryption u sing RSA d oes not ma ke sense a nd is not  standardiz ed.
  55    *
  56    * Note: R SA keys sh ould be at  least 512  bits long
  57    *
  58    * @since    1.6
  59    * @author   Andreas  Sterbenz
  60    * @author   Vincent  Ryan
  61    */
  62   public fin al class R SACipher e xtends Cip herSpi {
  63  
  64       // con stant for  an empty b yte array
  65       privat e final st atic byte[ ] B0 = new  byte[0];
  66  
  67       // mod e constant  for publi c key encr yption
  68       privat e final st atic int M ODE_ENCRYP T = 1;
  69       // mod e constant  for priva te key dec ryption
  70       privat e final st atic int M ODE_DECRYP T = 2;
  71       // mod e constant  for priva te key enc ryption (s igning)
  72       privat e final st atic int M ODE_SIGN     = 3;
  73       // mod e constant  for publi c key decr yption (ve rifying)
  74       privat e final st atic int M ODE_VERIFY   = 4;
  75  
  76       // con stant for  PKCS#1 v1. 5 RSA
  77       privat e final st atic Strin g PAD_PKCS 1 = "PKCS1 Padding";
  78       privat e final st atic int P AD_PKCS1_L ENGTH = 11 ;
  79  
  80       // cur rent mode,  one of MO DE_* above . Set when  init() is  called
  81       privat e int mode ;
  82  
  83       // act ive paddin g type, on e of PAD_*  above. Se t by setPa dding()
  84       privat e String p addingType ;
  85       privat e int padd ingLength  = 0;
  86  
  87       // buf fer for th e data
  88       privat e byte[] b uffer;
  89       // off set into t he buffer  (number of  bytes buf fered)
  90       privat e int bufO fs;
  91  
  92       // siz e of the o utput (the  length of  the key).
  93       privat e int outp utSize;
  94  
  95       // the  public ke y, if we w ere initia lized usin g a public  key
  96       privat e sun.secu rity.mscap i.Key publ icKey;
  97  
  98       // the  private k ey, if we  were initi alized usi ng a priva te key
  99       privat e sun.secu rity.mscap i.Key priv ateKey;
  100  
  101         // cipher  parameter  for TLS RS A premaste PW     
  102       privat e Algorith mParameter Spec spec  = null;
  103  
  104       // the  source of  randomnes s
  105       privat e SecureRa ndom rando m;
  106  
  107       public  RSACipher () {
  108           pa ddingType  = PAD_PKCS 1;
  109       }
  110  
  111       // mod es do not  make sense  for RSA,  but allow  ECB
  112       // see  JCE spec
  113       protec ted void e ngineSetMo de(String  mode) thro ws NoSuchA lgorithmEx ception {
  114           if  (mode.equ alsIgnoreC ase("ECB")  == false)  {
  115                throw ne w NoSuchAl gorithmExc eption("Un supported  mode " + m ode);
  116           }
  117       }
  118  
  119       // set  the paddi ng type
  120       // see  JCE spec
  121       protec ted void e ngineSetPa dding(Stri ng padding Name)
  122                throws N oSuchPaddi ngExceptio n {
  123           if  (paddingN ame.equals IgnoreCase (PAD_PKCS1 )) {
  124                paddingT ype = PAD_ PKCS1;
  125           }  else {
  126                throw ne w NoSuchPa ddingExcep tion
  127                    ("Pa dding " +  paddingNam e + " not  supported" );
  128           }
  129       }
  130  
  131       // ret urn 0 as b lock size,  we are no t a block  cipher
  132       // see  JCE spec
  133       protec ted int en gineGetBlo ckSize() {
  134           re turn 0;
  135       }
  136  
  137       // ret urn the ou tput size
  138       // see  JCE spec
  139       protec ted int en gineGetOut putSize(in t inputLen ) {
  140           re turn outpu tSize;
  141       }
  142  
  143       // no  iv, return  null
  144       // see  JCE spec
  145       protec ted byte[]  engineGet IV() {
  146           re turn null;
  147       }
  148  
  149       // no  parameters , return n ull
  150       // see  JCE spec
  151       protec ted Algori thmParamet ers engine GetParamet ers() {
  152           re turn null;
  153       }
  154  
  155       // see  JCE spec
  156       protec ted void e ngineInit( int opmode , Key key,  SecureRan dom random )
  157                throws I nvalidKeyE xception {
  158           in it(opmode,  key);
  159       }
  160  
  161       // see  JCE spec
  162       protec ted void e ngineInit( int opmode , Key key,
  163                Algorith mParameter Spec param s, SecureR andom rand om)
  164                throws I nvalidKeyE xception,  InvalidAlg orithmPara meterExcep tion {
  165  
  166           if  (params ! = null) {
  167                if (!(pa rams insta nceof TlsR saPremaste rSecretPar ameterSpec )) {
  168                    thro w new Inva lidAlgorit hmParamete rException (
  169                             "Param eters not  supported" );
  170                }
  171                spec = p arams;
  172                  this.rando m = random    // for TLS  RSA prema ster  PW     
  173           }
  174           in it(opmode,  key);
  175       }
  176  
  177       // see  JCE spec
  178       protec ted void e ngineInit( int opmode , Key key,
  179                Algorith mParameter s params,  SecureRand om random)
  180                throws I nvalidKeyE xception,  InvalidAlg orithmPara meterExcep tion {
  181  
  182           if  (params ! = null) {
  183                throw ne w InvalidA lgorithmPa rameterExc eption
  184                    ("Pa rameters n ot support ed");
  185           }
  186           in it(opmode,  key);
  187       }
  188  
  189       // ini tialize th is cipher
  190       privat e void ini t(int opmo de, Key ke y) throws  InvalidKey Exception  {
  191  
  192           bo olean encr ypt;
  193  
  194           sw itch (opmo de) {
  195           ca se Cipher. ENCRYPT_MO DE:
  196           ca se Cipher. WRAP_MODE:
  197                paddingL ength = PA D_PKCS1_LE NGTH;
  198                encrypt  = true;
  199                break;
  200           ca se Cipher. DECRYPT_MO DE:
  201           ca se Cipher. UNWRAP_MOD E:
  202                paddingL ength = 0;  // reset
  203                encrypt  = false;
  204                break;
  205           de fault:
  206                throw ne w InvalidK eyExceptio n("Unknown  mode: " +  opmode);
  207           }
  208  
  209           if  (!(key in stanceof s un.securit y.mscapi.K ey)) {
  210                if (key  instanceof  java.secu rity.inter faces.RSAP ublicKey)  {
  211                    java .security. interfaces .RSAPublic Key rsaKey  =
  212                         (java.secu rity.inter faces.RSAP ublicKey)  key;
  213  
  214                    // C onvert key  to MSCAPI  format
  215  
  216                    BigI nteger mod ulus = rsa Key.getMod ulus();
  217                    BigI nteger exp onent =  r saKey.getP ublicExpon ent();
  218  
  219                    // C heck again st the loc al and glo bal values  to make s ure
  220                    // t he sizes a re ok.  Ro und up to  the neares t byte.
  221                    RSAK eyFactory. checkKeyLe ngths(((mo dulus.bitL ength() +  7) & ~7),
  222                         exponent,  -1, RSAKey PairGenera tor.KEY_SI ZE_MAX);
  223  
  224                    byte [] modulus Bytes = mo dulus.toBy teArray();
  225                    byte [] exponen tBytes = e xponent.to ByteArray( );
  226  
  227                    // A djust key  length due  to sign b it
  228                    int  keyBitLeng th = (modu lusBytes[0 ] == 0)
  229                         ? (modulus Bytes.leng th - 1) *  8
  230                         : modulusB ytes.lengt h * 8;
  231  
  232                    byte [] keyBlob  = RSASign ature.gene ratePublic KeyBlob(
  233                         keyBitLeng th, modulu sBytes, ex ponentByte s);
  234  
  235                    try  {
  236                         key = RSAS ignature.i mportPubli cKey(keyBl ob, keyBit Length);
  237  
  238                    } ca tch (KeySt oreExcepti on e) {
  239                         throw new  InvalidKey Exception( e);
  240                    }
  241  
  242                } else {
  243                    thro w new Inva lidKeyExce ption("Uns upported k ey type: "  + key);
  244                }
  245           }
  246  
  247           if  (key inst anceof Pub licKey) {
  248                mode = e ncrypt ? M ODE_ENCRYP T : MODE_V ERIFY;
  249                publicKe y = (sun.s ecurity.ms capi.Key)k ey;
  250                privateK ey = null;
  251                outputSi ze = publi cKey.lengt h() / 8;
  252           }  else if (k ey instanc eof Privat eKey) {
  253                mode = e ncrypt ? M ODE_SIGN :  MODE_DECR YPT;
  254                privateK ey = (sun. security.m scapi.Key) key;
  255                publicKe y = null;
  256                outputSi ze = priva teKey.leng th() / 8;
  257           }  else {
  258                throw ne w InvalidK eyExceptio n("Unknown  key type:  " + key);
  259           }
  260  
  261           bu fOfs = 0;
  262           bu ffer = new  byte[outp utSize];
  263       }
  264  
  265       // int ernal upda te method
  266       privat e void upd ate(byte[]  in, int i nOfs, int  inLen) {
  267           if  ((inLen = = 0) || (i n == null) ) {
  268                return;
  269           }
  270           if  (bufOfs +  inLen > ( buffer.len gth - padd ingLength) ) {
  271                bufOfs =  buffer.le ngth + 1;
  272                return;
  273           }
  274           Sy stem.array copy(in, i nOfs, buff er, bufOfs , inLen);
  275           bu fOfs += in Len;
  276       }
  277  
  278       // int ernal doFi nal() meth od. Here w e perform  the actual  RSA opera tion
  279       privat e byte[] d oFinal() t hrows BadP addingExce ption,
  280                IllegalB lockSizeEx ception {
  281           if  (bufOfs >  buffer.le ngth) {
  282                throw ne w IllegalB lockSizeEx ception("D ata must n ot be long er "
  283                    + "t han " + (b uffer.leng th - paddi ngLength)   + " bytes ");
  284           }
  285  
  286           tr y {
  287                byte[] d ata = buff er;
  288                switch ( mode) {
  289                case MOD E_SIGN:
  290                    retu rn encrypt Decrypt(da ta, bufOfs ,
  291                         privateKey .getHCrypt Key(), tru e);
  292  
  293                case MOD E_VERIFY:
  294                    retu rn encrypt Decrypt(da ta, bufOfs ,
  295                         publicKey. getHCryptK ey(), fals e);
  296  
  297                case MOD E_ENCRYPT:
  298                    retu rn encrypt Decrypt(da ta, bufOfs ,
  299                         publicKey. getHCryptK ey(), true );
  300  
  301                case MOD E_DECRYPT:
  302                    retu rn encrypt Decrypt(da ta, bufOfs ,
  303                         privateKey .getHCrypt Key(), fal se);
  304  
  305                default:
  306                    thro w new Asse rtionError ("Internal  error");
  307                }
  308  
  309           }  catch (Key Exception  e) {
  310                throw ne w Provider Exception( e);
  311  
  312           }  finally {
  313                bufOfs =  0;
  314           }
  315       }
  316  
  317       // see  JCE spec
  318       protec ted byte[]  engineUpd ate(byte[]  in, int i nOfs, int  inLen) {
  319           up date(in, i nOfs, inLe n);
  320           re turn B0;
  321       }
  322  
  323       // see  JCE spec
  324       protec ted int en gineUpdate (byte[] in , int inOf s, int inL en, byte[]  out,
  325                int outO fs) {
  326           up date(in, i nOfs, inLe n);
  327           re turn 0;
  328       }
  329  
  330       // see  JCE spec
  331       protec ted byte[]  engineDoF inal(byte[ ] in, int  inOfs, int  inLen)
  332                throws B adPaddingE xception,  IllegalBlo ckSizeExce ption {
  333           up date(in, i nOfs, inLe n);
  334           re turn doFin al();
  335       }
  336  
  337       // see  JCE spec
  338       protec ted int en gineDoFina l(byte[] i n, int inO fs, int in Len, byte[ ] out,
  339                int outO fs) throws  ShortBuff erExceptio n, BadPadd ingExcepti on,
  340                IllegalB lockSizeEx ception {
  341           if  (outputSi ze > out.l ength - ou tOfs) {
  342                throw ne w ShortBuf ferExcepti on
  343                    ("Ne ed " + out putSize +  " bytes fo r output") ;
  344           }
  345           up date(in, i nOfs, inLe n);
  346           by te[] resul t = doFina l();
  347           in t n = resu lt.length;
  348           Sy stem.array copy(resul t, 0, out,  outOfs, n );
  349           re turn n;
  350       }
  351  
  352       // see  JCE spec
  353       protec ted byte[]  engineWra p(Key key)  throws In validKeyEx ception,
  354                IllegalB lockSizeEx ception {
  355           by te[] encod ed = key.g etEncoded( ); // TODO  - unextra ctable key
  356           if  ((encoded  == null)  || (encode d.length = = 0)) {
  357                throw ne w InvalidK eyExceptio n("Could n ot obtain  encoded ke y");
  358           }
  359           if  (encoded. length > b uffer.leng th) {
  360                throw ne w InvalidK eyExceptio n("Key is  too long f or wrappin g");
  361           }
  362           up date(encod ed, 0, enc oded.lengt h);
  363           tr y {
  364                return d oFinal();
  365           }  catch (Bad PaddingExc eption e)  {
  366                // shoul d not occu r
  367                throw ne w InvalidK eyExceptio n("Wrappin g failed",  e);
  368           }
  369       }
  370  
  371       // see  JCE spec
  372       protec ted java.s ecurity.Ke y engineUn wrap(byte[ ] wrappedK ey,
  373                String a lgorithm,
  374                int type ) throws I nvalidKeyE xception,  NoSuchAlgo rithmExcep tion {
  375  
  376           if  (wrappedK ey.length  > buffer.l ength) {
  377                throw ne w InvalidK eyExceptio n("Key is  too long f or unwrapp ing");
  378           }
  379  
  380           bo olean isTl sRsaPremas terSecret  =
  381                    algo rithm.equa ls("TlsRsa PremasterS ecret");
  382           Ex ception fa ilover = n ull;
  383           by te[] encod ed = null;
  384  
  385           up date(wrapp edKey, 0,  wrappedKey .length);
  386           tr y {
  387                encoded  = doFinal( );
  388           }  catch (Bad PaddingExc eption e)  {
  389                if (isTl sRsaPremas terSecret)  {
  390                    fail over = e;
  391                } else {
  392                    thro w new Inva lidKeyExce ption("Unw rapping fa iled", e);
  393                }
  394           }  catch (Ill egalBlockS izeExcepti on e) {
  395                // shoul d not occu r, handled  with leng th check a bove
  396                throw ne w InvalidK eyExceptio n("Unwrapp ing failed ", e);
  397           }
  398  
  399           if  (isTlsRsa PremasterS ecret) {
  400                if (!(sp ec instanc eof TlsRsa PremasterS ecretParam eterSpec))  {
  401                    thro w new Ille galStateEx ception(
  402                             "No Tl sRsaPremas terSecretP arameterSp ec specifi ed");
  403                }
  404  
  405                  // polish  the TLS pr emaster  PW     
  406                encoded  = KeyUtil. checkTlsPr eMasterSec retKey(
  407                    ((Tl sRsaPremas terSecretP arameterSp ec)spec).g etClientVe rsion(),
  408                    ((Tl sRsaPremas terSecretP arameterSp ec)spec).g etServerVe rsion(),
  409                    rand om, encode d, (failov er != null ));
  410           }
  411  
  412           re turn const ructKey(en coded, alg orithm, ty pe);
  413       }
  414  
  415       // see  JCE spec
  416       protec ted int en gineGetKey Size(Key k ey) throws  InvalidKe yException  {
  417  
  418           if  (key inst anceof sun .security. mscapi.Key ) {
  419                return ( (sun.secur ity.mscapi .Key) key) .length();
  420  
  421           }  else if (k ey instanc eof RSAKey ) {
  422                return ( (RSAKey) k ey).getMod ulus().bit Length();
  423  
  424           }  else {
  425                throw ne w InvalidK eyExceptio n("Unsuppo rted key t ype: " + k ey);
  426           }
  427       }
  428  
  429       // Con struct an  X.509 enco ded public  key.
  430       privat e static P ublicKey c onstructPu blicKey(by te[] encod edKey,
  431           St ring encod edKeyAlgor ithm)
  432                throws I nvalidKeyE xception,  NoSuchAlgo rithmExcep tion {
  433  
  434           tr y {
  435                KeyFacto ry keyFact ory = KeyF actory.get Instance(e ncodedKeyA lgorithm);
  436                X509Enco dedKeySpec  keySpec =  new X509E ncodedKeyS pec(encode dKey);
  437  
  438                return k eyFactory. generatePu blic(keySp ec);
  439  
  440           }  catch (NoS uchAlgorit hmExceptio n nsae) {
  441                throw ne w NoSuchAl gorithmExc eption("No  installed  provider  " +
  442                    "sup ports the  " + encode dKeyAlgori thm + " al gorithm",  nsae);
  443  
  444           }  catch (Inv alidKeySpe cException  ike) {
  445                throw ne w InvalidK eyExceptio n("Cannot  construct  public key ", ike);
  446           }
  447       }
  448  
  449       // Con struct a P KCS #8 enc oded priva te key.
  450       privat e static P rivateKey  constructP rivateKey( byte[] enc odedKey,
  451           St ring encod edKeyAlgor ithm)
  452                throws I nvalidKeyE xception,  NoSuchAlgo rithmExcep tion {
  453  
  454           tr y {
  455                KeyFacto ry keyFact ory = KeyF actory.get Instance(e ncodedKeyA lgorithm);
  456                PKCS8Enc odedKeySpe c keySpec  = new PKCS 8EncodedKe ySpec(enco dedKey);
  457  
  458                return k eyFactory. generatePr ivate(keyS pec);
  459  
  460           }  catch (NoS uchAlgorit hmExceptio n nsae) {
  461                throw ne w NoSuchAl gorithmExc eption("No  installed  provider  " +
  462                    "sup ports the  " + encode dKeyAlgori thm + " al gorithm",  nsae);
  463  
  464           }  catch (Inv alidKeySpe cException  ike) {
  465                throw ne w InvalidK eyExceptio n("Cannot  construct  private ke y", ike);
  466           }
  467       }
  468  
  469         // Constru ct an enco ded  PW        key.
  470       privat e static S ecretKey c onstructSe cretKey(by te[] encod edKey,
  471           St ring encod edKeyAlgor ithm) {
  472  
  473           re turn new S ecretKeySp ec(encoded Key, encod edKeyAlgor ithm);
  474       }
  475  
  476       privat e static K ey constru ctKey(byte [] encoded Key,
  477                String e ncodedKeyA lgorithm,
  478                int keyT ype) throw s InvalidK eyExceptio n, NoSuchA lgorithmEx ception {
  479  
  480           sw itch (keyT ype) {
  481                case Cip her.PUBLIC _KEY:
  482                    retu rn constru ctPublicKe y(encodedK ey, encode dKeyAlgori thm);
  483                case Cip her.PRIVAT E_KEY:
  484                    retu rn constru ctPrivateK ey(encoded Key, encod edKeyAlgor ithm);
  485                case Cip her.SECRET _KEY:
  486                    retu rn constru ctSecretKe y(encodedK ey, encode dKeyAlgori thm);
  487                default:
  488                    thro w new Inva lidKeyExce ption("Unk nown key t ype " + ke yType);
  489           }
  490       }
  491  
  492       /*
  493        * Enc rypt/decry pt a data  buffer usi ng Microso ft Crypto  API with H CRYPTKEY.
  494        * It  expects an d returns  ciphertext  data in b ig-endian  form.
  495        */
  496       privat e native s tatic byte [] encrypt Decrypt(by te[] data,  int dataS ize,
  497           lo ng hCryptK ey, boolea n doEncryp t) throws  KeyExcepti on;
  498  
  499   }