|
| 1 | +// |
| 2 | +// Copyright (c) 2018 The nanoFramework project contributors |
| 3 | +// Portions Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +// See LICENSE file in the project root for full license information. |
| 5 | +// |
| 6 | + |
| 7 | +using System.Runtime.CompilerServices; |
| 8 | +using System.Security.Cryptography.X509Certificates; |
| 9 | +using System.Text; |
| 10 | + |
| 11 | +namespace System.Net.Security |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Provides an interface to the device certificate store to manage <see cref="X509Certificate"/>. |
| 15 | + /// </summary> |
| 16 | + public static class CertificateManager |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Adds a Certificate Authority Root bundle <see cref="X509Certificate"/> to the store. |
| 20 | + /// If there is already a CA Root bundle it will be replaced with this one. |
| 21 | + /// </summary> |
| 22 | + /// <param name="ca">The Certificate Authority certificate bundle to be added store.</param> |
| 23 | + /// <returns> |
| 24 | + /// True if the certificate bundle was correctly added to the device certificate store. |
| 25 | + /// </returns> |
| 26 | + /// <remarks> |
| 27 | + /// This method is exclusive of nanoFramework. There is no equivalent in .NET framework. |
| 28 | + /// </remarks> |
| 29 | + public static bool AddCaCertificateBundle(X509Certificate[] ca) |
| 30 | + { |
| 31 | + // build a string concatenating all the certificates |
| 32 | + StringBuilder bundle = new StringBuilder(); |
| 33 | + |
| 34 | + foreach(X509Certificate cert in ca) |
| 35 | + { |
| 36 | + byte[] certRaw = cert.GetRawCertData(); |
| 37 | + |
| 38 | + // remove the terminator from each string |
| 39 | + bundle.Append(Encoding.UTF8.GetString(certRaw, 0, certRaw.Length - 1)); |
| 40 | + } |
| 41 | + |
| 42 | + // add terminator |
| 43 | + bundle.Append("\0"); |
| 44 | + |
| 45 | + return AddCaCertificateBundle(bundle.ToString()); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Adds a Certificate Authority Root bundle <see cref="X509Certificate"/> to the store. |
| 50 | + /// If there is already a CA Root bundle it will be replaced with this one. |
| 51 | + /// </summary> |
| 52 | + /// <param name="ca">The Certificate Authority certificate bundle to be added store.</param> |
| 53 | + /// <returns> |
| 54 | + /// True if the certificate bundle was correctly added to the device certificate store. |
| 55 | + /// </returns> |
| 56 | + /// <remarks> |
| 57 | + /// This method is exclusive of nanoFramework. There is no equivalent in .NET framework. |
| 58 | + /// </remarks> |
| 59 | + public static bool AddCaCertificateBundle(string ca) |
| 60 | + { |
| 61 | + return AddCaCertificateBundle(Encoding.UTF8.GetBytes(ca)); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Adds a Certificate Authority Root bundle <see cref="X509Certificate"/> to the store. |
| 66 | + /// If there is already a CA Root bundle it will be replaced with this one. |
| 67 | + /// </summary> |
| 68 | + /// <param name="ca">The Certificate Authority certificate bundle to be added store.</param> |
| 69 | + /// <returns> |
| 70 | + /// True if the certificate bundle was correctly added to the device certificate store. |
| 71 | + /// </returns> |
| 72 | + /// <remarks> |
| 73 | + /// This method is exclusive of nanoFramework. There is no equivalent in .NET framework. |
| 74 | + /// </remarks> |
| 75 | + [MethodImpl(MethodImplOptions.InternalCall)] |
| 76 | + public static extern bool AddCaCertificateBundle(byte[] ca); |
| 77 | + } |
| 78 | +} |
0 commit comments