1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This code is made available to you under your choice of the following sets
4  * of licensing terms:
5  */
6 /* This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9  */
10 /* Copyright 2015 Mozilla Contributors
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24 
25 #include "mozpkix/pkixutil.h"
26 
27 namespace mozilla { namespace pkix {
28 
29 Result
DigestSignedData(TrustDomain & trustDomain,const der::SignedDataWithSignature & signedData,uint8_t (& digestBuf)[MAX_DIGEST_SIZE_IN_BYTES],der::PublicKeyAlgorithm & publicKeyAlg,SignedDigest & signedDigest)30 DigestSignedData(TrustDomain& trustDomain,
31                  const der::SignedDataWithSignature& signedData,
32                  /*out*/ uint8_t(&digestBuf)[MAX_DIGEST_SIZE_IN_BYTES],
33                  /*out*/ der::PublicKeyAlgorithm& publicKeyAlg,
34                  /*out*/ SignedDigest& signedDigest)
35 {
36   Reader signatureAlg(signedData.algorithm);
37   Result rv = der::SignatureAlgorithmIdentifierValue(
38                 signatureAlg, publicKeyAlg, signedDigest.digestAlgorithm);
39   if (rv != Success) {
40     return rv;
41   }
42   if (!signatureAlg.AtEnd()) {
43     return Result::ERROR_BAD_DER;
44   }
45 
46   size_t digestLen = DigestAlgorithmToSizeInBytes(signedDigest.digestAlgorithm);
47   assert(digestLen <= sizeof(digestBuf));
48 
49   rv = trustDomain.DigestBuf(signedData.data, signedDigest.digestAlgorithm,
50                              digestBuf, digestLen);
51   if (rv != Success) {
52     return rv;
53   }
54   rv = signedDigest.digest.Init(digestBuf, digestLen);
55   if (rv != Success) {
56     return rv;
57   }
58 
59   return signedDigest.signature.Init(signedData.signature);
60 }
61 
62 Result
VerifySignedDigest(TrustDomain & trustDomain,der::PublicKeyAlgorithm publicKeyAlg,const SignedDigest & signedDigest,Input signerSubjectPublicKeyInfo)63 VerifySignedDigest(TrustDomain& trustDomain,
64                    der::PublicKeyAlgorithm publicKeyAlg,
65                    const SignedDigest& signedDigest,
66                    Input signerSubjectPublicKeyInfo)
67 {
68   switch (publicKeyAlg) {
69     case der::PublicKeyAlgorithm::ECDSA:
70       return trustDomain.VerifyECDSASignedDigest(signedDigest,
71                                                  signerSubjectPublicKeyInfo);
72     case der::PublicKeyAlgorithm::RSA_PKCS1:
73       return trustDomain.VerifyRSAPKCS1SignedDigest(signedDigest,
74                                                     signerSubjectPublicKeyInfo);
75     case der::PublicKeyAlgorithm::Uninitialized:
76       assert(false);
77       return Result::FATAL_ERROR_LIBRARY_FAILURE;
78     MOZILLA_PKIX_UNREACHABLE_DEFAULT_ENUM
79   }
80 }
81 
82 Result
VerifySignedData(TrustDomain & trustDomain,const der::SignedDataWithSignature & signedData,Input signerSubjectPublicKeyInfo)83 VerifySignedData(TrustDomain& trustDomain,
84                  const der::SignedDataWithSignature& signedData,
85                  Input signerSubjectPublicKeyInfo)
86 {
87   uint8_t digestBuf[MAX_DIGEST_SIZE_IN_BYTES];
88   der::PublicKeyAlgorithm publicKeyAlg;
89   SignedDigest signedDigest;
90   Result rv = DigestSignedData(trustDomain, signedData, digestBuf,
91                                publicKeyAlg, signedDigest);
92   if (rv != Success) {
93     return rv;
94   }
95   return VerifySignedDigest(trustDomain, publicKeyAlg, signedDigest,
96                             signerSubjectPublicKeyInfo);
97 }
98 
99 } } // namespace mozilla::pkix
100