LCOV - code coverage report
Current view: top level - dom/crypto - KeyAlgorithmProxy.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 63 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 16 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 Source Code Form is subject to the terms of the Mozilla Public
       4             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       5             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       6             : 
       7             : #ifndef mozilla_dom_KeyAlgorithmProxy_h
       8             : #define mozilla_dom_KeyAlgorithmProxy_h
       9             : 
      10             : #include "pk11pub.h"
      11             : #include "js/StructuredClone.h"
      12             : #include "mozilla/dom/KeyAlgorithmBinding.h"
      13             : #include "mozilla/dom/WebCryptoCommon.h"
      14             : 
      15             : #define KEY_ALGORITHM_SC_VERSION 0x00000001
      16             : 
      17             : namespace mozilla {
      18             : namespace dom {
      19             : 
      20             : // A heap-safe variant of RsaHashedKeyAlgorithm
      21             : // The only difference is that it uses CryptoBuffer instead of Uint8Array
      22           0 : struct RsaHashedKeyAlgorithmStorage {
      23             :   nsString mName;
      24             :   KeyAlgorithm mHash;
      25             :   uint16_t mModulusLength;
      26             :   CryptoBuffer mPublicExponent;
      27             : 
      28             :   bool
      29           0 :   ToKeyAlgorithm(JSContext* aCx, RsaHashedKeyAlgorithm& aRsa) const
      30             :   {
      31           0 :     JS::Rooted<JSObject*> exponent(aCx, mPublicExponent.ToUint8Array(aCx));
      32           0 :     if (!exponent) {
      33           0 :       return false;
      34             :     }
      35             : 
      36           0 :     aRsa.mName = mName;
      37           0 :     aRsa.mModulusLength = mModulusLength;
      38           0 :     aRsa.mHash.mName = mHash.mName;
      39           0 :     aRsa.mPublicExponent.Init(exponent);
      40           0 :     aRsa.mPublicExponent.ComputeLengthAndData();
      41             : 
      42           0 :     return true;
      43             :   }
      44             : };
      45             : 
      46             : // A heap-safe variant of DhKeyAlgorithm
      47             : // The only difference is that it uses CryptoBuffers instead of Uint8Arrays
      48           0 : struct DhKeyAlgorithmStorage {
      49             :   nsString mName;
      50             :   CryptoBuffer mPrime;
      51             :   CryptoBuffer mGenerator;
      52             : 
      53             :   bool
      54           0 :   ToKeyAlgorithm(JSContext* aCx, DhKeyAlgorithm& aDh) const
      55             :   {
      56           0 :     JS::Rooted<JSObject*> prime(aCx, mPrime.ToUint8Array(aCx));
      57           0 :     if (!prime) {
      58           0 :       return false;
      59             :     }
      60             : 
      61           0 :     JS::Rooted<JSObject*> generator(aCx, mGenerator.ToUint8Array(aCx));
      62           0 :     if (!generator) {
      63           0 :       return false;
      64             :     }
      65             : 
      66           0 :     aDh.mName = mName;
      67           0 :     aDh.mPrime.Init(prime);
      68           0 :     aDh.mPrime.ComputeLengthAndData();
      69           0 :     aDh.mGenerator.Init(generator);
      70           0 :     aDh.mGenerator.ComputeLengthAndData();
      71             : 
      72           0 :     return true;
      73             :   }
      74             : };
      75             : 
      76             : // This class encapuslates a KeyAlgorithm object, and adds several
      77             : // methods that make WebCrypto operations simpler.
      78           0 : struct KeyAlgorithmProxy
      79             : {
      80             :   enum KeyAlgorithmType {
      81             :     AES,
      82             :     HMAC,
      83             :     RSA,
      84             :     EC,
      85             :     DH,
      86             :   };
      87             :   KeyAlgorithmType mType;
      88             : 
      89             :   // Plain is always populated with the algorithm name
      90             :   // Others are only populated for the corresponding key type
      91             :   nsString mName;
      92             :   AesKeyAlgorithm mAes;
      93             :   HmacKeyAlgorithm mHmac;
      94             :   RsaHashedKeyAlgorithmStorage mRsa;
      95             :   EcKeyAlgorithm mEc;
      96             :   DhKeyAlgorithmStorage mDh;
      97             : 
      98             :   // Structured clone
      99             :   bool WriteStructuredClone(JSStructuredCloneWriter* aWriter) const;
     100             :   bool ReadStructuredClone(JSStructuredCloneReader* aReader);
     101             : 
     102             :   // Extract various forms of derived information
     103             :   CK_MECHANISM_TYPE Mechanism() const;
     104             :   nsString JwkAlg() const;
     105             : 
     106             :   // And in static form for calling on raw KeyAlgorithm dictionaries
     107             :   static CK_MECHANISM_TYPE GetMechanism(const KeyAlgorithm& aAlgorithm);
     108             :   static CK_MECHANISM_TYPE GetMechanism(const HmacKeyAlgorithm& aAlgorithm);
     109             :   static nsString GetJwkAlg(const KeyAlgorithm& aAlgorithm);
     110             : 
     111             :   // Construction of the various algorithm types
     112             :   void
     113           0 :   MakeAes(const nsString& aName, uint32_t aLength)
     114             :   {
     115           0 :     mType = AES;
     116           0 :     mName = aName;
     117           0 :     mAes.mName = aName;
     118           0 :     mAes.mLength = aLength;
     119           0 :   }
     120             : 
     121             :   void
     122           0 :   MakeHmac(uint32_t aLength, const nsString& aHashName)
     123             :   {
     124           0 :     mType = HMAC;
     125           0 :     mName = NS_LITERAL_STRING(WEBCRYPTO_ALG_HMAC);
     126           0 :     mHmac.mName = NS_LITERAL_STRING(WEBCRYPTO_ALG_HMAC);
     127           0 :     mHmac.mLength = aLength;
     128           0 :     mHmac.mHash.mName = aHashName;
     129           0 :   }
     130             : 
     131             :   bool
     132           0 :   MakeRsa(const nsString& aName, uint32_t aModulusLength,
     133             :          const CryptoBuffer& aPublicExponent, const nsString& aHashName)
     134             :   {
     135           0 :     mType = RSA;
     136           0 :     mName = aName;
     137           0 :     mRsa.mName = aName;
     138           0 :     mRsa.mModulusLength = aModulusLength;
     139           0 :     mRsa.mHash.mName = aHashName;
     140           0 :     if (!mRsa.mPublicExponent.Assign(aPublicExponent)) {
     141           0 :       return false;
     142             :     }
     143           0 :     return true;
     144             :   }
     145             : 
     146             :   void
     147           0 :   MakeEc(const nsString& aName, const nsString& aNamedCurve)
     148             :   {
     149           0 :     mType = EC;
     150           0 :     mName = aName;
     151           0 :     mEc.mName = aName;
     152           0 :     mEc.mNamedCurve = aNamedCurve;
     153           0 :   }
     154             : 
     155             :   bool
     156           0 :   MakeDh(const nsString& aName, const CryptoBuffer& aPrime,
     157             :          const CryptoBuffer& aGenerator)
     158             :   {
     159           0 :     mType = DH;
     160           0 :     mName = aName;
     161           0 :     mDh.mName = aName;
     162           0 :     if (!mDh.mPrime.Assign(aPrime)) {
     163           0 :       return false;
     164             :     }
     165           0 :     if (!mDh.mGenerator.Assign(aGenerator)) {
     166           0 :       return false;
     167             :     }
     168           0 :     return true;
     169             :   }
     170             : };
     171             : 
     172             : } // namespace dom
     173             : } // namespace mozilla
     174             : 
     175             : #endif // mozilla_dom_KeyAlgorithmProxy_h

Generated by: LCOV version 1.13