summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Security/Cryptography/RSAPKCS1KeyExchangeFormatter.cs
blob: 622d4e9700bee79d767c31eb683c1bd171024411 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.Security.Cryptography {
    using System.Globalization;
    using System.Diagnostics.Contracts;

    [System.Runtime.InteropServices.ComVisible(true)]
    public class RSAPKCS1KeyExchangeFormatter : AsymmetricKeyExchangeFormatter {
        RandomNumberGenerator RngValue;
        RSA _rsaKey;

        //
        // public constructors
        //

        public RSAPKCS1KeyExchangeFormatter() {}

        public RSAPKCS1KeyExchangeFormatter(AsymmetricAlgorithm key) {
            if (key == null) 
                throw new ArgumentNullException("key");
            Contract.EndContractBlock();
            _rsaKey = (RSA) key;
        }

        //
        // public properties
        //

        public override String Parameters {
            get { return "<enc:KeyEncryptionMethod enc:Algorithm=\"http://www.microsoft.com/xml/security/algorithm/PKCS1-v1.5-KeyEx\" xmlns:enc=\"http://www.microsoft.com/xml/security/encryption/v1.0\" />"; }
        }

        public RandomNumberGenerator Rng {
            get { return RngValue; }
            set { RngValue = value; }
        }
        
        //
        // public methods
        //

        public override void SetKey(AsymmetricAlgorithm key) {
            if (key == null) 
                throw new ArgumentNullException("key");
            Contract.EndContractBlock();
            _rsaKey = (RSA) key;
        }

        public override byte[] CreateKeyExchange(byte[] rgbData) {
            if (_rsaKey == null)
                throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));

            byte[] rgbKeyEx;
            if (_rsaKey is RSACryptoServiceProvider) {
                rgbKeyEx = ((RSACryptoServiceProvider) _rsaKey).Encrypt(rgbData, false);
            }
            else {
                int cb = _rsaKey.KeySize/8;
                if ((rgbData.Length + 11) > cb)
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_Padding_EncDataTooBig", cb-11));
                byte[]  rgbInput = new byte[cb];

                //
                //  We want to pad to the following format:
                //      00 || 02 || PS || 00 || D
                //
                //      PS - pseudorandom non zero bytes
                //      D - data
                //

                if (RngValue == null) {
                    RngValue = RandomNumberGenerator.Create();
                }
                
                Rng.GetNonZeroBytes(rgbInput);
                rgbInput[0] = 0;
                rgbInput[1] = 2;
                rgbInput[cb-rgbData.Length-1] = 0;
                Buffer.InternalBlockCopy(rgbData, 0, rgbInput, cb-rgbData.Length, rgbData.Length);

                //
                //  Now encrypt the value and return it. (apply public key)
                //

                rgbKeyEx = _rsaKey.EncryptValue(rgbInput);
            }
            return rgbKeyEx;
        }

        public override byte[] CreateKeyExchange(byte[] rgbData, Type symAlgType) {
            return CreateKeyExchange(rgbData);
        }
    }
}