|
| 1 | +// |
| 2 | +// BouncyCastleDkimPrivateKey.cs |
| 3 | +// |
| 4 | +// Author: Jeffrey Stedfast <[email protected]> |
| 5 | +// |
| 6 | +// Copyright (c) 2013-2025 .NET Foundation and Contributors |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in |
| 16 | +// all copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | +// THE SOFTWARE. |
| 25 | +// |
| 26 | + |
| 27 | +using System; |
| 28 | +using System.IO; |
| 29 | + |
| 30 | +using Org.BouncyCastle.Crypto; |
| 31 | +using Org.BouncyCastle.OpenSsl; |
| 32 | + |
| 33 | +namespace MimeKit.Cryptography { |
| 34 | + /// <summary> |
| 35 | + /// A DKIM private key implemented using BouncyCastle. |
| 36 | + /// </summary> |
| 37 | + /// <remarks> |
| 38 | + /// A DKIM private key implemented using BouncyCastle. |
| 39 | + /// </remarks> |
| 40 | + public class BouncyCastleDkimPrivateKey : BouncyCastleDkimKey, IDkimPrivateKey |
| 41 | + { |
| 42 | + /// <summary> |
| 43 | + /// Initializes a new instance of the <see cref="BouncyCastleDkimPrivateKey"/> class. |
| 44 | + /// </summary> |
| 45 | + /// <remarks> |
| 46 | + /// Creates a new <see cref="BouncyCastleDkimPrivateKey"/>. |
| 47 | + /// </remarks> |
| 48 | + /// <param name="key">The private key.</param> |
| 49 | + /// <exception cref="System.ArgumentNullException"> |
| 50 | + /// <paramref name="key"/> is <c>null</c>. |
| 51 | + /// </exception> |
| 52 | + /// <exception cref="System.ArgumentException"> |
| 53 | + /// <paramref name="key"/> is not a private key. |
| 54 | + /// </exception> |
| 55 | + public BouncyCastleDkimPrivateKey (AsymmetricKeyParameter key) |
| 56 | + { |
| 57 | + if (key is null) |
| 58 | + throw new ArgumentNullException (nameof (key)); |
| 59 | + |
| 60 | + if (!key.IsPrivate) |
| 61 | + throw new ArgumentException ("The key must be a private key.", nameof (key)); |
| 62 | + |
| 63 | + Key = key; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Create a DKIM signature context suitable for signing. |
| 68 | + /// </summary> |
| 69 | + /// <remarks> |
| 70 | + /// Creates a DKIM signature context suitable for signing. |
| 71 | + /// </remarks> |
| 72 | + /// <param name="algorithm">The DKIM signature algorithm.</param> |
| 73 | + /// <returns>The DKIM signature context.</returns> |
| 74 | + /// <exception cref="System.NotSupportedException"> |
| 75 | + /// The specified <paramref name="algorithm"/> is not supported. |
| 76 | + /// </exception> |
| 77 | + public IDkimSignatureContext CreateSigningContext (DkimSignatureAlgorithm algorithm) |
| 78 | + { |
| 79 | + return CreateSignatureContext (algorithm, true); |
| 80 | + } |
| 81 | + |
| 82 | + static AsymmetricKeyParameter LoadPrivateKey (Stream stream) |
| 83 | + { |
| 84 | + AsymmetricKeyParameter key = null; |
| 85 | + |
| 86 | + using (var reader = new StreamReader (stream)) { |
| 87 | + var pem = new PemReader (reader); |
| 88 | + |
| 89 | + var keyObject = pem.ReadObject (); |
| 90 | + |
| 91 | + if (keyObject is AsymmetricCipherKeyPair pair) { |
| 92 | + key = pair.Private; |
| 93 | + } else if (keyObject is AsymmetricKeyParameter param) { |
| 94 | + key = param; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (key == null || !key.IsPrivate) |
| 99 | + throw new FormatException ("Private key not found."); |
| 100 | + |
| 101 | + return key; |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Load a private key from the specified stream. |
| 106 | + /// </summary> |
| 107 | + /// <remarks> |
| 108 | + /// Loads a private key from the specified stream. |
| 109 | + /// </remarks> |
| 110 | + /// <param name="stream">A stream containing the private DKIM key data.</param> |
| 111 | + /// <returns>A <see cref="BouncyCastleDkimPrivateKey"/>.</returns> |
| 112 | + /// <exception cref="System.ArgumentNullException"> |
| 113 | + /// <paramref name="stream"/> is <c>null</c>. |
| 114 | + /// </exception> |
| 115 | + /// <exception cref="System.FormatException"> |
| 116 | + /// The stream did not contain a private key in PEM format. |
| 117 | + /// </exception> |
| 118 | + /// <exception cref="System.IO.IOException"> |
| 119 | + /// An I/O error occurred. |
| 120 | + /// </exception> |
| 121 | + public static BouncyCastleDkimPrivateKey Load (Stream stream) |
| 122 | + { |
| 123 | + if (stream is null) |
| 124 | + throw new ArgumentNullException (nameof (stream)); |
| 125 | + |
| 126 | + var key = LoadPrivateKey (stream); |
| 127 | + |
| 128 | + return new BouncyCastleDkimPrivateKey (key); |
| 129 | + } |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Load a private key from the specified file. |
| 133 | + /// </summary> |
| 134 | + /// <remarks> |
| 135 | + /// Loads a private key from the specified file. |
| 136 | + /// </remarks> |
| 137 | + /// <param name="fileName">A file containing the private DKIM key data.</param> |
| 138 | + /// <returns>A <see cref="BouncyCastleDkimPrivateKey"/>.</returns> |
| 139 | + /// <exception cref="System.ArgumentNullException"> |
| 140 | + /// <paramref name="fileName"/> is <c>null</c>. |
| 141 | + /// </exception> |
| 142 | + /// <exception cref="System.FormatException"> |
| 143 | + /// The stream did not contain a private key in PEM format. |
| 144 | + /// </exception> |
| 145 | + /// <exception cref="System.IO.IOException"> |
| 146 | + /// An I/O error occurred. |
| 147 | + /// </exception> |
| 148 | + public static BouncyCastleDkimPrivateKey Load (string fileName) |
| 149 | + { |
| 150 | + if (fileName is null) |
| 151 | + throw new ArgumentNullException (nameof (fileName)); |
| 152 | + |
| 153 | + using (var stream = File.OpenRead (fileName)) |
| 154 | + return Load (stream); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments