Skip to content

Instantly share code, notes, and snippets.

View markscottwright's full-sized avatar

Mark Wright markscottwright

  • Washington, DC
View GitHub Profile
@markscottwright
markscottwright / gist:5f20bfd3be83f7e6280e0a84e16736cc
Last active October 19, 2017 19:01
How to create a self-signed certificate in Python

How to create a self-signed certificate in Python

I used to have a Word Press blog (I suppose I still do) and this was by far my most popular post. (A quick google search showed my code in a bunch of projects).

Often times, you need a keypair and certificate for a website, but you don't need it to be signed by a recognized CA. Here's how to do that in python. Note that the method below isn't the most current, since it's using the common name component of the certificate's Subject as the hostname, instead of the Subject Alternative Name. See rfc2818 for more information.

from socket import gethostname
from OpenSSL import crypto
@markscottwright
markscottwright / convert to pkcs 8.md
Last active June 23, 2022 14:06
How to convert a java private key from PKCS#1 encoding to PKCS#8

I had some historical key material data in pkcs#1 format that needed to be in pkcs#8 for input into another system. Here's how to do it, using BouncyCastle:

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.DERObject;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import java.security.PrivateKey;
@markscottwright
markscottwright / basepkcs11.cpp
Created October 31, 2017 13:46
PKCS11 skeleton
/*
* This is a skeleton for a pkcs11 application that takes care of loading the named pkcs11 library and getting
* the list of pkcs11 functions.
*/
#include <iostream>
#include <iomanip>
#include <windows.h>
// get this from https://www.cryptsoft.com/pkcs11doc/STANDARD/include/v220
// also get pkcs11t.h, pkcs11f.h, pkcs11.h
@markscottwright
markscottwright / UsernamePasswordAdder.java
Created January 24, 2018 21:38
How to supply username and password to a WS-Security 1.0 web service using the JAX-WS RI
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
@markscottwright
markscottwright / gist:d3330f76e89ee5cc0e51c155920285ff
Last active April 16, 2024 20:41
How to verify a detached pkcs7 signature
# how to verify the signature if you have the CAs certificate. This doesn't seem to work if you specify
# a subordinate CA, even if that CA is the one that issued the cert that created the signature.
openssl smime -verify -inform der -in signature-file -content signed-file -CAfile ca-certificate-in-pem-format
# how to verify everything except the certificate - so the signatures are checked, but no attempt is made
# to verify that the CAs certificate is trusted
openssl smime -verify -noverify -inform der -in signature-file -content signed-file
@markscottwright
markscottwright / tableformat.vim
Created April 6, 2018 19:02
How to format a org-mode style table in Vim
# how to format a table in VIM
function! Strip(input_string)
return substitute(a:input_string, '^\s*\(.\{-}\)\s*$', '\1', '')
endfunction
function! GetFields(line_num)
let cur_line = Strip(getline(a:line_num))
let maybe_padded = split(cur_line, "|")
let fields = []
@markscottwright
markscottwright / test.py
Created August 23, 2018 17:45
How to log SQL in Django Unit Tests
import logging
from django.conf import settings
logging.getLogger('django.db.backends').setLevel(logging.DEBUG)
logging.getLogger('django.db.backends').addHandler(logging.StreamHandler())
settings.DEBUG = True
@markscottwright
markscottwright / AIAFetcher.java
Created November 27, 2018 14:50
Get the CA Issuer URLs from a X509Cert in java
import java.io.IOException;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.x509.AccessDescription;
@markscottwright
markscottwright / DropTarget.java
Last active September 24, 2021 18:43
How to accept dropped files in Java Swing
package scratch;
import java.awt.BorderLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@markscottwright
markscottwright / SignedOfficeDocumentCerts.java
Created December 12, 2018 14:48
How to retrieve the signing certs and any attached chain of trust from a signed Office document.
package scratch;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.ArrayList;