<commit_before>#----------------------------------------------------------------------
# Copyright (c) 2011-2013 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
#----------------------------------------------------------------------

import sfa.trust.certificate
import subprocess
import os
import tempfile
from chapi_log import *

# A set of utilities to pull infomration out of X509 certs

# Pull the certificate from the speaks-for credential
def get_cert_from_credential(cred):
    start_tag = '<X509Certificate>'
    end_tag = '</X509Certificate>'
    start_index = cred.find(start_tag)
    end_index = cred.find(end_tag)
    raw_cert = cred[start_index+len(start_tag):end_index]
    cert_string = '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----' % raw_cert
    return cert_string

# Pull out the UUID from the certificate
def get_uuid_from_cert(cert):
    cert_object = sfa.trust.certificate.Certificate(string=cert)
    subject_alt_names = cert_object.get_extension('subjectAltName')
    san_parts = subject_alt_names.split(',')
    uuid = None
    for san_part in san_parts:
        san_part = san_part.strip()
        if san_part.startswith('URI:urn:uuid'):
            uuid = san_part[13:]
            break
    # The MA cert has the uuid in the field URI:uuid (which is wrong)
    if uuid is None:
        for san_part in san_parts:
            san_part = san_part.strip()
            if san_part.startswith('URI:uuid'):
                uuid = san_part[9:]
                break
    return uuid

# Pull out the URN from the certificate
def get_urn_from_cert(cert):
    cert_object = sfa.trust.certificate.Certificate(string=cert)
    subject_alt_names = cert_object.get_extension('subjectAltName')
    san_parts = subject_alt_names.split(',')
    urn = None
    for san_part in san_parts:
        san_part = san_part.strip()
        if san_part.startswith('URI:urn:publicid'):
            urn = san_part[4:]
            break
    return urn

# Pull out the email from the certificate
def get_email_from_cert(cert):
    cert_object = sfa.trust.certificate.Certificate(string=cert)
    subject_alt_names = cert_object.get_extension('subjectAltName')
    san_parts = subject_alt_names.split(',')
    email = None
    for san_part in san_parts:
        san_part = san_part.strip()
        if san_part.startswith('email:'):
            email = san_part[6:]
            break
    return email

# Pull the object name fro the URN
# It is the part after the last +
def get_name_from_urn(urn):
    parts = urn.split("+")<commit_msg>check for nll URN in util function<commit_after>#----------------------------------------------------------------------
# Copyright (c) 2011-2013 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
#----------------------------------------------------------------------

import sfa.trust.certificate
import subprocess
import os
import tempfile
from chapi_log import *

# A set of utilities to pull infomration out of X509 certs

# Pull the certificate from the speaks-for credential
def get_cert_from_credential(cred):
    start_tag = '<X509Certificate>'
    end_tag = '</X509Certificate>'
    start_index = cred.find(start_tag)
    end_index = cred.find(end_tag)
    raw_cert = cred[start_index+len(start_tag):end_index]
    cert_string = '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----' % raw_cert
    return cert_string

# Pull out the UUID from the certificate
def get_uuid_from_cert(cert):
    cert_object = sfa.trust.certificate.Certificate(string=cert)
    subject_alt_names = cert_object.get_extension('subjectAltName')
    san_parts = subject_alt_names.split(',')
    uuid = None
    for san_part in san_parts:
        san_part = san_part.strip()
        if san_part.startswith('URI:urn:uuid'):
            uuid = san_part[13:]
            break
    # The MA cert has the uuid in the field URI:uuid (which is wrong)
    if uuid is None:
        for san_part in san_parts:
            san_part = san_part.strip()
            if san_part.startswith('URI:uuid'):
                uuid = san_part[9:]
                break
    return uuid

# Pull out the URN from the certificate
def get_urn_from_cert(cert):
    cert_object = sfa.trust.certificate.Certificate(string=cert)
    subject_alt_names = cert_object.get_extension('subjectAltName')
    san_parts = subject_alt_names.split(',')
    urn = None
    for san_part in san_parts:
        san_part = san_part.strip()
        if san_part.startswith('URI:urn:publicid'):
            urn = san_part[4:]
            break
    return urn

# Pull out the email from the certificate
def get_email_from_cert(cert):
    cert_object = sfa.trust.certificate.Certificate(string=cert)
    subject_alt_names = cert_object.get_extension('subjectAltName')
    san_parts = subject_alt_names.split(',')
    email = None
    for san_part in san_parts:
        san_part = san_part.strip()
        if san_part.startswith('email:'):
            email = san_part[6:]
            break
    return email

# Pull the object name from the URN
# It is the part after the last +
def get_name_from_urn(urn):
    if urn is None:
        return None
    parts = urn.split("+")
{'fraction_non_alphanumeric': 0.11934214225470903, 'fraction_numerical': 0.006184987348889514, 'mean_word_length': 2.8605534454693435, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5077055', 'n_tokens_mistral': 2108, 'n_tokens_neox': 2008, 'n_words': 711}