summary refs log tree commit diff
path: root/crypto/src/tls/TlsRsaUtilities.cs
blob: 0652795286d48c0152354eb7fa36fec196f88b63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.IO;

using Org.BouncyCastle.Tls.Crypto;

namespace Org.BouncyCastle.Tls
{
    /// <summary>RSA utility methods.</summary>
    public abstract class TlsRsaUtilities
    {
        /// <summary>Generate a pre_master_secret and send it encrypted to the server.</summary>
        /// <exception cref="IOException"/>
        public static TlsSecret GenerateEncryptedPreMasterSecret(TlsContext context, TlsCertificate certificate,
            Stream output)
        {
            TlsSecret preMasterSecret = context.Crypto.GenerateRsaPreMasterSecret(context.RsaPreMasterSecretVersion);

            TlsEncryptor encryptor = certificate.CreateEncryptor(TlsCertificateRole.RsaEncryption);

            byte[] encryptedPreMasterSecret = preMasterSecret.Encrypt(encryptor);
            TlsUtilities.WriteEncryptedPms(context, encryptedPreMasterSecret, output);

            return preMasterSecret;
        }
    }
}