jdk/src/share/classes/com/sun/crypto/provider/TlsRsaPremasterSecretGenerator.java
changeset 23733 b9b80421cfa7
parent 22309 1990211a42e5
equal deleted inserted replaced
23732:44fe768edfd2 23733:b9b80421cfa7
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    54         throw new InvalidParameterException(MSG);
    54         throw new InvalidParameterException(MSG);
    55     }
    55     }
    56 
    56 
    57     protected void engineInit(AlgorithmParameterSpec params,
    57     protected void engineInit(AlgorithmParameterSpec params,
    58             SecureRandom random) throws InvalidAlgorithmParameterException {
    58             SecureRandom random) throws InvalidAlgorithmParameterException {
    59         if (params instanceof TlsRsaPremasterSecretParameterSpec == false) {
    59         if (!(params instanceof TlsRsaPremasterSecretParameterSpec)) {
    60             throw new InvalidAlgorithmParameterException(MSG);
    60             throw new InvalidAlgorithmParameterException(MSG);
    61         }
    61         }
    62         this.spec = (TlsRsaPremasterSecretParameterSpec)params;
    62         this.spec = (TlsRsaPremasterSecretParameterSpec)params;
    63         this.random = random;
    63         this.random = random;
    64     }
    64     }
    65 
    65 
    66     protected void engineInit(int keysize, SecureRandom random) {
    66     protected void engineInit(int keysize, SecureRandom random) {
    67         throw new InvalidParameterException(MSG);
    67         throw new InvalidParameterException(MSG);
    68     }
    68     }
    69 
    69 
       
    70     // Only can be used in client side to generate TLS RSA premaster secret.
    70     protected SecretKey engineGenerateKey() {
    71     protected SecretKey engineGenerateKey() {
    71         if (spec == null) {
    72         if (spec == null) {
    72             throw new IllegalStateException(
    73             throw new IllegalStateException(
    73                 "TlsRsaPremasterSecretGenerator must be initialized");
    74                 "TlsRsaPremasterSecretGenerator must be initialized");
    74         }
    75         }
    75         byte[] b = spec.getEncodedSecret();
    76 
    76         if (b == null) {
    77         if (random == null) {
    77             if (random == null) {
    78             random = new SecureRandom();
    78                 random = new SecureRandom();
       
    79             }
       
    80             b = new byte[48];
       
    81             random.nextBytes(b);
       
    82             b[0] = (byte)spec.getMajorVersion();
       
    83             b[1] = (byte)spec.getMinorVersion();
       
    84         }
    79         }
       
    80         byte[] b = new byte[48];
       
    81         random.nextBytes(b);
       
    82         b[0] = (byte)spec.getMajorVersion();
       
    83         b[1] = (byte)spec.getMinorVersion();
    85 
    84 
    86         return new SecretKeySpec(b, "TlsRsaPremasterSecret");
    85         return new SecretKeySpec(b, "TlsRsaPremasterSecret");
    87     }
    86     }
    88 
    87 
    89 }
    88 }