46#include <openssl/pem.h>
87 klen = (klen <= 0) ? 24 : klen;
90 char *realsalt = (
char *)salt;
95 char *ibeg = (
char *)memchr(salt+1,
'$',slen-1);
98 int newit = strtol(ibeg+1, &del, 10);
99 if (newit > 0 && del[0] ==
'$' && errno != ERANGE) {
103 realslen = slen - (int)(realsalt-salt);
107 PKCS5_PBKDF2_HMAC_SHA1(pass, plen,
108 (
unsigned char *)realsalt, realslen, it,
109 klen, (
unsigned char *)key);
119 X509 *c = cert ? (X509 *)(cert->
Opaque()) : 0;
120 X509 *r = ref ? (X509 *)(ref->
Opaque()) : 0;
121 EVP_PKEY *rk = r ? X509_get_pubkey(r) : 0;
122 if (!c || !rk)
return 0;
125 return (X509_verify(c, rk) > 0);
135 if (!chain || chain->
Size() <= 1)
139 X509_STORE *store = X509_STORE_new();
144 X509_STORE_set_verify_cb_func(store, 0);
150 X509_STORE_add_cert(store, (X509 *)(cert->
Opaque()));
153 STACK_OF(X509) *stk = sk_X509_new_null();
159 while ((cert = chain->
Next()) && cert->
Opaque()) {
161 cref = (X509 *)(cert->
Opaque());
162 sk_X509_push(stk, (X509 *)(cert->
Opaque()));
166 if (sk_X509_num(stk) != chain->
Size() - 1)
170 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
175 X509_STORE_CTX_init(ctx, store, cref, stk);
178 bool verify_ok = (X509_verify_cert(ctx) == 1);
186 X509_STORE_CTX_free(ctx);
187 sk_X509_pop_free(stk, X509_free);
188 X509_STORE_free(store);
198 EPNAME(
"X509ExportChain");
202 if (!chain || chain->
Size() <= 0) {
203 DEBUG(
"chain undefined or empty: nothing to export");
210 DEBUG(
"chain contains only a CA certificate: nothing to export");
215 BIO *bmem = BIO_new(BIO_s_mem());
217 DEBUG(
"unable to create BIO for memory operations");
226 if (!PEM_write_bio_X509(bmem, (X509 *)c->
Opaque())) {
227 DEBUG(
"error while writing proxy certificate");
232 if (withprivatekey) {
235 if (!PEM_write_bio_PrivateKey(bmem, (EVP_PKEY *)(k->
Opaque()),
237 DEBUG(
"error while writing proxy private key");
246 DEBUG(
"Encountered CA in chain; breaking. Subject: " << c->
Subject());
251 if (!PEM_write_bio_X509(bmem, (X509 *)c->
Opaque())) {
252 DEBUG(
"error while writing proxy certificate");
257 DEBUG(
"Encountered self-signed CA in chain; breaking. Subject: " << c->
Subject());
264 int blen = BIO_get_mem_data(bmem, &bdata);
265 DEBUG(
"BIO data: "<<blen<<
" bytes at 0x"<<(
int *)bdata);
272 DEBUG(
"result of serialization: "<<bck->
size<<
" bytes");
274 DEBUG(
"unable to create bucket for serialized format");
290 EPNAME(
"X509ChainToFile");
293 if (!x509 || !file) {
294 DEBUG(
"Invalid inputs");
298 if (PEM_write_X509(file, (X509 *)x509->
Opaque()) != 1) {
299 DEBUG(
"error while writing certificate " << fname);
310 EPNAME(
"X509ChainToFile");
314 DEBUG(
"Invalid inputs");
319 FILE *fp =
fopen(fn,
"w");
321 DEBUG(
"cannot open file to save chain (file: "<<fn<<
")");
324 int ifp = fileno(fp);
326 DEBUG(
"got invalid file descriptor (file: "<<fn<<
")");
336 DEBUG(
"could not lock file: "<<fn<<
")");
342 if (fchmod(ifp, 0600) == -1) {
343 DEBUG(
"cannot set permissions on file: "<<fn<<
" (errno: "<<errno<<
")");
353 if (PEM_write_X509(fp, (X509 *)c->
Opaque()) != 1) {
354 DEBUG(
"error while writing proxy certificate");
361 if (PEM_write_PrivateKey(fp, (EVP_PKEY *)(k->
Opaque()),
362 0, 0, 0, 0, 0) != 1) {
363 DEBUG(
"error while writing proxy private key");
371 if (PEM_write_X509(fp, (X509 *)c->
Opaque()) != 1) {
372 DEBUG(
"error while writing proxy certificate");
393 DEBUG(
"chain undefined: can do nothing");
410 STACK_OF(X509) *pChain = pc->getChain();
412 for (
int i=0; i < sk_X509_num(pChain); i++) {
413 X509 *cert = sk_X509_value(pChain, i);
425 DEBUG(
"could not create certificate: memory exhausted?");
444 FILE *fcer =
fopen(fname,
"r");
446 DEBUG(
"unable to open file (errno: "<<errno<<
")");
458 const char *fname,
const char *fkey)
469 DEBUG(
"FILE object undefined: can do nothing");
475 DEBUG(
"chain undefined: can do nothing");
481 while (PEM_read_X509(fcer, &xcer, 0, 0)) {
487 DEBUG(
"certificate for '"<<c->
Subject()<<
"'added to the chain - ord: "<<chain->
Size());
489 DEBUG(
"could not create certificate: memory exhausted?");
507 fcer =
fopen(fkey,
"r");
509 DEBUG(
"unable to open key file (errno: "<<errno<<
")");
515 if (!PEM_read_PrivateKey(fcer, &rsa, 0, 0)) {
516 DEBUG(
"no RSA private key found in file " << fname);
518 DEBUG(
"found a RSA private key in file " << fname);
524 while (cert && cert->
Opaque()) {
527 EVP_PKEY *evpp = X509_get_pubkey((X509 *)(cert->
Opaque()));
530#if OPENSSL_VERSION_NUMBER < 0x30000000L
531 int rc = EVP_PKEY_cmp(evpp, rsa);
533 int rc = EVP_PKEY_eq(evpp, rsa);
540 DEBUG(
"RSA key completed");
547 cert = chain->
Next();
571 EPNAME(
"X509ParseBucket");
575 if (!b || b->
size <= 0) {
576 DEBUG(
"bucket undefined or empty: can do nothing");
582 DEBUG(
"chain undefined: can do nothing");
587 BIO *bmem = BIO_new(BIO_s_mem());
589 DEBUG(
"unable to create BIO to import certificates");
594 if (BIO_write(bmem,(
const void *)(b->
buffer),b->
size) != b->
size) {
595 DEBUG(
"problems writing data to BIO");
602 while (PEM_read_bio_X509(bmem, &xcer, 0, 0)) {
609 DEBUG(
"certificate added to the chain - ord: "<<chain->
Size());
611 DEBUG(
"could not create certificate: memory exhausted?");
623 if (nci && BIO_write(bmem,(
const void *)(b->
buffer),b->
size) == b->
size) {
625 if (!PEM_read_bio_PrivateKey(bmem, &rsa, 0, 0)) {
626 DEBUG(
"no RSA private key found in bucket");
628 DEBUG(
"found a RSA private key in bucket");
634 while (cert && cert->
Opaque()) {
637 EVP_PKEY *evpp = X509_get_pubkey((X509 *)(cert->
Opaque()));
640#if OPENSSL_VERSION_NUMBER < 0x30000000L
641 int rc = EVP_PKEY_cmp(evpp, rsa);
643 int rc = EVP_PKEY_eq(evpp, rsa);
650 DEBUG(
"RSA key completed");
657 cert = chain->
Next();
685 if (!tsn1)
return etime;
694 if ((sscanf((
const char *)(tsn1->data),
695 "%02d%02d%02d%02d%02d%02d%c",
696 &(ltm.tm_year), &(ltm.tm_mon), &(ltm.tm_mday),
697 &(ltm.tm_hour), &(ltm.tm_min), &(ltm.tm_sec),
698 &zz) != 7) || (zz !=
'Z')) {
700 if ((sscanf((
const char *)(tsn1->data),
701 "%04d%02d%02d%02d%02d%02d%c",
702 &(ltm.tm_year), &(ltm.tm_mon), &(ltm.tm_mday),
703 &(ltm.tm_hour), &(ltm.tm_min), &(ltm.tm_sec),
704 &zz) != 7) || (zz !=
'Z')) {
714 if (ltm.tm_year < 50) {
716 }
else if (ltm.tm_year < 100) {
725 etime = mktime(<m);
740#ifndef USEX509NAMEONELINE
741 BIO *mbio = BIO_new(BIO_s_mem());
742 X509_NAME_print_ex(mbio, nm, 0, XN_FLAG_SEP_MULTILINE);
744 long len = BIO_get_mem_data(mbio, &data);
750 char *xn = X509_NAME_oneline(nm, 0, 0);
void XrdCryptosslNameOneLine(X509_NAME *nm, XrdOucString &s)
int XrdCryptosslX509ParseBucket(XrdSutBucket *b, XrdCryptoX509Chain *chain)
int XrdCryptosslKDFunLen()
int XrdCryptosslX509ToFile(XrdCryptoX509 *x509, FILE *file, const char *fname)
int XrdCryptosslX509VerifyCB(int ok, X509_STORE_CTX *ctx)
int XrdCryptosslX509ChainToFile(XrdCryptoX509Chain *ch, const char *fn)
XrdSutBucket * XrdCryptosslX509ExportChain(XrdCryptoX509Chain *chain, bool withprivatekey)
int XrdCryptosslX509ParseStack(XrdTlsPeerCerts *pc, XrdCryptoX509Chain *chain)
int XrdCryptosslKDFun(const char *pass, int plen, const char *salt, int slen, char *key, int klen)
int XrdCryptosslX509ParseFile(const char *fname, XrdCryptoX509Chain *chain, const char *fkey)
time_t XrdCryptosslASN1toUTC(const ASN1_TIME *tsn1)
bool XrdCryptosslX509VerifyChain(XrdCryptoX509Chain *chain, int &errcode)
static int gErrVerifyChain
bool XrdCryptosslX509VerifyCert(XrdCryptoX509 *cert, XrdCryptoX509 *ref)
virtual XrdCryptoRSAdata Opaque()
void PushBack(XrdCryptoX509 *c)
XrdCryptoX509 * End() const
XrdCryptoX509 * SearchBySubject(const char *subject, ESearchMode mode=kExact)
virtual const char * Subject()
virtual void SetPKI(XrdCryptoX509data pki)
virtual XrdCryptoX509data Opaque()
virtual XrdCryptoRSA * PKI()
virtual const char * SubjectHash(int)
virtual const char * IssuerHash(int)
virtual const char * Issuer()
void insert(const int i, int start=-1)
int replace(const char *s1, const char *s2, int from=0, int to=-1)
int SetBuf(const char *nb=0, int ns=0)
X509 * getCert(bool upref=true)