7506a8bb0025dd7de6f862ddb0fbd370e4c39f81
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / XSECCryptoX509CRL.cpp
1 /*
2  * Copyright 2001-2010 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * XSECCryptoX509CRL.cpp
19  * 
20  * Wrapper for X.509 CRL objects, similar to existing XSEC wrappers.
21  */
22
23 #include "internal.h"
24 #include "security/XSECCryptoX509CRL.h"
25
26 #include <xsec/framework/XSECError.hpp>
27 #include <xsec/enc/XSECCryptoException.hpp>
28
29 using namespace xmltooling;
30
31 XSECCryptoX509CRL::XSECCryptoX509CRL()
32 {
33 }
34
35 XSECCryptoX509CRL::~XSECCryptoX509CRL()
36 {
37 }
38
39 void XSECCryptoX509CRL::loadX509CRLPEM(const char* buf, unsigned int len)
40 {
41         const char * b;
42         char * b1 = nullptr;
43         if (len == 0)
44                 b = buf;
45         else {
46                 XSECnew(b1, char[len+1]);
47                 memcpy(b1, buf, len);
48                 b1[len] = '\0';
49                 b = b1;
50         }
51
52         const char *p = strstr(buf, "-----BEGIN X509 CRL-----");
53
54         if (p == nullptr) {
55
56                 if (b1 != nullptr)
57                         delete[] b1;
58
59                 throw XSECCryptoException(XSECCryptoException::X509Error,
60                 "X509CRL::loadX509CRLPEM - Cannot find start of PEM CRL");
61
62         }
63
64         p += strlen("-----BEGIN X509 CRL-----");
65
66         while (*p == '\n' || *p == '\r' || *p == '-')
67                 p++;
68
69         safeBuffer output;
70         int i = 0;
71         while (*p != '\0' && *p != '-') {
72                 output[i++] = *p;
73                 ++p;
74         }
75
76         if (strstr(p, "-----END X509 CRL-----") != p) {
77
78                 if (b1 != nullptr)
79                         delete[] b1;
80
81                 throw XSECCryptoException(XSECCryptoException::X509Error,
82                 "X509CRL::loadX509PEMCRL - Cannot find end of PEM certificate");
83
84         }
85         
86         if (b1 != nullptr)
87                 delete[] b1;
88
89         output[i] = '\0';
90
91         this->loadX509CRLBase64Bin(output.rawCharBuffer(), i);
92
93 }
94