security/pkix/lib/pkixutil.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* Copyright 2013 Mozilla Foundation
michael@0 4 *
michael@0 5 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 6 * you may not use this file except in compliance with the License.
michael@0 7 * You may obtain a copy of the License at
michael@0 8 *
michael@0 9 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 10 *
michael@0 11 * Unless required by applicable law or agreed to in writing, software
michael@0 12 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 14 * See the License for the specific language governing permissions and
michael@0 15 * limitations under the License.
michael@0 16 */
michael@0 17
michael@0 18 #ifndef mozilla_pkix__pkixutil_h
michael@0 19 #define mozilla_pkix__pkixutil_h
michael@0 20
michael@0 21 #include "pkix/pkixtypes.h"
michael@0 22 #include "prerror.h"
michael@0 23 #include "seccomon.h"
michael@0 24 #include "secerr.h"
michael@0 25
michael@0 26 namespace mozilla { namespace pkix {
michael@0 27
michael@0 28 enum Result
michael@0 29 {
michael@0 30 Success = 0,
michael@0 31 FatalError = -1, // An error was encountered that caused path building
michael@0 32 // to stop immediately. example: out-of-memory.
michael@0 33 RecoverableError = -2 // an error that will cause path building to continue
michael@0 34 // searching for alternative paths. example: expired
michael@0 35 // certificate.
michael@0 36 };
michael@0 37
michael@0 38 // When returning errors, use this function instead of calling PR_SetError
michael@0 39 // directly. This helps ensure that we always call PR_SetError when we return
michael@0 40 // an error code. This is a useful place to set a breakpoint when a debugging
michael@0 41 // a certificate verification failure.
michael@0 42 inline Result
michael@0 43 Fail(Result result, PRErrorCode errorCode)
michael@0 44 {
michael@0 45 PR_ASSERT(result != Success);
michael@0 46 PR_SetError(errorCode, 0);
michael@0 47 return result;
michael@0 48 }
michael@0 49
michael@0 50 inline Result
michael@0 51 MapSECStatus(SECStatus srv)
michael@0 52 {
michael@0 53 if (srv == SECSuccess) {
michael@0 54 return Success;
michael@0 55 }
michael@0 56
michael@0 57 PRErrorCode error = PORT_GetError();
michael@0 58 switch (error) {
michael@0 59 case SEC_ERROR_EXTENSION_NOT_FOUND:
michael@0 60 return RecoverableError;
michael@0 61
michael@0 62 case PR_INVALID_STATE_ERROR:
michael@0 63 case SEC_ERROR_LIBRARY_FAILURE:
michael@0 64 case SEC_ERROR_NO_MEMORY:
michael@0 65 return FatalError;
michael@0 66 }
michael@0 67
michael@0 68 // TODO: PORT_Assert(false); // we haven't classified the error yet
michael@0 69 return RecoverableError;
michael@0 70 }
michael@0 71
michael@0 72 // During path building and verification, we build a linked list of BackCerts
michael@0 73 // from the current cert toward the end-entity certificate. The linked list
michael@0 74 // is used to verify properties that aren't local to the current certificate
michael@0 75 // and/or the direct link between the current certificate and its issuer,
michael@0 76 // such as name constraints.
michael@0 77 //
michael@0 78 // Each BackCert contains pointers to all the given certificate's extensions
michael@0 79 // so that we can parse the extension block once and then process the
michael@0 80 // extensions in an order that may be different than they appear in the cert.
michael@0 81 class BackCert
michael@0 82 {
michael@0 83 public:
michael@0 84 // ExcludeCN means that GetConstrainedNames won't include the subject CN in
michael@0 85 // its results. IncludeCN means that GetConstrainedNames will include the
michael@0 86 // subject CN in its results.
michael@0 87 enum ConstrainedNameOptions { ExcludeCN = 0, IncludeCN = 1 };
michael@0 88
michael@0 89 // nssCert and childCert must be valid for the lifetime of BackCert
michael@0 90 BackCert(CERTCertificate* nssCert, BackCert* childCert,
michael@0 91 ConstrainedNameOptions cnOptions)
michael@0 92 : encodedBasicConstraints(nullptr)
michael@0 93 , encodedCertificatePolicies(nullptr)
michael@0 94 , encodedExtendedKeyUsage(nullptr)
michael@0 95 , encodedKeyUsage(nullptr)
michael@0 96 , encodedNameConstraints(nullptr)
michael@0 97 , encodedInhibitAnyPolicy(nullptr)
michael@0 98 , childCert(childCert)
michael@0 99 , nssCert(nssCert)
michael@0 100 , constrainedNames(nullptr)
michael@0 101 , cnOptions(cnOptions)
michael@0 102 {
michael@0 103 }
michael@0 104
michael@0 105 Result Init();
michael@0 106
michael@0 107 const SECItem* encodedBasicConstraints;
michael@0 108 const SECItem* encodedCertificatePolicies;
michael@0 109 const SECItem* encodedExtendedKeyUsage;
michael@0 110 const SECItem* encodedKeyUsage;
michael@0 111 const SECItem* encodedNameConstraints;
michael@0 112 const SECItem* encodedInhibitAnyPolicy;
michael@0 113
michael@0 114 BackCert* const childCert;
michael@0 115
michael@0 116 // Only non-const so that we can pass this to TrustDomain::IsRevoked,
michael@0 117 // which only takes a non-const pointer because VerifyEncodedOCSPResponse
michael@0 118 // requires it, and that is only because the implementation of
michael@0 119 // VerifyEncodedOCSPResponse does a CERT_DupCertificate. TODO: get rid
michael@0 120 // of that CERT_DupCertificate so that we can make all these things const.
michael@0 121 /*const*/ CERTCertificate* GetNSSCert() const { return nssCert; }
michael@0 122
michael@0 123 // Returns the names that should be considered when evaluating name
michael@0 124 // constraints. The list is constructed lazily and cached. The result is a
michael@0 125 // weak reference; do not try to free it, and do not hold long-lived
michael@0 126 // references to it.
michael@0 127 Result GetConstrainedNames(/*out*/ const CERTGeneralName** result);
michael@0 128
michael@0 129 // This is the only place where we should be dealing with non-const
michael@0 130 // CERTCertificates.
michael@0 131 Result PrependNSSCertToList(CERTCertList* results);
michael@0 132
michael@0 133 PLArenaPool* GetArena();
michael@0 134
michael@0 135 private:
michael@0 136 CERTCertificate* nssCert;
michael@0 137
michael@0 138 ScopedPLArenaPool arena;
michael@0 139 CERTGeneralName* constrainedNames;
michael@0 140 ConstrainedNameOptions cnOptions;
michael@0 141
michael@0 142 BackCert(const BackCert&) /* = delete */;
michael@0 143 void operator=(const BackCert&); /* = delete */;
michael@0 144 };
michael@0 145
michael@0 146 } } // namespace mozilla::pkix
michael@0 147
michael@0 148 #endif // mozilla_pkix__pkixutil_h

mercurial