/** * Gets the Base64 encoded public key that forms part of this instance's identity keypair. * @return the Base64 encoded public key * @since 2.16 */ @Nullable public String getIdentityPublicKey() { RSAPublicKey key = InstanceIdentityProvider.RSA.getPublicKey(); return key == null ? null : new String(Base64.encodeBase64(key.getEncoded()), Charset.forName("UTF-8")); }
/** * Gets base64-encoded public key. */ public String getEncodedPublicKey() { return new String(Base64.encodeBase64(getPublicKey().getEncoded())); } }
/** * Returns the PEM encoded public key. * * @return the PEM encoded public key. */ public String getPublicKey() { RSAPublicKey key = InstanceIdentityProvider.RSA.getPublicKey(); if (key == null) { return null; } byte[] encoded = Base64.encodeBase64(key.getEncoded()); int index = 0; StringBuilder buf = new StringBuilder(encoded.length + 20); while (index < encoded.length) { int len = Math.min(64, encoded.length - index); if (index > 0) { buf.append("\n"); } buf.append(new String(encoded, index, len, Charsets.UTF_8)); index += len; } return String.format("-----BEGIN PUBLIC KEY-----%n%s%n-----END PUBLIC KEY-----%n", buf.toString()); }
@Override public String toString() { return "RsaCipher [privateKey=" + new String(privateKey.getEncoded()) + ", publicKey=" + new String(publicKey.getEncoded()) + "]"; }
/** * Returns the fingerprint of the public key. * * @return the fingerprint of the public key. */ public String getFingerprint() { RSAPublicKey key = InstanceIdentityProvider.RSA.getPublicKey(); if (key == null) { return null; } // TODO replace with org.jenkinsci.remoting.util.KeyUtils once JENKINS-36871 changes are merged try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.reset(); byte[] bytes = digest.digest(key.getEncoded()); StringBuilder result = new StringBuilder(Math.max(0, bytes.length * 3 - 1)); for (int i = 0; i < bytes.length; i++) { if (i > 0) { result.append(':'); } int b = bytes[i] & 0xFF; result.append(Character.forDigit((b>>4)&0x0f, 16)).append(Character.forDigit(b&0xf, 16)); } return result.toString(); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("JLS mandates MD5 support"); } } }
public void setKeyPair(KeyPair keyPair) { PrivateKey privateKey = keyPair.getPrivate(); Assert.state(privateKey instanceof RSAPrivateKey, "KeyPair must be an RSA "); signer = new RsaSigner((RSAPrivateKey) privateKey); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); verifier = new RsaVerifier(publicKey); verifierKey = "-----BEGIN PUBLIC KEY-----\n" + new String(Base64.encode(publicKey.getEncoded())) + "\n-----END PUBLIC KEY-----"; }
StringWriter writer = new StringWriter(); PemWriter pemWriter = new PemWriter(writer); pemWriter.writeObject(new PemObject("PUBLIC KEY", pub.getEncoded())); pemWriter.flush(); pemWriter.close(); result.put("publickey_DER_BASE64", Base64.getEncoder().encodeToString(pub.getEncoded())); result.put("publickey_PEM", pubkey_pem); result.put("keyhash", IO.getKeyHash(pub));
@Override public String toString() { return "RsaCipher [publicKey=" + new String(publicKey.getEncoded()) + "]"; }
@Override public void serialize(final Type type, final Object object, final DataOutputStream dos) throws IOException { final RSAPublicKey key = (RSAPublicKey) object; final byte[] encoded = key.getEncoded(); dos.writeInt(encoded.length); dos.write(encoded); }
/** * Base64 encodes a public key with no chunking. * @param publicKey The public key * @return Base64 representation */ public static String base64EncodeNoChunking(RSAPublicKey publicKey) { return new String( Base64.encodeBase64(publicKey.getEncoded(), false), StandardCharsets.UTF_8); }
/** * Gets base64-encoded public key. */ public String getEncodedPublicKey() { return new String(Base64.encodeBase64(getPublicKey().getEncoded())); } }
/** * Gets the Base64 encoded public key that forms part of this instance's identity keypair. * @return the Base64 encoded public key * @since 2.16 */ @Nullable public String getIdentityPublicKey() { RSAPublicKey key = InstanceIdentityProvider.RSA.getPublicKey(); return key == null ? null : new String(Base64.encodeBase64(key.getEncoded()), Charset.forName("UTF-8")); }
public void setKeyPair(KeyPair keyPair) { PrivateKey privateKey = keyPair.getPrivate(); Assert.state(privateKey instanceof RSAPrivateKey, "KeyPair must be an RSA "); signer = new RsaSigner((RSAPrivateKey) privateKey); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); verifier = new RsaVerifier(publicKey); verifierKey = "-----BEGIN PUBLIC KEY-----\n" + new String(Base64.encode(publicKey.getEncoded())) + "\n-----END PUBLIC KEY-----"; }
public static String getInstanceId() { try { if( _id == null ) { _id = Util.getDigestOf( new ByteArrayInputStream(InstanceIdentity.get().getPublic().getEncoded())); } } catch (IOException e) { LOG.error("Could not get Jenkins instance ID."); _id = ""; } return _id; }
public String getEncodedRsaPublicKey() { return CodecUtils.encodeBase64(this.getRsaPublicKey().getEncoded()); }
/** * Append some tags to identify who created this container. */ protected static void insertLabels(CreateContainerCmd containerConfig, Run run) { // add tags Map<String, String> labels = containerConfig.getLabels(); if (labels == null) labels = new HashMap<>(); labels.put("YAD_PLUGIN", DockerShellStep.class.getName()); labels.put("JENKINS_JOB_BASE_NAME", run.getParent().getName()); labels.put("JENKINS_INSTANCE_IDENTITY", new String(encodeBase64(InstanceIdentity.get().getPublic().getEncoded()), UTF_8)); containerConfig.withLabels(labels); }
@Override public SlaveInstaller createIfApplicable(Channel c) throws IOException, InterruptedException { if (c.call(new HasSystemd())) { RSAPublicKey key = id.getPublic(); String instanceId = Util.getDigestOf(new String(Base64.encodeBase64(key.getEncoded()))).substring(0,8); return new SystemdSlaveInstaller(instanceId); } return null; }
@Override public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException { RSAPublicKey key = new InstanceIdentity().getPublic(); rsp.setStatus(HttpServletResponse.SC_OK); rsp.setHeader(X_INSTANCE_IDENTITY, new String(encodeBase64(key.getEncoded()), UTF_8)); } });
private static JcaX509v3CertificateBuilder addJcaX509Extension(String commonsName, RSAPublicKey publicKey, X509Certificate issuerCertificate, long duration, boolean isCaCertificate) throws NoSuchAlgorithmException, CertIOException { long end = System.currentTimeMillis() + duration; BigInteger serial = BigInteger.valueOf(new SecureRandom(publicKey.getEncoded()).nextLong()); JcaX509v3CertificateBuilder certificateBuilder = new JcaX509v3CertificateBuilder(new org.bouncycastle.asn1.x500.X500Name(issuerCertificate.getSubjectDN().getName()), serial, new Date(), new Date(end), new org.bouncycastle.asn1.x500.X500Name(COMMON_NAME_ENTRY + commonsName), publicKey); JcaX509ExtensionUtils jcaX509ExtensionUtils = new JcaX509ExtensionUtils(); certificateBuilder.addExtension(subjectKeyIdentifier, false, jcaX509ExtensionUtils.createSubjectKeyIdentifier(publicKey)); certificateBuilder.addExtension(basicConstraints, isCaCertificate, new BasicConstraints(isCaCertificate)); return certificateBuilder; }
@Override public SlaveInstaller createIfApplicable(Channel c) throws IOException, InterruptedException { if (c.call(new Predicate())) { RSAPublicKey key = id.getPublic(); String instanceId = Util.getDigestOf(new String(Base64.encodeBase64(key.getEncoded()))).substring(0,8); return new LaunchdSlaveInstaller(instanceId); } return null; }