/// <summary> /// Imports an RFC 7468 PEM-encoded key, replacing the keys for this object. /// </summary> /// <param name="input">The PEM text of the key to import.</param> /// <exception cref="ArgumentException"> /// <para> /// <paramref name="input"/> does not contain a PEM-encoded key with a recognized label. /// </para> /// <para> /// -or- /// </para> /// <para> /// <paramref name="input"/> contains multiple PEM-encoded keys with a recognized label. /// </para> /// <para> /// -or- /// </para> /// <para> /// <paramref name="input"/> contains an encrypted PEM-encoded key. /// </para> /// </exception> /// <remarks> /// <para> /// Unsupported or malformed PEM-encoded objects will be ignored. If multiple supported PEM labels /// are found, an exception is raised to prevent importing a key when /// the key is ambiguous. /// </para> /// <para> /// This method supports the following PEM labels: /// <list type="bullet"> /// <item><description>PUBLIC KEY</description></item> /// <item><description>PRIVATE KEY</description></item> /// <item><description>RSA PRIVATE KEY</description></item> /// <item><description>RSA PUBLIC KEY</description></item> /// </list> /// </para> /// </remarks> public override void ImportFromPem(ReadOnlySpan <char> input) { PemKeyHelpers.ImportPem(input, label => { if (label.SequenceEqual(PemLabels.RsaPrivateKey)) { return(ImportRSAPrivateKey); } else if (label.SequenceEqual(PemLabels.Pkcs8PrivateKey)) { return(ImportPkcs8PrivateKey); } else if (label.SequenceEqual(PemLabels.RsaPublicKey)) { return(ImportRSAPublicKey); } else if (label.SequenceEqual(PemLabels.SpkiPublicKey)) { return(ImportSubjectPublicKeyInfo); } else { return(null); } }); }
/// <summary> /// Imports an RFC 7468 textually encoded key, replacing the keys for this object. /// </summary> /// <param name="input">The text of the PEM key to import.</param> /// <exception cref="ArgumentException"> /// <para> /// <paramref name="input"/> does not contain a PEM-encoded key with a recognized label. /// </para> /// <para> /// -or- /// </para> /// <para> /// <paramref name="input"/> contains multiple PEM-encoded keys with a recognized label. /// </para> /// <para> /// -or- /// </para> /// <para> /// <paramref name="input"/> contains an encrypted PEM-encoded key. /// </para> /// </exception> /// <exception cref="NotImplementedException"> /// A derived type has not provided an implementation for <see cref="ImportPkcs8PrivateKey" /> /// or <see cref="ImportSubjectPublicKeyInfo" />. /// </exception> /// <remarks> /// <para> /// Unsupported or malformed PEM-encoded objects will be ignored. If multiple supported PEM labels /// are found, an exception is raised to prevent importing a key when /// the key is ambiguous. /// </para> /// <para> /// This method supports the following PEM labels: /// <list type="bullet"> /// <item><description>PUBLIC KEY</description></item> /// <item><description>PRIVATE KEY</description></item> /// </list> /// </para> /// <para> /// Types that override this method may support additional PEM labels. /// </para> /// </remarks> public virtual void ImportFromPem(ReadOnlySpan <char> input) { PemKeyHelpers.ImportPem(input, label => label switch { PemLabels.Pkcs8PrivateKey => ImportPkcs8PrivateKey, PemLabels.SpkiPublicKey => ImportSubjectPublicKeyInfo, _ => null, });