ARC SDK
Credential.h
1 #ifndef __ARC_CREDENTIAL_H__
2 #define __ARC_CREDENTIAL_H__
3 
4 #include <stdlib.h>
5 #include <stdexcept>
6 #include <iostream>
7 #include <string>
8 #include <openssl/asn1.h>
9 #include <openssl/pem.h>
10 #include <openssl/x509.h>
11 #include <openssl/x509v3.h>
12 #include <openssl/pkcs12.h>
13 #include <openssl/err.h>
14 
15 #include <arc/Logger.h>
16 #include <arc/DateTime.h>
17 #include <arc/UserConfig.h>
18 
19 #include <arc/credential/CertUtil.h>
20 #include <arc/credential/PasswordSource.h>
21 
22 namespace Arc {
23 
26 
27 
32 class CredentialError : public std::runtime_error {
33  public:
34  // Constructor
38  CredentialError(const std::string& what="");
39 };
40 
41 typedef enum {CRED_PEM, CRED_DER, CRED_PKCS, CRED_UNKNOWN} Credformat;
43 
46 typedef enum { SIGN_DEFAULT = 0,
47  SIGN_SHA1,
48 #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL)
49  SIGN_SHA224,
50  SIGN_SHA256,
51  SIGN_SHA384,
52  SIGN_SHA512
53 #endif
54  } Signalgorithm;
55 
57 extern Logger CredentialLogger;
58 
60 
76 class Credential {
77  public:
81  Credential();
82 
86  Credential(int keybits);
87 
88  virtual ~Credential();
89 
93  Credential(const std::string& CAfile, const std::string& CAkey,
94  const std::string& CAserial,
95  const std::string& extfile, const std::string& extsect,
96  const std::string& passphrase4key);
97 
102  Credential(const std::string& CAfile, const std::string& CAkey,
103  const std::string& CAserial,
104  const std::string& extfile, const std::string& extsect,
105  PasswordSource& passphrase4key);
106 
146  Credential(Time start, Period lifetime = Period("PT12H"),
147  int keybits = 1024, std::string proxyversion = "rfc",
148  std::string policylang = "inheritAll", std::string policy = "",
149  int pathlength = -1);
150 
165  Credential(const std::string& cert, const std::string& key, const std::string& cadir,
166  const std::string& cafile, const std::string& passphrase4key = "",
167  const bool is_file = true);
168 
173  Credential(const std::string& cert, const std::string& key, const std::string& cadir,
174  const std::string& cafile, PasswordSource& passphrase4key,
175  const bool is_file = true);
176 
184  Credential(const UserConfig& usercfg, const std::string& passphrase4key = "");
185 
190  Credential(const UserConfig& usercfg, PasswordSource& passphrase4key);
191 
193  static void InitProxyCertInfo(void);
194 
199  static bool IsCredentialsValid(const UserConfig& usercfg);
200 
202  void AddCertExtObj(std::string& sn, std::string& oid);
203 
205 
208  void SetSigningAlgorithm(Signalgorithm signing_algorithm = SIGN_DEFAULT);
209 
211 
214  void SetKeybits(int keybits = 0);
215 
216  static std::string NoPassword(void) { return std::string("\0",1); };
217 
218  private:
219 
221  Credential(const Credential&);
222 
223  void InitCredential(const std::string& cert, const std::string& key, const std::string& cadir,
224  const std::string& cafile, PasswordSource& passphrase4key, const bool is_file);
225 
227  //void loadKeyString(const std::string& key, EVP_PKEY* &pkey, const std::string& passphrase = "");
228  void loadKeyString(const std::string& key, EVP_PKEY* &pkey, PasswordSource& passphrase);
229  //void loadKeyFile(const std::string& keyfile, EVP_PKEY* &pkey, const std::string& passphrase = "");
230  void loadKeyFile(const std::string& keyfile, EVP_PKEY* &pkey, PasswordSource& passphrase);
231  //void loadKey(BIO* bio, EVP_PKEY* &pkey, const std::string& passphrase = "", const std::string& prompt_info = "", const bool is_file = true);
232 
236  void loadCertificateString(const std::string& cert, X509* &x509, STACK_OF(X509)** certchain);
237  void loadCertificateFile(const std::string& certfile, X509* &x509, STACK_OF(X509)** certchain);
238  //void loadCertificate(BIO* bio, X509* &x509, STACK_OF(X509)** certchain, const bool is_file=true);
239 
241  void InitVerification(void);
242 
247  bool Verify(void);
248 
254  X509_EXTENSION* CreateExtension(const std::string& name, const std::string& data, bool crit = false);
255 
263  bool SetProxyPeriod(X509* tosign, X509* issuer, const Time& start, const Period& lifetime);
264 
268  bool SignRequestAssistant(Credential* proxy, EVP_PKEY* req_pubkey, X509** tosign);
269 
270  public:
272  void LogError(void) const;
273 
274  /************************************/
275  /*****Get information from "this" object**/
276 
278  bool GetVerification(void) const {return verification_valid; };
279 
281  EVP_PKEY* GetPrivKey(void) const;
282 
284  EVP_PKEY* GetPubKey(void) const;
285 
287  X509* GetCert(void) const;
288 
290  X509_REQ* GetCertReq(void) const;
291 
293  STACK_OF(X509)* GetCertChain(void) const;
294 
298  int GetCertNumofChain(void) const;
299 
304  Credformat getFormat_BIO(BIO * in, const bool is_file = true) const;
305  Credformat getFormat_str(const std::string& source) const;
306 
308  std::string GetDN(void) const;
309 
313  std::string GetIdentityName(void) const;
314 
316  ArcCredential::certType GetType(void) const;
317 
319  std::string GetIssuerName(void) const;
320 
324  std::string GetCAName(void) const;
325 
330 
334  int GetKeybits(void) const;
335 
339  std::string GetProxyPolicy(void) const;
340 
344  void SetProxyPolicy(const std::string& proxyversion, const std::string& policylang,
345  const std::string& policy, int pathlength);
346 
352  bool OutputPrivatekey(std::string &content, bool encryption = false, const std::string& passphrase ="");
353 
360  bool OutputPrivatekey(std::string &content, bool encryption, PasswordSource& passphrase);
361 
363  bool OutputPublickey(std::string &content);
364 
369  bool OutputCertificate(std::string &content, bool is_der=false);
370 
375  bool OutputCertificateChain(std::string &content, bool is_der=false);
376 
378  Period GetLifeTime(void) const;
379 
381  Time GetStartTime() const;
382 
384  Time GetEndTime() const;
385 
387  void SetLifeTime(const Period& period);
388 
390  void SetStartTime(const Time& start_time);
391 
393  bool IsValid(void);
394 
395  /************************************/
396  /*****Generate certificate request, add certificate extension, inquire certificate request,
397  *and sign certificate request
398  **/
399 
406  bool AddExtension(const std::string& name, const std::string& data, bool crit = false);
407 
422  bool AddExtension(const std::string& name, char** binary);
423 
429  std::string GetExtension(const std::string& name);
430 
437  bool GenerateEECRequest(BIO* reqbio, BIO* keybio, const std::string& dn = "");
438 
440  bool GenerateEECRequest(std::string &reqcontent, std::string &keycontent, const std::string& dn = "");
441 
443  bool GenerateEECRequest(const char* request_filename, const char* key_filename, const std::string& dn = "");
444 
449  bool GenerateRequest(BIO* bio, bool if_der = false);
450 
452  bool GenerateRequest(std::string &content, bool if_der = false);
453 
455  bool GenerateRequest(const char* filename, bool if_der = false);
456 
465  bool InquireRequest(BIO* reqbio, bool if_eec = false, bool if_der = false);
466 
468  bool InquireRequest(std::string &content, bool if_eec = false, bool if_der = false);
469 
471  bool InquireRequest(const char* filename, bool if_eec = false, bool if_der = false);
472 
479  bool SignRequest(Credential* proxy, BIO* outputbio, bool if_der = false);
480 
486  bool SignRequest(Credential* proxy, std::string &content, bool if_der = false);
487 
493  bool SignRequest(Credential* proxy, const char* filename, bool if_der = false);
494 
502  bool SelfSignEECRequest(const std::string& dn, const char* extfile, const std::string& extsect, const char* certfile);
503 
504  //The following three methods is about signing an EEC certificate by implementing the same
505  //functionality as a normal CA
507  bool SignEECRequest(Credential* eec, const std::string& dn, BIO* outputbio);
508 
510  bool SignEECRequest(Credential* eec, const std::string& dn, std::string &content);
511 
513  bool SignEECRequest(Credential* eec, const std::string& dn, const char* filename);
514 
515  private:
516  // PKI files
517  std::string cacertfile_;
518  std::string cacertdir_;
519  std::string certfile_;
520  std::string keyfile_;
521 
522  // Verification context
524 
525  //Verification result
526  bool verification_valid;
527 
528  //Certificate structures
529  X509 * cert_; //certificate
530  ArcCredential::certType cert_type_;
531  EVP_PKEY * pkey_; //private key
532  STACK_OF(X509) * cert_chain_; //certificates chain which is parsed
533  //from the certificate, after
534  //verification, the ca certificate
535  //will be included
536  ArcCredential::PROXYCERTINFO* proxy_cert_info_;
537  Credformat format;
538  Time start_;
539  Period lifetime_;
540 
541  //Certificate request
542  X509_REQ* req_;
543  RSA* rsa_key_;
544  EVP_MD* signing_alg_;
545  int keybits_;
546 
547  //Proxy policy
548  std::string proxyversion_;
549  std::string policy_;
550  std::string policylang_;
551  int proxyver_;
552  int pathlength_;
553 
554  //Extensions for certificate, such as certificate policy, attributes, etc.
555  STACK_OF(X509_EXTENSION)* extensions_;
556 
557  //CA functionality related information
558  std::string CAserial_;
559  std::string extfile_;
560  std::string extsect_;
561 
562  static X509_NAME *parse_name(char *subject, long chtype, int multirdn);
563 };
564 
565 }// namespace Arc
566 
567 #endif /* __ARC_CREDENTIAL_H__ */
568