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