def __init__(self, privateKeyString):
     """
     Initialize object with an RSA or EC private key in JWK or PEM format
     
     Signature algorithms are assumed to be given in the IETF JOSE format
     
     This class is essentially a wrapper over the currently disparate Python
     EC and RSA libraries, not limited to JSON or JCS
     """
     if '"kty"' in privateKeyString:
         jwk = parseJson(privateKeyString)
         keyType = jwk['kty']
         if keyType == 'RSA':
             self.nativePrivateKey = RSA.construct([
                 cryptoBigNumDecode(jwk['n']),
                 cryptoBigNumDecode(jwk['e']),
                 cryptoBigNumDecode(jwk['d']),
                 cryptoBigNumDecode(jwk['p']),
                 cryptoBigNumDecode(jwk['q'])
             ])
             """ JWK syntax checking... """
             cryptoBigNumDecode(jwk['dp'])
             cryptoBigNumDecode(jwk['dq'])
             cryptoBigNumDecode(jwk['qi'])
         elif keyType == 'EC':
             self.nativePrivateKey = EC.from_string(
                 base64UrlDecode(jwk['d']), getEcCurve(jwk['crv']))
         else:
             raise ValueError('Unsupported key type: "' + keyType + '"')
     else:
         if ' RSA ' in privateKeyString:
             self.nativePrivateKey = RSA.importKey(privateKeyString)
         else:
             self.nativePrivateKey = EC.from_pem(privateKeyString)
     """
     Set default signature algorithm
     """
     if self.isRSA():
         self.algorithm = 'RS256'
     else:
         self.algorithm = 'ES256'
Example #2
0

def readFile(name):
    return codecs.open(name, "r", "utf-8").read()


keyString = readFile(sys.argv[1])

signatureKey = SignatureKey.new(keyString)
if signatureKey.isRSA():
    print "RSA key"
else:
    print "EC key"

if len(sys.argv) == 3:
    jsonObject = JSONObjectWriter(parseJson(readFile(sys.argv[2])))
else:
    jsonObject = JSONObjectWriter()
    jsonObject.setInt("an_int", 7)

    jsonObject.setString("a_string", "Sure")
    jsonObject.setObject("an_object").setString(
        "another_string",
        "Yeah").setFloat("a_float", 1e+5).setBinary("a_blob",
                                                    '\x00\x01\x03\x04\x05')
    jsonObject.setArray("an_array").setInt(45).setString("Nope").setObject()
    jsonObject.setArray("two_dimensional").setArray().setString("Bye")

jsonObject.setSignature(signatureKey)

print jsonObject.serialize().encode("utf-8")
Example #3
0
# Our test program
if not len(sys.argv) in (2,3):
    print 'Private-key [JSON-in-file]'
    sys.exit(1)

def readFile(name):
  return codecs.open(name, "r", "utf-8").read()

keyString = readFile(sys.argv[1])

signatureKey = SignatureKey.new(keyString)
if signatureKey.isRSA():
  print "RSA key"
else:
  print "EC key"

if len(sys.argv) == 3:
  jsonObject = JSONObjectWriter(parseJson(readFile(sys.argv[2])))
else:
  jsonObject = JSONObjectWriter()
  jsonObject.setInt("an_int", 7)

  jsonObject.setString("a_string", "Sure")
  jsonObject.setObject("an_object").setString("another_string","Yeah").setFloat("a_float",1e+5).setBinary("a_blob",'\x00\x01\x03\x04\x05')
  jsonObject.setArray("an_array").setInt(45).setString("Nope").setObject()
  jsonObject.setArray("two_dimensional").setArray().setString("Bye")

jsonObject.setSignature(signatureKey)

print jsonObject.serialize().encode("utf-8")
Example #4
0
from org.webpki.json import JCSValidator

from org.webpki.json.Utils import parseJson

# Our test program
if len(sys.argv) != 2:
    print 'No input file given'
    sys.exit(1)

# There should be a file with utf-8 json in, read and parse it
jsonString = codecs.open(sys.argv[1], "r", "utf-8").read()

# print jsonString

def checkAllSignatures(jsonObject):
    for w in jsonObject:
        if isinstance(jsonObject[w],collections.OrderedDict):
            checkAllSignatures(jsonObject[w])
    if w == 'signature':
        validator = JCSValidator.new(jsonObject)
        print 'PEM=\n' + validator.getPublicKey('PEM') + 'JWK=\n' + validator.getPublicKey('JWK')

# Just check the outer signature
jsonObject = parseJson(jsonString)
JCSValidator.new(jsonObject)
print 'Valid (since it didn\'t raise an exception)'

# For fun we can traverse the entire object and look for inner signatures as well 
checkAllSignatures(jsonObject)

Example #5
0
from org.webpki.json.Utils import parseJson

# Our test program
if len(sys.argv) != 2:
    print('No input file given')
    sys.exit(1)

# There should be a file with utf-8 json in, read and parse it
jsonString = codecs.open(sys.argv[1], "r", "utf-8").read()

# print jsonString


def checkAllSignatures(jsonObject):
    for w in jsonObject:
        if isinstance(jsonObject[w], collections.OrderedDict):
            checkAllSignatures(jsonObject[w])
    if w == 'signature':
        validator = JCSValidator.new(jsonObject)
        print('JWK=\n' + validator.getPublicKey('JWK'))
        print('PEM=\n' + validator.getPublicKey('PEM'))


# Just check the outer signature
jsonObject = parseJson(jsonString)
JCSValidator.new(jsonObject)
print('Valid (since it didn\'t raise an exception)')

# For fun we can traverse the entire object and look for inner signatures as well
checkAllSignatures(jsonObject)