Php get subjectAltName from csr - php-openssl

Good evening my friend
I generated san csr via openssl function using php. But i cant get subjectAltName. Please How can i extract subjetAltName from csr?
shell_exec('openssl req -noout -text -in'. $csr)

I hope this helps:
$r = shell_exec('openssl req -noout -text -in '. $csr)
^- a space here!
for my test however openssl did not print a Subject Alt Name, so i'll show how to get the Subject:
preg_match('/Subject:\s*(.*)\n/', $r, $matches);
$subject = $matches[1];

Related

Cannot get any results with Net::SSLeay for openssl

I am trying to create a script that looks at the data for my certs in my ~/ssl/certs folder and displays the issuer information to me.
It's written in perl and it would be easy to just say:
$data = `/usr/bin/openssl x509 -in $file -noout -issuer`
However that is not very portable. I am trying to use Net::SSLeay instead to get the same output, however all I can seem to manage are checksum numbers, what am I missing? Here is what I got
#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;
use Net::SSLeay qw(die_now die_if_ssl_error);
Net::SSLeay::load_error_strings();
Net::SSLeay::SSLeay_add_ssl_algorithms(); # Important!
Net::SSLeay::ENGINE_load_builtin_engines(); # If you want built-in engines
Net::SSLeay::ENGINE_register_all_complete(); # If you want built-in engines
Net::SSLeay::randomize();
Net::SSLeay::library_init();
Net::SSLeay::OpenSSL_add_all_algorithms();
my $file = '~/ssl/certs/certificate.crt';
my $x509 = Net::SSLeay::X509_new();
Net::SSLeay::X509_free($x509);
my $type = Net::SSLeay::X509_certificate_type($x509);
my $ctx = Net::SSLeay::CTX_new_with_method(Net::SSLeay::TLSv1_method());
my $test = Net::SSLeay::X509_load_cert_file( $ctx, $file, $type );
my $info = Net::SSLeay::X509_issuer_name_hash($x509);
say "\nInfo = $info \nX509 = $x509\nTest= $test\nType = $type\nCTX = $ctx";
This is my output:
Info = 4003674586
X509 = 16119648
Test= 0
Type = 0
CTX = 16137888
I've read through all the source code and the documentation, none of it makes any sense.
You don't need all this context etc. After you've initialized the SSL library you can simply do:
my $bio = Net::SSLeay::BIO_new_file($file,'r') or die $!;
my $cert = Net::SSLeay::PEM_read_bio_X509($bio);
Net::SSLeay::BIO_free($bio);
$cert or die "cannot parse $file as PEM X509 cert: ".
Net::SSLeay::ERR_error_string(Net::SSLeay::ERR_get_error());
my $issuer = Net::SSLeay::X509_NAME_oneline(
Net::SSLeay::X509_get_issuer_name($cert));
This example may be able to get you started:
https://metacpan.org/source/MIKEM/Net-SSLeay-1.68/examples/x509_cert_details.pl
I've snipped the relevant bits from it to just get the issuer:
#!/usr/bin/perl
use strict;
use warnings;
use Net::SSLeay qw/XN_FLAG_RFC2253 ASN1_STRFLGS_ESC_MSB/;
Net::SSLeay::randomize();
Net::SSLeay::load_error_strings();
Net::SSLeay::ERR_load_crypto_strings();
Net::SSLeay::SSLeay_add_ssl_algorithms();
my $file = shift;
chomp($file);
my $bio = Net::SSLeay::BIO_new_file($file, 'rb') or die "ERROR: BIO_new_file failed";
my $x509 = Net::SSLeay::PEM_read_bio_X509($bio) or die "ERROR: PEM_read_bio_X509 failed";
my $issuer_name = Net::SSLeay::X509_get_issuer_name($x509);
print Net::SSLeay::X509_NAME_print_ex($issuer_name) . "\n";
So then:
$ perl ssl.pl /usr/share/ca-certificates/mozilla/XRamp_Global_CA_Root.crt
CN=XRamp Global Certification Authority,O=XRamp Security Services Inc,OU=www.xrampsecurity.com,C=US

attachment displaying full path when emailed using perl script

My perl script emails attachment which displays full path (/abc/xyz/report.xls) in outlook email client but not in lotus notes email client.
environment: 2010 Outlook on windows 7 OS.
How do I make sure path is not displayed in the attachment?
Code Snippet:
&sendEmail(#subject, #message, $exception_report_xls);
sub sendEmail
{
my $subject = "#subject";
my $distlist = 'ayc#abc.com';
my $message ="\n\n\n#message \n";
my $send = system("\(echo \"$message\" ; uuencode \"$exception_report_xls\" \"$exception_report_xls\"\) | mailx -s \"$subject\" \"$distlist\"");
}
Appreciate any resonse!
Ramya.
You are writing the full path and filename of the XLS file in the source of the message. So, some mail clients will display the full path.
If you are wedded to using (antiquated) UUENCODE, try it this way instead:
sub Send_Email_UUENCODE {
local($to, $from, $subj, $message, $attach) = #_;
local($i,$filename,$cmd,$att,$mailprog);
$i=length($attach)-1;
while( (substr($attach,$i,1) cmp '/')!=0 && $i>0) {$i--;}
$filename=substr($attach,$i+1,length($attach)-$i-1);
if ($attach) {
$cmd="uuencode " . $attach . ' ' . $filename;
$att=`$cmd`;
}
$mailprog = "/usr/sbin/sendmail -f " . $from;
open (MAIL, "|$mailprog $to") || die "Can't open $mailprog!\n";
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subj\n";
print MAIL "$message\n";
if ($attach) {
print MAIL "$att\n";
}
close (MAIL);
}
But, better might be to use MIME encoding instead of UUENCODE.

Generate valid Unix username and password in Perl

I need to generate a username and password pair in order to create user on a Unix system, preferably a pair that would be hard to guess (for security). Does anyone know any good way of doing this using Perl?
UPDATE
So I kind of combined some of the answers below and did some of my own stuff, and this it what I ended up getting. Now these generated usernames and passwords are kind of cryptic and not easy to remember, which is what I was looking for. However, if you're looking for more readable generation, you'll have to tweak it to your liking.
use String::Random qw(random_regex);
use Data::Random::WordList;
my $user = random_regex('\w{3,6}\d{3,6}\w{3,7}') . time . int(rand(1000));
my $wl = new Data::Random::WordList( wordlist => '/usr/share/dict/words' );
my #rand_words = $wl->get_words(int(rand(3)) + 3);
$wl->close();
my $pass = join " ", #rand_words;
$pass .= ' ' . int(rand(1000)) . time;
my $crypt_pass = crypt($pass,'salt');
system "useradd -p $crypt_pass $user";
#you can now login with $user and $pass on your system
For generating password You can use:
$password = `date | md5sum | fold -w 10 | head -n 1`;
$password = crypt($password,'some_string');
It's actually more Bash than Perl but it does the job.
From http://sysadminsjourney.com/content/2009/09/16/random-password-generation-perl-one-liner/
perl -le 'print map { (a..z,A..Z,0..9)[rand 62] } 0..pop' 8
Will generate a random 8 character string from the characters a-z, A-Z and 0-9 .
You should be able to modify this to generate the username and password.
Username would be:
perl -le 'print map { ([a-z_][a-z0-9_][rand 62] } 0..pop' 30
The password would be similar, but you would need to use the passwd command to actually set it to the user. Not sure if Perl has an equivalent.
As mentioned by others , its more BASH than perl. Here is my way of generating the password:
passwd=`date +%s | sha256sum | base64 | head -1 | cut -c 1-8`

How do I set the name of an email sender via PHP

So I want the from field when an email is opened to be something like
"Jack Sparrow Via somesite" as opposed to an explicit email address.
I wondering how to set this in PHP's mail() function?
You can accomplish this by using basic headers.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Jack Sparrow <jsparrow#blackpearl.com>' . PHP_EOL .
'Reply-To: Jack Sparrow <jsparrow#blackpearl.com>' . PHP_EOL .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
You have to set your headers:
$to = "Someone#email.com";
$header = "FROM: Jack Sparrow <some#site.com>\r\n";
$message = "Your message here.";
$subject = "Your subject";
mail($to,$subject,$message,$header) or die();
just use like this
$headers .= 'From: [Name of the person]'.'<'. $this->from .'>'. "\n";
in Header Section.
If you use PHP Mailer from GitHub, then you do it by:
$mail->SetFrom("info#mibckerala.org", "MIBC");
$to = "Someone#email.com";
$message = "Your message here.";
$subject = "Your subject";
mail($to,$subject,$message,$header, '-f some#site.com -F "Jack Sparrow"') or die();
Just add the -f parameter to provide the sender's email and -F parameter to provide the sender's name to the mail(), all as a string.
Note: double quotes around "Jack Sparrow"
Assuming that you are using sendmail on a Linux machine:
You can find more detailed information by typing "man sendmail" in your shell.
I am not sure exactly what you mean, but as you can see on PHP.net mail function.
To add the name to the from section you need to send this in the headers.
From:Name<email#example.com>

How to send simple text file as attachment using HP-UX shell script?

I need to send an email with a text file as attachment using shell script in HP-UX; I don't have mutt installed.
I am using following command but it sends the file content in body of email, I want it as an attachment.
mailx -s "Report" name#example.com < file.txt
I wrote this ksh function a few years ago
# usage: email_attachment to cc subject body attachment_filename
email_attachment() {
to="$1"
cc="$2"
subject="$3"
body="$4"
filename="${5:-''}"
boundary="_====_blah_====_$(date +%Y%m%d%H%M%S)_====_"
{
print -- "To: $to"
print -- "Cc: $cc"
print -- "Subject: $subject"
print -- "Content-Type: multipart/mixed; boundary=\"$boundary\""
print -- "Mime-Version: 1.0"
print -- ""
print -- "This is a multi-part message in MIME format."
print -- ""
print -- "--$boundary"
print -- "Content-Type: text/plain; charset=ISO-8859-1"
print -- ""
print -- "$body"
print -- ""
if [[ -n "$filename" && -f "$filename" && -r "$filename" ]]; then
print -- "--$boundary"
print -- "Content-Transfer-Encoding: base64"
print -- "Content-Type: application/octet-stream; name=$filename"
print -- "Content-Disposition: attachment; filename=$filename"
print -- ""
print -- "$(perl -MMIME::Base64 -e 'undef $/; open $fh, shift; print MIME::Base64::encode(<$fh>); close $fh; ' $filename)"
print -- ""
fi
print -- "--${boundary}--"
} | /usr/lib/sendmail -oi -t
}
uuencode is your friend.
Here is a tested example:
(uuencode .vimrc vimrc.txt; uuencode .zshrc zshrc.txt; echo Here are your attachments) | mailx -s 'Mail with attachments' email_address
I was having the same problem where the output of uuencode was being sent as part of the message body rather than as an attached file (at least when using Outlook 2010 to view the sent mail). I found the answer in this thread http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html
Adding -m causes mailx to not add MIME header lines when sending email. The OP's command would be altered to look like:
mailx -m -s "Report" name#example.com < file.txt
I also encountered the same problem few months ago.
The command I needed was ux2dos
( cat message_content_file; ux2dos file.txt | uuencode file.txt file.txt ) | mailx -m -s "subject" -r mail#sender mail#recipient
I hope it can help !
Regards