Home | History | Annotate | Download | only in crypto
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CRYPTO_SIGNATURE_CREATOR_H_
      6 #define CRYPTO_SIGNATURE_CREATOR_H_
      7 #pragma once
      8 
      9 #include "build/build_config.h"
     10 
     11 #if defined(USE_OPENSSL)
     12 // Forward declaration for openssl/*.h
     13 typedef struct env_md_ctx_st EVP_MD_CTX;
     14 #elif defined(USE_NSS)
     15 // Forward declaration.
     16 struct SGNContextStr;
     17 #elif defined(OS_MACOSX)
     18 #include <Security/cssm.h>
     19 #endif
     20 
     21 #include <vector>
     22 
     23 #include "base/basictypes.h"
     24 #include "crypto/rsa_private_key.h"
     25 
     26 #if defined(OS_WIN)
     27 #include "crypto/scoped_capi_types.h"
     28 #endif
     29 
     30 namespace crypto {
     31 
     32 // Signs data using a bare private key (as opposed to a full certificate).
     33 // Currently can only sign data using SHA-1 with RSA encryption.
     34 class SignatureCreator {
     35  public:
     36   ~SignatureCreator();
     37 
     38   // Create an instance. The caller must ensure that the provided PrivateKey
     39   // instance outlives the created SignatureCreator.
     40   static SignatureCreator* Create(RSAPrivateKey* key);
     41 
     42   // Update the signature with more data.
     43   bool Update(const uint8* data_part, int data_part_len);
     44 
     45   // Finalize the signature.
     46   bool Final(std::vector<uint8>* signature);
     47 
     48  private:
     49   // Private constructor. Use the Create() method instead.
     50   SignatureCreator();
     51 
     52   RSAPrivateKey* key_;
     53 
     54 #if defined(USE_OPENSSL)
     55   EVP_MD_CTX* sign_context_;
     56 #elif defined(USE_NSS)
     57   SGNContextStr* sign_context_;
     58 #elif defined(OS_MACOSX)
     59   CSSM_CC_HANDLE sig_handle_;
     60 #elif defined(OS_WIN)
     61   ScopedHCRYPTHASH hash_object_;
     62 #endif
     63 
     64   DISALLOW_COPY_AND_ASSIGN(SignatureCreator);
     65 };
     66 
     67 }  // namespace crypto
     68 
     69 #endif  // CRYPTO_SIGNATURE_CREATOR_H_
     70