Ejemplo n.º 1
0
        /// <summary>
        ///   Create a PEM-encoded PKCS#10 CertificationRequest representing the current state
        ///   of this object using the provided signature generator.
        /// </summary>
        /// <param name="signatureGenerator">
        ///   A <see cref="X509SignatureGenerator"/> with which to sign the request.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///   <paramref name="signatureGenerator" /> is <see langword="null" />.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        ///   <para>
        ///     <see cref="OtherRequestAttributes"/> contains a <see langword="null" /> value.
        ///   </para>
        ///   <para>- or -</para>
        ///   <para>
        ///     <see cref="OtherRequestAttributes"/> contains an entry with a <see langword="null" />
        ///     <see cref="AsnEncodedData.Oid" /> value.
        ///   </para>
        ///   <para>- or -</para>
        ///   <para>
        ///     <see cref="OtherRequestAttributes"/> contains an entry representing the PKCS#9
        ///     Extension Request Attribute (1.2.840.113549.1.9.14).
        ///   </para>
        ///   <para>- or -</para>
        ///   <para>
        ///     <see cref="CertificateExtensions"/> contains a <see langword="null" /> value.
        ///   </para>
        ///   <para>- or -</para>
        ///   <para>
        ///     <see cref="CertificateExtensions"/> contains an entry with a <see langword="null" />
        ///     <see cref="AsnEncodedData.Oid" /> value.
        ///   </para>
        ///   <para>- or -</para>
        ///   <para>
        ///     This object was created with a constructor which did not accept a signing key.
        ///   </para>
        /// </exception>
        /// <exception cref="CryptographicException">
        ///   A cryptographic error occurs while creating the signing request.
        /// </exception>
        /// <seealso cref="CreateSigningRequest(X509SignatureGenerator)"/>
        public string CreateSigningRequestPem(X509SignatureGenerator signatureGenerator)
        {
            ArgumentNullException.ThrowIfNull(signatureGenerator);

            byte[] der = CreateSigningRequest(signatureGenerator);
            return(PemEncoding.WriteString(PemLabels.Pkcs10CertificateRequest, der));
        }
Ejemplo n.º 2
0
        public string ExportPkcs7Pem()
        {
            byte[]? pkcs7 = Export(X509ContentType.Pkcs7);

            if (pkcs7 is null)
            {
                throw new CryptographicException(SR.Cryptography_X509_ExportFailed);
            }

            return(PemEncoding.WriteString(PemLabels.Pkcs7Certificate, pkcs7));
        }
        internal CertificateData(byte[] rawData)
        {
#if DEBUG
            try
            {
#endif
            RawData     = rawData;
            certificate = CertificateAsn.Decode(rawData, AsnEncodingRules.DER);
            certificate.TbsCertificate.ValidateVersion();
            Issuer      = new X500DistinguishedName(certificate.TbsCertificate.Issuer.Span);
            Subject     = new X500DistinguishedName(certificate.TbsCertificate.Subject.Span);
            IssuerName  = Issuer.Name;
            SubjectName = Subject.Name;

            AsnWriter writer = new AsnWriter(AsnEncodingRules.DER);
            certificate.TbsCertificate.SubjectPublicKeyInfo.Encode(writer);
            SubjectPublicKeyInfo = writer.Encode();

            Extensions = new List <X509Extension>((certificate.TbsCertificate.Extensions?.Length).GetValueOrDefault());
            if (certificate.TbsCertificate.Extensions != null)
            {
                foreach (X509ExtensionAsn rawExtension in certificate.TbsCertificate.Extensions)
                {
                    X509Extension extension = new X509Extension(
                        rawExtension.ExtnId,
                        rawExtension.ExtnValue.Span,
                        rawExtension.Critical);

                    Extensions.Add(extension);
                }
            }
#if DEBUG
        }

        catch (Exception e)
        {
            string pem = PemEncoding.WriteString(PemLabels.X509Certificate, rawData);
            throw new CryptographicException($"Error in reading certificate:{Environment.NewLine}{pem}", e);
        }
#endif
        }
Ejemplo n.º 4
0
 /// <summary>
 ///   Create a PEM-encoded PKCS#10 CertificationRequest representing the current state
 ///   of this object using the provided signature generator.
 /// </summary>
 /// <exception cref="InvalidOperationException">
 ///   <para>
 ///     <see cref="OtherRequestAttributes"/> contains a <see langword="null" /> value.
 ///   </para>
 ///   <para>- or -</para>
 ///   <para>
 ///     <see cref="OtherRequestAttributes"/> contains an entry with a <see langword="null" />
 ///     <see cref="AsnEncodedData.Oid" /> value.
 ///   </para>
 ///   <para>- or -</para>
 ///   <para>
 ///     <see cref="OtherRequestAttributes"/> contains an entry representing the PKCS#9
 ///     Extension Request Attribute (1.2.840.113549.1.9.14).
 ///   </para>
 ///   <para>- or -</para>
 ///   <para>
 ///     <see cref="CertificateExtensions"/> contains a <see langword="null" /> value.
 ///   </para>
 ///   <para>- or -</para>
 ///   <para>
 ///     <see cref="CertificateExtensions"/> contains an entry with a <see langword="null" />
 ///     <see cref="AsnEncodedData.Oid" /> value.
 ///   </para>
 ///   <para>- or -</para>
 ///   <para>
 ///     This object was created with a constructor which did not accept a signing key.
 ///   </para>
 /// </exception>
 /// <exception cref="CryptographicException">
 ///   A cryptographic error occurs while creating the signing request.
 /// </exception>
 /// <seealso cref="CreateSigningRequest()"/>
 public string CreateSigningRequestPem()
 {
     byte[] der = CreateSigningRequest();
     return(PemEncoding.WriteString(PemLabels.Pkcs10CertificateRequest, der));
 }