1 // Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <assert.h>
16 
17 #include <openssl/asn1.h>
18 #include <openssl/err.h>
19 #include <openssl/mem.h>
20 #include <openssl/obj.h>
21 #include <openssl/x509.h>
22 
23 
X509_CRL_print_fp(FILE * fp,X509_CRL * x)24 int X509_CRL_print_fp(FILE *fp, X509_CRL *x) {
25   BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
26   if (b == NULL) {
27     OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
28     return 0;
29   }
30   int ret = X509_CRL_print(b, x);
31   BIO_free(b);
32   return ret;
33 }
34 
X509_CRL_print(BIO * out,X509_CRL * x)35 int X509_CRL_print(BIO *out, X509_CRL *x) {
36   long version = X509_CRL_get_version(x);
37   assert(X509_CRL_VERSION_1 <= version && version <= X509_CRL_VERSION_2);
38   const X509_ALGOR *sig_alg;
39   const ASN1_BIT_STRING *signature;
40   X509_CRL_get0_signature(x, &signature, &sig_alg);
41   if (BIO_printf(out, "Certificate Revocation List (CRL):\n") <= 0 ||
42       BIO_printf(out, "%8sVersion %ld (0x%lx)\n", "", version + 1,
43                  (unsigned long)version) <= 0 ||
44       // Note this and the other |X509_signature_print| call both print the
45       // outer signature algorithm, rather than printing the inner and outer
46       // ones separately. This matches OpenSSL, though it was probably a bug.
47       !X509_signature_print(out, sig_alg, NULL)) {
48     return 0;
49   }
50 
51   char *issuer = X509_NAME_oneline(X509_CRL_get_issuer(x), NULL, 0);
52   int ok = issuer != NULL && BIO_printf(out, "%8sIssuer: %s\n", "", issuer) > 0;
53   OPENSSL_free(issuer);
54   if (!ok) {
55     return 0;
56   }
57 
58   if (BIO_printf(out, "%8sLast Update: ", "") <= 0 ||
59       !ASN1_TIME_print(out, X509_CRL_get0_lastUpdate(x)) ||
60       BIO_printf(out, "\n%8sNext Update: ", "") <= 0) {
61     return 0;
62   }
63   if (X509_CRL_get0_nextUpdate(x)) {
64     if (!ASN1_TIME_print(out, X509_CRL_get0_nextUpdate(x))) {
65       return 0;
66     }
67   } else {
68     if (BIO_printf(out, "NONE") <= 0) {
69       return 0;
70     }
71   }
72 
73   if (BIO_printf(out, "\n") <= 0 ||
74       !X509V3_extensions_print(out, "CRL extensions",
75                                X509_CRL_get0_extensions(x), 0, 8)) {
76     return 0;
77   }
78 
79   const STACK_OF(X509_REVOKED) *rev = X509_CRL_get_REVOKED(x);
80   if (sk_X509_REVOKED_num(rev) > 0) {
81     if (BIO_printf(out, "Revoked Certificates:\n") <= 0) {
82       return 0;
83     }
84   } else {
85     if (BIO_printf(out, "No Revoked Certificates.\n") <= 0) {
86       return 0;
87     }
88   }
89 
90   for (size_t i = 0; i < sk_X509_REVOKED_num(rev); i++) {
91     const X509_REVOKED *r = sk_X509_REVOKED_value(rev, i);
92     if (BIO_printf(out, "    Serial Number: ") <= 0 ||
93         i2a_ASN1_INTEGER(out, X509_REVOKED_get0_serialNumber(r)) <= 0 ||
94         BIO_printf(out, "\n        Revocation Date: ") <= 0 ||
95         !ASN1_TIME_print(out, X509_REVOKED_get0_revocationDate(r)) ||
96         BIO_printf(out, "\n") <= 0 ||
97         !X509V3_extensions_print(out, "CRL entry extensions",
98                                  X509_REVOKED_get0_extensions(r), 0, 8)) {
99     }
100   }
101 
102   return X509_signature_print(out, sig_alg, signature);
103 }
104